/usr/bin/horde-vfs is in php-horde-vfs 2.2.0-4.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | #!/usr/bin/php
<?php
/**
* This is a command line interface for the VFS package.
*
* `vfs.php help' shows some usage instructions.
*
* @package Vfs
*/
/** PEAR */
require_once 'PEAR.php';
/** Console_Getopt */
require_once 'Console/Getopt.php';
/** DB */
require_once 'DB.php';
/** VFS */
require_once 'VFS.php';
/* Track errors. */
ini_set('track_errors', true);
/* Get command line options. */
$argv = Console_Getopt::readPHPArgv();
if (is_a($argv, 'PEAR_Error')) {
usage($argv->getMessage());
}
$cmd = array_shift($argv);
$options = Console_Getopt::getopt2($argv, '', array());
if (is_a($options, 'PEAR_Error')) {
usage($options->getMessage());
}
/* Show help? */
if (!count($options[1]) || in_array('help', $options[1])) {
usage();
}
/* Get and execute the command. */
$command = array_shift($options[1]);
switch ($command) {
case 'ls':
if (!count($options[1])) {
usage($command);
}
$params = Console_Getopt::getopt2($options[1], 'alR');
if (is_a($params, 'PEAR_Error')) {
usage($params->getMessage());
}
$path = array_shift($params[1]);
ls($path, mergeOptions($params[0]), $params[1]);
break;
case 'cp':
if (!count($options[1])) {
usage($command);
}
$params = Console_Getopt::getopt2($options[1], 'arv');
if (is_a($params, 'PEAR_Error')) {
usage($params->getMessage());
}
$source = array_shift($params[1]);
$target = array_shift($params[1]);
cp($source, $target, mergeOptions($params[0]), $params[1]);
break;
default:
usage();
break;
}
/**
* Lists the contents of the specified directory.
*
* @param string $url The URL of the VFS backend
* @param array $argv Additional options
* @param string $filter Additional parameters
*/
function ls($url, $argv, $filter)
{
$params = url2params($url);
$recursive = in_array('R', $argv);
$vfs = vfs($params);
try {
$list = $vfs->listFolder($params['path'],
count($filter) ? $filter[0] : null,
in_array('a', $argv));
} catch (Horde_Vfs_Exception $e) {
usage($e);
}
if (in_array('a', $argv)) {
$list = array_merge(array('.' => array('name' => '.'),
'..' => array('name' => '..')),
$list);
}
$list = array_keys($list);
$max = max(array_map(create_function('$a', 'return strlen($a);'), $list)) + 2;
$line = '';
$dirs = array();
if ($recursive) {
echo $params['path'] . ":\n";
}
foreach ($list as $entry) {
if ($vfs->isFolder($params['path'], $entry)) {
$dirs[] = $entry;
}
$entry = sprintf('%-' . $max . 's', $entry);
if (strlen($line . $entry) > 80 && !empty($line)) {
echo $line . "\n";
$line = '';
}
$line .= $entry;
}
if (!empty($line)) {
echo $line . "\n";
}
if ($recursive && count($dirs)) {
foreach ($dirs as $dir) {
echo "\n";
ls($url . '/' . $dir, $argv, $filter);
}
}
}
/**
* Copies one or several files to a different location.
*
* @param string $source The source file(s) or directory.
* @param string $target The target file or directory.
* @param array $argv Additional options
* @param string $filter Additional parameters
*/
function cp($source, $target, $argv, $filter)
{
$source_params = url2params($source);
$source_path = rtrim($source_params['path'], '/');
unset($source_params['path']);
$target_params = url2params($target);
$target_path = rtrim($target_params['path'], '/');
unset($target_params['path']);
if ($source_params == $target_params) {
// TODO: Shortcut with Horde_Vfs::copy()
}
$source_vfs = vfs($source_params);
$target_vfs = vfs($target_params);
_cp($source_vfs, $target_vfs, $source_path, $target_path, $argv, $filter);
}
/**
* Copies one or several files to a different location.
*
* @param VFS $source_vfs The source VFS object.
* @param VFS $target_vfs The The target VFS object.
* @param string $source_path The source file(s) or directory.
* @param string $target_path The target file or directory.
* @param array $argv Additional options
* @param string $filter Additional parameters
*/
function _cp(&$source_vfs, &$target_vfs, $source_path, $target_path, $argv,
$filter)
{
$source_object = basename($source_path);
$source_parent_path = dirname($source_path);
$target_object = basename($target_path);
$target_parent_path = dirname($target_path);
$recursive = in_array('r', $argv);
if ($source_vfs->isFolder($source_parent_path, $source_object)) {
if (!$recursive) {
echo "Skipping directory $source_path\n";
return;
}
if (!$target_vfs->isFolder($target_parent_path, $target_object)) {
if ($target_vfs->exists($target_parent_path, $target_object)) {
usage(PEAR::raiseError('You can\'t copy a folder on a file.'));
} else {
$target_vfs->createFolder($target_parent_path, $target_object);
}
}
if (!$target_vfs->isFolder($target_path, $source_object)) {
if ($target_vfs->exists($target_path, $source_object)) {
usage(PEAR::raiseError('You can\'t copy a folder on a file.'));
} elseif (!$target_vfs->exists($target_path, $source_object)) {
$target_vfs->createFolder($target_path, $source_object);
}
}
$list = $source_vfs->listFolder($source_path,
count($filter) ? $filter[0] : null,
in_array('a', $argv));
foreach ($list as $item) {
_cp($source_vfs, $target_vfs, $source_path . '/' . $item['name'],
$target_path . '/' . $source_object, $argv, $filter);
}
return;
}
try {
$data = &$source_vfs->read($source_parent_path, $source_object);
} catch (Horde_Vfs_Exception $e) {
usage($e);
}
if ($target_vfs->isFolder($target_parent_path, $target_object)) {
if (in_array('v', $argv)) {
echo '`' . $source_path . '\' -> `' . $target_path . '/' .
$source_object . "'\n";
}
try {
$target_vfs->writeData($target_path, $source_object, $data, true);
} catch (Horde_Vfs_Exception $e) {
usage($e);
}
} elseif ($target_vfs->isFolder(dirname($target_parent_path),
basename($target_parent_path))) {
if (in_array('v', $argv)) {
echo '`' . $source_path . '\' -> `' . $target_path . "'\n";
}
try {
$target_vfs->writeData($target_parent_path, $target_object, $data, true);
} catch (Horde_Vfs_Exception $e) {
usage($e);
}
} else {
usage(new Horde_Vfs_Exception('"' . $target_parent_path . '" does not exist or is not a folder.'));
}
}
/**
* Shows some error and usage information.
*
* @param PEAR_Error $error If specified its error messages will be displayed.
*/
function usage($error = null)
{
if ($error instanceof Horde_Vfs_Exception) {
echo $error->getMessage() . "\n";
} else {
switch ($error) {
case 'ls':
echo 'Usage: vfs.php ls [-alR] <parameters>';
break;
case 'cp':
echo 'Usage: vfs.php cp [-arv] <parameters> <parameters>';
break;
}
}
$cmd = basename($GLOBALS['cmd']);
echo <<<USAGE
Usage: $cmd [options] command [command-options] <parameters> ...
Available commands:
ls - lists a folders content.
cp - copies a file or folder to a different location.
<parameters> can be paths specified like an URL, e.g.:
file:///var/lib/horde/vfs/foo/bar
ftp://john:secret@ftp.example.com/foo/bar
ssh2://john:secret@ssh.example.com/foo/bar
sql://john:secret@localhost/mysql/horde/horde_vfs/foo/bar
The SQL URL is build with the following scheme:
sql://[<user>[:<password>]@]<hostname>/<dbtype>/<database>/<table>[/<path>]
USAGE;
exit;
}
/**
* Returns a VFS instance.
*
* @param array $params A complete parameter set including the driver name
* for the requested VFS instance.
*
* @return VFS An instance of the requested VFS backend.
*/
function vfs($params)
{
return Horde_Vfs::factory($params['driver'], $params);
}
/**
* Merges a set of options as returned by Console_Getopt::getopt2() into a
* single array.
*
* @param array $options A two dimensional array with the options.
*
* @return array A flat array with the options.
*/
function mergeOptions($options)
{
$result = array();
foreach ($options as $param) {
$result = array_merge($result, $param);
}
return $result;
}
/**
* Parses a URL into a set of parameters that can be used to instantiate a
* VFS object.
*
* @todo Document the possible URL formats.
*
* @param string $url A URL with all necessary information for a VFS driver.
*
* @return array A hash with the parsed information.
*/
function url2params($url)
{
$params = array('path' => '');
$url = @parse_url($url);
if (!is_array($url)) {
usage(PEAR::raiseError($php_errormsg));
}
$params['driver'] = $url['scheme'];
if (isset($url['host'])) {
$params['hostspec'] = $url['host'];
}
if (isset($url['port'])) {
$params['port'] = $url['port'];
}
if (isset($url['user'])) {
$params['username'] = $url['user'];
}
if (isset($url['pass'])) {
$params['password'] = $url['pass'];
}
if (isset($url['path'])) {
switch ($url['scheme']) {
case 'ftp':
$params['path'] = $url['path'];
break;
case 'file':
$params['vfsroot'] = $url['path'];
break;
case 'sql':
$path = explode('/', trim($url['path'], '/'));
if (count($path) == 2) {
usage(PEAR::raiseError('No table specified for SQL driver.'));
}
if (count($path) == 1) {
usage(PEAR::raiseError('No database and no table specified for SQL driver.'));
}
if (!count($path)) {
usage(PEAR::raiseError('No database type, database, and table specified for SQL driver.'));
}
$params['phptype'] = array_shift($path);
$params['database'] = array_shift($path);
$params['table'] = array_shift($path);
$params['path'] = implode('/', $path);
break;
case 'ssh2':
$params['path'] = $url['path'];
break;
default:
usage(PEAR::raiseError('Only the SQL, File, and FTP drivers are supported at the moment.'));
break;
}
if (isset($url['query'])) {
$queries = explode('&', $url['query']);
foreach ($queries as $query) {
$pair = explode('=', $query);
$params[$pair[0]] = isset($pair[1]) ? $pair[1] : true;
}
}
}
return $params;
}
|