cp and mv commands implemented

This commit is contained in:
BlackLight 2011-01-11 12:18:45 +01:00
parent c6730f6dec
commit b1b6206baa
20 changed files with 336 additions and 373 deletions

View file

@ -48,11 +48,7 @@
{ {
if ( http.readyState == 4 && http.status == 200 ) if ( http.readyState == 4 && http.status == 200 )
{ {
if ( http.responseText.length > 0 ) shell.cmdOut.innerHTML = http.responseText;
{
shell.cmdOut.innerHTML = http.responseText;
}
shell.auto_prompt_refresh = true; shell.auto_prompt_refresh = true;
shell.refreshPrompt ( false, false ); shell.refreshPrompt ( false, false );
} }

73
commands/cp.json Normal file
View file

@ -0,0 +1,73 @@
{
"name" : "cp",
"info" : {
"syntax" : "cp <source file> <destination file>",
"brief" : "Copy a file to another",
},
"action" : function ( arg )
{
var src = null;
var dest = null;
if ( !arg || arg.length == 0 )
{
return "Usage: " + this.info.syntax + "<br/>\n";
}
if ( arg.match ( /^\s*('|")([^'|"]+)('|")/ ))
{
src = RegExp.$2;
arg = arg.replace ( new RegExp ( '^\s*' + RegExp.$1 + RegExp.$2 + RegExp.$3 + '\s*' ), '' );
} else if ( arg.match ( /^\s*([^\s]+)/ )) {
src = RegExp.$1;
arg = arg.replace ( new RegExp ( '^\s*' + RegExp.$1 + '\s*' ), '' );
} else {
return "Usage: " + this.info.syntax + "<br/>\n";
}
if ( !src || arg.length == 0 )
{
return "Usage: " + this.info.syntax + "<br/>\n";
}
if ( arg.match ( /^\s*('|")([^'|"]+)('|")/ ))
{
dest = RegExp.$2;
} else {
arg.match ( /^\s*(.*)$/ );
dest = RegExp.$1;
}
src = shell.expandPath ( src );
dest = shell.expandPath ( dest );
shell.auto_prompt_refresh = false;
var users_php = window.location.href;
users_php = users_php.replace ( /\/([a-zA-Z\.]+)$/, '/modules/users/users.php' );
params = 'action=cp&src=' + escape ( src ) + '&dest=' + escape ( dest );
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 )
{
shell.cmdOut.innerHTML = http.responseText;
shell.refreshFiles();
shell.refreshPrompt ( false, false );
shell.auto_prompt_refresh = true;
}
}
http.send ( params );
shell.cmdOut.innerHTML = '';
}
}

View file

@ -65,49 +65,8 @@
{ {
if ( http.readyState == 4 && http.status == 200 ) if ( http.readyState == 4 && http.status == 200 )
{ {
if ( http.responseText.length > 0 ) shell.cmdOut.innerHTML = http.responseText;
{ shell.refreshFiles();
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.auto_prompt_refresh = true;
shell.refreshPrompt ( false, false ); shell.refreshPrompt ( false, false );
} }

View file

@ -38,44 +38,7 @@
{ {
if ( http.readyState == 4 && http.status == 200 ) if ( http.readyState == 4 && http.status == 200 )
{ {
var files_config = window.location.href; shell.refreshFiles();
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 );
} }
} }

View file

@ -31,51 +31,8 @@
{ {
if ( http.readyState == 4 && http.status == 200 ) if ( http.readyState == 4 && http.status == 200 )
{ {
if ( http.responseText.length > 0 ) shell.cmdOut.innerHTML = http.responseText;
{ shell.refreshFiles();
shell.cmdOut.innerHTML = http.responseText;
} else {
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.cmdOut.innerHTML = '';
}
shell.refreshPrompt ( false, false ); shell.refreshPrompt ( false, false );
shell.auto_prompt_focus = true; shell.auto_prompt_focus = true;
shell.auto_prompt_refresh = true; shell.auto_prompt_refresh = true;

73
commands/mv.json Normal file
View file

@ -0,0 +1,73 @@
{
"name" : "mv",
"info" : {
"syntax" : "mv &lt;source file&gt; &lt;destination file&gt;",
"brief" : "Move a file to another",
},
"action" : function ( arg )
{
var src = null;
var dest = null;
if ( !arg || arg.length == 0 )
{
return "Usage: " + this.info.syntax + "<br/>\n";
}
if ( arg.match ( /^\s*('|")([^'|"]+)('|")/ ))
{
src = RegExp.$2;
arg = arg.replace ( new RegExp ( '^\s*' + RegExp.$1 + RegExp.$2 + RegExp.$3 + '\s*' ), '' );
} else if ( arg.match ( /^\s*([^\s]+)/ )) {
src = RegExp.$1;
arg = arg.replace ( new RegExp ( '^\s*' + RegExp.$1 + '\s*' ), '' );
} else {
return "Usage: " + this.info.syntax + "<br/>\n";
}
if ( !src || arg.length == 0 )
{
return "Usage: " + this.info.syntax + "<br/>\n";
}
if ( arg.match ( /^\s*('|")([^'|"]+)('|")/ ))
{
dest = RegExp.$2;
} else {
arg.match ( /^\s*(.*)$/ );
dest = RegExp.$1;
}
src = shell.expandPath ( src );
dest = shell.expandPath ( dest );
shell.auto_prompt_refresh = false;
var users_php = window.location.href;
users_php = users_php.replace ( /\/([a-zA-Z\.]+)$/, '/modules/users/users.php' );
params = 'action=mv&src=' + escape ( src ) + '&dest=' + escape ( dest );
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 )
{
shell.cmdOut.innerHTML = http.responseText;
shell.refreshFiles();
shell.refreshPrompt ( false, false );
shell.auto_prompt_refresh = true;
}
}
http.send ( params );
shell.cmdOut.innerHTML = '';
}
}

View file

@ -388,46 +388,8 @@
if ( http.responseText.length > 0 ) if ( http.responseText.length > 0 )
{ {
shell.editor_status.innerHTML = http.responseText; shell.editor_status.innerHTML = http.responseText;
shell.refreshFiles();
setTimeout ( 'shell.editor_status.innerHTML = shell.default_editor_status', 1500 ); setTimeout ( 'shell.editor_status.innerHTML = shell.default_editor_status', 1500 );
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 );
} }
} }
} }

View file

@ -72,13 +72,7 @@
{ {
if ( http.readyState == 4 && http.status == 200 ) if ( http.readyState == 4 && http.status == 200 )
{ {
if ( http.responseText.length > 0 ) shell.cmdOut.innerHTML = http.responseText;
{
shell.cmdOut.innerHTML = http.responseText;
} else {
shell.cmdOut.innerHTML = '';
}
shell.refreshPrompt ( false, false ); shell.refreshPrompt ( false, false );
} }
} }

View file

@ -31,51 +31,8 @@
{ {
if ( http.readyState == 4 && http.status == 200 ) if ( http.readyState == 4 && http.status == 200 )
{ {
if ( http.responseText.length > 0 ) shell.cmdOut.innerHTML = http.responseText;
{ shell.refreshFiles();
shell.cmdOut.innerHTML = http.responseText;
} else {
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.cmdOut.innerHTML = '';
}
shell.refreshPrompt ( false, false ); shell.refreshPrompt ( false, false );
shell.auto_prompt_focus = true; shell.auto_prompt_focus = true;
shell.auto_prompt_refresh = true; shell.auto_prompt_refresh = true;

View file

@ -31,51 +31,8 @@
{ {
if ( http.readyState == 4 && http.status == 200 ) if ( http.readyState == 4 && http.status == 200 )
{ {
if ( http.responseText.length > 0 ) shell.cmdOut.innerHTML = http.responseText;
{ shell.refreshFiles();
shell.cmdOut.innerHTML = http.responseText;
} else {
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.cmdOut.innerHTML = '';
}
shell.refreshPrompt ( false, false ); shell.refreshPrompt ( false, false );
shell.auto_prompt_focus = true; shell.auto_prompt_focus = true;
shell.auto_prompt_refresh = true; shell.auto_prompt_refresh = true;

View file

@ -73,45 +73,7 @@
{ {
var user = RegExp.$1; var user = RegExp.$1;
shell.user = user; shell.user = user;
shell.refreshFiles();
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 );
var xml2 = new XMLHttpRequest(); var xml2 = new XMLHttpRequest();
xml2.open ( "POST", users_php, true ); xml2.open ( "POST", users_php, true );

View file

@ -31,51 +31,8 @@
{ {
if ( http.readyState == 4 && http.status == 200 ) if ( http.readyState == 4 && http.status == 200 )
{ {
if ( http.responseText.length > 0 ) shell.cmdOut.innerHTML = http.responseText;
{ shell.refreshFiles();
shell.cmdOut.innerHTML = http.responseText;
} else {
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.cmdOut.innerHTML = '';
}
shell.refreshPrompt ( false, false ); shell.refreshPrompt ( false, false );
shell.auto_prompt_focus = true; shell.auto_prompt_focus = true;
shell.auto_prompt_refresh = true; shell.auto_prompt_refresh = true;

View file

@ -59,13 +59,7 @@
{ {
if ( http.readyState == 4 && http.status == 200 ) if ( http.readyState == 4 && http.status == 200 )
{ {
if ( http.responseText.length > 0 ) shell.cmdOut.innerHTML = http.responseText;
{
shell.cmdOut.innerHTML = http.responseText;
} else {
shell.cmdOut.innerHTML = '';
}
shell.auto_prompt_focus = true; shell.auto_prompt_focus = true;
shell.auto_prompt_refresh = true; shell.auto_prompt_refresh = true;
shell.refreshPrompt ( false, false ); shell.refreshPrompt ( false, false );

View file

@ -29,11 +29,7 @@
{ {
if ( http.readyState == 4 && http.status == 200 ) if ( http.readyState == 4 && http.status == 200 )
{ {
if ( http.responseText.length > 0 ) shell.cmdOut.innerHTML = http.responseText;
{
shell.cmdOut.innerHTML = http.responseText;
}
shell.auto_prompt_refresh = true; shell.auto_prompt_refresh = true;
shell.refreshPrompt ( false, false ); shell.refreshPrompt ( false, false );
} }

View file

@ -23,11 +23,7 @@
{ {
if ( http.readyState == 4 && http.status == 200 ) if ( http.readyState == 4 && http.status == 200 )
{ {
if ( http.responseText.length > 0 ) shell.cmdOut.innerHTML = http.responseText;
{
shell.cmdOut.innerHTML = http.responseText;
}
shell.auto_prompt_refresh = true; shell.auto_prompt_refresh = true;
shell.refreshPrompt ( false, false ); shell.refreshPrompt ( false, false );
} }

View file

@ -318,6 +318,111 @@ function __link ( $resource, $link, $type )
return "Unable to link the resource"; return "Unable to link the resource";
} }
function __cp ( $src, $dest )
{
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';
}
$src_index = -1;
$dest_index = -1;
for ( $i=0; $i < count ( $json ) && ( $src_index == -1 || $dest_index == -1 ); $i++ )
{
if ( $json[$i]['path'] == $src )
{
$src_index = $i;
}
if ( $json[$i]['path'] == $dest )
{
$dest_index = $i;
}
}
if ( $src_index == -1 )
{
$src = str_replace ( '<', '&lt;', $src );
$src = str_replace ( '>', '&gt;', $src );
return "cp: Cannot stat ".$src.": No such file or directory\n";
}
if ( $dest_index == -1 )
{
$ret = __touch ( $dest, 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 ) && $dest_index == -1; $i++ )
{
$out .= $json[$i]['path']. ", ";
if ( $json[$i]['path'] == $dest )
{
$dest_index = $i;
}
}
}
if ( $dest_index == -1 )
{
$dest = str_replace ( '<', '&lt;', $dest );
$dest = str_replace ( '>', '&gt;', $dest );
return "cp: Could not create the file $dest\n";
}
foreach ( array_keys ( $json[$dest_index] ) as $key )
{
if ( $key != 'path' )
{
unset ( $json[$dest_index][$key] );
}
}
foreach ( array_keys ( $json[$src_index] ) as $key )
{
if ( $key != 'path' )
{
$json[$dest_index][$key] = $json[$src_index][$key];
}
}
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;
}
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

@ -394,6 +394,25 @@ switch ( $action )
print __link ( $resource, $link, $type ); print __link ( $resource, $link, $type );
break; break;
case 'cp':
case 'mv':
$src = $_REQUEST['src'];
$dest = $_REQUEST['dest'];
if ( !( $src && $dest ))
{
return false;
}
print __cp ( $src, $dest );
if ( $action == 'mv' )
{
print __rm ( $src );
}
break;
default : default :
print "Unallowed action\n"; print "Unallowed action\n";
break; break;

View file

@ -400,11 +400,7 @@ function blash ()
var outDiv = document.createElement ( 'span' ); var outDiv = document.createElement ( 'span' );
outDiv.innerHTML = text; 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.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 )) {
@ -942,5 +938,50 @@ function blash ()
return false; return false;
} }
/**
* \brief Refresh the file list for the current user
*/
this.refreshFiles = function ()
{
var files_config = window.location.href;
files_config = files_config.replace ( /\/([a-zA-Z\.]+)$/, '/modules/users/files.php' );
var http = new XMLHttpRequest();
http.open ( "GET", files_config, true );
http.onreadystatechange = function ()
{
if ( http.readyState == 4 && http.status == 200 )
{
shell.files = eval ( '(' + http.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;
}
}
http.send ( null );
}
} }

View file

@ -55,6 +55,7 @@
"commands" : [ "commands" : [
"cat", "cat",
"cd", "cd",
"cp",
"chmod", "chmod",
"clear", "clear",
"echo", "echo",
@ -66,6 +67,7 @@
"ls", "ls",
"man", "man",
"mkdir", "mkdir",
"mv",
"nano", "nano",
"passwd", "passwd",
"pwd", "pwd",

View file

@ -24,11 +24,11 @@ $files_json = <<<JSON
, {"path": "/tutorials/tut1", "type": "file", "href": "/software/tut1.pdf"} , {"path": "/tutorials/tut1", "type": "file", "href": "/software/tut1.pdf"}
, {"path": "/tutorials/tut2", "type": "file", "href": "/software/tut2.pdf"} , {"path": "/tutorials/tut2", "type": "file", "href": "/software/tut2.pdf"}
, {"path": "/github", "type": "file", "href": "https://github.com/BlackLight/blash"} , {"path": "/github", "type": "file", "href": "https://github.com/BlackLight/blash"}
, {"path": "/aboutme", "type": "file", "content": "Luke, I am your father"}
, {"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"} , {"path": "/google", "type": "file", "owner": "root", "can_read": "@all", "can_write": "root", "href": "http://www.google.com"}
, {"path": "/aboutme", "type": "file", "content": "Luke, I am your father"}
] ]
JSON; JSON;