Source for file System.php
Documentation is available at System.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at the following url: |
// | 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 world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Tomas V.V.Cox <cox@idecnet.com> |
// +----------------------------------------------------------------------+
// $Id: System.php,v 1.36 2004/06/15 16:33:46 pajoye Exp $
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/
* 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));
* @author Tomas V.V.Cox <cox@idecnet.com>
* @version $Revision: 1.36 $
* @see http://pear.php.net/manual/
* 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
function _parseArgs ($argv, $short_options, $long_options = null )
if (!is_array($argv) && $argv !== null ) {
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
function raiseError ($error)
if (PEAR ::isError ($error)) {
$error = $error->getMessage ();
* Creates a nested array representing the structure of a directory
* System::_dirToStruct('dir1', 0) =>
* @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
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 '..' ?
if ($file != '.' && $file != '..') {
if ($aktinst < $maxinst || $maxinst == 0 ) {
$path = $sPath . DIRECTORY_SEPARATOR . $val;
$tmp = System::_dirToStruct ($path, $maxinst, $aktinst+1 );
$struct['files'][] = $path;
* Creates a nested array representing the structure of a directory and files
* @param array $files Array listing files and dirs
* @see System::_dirToStruct()
function _multipleToStruct ($files)
$struct = array ('dirs' => array (), 'files' => array ());
foreach ($files as $file) {
$tmp = System::_dirToStruct ($file, 0 );
$struct['files'][] = $file;
* 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
$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 (isset ($do_recursive)) {
$struct = System::_multipleToStruct ($opts[1 ]);
foreach($struct['files'] as $file) {
foreach($struct['dirs'] as $dir) {
foreach ($opts[1 ] as $file) {
$delete = (is_dir($file)) ? 'rmdir' : 'unlink';
* 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
$opts = System::_parseArgs ($args, 'pm:');
if (PEAR ::isError ($opts)) {
return System::raiseError ($opts);
$mode = 0777; // default mode
foreach($opts[0 ] as $opt) {
} elseif ($opt[0 ] == 'm') {
// if the mode is clearly an octal number (starts with 0)
if (strlen($opt[1 ]) && $opt[1 ]{0 } == '0') {
if (isset ($create_parents)) {
foreach($opts[1 ] as $dir) {
while (!@is_dir($dir) && $dir != DIRECTORY_SEPARATOR ) {
foreach($opts[1 ] as $dir) {
* 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
for($i=0; $i < count($args); $i++ ) {
$outputfile = $args[$i+1 ];
} elseif ($args[$i] == '>>') {
$outputfile = $args[$i+1 ];
if (!$outputfd = fopen($outputfile, $mode)) {
$err = System::raiseError (" Could not open $outputfile" );
foreach ($files as $file) {
if (!$fd = fopen($file, 'r')) {
System::raiseError (" Could not open $file" );
while ($cont = fread($fd, 2048 )) {
* Creates temporary files or directories. This function will remove
* the created files when the scripts finish its execution.
* 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
* -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
static $first_time = true;
$opts = System ::_parseArgs ($args, 't:d');
if (PEAR ::isError ($opts)) {
return System::raiseError ($opts);
foreach($opts[0 ] as $opt) {
} elseif ($opt[0 ] == 't') {
$prefix = (isset ($opts[1 ][0 ])) ? $opts[1 ][0 ] : 'tmp';
if (isset ($tmp_is_dir)) {
unlink($tmp); // be careful possible race condition here
return System::raiseError (" Unable to create temporary directory $tmpdir" );
$GLOBALS['_System_temp_files'][] = $tmp;
PEAR ::registerShutdownFunc (array ('System', '_removeTmpFiles'));
* Remove temporary files created my mkTemp. This function is executed
* at script shutdown time
function _removeTmpFiles ()
if (count($GLOBALS['_System_temp_files'])) {
$delete = $GLOBALS['_System_temp_files'];
* 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
if ($var = isset ($_ENV['TEMP']) ? $_ENV['TEMP'] : getenv('TEMP')) {
if ($var = isset ($_ENV['TMP']) ? $_ENV['TMP'] : getenv('TMP')) {
if ($var = isset ($_ENV['windir']) ? $_ENV['windir'] : getenv('windir')) {
return getenv('SystemRoot') . '\temp';
if ($var = isset ($_ENV['TMPDIR']) ? $_ENV['TMPDIR'] : getenv('TMPDIR')) {
* 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 <ssb@php.net>
function which($program, $fallback = false )
// is_executable() is not available on windows
$pear_is_executable = 'is_file';
$pear_is_executable = 'is_executable';
return (@$pear_is_executable($program)) ? $program : $fallback;
// XXX FIXME honor safe mode
$exe_suffixes = OS_WINDOWS ? array ('.exe','.bat','.cmd','.com') : array ('');
foreach ($exe_suffixes as $suff) {
foreach ($path_elements as $dir) {
$file = $dir . DIRECTORY_SEPARATOR . $program . $suff;
if (@is_file($file) && @$pear_is_executable($file)) {
* 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");
* $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
$args = preg_split('/\s+/', $args, -1 , PREG_SPLIT_NO_EMPTY );
$do_files = $do_dirs = true;
for ($i = 0; $i < count($args); $i++ ) {
if (in_array($args[$i+1 ], array ('d', 'f'))) {
if ($args[$i+1 ] == 'd') {
$path = System::_dirToStruct ($dir, $depth);
if ($do_files && $do_dirs) {
$patterns = implode('|', $patterns);
for ($i = 0; $i < count($files); $i++ ) {
Documentation generated on Mon, 11 Mar 2019 14:24:01 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|