<?php
// xml_to_csv.php
// RJM Programming
// August, 2018
// Thanks to https://edmondscommerce.github.io/php/generic-xml-to-csv-converter.html
$xmlstr="";
$outidea="outfile.csv";
$keep=true;
if (!isset($_GET['keep']) && !isset($_POST['keep'])) {
 $keep=false;
}
if (isset($_GET['xml'])) {
 if (strpos(strtolower("~" . urldecode($_GET['xml'])), "~http") !== false) {
   $xmlstr=file_get_contents(urldecode($_GET['xml']));
   $outidea=explode("." . explode(".",urldecode($_GET['xml']))[-1 + sizeof(explode(".",urldecode($_GET['xml'])))], urldecode($_GET['xml']))[0];
   $outidea=explode("/", $outidea)[-1 + sizeof(explode("/", $outidea))];
   $outidea=explode(substr("\\",0,1), $outidea)[-1 + sizeof(explode(substr("\\",0,1), $outidea))];
   $outidea.=".csv";
 } else if (strtolower(("~" . urldecode($_GET['xml'])), "~<") !== false) {
   $xmlstr=str_replace("+"," ",urldecode($_GET['xml']));
 }
 if ($xmlstr != "") {
   $xml = simplexml_load_string($xmlstr);
 } else if (file_exists(urldecode($_GET['xml']))) {
   $xmlstr=" ";
   $outidea=explode("." . explode(".",urldecode($_GET['xml']))[-1 + sizeof(explode(".",urldecode($_GET['xml'])))], urldecode($_GET['xml']))[0];
   $outidea=explode("/", $outidea)[-1 + sizeof(explode("/", $outidea))];
   $outidea=explode(substr("\\",0,1), $outidea)[-1 + sizeof(explode(substr("\\",0,1), $outidea))];
   $outidea.=".csv";
   $xml = simplexml_load_file(urldecode($_GET['xml']));
 }
} else if (isset($_POST['xml'])) {
 if (strpos(strtolower("~" . urldecode($_POST['xml'])), "~http") !== false) {
   $xmlstr=file_get_contents(urldecode($_POST['xml']));
   $outidea=explode("." . explode(".",urldecode($_POST['xml']))[-1 + sizeof(explode(".",urldecode($_POST['xml'])))], urldecode($_POST['xml']))[0];
   $outidea=explode("/", $outidea)[-1 + sizeof(explode("/", $outidea))];
   $outidea=explode(substr("\\",0,1), $outidea)[-1 + sizeof(explode(substr("\\",0,1), $outidea))];
   $outidea.=".csv";
 } else if (strtolower(("~" . urldecode($_POST['xml'])), "~<") !== false) {
   $xmlstr=str_replace("+"," ",urldecode($_POST['xml']));
 }
 if ($xmlstr != "") {
   $xml = simplexml_load_string($xmlstr);
 } else if (file_exists(urldecode($_POST['xml']))) { 
   $xmlstr=" ";
   $outidea=explode("." . explode(".",urldecode($_POST['xml']))[-1 + sizeof(explode(".",urldecode($_POST['xml'])))], urldecode($_POST['xml']))[0];
   $outidea=explode("/", $outidea)[-1 + sizeof(explode("/", $outidea))];
   $outidea=explode(substr("\\",0,1), $outidea)[-1 + sizeof(explode(substr("\\",0,1), $outidea))];
   $outidea.=".csv";
   $xml = simplexml_load_file(urldecode($_POST['xml']));
 }
}

if ($xmlstr != "") {
 while (file_exists($outidea)) {
  $outidea=str_replace(".csv","1.csv",$outidea);
 }
 $outstream = fopen($outidea,'w');
 $header=false;
 foreach($xml as $k=>$details) {
    if (!$header) {
        $harr=array_keys(get_object_vars($details));
        if (sizeof($harr) != 0) {
         fputcsv($outstream,$harr);
         $header=true;
        }
    }
    $harr=get_object_vars($details);
    if (sizeof($harr) != 0) fputcsv($outstream,$harr);
 }
 fclose($outstream);
 //$csvstr=preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "",file_get_contents($outidea));
 //$csvstr=preg_replace("/(^[\n]*|[\n]+)[\s\t]*[\n]+/", "",$csvstr);
 $csvstr=file_get_contents($outidea);
 if (isset($_GET['download']) || isset($_POST['download'])) {
  echo "<!doctype html><html><head><title>XML to CSV - RJM Programming - August, 2018</title></head>
 <body>
 <h1 id='myh1'>XML to CSV <a href='" . $outidea . "' download>" . $outidea . "</a></h1>
 <h3>RJM Programming - August, 2018</h3><br><br> 
 <form action='./xml_to_csv.php' method='GET'>
 XML File or URL: <input type='text' name='xml' value=''></input><br><br> 
 <input type='submit' value='Convert to CSV'></input>
 </form>
 </body></html>";
 } else {
  if (!$keep) {
   unlink($outidea);
   header('Content-type: text/csv');
   echo $csvstr;
   exit;
  } else {
   if (isset($_GET['keep'])) {
    if (urldecode($_GET['keep']) == 'keep') {
     unlink($outidea);
     echo "<!doctype html><html><head><title>XML to CSV - RJM Programming - August, 2018</title>
 <script type='text/javascript'>
  function onld() {
    if (parent.document) {
    if (parent.document.getElementById('keep')) {
      parent.document.getElementById('keep').value=document.URL.split('#')[0].split('?')[0].split('xml_to_csv.php')[0] + '" . $outidea . "';
      if (parent.document.getElementById('xml')) {
        parent.document.getElementById('xml').value='" . str_replace('"',"' + String.fromCharCode(34) + '",str_replace("\n","' + String.fromCharCode(10) + '",str_replace("'","' + String.fromCharCode(39) + '",$csvstr))) . "';
      }
      if (parent.document.getElementById('myxmliframe')) {
        parent.document.getElementById('myxmliframe').src=document.URL.split('#')[0].split('?')[0].split('xml_to_csv.php')[0] + '" . $outidea . "';
      }
    }
    }
  }
 </script>
 </head>
 <body onload='onld();'></body></html>";
    } else {
     header('Content-type: text/csv');
     echo $csvstr;
     exit;
    }
   } else if (isset($_POST['keep'])) {
    if (urldecode($_POST['keep']) == 'keep') {
     unlink($outidea);
     echo "<!doctype html><html><head><title>XML to CSV - RJM Programming - August, 2018</title>
 <script type='text/javascript'>
  function onld() {
    if (parent.document) {
    if (parent.document.getElementById('keep')) {
      parent.document.getElementById('keep').value=document.URL.split('#')[0].split('?')[0].split('xml_to_csv.php')[0] + '" . $outidea . "';
      if (parent.document.getElementById('xml')) {
        parent.document.getElementById('xml').value='" . str_replace('"',"' + String.fromCharCode(34) + '",str_replace("\n","' + String.fromCharCode(10) + '",str_replace("'","' + String.fromCharCode(39) + '",$csvstr))) . "';
      }
      if (parent.document.getElementById('myxmliframe')) {
        parent.document.getElementById('myxmliframe').src=document.URL.split('#')[0].split('?')[0].split('xml_to_csv.php')[0] + '" . $outidea . "';
      }
    }
    }
  }
 </script>
 </head>
 <body onload='onld();'></body></html>";
    } else {
     header('Content-type: text/csv');
     echo $csvstr;
     exit;
    }
   }
  } 
 }
} else {
 echo "<!doctype html><html><head><title>XML to CSV - RJM Programming - August, 2018</title></head>
 <body>
 <h1>XML to CSV</h1>
 <h3>RJM Programming - August, 2018</h3><br><br> 
 <form action='./xml_to_csv.php' method='GET'>
 XML File or URL: <input type='text' name='xml' value=''></input><br><br> 
 <input type='submit' value='Convert to CSV'></input>
 </form>
 </body></html>";
}
?>
