<?php
 // global_substitution.php
 // RJM Programming
 // March, 2017
 // Global substitution in two parts:
 //    1) From filespec create CSV mapping (global_substitution.csv) for second part via section tags containing role="paragraph" and old_label="[OldLabel]" and label="[NewLabel]" all before section tag end
 //    2) From input index file and CSV above create Korn Shell (ksh) file that runs ... ksh -x global_substitution.ksh ... to update for new index file (but backup yourself first)
 // September, 2017 ... Allow for jobs where supplied with index file and intermediate CSV and a "halfway prefix" to search for in CSV file, but no KSH file yet
 //                 ... Allow for a final COM_rep.txt "sanity check" extraction of a single tag value
 
 ini_set('max_execution_time', 6000);

 $results="";
 $csvout="";
 $kshout="#!/bin/ksh\n";
 $delim="";
 

 
 $filespec="COM_.TL16~INDEX_pudd.xml";
 if (isset($_GET['filespec'])) $filespec=str_replace("+"," ",urldecode($_GET['filespec']));
 $indexfilecalled="COMM.TL16~*.xml";
 if (isset($_GET['index'])) $indexfilecalled=str_replace("+"," ",urldecode($_GET['index']));
 $startfind="<section ";
 if (isset($_GET['startfind'])) $startfind=str_replace("+"," ",urldecode($_GET['startfind']));
 $endfind=">";
 if (isset($_GET['endfind'])) $endfind=str_replace("+"," ",urldecode($_GET['endfind']));
 $newlabelfindstart=' label="';
 if (isset($_GET['newlabelfindstart'])) $newlabelfindstart=str_replace("+"," ",urldecode($_GET['newlabelfindstart']));
 $newlabelfindend='"';
 if (isset($_GET['newlabelfindend'])) $newlabelfindend=str_replace("+"," ",urldecode($_GET['newlabelfindend']));
 $oldlabelfindstart=' old_label="';
 if (isset($_GET['oldlabelfindstart'])) $oldlabelfindstart=str_replace("+"," ",urldecode($_GET['oldlabelfindstart']));
 $oldlabelfindend='"';
 if (isset($_GET['oldlabelfindend'])) $oldlabelfindend=str_replace("+"," ",urldecode($_GET['oldlabelfindend']));
 
 $labelthis=' uci="';
 if (isset($_GET['labelthis'])) $labelthis=str_replace("+"," ",urldecode($_GET['labelthis']));
 $labelthistwo='"';
 if (isset($_GET['labelthistwo'])) $labelthistwo=str_replace("+"," ",urldecode($_GET['labelthistwo']));

 
 $findthis=' role="paragraph"';
 if (isset($_GET['findthis'])) $findthis=str_replace("+"," ",urldecode($_GET['findthis']));
 $findthistwo='';
 if (isset($_GET['findthistwo'])) $findthistwo=str_replace("+"," ",urldecode($_GET['findthistwo']));
 $findthisthree='';
 if (isset($_GET['findthisthree'])) $findthisthree=str_replace("+"," ",urldecode($_GET['findthisthree']));

 $titlethis='>';
 if (isset($_GET['titlethis'])) $titlethis=str_replace("+"," ",urldecode($_GET['titlethis']));
 $titlethistwo='</title>';
 if (isset($_GET['titlethistwo'])) $titlethistwo=str_replace("+"," ",urldecode($_GET['titlethistwo']));

 $csvname="global_substitution.csv"; 
 if (isset($_GET['csvname'])) $csvname=str_replace("+"," ",urldecode($_GET['csvname']));
 $halfwayprefix=""; 
 if (isset($_GET['halfwayprefix'])) $halfwayprefix=str_replace(",","",str_replace("+"," ",urldecode($_GET['halfwayprefix'])));
 $kshname="global_substitution.ksh";
 if (isset($_GET['kshname'])) $kshname=str_replace("+"," ",urldecode($_GET['kshname']));
 $doksh=""; // "ksh -x global_substitution.ksh";
 if (isset($_GET['doksh'])) $doksh=str_replace("+"," ",urldecode($_GET['doksh']));
 $followup=""; 
 if (isset($_GET['followup'])) $followup=str_replace(">","",str_replace("<","",str_replace("</","",str_replace("+"," ",urldecode($_GET['followup'])))));
 $enforce=""; 
 if (isset($_GET['enforce'])) $enforce=str_replace("+"," ",urldecode($_GET['enforce']));
 $objections=""; 
 if (isset($_GET['objections'])) $objections=str_replace("+"," ",urldecode($_GET['objections']));
 
 if ($enforce != "" && $kshname != "") {
   if (file_exists($kshname)) {
     unlink($kshname);
   }
 }

 $topresults="";
 
 $uext=".XML";
 $lext=".xml";
 $ualt=".XXX";
 $lalt=".xxx";
 $extis=explode(".", $indexfilecalled);
 if (sizeof($extis) > 1) {
   $uext=strtoupper("." . $extis[-1 + sizeof($extis)]);
   $lext=strtolower("." . $extis[-1 + sizeof($extis)]);
   if ($lext == ".xxx") {
     $lalt=".xzz";
     $ualt=".XZZ";
   } else {
     $lalt=substr($lext,0,2) . "xx";
     $ualt=substr($uext,0,2) . "XX";
   }
 }

 if ($halfwayprefix != "" && file_exists($csvname) && file_exists($indexfilecalled) && !file_exists($kshname)) { //  jobs where supplied with index file and intermediate CSV and a "halfway prefix" to search for in CSV file, but no KSH file yet 
   $csvcont=@file_get_contents($csvname);
   $csvlines=explode("\n",$csvcont);
   $oldresults="";
   if ($doksh != "") {
    $results="<details><summary style='background-color:pink;'>" . basename($indexfilecalled) . " changes on " . gmdate("l jS \of F Y h:i:s A") . " GMT below ...</summary><table style='width:100%;'><tbody style='width:100%;'><tr style='width:100%;'><td style='width:45%;vertical-align:top;'>" . basename($indexfilecalled) . " below ...<br><div style='border:5px solid red;' id='dnew'>" . str_replace("<","&lt;",str_replace(">","&gt;",file_get_contents($indexfilecalled))) . "</div></td>";
    $oldresults="<td style='width:10%;vertical-align:top;'> ... New Versus Old ...</td><td style='width:45%;vertical-align:top;'>" . basename($indexfilecalled . "_original_backup") . " below ...<br><div style='border:5px solid green;' id='dold'>" . str_replace("<","&lt;",str_replace(">","&gt;",file_get_contents($indexfilecalled))) . "</div></td></tr></tbody></table></details><br>";
   }
   for ($icsvlines=0; $icsvlines<sizeof($csvlines); $icsvlines++) {
    if (strpos($csvlines[$icsvlines],$halfwayprefix) !== false) {
    $oldfielddata=explode(",",$csvlines[$icsvlines])[0];
    $newfielddata="";
    if (strpos($csvlines[$icsvlines],"," . $halfwayprefix) !== false) {
     $newfielddata=explode(",",($halfwayprefix . explode(",[",$csvlines[$icsvlines])[1]))[0];
    }
    if ($doksh != "") {
      if ($oldfielddata != "") {
        $oldresults=str_replace($oldfielddata,"<span style='background-color:red;' title='Changed to " . $newfielddata . "'>" . $oldfielddata . "</span>", $oldresults);
        if ($newfielddata != "") $results=str_replace($oldfielddata,"<span style='background-color:green;' title='Changed from " . $oldfielddata . "'>" . $newfielddata . "</span>", $results);
      }
    }
    $kshout.=$delim . "cat " . $indexfilecalled . " | sed '/\\" . str_replace("]","\\]",$oldfielddata) . "/s//\\" . str_replace("]","\\]",$newfielddata) . "/g' > " . str_replace($uext,$ualt,str_replace($lext,$lalt,$indexfilecalled)) . " ; rm -f " . $indexfilecalled . " ; cat " . str_replace($uext,$ualt,str_replace($lext,$lalt,$indexfilecalled)) . " > " . $indexfilecalled . " ; ";
	$delim="\n";
	}
   }
 if ($delim != "") {
  rename($csvname, $csvname . "_original_backup"); 
  file_put_contents($kshname, $kshout);
 
  if ($doksh != "") {
    if (!file_exists($indexfilecalled . "_original_backup")) copy($indexfilecalled, $indexfilecalled . "_original_backup");
    $results.=$oldresults;
    file_put_contents("COM_.htm", "<!doctype html><html><body><div>" . explode("</summary>",explode("</details>",$results)[0])[-1 + sizeof(explode("</summary>",explode("</details>",$results)[0]))] . "</div></body></html>");
    exec($doksh);
    sleep(2);
    $results.="<details><summary style='background-color:cyan;'>" . basename($indexfilecalled) . " " . gmdate("l jS \of F Y h:i:s A") . " GMT below ...</summary><table style='width:100%;'><tbody style='width:100%;'><tr style='width:100%;'><td style='width:45%;vertical-align:top;'>" . basename($indexfilecalled) . " below ...<br><textarea cols=180 rows=50 id='new'>" . file_get_contents($indexfilecalled) . "</textarea></td><td style='width:10%;vertical-align:top;'> ... New Versus Old ...</td><td style='width:45%;vertical-align:top;'>" . basename($indexfilecalled . "_original_backup") . " below ...<br><textarea id='old' cols=180 rows=50>" . file_get_contents($indexfilecalled . "_original_backup") . "</textarea></td></tr></tbody></table></details><br>";
  }
 }
 } else if (isset($_GET['filespec'])) {   //  jobs with no CSV yet
 $topresults="";
 $oldresults="";
 $newresults="";
 if ($doksh != "") {
    $newresults.="<details><summary style='background-color:pink;'>" . basename($indexfilecalled) . " changes on " . gmdate("l jS \of F Y h:i:s A") . " GMT below ...</summary><table style='width:100%;'><tbody style='width:100%;'><tr style='width:100%;'><td style='width:45%;vertical-align:top;'>" . basename($indexfilecalled) . " below ...<br><div style='border:5px solid red;' id='dnew'>" . str_replace("<","&lt;",str_replace(">","&gt;",file_get_contents($indexfilecalled))) . "</div></td>";
    $oldresults.="<td style='width:10%;vertical-align:top;'> ... New Versus Old ...</td><td style='width:45%;vertical-align:top;'>" . basename($indexfilecalled . "_original_backup") . " below ...<br><div style='border:5px solid green;' id='dold'>" . str_replace("<","&lt;",str_replace(">","&gt;",file_get_contents($indexfilecalled))) . "</div></td></tr></tbody></table></details><br>";
 }
 foreach (glob($filespec) as $filename) {
   $cont=@file_get_contents($filename);
   $chapteruci="";
   if ($labelthis != "" && $labelthistwo != "") {
   if (strpos($cont,$labelthis) !== false) {
    $chapteruci=explode($labelthistwo,explode($labelthis, $cont)[1])[0];
   }
   }
   $sections=explode($startfind, $cont);
   for ($i=1; $i<sizeof($sections); $i++) {
     $thisbits=explode($endfind, $sections[$i]);
     $thebit="  " . $thisbits[0];
     if (strpos($thebit, $oldlabelfindstart) !== false && strpos($thebit, $newlabelfindstart) !== false && ($findthis == '' || strpos($thebit, $findthis) !== false) && ($findthistwo == '' || strpos($thebit, $findthistwo) !== false) && ($findthisthree == '' || strpos($thebit, $findthisthree) === false)) {
      if (str_replace("]","\\]",explode($oldlabelfindend,explode($oldlabelfindstart, $thebit)[1])[0]) != "") {

        $subtitle="";
        if ($titlethis != "" && $titlethistwo != "") {
        try {
        if (strpos($cont, trim($thebit)) !== false) {
        if (strpos(explode(str_replace(">>",">",str_replace("</<","</",str_replace("</</","</",("</" . trim($startfind) . ">")))),explode(trim($thebit), $cont)[1])[0], $titlethistwo) !== false && strpos(explode(str_replace(">>",">",str_replace("</<","</",str_replace("</</","</",("</" . trim($startfind) . ">")))),explode(trim($thebit), $cont)[1])[0], $titlethis) !== false) {
        

        $subtitle=explode($titlethis,explode($titlethistwo,str_replace("<emphasis>","",str_replace("</emphasis>","",str_replace("<quote>","",str_replace("</quote>","",explode(trim($thebit), $cont)[1])))))[0])[-1 + sizeof(explode($titlethis,explode($titlethistwo,str_replace("<emphasis>","",str_replace("</emphasis>","",str_replace("<quote>","",str_replace("</quote>","",explode(trim($thebit), $cont)[1])))))[0]))];


        }
        }
        } catch (Exception $ee) {
        $subtitle="";
        }
        }

        $csvout.=$delim . $chapteruci . "," . explode($oldlabelfindend,explode($oldlabelfindstart, $thebit)[1])[0] . ",," . explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0] . "," . $subtitle;
    if ($doksh != "") {
      if (explode($oldlabelfindend,explode($oldlabelfindstart, $thebit)[1])[0] != "") {
        $oldresults=str_replace(explode($oldlabelfindend,explode($oldlabelfindstart, $thebit)[1])[0],"<span style='background-color:red;' title='Changed to " . explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0] . "'>" . explode($oldlabelfindend,explode($oldlabelfindstart, $thebit)[1])[0] . "</span>", $oldresults);
        if (explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0] != "") $newresults=str_replace(explode($oldlabelfindend,explode($oldlabelfindstart, $thebit)[1])[0],"<span style='background-color:green;' title='Changed from " . explode($oldlabelfindend,explode($oldlabelfindstart, $thebit)[1])[0] . "'>" . explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0] . "</span>", $newresults);
      }
    }
        $kshout.=$delim . "cat " . $indexfilecalled . " | sed '/\\" . str_replace("]","\\]",explode($oldlabelfindend,explode($oldlabelfindstart, $thebit)[1])[0]) . "/s//\\" . str_replace("]","\\]",explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0]) . "/g' > " . str_replace($uext,$ualt,str_replace($lext,$lalt,$indexfilecalled)) . " ; rm -f " . $indexfilecalled . " ; cat " . str_replace($uext,$ualt,str_replace($lext,$lalt,$indexfilecalled)) . " > " . $indexfilecalled . " ; ";
		$delim="\n";
	  }
     } else if ($objections == "" && strpos($thebit, $newlabelfindstart) !== false && strpos($thebit, $newlabelfindstart) !== false && ($findthis == '' || strpos($thebit, $findthis) !== false) && ($findthistwo == '' || strpos($thebit, $findthistwo) !== false) && ($findthisthree == '' || strpos($thebit, $findthisthree) === false)) {
        //echo "it happens";
        //exit;
      if (str_replace("]","\\]",explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0]) != "") {
//echo "thebit=" . trim($thebit) . " afters=" . str_replace(">","&gt;",str_replace("<","&lt;",explode(trim($thebit), $cont)[1])) . " thisbits[1]=" . str_replace("<","&lt;",str_replace(">","&gt;",$thisbits[1]));
//exit;
        $subtitle="";

        if ($titlethis != "" && $titlethistwo != "") {
        try {
        if (strpos($cont, trim($thebit)) !== false) {
        if (strpos(explode(str_replace(">>",">",str_replace("</<","</",str_replace("</</","</",("</" . trim($startfind) . ">")))),explode(trim($thebit), $cont)[1])[0], $titlethistwo) !== false && strpos(explode(str_replace(">>",">",str_replace("</<","</",str_replace("</</","</",("</" . trim($startfind) . ">")))),explode(trim($thebit), $cont)[1])[0], $titlethis) !== false) {
        
        $subtitle=explode($titlethis,explode($titlethistwo,str_replace("<emphasis>","",str_replace("</emphasis>","",str_replace("<quote>","",str_replace("</quote>","",explode(trim($thebit), $cont)[1])))))[0])[-1 + sizeof(explode($titlethis,explode($titlethistwo,str_replace("<emphasis>","",str_replace("</emphasis>","",str_replace("<quote>","",str_replace("</quote>","",explode(trim($thebit), $cont)[1])))))[0]))];
        }
        }
        } catch (Exception $ee) {
        $subtitle="";
        }
        }

        $csvout.=$delim . $chapteruci . ",,," . explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0] . "," . $subtitle;
    if ($doksh != "") {
      if (explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0] != "") {
        $oldresults=str_replace(explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0],"<span style='background-color:red;' title='Changed to " . explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0] . "'>" . explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0] . "</span>", $oldresults);
        if (explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0] != "") $newresults=str_replace(explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0],"<span style='background-color:green;' title='Changed from " . explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0] . "'>" . explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0] . "</span>", $newresults);
      }
    }
        $kshout.=$delim . "cat " . $indexfilecalled . " | sed '/\\" . "" . "/s//\\" . str_replace("]","\\]",explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0]) . "/g' > " . str_replace($uext,$ualt,str_replace($lext,$lalt,$indexfilecalled)) . " ; rm -f " . $indexfilecalled . " ; cat " . str_replace($uext,$ualt,str_replace($lext,$lalt,$indexfilecalled)) . " > " . $indexfilecalled . " ; ";
		$delim="\n";
	  }
     }
   }
 }
 $topresults.=$newresults . $oldresults;
 if ($csvout != "") {
  file_put_contents($csvname, $csvout); 
  file_put_contents($kshname, $kshout);
  if ($topresults != "") {
    file_put_contents("COM_.htm", "<!doctype html><html><body><div>" . explode("</summary>",explode("</details>",$topresults)[0])[-1 + sizeof(explode("</summary>",explode("</details>",$topresults)[0]))] . "</div></body></html>");
  }
  if ($doksh != "") {
    if (!file_exists($indexfilecalled . "_original_backup")) copy($indexfilecalled, $indexfilecalled . "_original_backup");
    exec($doksh);
    sleep(2);
    $results="<details><summary style='background-color:cyan;'>" . basename($indexfilecalled) . " " . gmdate("l jS \of F Y h:i:s A") . " GMT below ...</summary><table style='width:100%;'><tbody style='width:100%;'><tr style='width:100%;'><td style='width:45%;vertical-align:top;'>" . basename($indexfilecalled) . " below ...<br><textarea cols=180 rows=50 id='new'>" . file_get_contents($indexfilecalled) . "</textarea></td><td style='width:10%;vertical-align:top;'> ... New Versus Old ...</td><td style='width:45%;vertical-align:top;'>" . basename($indexfilecalled . "_original_backup") . " below ...<br><textarea id='old' cols=180 rows=50>" . file_get_contents($indexfilecalled . "_original_backup") . "</textarea></td></tr></tbody></table></details><br>";
    $results.=$topresults;
  }
 }
 }

 if ($followup != "" && file_exists($indexfilecalled)) {
   $cont=@file_get_contents($indexfilecalled);
   $sections=explode("</" . $followup . ">", $cont);
   $repd="";
   for ($i=0; $i<sizeof($sections); $i++) {
     $repd.=explode("<" . $followup . ">",$sections[$i])[-1 + sizeof(explode("<" . $followup . ">",$sections[$i]))] . "\n";
   }
   file_put_contents("COM_rep.txt", $repd);
 }

 
 ?>
 <!doctype html>
 <html>
 <head>
 <title>Global Substitution Helper - RJM Programming - September, 2017</title>
 </head>
 <body onload=" document.getElementById('myform').action=document.URL.split('#')[0].split('?')[0]; " style="background-color:#dd9999;">
 <h1 style='display:inline-block;'>Global Substitution Helper</h1><h3 style='display:inline-block;'>&nbsp;&nbsp;RJM Programming - September, 2017</h3>
  <div id='results'><?php echo $results; ?></div>
 <form id="myform" action="#" method="GET">
 File name for input (and output) index file (to change): <input style="width:40%;" type="text" name="index" value="COM_.TL16~INDEX_pudd.xml"></input><br><br>
 File specification for input data: <input style="width:40%;" type="text" name="filespec" value="COMM.TL16~*.xml"></input><br>
 Start delimitation of tags of interest: <input type="text" name="startfind" value="<section "></input><br>
 End delimitation of tags of interest: <input type="text" name="endfind" value=">"></input><br>
 New label regarding global substitutions start delimitation (but if you want <a onlick="document.getElementById('objections).value='y';" style="cursor:pointer;text-decoration:underline;" title="Make sure no Korn Shell file exists ahead of run">both</a> old and new finds click "both"): <input type="text" name="newlabelfindstart" value=' label="'></input><br>
 New label regarding global substitutions end delimitation: <input type="text" name="newlabelfindend" value='"'></input><br>
 Old label regarding global substitutions start delimitation: <input type="text" name="oldlabelfindstart" value=' old_label="'></input><br>
 Old label regarding global substitutions end delimitation: <input type="text" name="oldlabelfindend" value='"'></input><br>
 First Label (CSV first field) tag prefix: <input type="text" name="labelthis" value=' uci="'></input><br>
 First Label (CSV first field) tag suffix: <input type="text" name="labelthistwo" value='"'></input><br>
 Find this within tag 1/3: <input type="text" name="findthis" value=' role="paragraph"'></input><br>
 Find this within tag 2/3: <input type="text" name="findthistwo" value=''></input><br>
 Do not find this within tag: <input type="text" name="findthisthree" value=''></input><br>
 After Find Title (CSV last field) tag prefix: <input type="text" name="titlethis" value='&gt;'></input><br>
 After Find Title (CSV last field) tag suffix: <input type="text" name="titlethistwo" value='&lt;/title&gt;'></input><br><br>
 Output (and intermediate) CSV file name: <input style="width:40%;" type="text" name="csvname" value="global_substitution.csv"></input><br><br>
 Halfway House Old and New Prefix (eg: [) for jobs where supplied with existant index file and existant intermediate CSV file,<br> but <a onlick="document.getElementById('enforce').value='y';" style="cursor:pointer;text-decoration:underline;" title="Make sure no Korn Shell file exists ahead of run">no</a> existant Korn Shell file yet (and so for this we also recommend filling in "finalize job" below): <input style="background-color:yellow;width:50px;" type="text" name="halfwayprefix" value=''></input><br><br>
 Output (and intermediate) Korn Shell script file name: <input style="width:40%;" type="text" name="kshname" value="global_substitution.ksh"></input><br><br>
 Optional command line to finalize job (eg. ksh -x global_substitution.ksh) ... can result in <a target=_blank title='COM_.htm' href='./COM_.htm'>COM_.htm</a> report: <input style="width:40%;" type="text" name="doksh" value=''></input><br>
 Follow Up COM_rep.txt "sanity check" report tag report basis (eg. &lt;olink&gt;) ... can result in <a target=_blank title='COM_rep.txt' href='./COM_rep.txt'>COM_rep.txt</a> report: <input style="width:100px;" type="text" name="followup" value=''></input><br><br>
 <input type='hidden' value='' name='enforce' id='enforce'></input>
 <input type='hidden' value='' name='objections' id='objections'></input>
 <input id='mysub' onclick="this.style.cursor='progress';" type="submit" value="Run this Job"></input>
 </form>
 </body>
 </html>
 

