2001-10-29 20:15:53 +08:00
|
|
|
<?php
|
|
|
|
//
|
|
|
|
// +----------------------------------------------------------------------+
|
2004-01-09 01:33:29 +08:00
|
|
|
// | PHP Version 5 |
|
2001-10-29 20:15:53 +08:00
|
|
|
// +----------------------------------------------------------------------+
|
2004-01-09 01:33:29 +08:00
|
|
|
// | Copyright (c) 1997-2004 The PHP Group |
|
2001-10-29 20:15:53 +08:00
|
|
|
// +----------------------------------------------------------------------+
|
|
|
|
// | This source file is subject to version 2.0 of the PHP license, |
|
|
|
|
// | that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
// | available through the world-wide-web at the following url: |
|
|
|
|
// | http://www.php.net/license/3_0.txt. |
|
2001-10-29 20:15:53 +08:00
|
|
|
// | If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
// | obtain it through the world-wide-web, please send a note to |
|
|
|
|
// | license@php.net so we can mail you a copy immediately. |
|
|
|
|
// +----------------------------------------------------------------------+
|
2002-02-28 16:29:35 +08:00
|
|
|
// | Authors: Tomas V.V.Cox <cox@idecnet.com> |
|
2001-10-29 20:15:53 +08:00
|
|
|
// +----------------------------------------------------------------------+
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
//
|
|
|
|
|
|
|
|
require_once 'PEAR.php';
|
|
|
|
require_once 'Console/Getopt.php';
|
|
|
|
|
2002-06-01 01:47:19 +08:00
|
|
|
$GLOBALS['_System_temp_files'] = array();
|
|
|
|
|
2001-10-29 20:15:53 +08:00
|
|
|
/**
|
|
|
|
* System offers cross plattform compatible system functions
|
|
|
|
*
|
|
|
|
* Static functions for different operations. Should work under
|
2002-02-11 01:03:22 +08:00
|
|
|
* Unix and Windows. The names and usage has been taken from its respectively
|
|
|
|
* GNU commands. The functions will return (bool) false on error and will
|
|
|
|
* trigger the error with the PHP trigger_error() function (you can silence
|
|
|
|
* the error by prefixing a '@' sign after the function call).
|
2001-10-29 20:15:53 +08:00
|
|
|
*
|
2002-02-11 01:03:22 +08:00
|
|
|
* Documentation on this class you can find in:
|
|
|
|
* http://pear.php.net/manual/
|
2001-10-29 20:15:53 +08:00
|
|
|
*
|
2002-02-11 01:03:22 +08:00
|
|
|
* Example usage:
|
|
|
|
* if (!@System::rm('-r file1 dir1')) {
|
|
|
|
* print "could not delete file1 or dir1";
|
|
|
|
* }
|
2001-10-29 20:15:53 +08:00
|
|
|
*
|
2003-09-16 04:17:34 +08:00
|
|
|
* In case you need to to pass file names with spaces,
|
|
|
|
* pass the params as an array:
|
|
|
|
*
|
|
|
|
* System::rm(array('-r', $file1, $dir1));
|
|
|
|
*
|
2001-10-29 20:15:53 +08:00
|
|
|
* @package System
|
|
|
|
* @author Tomas V.V.Cox <cox@idecnet.com>
|
2001-12-19 01:06:28 +08:00
|
|
|
* @version $Revision$
|
2001-10-29 20:15:53 +08:00
|
|
|
* @access public
|
2002-02-11 01:03:22 +08:00
|
|
|
* @see http://pear.php.net/manual/
|
2001-10-29 20:15:53 +08:00
|
|
|
*/
|
2002-06-01 01:47:19 +08:00
|
|
|
class System
|
2001-10-29 20:15:53 +08:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* returns the commandline arguments of a function
|
|
|
|
*
|
|
|
|
* @param string $argv the commandline
|
2001-12-16 09:30:31 +08:00
|
|
|
* @param string $short_options the allowed option short-tags
|
2001-10-29 20:15:53 +08:00
|
|
|
* @param string $long_options the allowed option long-tags
|
|
|
|
* @return array the given options and there values
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _parseArgs($argv, $short_options, $long_options = null)
|
|
|
|
{
|
2001-12-16 09:30:31 +08:00
|
|
|
if (!is_array($argv) && $argv !== null) {
|
2003-12-07 06:07:49 +08:00
|
|
|
$argv = preg_split('/\s+/', $argv);
|
2001-10-29 20:15:53 +08:00
|
|
|
}
|
2003-12-11 13:54:35 +08:00
|
|
|
return Console_Getopt::getopt2($argv, $short_options);
|
2001-10-29 20:15:53 +08:00
|
|
|
}
|
|
|
|
|
2002-02-11 01:03:22 +08:00
|
|
|
/**
|
|
|
|
* Output errors with PHP trigger_error(). You can silence the errors
|
|
|
|
* with prefixing a "@" sign to the function call: @System::mkdir(..);
|
|
|
|
*
|
|
|
|
* @param mixed $error a PEAR error or a string with the error message
|
|
|
|
* @return bool false
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function raiseError($error)
|
|
|
|
{
|
|
|
|
if (PEAR::isError($error)) {
|
|
|
|
$error = $error->getMessage();
|
|
|
|
}
|
|
|
|
trigger_error($error, E_USER_WARNING);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-10-29 20:15:53 +08:00
|
|
|
/**
|
|
|
|
* Creates a nested array representing the structure of a directory
|
|
|
|
*
|
|
|
|
* System::_dirToStruct('dir1', 0) =>
|
|
|
|
* Array
|
|
|
|
* (
|
|
|
|
* [dirs] => Array
|
|
|
|
* (
|
|
|
|
* [0] => dir1
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* [files] => Array
|
|
|
|
* (
|
|
|
|
* [0] => dir1/file2
|
|
|
|
* [1] => dir1/file3
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* @param string $sPath Name of the directory
|
|
|
|
* @param integer $maxinst max. deep of the lookup
|
|
|
|
* @param integer $aktinst starting deep of the lookup
|
|
|
|
* @return array the structure of the dir
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
|
|
|
|
function _dirToStruct($sPath, $maxinst, $aktinst = 0)
|
|
|
|
{
|
|
|
|
$struct = array('dirs' => array(), 'files' => array());
|
|
|
|
if (($dir = @opendir($sPath)) === false) {
|
2002-02-11 01:03:22 +08:00
|
|
|
System::raiseError("Could not open dir $sPath");
|
2001-10-29 20:15:53 +08:00
|
|
|
return $struct; // XXX could not open error
|
|
|
|
}
|
|
|
|
$struct['dirs'][] = $sPath; // XXX don't add if '.' or '..' ?
|
|
|
|
$list = array();
|
|
|
|
while ($file = readdir($dir)) {
|
|
|
|
if ($file != '.' && $file != '..') {
|
|
|
|
$list[] = $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dir);
|
|
|
|
sort($list);
|
2003-09-16 04:17:34 +08:00
|
|
|
if ($aktinst < $maxinst || $maxinst == 0) {
|
|
|
|
foreach($list as $val) {
|
|
|
|
$path = $sPath . DIRECTORY_SEPARATOR . $val;
|
|
|
|
if (is_dir($path)) {
|
2001-10-29 20:15:53 +08:00
|
|
|
$tmp = System::_dirToStruct($path, $maxinst, $aktinst+1);
|
|
|
|
$struct = array_merge_recursive($tmp, $struct);
|
2003-09-16 04:17:34 +08:00
|
|
|
} else {
|
|
|
|
$struct['files'][] = $path;
|
2001-10-29 20:15:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $struct;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a nested array representing the structure of a directory and files
|
|
|
|
*
|
|
|
|
* @param array $files Array listing files and dirs
|
|
|
|
* @return array
|
|
|
|
* @see System::_dirToStruct()
|
|
|
|
*/
|
|
|
|
function _multipleToStruct($files)
|
|
|
|
{
|
|
|
|
$struct = array('dirs' => array(), 'files' => array());
|
2002-06-01 02:12:56 +08:00
|
|
|
settype($files, 'array');
|
|
|
|
foreach ($files as $file) {
|
2001-10-29 20:15:53 +08:00
|
|
|
if (is_dir($file)) {
|
|
|
|
$tmp = System::_dirToStruct($file, 0);
|
|
|
|
$struct = array_merge_recursive($tmp, $struct);
|
|
|
|
} else {
|
|
|
|
$struct['files'][] = $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $struct;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2001-12-16 09:30:31 +08:00
|
|
|
* The rm command for removing files.
|
2001-10-29 20:15:53 +08:00
|
|
|
* Supports multiple files and dirs and also recursive deletes
|
|
|
|
*
|
|
|
|
* @param string $args the arguments for rm
|
|
|
|
* @return mixed PEAR_Error or true for success
|
2001-12-16 09:30:31 +08:00
|
|
|
* @access public
|
2001-10-29 20:15:53 +08:00
|
|
|
*/
|
|
|
|
function rm($args)
|
|
|
|
{
|
|
|
|
$opts = System::_parseArgs($args, 'rf'); // "f" do nothing but like it :-)
|
|
|
|
if (PEAR::isError($opts)) {
|
2002-02-11 01:03:22 +08:00
|
|
|
return System::raiseError($opts);
|
2001-10-29 20:15:53 +08:00
|
|
|
}
|
|
|
|
foreach($opts[0] as $opt) {
|
|
|
|
if ($opt[0] == 'r') {
|
|
|
|
$do_recursive = true;
|
|
|
|
}
|
|
|
|
}
|
2002-02-11 01:03:22 +08:00
|
|
|
$ret = true;
|
2001-10-29 20:15:53 +08:00
|
|
|
if (isset($do_recursive)) {
|
|
|
|
$struct = System::_multipleToStruct($opts[1]);
|
|
|
|
foreach($struct['files'] as $file) {
|
2002-06-02 22:27:15 +08:00
|
|
|
if (!@unlink($file)) {
|
2002-02-11 01:03:22 +08:00
|
|
|
$ret = false;
|
|
|
|
}
|
2001-10-29 20:15:53 +08:00
|
|
|
}
|
|
|
|
foreach($struct['dirs'] as $dir) {
|
2002-06-02 22:27:15 +08:00
|
|
|
if (!@rmdir($dir)) {
|
2002-02-11 01:03:22 +08:00
|
|
|
$ret = false;
|
|
|
|
}
|
2001-10-29 20:15:53 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach ($opts[1] as $file) {
|
2002-02-11 01:03:22 +08:00
|
|
|
$delete = (is_dir($file)) ? 'rmdir' : 'unlink';
|
2002-06-02 22:27:15 +08:00
|
|
|
if (!@$delete($file)) {
|
2002-02-11 01:03:22 +08:00
|
|
|
$ret = false;
|
|
|
|
}
|
2001-10-29 20:15:53 +08:00
|
|
|
}
|
|
|
|
}
|
2002-02-11 01:03:22 +08:00
|
|
|
return $ret;
|
2001-10-29 20:15:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2002-08-11 00:33:08 +08:00
|
|
|
* Make directories. Note that we use call_user_func('mkdir') to avoid
|
|
|
|
* a problem with ZE2 calling System::mkDir instead of the native PHP func.
|
2001-10-29 20:15:53 +08:00
|
|
|
*
|
|
|
|
* @param string $args the name of the director(y|ies) to create
|
2002-02-11 01:03:22 +08:00
|
|
|
* @return bool True for success
|
2001-12-16 09:30:31 +08:00
|
|
|
* @access public
|
2001-10-29 20:15:53 +08:00
|
|
|
*/
|
|
|
|
function mkDir($args)
|
|
|
|
{
|
|
|
|
$opts = System::_parseArgs($args, 'pm:');
|
|
|
|
if (PEAR::isError($opts)) {
|
2002-02-11 01:03:22 +08:00
|
|
|
return System::raiseError($opts);
|
2001-10-29 20:15:53 +08:00
|
|
|
}
|
|
|
|
$mode = 0777; // default mode
|
|
|
|
foreach($opts[0] as $opt) {
|
|
|
|
if ($opt[0] == 'p') {
|
|
|
|
$create_parents = true;
|
|
|
|
} elseif($opt[0] == 'm') {
|
2004-01-19 11:32:12 +08:00
|
|
|
// if the mode is clearly an octal number (starts with 0)
|
|
|
|
// convert it to decimal
|
|
|
|
if (strlen($opt[1]) && $opt[1]{0} == '0') {
|
|
|
|
$opt[1] = octdec($opt[1]);
|
|
|
|
} else {
|
|
|
|
// convert to int
|
|
|
|
$opt[1] += 0;
|
|
|
|
}
|
2001-10-29 20:15:53 +08:00
|
|
|
$mode = $opt[1];
|
|
|
|
}
|
|
|
|
}
|
2002-01-24 02:53:08 +08:00
|
|
|
$ret = true;
|
2001-10-29 20:15:53 +08:00
|
|
|
if (isset($create_parents)) {
|
|
|
|
foreach($opts[1] as $dir) {
|
|
|
|
$dirstack = array();
|
|
|
|
while (!@is_dir($dir) && $dir != DIRECTORY_SEPARATOR) {
|
|
|
|
array_unshift($dirstack, $dir);
|
|
|
|
$dir = dirname($dir);
|
|
|
|
}
|
|
|
|
while ($newdir = array_shift($dirstack)) {
|
2002-08-11 00:33:08 +08:00
|
|
|
if (!call_user_func('mkdir', $newdir, $mode)) {
|
2002-01-24 02:53:08 +08:00
|
|
|
$ret = false;
|
2001-10-29 20:15:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach($opts[1] as $dir) {
|
2002-08-11 00:33:08 +08:00
|
|
|
if (!@is_dir($dir) && !call_user_func('mkdir', $dir, $mode)) {
|
2002-01-24 02:53:08 +08:00
|
|
|
$ret = false;
|
2001-10-29 20:15:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-01-24 02:53:08 +08:00
|
|
|
return $ret;
|
2001-10-29 20:15:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Concatenate files
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* 1) $var = System::cat('sample.txt test.txt');
|
|
|
|
* 2) System::cat('sample.txt test.txt > final.txt');
|
|
|
|
* 3) System::cat('sample.txt test.txt >> final.txt');
|
|
|
|
*
|
|
|
|
* Note: as the class use fopen, urls should work also (test that)
|
|
|
|
*
|
|
|
|
* @param string $args the arguments
|
|
|
|
* @return boolean true on success
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function &cat($args)
|
|
|
|
{
|
|
|
|
$ret = null;
|
|
|
|
$files = array();
|
|
|
|
if (!is_array($args)) {
|
|
|
|
$args = preg_split('/\s+/', $args);
|
|
|
|
}
|
|
|
|
for($i=0; $i < count($args); $i++) {
|
|
|
|
if ($args[$i] == '>') {
|
|
|
|
$mode = 'wb';
|
|
|
|
$outputfile = $args[$i+1];
|
|
|
|
break;
|
|
|
|
} elseif ($args[$i] == '>>') {
|
|
|
|
$mode = 'ab+';
|
|
|
|
$outputfile = $args[$i+1];
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$files[] = $args[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($mode)) {
|
|
|
|
if (!$outputfd = fopen($outputfile, $mode)) {
|
2003-12-12 00:07:01 +08:00
|
|
|
$err = System::raiseError("Could not open $outputfile");
|
|
|
|
return $err;
|
2001-10-29 20:15:53 +08:00
|
|
|
}
|
|
|
|
$ret = true;
|
|
|
|
}
|
2002-02-11 01:03:22 +08:00
|
|
|
foreach ($files as $file) {
|
2001-10-29 20:15:53 +08:00
|
|
|
if (!$fd = fopen($file, 'r')) {
|
2002-02-11 01:03:22 +08:00
|
|
|
System::raiseError("Could not open $file");
|
|
|
|
continue;
|
2001-10-29 20:15:53 +08:00
|
|
|
}
|
2002-06-01 02:49:47 +08:00
|
|
|
while ($cont = fread($fd, 2048)) {
|
2001-10-29 20:15:53 +08:00
|
|
|
if (isset($outputfd)) {
|
|
|
|
fwrite($outputfd, $cont);
|
|
|
|
} else {
|
|
|
|
$ret .= $cont;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose($fd);
|
|
|
|
}
|
|
|
|
if (@is_resource($outputfd)) {
|
|
|
|
fclose($outputfd);
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2001-12-16 09:30:31 +08:00
|
|
|
/**
|
2002-06-01 01:47:19 +08:00
|
|
|
* Creates temporary files or directories. This function will remove
|
|
|
|
* the created files when the scripts finish its execution.
|
2001-12-16 09:30:31 +08:00
|
|
|
*
|
|
|
|
* Usage:
|
2001-12-16 09:36:49 +08:00
|
|
|
* 1) $tempfile = System::mktemp("prefix");
|
|
|
|
* 2) $tempdir = System::mktemp("-d prefix");
|
|
|
|
* 3) $tempfile = System::mktemp();
|
|
|
|
* 4) $tempfile = System::mktemp("-t /var/tmp prefix");
|
2001-12-16 09:30:31 +08:00
|
|
|
*
|
|
|
|
* prefix -> The string that will be prepended to the temp name
|
|
|
|
* (defaults to "tmp").
|
2002-06-01 01:47:19 +08:00
|
|
|
* -d -> A temporary dir will be created instead of a file.
|
|
|
|
* -t -> The target dir where the temporary (file|dir) will be created. If
|
2001-12-16 09:30:31 +08:00
|
|
|
* this param is missing by default the env vars TMP on Windows or
|
|
|
|
* TMPDIR in Unix will be used. If these vars are also missing
|
|
|
|
* c:\windows\temp or /tmp will be used.
|
|
|
|
*
|
|
|
|
* @param string $args The arguments
|
2002-02-11 01:03:22 +08:00
|
|
|
* @return mixed the full path of the created (file|dir) or false
|
|
|
|
* @see System::tmpdir()
|
2001-12-16 09:30:31 +08:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function mktemp($args = null)
|
|
|
|
{
|
2002-06-01 01:47:19 +08:00
|
|
|
static $first_time = true;
|
2001-12-16 09:30:31 +08:00
|
|
|
$opts = System::_parseArgs($args, 't:d');
|
|
|
|
if (PEAR::isError($opts)) {
|
2002-02-11 01:03:22 +08:00
|
|
|
return System::raiseError($opts);
|
2001-12-16 09:30:31 +08:00
|
|
|
}
|
|
|
|
foreach($opts[0] as $opt) {
|
|
|
|
if($opt[0] == 'd') {
|
|
|
|
$tmp_is_dir = true;
|
|
|
|
} elseif($opt[0] == 't') {
|
|
|
|
$tmpdir = $opt[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$prefix = (isset($opts[1][0])) ? $opts[1][0] : 'tmp';
|
2001-12-26 09:23:04 +08:00
|
|
|
if (!isset($tmpdir)) {
|
|
|
|
$tmpdir = System::tmpdir();
|
2001-12-16 09:30:31 +08:00
|
|
|
}
|
2002-02-11 01:03:22 +08:00
|
|
|
if (!System::mkDir("-p $tmpdir")) {
|
|
|
|
return false;
|
|
|
|
}
|
2001-12-16 09:30:31 +08:00
|
|
|
$tmp = tempnam($tmpdir, $prefix);
|
2002-02-11 01:03:22 +08:00
|
|
|
if (isset($tmp_is_dir)) {
|
2001-12-16 09:30:31 +08:00
|
|
|
unlink($tmp); // be careful possible race condition here
|
2002-08-11 00:33:08 +08:00
|
|
|
if (!call_user_func('mkdir', $tmp, 0700)) {
|
2002-02-11 01:03:22 +08:00
|
|
|
return System::raiseError("Unable to create temporary directory $tmpdir");
|
2001-12-16 09:30:31 +08:00
|
|
|
}
|
|
|
|
}
|
2002-06-01 01:47:19 +08:00
|
|
|
$GLOBALS['_System_temp_files'][] = $tmp;
|
|
|
|
if ($first_time) {
|
|
|
|
PEAR::registerShutdownFunc(array('System', '_removeTmpFiles'));
|
|
|
|
$first_time = false;
|
|
|
|
}
|
2001-12-16 09:30:31 +08:00
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:47:19 +08:00
|
|
|
/**
|
|
|
|
* Remove temporary files created my mkTemp. This function is executed
|
|
|
|
* at script shutdown time
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _removeTmpFiles()
|
|
|
|
{
|
|
|
|
if (count($GLOBALS['_System_temp_files'])) {
|
|
|
|
$delete = $GLOBALS['_System_temp_files'];
|
|
|
|
array_unshift($delete, '-r');
|
|
|
|
System::rm($delete);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-11 01:03:22 +08:00
|
|
|
/**
|
|
|
|
* Get the path of the temporal directory set in the system
|
|
|
|
* by looking in its environments variables.
|
2002-08-29 18:08:37 +08:00
|
|
|
* Note: php.ini-recommended removes the "E" from the variables_order setting,
|
|
|
|
* making unavaible the $_ENV array, that s why we do tests with _ENV
|
2002-02-11 01:03:22 +08:00
|
|
|
*
|
|
|
|
* @return string The temporal directory on the system
|
|
|
|
*/
|
2001-12-26 09:23:04 +08:00
|
|
|
function tmpdir()
|
|
|
|
{
|
2002-07-26 18:18:28 +08:00
|
|
|
if (OS_WINDOWS) {
|
2002-08-29 18:08:37 +08:00
|
|
|
if ($var = isset($_ENV['TEMP']) ? $_ENV['TEMP'] : getenv('TEMP')) {
|
|
|
|
return $var;
|
2001-12-26 09:23:04 +08:00
|
|
|
}
|
2002-08-29 18:08:37 +08:00
|
|
|
if ($var = isset($_ENV['TMP']) ? $_ENV['TMP'] : getenv('TMP')) {
|
|
|
|
return $var;
|
2001-12-26 09:23:04 +08:00
|
|
|
}
|
2002-08-29 18:08:37 +08:00
|
|
|
if ($var = isset($_ENV['windir']) ? $_ENV['windir'] : getenv('windir')) {
|
|
|
|
return $var;
|
2001-12-26 09:50:08 +08:00
|
|
|
}
|
2002-08-26 02:11:24 +08:00
|
|
|
return getenv('SystemRoot') . '\temp';
|
2001-12-26 09:23:04 +08:00
|
|
|
}
|
2002-08-29 18:08:37 +08:00
|
|
|
if ($var = isset($_ENV['TMPDIR']) ? $_ENV['TMPDIR'] : getenv('TMPDIR')) {
|
|
|
|
return $var;
|
2001-12-26 09:23:04 +08:00
|
|
|
}
|
|
|
|
return '/tmp';
|
|
|
|
}
|
2002-02-06 16:32:00 +08:00
|
|
|
|
|
|
|
/**
|
2002-10-12 22:17:44 +08:00
|
|
|
* The "which" command (show the full path of a command)
|
2002-02-06 16:32:00 +08:00
|
|
|
*
|
|
|
|
* @param string $program The command to search for
|
|
|
|
* @return mixed A string with the full path or false if not found
|
2003-03-18 20:06:09 +08:00
|
|
|
* @author Stig Bakken <ssb@php.net>
|
2002-02-06 16:32:00 +08:00
|
|
|
*/
|
2002-10-12 22:17:44 +08:00
|
|
|
function which($program, $fallback = false)
|
2002-02-06 16:32:00 +08:00
|
|
|
{
|
2003-09-16 04:17:34 +08:00
|
|
|
// is_executable() is not available on windows
|
|
|
|
if (OS_WINDOWS) {
|
2002-12-22 09:32:01 +08:00
|
|
|
$pear_is_executable = 'is_file';
|
|
|
|
} else {
|
|
|
|
$pear_is_executable = 'is_executable';
|
|
|
|
}
|
|
|
|
|
2002-02-06 16:32:00 +08:00
|
|
|
// full path given
|
|
|
|
if (basename($program) != $program) {
|
2002-12-22 09:32:01 +08:00
|
|
|
return (@$pear_is_executable($program)) ? $program : $fallback;
|
2002-02-06 16:32:00 +08:00
|
|
|
}
|
2002-12-22 09:32:01 +08:00
|
|
|
|
2002-02-06 16:32:00 +08:00
|
|
|
// XXX FIXME honor safe mode
|
|
|
|
$path_delim = OS_WINDOWS ? ';' : ':';
|
2003-01-09 23:11:27 +08:00
|
|
|
$exe_suffixes = OS_WINDOWS ? array('.exe','.bat','.cmd','.com') : array('');
|
2002-02-06 16:32:00 +08:00
|
|
|
$path_elements = explode($path_delim, getenv('PATH'));
|
2003-01-09 23:11:27 +08:00
|
|
|
foreach ($exe_suffixes as $suff) {
|
|
|
|
foreach ($path_elements as $dir) {
|
|
|
|
$file = $dir . DIRECTORY_SEPARATOR . $program . $suff;
|
|
|
|
if (@is_file($file) && @$pear_is_executable($file)) {
|
|
|
|
return $file;
|
|
|
|
}
|
2002-02-06 16:32:00 +08:00
|
|
|
}
|
|
|
|
}
|
2002-10-12 22:17:44 +08:00
|
|
|
return $fallback;
|
2002-02-06 16:32:00 +08:00
|
|
|
}
|
2003-09-16 04:17:34 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The "find" command
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
*
|
|
|
|
* System::find($dir);
|
|
|
|
* System::find("$dir -type d");
|
|
|
|
* System::find("$dir -type f");
|
|
|
|
* System::find("$dir -name *.php");
|
|
|
|
* System::find("$dir -name *.php -name *.htm*");
|
|
|
|
* System::find("$dir -maxdepth 1");
|
|
|
|
*
|
|
|
|
* Params implmented:
|
|
|
|
* $dir -> Start the search at this directory
|
|
|
|
* -type d -> return only directories
|
|
|
|
* -type f -> return only files
|
|
|
|
* -maxdepth <n> -> max depth of recursion
|
|
|
|
* -name <pattern> -> search pattern (bash style). Multiple -name param allowed
|
|
|
|
*
|
|
|
|
* @param mixed Either array or string with the command line
|
|
|
|
* @return array Array of found files
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function find($args)
|
|
|
|
{
|
|
|
|
if (!is_array($args)) {
|
|
|
|
$args = preg_split('/\s+/', $args, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
}
|
|
|
|
$dir = array_shift($args);
|
|
|
|
$patterns = array();
|
|
|
|
$depth = 0;
|
|
|
|
$do_files = $do_dirs = true;
|
|
|
|
for ($i = 0; $i < count($args); $i++) {
|
|
|
|
switch ($args[$i]) {
|
|
|
|
case '-type':
|
|
|
|
if (in_array($args[$i+1], array('d', 'f'))) {
|
|
|
|
if ($args[$i+1] == 'd') {
|
|
|
|
$do_files = false;
|
|
|
|
} else {
|
|
|
|
$do_dirs = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
break;
|
|
|
|
case '-name':
|
|
|
|
$patterns[] = "(" . preg_replace(array('/\./', '/\*/'),
|
|
|
|
array('\.', '.*'),
|
|
|
|
$args[$i+1])
|
|
|
|
. ")";
|
|
|
|
$i++;
|
|
|
|
break;
|
|
|
|
case '-maxdepth':
|
|
|
|
$depth = $args[$i+1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$path = System::_dirToStruct($dir, $depth);
|
|
|
|
if ($do_files && $do_dirs) {
|
|
|
|
$files = array_merge($path['files'], $path['dirs']);
|
|
|
|
} elseif ($do_dirs) {
|
|
|
|
$files = $path['dirs'];
|
|
|
|
} else {
|
|
|
|
$files = $path['files'];
|
|
|
|
}
|
|
|
|
if (count($patterns)) {
|
|
|
|
$patterns = implode('|', $patterns);
|
|
|
|
$ret = array();
|
|
|
|
for ($i = 0; $i < count($files); $i++) {
|
|
|
|
if (preg_match("#^$patterns\$#", $files[$i])) {
|
|
|
|
$ret[] = $files[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
return $files;
|
|
|
|
}
|
2001-10-29 20:15:53 +08:00
|
|
|
}
|
2003-01-09 23:11:27 +08:00
|
|
|
?>
|