* @copyrights (c) 2016, 2024, 5 Mode
* @license https://opensource.org/licenses/BSD-3-Clause
*/
require "HC_init.inc";
$cmdHistory = [];
$cmd = HC_STR;
$opt = HC_STR;
$param1 = HC_STR;
$param2 = HC_STR;
$param3 = HC_STR;
$cmdRecallHistory = [];
function showHistory() {
global $cmdHistory;
$i = 1;
foreach($cmdHistory as $val) {
echo(HTMLencode($val));
$i++;
}
}
function updateHistory(&$update, $maxItems) {
global $cmdHistory;
// Making enough space in $cmdHistory for the update..
$shift = (count($cmdHistory) + count($update)) - $maxItems;
if ($shift > 0) {
$cmdHistory = array_slice($cmdHistory, $shift, $maxItems);
}
// Adding $cmdHistory update..
if (count($update) > $maxItems) {
$beginUpd = count($update) - ($maxItems-1);
} else {
$beginUpd = 0;
}
$update = array_slice($update, $beginUpd, $maxItems);
foreach($update as $val) {
$cmdHistory[] = $val;
}
// Writing out $cmdHistory on disk..
$filepath = HC_APP_PATH . HC_SLASH . ".HC_history";
file_put_contents($filepath, implode('', $cmdHistory));
}
function loadRecallHistory() {
global $cmdRecallHistory;
$tmpcmdRecallHistory = file(HC_APP_PATH . HC_SLASH . ".HC_Recallhistory");
foreach($tmpcmdRecallHistory as $val) {
$cmdRecallHistory[left($val, strlen($val)-1)]=$val;
}
}
function updateRecallHistory($update, $maxItems) {
global $cmdRecallHistory;
if (!array_key_exists($update, $cmdRecallHistory)) {
// Making enough space in $cmdHistory for the update..
$shift = (count($cmdRecallHistory) + 1) - $maxItems;
if ($shift > 0) {
$cmdRecallHistory = array_slice($cmdRecallHistory, $shift, $maxItems);
}
$cmdRecallHistory[$update] = $update . "\n";
}
// Writing out $cmdRecallHistory on disk..
$filepath = HC_APP_PATH . HC_SLASH . ".HC_Recallhistory";
file_put_contents($filepath, implode('', $cmdRecallHistory));
}
function updateHistoryWithErr(string $err) {
global $prompt;
global $command;
$output = [];
$output[] = $prompt . " " . $command . "\n";
$output[] = "$err\n";
updateHistory($output, HC_HISTORY_MAX_ITEMS);
}
function myExecCommand() {
global $prompt;
global $command;
// Exec command..
$output = [];
$output[] = $prompt . " " . $command . "\n";
exec($command, $output);
// Update history..
foreach ($output as &$val) {
if (right($val,1)!="\n") {
$val = $val . "\n";
}
}
updateRecallHistory($command, HC_RECALL_HISTORY_MAX_ITEMS);
updateHistory($output, HC_HISTORY_MAX_ITEMS);
}
function myExecCopy() {
global $prompt;
global $command;
global $param1;
global $param2;
// Exec command..
$output = [];
$output[] = $prompt . " " . $command . "\n";
copy($param1, $param2);
// Update history..
foreach ($output as &$val) {
if (right($val,1)!="\n") {
$val = $val . "\n";
}
}
updateRecallHistory($command, HC_RECALL_HISTORY_MAX_ITEMS);
updateHistory($output, HC_HISTORY_MAX_ITEMS);
}
function myExecCDFolderCommand() {
global $prompt;
global $command;
global $param1;
global $curPath;
// Exec command..
$output = [];
$output[] = $prompt . " " . $command . "\n";
//exec($command, $output);
$newPath = $curPath . HC_SLASH . $param1;
chdir($newPath);
$curPath = $newPath;
$curDir = $param1;
$prompt = str_replace("$1", $curDir, HC_APP_PROMPT);
// Update history..
foreach ($output as &$val) {
if (right($val,1)!="\n") {
$val = $val . "\n";
}
}
updateRecallHistory($command, HC_RECALL_HISTORY_MAX_ITEMS);
updateHistory($output, HC_HISTORY_MAX_ITEMS);
}
function myExecCDBackwCommand() {
global $prompt;
global $command;
global $curPath;
// Exec command..
$output = [];
$output[] = $prompt . " " . $command . "\n";
//exec($command, $output);
$ipos = strripos($curPath, HC_SLASH);
$newPath = substr($curPath, 0, $ipos);
chdir($newPath);
$curPath = getcwd();
$ipos = strripos($curPath, HC_SLASH);
$curDir = substr($curPath, $ipos);
$prompt = str_replace("$1", $curDir, HC_APP_PROMPT);
// Update history..
foreach ($output as &$val) {
if (right($val,1)!="\n") {
$val = $val . "\n";
}
}
updateRecallHistory($command, HC_RECALL_HISTORY_MAX_ITEMS);
updateHistory($output, HC_HISTORY_MAX_ITEMS);
}
function parseCommand() {
global $command;
global $cmd;
global $opt;
global $param1;
global $param2;
global $param3;
$str = trim($command);
$ipos = stripos($str, HC_SPACE);
if ($ipos > 0) {
$cmd = left($str, $ipos);
$str = substr($str, $ipos+1);
} else {
$cmd = $str;
return;
}
if (left($str, 1) === "-") {
$ipos = stripos($str, HC_SPACE);
if ($ipos > 0) {
$opt = left($str, $ipos);
$str = substr($str, $ipos+1);
} else {
$opt = $str;
return;
}
}
$ipos = stripos($str, HC_SPACE);
if ($ipos > 0) {
$param1 = left($str, $ipos);
$str = substr($str, $ipos+1);
} else {
$param1 = $str;
return;
}
$ipos = stripos($str, HC_SPACE);
if ($ipos > 0) {
$param2 = left($str, $ipos);
$str = substr($str, $ipos+1);
} else {
$param2 = $str;
return;
}
$ipos = stripos($str, HC_SPACE);
if ($ipos > 0) {
$param3 = left($str, $ipos);
$str = substr($str, $ipos+1);
} else {
$param3 = $str;
return;
}
}
function is_word(string $string) {
return preg_match("/^[\w\-]+?$/", $string);
}
function cdparamValidation() {
global $curPath;
global $param1;
global $param2;
//param1!="" and isword
if (($param1===HC_STR) && !is_word($param1)) {
updateHistoryWithErr("invalid dir");
return false;
}
//param2==""
if ($param2!==HC_STR) {
updateHistoryWithErr("invalid parameters");
return false;
}
//param1 exist and is_dir
$path = $curPath . HC_SLASH . $param1;
if (!file_exists($path) || !is_dir($path)) {
updateHistoryWithErr("dir doesn't exist");
return false;
}
return true;
}
function cpparamValidation() {
global $curPath;
global $opt;
global $param1;
global $param2;
global $param3;
//opt!="" and opt!="-R" and opt!="-Rp"
if (($opt!==HC_STR) && ($opt!=="-R") && ($opt!=="-Rp") && ($opt!=="-p")) {
updateHistoryWithErr("invalid parameters");
return false;
}
//param1!="" and isword
if (($param1===HC_STR) && !is_word($param1)) {
updateHistoryWithErr("invalid source path");
return false;
}
//param2!="" and isword
if (($param2===HC_STR) && !is_word($param2)) {
updateHistoryWithErr("invalid destination path");
return false;
}
if ($param3!=HC_STR) {
updateHistoryWithErr("invalid parameters");
return false;
}
//param1 exist
$path = $curPath . HC_SLASH . $param1;
if (!file_exists($path)) {
updateHistoryWithErr("source must exists");
return false;
}
//param2 doesn't exist
$path = $curPath . HC_SLASH . $param2;
if (file_exists($path)) {
updateHistoryWithErr("destination already exists");
return false;
}
return true;
}
function mvparamValidation() {
global $curPath;
global $opt;
global $param1;
global $param2;
global $param3;
//opt!="" and opt!="-R"
if ($opt!==HC_STR) {
updateHistoryWithErr("invalid parameters");
return false;
}
//param1!="" and isword
if (($param1===HC_STR) && !is_word($param1)) {
updateHistoryWithErr("invalid source path");
return false;
}
//param2!="" and isword
if (($param2===HC_STR) && !is_word($param2)) {
updateHistoryWithErr("invalid destination path");
return false;
}
if ($param3!=HC_STR) {
updateHistoryWithErr("invalid parameters");
return false;
}
//param1 exist
$path = $curPath . HC_SLASH . $param1;
if (!file_exists($path)) {
updateHistoryWithErr("source must exists");
return false;
}
//param2 doesn't exist
$path = $curPath . HC_SLASH . $param2;
if (file_exists($path)) {
updateHistoryWithErr("destination already exists");
return false;
}
return true;
}
$password = filter_input(INPUT_POST, "Password");
$command = filter_input(INPUT_POST, "CommandLine");
$pwd = filter_input(INPUT_POST, "pwd");
$hideFB = filter_input(INPUT_POST, "hideFB");
if ($password !== HC_STR) {
$hash = hash("sha256", $password . HC_APP_SALT, false);
if ($hash !== HC_APP_HASH) {
$password=HC_STR;
}
}
$curPath = HC_CMDLINE_CD_DEPTH;
if ($pwd!==HC_STR) {
if (left($pwd, strlen(HC_CMDLINE_CD_DEPTH)) === HC_CMDLINE_CD_DEPTH) {
$curPath = $pwd;
chdir($curPath);
}
}
$ipos = strripos($curPath, HC_SLASH);
$curDir = substr($curPath, $ipos);
$prompt = str_replace("$1", $curDir, HC_APP_PROMPT);
if ($password !== HC_STR) {
loadRecallHistory();
$cmdHistory = file(HC_APP_PATH . HC_SLASH . ".HC_history");
parseCommand($command);
//echo("cmd=" . $cmd . "
");
//echo("opt=" . $opt . "
");
//echo("param1=" . $param1 . "
");
//echo("param2=" . $param2 . "
");
if (mb_stripos(HC_CMDLINE_VALIDCMDS, "|" . $command . "|")) {
if ($command === "cd ..") {
$ipos = strripos($curPath, HC_SLASH);
$nextPath = substr($curPath, 0, $ipos);
if (strlen(HC_CMDLINE_CD_DEPTH) > strlen($nextPath)) {
updateHistoryWithErr("out of root boundary");
} else {
myExecCDBackwCommand();
}
} else {
myExecCommand();
}
} else if (mb_stripos(HC_CMDLINE_VALIDCMDS, "|" . $cmd . "|")) {
if ($cmd === "cd") {
if (cdparamValidation()) {
myExecCDFolderCommand();
}
} else if ($cmd === "cp") {
if (cpparamValidation()) {
myExecCommand();
}
} else if ($cmd === "mv") {
if (mvparamValidation()) {
myExecCommand();
}
}
} else {
updateHistoryWithErr("invalid command");
}
} else {
$cmdHistory = [];
}
?>