2007-05-29 10:35:27 +02:00
|
|
|
#!/usr/bin/php -qC
|
|
|
|
<?php
|
|
|
|
/**
|
2008-01-29 08:24:16 +01:00
|
|
|
* Filemanager - Command line interface: ls
|
2007-05-29 10:35:27 +02:00
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @package admin
|
|
|
|
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @copyright (c) 2006 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
chdir(dirname(__FILE__)); // to enable our relative pathes to work
|
|
|
|
|
2008-01-29 08:24:16 +01:00
|
|
|
if (isset($_SERVER['HTTP_HOST'])) // security precaution: forbit calling ls as web-page
|
2007-05-29 10:35:27 +02:00
|
|
|
{
|
2008-01-29 08:24:16 +01:00
|
|
|
die('<h1>'.basename(__FILE__).' must NOT be called as web-page --> exiting !!!</h1>');
|
2007-05-29 10:35:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* callback if the session-check fails, redirects via xajax to login.php
|
|
|
|
*
|
|
|
|
* @param array &$account account_info with keys 'login', 'passwd' and optional 'passwd_type'
|
|
|
|
* @return boolean/string true if we allow the access and account is set, a sessionid or false otherwise
|
|
|
|
*/
|
|
|
|
function user_pass_from_argv(&$account)
|
|
|
|
{
|
|
|
|
$account = $GLOBALS['egw_login_data'];
|
|
|
|
//print_r($account);
|
|
|
|
if (!($sessionid = $GLOBALS['egw']->session->create($account)))
|
|
|
|
{
|
|
|
|
echo "Wrong admin-account or -password !!!\n\n";
|
|
|
|
usage('',1);
|
|
|
|
}
|
|
|
|
return $sessionid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Give a usage message and exit
|
|
|
|
*
|
|
|
|
* @param string $action=null
|
|
|
|
* @param int $ret=0 exit-code
|
|
|
|
*/
|
|
|
|
function usage($action=null,$ret=0)
|
|
|
|
{
|
2008-01-29 08:24:16 +01:00
|
|
|
$cmd = basename(__FILE__);
|
2008-01-30 07:47:53 +01:00
|
|
|
echo "Usage:\t$cmd URL [URL2 ...]\n";
|
|
|
|
echo "\t$cmd --cat URL [URL2 ...]\n";
|
2008-01-29 08:24:16 +01:00
|
|
|
echo "\t$cmd --cp URL-from URL-to\n";
|
2008-01-30 07:47:53 +01:00
|
|
|
echo "\t$cmd --cp URL-from [URL-from2 ...] URL-to-directory\n";
|
|
|
|
echo "\t$cmd --rm URL [URL2 ...]\n";
|
|
|
|
echo "\t$cmd --mkdir [-p|--parents] URL [URL2 ...]\n";
|
|
|
|
echo "\t$cmd --rmdir URL [URL2 ...]\n";
|
|
|
|
echo "\t$cmd --touch [-d|--date time] URL [URL2 ...]\n";
|
|
|
|
echo "URL: oldvfs://user:password@domain/home/user/file, /dir/file, ...\n";
|
2007-05-29 10:35:27 +02:00
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
2008-01-30 07:47:53 +01:00
|
|
|
$long = $numeric = $recursive = false;
|
2008-01-29 08:24:16 +01:00
|
|
|
$argv = $_SERVER['argv'];
|
|
|
|
$cmd = basename(array_shift($argv),'.php');
|
|
|
|
|
|
|
|
if (!$argv) $argv = array('-h');
|
|
|
|
|
|
|
|
foreach($argv as $key => $option)
|
2007-05-29 10:35:27 +02:00
|
|
|
{
|
2008-01-29 08:24:16 +01:00
|
|
|
if ($option[0] != '-') continue;
|
|
|
|
|
|
|
|
unset($argv[$key]);
|
|
|
|
|
|
|
|
switch($option)
|
2007-05-29 10:35:27 +02:00
|
|
|
{
|
2008-01-29 08:24:16 +01:00
|
|
|
default:
|
|
|
|
case '-h': case '--help':
|
|
|
|
usage();
|
|
|
|
|
|
|
|
case '-l': case '--long':
|
|
|
|
$long = true;
|
|
|
|
continue 2; // switch is counting too!
|
|
|
|
|
|
|
|
case '-n': case '--numeric':
|
|
|
|
$numeric = true;
|
|
|
|
continue 2; // switch is counting too!
|
|
|
|
|
2008-01-30 07:47:53 +01:00
|
|
|
case '-r': case '--recursive':
|
|
|
|
case '-p': case '--parents':
|
|
|
|
$recursive = true;
|
|
|
|
continue 2; // switch is counting too!
|
|
|
|
|
|
|
|
case '-d': case '--date':
|
|
|
|
$time = strtotime($argv[$key+1]);
|
|
|
|
unset($argv[$key+1]);
|
|
|
|
break;
|
|
|
|
|
2008-01-29 08:24:16 +01:00
|
|
|
case '--cat': // cat files (!) to stdout
|
2008-01-30 07:47:53 +01:00
|
|
|
case '--cp': // copy files
|
|
|
|
case '--rm': // remove files
|
|
|
|
case '--ls': // list files
|
|
|
|
case '--rmdir': // remove dirs
|
|
|
|
case '--mkdir': // make directories
|
|
|
|
case '--rename':// rename
|
|
|
|
case '--touch': // touch
|
2008-01-29 08:24:16 +01:00
|
|
|
$cmd = substr($option,2);
|
|
|
|
continue 2; // switch is counting too!
|
2007-05-29 10:35:27 +02:00
|
|
|
}
|
2008-01-29 08:24:16 +01:00
|
|
|
}
|
|
|
|
$argv = array_values($argv);
|
|
|
|
$argc = count($argv);
|
|
|
|
|
|
|
|
switch($cmd)
|
|
|
|
{
|
|
|
|
case 'cp':
|
|
|
|
do_cp($argv);
|
|
|
|
break;
|
|
|
|
|
2008-01-30 07:47:53 +01:00
|
|
|
case 'rename':
|
|
|
|
if (count($argv) != 2) usage(null,3);
|
|
|
|
load_wrapper($argv[0]);
|
|
|
|
load_wrapper($argv[1]);
|
|
|
|
rename($argv[0],$argv[1]);
|
|
|
|
break;
|
|
|
|
|
2008-01-29 08:24:16 +01:00
|
|
|
default:
|
|
|
|
while($url = array_shift($argv))
|
|
|
|
{
|
|
|
|
load_wrapper($url);
|
|
|
|
//echo "$cmd $url (long=".(int)$long.", numeric=".(int)$numeric.")\n";
|
|
|
|
|
2008-01-30 07:47:53 +01:00
|
|
|
switch($cmd)
|
2008-01-29 08:24:16 +01:00
|
|
|
{
|
2008-01-30 07:47:53 +01:00
|
|
|
case 'rm':
|
|
|
|
unlink($url);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'rmdir':
|
|
|
|
rmdir($url);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'mkdir':
|
|
|
|
mkdir($url,null,$recursive);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'touch':
|
|
|
|
if (($scheme = parse_url($url,PHP_URL_SCHEME)))
|
2008-01-29 08:24:16 +01:00
|
|
|
{
|
2008-01-30 07:47:53 +01:00
|
|
|
load_wrapper($url);
|
|
|
|
if (class_exists($class = $scheme.'_stream_wrapper') && method_exists($class,'touch'))
|
|
|
|
{
|
|
|
|
call_user_func(array($scheme.'_stream_wrapper','touch'),$url,$time);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
die("Can't touch for scheme $scheme!\n");
|
|
|
|
}
|
2008-01-29 08:24:16 +01:00
|
|
|
}
|
2008-01-30 07:47:53 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
touch($url,$time);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'cat':
|
|
|
|
case 'ls':
|
|
|
|
default:
|
|
|
|
if (is_dir($url) && ($dir = opendir($url)))
|
|
|
|
{
|
|
|
|
if ($argc)
|
|
|
|
{
|
|
|
|
echo "\n".basename(parse_url($url,PHP_URL_PATH)).":\n";
|
|
|
|
}
|
|
|
|
while(($file = readdir($dir)) !== false)
|
|
|
|
{
|
|
|
|
do_stat($url.'/'.$file,$long,$numeric);
|
|
|
|
}
|
|
|
|
closedir($dir);
|
|
|
|
}
|
|
|
|
elseif ($cmd == 'cat')
|
|
|
|
{
|
|
|
|
if (!($f = fopen($url,'r')))
|
|
|
|
{
|
|
|
|
echo "File $url not found !!!\n\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($argc)
|
|
|
|
{
|
|
|
|
echo "\n".basename(parse_url($url,PHP_URL_PATH)).":\n";
|
|
|
|
}
|
|
|
|
fpassthru($f);
|
|
|
|
fclose($f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
do_stat($url,$long,$numeric);
|
|
|
|
}
|
|
|
|
if (!$long && $cmd == 'ls') echo "\n";
|
|
|
|
break;
|
2008-01-29 08:24:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the necessary wrapper for an url
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
*/
|
|
|
|
function load_wrapper($url)
|
|
|
|
{
|
2008-01-30 07:47:53 +01:00
|
|
|
switch($scheme = parse_url($url,PHP_URL_SCHEME))
|
2007-05-29 10:35:27 +02:00
|
|
|
{
|
|
|
|
case 'webdav':
|
|
|
|
require_once('HTTP/WebDAV/Client.php');
|
|
|
|
break;
|
|
|
|
case 'oldvfs':
|
2008-01-30 07:47:53 +01:00
|
|
|
case 'vfs':
|
2008-02-18 07:51:35 +01:00
|
|
|
case 'sqlfs':
|
2007-05-29 10:35:27 +02:00
|
|
|
if (!isset($GLOBALS['egw_info']))
|
|
|
|
{
|
|
|
|
$_GET['domain'] = parse_url($url,PHP_URL_HOST);
|
|
|
|
$GLOBALS['egw_login_data'] = array(
|
|
|
|
'login' => parse_url($url,PHP_URL_USER),
|
|
|
|
'passwd' => parse_url($url,PHP_URL_PASS),
|
|
|
|
'passwd_type' => 'text',
|
|
|
|
);
|
|
|
|
|
|
|
|
if (is_dir('/tmp')) ini_set('session.save_path','/tmp'); // regular users may have no rights to apache's session dir
|
|
|
|
|
|
|
|
$GLOBALS['egw_info'] = array(
|
|
|
|
'flags' => array(
|
2008-01-29 08:24:16 +01:00
|
|
|
'currentapp' => 'filemanager',
|
2007-05-29 10:35:27 +02:00
|
|
|
'noheader' => true,
|
|
|
|
'autocreate_session_callback' => 'user_pass_from_argv',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
include('../header.inc.php');
|
|
|
|
}
|
2008-01-30 07:47:53 +01:00
|
|
|
require_once(EGW_API_INC.'/class.'.$scheme.'_stream_wrapper.inc.php');
|
2007-05-29 10:35:27 +02:00
|
|
|
break;
|
2008-01-30 07:47:53 +01:00
|
|
|
case '': // default scheme is file and alsways available
|
2007-05-29 10:35:27 +02:00
|
|
|
break;
|
|
|
|
default:
|
2008-01-30 07:47:53 +01:00
|
|
|
if (!in_array($scheme,stream_get_wrappers()))
|
|
|
|
{
|
|
|
|
die("Unknown scheme '$scheme' in $url !!!\n\n");
|
|
|
|
}
|
|
|
|
break;
|
2007-05-29 10:35:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-29 08:24:16 +01:00
|
|
|
/**
|
|
|
|
* Give the stats for one file
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param boolean $long=false true=long listing with owner,group,size,perms, default false only filename
|
|
|
|
* @param boolean $numeric=false true=give numeric uid&gid, else resolve the id to a name
|
|
|
|
*/
|
|
|
|
function do_stat($url,$long=false,$numeric=false)
|
2007-05-29 10:35:27 +02:00
|
|
|
{
|
2008-01-29 08:24:16 +01:00
|
|
|
//echo "do_stat($url,$long,$numeric)\n";
|
2007-05-29 10:35:27 +02:00
|
|
|
$bname = basename(parse_url($url,PHP_URL_PATH));
|
|
|
|
|
2008-01-30 07:47:53 +01:00
|
|
|
if ($long && ($stat = stat($url)))
|
2007-05-29 10:35:27 +02:00
|
|
|
{
|
|
|
|
//print_r($stat);
|
|
|
|
|
|
|
|
$perms = verbosePerms($stat['mode']);
|
2008-01-29 08:24:16 +01:00
|
|
|
if ($numeric)
|
|
|
|
{
|
|
|
|
$uid = $stat['uid'];
|
|
|
|
$gid = $stat['gid'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($stat['uid'])
|
|
|
|
{
|
|
|
|
$uid = isset($GLOBALS['egw']) ? $GLOBALS['egw']->accounts->id2name($stat['uid']) : posix_getpwuid($stat['uid']);
|
|
|
|
if (is_array($uid)) $uid = $uid['name'];
|
|
|
|
}
|
2008-01-30 07:47:53 +01:00
|
|
|
if (!isset($uid)) $uid = 'root';
|
2008-01-29 08:24:16 +01:00
|
|
|
if ($stat['gid'])
|
|
|
|
{
|
2008-02-18 07:51:35 +01:00
|
|
|
$gid = isset($GLOBALS['egw']) ? $GLOBALS['egw']->accounts->id2name(-abs($stat['gid'])) : posix_getgrgid($stat['gid']);
|
2008-01-29 08:24:16 +01:00
|
|
|
if (is_array($gid)) $gid = $gid['name'];
|
|
|
|
}
|
2008-01-30 07:47:53 +01:00
|
|
|
if (!isset($gid)) $gid = 'root';
|
2008-01-29 08:24:16 +01:00
|
|
|
}
|
2007-05-29 10:35:27 +02:00
|
|
|
$size = hsize($stat['size']);
|
|
|
|
$mtime = date('Y-m-d H:i:s',$stat['mtime']);
|
|
|
|
$nlink = $stat['nlink'];
|
|
|
|
|
|
|
|
echo "$perms $nlink\t$uid\t$gid\t$size\t$mtime\t$bname\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
echo "$bname\t";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function hsize($size)
|
|
|
|
{
|
|
|
|
if ($size < 1024) return $size;
|
|
|
|
if ($size < 1024*1024) return sprintf('%3.1lfk',(float)$size/1024);
|
|
|
|
return sprintf('%3.1lfM',(float)$size/(1024*1024));
|
|
|
|
}
|
|
|
|
|
|
|
|
function verbosePerms( $in_Perms )
|
|
|
|
{
|
|
|
|
if($in_Perms & 0x1000) // FIFO pipe
|
|
|
|
{
|
|
|
|
$sP = 'p';
|
|
|
|
}
|
|
|
|
elseif($in_Perms & 0x2000) // Character special
|
|
|
|
{
|
|
|
|
$sP = 'c';
|
|
|
|
}
|
|
|
|
elseif($in_Perms & 0x4000) // Directory
|
|
|
|
{
|
|
|
|
$sP = 'd';
|
|
|
|
}
|
|
|
|
elseif($in_Perms & 0x6000) // Block special
|
|
|
|
{
|
|
|
|
$sP = 'b';
|
|
|
|
}
|
|
|
|
elseif($in_Perms & 0x8000) // Regular
|
|
|
|
{
|
|
|
|
$sP = '-';
|
|
|
|
}
|
|
|
|
elseif($in_Perms & 0xA000) // Symbolic Link
|
|
|
|
{
|
|
|
|
$sP = 'l';
|
|
|
|
}
|
|
|
|
elseif($in_Perms & 0xC000) // Socket
|
|
|
|
{
|
|
|
|
$sP = 's';
|
|
|
|
}
|
|
|
|
else // UNKNOWN
|
|
|
|
{
|
|
|
|
$sP = 'u';
|
|
|
|
}
|
|
|
|
|
|
|
|
// owner
|
|
|
|
$sP .= (($in_Perms & 0x0100) ? 'r' : '-') .
|
|
|
|
(($in_Perms & 0x0080) ? 'w' : '-') .
|
|
|
|
(($in_Perms & 0x0040) ? (($in_Perms & 0x0800) ? 's' : 'x' ) :
|
|
|
|
(($in_Perms & 0x0800) ? 'S' : '-'));
|
|
|
|
|
|
|
|
// group
|
|
|
|
$sP .= (($in_Perms & 0x0020) ? 'r' : '-') .
|
|
|
|
(($in_Perms & 0x0010) ? 'w' : '-') .
|
|
|
|
(($in_Perms & 0x0008) ? (($in_Perms & 0x0400) ? 's' : 'x' ) :
|
|
|
|
(($in_Perms & 0x0400) ? 'S' : '-'));
|
|
|
|
|
|
|
|
// world
|
|
|
|
$sP .= (($in_Perms & 0x0004) ? 'r' : '-') .
|
|
|
|
(($in_Perms & 0x0002) ? 'w' : '-') .
|
|
|
|
(($in_Perms & 0x0001) ? (($in_Perms & 0x0200) ? 't' : 'x' ) :
|
|
|
|
(($in_Perms & 0x0200) ? 'T' : '-'));
|
|
|
|
return $sP;
|
|
|
|
}
|
|
|
|
|
2008-01-29 08:24:16 +01:00
|
|
|
function do_cp($argv)
|
|
|
|
{
|
|
|
|
$to = array_pop($argv);
|
|
|
|
load_wrapper($to);
|
|
|
|
|
|
|
|
if (count($argv) > 1 && !is_dir($to))
|
|
|
|
{
|
2008-01-30 07:47:53 +01:00
|
|
|
usage(null,4);
|
2008-01-29 08:24:16 +01:00
|
|
|
}
|
|
|
|
if (count($argv) > 1)
|
|
|
|
{
|
|
|
|
foreach($argv as $from)
|
|
|
|
{
|
|
|
|
do_cp(array($from,$to));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$from = array_shift($argv);
|
|
|
|
load_wrapper($from);
|
|
|
|
|
|
|
|
if (!($from_fp = fopen($from,'r')))
|
|
|
|
{
|
2008-01-30 07:47:53 +01:00
|
|
|
die("File $from not found!\n");
|
2008-01-29 08:24:16 +01:00
|
|
|
}
|
|
|
|
if (is_dir($to))
|
|
|
|
{
|
|
|
|
$path = parse_url($from,PHP_URL_PATH);
|
|
|
|
$to .= '/'.basename($path);
|
|
|
|
}
|
|
|
|
if (!($to_fp = fopen($to,'w')))
|
|
|
|
{
|
2008-01-30 07:47:53 +01:00
|
|
|
die("Can't open $to for writing!\n");
|
2008-01-29 08:24:16 +01:00
|
|
|
}
|
|
|
|
$count = stream_copy_to_stream($from_fp,$to_fp);
|
|
|
|
|
|
|
|
echo hsize($count)." bytes written to $to\n";
|
|
|
|
|
|
|
|
fclose($from_fp);
|
|
|
|
|
|
|
|
if (!fclose($to_fp))
|
|
|
|
{
|
|
|
|
die("Error closing $to!\n");
|
|
|
|
}
|
|
|
|
}
|