<?php
 // mil_mapping.php
 // RJM Programming
 // September, 2017
 // XML subtractions and additions 

function str_replace_first($from, $to, $subject) {  // thanks to https://stackoverflow.com/questions/1252693/using-str-replace-so-that-it-only-acts-on-the-first-match
    $from = '/'.preg_quote($from, '/').'/';

    return preg_replace($from, $to, $subject, 1);
}

 $results="";
 $csvout="";
 $kshout="#!/bin/ksh\n";
 $delim="";
 
 $filespec="COMM.MIL*.xml";
 if (isset($_GET['filespec'])) $filespec=str_replace("+"," ",urldecode($_GET['filespec']));
 $indexfilecalled="COMM.MIL~INDEX.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']));
 $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']));
 $csvname="mil_mapping.csv"; 
 if (isset($_GET['csvname'])) $csvname=str_replace("+"," ",urldecode($_GET['csvname']));
 $kshname="mil_mapping.ksh";
 if (isset($_GET['kshname'])) $kshname=str_replace("+"," ",urldecode($_GET['kshname']));
 $doksh=""; // "ksh -x mil_mapping.ksh";
 if (isset($_GET['doksh'])) $doksh=str_replace("+"," ",urldecode($_GET['doksh']));
 
 $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";
   }
 }


// </title><section role="annot_cont”
 $startfind='role="annot_cont"';
 
 
 foreach (glob($filespec) as $filename) {
   $precont=@file_get_contents($filename);
   $cont=$precont;
   $sections=explode($startfind, $cont);
   for ($i=1; $i<sizeof($sections); $i++) {
     // check that "<section " preceded
     if (explode(" ",explode("<", $sections[-1 + $i])[-1 + sizeof(explode("<", $sections[-1 + $i]))])[0] == "section") {
       // before that last end tag should be </title>
       if (explode(">",explode("<", str_replace("<?","  ",str_replace("<section ","        ",$sections[-1 + $i])))[-1 + sizeof(explode("<", str_replace("<?","  ",str_replace("<section ","        ",$sections[-1 + $i]))))])[0] == "/title") {
          // after that will be "<leg-history "
          if (strpos($sections[$i], "<leg-history ") !== false) {
            // and in between will be no already done <title></title> or other type of tag actually
            if (strpos("<title", explode("<leg-history ", $section[$i])[0]) === false && strpos("<", explode("<leg-history ", $section[$i])[0]) === false && strpos("</title>", explode("<leg-history ", $section[$i])[0]) === false) {
              $bits=explode("<leg-history ", $sections[$i]);
              if (strpos(explode(">", $sections[$i])[0], ' href="') !== false) {
                $hrefv=explode('"',explode('href="', explode(">", $sections[$i])[0])[1])[0];
                $newsectionone=str_replace($bits[0], str_replace(' href="' . $hrefv . '"', '', $bits[0] . '<title arch="online">Note</title>'), $sections[$i]);
                $cont=str_replace($startfind . $sections[$i], $startfind . $newsectionone, $cont);
                $sections=explode($startfind, $cont);
             } else {
                $newsectionone=str_replace_first($bits[0], $bits[0] . '<title arch="online">Note</title>', $sections[$i]);
                $cont=str_replace($startfind . $sections[$i], $startfind . $newsectionone, $cont);
                $sections=explode($startfind, $cont);
              }
            }
          } else if (strpos($sections[$i], "<notes ") !== false) {
            // and in between will be no already done <title></title> or other type of tag actually
            if (strpos("<title", explode("<notes ", $section[$i])[0]) === false && strpos("<", explode("<notes ", $section[$i])[0]) === false && strpos("</title>", explode("<notes ", $section[$i])[0]) === false) {
              $bits=explode("<notes ", $sections[$i]);
              if (strpos(explode(">", $sections[$i])[0], ' href="') !== false) {
                $hrefv=explode('"',explode('href="', explode(">", $sections[$i])[0])[1])[0];
                $newsectionone=str_replace($bits[0], str_replace(' href="' . $hrefv . '"', '', $bits[0] . '<title arch="online">Note</title>'), $sections[$i]);
                $cont=str_replace($startfind . $sections[$i], $startfind . $newsectionone, $cont);
                $sections=explode($startfind, $cont);
              } else {
                $newsectionone=str_replace_first($bits[0], $bits[0] . '<title arch="online">Note</title>', $sections[$i]);
                $cont=str_replace($startfind . $sections[$i], $startfind . $newsectionone, $cont);
                $sections=explode($startfind, $cont);
              }
            }
          }
       }
     }
   }
   
   if ($cont != $precont) {
    if (!file_exists($filename . "_original_backup")) copy($filename, $filename . "_original_backup");
    file_put_contents($filename, $cont); 
   }
   
 }
 
 ?>
 <!doctype html>
 <html>
 <body onload=" document.getElementById('myform').action=document.URL.split('#')[0].split('?')[0]; " style="background-color:lightgreen;">
 <h1>Adder Innerer of &lt;title arch="online"&gt;Note&lt;/title&gt;</h1>
 <h3>RJM Programming</h3>
 <h3>August, 2017</h3>
 <br>
 <div id='results'><?php echo $results; ?></div>
 <form id="myform" action="#" method="GET">
 File specification for input data: <input type="text" name="filespec" value="COMM.MIL*.xml"></input><br>
 Start delimitation of tags of interest: <input type="text" name="startfind" value='role="annot_cont"'></input><br>
 <input type="submit" value="Run this Job"></input>
 </form>
 </body>
 </html>
 
