<?php
// csv.php
// Include file for Google Charts csv Spreadsheet integration purposes
// RJM Programming
// October, 2015
$csv_file_content="";
$csvprefix="";
if (isset($_GET['label'])) $csvprefix.=$_GET['label'];
if (isset($_POST['label'])) $csvprefix.=$_POST['label'];
if (isset($_GET['value'])) $csvprefix.="~" . $_GET['value'];
if (isset($_POST['value'])) $csvprefix.="~" . $_POST['value'];
$csvprefix=str_replace("~", ",",str_replace("~,", ",",str_replace(",~", ",",$csvprefix)));
$ioc="";
if (isset($_GET['onclick'])) $ioc=$_GET['onclick'];
if (isset($_POST['onclick'])) $ioc=$_POST['onclick'];
$csvfile="";
if (isset($_GET['csv'])) $csvfile=$_GET['csv'];
if (isset($_POST['csv'])) $csvfile=$_POST['csv'];
$datais="";
if (isset($_GET['data'])) $datais=$_GET['data'];
if (isset($_POST['data'])) $datais=$_POST['data'];
if (strlen($ioc) > 0 && strlen($csvfile) > 0 && strpos(strtolower($csvfile) . "~", ".csv~") !== false) {
  $csv_file_content=str_replace('~','"',str_replace("[","",str_replace("]","",str_replace(", [","\n",str_replace(",[","\n",urldecode($csvprefix . $datais))))));
  if (strpos($csv_file_content," ") === false) $csv_file_content=str_replace("'","",$csv_file_content);
  if (1 == 2) file_put_contents($csvfile0, $csv_file_content);
  // Thanks to http://stackoverflow.com/questions/4348802/how-can-i-output-a-utf-8-csv-in-php-that-excel-will-read-properly
  if (strlen($csv_file_content) > 0) {
   header('Content-Description: File Transfer');
   header('Content-Type: application/octet-stream');
   $hdr='Content-Disposition: attachment; filename=' . $csvfile;
   header($hdr);
   header('Content-Transfer-Encoding: binary');
   header('Expires: 0');
   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
   header('Pragma: public');
   echo "\xEF\xBB\xBF"; // UTF-8 BOM
   echo $csv_file_content;
   exit();
  }
}
$csvfile0="";
if (isset($_GET['csv0'])) $csvfile0=$_GET['csv0'];
if (isset($_POST['csv0'])) $csvfile0=$_POST['csv0'];
$datais0="";
if (isset($_GET['data0'])) $datais0=$_GET['data0'];
if (isset($_POST['data0'])) $datais0=$_POST['data0'];
if (strlen($ioc) > 0 && strlen($csvfile0) > 0 && strpos(strtolower($csvfile0) . "~", ".csv~") !== false) {
  $csv_file_content=str_replace('~','"',str_replace("[","",str_replace("]","",str_replace(", [","\n",str_replace(",[","\n",urldecode($csvprefix . $datais0))))));
  if (strpos($csv_file_content," ") === false) $csv_file_content=str_replace("'","",$csv_file_content);
  if (1 == 2) file_put_contents($csvfile0, $csv_file_content);
  // Thanks to http://stackoverflow.com/questions/4348802/how-can-i-output-a-utf-8-csv-in-php-that-excel-will-read-properly
  if (strlen($csv_file_content) > 0) {
   header('Content-Description: File Transfer');
   header('Content-Type: application/octet-stream');
   $hdr='Content-Disposition: attachment; filename=' . $csvfile0;
   header($hdr);
   header('Content-Transfer-Encoding: binary');
   header('Expires: 0');
   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
   header('Pragma: public');
   echo "\xEF\xBB\xBF"; // UTF-8 BOM
   echo $csv_file_content;
   exit();
  }
}
?>
