diff --git a/thirdparty/pear/Console/Getopt.php b/thirdparty/pear/Console/Getopt.php new file mode 100644 index 0000000..7966d1a --- /dev/null +++ b/thirdparty/pear/Console/Getopt.php @@ -0,0 +1,251 @@ + | +// +----------------------------------------------------------------------+ +// +// $Id: Getopt.php,v 1.21.4.7 2003/12/05 21:57:01 andrei Exp $ + +require_once 'PEAR.php'; + +/** + * Command-line options parsing class. + * + * @author Andrei Zmievski + * + */ +class Console_Getopt { + /** + * Parses the command-line options. + * + * The first parameter to this function should be the list of command-line + * arguments without the leading reference to the running program. + * + * The second parameter is a string of allowed short options. Each of the + * option letters can be followed by a colon ':' to specify that the option + * requires an argument, or a double colon '::' to specify that the option + * takes an optional argument. + * + * The third argument is an optional array of allowed long options. The + * leading '--' should not be included in the option name. Options that + * require an argument should be followed by '=', and options that take an + * option argument should be followed by '=='. + * + * The return value is an array of two elements: the list of parsed + * options and the list of non-option command-line arguments. Each entry in + * the list of parsed options is a pair of elements - the first one + * specifies the option, and the second one specifies the option argument, + * if there was one. + * + * Long and short options can be mixed. + * + * Most of the semantics of this function are based on GNU getopt_long(). + * + * @param array $args an array of command-line arguments + * @param string $short_options specifies the list of allowed short options + * @param array $long_options specifies the list of allowed long options + * + * @return array two-element array containing the list of parsed options and + * the non-option arguments + * + * @access public + * + */ + function getopt2($args, $short_options, $long_options = null) + { + return Console_Getopt::doGetopt(2, $args, $short_options, $long_options); + } + + /** + * This function expects $args to start with the script name (POSIX-style). + * Preserved for backwards compatibility. + * @see getopt2() + */ + function getopt($args, $short_options, $long_options = null) + { + return Console_Getopt::doGetopt(1, $args, $short_options, $long_options); + } + + /** + * The actual implementation of the argument parsing code. + */ + function doGetopt($version, $args, $short_options, $long_options = null) + { + // in case you pass directly readPHPArgv() as the first arg + if (PEAR::isError($args)) { + return $args; + } + if (empty($args)) { + return array(array(), array()); + } + $opts = array(); + $non_opts = array(); + + settype($args, 'array'); + + if ($long_options) { + sort($long_options); + } + + /* + * Preserve backwards compatibility with callers that relied on + * erroneous POSIX fix. + */ + if ($version < 2) { + if (isset($args[0]{0}) && $args[0]{0} != '-') { + array_shift($args); + } + } + + reset($args); + while (list($i, $arg) = each($args)) { + + /* The special element '--' means explicit end of + options. Treat the rest of the arguments as non-options + and end the loop. */ + if ($arg == '--') { + $non_opts = array_merge($non_opts, array_slice($args, $i + 1)); + break; + } + + if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) { + $non_opts = array_merge($non_opts, array_slice($args, $i)); + break; + } elseif (strlen($arg) > 1 && $arg{1} == '-') { + $error = Console_Getopt::_parseLongOption(substr($arg, 2), $long_options, $opts, $args); + if (PEAR::isError($error)) + return $error; + } else { + $error = Console_Getopt::_parseShortOption(substr($arg, 1), $short_options, $opts, $args); + if (PEAR::isError($error)) + return $error; + } + } + + return array($opts, $non_opts); + } + + /** + * @access private + * + */ + function _parseShortOption($arg, $short_options, &$opts, &$args) + { + for ($i = 0; $i < strlen($arg); $i++) { + $opt = $arg{$i}; + $opt_arg = null; + + /* Try to find the short option in the specifier string. */ + if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':') + { + return PEAR::raiseError("Console_Getopt: unrecognized option -- $opt"); + } + + if (strlen($spec) > 1 && $spec{1} == ':') { + if (strlen($spec) > 2 && $spec{2} == ':') { + if ($i + 1 < strlen($arg)) { + /* Option takes an optional argument. Use the remainder of + the arg string if there is anything left. */ + $opts[] = array($opt, substr($arg, $i + 1)); + break; + } + } else { + /* Option requires an argument. Use the remainder of the arg + string if there is anything left. */ + if ($i + 1 < strlen($arg)) { + $opts[] = array($opt, substr($arg, $i + 1)); + break; + } else if (list(, $opt_arg) = each($args)) + /* Else use the next argument. */; + else + return PEAR::raiseError("Console_Getopt: option requires an argument -- $opt"); + } + } + + $opts[] = array($opt, $opt_arg); + } + } + + /** + * @access private + * + */ + function _parseLongOption($arg, $long_options, &$opts, &$args) + { + @list($opt, $opt_arg) = explode('=', $arg); + $opt_len = strlen($opt); + + for ($i = 0; $i < count($long_options); $i++) { + $long_opt = $long_options[$i]; + $opt_start = substr($long_opt, 0, $opt_len); + + /* Option doesn't match. Go on to the next one. */ + if ($opt_start != $opt) + continue; + + $opt_rest = substr($long_opt, $opt_len); + + /* Check that the options uniquely matches one of the allowed + options. */ + if ($opt_rest != '' && $opt{0} != '=' && + $i + 1 < count($long_options) && + $opt == substr($long_options[$i+1], 0, $opt_len)) { + return PEAR::raiseError("Console_Getopt: option --$opt is ambiguous"); + } + + if (substr($long_opt, -1) == '=') { + if (substr($long_opt, -2) != '==') { + /* Long option requires an argument. + Take the next argument if one wasn't specified. */; + if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) { + return PEAR::raiseError("Console_Getopt: option --$opt requires an argument"); + } + } + } else if ($opt_arg) { + return PEAR::raiseError("Console_Getopt: option --$opt doesn't allow an argument"); + } + + $opts[] = array('--' . $opt, $opt_arg); + return; + } + + return PEAR::raiseError("Console_Getopt: unrecognized option --$opt"); + } + + /** + * Safely read the $argv PHP array across different PHP configurations. + * Will take care on register_globals and register_argc_argv ini directives + * + * @access public + * @return mixed the $argv PHP array or PEAR error if not registered + */ + function readPHPArgv() + { + global $argv; + if (!is_array($argv)) { + if (!@is_array($_SERVER['argv'])) { + if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) { + return PEAR::raiseError("Console_Getopt: Could not read cmd args (register_argc_argv=Off?)"); + } + return $GLOBALS['HTTP_SERVER_VARS']['argv']; + } + return $_SERVER['argv']; + } + return $argv; + } + +} + +?> diff --git a/thirdparty/pear/GraphViz.php b/thirdparty/pear/GraphViz.php new file mode 100644 index 0000000..b439ad4 --- /dev/null +++ b/thirdparty/pear/GraphViz.php @@ -0,0 +1,572 @@ + and + * Sebastian Bergmann . All rights reserved. + * + * LICENSE: This source file is subject to version 3.0 of the PHP license + * that is available through the world-wide-web at the following URI: + * http://www.php.net/license/3_0.txt. If you did not receive a copy of + * the PHP License and are unable to obtain it through the web, please + * send a note to license@php.net so we can mail you a copy immediately. + * + * @category Image + * @package GraphViz + * @author Dr. Volker Göbbels + * @author Sebastian Bergmann + * @author Karsten Dambekalns + * @author Michael Lively Jr. + * @copyright 2001-2006 Sebastian Bergmann + * @license http://www.php.net/license/3_0.txt PHP License 3.0 + * @version CVS: $Id: GraphViz.php,v 1.25 2006/05/16 15:49:19 sebastian Exp $ + * @link http://pear.php.net/package/Image_GraphViz + * @since File available since Release 0.1 + */ + +require_once 'System.php'; + +/** + * Interface to AT&T's GraphViz tools. + * + * The GraphViz class allows for the creation of and the work with directed + * and undirected graphs and their visualization with AT&T's GraphViz tools. + * + * + * addNode( + * 'Node1', + * array( + * 'URL' => 'http://link1', + * 'label' => 'This is a label', + * 'shape' => 'box' + * ) + * ); + * + * $graph->addNode( + * 'Node2', + * array( + * 'URL' => 'http://link2', + * 'fontsize' => '14' + * ) + * ); + * + * $graph->addNode( + * 'Node3', + * array( + * 'URL' => 'http://link3', + * 'fontsize' => '20' + * ) + * ); + * + * $graph->addEdge( + * array( + * 'Node1' => 'Node2' + * ), + * array( + * 'label' => 'Edge Label' + * ) + * ); + * + * $graph->addEdge( + * array( + * 'Node1' => 'Node2' + * ), + * array( + * 'color' => 'red' + * ) + * ); + * + * $graph->image(); + * ?> + * + * + * @category Image + * @package GraphViz + * @author Sebastian Bergmann + * @author Dr. Volker Göbbels + * @author Karsten Dambekalns + * @author Michael Lively Jr. + * @copyright Copyright © 2001-2006 Dr. Volker Göbbels and Sebastian Bergmann + * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0 + * @version Release: @package_version@ + * @link http://pear.php.net/package/Image_GraphViz + * @since Class available since Release 0.1 + */ +class Image_GraphViz +{ + /** + * Path to GraphViz/dot command + * + * @var string + */ + var $dotCommand = 'dot'; + + /** + * Path to GraphViz/neato command + * + * @var string + */ + var $neatoCommand = 'neato'; + + /** + * Representation of the graph + * + * @var array + */ + var $graph; + + /** + * Constructor. + * + * Setting the name of the Graph is useful for including multiple image maps on + * one page. If not set, the graph will be named 'G'. + * + * @param boolean $directed Directed (TRUE) or undirected (FALSE) graph. + * @param array $attributes Attributes of the graph + * @param string $name Name of the Graph + * @access public + */ + function Image_GraphViz($directed = TRUE, $attributes = array(), $name = NULL) + { + $this->setDirected($directed); + $this->setAttributes($attributes); + $this->graph['name'] = $name; + } + + /** + * Output image of the graph in a given format. + * + * @param string Format of the output image. + * This may be one of the formats supported by GraphViz. + * @access public + */ + function image($format = 'svg') + { + if ($data = $this->fetch($format)) { + $sendContentLengthHeader = TRUE; + + switch ($format) { + case 'gif': + case 'png': + case 'wbmp': { + header('Content-Type: image/' . $format); + } + break; + + case 'jpg': { + header('Content-Type: image/jpeg'); + } + break; + + case 'pdf': { + header('Content-Type: application/pdf'); + } + break; + + case 'svg': { + header('Content-Type: image/svg+xml'); + } + break; + + default: { + $sendContentLengthHeader = FALSE; + } + } + + if ($sendContentLengthHeader) { + header('Content-Length: ' . strlen($data)); + } + + echo $data; + } + } + + /** + * Return image (data) of the graph in a given format. + * + * @param string Format of the output image. + * This may be one of the formats supported by GraphViz. + * @return string The image (data) created by GraphViz. + * @access public + * @since Method available since Release 1.1.0 + */ + function fetch($format = 'svg') + { + if ($file = $this->saveParsedGraph()) { + $outputfile = $file . '.' . $format; + $command = $this->graph['directed'] ? $this->dotCommand : $this->neatoCommand; + $command .= ' -T' . escapeshellarg($format) . ' -o' . escapeshellarg($outputfile) . ' ' . escapeshellarg($file); + + @`$command`; + @unlink($file); + + $fp = fopen($outputfile, 'rb'); + + if ($fp) { + $data = fread($fp, filesize($outputfile)); + fclose($fp); + @unlink($outputfile); + } + + return $data; + } + + return FALSE; + } + + /** + * Render a given dot file into another format. + * + * @param string The absolute path of the dot file to use. + * @param string The absolute path of the file to save to. + * @param string Format of the output image. + * This may be one of the formats supported by GraphViz. + * @return bool True if the file was saved, false otherwise. + * @access public + */ + function renderDotFile($dotfile, $outputfile, $format = 'svg') + { + if (file_exists($dotfile)) { + $oldmtime = file_exists($outputfile) ? filemtime($outputfile) : 0; + + $command = $this->graph['directed'] ? $this->dotCommand : $this->neatoCommand; + $command .= ' -T' . escapeshellarg($format) . ' -o' . escapeshellarg($outputfile) . ' ' . escapeshellarg($dotfile); + + @`$command`; + + if (file_exists($outputfile) && filemtime($outputfile) > $oldmtime) { + return TRUE; + } + } + + return FALSE; + } + + /** + * Add a cluster to the graph. + * + * @param string ID. + * @param array Title. + * @param array Attributes of the cluster. + * @access public + */ + function addCluster($id, $title, $attributes = array()) + { + $this->graph['clusters'][$id]['title'] = $title; + $this->graph['clusters'][$id]['attributes'] = $attributes; + } + + /** + * Add a note to the graph. + * + * @param string Name of the node. + * @param array Attributes of the node. + * @param string Group of the node. + * @access public + */ + function addNode($name, $attributes = array(), $group = 'default') + { + $this->graph['nodes'][$group][$name] = $attributes; + } + + /** + * Remove a node from the graph. + * + * @param Name of the node to be removed. + * @access public + */ + function removeNode($name, $group = 'default') + { + if (isset($this->graph['nodes'][$group][$name])) { + unset($this->graph['nodes'][$group][$name]); + } + } + + /** + * Add an edge to the graph. + * + * Caveat! This cannot handle multiple identical edges. If you use non-numeric + * IDs for the nodes, this will not do (too much) harm. For numeric IDs the + * array_merge() that is used will change the keys when merging arrays, leading + * to new nodes appearing in the graph. + * + * @param array Start and End node of the edge. + * @param array Attributes of the edge. + * @access public + */ + function addEdge($edge, $attributes = array()) + { + if (is_array($edge)) { + $from = key($edge); + $to = $edge[$from]; + $id = $from . '_' . $to; + + if (!isset($this->graph['edges'][$id])) { + $this->graph['edges'][$id] = $edge; + } else { + $this->graph['edges'][$id] = array_merge( + $this->graph['edges'][$id], + $edge + ); + } + + if (is_array($attributes)) { + if (!isset($this->graph['edgeAttributes'][$id])) { + $this->graph['edgeAttributes'][$id] = $attributes; + } else { + $this->graph['edgeAttributes'][$id] = array_merge( + $this->graph['edgeAttributes'][$id], + $attributes + ); + } + } + } + } + + /** + * Remove an edge from the graph. + * + * @param array Start and End node of the edge to be removed. + * @access public + */ + function removeEdge($edge) + { + if (is_array($edge)) { + $from = key($edge); + $to = $edge[$from]; + $id = $from . '_' . $to; + + if (isset($this->graph['edges'][$id])) { + unset($this->graph['edges'][$id]); + } + + if (isset($this->graph['edgeAttributes'][$id])) { + unset($this->graph['edgeAttributes'][$id]); + } + } + } + + /** + * Add attributes to the graph. + * + * @param array Attributes to be added to the graph. + * @access public + */ + function addAttributes($attributes) + { + if (is_array($attributes)) { + $this->graph['attributes'] = array_merge( + $this->graph['attributes'], + $attributes + ); + } + } + + /** + * Set attributes of the graph. + * + * @param array Attributes to be set for the graph. + * @access public + */ + function setAttributes($attributes) + { + if (is_array($attributes)) { + $this->graph['attributes'] = $attributes; + } + } + + /** + * Set directed/undirected flag for the graph. + * + * @param boolean Directed (TRUE) or undirected (FALSE) graph. + * @access public + */ + function setDirected($directed) + { + if (is_bool($directed)) { + $this->graph['directed'] = $directed; + } + } + + /** + * Load graph from file. + * + * @param string File to load graph from. + * @access public + */ + function load($file) + { + if ($serializedGraph = implode('', @file($file))) { + $this->graph = unserialize($serializedGraph); + } + } + + /** + * Save graph to file. + * + * @param string File to save the graph to. + * @return mixed File the graph was saved to, FALSE on failure. + * @access public + */ + function save($file = '') + { + $serializedGraph = serialize($this->graph); + + if (empty($file)) { + $file = System::mktemp('graph_'); + } + + if ($fp = @fopen($file, 'w')) { + @fputs($fp, $serializedGraph); + @fclose($fp); + + return $file; + } + + return FALSE; + } + + /** + * Parse the graph into GraphViz markup. + * + * @return string GraphViz markup + * @access public + */ + function parse() + { + if (isset($this->graph['name']) && is_string($this->graph['name'])) { + $parsedGraph = "digraph " . $this->graph['name'] . " {\n"; + } else { + $parsedGraph = "digraph G {\n"; + } + + if (isset($this->graph['attributes'])) { + foreach ($this->graph['attributes'] as $key => $value) { + $attributeList[] = $key . '="' . $value . '"'; + } + + if (!empty($attributeList)) { + $parsedGraph .= 'graph [ '.implode(',', $attributeList) . " ];\n"; + } + } + + if (isset($this->graph['nodes'])) { + foreach($this->graph['nodes'] as $group => $nodes) { + if ($group != 'default') { + $parsedGraph .= sprintf( + "subgraph \"cluster_%s\" {\nlabel=\"%s\";\n", + + $group, + isset($this->graph['clusters'][$group]) ? $this->graph['clusters'][$group]['title'] : '' + ); + + if (isset($this->graph['clusters'][$group]['attributes'])) { + unset($attributeList); + + foreach ($this->graph['clusters'][$group]['attributes'] as $key => $value) { + $attributeList[] = $key . '="' . $value . '"'; + } + + if (!empty($attributeList)) { + $parsedGraph .= implode(',', $attributeList) . ";\n"; + } + } + } + + foreach($nodes as $node => $attributes) { + unset($attributeList); + + foreach($attributes as $key => $value) { + $attributeList[] = $key . '="' . $value . '"'; + } + + if (!empty($attributeList)) { + $parsedGraph .= sprintf( + "\"%s\" [ %s ];\n", + addslashes(stripslashes($node)), + implode(',', $attributeList) + ); + } + } + + if ($group != 'default') { + $parsedGraph .= "}\n"; + } + } + } + + if (isset($this->graph['edges'])) { + foreach($this->graph['edges'] as $label => $node) { + unset($attributeList); + + $from = key($node); + $to = $node[$from]; + + foreach($this->graph['edgeAttributes'][$label] as $key => $value) { + $attributeList[] = $key . '="' . $value . '"'; + } + + $parsedGraph .= sprintf( + '"%s" -> "%s"', + addslashes(stripslashes($from)), + addslashes(stripslashes($to)) + ); + + if (!empty($attributeList)) { + $parsedGraph .= sprintf( + ' [ %s ]', + implode(',', $attributeList) + ); + } + + $parsedGraph .= ";\n"; + } + } + + return $parsedGraph . "}\n"; + } + + /** + * Save GraphViz markup to file. + * + * @param string File to write the GraphViz markup to. + * @return mixed File to which the GraphViz markup was + * written, FALSE on failure. + * @access public + */ + function saveParsedGraph($file = '') + { + $parsedGraph = $this->parse(); + + if (!empty($parsedGraph)) { + if (empty($file)) { + $file = System::mktemp('graph_'); + } + + if ($fp = @fopen($file, 'w')) { + @fputs($fp, $parsedGraph); + @fclose($fp); + + return $file; + } + } + + return FALSE; + } +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * c-hanging-comment-ender-p: nil + * End: + */ +?> diff --git a/thirdparty/pear/System.php b/thirdparty/pear/System.php new file mode 100644 index 0000000..dc3acee --- /dev/null +++ b/thirdparty/pear/System.php @@ -0,0 +1,540 @@ + | +// +----------------------------------------------------------------------+ +// +// $Id: System.php,v 1.36 2004/06/15 16:33:46 pajoye Exp $ +// + +require_once 'PEAR.php'; +require_once 'Console/Getopt.php'; + +$GLOBALS['_System_temp_files'] = array(); + +/** +* System offers cross plattform compatible system functions +* +* Static functions for different operations. Should work under +* 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). +* +* Documentation on this class you can find in: +* http://pear.php.net/manual/ +* +* Example usage: +* if (!@System::rm('-r file1 dir1')) { +* print "could not delete file1 or dir1"; +* } +* +* In case you need to to pass file names with spaces, +* pass the params as an array: +* +* System::rm(array('-r', $file1, $dir1)); +* +* @package System +* @author Tomas V.V.Cox +* @version $Revision: 1.36 $ +* @access public +* @see http://pear.php.net/manual/ +*/ +class System +{ + /** + * returns the commandline arguments of a function + * + * @param string $argv the commandline + * @param string $short_options the allowed option short-tags + * @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) + { + if (!is_array($argv) && $argv !== null) { + $argv = preg_split('/\s+/', $argv); + } + return Console_Getopt::getopt2($argv, $short_options); + } + + /** + * 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; + } + + /** + * 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) { + System::raiseError("Could not open dir $sPath"); + 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); + if ($aktinst < $maxinst || $maxinst == 0) { + foreach($list as $val) { + $path = $sPath . DIRECTORY_SEPARATOR . $val; + if (is_dir($path)) { + $tmp = System::_dirToStruct($path, $maxinst, $aktinst+1); + $struct = array_merge_recursive($tmp, $struct); + } else { + $struct['files'][] = $path; + } + } + } + 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()); + settype($files, 'array'); + foreach ($files as $file) { + if (is_dir($file)) { + $tmp = System::_dirToStruct($file, 0); + $struct = array_merge_recursive($tmp, $struct); + } else { + $struct['files'][] = $file; + } + } + return $struct; + } + + /** + * The rm command for removing files. + * Supports multiple files and dirs and also recursive deletes + * + * @param string $args the arguments for rm + * @return mixed PEAR_Error or true for success + * @access public + */ + function rm($args) + { + $opts = System::_parseArgs($args, 'rf'); // "f" do nothing but like it :-) + 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; + } + } + 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; + } + + /** + * 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. + * + * @param string $args the name of the director(y|ies) to create + * @return bool True for success + * @access public + */ + function mkDir($args) + { + $opts = System::_parseArgs($args, 'pm:'); + if (PEAR::isError($opts)) { + return System::raiseError($opts); + } + $mode = 0777; // default mode + foreach($opts[0] as $opt) { + if ($opt[0] == 'p') { + $create_parents = true; + } elseif($opt[0] == 'm') { + // 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; + } + $mode = $opt[1]; + } + } + $ret = true; + 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)) { + if (!call_user_func('mkdir', $newdir, $mode)) { + $ret = false; + } + } + } + } else { + foreach($opts[1] as $dir) { + if (!@is_dir($dir) && !call_user_func('mkdir', $dir, $mode)) { + $ret = false; + } + } + } + return $ret; + } + + /** + * 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)) { + $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 (isset($outputfd)) { + fwrite($outputfd, $cont); + } else { + $ret .= $cont; + } + } + fclose($fd); + } + if (@is_resource($outputfd)) { + fclose($outputfd); + } + return $ret; + } + + /** + * Creates temporary files or directories. This function will remove + * the created files when the scripts finish its execution. + * + * Usage: + * 1) $tempfile = System::mktemp("prefix"); + * 2) $tempdir = System::mktemp("-d prefix"); + * 3) $tempfile = System::mktemp(); + * 4) $tempfile = System::mktemp("-t /var/tmp prefix"); + * + * prefix -> The string that will be prepended to the temp name + * (defaults to "tmp"). + * -d -> A temporary dir will be created instead of a file. + * -t -> The target dir where the temporary (file|dir) will be created. If + * 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 + * @return mixed the full path of the created (file|dir) or false + * @see System::tmpdir() + * @access public + */ + 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("-p $tmpdir")) { + return false; + } + $tmp = tempnam($tmpdir, $prefix); + if (isset($tmp_is_dir)) { + unlink($tmp); // be careful possible race condition here + if (!call_user_func('mkdir', $tmp, 0700)) { + return System::raiseError("Unable to create temporary directory $tmpdir"); + } + } + $GLOBALS['_System_temp_files'][] = $tmp; + if ($first_time) { + PEAR::registerShutdownFunc(array('System', '_removeTmpFiles')); + $first_time = false; + } + return $tmp; + } + + /** + * 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); + } + } + + /** + * Get the path of the temporal directory set in the system + * by looking in its environments variables. + * 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 + * + * @return string The temporal directory on the system + */ + function tmpdir() + { + if (OS_WINDOWS) { + if ($var = isset($_ENV['TEMP']) ? $_ENV['TEMP'] : getenv('TEMP')) { + return $var; + } + if ($var = isset($_ENV['TMP']) ? $_ENV['TMP'] : getenv('TMP')) { + 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 '/tmp'; + } + + /** + * The "which" command (show the full path of a command) + * + * @param string $program The command to search for + * @return mixed A string with the full path or false if not found + * @author Stig Bakken + */ + function which($program, $fallback = false) + { + // is_executable() is not available on windows + if (OS_WINDOWS) { + $pear_is_executable = 'is_file'; + } else { + $pear_is_executable = 'is_executable'; + } + + // full path given + if (basename($program) != $program) { + return (@$pear_is_executable($program)) ? $program : $fallback; + } + + // XXX FIXME honor safe mode + $path_delim = OS_WINDOWS ? ';' : ':'; + $exe_suffixes = OS_WINDOWS ? array('.exe','.bat','.cmd','.com') : array(''); + $path_elements = explode($path_delim, getenv('PATH')); + 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; + } + } + } + return $fallback; + } + + /** + * 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 -> max depth of recursion + * -name -> 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; + } +} +?>