Skip to content

Commit 7bcb663

Browse files
committed
initial commit
0 parents  commit 7bcb663

File tree

13 files changed

+267
-0
lines changed

13 files changed

+267
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.user

commandline/index.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
require 'library/init.php';
3+
header("Content-Type: text/json;");
4+
5+
lConnection::init();
6+
7+
$command = (isset($_GET["command"]) and $_GET["command"]!="") ? $_GET["command"] : null;
8+
if($command!=null){
9+
lCommand::addToStack($command);
10+
}
11+
if(!lConnection::isAuthorized()){
12+
lCommand::addToStackIfNotExists("login");
13+
}
14+
15+
lCommand::performNextCommand();
16+
17+
lJSON::dump(lCommand::getResult());
18+
?>

commandline/library/init.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
require 'lConnection.php';
3+
require 'lCommand.php';
4+
require 'lStorage.php';
5+
require 'lJSON.php';
6+
?>

commandline/library/lCommand.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
abstract class lCommand{
3+
static function addToStack($command){
4+
array_push($_SESSION["commandstack"], $command);
5+
}
6+
7+
static function addToStackIfNotExists($command){
8+
if(!lCommand::existsCommandInStack($command)) lCommand::addToStack($command);
9+
}
10+
11+
static function existsCommandInStack($command){
12+
foreach($_SESSION["commandstack"] as $c){
13+
$programm = explode(" ", $c)[0];
14+
if($programm==$command || $c==$command) return true;
15+
}
16+
return false;
17+
}
18+
19+
static function getResult(){
20+
return $_SESSION["commandresult"];
21+
}
22+
23+
static function write($result){
24+
$_SESSION["commandresult"] .= (strlen($_SESSION["commandresult"])==0 ? "" : "<br>").$result;
25+
}
26+
27+
private static function popCommand(){
28+
$_SESSION["commandstack"] = array_slice($_SESSION["commandstack"], 1);
29+
}
30+
31+
private static function getCurrentCommand(){
32+
return count($_SESSION["commandstack"])>0 ? $_SESSION["commandstack"][0] : "";
33+
}
34+
35+
static function getNextItemAndPop(){
36+
$command = lCommand::getCurrentCommand();
37+
lCommand::popCommand();
38+
return $command;
39+
}
40+
41+
static function performNextCommand(){
42+
$command = lCommand::getCurrentCommand();
43+
lCommand::popCommand();
44+
45+
// determine command
46+
$splits = explode(" ",$command);
47+
$program = $splits[0];
48+
$args = array_slice($splits, 1);
49+
50+
// call command
51+
if(lConnection::isAuthorized() or $program=="login"){
52+
if(file_exists("programms/$program/init.php")){
53+
require_once("programms/$program/init.php");
54+
$programmName = $program."Main";
55+
$programmName($args, $command);
56+
}else{
57+
lCommand::write("command \"$command\" not found");
58+
}
59+
}else{
60+
lCommand::write("not authorized to run this command");
61+
}
62+
}
63+
}
64+
?>

commandline/library/lConnection.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
abstract class lConnection{
3+
static function init(){
4+
session_start();
5+
$_SESSION["commandresult"] = "";
6+
if(!isset($_SESSION["authorized"])){
7+
$_SESSION["authorized"] = false;
8+
$_SESSION["commandstack"] = array();
9+
$_SESSION["storage"] = array();
10+
}
11+
}
12+
13+
static function isAuthorized(){
14+
return $_SESSION["authorized"];
15+
}
16+
17+
static function authorize($username, $password){
18+
$userfile = "users/$username.user";
19+
if(file_exists($userfile) and file_get_contents($userfile)==$password){
20+
$_SESSION["authorized"] = true;
21+
return true;
22+
}
23+
return false;
24+
}
25+
}
26+
?>

commandline/library/lJSON.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
abstract class lJSON{
3+
static function dump($object){
4+
echo json_encode([
5+
"return" => $object,
6+
"stack" => $_SESSION["commandstack"]
7+
]);
8+
}
9+
}
10+
?>

commandline/library/lStorage.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
abstract class lStorage{
3+
static $program = ".main";
4+
5+
static function save($key, $value){
6+
if(!isset($_SESSION["storage"][lStorage::$program])) $_SESSION["storage"][lStorage::$program] = [];
7+
$_SESSION["storage"][lStorage::$program][$key] = $value;
8+
}
9+
10+
static function get($key){
11+
return $_SESSION["storage"][lStorage::$program][$key];
12+
}
13+
}
14+
?>

commandline/programms/echo/init.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
function echoMain($args, $command){
3+
lCommand::write("> $command");
4+
$str = "";
5+
foreach($args as $a) $str.= "$a ";
6+
lCommand::write($str);
7+
}
8+
?>

commandline/programms/login/init.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
function loginMain($args, $command){
3+
if(count($args)==0){
4+
lCommand::write("> login");
5+
lCommand::write("username?");
6+
lCommand::addToStack("login -waitforusername");
7+
}
8+
if(count($args)==1){
9+
if($args[0]=="-waitforusername"){
10+
$username = lCommand::getNextItemAndPop();
11+
lStorage::save("username", $username);
12+
lCommand::write("password?");
13+
lCommand::addToStack("login -waitforpassword");
14+
}
15+
else if($args[0]=="-waitforpassword"){
16+
$password = lCommand::getNextItemAndPop();
17+
$username = lStorage::get("username");
18+
if(lConnection::authorize($username, $password)){
19+
lCommand::write("welcome, ".$username);
20+
}else{
21+
lCommand::write("not authorized");
22+
lCommand::addToStack("login");
23+
lCommand::performNextCommand();
24+
}
25+
}
26+
}
27+
28+
}
29+
?>

commandline/programms/logout/init.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
function logoutMain($args, $command){
3+
lCommand::write("> logout");
4+
lCommand::write("logged out successfully. bye.");
5+
$_SESSION["authorized"] = false;
6+
}
7+
?>

commandline/programms/reset/init.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
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();
9+
}
10+
?>

commandline/users/.htaccess

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deny from all

index.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
?>
4+
5+
<html>
6+
<head>
7+
<title>Command Line</title>
8+
<meta charset="utf-8">
9+
<style>
10+
html, body{
11+
padding: 12px;
12+
background: #000;
13+
color: #fff;
14+
font-size: 16px !important;
15+
font-family: "Menlo", "Raster Fonts", "Monospace";
16+
}
17+
18+
#content{
19+
position: absolute;
20+
bottom: 50px;
21+
}
22+
23+
textarea{
24+
float: right;
25+
outline: none;
26+
border: none;
27+
background: transparent;
28+
width: calc(100% - 15px);
29+
resize: none;
30+
color: #fff !important;
31+
font-size: 16px !important;
32+
font-family: "Menlo", "Raster Fonts", "Monospace";
33+
}
34+
.input{
35+
position: absolute;
36+
bottom: 16px;
37+
}
38+
.input span{
39+
line-height: 27px;
40+
}
41+
</style>
42+
<script>
43+
class CommandLine{
44+
static parseCommand(event){
45+
if(event.keyCode==13){
46+
event.preventDefault();
47+
CommandLine.sendCommand(event.srcElement.value)
48+
event.srcElement.value = "";
49+
}
50+
}
51+
static sendCommand(command){
52+
let xhttp = new XMLHttpRequest();
53+
xhttp.onreadystatechange = function() {
54+
if (this.readyState == 4 && this.status == 200) {
55+
let answer = JSON.parse(xhttp.responseText);
56+
console.log(answer);
57+
CommandLine.print(answer["return"]);
58+
}
59+
};
60+
xhttp.open("GET", "commandline/?command="+encodeURI(command), true);
61+
xhttp.send();
62+
}
63+
static print(content){
64+
document.getElementById("content").innerHTML += "<br>"+content;
65+
}
66+
}
67+
</script>
68+
</head>
69+
<body onclick="document.getElementsByTagName('textarea')[0].focus();">
70+
<div id="content" class="content"></div>
71+
<div class="input"><span>$</span><textarea autofocus rows="1" onkeydown="CommandLine.parseCommand(event);"></textarea></div>
72+
</body>
73+
</html>

0 commit comments

Comments
 (0)