metanohi-misc-subsites/projects/webcli/script.php

275 lines
6.5 KiB
PHP

<?php
/*
This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://sam.zoy.org/wtfpl/COPYING for more details.
*/
/*
The above is true for both the PHP code and the JavaScript code in this
file, but also the mix of XML and JavaScript code found in commands.js.
*/
header("Content-type: text/javascript");
$o=fopen("commands.js","r");
$a=fread($o,filesize("commands.js"));
fclose($o);
$a=str_replace("''","\&quot;",str_replace("&gt;".chr(13).chr(10),">\n",str_replace(">","&gt;",str_replace("\n&lt;","\n<",str_replace("<","&lt;",str_replace("\&","&amp;",str_replace("&&","&amp;&amp;",$a)))))));
$a="<".substr($a,4,strlen($a)-8).">";
$xml=new SimpleXMLElement($a);
//$xml=simplexml_load_file("commands.js");
$i=0;
foreach($xml->children() as $child) {
$newfunction=true;
$childname=$child->getName();
$ifpl="";
$what="None";
$param="None";
foreach($child->attributes() as $a=>$b) {
if ($a=="rel" && $b!="") {
$newfunction=false;
$childname=$b;
}
else if ($a=="what" && $b!="") {
$what=$b;
}
else if ($a=="param" && $b!="") {
$param=$b;
}
else if ($a=="pl" && $b!="") {
$pl=$b;
$apl.="\nelse if (pl==\"".$pl."\") pl_".strtolower($pl)."()";
$ifpl="\npl=\"".$pl."\"";
foreach($child->children() as $nchild) {
$plfunctions.="\n\nfunction pl_".strtolower($pl)."() {".$nchild."}";
}
}
}
if (!$newfunction) {
foreach($xml->children() as $childb) {
if ($childb->getName()==$childname) {
foreach($childb->attributes() as $ab=>$bb) {
if ($ab=="what" && $bb!="") {
if ($what!="None") {
$bb=$what;
}
$what=$bb." (does the same as [".$childb->getName()."])";
}
else if ($ab=="param" && $bb!="") {
if ($param!="None") {
$bb=$param;
}
$param=$bb." (the same as [".$childb->getName()."])";
}
}
}
}
}
else {
$allfunctions.="\n\nfunction command_".$child->getName()."() {".$ifpl.$child."}";
}
$determinefunction.="\nelse if (ca==\"".$child->getName()."\") command_".$childname."()";
$determinewhat.="\nelse if (cb==\"".$child->getName()."\") return \"Description: ".$what."\\nParameters: ".$param."\"";
$allcommands[$i]="\\n".$child->getName().": ".$what;
$allcommandsdetailed[$i]="\\nThe \\\"".$child->getName()."\\\" command\\nDescription: ".$what."\\nParameters: ".$param."\\n";
//$allcommands.="\\n".$child->getName().": ".$what;
//$allcommandsdetailed.="\\nThe \\\"".$child->getName()."\\\" command\\nDescription: ".$what."\\nParameters: ".$param."\\n";
$commandexists.=" || cb==\"".$child->getName()."\"";
$i++;
}
$allfunctions=substr($allfunctions,2);
$determinewhat=substr($determinewhat,6);
sort($allcommands);
while (list($key, $val) = each($allcommands)) {
$nallcommands.=$val;
}
sort($allcommandsdetailed);
while (list($key, $val) = each($allcommandsdetailed)) {
$nallcommandsdetailed.=$val;
}
$allcommands="";
$allcommandsdetailed="";
$allcommands=substr($nallcommands,2);
$allcommandsdetailed=substr($nallcommandsdetailed,2);
$commandexists=substr($commandexists,4);
include("functions.js");
echo "\n\n".$allfunctions.$plfunctions;
?>
function add(sometext,l,ll) {
nn="\n\n"
if (l) nn="\n"
if (ll) nn=""
t.value+=sometext+nn+pl+">"
t.focus()
}
function resize() {
if (self.innerHeight) h=self.innerHeight
else if (document.documentElement && document.documentElement.clientHeight) h=document.documentElement.clientHeight
else if (document.body) h=document.body.clientHeight
t.style.height=h-2+"px"
scrolldown()
}
function focusafterawhile() {
if (focuswait) {
focuswait=false
setTimeout("focusafterawhile()",250)
}
else
t.focus()
}
function scrolldown() {
th=t.scrollHeight
if (th>h) t.scrollTop=th
}
function onenter(e) {
if (!enterdown) {
if (window.event) keycode=window.event.keyCode
else if (e) keycode=e.which
enterdown=true
setTimeout("onenter()",50)
}
else {
enterdown=false
if (keycode!=13) return
msga=t.value.split("\n")
msgl=msga.length
msg=msga[msgl-2].substr(1+pl.length)
//msg=msg.toLowerCase()
msgl=msg.length
if (ie() || opera()) {
msgl-=1
msg=msg.substr(0,msgl)
}
msgps=new Array()
c=0
cc=0
qtd=false
for (i=0; i<msgl; i+=1) {
msgt=msg.substr(i,1)
if (msgt=="\"") {
if (qtd) qtd=false
else qtd=true
}
else if (msgt==" " && !qtd) {
msgps[c]=msg.substr(cc,i-cc).replace("\"","").replace("\"","")
c+=1
cc=i+1
}
}
msgps[c]=msg.substr(cc,i-cc).replace("\"","").replace("\"","")
cac=msgps[0]
cbc=msgps[1]
ccc=msgps[2]
ca=false
cb=false
cc=false
if (!cbc)
ca=cac.toLowerCase()
else if (!ccc) {
ca=cac.toLowerCase()
cb=cbc.toLowerCase()
}
else {
ca=cac.toLowerCase()
cb=cbc.toLowerCase()
cc=ccc.toLowerCase()
}
if (pl=="") {
if (ca=="") add("",true,true)<?php echo $determinefunction."\n"; ?>
else add("Error! The command \""+ca+"\" doesn't exist! Type help for help.")
}
else {
if (ca=="") add("",true,true)
else if (ca=="/exit") exit()<?php echo $apl; ?>
}
if (opera()) setCaretToEnd(t)
scrolldown()
}
}
function start() {
document.title="webCLI"
stylelink=document.createElement("link")
stylelink.href="style.css"
stylelink.type="text/css"
stylelink.rel="stylesheet"
head=document.getElementsByTagName("head")[0]
head.appendChild(stylelink)
enterdown=false
allcommands="<?php echo $allcommands; ?>"
allcommandsdetailed="<?php echo $allcommandsdetailed; ?>"
anirunning=false
svsn=0
svs=new Array()
startmessage="webCLI [version 0.01]\nwebCLI is free software. No warranty blah blah."
if (opera()) beware="\nYou are using Opera, and webCLI is, for some reason, not working perfectly in Opera. Most of it works, though. You might want to consider using Firefox, where webCLI works perfectly."
else if (ie()) beware="\nYou are using Internet Explorer, and webCLI works okay in this browser. However, you might want to consider using Firefox, where webCLI works perfectly."
else beware=""
pl=""
cookie=navigator.cookieEnabled
if (cookie) {
var coie=getCookie("mem")
if (coie) {
coie=coie.split("\n")
csvn=coie.length
}
else csvn=0
}
img1=new Image()
img1.src="img/m1.png"
img2=new Image()
img2.src="img/m2.png"
img3=new Image()
img3.src="img/m3.png"
img4=new Image()
img4.src="img/road.png"
bdy=document.body
t=document.createElement("textarea")
bdy.appendChild(t)
resize()
t.innerHTML=""
add(startmessage+beware,true)
focuswait=true
if (ie() || opera()) setCaretToEnd(t)
focusafterawhile()
}
window.onload=start
window.onresize=resize
document.onkeydown=onenter