<?php
// renameidea.php
// File rename idea
// RJM Programming
// December, 2018
// Filename: [PATH][act-]1989-76[-2017-07-01][.xml] ... ie. FolderIn Prefix Stays Goes Extension
// For example, replace ‘act-‘ with ‘ACT~AUS~NSW~Y.’ and delete the date at the end ‘-2017-07-01’ – this will vary with each file

$path="";
$prefix="act-";
if (isset($_GET['prefix'])) {
   $prefix=str_replace("+"," ",urldecode($_GET['prefix']));
} else if (isset($_POST['prefix'])) {
   $prefix=str_replace("+"," ",urldecode($_POST['prefix']));
}
$newprefix="ACT~AUS~NSW~Y.";
if (isset($_GET['newprefix'])) {
   $newprefix=str_replace("+"," ",urldecode($_GET['newprefix']));
} else if (isset($_POST['newprefix'])) {
   $newprefix=str_replace("+"," ",urldecode($_POST['newprefix']));
}
$stays="1989-76";
$goes="-2017-07-01";
if (isset($_GET['goes'])) {
   $goes=str_replace("+"," ",urldecode($_GET['goes']));
} else if (isset($_POST['goes'])) {
   $goes=str_replace("+"," ",urldecode($_POST['goes']));
}
$xgoes="";
for ($ii=0; $ii<strlen($goes); $ii++) {
 if (substr($goes, $ii, 1) >= 'a' && substr($goes, $ii, 1) <= 'z') {
   $xgoes.="[a-z]";
 } else if (substr($goes, $ii, 1) >= 'A' && substr($goes, $ii, 1) <= 'Z') {
   $xgoes.="[A-Z]";
 } else if (substr($goes, $ii, 1) >= '0' && substr($goes, $ii, 1) <= '9') {
   $xgoes.="[0-9]";
 } else {
   $xgoes.=substr($goes, $ii, 1);
 }
}
$extension=".xml";
if (isset($_GET['extension'])) {
   $extension=str_replace("+"," ",urldecode($_GET['extension']));
} else if (isset($_POST['extension'])) {
   $extension=str_replace("+"," ",urldecode($_POST['extension']));
}
$doit=true;
$couldhave="";
if (isset($_GET['preview'])) {
   $doit=false;
   $couldhave="could have ";
} else if (isset($_POST['preview'])) {
   $doit=false;
   $couldhave="could have ";
}

if (isset($_GET['path'])) {
   $path=str_replace("+"," ",urldecode($_GET['path']));
   $htmlis="<html><body><h1>Rename Report ...</h1><br><h3>Filename: act-1989-76-2017-07-01.xml ... replace act- with ACT~AUS~NSW~Y. and delete the date at the end -2017-07-01 ... this will vary with each file</h3><br><div></div></body></html>";
   foreach (glob($path . $prefix . "*" . $xgoes . $extension) as $xmlfil) {
     $predotxml=explode($extension, $xmlfil);
     $postdotxml=explode($prefix, substr($predotxml[0],0,(strlen($predotxml[0]) - strlen($goes))));
     $renfile=$postdotxml[0] . $newprefix . $postdotxml[1] . $extension;
     $htmlis=str_replace("</div>", "<br>File candidate called " . $xmlfil . " found that we " . $couldhave . "renamed to " . $renfile . "</div>", $htmlis);
     if ($doit) { rename($xmlfil, $renfile); }
   }
   echo $htmlis;
   exit;
} else if (isset($_POST['path'])) {
   $path=str_replace("+"," ",urldecode($_POST['path']));
   $htmlis="<html><body><h1>Rename Report ...</h1><br><h3>Filename: act-1989-76-2017-07-01.xml ... replace act- with ACT~AUS~NSW~Y. and delete the date at the end -2017-07-01 ... this will vary with each file</h3><br><div></div></body></html>";
   foreach (glob($path . $prefix . "*" . $xgoes . $extension) as $xmlfil) {
     $predotxml=explode($extension, $xmlfil);
     $postdotxml=explode($prefix, substr($predotxml[0],0,(strlen($predotxml[0]) - strlen($goes))));
     $renfile=$postdotxml[0] . $newprefix . $postdotxml[1] . $extension;
     $htmlis=str_replace("</div>", "<br>File candidate called " . $xmlfil . " found that we " . $couldhave . "renamed to " . $renfile . "</div>", $htmlis);
     if ($doit) { rename($xmlfil, $renfile); }
   }
   echo $htmlis;
   exit;
} else {
   if (!isset($_GET['nothing'])) {
   $htmlis="<html><body><h1>Rename Run ...</h1><br><h3>Filename: act-1989-76-2017-07-01.xml ... replace act- with ACT~AUS~NSW~Y. and delete the date at the end -2017-07-01 ... this will vary with each file</h3><br><form target=ibelow action=./renameidea.php method=GET><br>
<table>
<tr><th>Path</th><th>Prefix ... to ...<br>New Prefix</th><th>Stays</th><th>Goes<br>(in terms of<br> its length)</th><th>Extension<br>Preview Only?</th></tr>
<tr><td><input style='width:170px;' name=path type='text' value='" . dirname(__FILE__) . "/' placeholder='" . dirname(__FILE__) . "/'></input></td><td><input name=prefix type='text' value='act-' placeholder='act-'></input><br> ... to ... <input name=newprefix type='text' value='ACT~AUS~NSW~Y.' placeholder='ACT~AUS~NSW~Y.'></input></td><td><input id=stays type='text' value='1989-76' placeholder='1989-76'></input></td><td><input name=goes type='text' value='-2017-07-01' placeholder='-2017-07-01'></input></td><td><input name=extension type='text' value='.xml' placeholder='.xml'></input><br>Preview Only? <input name=preview type=checkbox></input></td></tr>
</table>
<input type=submit value='Rename'></input><form><br><br><iframe src='./renameidea.php?nothing=y' name=ibelow id=ibelow style='width:100%;height:600px;'></iframe></body></html>";
   echo $htmlis;
   exit;
   } else {
   exit;
   }
}
?>
