Adding users list and ln command

This commit is contained in:
BlackLight 2011-01-10 22:31:56 +01:00
parent e5fbc10055
commit c6730f6dec
12 changed files with 266 additions and 8 deletions

View file

@ -49,6 +49,11 @@ span.directory
color : #2553ee; color : #2553ee;
} }
span.link
{
color : #46a4ef;
}
a a
{ {
color : green; color : green;

View file

@ -32,6 +32,8 @@
if ( dir[i].href ) if ( dir[i].href )
{ {
window.open ( dir[i].href ); window.open ( dir[i].href );
} else if ( dir[i].link_to && dir[i].link_to.length > 0 ) {
return this.action ( dir[i].link_to );
} else if ( dir[i].content ) { } else if ( dir[i].content ) {
return '<pre>' + dir[i].content + "</pre>"; return '<pre>' + dir[i].content + "</pre>";
} }

View file

@ -55,8 +55,9 @@
{ {
found = true; found = true;
if ( shell.files[i].type != 'directory' ) if ( shell.files[i].link_to && shell.files[i].link_to.length > 0 ) {
{ return this.action ( shell.files[i].link_to );
} else if ( shell.files[i].type != 'directory' ) {
return "cd: not a directory: " + arg + "<br/>\n"; return "cd: not a directory: " + arg + "<br/>\n";
} }
} }

120
commands/ln.json Normal file
View file

@ -0,0 +1,120 @@
{
"name" : "ln",
"info" : {
"syntax" : "ln &lt;path or URL&gt; &lt;link name&gt;",
"brief" : "Link a specified path inside the system or a URL to a new link file",
},
"action" : function ( arg )
{
var res = null;
var link = null;
if ( !arg || arg.length == 0 )
{
return "Usage: " + this.info.syntax + "<br/>\n";
}
if ( arg.match ( /^\s*('|")([^'|"]+)('|")/ ))
{
res = RegExp.$2;
arg = arg.replace ( new RegExp ( '^\s*' + RegExp.$1 + RegExp.$2 + RegExp.$3 + '\s*' ), '' );
} else if ( arg.match ( /^\s*([^\s]+)/ )) {
res = RegExp.$1;
arg = arg.replace ( new RegExp ( '^\s*' + RegExp.$1 + '\s*' ), '' );
} else {
return "Usage: " + this.info.syntax + "<br/>\n";
}
if ( !res || arg.length == 0 )
{
return "Usage: " + this.info.syntax + "<br/>\n";
}
if ( arg.match ( /^\s*('|")([^'|"]+)('|")/ ))
{
link = RegExp.$2;
} else {
arg.match ( /^\s*(.*)$/ );
link = RegExp.$1;
}
var link_type = null;
if ( res.match ( /^[a-z0-9]+:\/\// ))
{
link_type = 'href';
} else {
link_type = 'local';
}
var users_php = window.location.href;
users_php = users_php.replace ( /\/([a-zA-Z\.]+)$/, '/modules/users/users.php' );
shell.auto_prompt_refresh = false;
link = shell.expandPath ( link );
params = 'action=link&resource=' + escape ( res ) + '&link=' + escape ( link ) + '&type=' + escape ( link_type );
var http = new XMLHttpRequest();
http.open ( "POST", users_php, true );
http.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
http.setRequestHeader( "Content-length", params.length );
http.setRequestHeader( "Connection", "close" );
http.onreadystatechange = function ()
{
if ( http.readyState == 4 && http.status == 200 )
{
if ( http.responseText.length > 0 )
{
shell.cmdOut.innerHTML = http.responseText;
}
var files_config = window.location.href;
files_config = files_config.replace ( /\/([a-zA-Z\.]+)$/, '/modules/users/files.php' );
var http2 = new XMLHttpRequest();
http2.open ( "GET", files_config, true );
http2.onreadystatechange = function ()
{
if ( http2.readyState == 4 && http2.status == 200 )
{
shell.files = eval ( '(' + http2.responseText + ')' );
// Remove duplicates
var tmp = new Array();
for ( var i in shell.files )
{
var contains = false;
for ( var j=0; j < tmp.length && !contains; j++ )
{
if ( shell.files[i].path == tmp[j].path )
{
contains = true;
}
}
if ( !contains )
{
tmp.push ( shell.files[i] );
}
}
shell.files = tmp;
}
}
http2.send ( null );
shell.auto_prompt_refresh = true;
shell.refreshPrompt ( false, false );
}
}
http.send ( params );
shell.cmdOut.innerHTML = '';
}
}

View file

@ -51,6 +51,7 @@
"path" : RegExp.$1, "path" : RegExp.$1,
"type" : dir.type, "type" : dir.type,
"href" : dir.href, "href" : dir.href,
"link" : dir.link_to
}); });
} }
} }
@ -114,6 +115,8 @@
if ( dirs[i].href && dirs[i].href.length > 0 ) if ( dirs[i].href && dirs[i].href.length > 0 )
{ {
out += '<a href="' + dirs[i].href + '" class="file" target="_new">' + dirs[i].path + '</a>*<br/>'; out += '<a href="' + dirs[i].href + '" class="file" target="_new">' + dirs[i].path + '</a>*<br/>';
} else if ( dirs[i].link && dirs[i].link.length > 0 ) {
out += '<span class="link">' + dirs[i].path + '</span>@ -&gt; ' + dirs[i].link + '<br/>';
} else { } else {
out += dirs[i].path + '<br/>'; out += dirs[i].path + '<br/>';
} }

40
commands/users.json Normal file
View file

@ -0,0 +1,40 @@
{
"name" : "users",
"info" : {
"syntax" : "users",
"brief" : "Show the users on the system"
},
"action" : function ( arg )
{
var users_php = window.location.href;
users_php = users_php.replace ( /\/([a-zA-Z\.]+)$/, '/modules/users/users.php' );
params = 'action=list';
shell.auto_prompt_refresh = false;
var http = new XMLHttpRequest();
http.open ( "POST", users_php, true );
http.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
http.setRequestHeader( "Content-length", params.length );
http.setRequestHeader( "Connection", "close" );
http.onreadystatechange = function ()
{
if ( http.readyState == 4 && http.status == 200 )
{
if ( http.responseText.length > 0 )
{
shell.cmdOut.innerHTML = http.responseText;
}
shell.auto_prompt_refresh = true;
shell.refreshPrompt ( false, false );
}
}
http.send ( params );
shell.cmdOut.innerHTML = '';
}
}

View file

@ -259,6 +259,65 @@ function getPerms ( $resource )
return $response; return $response;
} }
function __link ( $resource, $link, $type )
{
$ret = __touch ( $link, null );
if ( strlen ( $ret ) > 0 )
{
return $ret;
}
include "../../system/files_json.php";
if ( !$files_json || strlen ( $files_json ) == 0 )
{
return 'Error: Empty JSON file container';
}
$json = json_decode ( $files_json, true );
if ( !$json )
{
return 'Error: Empty JSON file container';
}
for ( $i=0; $i < count ( $json ); $i++ )
{
$path = $json[$i]['path'];
if ( !$path || strlen ( $path ) == 0 )
{
continue;
}
if ( $path == $link )
{
unset ( $json[$i]['content'] );
if ( $type == 'href' )
{
$json[$i]['href'] = $resource;
} else if ( $type == 'local' ) {
$json[$i]['link_to'] = $resource;
} else {
return "No link type specified (href|local)\n";
}
if ( !( $fp = fopen ( "../../system/files_json.php", "w" )))
{
return "Unable to write on directories file\n";
}
fwrite ( $fp, "<?php\n\n\$files_json = <<<JSON\n".__json_encode ( $json )."\nJSON;\n\n?>");
fclose ( $fp );
return false;
}
}
return "Unable to link the resource";
}
function __json_encode( $data ) { function __json_encode( $data ) {
if ( is_array ($data) || is_object ($data) ) { if ( is_array ($data) || is_object ($data) ) {
$islist = is_array ($data) && ( empty ($data) || array_keys ($data) === range (0,count($data)-1) ); $islist = is_array ($data) && ( empty ($data) || array_keys ($data) === range (0,count($data)-1) );

View file

@ -381,6 +381,19 @@ switch ( $action )
break; break;
case 'link':
$resource = $_REQUEST['resource'];
$link = $_REQUEST['link'];
$type = $_REQUEST['type'];
if ( !( $resource && $link && $type ))
{
return false;
}
print __link ( $resource, $link, $type );
break;
default : default :
print "Unallowed action\n"; print "Unallowed action\n";
break; break;

View file

@ -392,7 +392,20 @@ function blash ()
} else if ( key == 76 && evt.ctrlKey ) { } else if ( key == 76 && evt.ctrlKey ) {
// CTRL-l clears the screen // CTRL-l clears the screen
evt.preventDefault(); evt.preventDefault();
this.refreshPrompt ( true, false ); this.refreshPrompt ( true, true );
this.cmdOut.innerHTML = '';
var text = ( this.json.promptText ) ? this.json.promptText : "[%n@%m %W] $ ";
text = this.unescapePrompt ( text, this.json.promptSequences );
var outDiv = document.createElement ( 'span' );
outDiv.innerHTML = text;
//outDiv.innerHTML = ((value.length > 0) ? value : '') +
// ( value.match ( /<br\/?>\s*$/ ) ? '' : '<br/>' ) + ((out.length > 0) ? (out + '<br/>') : '') + text + (( shell.__first_cmd ) ? '<br/>' : '' );
//outDiv.innerHTML = outDiv.innerHTML.replace ( /<br\/?>\s*$/, '' );
this.window.insertBefore ( outDiv, document.getElementsByName ( "blashPrompt" )[0] );
// this.window.appendChild ( outDiv );
return false; return false;
} else if ( key == 13 || key == 10 || ( key == 67 && evt.ctrlKey )) { } else if ( key == 13 || key == 10 || ( key == 67 && evt.ctrlKey )) {
if ( this.prompt.value.length != 0 && ( key != 67 || !evt.ctrlKey )) if ( this.prompt.value.length != 0 && ( key != 67 || !evt.ctrlKey ))

View file

@ -62,6 +62,7 @@
"find", "find",
"grep", "grep",
"logout", "logout",
"ln",
"ls", "ls",
"man", "man",
"mkdir", "mkdir",

View file

@ -1,7 +1,3 @@
/**
* Default style for blash
*/
body body
{ {
background-color : black; background-color : black;
@ -53,6 +49,11 @@ span.directory
color : #2553ee; color : #2553ee;
} }
span.link
{
color : #46a4ef;
}
a a
{ {
color : green; color : green;

View file

@ -10,7 +10,6 @@ $files_json = <<<JSON
, {"path": "/software", "type": "directory"} , {"path": "/software", "type": "directory"}
, {"path": "/etc", "type": "directory"} , {"path": "/etc", "type": "directory"}
, {"path": "/home", "type": "directory"} , {"path": "/home", "type": "directory"}
, {"path": "/google", "type": "file", "href": "http://www.google.com"}
, {"path": "/blog/post1", "type": "file", "content": "This is my first post"} , {"path": "/blog/post1", "type": "file", "content": "This is my first post"}
, {"path": "/blog/post2", "type": "file", "content": "This is my second post"} , {"path": "/blog/post2", "type": "file", "content": "This is my second post"}
, {"path": "/blog/post3", "type": "file", "content": "This is my third post"} , {"path": "/blog/post3", "type": "file", "content": "This is my third post"}
@ -29,6 +28,7 @@ $files_json = <<<JSON
, {"path": "/contacts", "type": "file", "content": "Contact me at spam@montypython.com"} , {"path": "/contacts", "type": "file", "content": "Contact me at spam@montypython.com"}
, {"path": "/irc", "type": "file", "content": "IRC channel at #thegame@irc.randomstuff.com"} , {"path": "/irc", "type": "file", "content": "IRC channel at #thegame@irc.randomstuff.com"}
, {"path": "/root", "type": "directory", "can_write": "root", "can_read": "root"} , {"path": "/root", "type": "directory", "can_write": "root", "can_read": "root"}
, {"path": "/google", "type": "file", "owner": "root", "can_read": "@all", "can_write": "root", "href": "http://www.google.com"}
] ]
JSON; JSON;