<?php
// csv.php
// Include file for Google Charts csv Spreadsheet integration purposes
// RJM Programming
// October, 2015
function datefixes($inch) {
  $ouch=$inch;
  $dbits=explode("new Date(", $inch);
  $dtwobits=explode("newDate(", $inch);
  if (sizeof($dbits) > 1) {
    if (substr($ouch, 0, 1) == ",") $ouch="Date" . $ouch;
    for ($i=(sizeof($dbits) - 1); $i>=0; $i--) {
      $dhuhs=explode(")", $dbits[$i]);
      $ymd=explode(",", $dhuhs[0]);
      $ouch=str_replace("new Date(" . $dhuhs[0] . ")", $ymd[2] . "/" . ($ymd[1] + 1) . "/" . $ymd[0], $ouch);
    }
  }
  if (sizeof($dtwobits) > 1) {
    if (substr($ouch, 0, 1) == ",") $ouch="Date" . $ouch;
    for ($i=(sizeof($dtwobits) - 1); $i>=0; $i--) {
      $dhuhs=explode(")", $dtwobits[$i]);
      $ymd=explode(",", $dhuhs[0]);
      $ouch=str_replace("newDate(" . $dhuhs[0] . ")", $ymd[2] . "/" . ($ymd[1] + 1) . "/" . $ymd[0], $ouch);
    }
  }
  return $ouch;
}

$csv_file_content="";
$datais="";
if (isset($_GET['data'])) $datais=$_GET['data'];
if (isset($_POST['data'])) $datais=$_POST['data'];
if ($datais == "" && strpos($_SERVER['QUERY_STRING'], "&data=") !== false) {
       $things=explode("&data=", $_SERVER['QUERY_STRING']);
       if (sizeof($things) > 1) {
        $subthings=explode("&", $things[1]);
        $datais = $subthings[0];
       }
}
if ($datais != "" && strpos($datais, "[") !== false && strpos($datais, "[") < strpos($datais, ",")) {
       $datais="," . $datais;
}
$csvprefix="";
if (isset($_GET['label'])) $csvprefix.=$_GET['label'];
if (isset($_POST['label'])) $csvprefix.=$_POST['label'];
if (isset($_GET['task'])) $csvprefix.=$_GET['task'];
if (isset($_POST['task'])) $csvprefix.=$_POST['task'];
if (isset($_GET['country'])) $csvprefix.=$_GET['country'];
if (isset($_POST['country'])) $csvprefix.=$_POST['country'];
if (strpos(urldecode($datais), '),newDate(') !== false && strpos(urldecode($datais), ')]') === false && $csvprefix == '') {
$csvprefix='"Task ID","Task Description","Start Date","End Date","Duration","Percent Complete","Dependencies"';
} else if (strpos(urldecode($datais), '),new Date(') !== false && $csvprefix == '') {
if (isset($_GET['desc'])) $csvprefix='"' . $_GET['desc'] . '","Start Date","End Date"';
if (isset($_POST['desc'])) $csvprefix='"' . $_POST['desc'] . '","Start Date","End Date"';
} else {
if (isset($_GET['popularity'])) $csvprefix.="~" . $_GET['popularity'];
if (isset($_POST['popularity'])) $csvprefix.="~" . $_POST['popularity'];
if (isset($_GET['population'])) $csvprefix.="~" . $_GET['population'];
if (isset($_POST['population'])) $csvprefix.="~" . $_POST['population'];
if (isset($_GET['value'])) $csvprefix.="~" . $_GET['value'];
if (isset($_POST['value'])) $csvprefix.="~" . $_POST['value'];
if (isset($_GET['desc'])) $csvprefix.="~" . $_GET['desc'];
if (isset($_POST['desc'])) $csvprefix.="~" . $_POST['desc'];
if ($csvprefix == "") {
  if (isset($_GET['title'])) $csvprefix.=$_GET['title'];
  if (isset($_POST['desc'])) $csvprefix.=$_POST['title'];
  if ($csvprefix != "") {
   $csvprefix="Date for " . $csvprefix;
   if (isset($_GET['desc1'])) $csvprefix.="~" . $_GET['desc1'];
   if (isset($_POST['desc1'])) $csvprefix.="~" . $_POST['desc1'];
   if (isset($_GET['title1'])) $csvprefix.="~" . $_GET['title1'];
   if (isset($_POST['title1'])) $csvprefix.="~" . $_POST['title1'];
   if (isset($_GET['text1'])) $csvprefix.="~" . $_GET['text1'];
   if (isset($_POST['text1'])) $csvprefix.="~" . $_POST['text1'];
   if (isset($_GET['desc2'])) $csvprefix.="~" . $_GET['desc2'];
   if (isset($_POST['desc2'])) $csvprefix.="~" . $_POST['desc2'];
   if (isset($_GET['title2'])) $csvprefix.="~" . $_GET['title2'];
   if (isset($_POST['title2'])) $csvprefix.="~" . $_POST['title2'];
   if (isset($_GET['text2'])) $csvprefix.="~" . $_GET['text2'];
   if (isset($_POST['text2'])) $csvprefix.="~" . $_POST['text2'];
  }
}
}
$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'];
if (strlen($ioc) > 0 && strlen($csvfile) > 0 && strpos(strtolower($csvfile) . "~", ".csv~") !== false) {
  $csv_file_content=datefixes(str_replace("\n\n","\n",str_replace('~','"',str_replace("[","",str_replace("]","",str_replace(", [","\n",str_replace(",[","\n",str_replace("'", '"', urldecode($csvprefix)) . "\n" . urldecode($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="";
$datais0="";
if (isset($_GET['data0'])) $datais0=$_GET['data0'];
if (isset($_POST['data0'])) $datais0=$_POST['data0'];
if ($datais0 == "" && strpos($_SERVER['QUERY_STRING'], "&data0=") !== false) {
       $things=explode("&data0=", $_SERVER['QUERY_STRING']);
       if (sizeof($things) > 1) {
        $subthings=explode("&", $things[1]);
        $datais0 = $subthings[0];
       }
}
if ($datais0 != "" && strpos($datais0, "[") !== false && strpos($datais0, "[") < strpos($datais0, ",")) {
       $datais0="," . $datais0;
}
if (isset($_GET['csv0'])) $csvfile0=$_GET['csv0'];
if (isset($_POST['csv0'])) $csvfile0=$_POST['csv0'];
if (strlen($ioc) > 0 && strlen($csvfile0) > 0 && strpos(strtolower($csvfile0) . "~", ".csv~") !== false) {
  //$csv_file_content=datefixes(str_replace("\n\n","\n",str_replace('~','"',str_replace("[","",str_replace("]","",str_replace(", [","\n",str_replace(",[","\n",urldecode($csvprefix . "\n" . $datais0))))))));
  $csv_file_content=datefixes(str_replace("\n\n","\n",str_replace('~','"',str_replace("[","",str_replace("]","",str_replace(", [","\n",str_replace(",[","\n",str_replace("'", '"', urldecode($csvprefix)) . "\n" . urldecode($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();
  }
}
?>
