<?php
// find_minus.php
// RJM Programming
// March, 2022

$i=0;
$u='1';
$allowed=["find . -name '*' ", ' -atime ', ' -Btime ', ' -ctime ', ' -mtime '];
$ok=true;
$uv='';
$ut='';
$tswitch='find via access time switch';

if (PHP_OS =='WINNT' || PHP_OS =='WIN32' || PHP_OS =='Windows') {
  $allowed=['dir ./* /S ', ' /O:-D '];
  $tswitch='dir newest files first';
} else if (strpos($_SERVER['SERVER_NAME'], 'rjmprogramming.com.au') !== false) {
  $allowed=["find . -name '*' ", ' -atime ', ' -Btime ', ' -ctime ', ' -mtime '];
}

$cmnd=$allowed[0];
$switch=$allowed[1];
if (isset($_GET['cmnd'])) {
   $cmnd=str_replace('+',' ',urldecode($_GET['cmnd']));
   $ok=false;
   //for ($i=0; $i<sizeof($allowed); $i++) {
     if ($allowed[$i] == $cmnd) { $ok=true; }
   //}
} else if (isset($_POST['cmnd'])) {
   $cmnd=str_replace('+',' ',urldecode($_POST['cmnd']));
   $ok=false;
   //for ($i=0; $i<sizeof($allowed); $i++) {
     if ($allowed[$i] == $cmnd) { $ok=true; }
   //}
}

if ($cmnd == "" || !$ok) {
  $cmnd=$allowed[0];
}

if (isset($_GET['switch'])) {
  $switch=str_replace('+',' ',urldecode($_GET['switch']));
} else if (isset($_POST['switch'])) {
  $switch=str_replace('+',' ',urldecode($_POST['switch']));
}

$origswitch=$switch;
if (strpos($origswitch, '-mtime') !== false) {
  $tswitch='find via modified time switch';
} else if (strpos($origswitch, '-Btime') !== false) {
  $tswitch='find via file inode creation time switch';
} else if (strpos($origswitch, '-ctime') !== false) {
  $tswitch='find via created time switch';
} else if (strpos($origswitch, '-atime') !== false) {
  $tswitch='find via access time switch';
}

if (strpos($switch, '/') === false) {
if (isset($_GET['number'])) {
  $u=str_replace('+',' ',urldecode($_GET['number']));
  if ($u == '') { $u='1'; }
  $switch.=str_replace('+',' ',urldecode($_GET['number']));
} else if (isset($_POST['number'])) {
  $u=str_replace('+',' ',urldecode($_POST['number']));
  if ($u == '') { $u='1'; }
  $switch.=str_replace('+',' ',urldecode($_POST['number']));
} else {
  $switch.='1';
}

if (isset($_GET['unit'])) {
  $uv=str_replace('+',' ',urldecode($_GET['unit']));
  if ($uv != '') {
    $ut=str_replace('m', 'Minute', str_replace('w', 'Week', str_replace('h', 'Hour', str_replace('s', 'Second', str_replace('d', 'Day', $uv))))) . '(s)';
  }
  $switch.=str_replace('+',' ',urldecode($_GET['unit']));
} else if (isset($_POST['unit'])) {
  $uv=str_replace('+',' ',urldecode($_POST['unit']));
  if ($uv != '') {
    $ut=str_replace('m', 'Minute', str_replace('w', 'Week', str_replace('h', 'Hour', str_replace('s', 'Second', str_replace('d', 'Day', $uv))))) . '(s)';
  }
  $switch.=str_replace('+',' ',urldecode($_POST['unit']));
} 
}

$selbit=$cmnd . "&nbsp;<input type=hidden id=cmnd name=cmnd value='" . $cmnd . "'></input><input type=hidden id=switch name=switch value='" . $origswitch . "'></input><select title='" . $tswitch . "' onchange=\"document.getElementById('switch').value=this.value; titleme(this);\" id=selcmnd><option value='" . $origswitch . "'>" . $origswitch . "</option></SELECT>&nbsp;<input style=display:inline-block;width:40px; title='Number of time units' type=number value=" . $u . " step=1 name=number></input>&nbsp;<select title='Time units [Day(s)]' name=unit><option value='" . $uv . "'>" . $ut . "</option><option value='s'>Second(s)</option><option value='m'>Minute(s)</option><option value='h'>Hour(s)</option><option value='d'>Day(s)</option><option value='w'>Week(s)</option></select>";
for ($i=1; $i<sizeof($allowed); $i++) {
 if ($allowed[$i] != $switch) {
  $selbit=str_replace('</SELECT>', '<option value="' . $allowed[$i] . '">' . $allowed[$i] . '</option></SELECT>', $selbit);
 }
}

$outpute = str_replace("\n", "<br>", exec($cmnd . $switch));
$output = str_replace("\n", "<br>", shell_exec($cmnd . $switch));

  
// Display the list of all file
// and directory, if chosen
echo "<html><head><title>find_minus.php - PHP Supervise find or dir</title><sc" . "ript type='text/javascript'> function titleme(so) { if (so.value.indexOf('-mtime') != -1) {  so.title='find via modified time switch'; } else if (so.value.indexOf('-Btime') != -1) {  so.title='find via file inode creation time switch'; } else if (so.value.indexOf('-ctime') != -1) {  so.title='find via created time switch'; } else if (so.value.indexOf('-atime') != -1) {  so.title='find via access time switch'; } } </script><style> td { vertical-align: top; } col:first-child {background: orange; } col:nth-child(2n+3) {background: #CCC} </style></head><body><form method=GET action=./find_minus.php><table cellpadding=5 cellspacing=5 border=20><col><col><tr><td>" . $selbit . "&nbsp;<input style=display:inline-block;background-color:lightgreen; type=submit value='Go'></input>&nbsp;&nbsp;</td><td style='text-align:right;'>RJM Programming - March, 2022<br>Thanks to <a target=_blank title='https://www.geeksforgeeks.org/php-shell_exec-vs-exec-function/' href='//www.geeksforgeeks.org/php-shell_exec-vs-exec-function/'>https://www.geeksforgeeks.org/php-shell_exec-vs-exec-function/</a></td></tr><tr><th>$" . "output = shell_exec('" . $cmnd . $switch . "');</th><th>$" . "output = exec('" . $cmnd . $switch . "');</th></tr><tr><td><pre>$output</pre></td><td><pre>$outpute</pre></td></tr></table>";
?>
