optimize the code in terminal.php;

This commit is contained in:
2024-07-15 09:38:43 +08:00
parent 0279ae6493
commit 36a7eecf94
7 changed files with 63 additions and 25 deletions
+2 -3
View File
@@ -1,8 +1,8 @@
<?php <?php
function cd($commands) function cd($commands)
{ {
if(sizeof($commands)>2){ if (sizeof($commands) > 2) {
file_put_contents("recent.txt", "Too many arguments.<br/>", FILE_APPEND); file_put_contents("recent.txt", "Invalid number of arguments.<br/>", FILE_APPEND);
return; return;
} }
$pwd = file_get_contents("pwd.txt"); $pwd = file_get_contents("pwd.txt");
@@ -23,4 +23,3 @@ function cd($commands)
} }
} }
} }
?>
+9
View File
@@ -0,0 +1,9 @@
<?php
function clear($commands)
{
if (sizeof($commands) == 1) {
file_put_contents("recent.txt", "");
} else {
file_put_contents("recent.txt", "Invalid number of arguments.<br/>", FILE_APPEND);
}
}
+12
View File
@@ -0,0 +1,12 @@
<?php
function help($commands)
{
$help = file_get_contents("static/terminal.php/help.txt");
if (sizeof($commands) == 1) {
file_put_contents("recent.txt", $help, FILE_APPEND);
} else if (sizeof($commands) == 2 && file_get_contents("static/help/$commands[1].txt") != NULL) {
file_put_contents("recent.txt", file_get_contents("static/help/$commands[1].txt"), FILE_APPEND);
} else {
file_put_contents("recent.txt", "Invalid number of arguments.<br/>", FILE_APPEND);
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
function mdir($commands)
{
$pwd = file_get_contents("pwd.txt");
if (sizeof($commands) == 2) {
if (!opendir("fileroot" . $pwd . "/" . $commands[1])) {
if (!mkdir("fileroot" . $pwd . "/" . $commands[1])) {
file_put_contents("recent.txt", "Error creating directory.No permissions?<br/>", FILE_APPEND);
}
} else {
file_put_contents("recent.txt", "Directory already exists.<br/>", FILE_APPEND);
}
} else {
file_put_contents("recent.txt", "Invalid number of arguments.<br/>", FILE_APPEND);
}
}
+10
View File
@@ -0,0 +1,10 @@
<?php
function pwd($commands)
{
$pwd = file_get_contents("pwd.txt");
if (sizeof($commands) == 1) {
file_put_contents("recent.txt", $pwd . "<br/>", FILE_APPEND);
} else {
file_put_contents("recent.txt", "Invalid number of arguments.<br/>", FILE_APPEND);
}
}
+3 -2
View File
@@ -20,7 +20,7 @@ function rm($commands)
} else { } else {
file_put_contents("recent.txt", "Not a directory.<br/>", FILE_APPEND); file_put_contents("recent.txt", "Not a directory.<br/>", FILE_APPEND);
} }
} else { } else if (sizeof($commands) == 2) {
if (is_dir("fileroot" . $pwd . "/" . $commands[1])) { if (is_dir("fileroot" . $pwd . "/" . $commands[1])) {
file_put_contents("recent.txt", "Is a directory.<br/>Try rm -r<br/>", FILE_APPEND); file_put_contents("recent.txt", "Is a directory.<br/>Try rm -r<br/>", FILE_APPEND);
} else { } else {
@@ -30,6 +30,7 @@ function rm($commands)
file_put_contents("recent.txt", "Error deleting.<br/>", FILE_APPEND); file_put_contents("recent.txt", "Error deleting.<br/>", FILE_APPEND);
} }
} }
} else {
file_put_contents("recent.txt", "Invalid number of arguments.<br/>", FILE_APPEND);
} }
} }
?>
+11 -20
View File
@@ -40,33 +40,24 @@ if (!opendir("fileroot")) {
echo "<script>window.scrollTo(0,document.body.scrollHeight)</script>"; echo "<script>window.scrollTo(0,document.body.scrollHeight)</script>";
exit(0); exit(0);
} }
if ($commands[0] == "pwd" & sizeof($commands) == 1) { if ($commands[0] == "pwd") {
file_put_contents("recent.txt", $pwd . "<br/>", FILE_APPEND); include "commands/pwd.php";
} else if ($commands[0] == "mkdir" && sizeof($commands) == 2) { pwd($commands);
if (!opendir("fileroot" . $pwd . "/" . $commands[1])) { } else if ($commands[0] == "mkdir") {
if (!mkdir("fileroot" . $pwd . "/" . $commands[1])) { include "commands/mkdir.php";
file_put_contents("recent.txt", "Error creating directory.No permissions?<br/>", FILE_APPEND); mdir($commands);
}
} else {
file_put_contents("recent.txt", "Directory already exists.<br/>", FILE_APPEND);
}
} else if ($commands[0] == "rm") { } else if ($commands[0] == "rm") {
include "commands/rm.php"; include "commands/rm.php";
rm($commands); rm($commands);
} else if ($commands[0] == "clear" & sizeof($commands) == 1) { } else if ($commands[0] == "clear") {
file_put_contents("recent.txt", ""); include "commands/clear.php";
clear($commands);
} else if ($commands[0] == "cd") { } else if ($commands[0] == "cd") {
include "commands/cd.php"; include "commands/cd.php";
cd($commands); cd($commands);
} else if ($commands[0] == "help") { } else if ($commands[0] == "help") {
$help = file_get_contents("static/terminal.php/help.txt"); include "commands/help.php";
if (sizeof($commands) == 1) { help($commands);
file_put_contents("recent.txt", $help, FILE_APPEND);
} else if (sizeof($commands) == 2 && file_get_contents("static/help/$commands[1].txt") != NULL) {
file_put_contents("recent.txt", file_get_contents("static/help/$commands[1].txt"), FILE_APPEND);
} else {
file_put_contents("recent.txt", "Unknown command.<br/>", FILE_APPEND);
}
} else { } else {
file_put_contents("recent.txt", "Unknown command.<br/>try 'help' or 'help [command]'<br/>", FILE_APPEND); file_put_contents("recent.txt", "Unknown command.<br/>try 'help' or 'help [command]'<br/>", FILE_APPEND);
} }