Skip to content

Commit 2add4d6

Browse files
committed
-added following commands:
- cd - pwd - ls - more - touch - exit - added autocomplete feature - added .htaccess protection for programs
1 parent 279828c commit 2add4d6

File tree

18 files changed

+231
-25
lines changed

18 files changed

+231
-25
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.user
2+
.idea/

commandline/autocomplete.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
require 'library/init.php';
3+
header("Content-Type: text/json;");
4+
5+
lConnection::init();
6+
7+
$input = (isset($_GET["input"]) and $_GET["input"]!="") ? trim($_GET["input"]) : null;
8+
if(lConnection::isAuthorized()){
9+
lJSON::dump(lCommand::getAutoCompleteFor($input));
10+
}else{
11+
lJSON::dump(str_replace($input, "", "login"));
12+
}
13+
14+
?>

commandline/library/init.php

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
require 'lCommand.php';
44
require 'lStorage.php';
55
require 'lJSON.php';
6+
require 'lSystem.php';
67
?>

commandline/library/lCommand.php

+31-6
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,59 @@
11
<?php
22
abstract class lCommand{
3+
static function getAutoCompleteFor($input){
4+
$input = explode(" ", $input);
5+
$input = $input[count($input)-1];
6+
$all = [];
7+
// add programs
8+
$dirs = array_filter(glob('programms/*'), 'is_dir');
9+
foreach($dirs as $d){
10+
array_push($all, str_replace("programms/", "", $d));
11+
}
12+
// add files
13+
$pwd = lSystem::getPWD();
14+
$files = glob($pwd."/*");
15+
foreach($files as $f){
16+
array_push($all, basename($f));
17+
}
18+
asort($all);
19+
foreach($all as $a){
20+
if(substr( $a, 0, strlen($input) ) == $input) return str_replace($input, "", $a);
21+
}
22+
return null;
23+
}
324
static function addToStack($command){
4-
array_push($_SESSION["commandstack"], $command);
25+
array_push($_SESSION["phpcommandline"]["commandstack"], $command);
26+
}
27+
static function setNextCommand($command){
28+
array_unshift($_SESSION["phpcommandline"]["commandstack"], $command);
529
}
630

731
static function addToStackIfNotExists($command){
832
if(!lCommand::existsCommandInStack($command)) lCommand::addToStack($command);
933
}
1034

1135
static function existsCommandInStack($command){
12-
foreach($_SESSION["commandstack"] as $c){
36+
foreach($_SESSION["phpcommandline"]["commandstack"] as $c){
1337
$programm = explode(" ", $c)[0];
1438
if($programm==$command || $c==$command) return true;
1539
}
1640
return false;
1741
}
1842

1943
static function getResult(){
20-
return $_SESSION["commandresult"];
44+
return $_SESSION["phpcommandline"]["commandresult"];
2145
}
2246

2347
static function write($result){
24-
$_SESSION["commandresult"] .= (strlen($_SESSION["commandresult"])==0 ? "" : "<br>").$result;
48+
$_SESSION["phpcommandline"]["commandresult"] .= (strlen($_SESSION["phpcommandline"]["commandresult"])==0 ? "" : "<br>").$result;
2549
}
2650

2751
private static function popCommand(){
28-
$_SESSION["commandstack"] = array_slice($_SESSION["commandstack"], 1);
52+
$_SESSION["phpcommandline"]["commandstack"] = array_slice($_SESSION["phpcommandline"]["commandstack"], 1);
2953
}
3054

3155
private static function getCurrentCommand(){
32-
return count($_SESSION["commandstack"])>0 ? $_SESSION["commandstack"][0] : "";
56+
return count($_SESSION["phpcommandline"]["commandstack"])>0 ? $_SESSION["phpcommandline"]["commandstack"][0] : "";
3357
}
3458

3559
static function getNextItemAndPop(){
@@ -58,6 +82,7 @@ static function performNextCommand(){
5882
lCommand::write("command \"$command\" not found");
5983
}
6084
}else{
85+
lCommand::write("> $command");
6186
lCommand::write("not authorized to run this command");
6287
}
6388
}

commandline/library/lConnection.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22
abstract class lConnection{
33
static function init(){
44
session_start();
5-
$_SESSION["commandresult"] = "";
6-
if(!isset($_SESSION["authorized"])){
7-
$_SESSION["authorized"] = false;
8-
$_SESSION["commandstack"] = array();
9-
$_SESSION["storage"] = array();
5+
if(!isset($_SESSION["phpcommandline"])) $_SESSION["phpcommandline"] = [];
6+
$_SESSION["phpcommandline"]["commandresult"] = "";
7+
if(!isset($_SESSION["phpcommandline"]["authorized"])){
8+
$_SESSION["phpcommandline"]["authorized"] = false;
9+
$_SESSION["phpcommandline"]["commandstack"] = array();
10+
$_SESSION["phpcommandline"]["storage"] = array();
1011
}
1112
}
1213

1314
static function isAuthorized(){
14-
return $_SESSION["authorized"];
15+
return $_SESSION["phpcommandline"]["authorized"];
1516
}
1617

1718
static function authorize($username, $password){
1819
$userfile = "users/$username.user";
1920
if(file_exists($userfile) and file_get_contents($userfile)==$password){
20-
$_SESSION["authorized"] = true;
21+
$_SESSION["phpcommandline"]["authorized"] = true;
2122
return true;
2223
}
2324
return false;

commandline/library/lJSON.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ abstract class lJSON{
33
static function dump($object){
44
echo json_encode([
55
"return" => $object,
6-
"stack" => $_SESSION["commandstack"]
6+
"stack" => $_SESSION["phpcommandline"]["commandstack"]
77
]);
88
}
99
}

commandline/library/lStorage.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ abstract class lStorage{
33
static $program = ".main";
44

55
static function save($key, $value){
6-
if(!isset($_SESSION["storage"][lStorage::$program])) $_SESSION["storage"][lStorage::$program] = [];
7-
$_SESSION["storage"][lStorage::$program][$key] = $value;
6+
if(!isset($_SESSION["phpcommandline"]["storage"][lStorage::$program])) $_SESSION["phpcommandline"]["storage"][lStorage::$program] = [];
7+
$_SESSION["phpcommandline"]["storage"][lStorage::$program][$key] = $value;
88
}
99

1010
static function get($key){
11-
return $_SESSION["storage"][lStorage::$program][$key];
11+
if(isset($_SESSION["phpcommandline"]["storage"][lStorage::$program])){
12+
return $_SESSION["phpcommandline"]["storage"][lStorage::$program][$key];
13+
}
14+
return null;
1215
}
1316
}
1417
?>

commandline/library/lSystem.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
abstract class lSystem{
3+
/*
4+
* returns the current directory
5+
*/
6+
static function getPWD(){
7+
$pwd = lStorage::get("pwd");
8+
if($pwd==null) $pwd = str_replace_last("/commandline", "", getcwd());
9+
lStorage::save("pwd", $pwd);
10+
return $pwd;
11+
}
12+
13+
/*
14+
* changed the current directory
15+
*/
16+
static function setPWD($pwd){
17+
lStorage::save("pwd", $pwd);
18+
}
19+
20+
/*
21+
* returns the if a file exists
22+
*/
23+
static function fileExists($file){
24+
return file_exists($file);
25+
}
26+
27+
/*
28+
* returns the if a file exists
29+
*/
30+
static function dirExists($dir){
31+
return is_dir($dir);
32+
}
33+
}
34+
?>

commandline/programms/.htaccess

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deny from all

commandline/programms/cd/init.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
function cdMain($args, $command){
3+
lCommand::write("> $command");
4+
if(count($args)==1){
5+
$dir = $args[0];
6+
$new_dir = change_dir($dir);
7+
if($new_dir!==false) lCommand::write("changed to ".$new_dir);
8+
else lCommand::write("this is not a directory");
9+
}else{
10+
lCommand::write("to change directory, call \"cd [directory]\"");
11+
}
12+
}
13+
14+
function change_dir($dir){
15+
$dir = rtrim($dir,"/");
16+
if($dir[0]=="/"){
17+
if(lSystem::dirExists($dir)){
18+
lSystem::setPWD($dir);
19+
return $dir;
20+
}
21+
return false;
22+
}else if($dir[0]=="."){
23+
$subDirs = explode("/", $dir);
24+
$c_dir = "";
25+
if($subDirs[0]==".."){
26+
$c_dir = implode("/",array_slice(explode("/", lSystem::getPWD()), 0, -1));
27+
if(lSystem::dirExists($c_dir)) {
28+
lSystem::setPWD($c_dir);
29+
}else{
30+
return false;
31+
}
32+
}
33+
if(count($subDirs)>1){
34+
return change_dir(implode("/", array_slice($subDirs, 1)));
35+
}
36+
return $c_dir;
37+
}else{
38+
$dir = lSystem::getPWD()."/".$dir;
39+
if(lSystem::dirExists($dir)) {
40+
lSystem::setPWD($dir);
41+
return $dir;
42+
}
43+
return false;
44+
}
45+
}
46+
?>

commandline/programms/exit/init.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
function exitMain($args, $command){
3+
lCommand::write("> $command");
4+
lCommand::write("note that exit is just an alias for logout");
5+
lCommand::setNextCommand("logout");
6+
lCommand::performNextCommand();
7+
}
8+
?>

commandline/programms/logout/init.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
function logoutMain($args, $command){
33
lCommand::write("> logout");
44
lCommand::write("logged out successfully. bye.");
5-
$_SESSION["authorized"] = false;
5+
$_SESSION["phpcommandline"]["authorized"] = false;
66
}
77
?>

commandline/programms/ls/init.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
function lsMain($args, $command){
3+
lCommand::write("> $command");
4+
$files = scandir(lSystem::getPWD());
5+
foreach($files as $f){
6+
if($f!="." and $f!="..") lCommand::write("$f");
7+
}
8+
}
9+
?>

commandline/programms/more/init.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
function moreMain($args, $command){
3+
lCommand::write("> $command");
4+
if(count($args)==1){
5+
if(lSystem::fileExists(lSystem::getPWD()."/".$args[0])){
6+
lCommand::write(nl2br(htmlentities(file_get_contents(lSystem::getPWD()."/".$args[0]))));
7+
}else{
8+
lCommand::write("file not found");
9+
}
10+
}else{
11+
lCommand::write("to view a file, call \"more [filename]\"");
12+
}
13+
}
14+
?>

commandline/programms/pwd/init.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
function pwdMain($args, $command){
3+
lCommand::write("> $command");
4+
$pwd = lSystem::getPWD();
5+
lCommand::write("$pwd");
6+
}
7+
8+
function str_replace_last( $search , $replace , $str ) {
9+
if( ( $pos = strrpos( $str , $search ) ) !== false ) {
10+
$search_length = strlen( $search );
11+
$str = substr_replace( $str , $replace , $pos , $search_length );
12+
}
13+
return $str;
14+
}
15+
?>

commandline/programms/reset/init.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
22
function resetMain($args, $command){
3-
lCommand::write("> login");
4-
lCommand::write("connection resetted successfully. you've been logged out.");
5-
$_SESSION["commandstack"] = [];
6-
$_SESSION["authorized"] = false;
7-
// lCommand::addToStack("login");
8-
// lCommand::performNextCommand();
3+
lCommand::write("> $command");
4+
lCommand::write("session storage and command stack resetted successfully. this is now a clear session.");
5+
$_SESSION["phpcommandline"]["commandstack"] = [];
6+
$_SESSION["phpcommandline"]["storage"] = [];
97
}
108
?>

commandline/programms/touch/init.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
function touchMain($args, $command){
3+
lCommand::write("> $command");
4+
if(count($args)==1){
5+
file_put_contents(lSystem::getPWD()."/".$args[0], "");
6+
}else if(count($args)==2){
7+
file_put_contents(lSystem::getPWD()."/".$args[0], $args[1]);
8+
}else{
9+
lCommand::write("to ceate a new file, call \"touch [filename]\" or \"touch [filename] [content]\"");
10+
}
11+
12+
}
13+
?>

index.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,16 @@ class CommandLine{
5757
CommandLine.historyindex = Math.max(-1, CommandLine.historyindex);
5858
CommandLine.loadHistoryCommand();
5959
}
60+
if(event.keyCode==9){
61+
event.preventDefault();
62+
CommandLine.getSuggestion();
63+
}
6064
}
6165
static sendCommand(command, pushToStack=true){
66+
if(command=="clear"){
67+
CommandLine.reset()
68+
return
69+
}
6270
if(pushToStack) CommandLine.commandhistory.unshift(command);
6371
let xhttp = new XMLHttpRequest();
6472
xhttp.onreadystatechange = function() {
@@ -74,12 +82,27 @@ class CommandLine{
7482
static print(content){
7583
document.getElementById("content").innerHTML += "<br>"+content;
7684
}
85+
static reset(content){
86+
document.getElementById("content").innerHTML = "";
87+
}
7788
static loadHistoryCommand(){
7889
if(CommandLine.historyindex==-1) document.getElementsByTagName('textarea')[0].value = "";
7990
else document.getElementsByTagName('textarea')[0].value = CommandLine.commandhistory[CommandLine.historyindex];
8091
document.getElementsByTagName('textarea')[0].focus();
8192
document.getElementsByTagName('textarea')[0].setSelectionRange(document.getElementsByTagName('textarea')[0].value.length,document.getElementsByTagName('textarea')[0].value.length);
8293
}
94+
static getSuggestion(){
95+
let input = document.getElementsByTagName('textarea')[0].value
96+
let xhttp = new XMLHttpRequest();
97+
xhttp.onreadystatechange = function() {
98+
if (this.readyState == 4 && this.status == 200) {
99+
let answer = JSON.parse(xhttp.responseText);
100+
console.log(answer);
101+
if(answer["return"]!=null) document.getElementsByTagName('textarea')[0].value += answer["return"];
102+
}
103+
};
104+
xhttp.open("GET", "commandline/autocomplete.php?input="+encodeURIComponent(input), true);
105+
xhttp.send();}
83106
}
84107
CommandLine.commandhistory = [];
85108
CommandLine.historyindex = -1;
@@ -88,6 +111,6 @@ class CommandLine{
88111
</head>
89112
<body ondblclick="document.getElementsByTagName('textarea')[0].focus();">
90113
<div id="content" class="content"></div>
91-
<div class="input"><span>$</span><textarea autofocus rows="1" onkeydown="CommandLine.parseCommand(event);"></textarea></div>
114+
<div class="input"><span>$</span><textarea autocomplete="off" spellcheck="false" autofocus rows="1" onkeydown="CommandLine.parseCommand(event);"></textarea></div>
92115
</body>
93116
</html>

0 commit comments

Comments
 (0)