<?php
// s_e.php
// RJM Programming
// March, 2022
// Use ls command, as the default, to shell_exec function
// Thanks to https://www.geeksforgeeks.org/php-shell_exec-vs-exec-function/
// and https://www.w3.org/Style/Examples/007/evenodd.en.html
// and http://osr600doc.sco.com/en/DOS_others/AP_D_Going_from_dO_to_u1.html

$allowed=['ls', 'df -k /', 'ps', 'date', 'cal'];
$ok=true;

if (PHP_OS =='WINNT' || PHP_OS =='WIN32' || PHP_OS =='Windows') {
  $allowed=['dir', 'wmic diskdrive get size', 'tasklist', 'date', 'time'];
} else if (strpos($_SERVER['SERVER_NAME'], 'rjmprogramming.com.au') !== false) {
  $allowed=['ls', 'date', 'cal', 'free'];
}

$cmnd=$allowed[0];
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];
}

$selbit="<input type=hidden id=cmnd name=cmnd value='" . $cmnd . "'></input><select onchange=\"document.getElementById('cmnd').value=this.value;\" id=selcmnd><option value='" . $cmnd . "'>" . $cmnd . "</option></select>";
for ($i=0; $i<sizeof($allowed); $i++) {
 if ($allowed[$i] != $cmnd) {
  $selbit=str_replace('</select>', '<option value="' . $allowed[$i] . '">' . $allowed[$i] . '</option></select>', $selbit);
 }
}

$outpute = str_replace("\n", "<br>", exec($cmnd));
if ($cmnd == 'cal') {
$outp = str_replace("\n", "<br>", shell_exec($cmnd));
$outsa = explode('_', $outp);
$output = $outp;
if (sizeof($outsa) == 3) {
  $output = $outsa[0] . '<font color=blue>' . $outsa[1] . '</font>' . $outsa[2];
} else {
  $outputee = str_replace("\n", "<br>", exec('date'));
  $outqa = explode(' ', $outputee);
  for ($i=0; $i<sizeof($outqa); $i++) {
     if (strlen($outqa[$i]) <= 2 && strpos($output, ' ' . $outqa[$i] . ' ') !== false) { 
       $outra = explode(' ' . $outqa[$i] . ' ', $output);
       $output = $outra[0] . ' <font color=blue>' . substr($outqa[$i], 0, 1) . '</font>' . trim(substr(($outqa[$i] . ' '), 1)) . ' ' . $outra[1];
     } else if (strlen($outqa[$i]) <= 2 && strpos($output, ' ' . $outqa[$i] . '') !== false) { 
       $outra = explode(' ' . $outqa[$i] . '', $output);
       $output = $outra[0] . ' <font color=blue>' . substr($outqa[$i], 0, 1) . '</font>' . trim(substr(($outqa[$i] . ' '), 1)) . ' ' . $outra[1];
     }
  }
}
} else {
$output = str_replace("\n", "<br>", shell_exec($cmnd));
}
  
// Display the list of all file
// and directory, if chosen
echo "<html><head><title>s_e.php - PHP Supervise shell_exec vs exec</title><style> td { vertical-align: top; } col:first-child {background: #FF0} col:nth-child(2n+3) {background: #CCC} </style></head><body><form method=GET action=./s_e.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 . "');</th><th>$" . "output = exec('" . $cmnd . "');</th></tr><tr><td><pre>$output</pre></td><td><pre>$outpute</pre></td></tr></table>";
?>
