123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629 |
- <?php
- require_once 'PEAR.php';
- require_once 'Console/Getopt.php';
- $GLOBALS['_System_temp_files'] = array();
- class System
- {
-
- public static function _parseArgs($argv, $short_options, $long_options = null)
- {
- if (!is_array($argv) && $argv !== null) {
-
-
- preg_match_all("/(?:[\"'])(.*?)(?:['\"])|([^\s]+)/", $argv, $av);
- $argv = $av[1];
- foreach ($av[2] as $k => $a) {
- if (empty($a)) {
- continue;
- }
- $argv[$k] = trim($a) ;
- }
- }
- return Console_Getopt::getopt2($argv, $short_options, $long_options);
- }
-
- protected static function raiseError($error)
- {
- if (PEAR::isError($error)) {
- $error = $error->getMessage();
- }
- trigger_error($error, E_USER_WARNING);
- return false;
- }
-
- protected static function _dirToStruct($sPath, $maxinst, $aktinst = 0, $silent = false)
- {
- $struct = array('dirs' => array(), 'files' => array());
- if (($dir = @opendir($sPath)) === false) {
- if (!$silent) {
- System::raiseError("Could not open dir $sPath");
- }
- return $struct;
- }
- $struct['dirs'][] = $sPath = realpath($sPath);
- $list = array();
- while (false !== ($file = readdir($dir))) {
- if ($file != '.' && $file != '..') {
- $list[] = $file;
- }
- }
- closedir($dir);
- natsort($list);
- if ($aktinst < $maxinst || $maxinst == 0) {
- foreach ($list as $val) {
- $path = $sPath . DIRECTORY_SEPARATOR . $val;
- if (is_dir($path) && !is_link($path)) {
- $tmp = System::_dirToStruct($path, $maxinst, $aktinst+1, $silent);
- $struct = array_merge_recursive($struct, $tmp);
- } else {
- $struct['files'][] = $path;
- }
- }
- }
- return $struct;
- }
-
- protected static function _multipleToStruct($files)
- {
- $struct = array('dirs' => array(), 'files' => array());
- settype($files, 'array');
- foreach ($files as $file) {
- if (is_dir($file) && !is_link($file)) {
- $tmp = System::_dirToStruct($file, 0);
- $struct = array_merge_recursive($tmp, $struct);
- } else {
- if (!in_array($file, $struct['files'])) {
- $struct['files'][] = $file;
- }
- }
- }
- return $struct;
- }
-
- public static function rm($args)
- {
- $opts = System::_parseArgs($args, 'rf');
- if (PEAR::isError($opts)) {
- return System::raiseError($opts);
- }
- foreach ($opts[0] as $opt) {
- if ($opt[0] == 'r') {
- $do_recursive = true;
- }
- }
- $ret = true;
- if (isset($do_recursive)) {
- $struct = System::_multipleToStruct($opts[1]);
- foreach ($struct['files'] as $file) {
- if (!@unlink($file)) {
- $ret = false;
- }
- }
- rsort($struct['dirs']);
- foreach ($struct['dirs'] as $dir) {
- if (!@rmdir($dir)) {
- $ret = false;
- }
- }
- } else {
- foreach ($opts[1] as $file) {
- $delete = (is_dir($file)) ? 'rmdir' : 'unlink';
- if (!@$delete($file)) {
- $ret = false;
- }
- }
- }
- return $ret;
- }
-
- public static function mkDir($args)
- {
- $opts = System::_parseArgs($args, 'pm:');
- if (PEAR::isError($opts)) {
- return System::raiseError($opts);
- }
- $mode = 0777;
- foreach ($opts[0] as $opt) {
- if ($opt[0] == 'p') {
- $create_parents = true;
- } elseif ($opt[0] == 'm') {
-
-
- if (strlen($opt[1]) && $opt[1]{0} == '0') {
- $opt[1] = octdec($opt[1]);
- } else {
-
- $opt[1] += 0;
- }
- $mode = $opt[1];
- }
- }
- $ret = true;
- if (isset($create_parents)) {
- foreach ($opts[1] as $dir) {
- $dirstack = array();
- while ((!file_exists($dir) || !is_dir($dir)) &&
- $dir != DIRECTORY_SEPARATOR) {
- array_unshift($dirstack, $dir);
- $dir = dirname($dir);
- }
- while ($newdir = array_shift($dirstack)) {
- if (!is_writeable(dirname($newdir))) {
- $ret = false;
- break;
- }
- if (!mkdir($newdir, $mode)) {
- $ret = false;
- }
- }
- }
- } else {
- foreach($opts[1] as $dir) {
- if ((@file_exists($dir) || !is_dir($dir)) && !mkdir($dir, $mode)) {
- $ret = false;
- }
- }
- }
- return $ret;
- }
-
- public static function &cat($args)
- {
- $ret = null;
- $files = array();
- if (!is_array($args)) {
- $args = preg_split('/\s+/', $args, -1, PREG_SPLIT_NO_EMPTY);
- }
- $count_args = count($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];
- }
- }
- $outputfd = false;
- if (isset($mode)) {
- if (!$outputfd = fopen($outputfile, $mode)) {
- $err = System::raiseError("Could not open $outputfile");
- return $err;
- }
- $ret = true;
- }
- foreach ($files as $file) {
- if (!$fd = fopen($file, 'r')) {
- System::raiseError("Could not open $file");
- continue;
- }
- while ($cont = fread($fd, 2048)) {
- if (is_resource($outputfd)) {
- fwrite($outputfd, $cont);
- } else {
- $ret .= $cont;
- }
- }
- fclose($fd);
- }
- if (is_resource($outputfd)) {
- fclose($outputfd);
- }
- return $ret;
- }
-
- public static function mktemp($args = null)
- {
- static $first_time = true;
- $opts = System::_parseArgs($args, 't:d');
- if (PEAR::isError($opts)) {
- return System::raiseError($opts);
- }
- 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';
- if (!isset($tmpdir)) {
- $tmpdir = System::tmpdir();
- }
- if (!System::mkDir(array('-p', $tmpdir))) {
- return false;
- }
- $tmp = tempnam($tmpdir, $prefix);
- if (isset($tmp_is_dir)) {
- unlink($tmp);
- if (!mkdir($tmp, 0700)) {
- return System::raiseError("Unable to create temporary directory $tmpdir");
- }
- }
- $GLOBALS['_System_temp_files'][] = $tmp;
- if (isset($tmp_is_dir)) {
-
- }
- if ($first_time) {
- PEAR::registerShutdownFunc(array('System', '_removeTmpFiles'));
- $first_time = false;
- }
- return $tmp;
- }
-
- public static function _removeTmpFiles()
- {
- if (count($GLOBALS['_System_temp_files'])) {
- $delete = $GLOBALS['_System_temp_files'];
- array_unshift($delete, '-r');
- System::rm($delete);
- $GLOBALS['_System_temp_files'] = array();
- }
- }
-
- public static function tmpdir()
- {
- if (OS_WINDOWS) {
- if ($var = isset($_ENV['TMP']) ? $_ENV['TMP'] : getenv('TMP')) {
- return $var;
- }
- if ($var = isset($_ENV['TEMP']) ? $_ENV['TEMP'] : getenv('TEMP')) {
- return $var;
- }
- if ($var = isset($_ENV['USERPROFILE']) ? $_ENV['USERPROFILE'] : getenv('USERPROFILE')) {
- return $var;
- }
- if ($var = isset($_ENV['windir']) ? $_ENV['windir'] : getenv('windir')) {
- return $var;
- }
- return getenv('SystemRoot') . '\temp';
- }
- if ($var = isset($_ENV['TMPDIR']) ? $_ENV['TMPDIR'] : getenv('TMPDIR')) {
- return $var;
- }
- return realpath('/tmp');
- }
-
- public static function which($program, $fallback = false)
- {
-
- if (!is_string($program) || '' == $program) {
- return $fallback;
- }
-
- if (basename($program) != $program) {
- $path_elements[] = dirname($program);
- $program = basename($program);
- } else {
- $path = getenv('PATH');
- if (!$path) {
- $path = getenv('Path');
- }
- $path_elements = explode(PATH_SEPARATOR, $path);
- }
- if (OS_WINDOWS) {
- $exe_suffixes = getenv('PATHEXT')
- ? explode(PATH_SEPARATOR, getenv('PATHEXT'))
- : array('.exe','.bat','.cmd','.com');
-
- if (strpos($program, '.') !== false) {
- array_unshift($exe_suffixes, '');
- }
- } else {
- $exe_suffixes = array('');
- }
- foreach ($exe_suffixes as $suff) {
- foreach ($path_elements as $dir) {
- $file = $dir . DIRECTORY_SEPARATOR . $program . $suff;
-
-
- if (OS_WINDOWS) {
- return $file;
- } else {
- if (is_executable($file)) {
- return $file;
- }
- }
- }
- }
- return $fallback;
- }
-
- public static function find($args)
- {
- if (!is_array($args)) {
- $args = preg_split('/\s+/', $args, -1, PREG_SPLIT_NO_EMPTY);
- }
- $dir = realpath(array_shift($args));
- if (!$dir) {
- return array();
- }
- $patterns = array();
- $depth = 0;
- $do_files = $do_dirs = true;
- $args_count = count($args);
- for ($i = 0; $i < $args_count; $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':
- $name = preg_quote($args[$i+1], '#');
-
-
- $name = strtr($name, array('\?' => '.', '\*' => '.*'));
- $patterns[] = '('.$name.')';
- $i++;
- break;
- case '-maxdepth':
- $depth = $args[$i+1];
- break;
- }
- }
- $path = System::_dirToStruct($dir, $depth, 0, true);
- 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)) {
- $dsq = preg_quote(DIRECTORY_SEPARATOR, '#');
- $pattern = '#(^|'.$dsq.')'.implode('|', $patterns).'($|'.$dsq.')#';
- $ret = array();
- $files_count = count($files);
- for ($i = 0; $i < $files_count; $i++) {
-
- $filepart = basename($files[$i]);
- if (preg_match($pattern, $filepart)) {
- $ret[] = $files[$i];
- }
- }
- return $ret;
- }
- return $files;
- }
- }
|