ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
System Class Reference
+ Collaboration diagram for System:

Static Public Member Functions

 _parseArgs ($argv, $short_options, $long_options=null)
 returns the commandline arguments of a function
 raiseError ($error)
 Output errors with PHP trigger_error().
 _dirToStruct ($sPath, $maxinst, $aktinst=0, $silent=false)
 Creates a nested array representing the structure of a directory.
 _multipleToStruct ($files)
 Creates a nested array representing the structure of a directory and files.
 rm ($args)
 The rm command for removing files.
 mkDir ($args)
 Make directories.
cat ($args)
 Concatenate files.
 mktemp ($args=null)
 Creates temporary files or directories.
 _removeTmpFiles ()
 Remove temporary files created my mkTemp.
 tmpdir ()
 Get the path of the temporal directory set in the system by looking in its environments variables.
 which ($program, $fallback=false)
 The "which" command (show the full path of a command)
 find ($args)
 The "find" command.

Detailed Description

Definition at line 59 of file System.php.

Member Function Documentation

System::_dirToStruct (   $sPath,
  $maxinst,
  $aktinst = 0,
  $silent = false 
)
static

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 ) )

Parameters
string$sPathName of the directory
integer$maxinstmax. deep of the lookup
integer$aktinststarting deep of the lookup
bool$silentif true, do not emit errors.
Returns
array the structure of the dir private

Definition at line 122 of file System.php.

References $dir, $file, $path, and raiseError().

Referenced by _multipleToStruct(), and find().

{
$struct = array('dirs' => array(), 'files' => array());
if (($dir = @opendir($sPath)) === false) {
if (!$silent) {
System::raiseError("Could not open dir $sPath");
}
return $struct; // XXX could not open error
}
$struct['dirs'][] = $sPath = realpath($sPath); // XXX don't add if '.' or '..' ?
$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;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

System::_multipleToStruct (   $files)
static

Creates a nested array representing the structure of a directory and files.

Parameters
array$filesArray listing files and dirs
Returns
array
See Also
System::_dirToStruct()

Definition at line 165 of file System.php.

References $file, $files, and _dirToStruct().

Referenced by rm().

{
$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;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

System::_parseArgs (   $argv,
  $short_options,
  $long_options = null 
)
static

returns the commandline arguments of a function

Parameters
string$argvthe commandline
string$short_optionsthe allowed option short-tags
string$long_optionsthe allowed option long-tags
Returns
array the given options and there values private

Definition at line 71 of file System.php.

Referenced by mkDir(), mktemp(), and rm().

{
if (!is_array($argv) && $argv !== null) {
$argv = preg_split('/\s+/', $argv, -1, PREG_SPLIT_NO_EMPTY);
}
return Console_Getopt::getopt2($argv, $short_options);
}

+ Here is the caller graph for this function:

System::_removeTmpFiles ( )
static

Remove temporary files created my mkTemp.

This function is executed at script shutdown time

private

Definition at line 435 of file System.php.

References $GLOBALS, and rm().

{
if (count($GLOBALS['_System_temp_files'])) {
$delete = $GLOBALS['_System_temp_files'];
array_unshift($delete, '-r');
System::rm($delete);
$GLOBALS['_System_temp_files'] = array();
}
}

+ Here is the call graph for this function:

& System::cat (   $args)
static

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)

Parameters
string$argsthe arguments
Returns
boolean true on success public

Definition at line 308 of file System.php.

References $file, $files, $ret, and raiseError().

{
$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;
}

+ Here is the call graph for this function:

System::find (   $args)
static

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

Parameters
mixedEither array or string with the command line
Returns
array Array of found files

Definition at line 559 of file System.php.

References $dir, $files, $name, $path, $ret, and _dirToStruct().

{
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], '#');
// our magic characters ? and * have just been escaped,
// so now we change the escaped versions to PCRE operators
$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++) {
// only search in the part of the file below the current directory
$filepart = basename($files[$i]);
if (preg_match($pattern, $filepart)) {
$ret[] = $files[$i];
}
}
return $ret;
}
return $files;
}

+ Here is the call graph for this function:

System::mkDir (   $args)
static

Make directories.

The -p option will create parent directories

Parameters
string$argsthe name of the director(y|ies) to create
Returns
bool True for success public

Definition at line 237 of file System.php.

References $dir, $ret, _parseArgs(), PEAR\isError(), and raiseError().

Referenced by mktemp().

{
$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 ((!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;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

System::mktemp (   $args = null)
static

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: or /tmp will be used.

Parameters
string$argsThe arguments
Returns
mixed the full path of the created (file|dir) or false
See Also
System::tmpdir() User interface

Definition at line 382 of file System.php.

References $GLOBALS, _parseArgs(), PEAR\isError(), mkDir(), raiseError(), PEAR\registerShutdownFunc(), and tmpdir().

{
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); // be careful possible race condition here
if (!mkdir($tmp, 0700)) {
return System::raiseError("Unable to create temporary directory $tmpdir");
}
}
$GLOBALS['_System_temp_files'][] = $tmp;
if (isset($tmp_is_dir)) {
//$GLOBALS['_System_temp_files'][] = dirname($tmp);
}
if ($first_time) {
PEAR::registerShutdownFunc(array('System', '_removeTmpFiles'));
$first_time = false;
}
return $tmp;
}

+ Here is the call graph for this function:

System::raiseError (   $error)
static

Output errors with PHP trigger_error().

You can silence the errors with prefixing a "@" sign to the function call: ::mkdir(..);

Parameters
mixed$errora PEAR error or a string with the error message
Returns
bool false private

Definition at line 88 of file System.php.

References $error, and PEAR\isError().

Referenced by _dirToStruct(), cat(), mkDir(), mktemp(), and rm().

{
$error = $error->getMessage();
}
trigger_error($error, E_USER_WARNING);
return false;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

System::rm (   $args)
static

The rm command for removing files.

Supports multiple files and dirs and also recursive deletes

Parameters
string$argsthe arguments for rm
Returns
mixed PEAR_Error or true for success public

Definition at line 191 of file System.php.

References $dir, $file, $ret, _multipleToStruct(), _parseArgs(), PEAR\isError(), and raiseError().

Referenced by _removeTmpFiles().

{
$opts = System::_parseArgs($args, 'rf'); // "f" does nothing but I 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;
}
}
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;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

System::tmpdir ( )
static

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

Returns
string The temporary directory on the system

Definition at line 454 of file System.php.

Referenced by mktemp(), OLE_PPS_File\OLE_PPS_File(), and OLE_PPS_Root\OLE_PPS_Root().

{
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');
}

+ Here is the caller graph for this function:

System::which (   $program,
  $fallback = false 
)
static

The "which" command (show the full path of a command)

Parameters
string$programThe command to search for
mixed$fallbackValue to return if $program is not found
Returns
mixed A string with the full path or false if not found
Author
Stig Bakken ssb@p.nosp@m.hp.n.nosp@m.et

Definition at line 487 of file System.php.

References $dir, $fallback, $file, and $path.

{
// enforce API
if (!is_string($program) || '' == $program) {
return $fallback;
}
// full path given
if (basename($program) != $program) {
$path_elements[] = dirname($program);
$program = basename($program);
} else {
// Honor safe mode
if (!ini_get('safe_mode') || !$path = ini_get('safe_mode_exec_dir')) {
$path = getenv('PATH');
if (!$path) {
$path = getenv('Path'); // some OSes are just stupid enough to do this
}
}
$path_elements = explode(PATH_SEPARATOR, $path);
}
if (OS_WINDOWS) {
$exe_suffixes = getenv('PATHEXT')
? explode(PATH_SEPARATOR, getenv('PATHEXT'))
: array('.exe','.bat','.cmd','.com');
// allow passing a command.exe param
if (strpos($program, '.') !== false) {
array_unshift($exe_suffixes, '');
}
// is_executable() is not available on windows for PHP4
$pear_is_executable = (function_exists('is_executable')) ? 'is_executable' : 'is_file';
} else {
$exe_suffixes = array('');
$pear_is_executable = 'is_executable';
}
foreach ($exe_suffixes as $suff) {
foreach ($path_elements as $dir) {
$file = $dir . DIRECTORY_SEPARATOR . $program . $suff;
if (@$pear_is_executable($file)) {
return $file;
}
}
}
return $fallback;
}

The documentation for this class was generated from the following file: