home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / php / pear / System.php < prev    next >
PHP Script  |  2001-11-13  |  9KB  |  286 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2001 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors:   Tomas V.V.Cox <cox@idecnet.com>                           |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: System.php,v 1.1.2.1 2001/11/13 01:26:39 ssb Exp $
  21. //
  22.  
  23. // TODO:
  24. // - Test under Windows (help is really appreciated in this point)
  25. // - Build strong tests
  26. // - Error reporting (now shows standar php errors)
  27. // - Write doc
  28.  
  29. require_once 'PEAR.php';
  30. require_once 'Console/Getopt.php';
  31.  
  32. /**
  33. * System offers cross plattform compatible system functions
  34. *
  35. * Static functions for different operations. Should work under
  36. * Unix and Windows. The names and usage has been taken from its repectively
  37. * GNU commands.
  38. *
  39. * Usage: System::rm('-r file1 dir1');
  40. *
  41. * ------------------- EXPERIMENTAL STATUS -------------------
  42. *
  43. * @package  System
  44. * @author   Tomas V.V.Cox <cox@idecnet.com>
  45. * @version  $Version$
  46. * @access   public
  47. */
  48. class System extends PEAR
  49. {
  50.     /**
  51.     * returns the commandline arguments of a function
  52.     *
  53.     * @param    string  $argv           the commandline
  54.     * @param    string  $short_options  the allowed option short-tags 
  55.     * @param    string  $long_options   the allowed option long-tags
  56.     * @return   array   the given options and there values
  57.     * @access private
  58.     */
  59.     function _parseArgs($argv, $short_options, $long_options = null)
  60.     {
  61.         if (!is_array($argv)) {
  62.             $argv = preg_split('/\s+/', $argv);
  63.         }
  64.         $options = Console_Getopt::getopt($argv, $short_options);
  65.         return $options;
  66.     }
  67.  
  68.     /**
  69.     * Creates a nested array representing the structure of a directory
  70.     *
  71.     * System::_dirToStruct('dir1', 0) =>
  72.     *   Array
  73.     *    (
  74.     *    [dirs] => Array
  75.     *        (
  76.     *            [0] => dir1
  77.     *        )
  78.     *
  79.     *    [files] => Array
  80.     *        (
  81.     *            [0] => dir1/file2
  82.     *            [1] => dir1/file3
  83.     *        )
  84.     *    )
  85.     * @param    string  $sPath      Name of the directory
  86.     * @param    integer $maxinst    max. deep of the lookup
  87.     * @param    integer $aktinst    starting deep of the lookup
  88.     * @return   array   the structure of the dir
  89.     * @access   private
  90.     */
  91.  
  92.     function _dirToStruct($sPath, $maxinst, $aktinst = 0)
  93.     {
  94.         $struct = array('dirs' => array(), 'files' => array());
  95.         if (($dir = @opendir($sPath)) === false) {
  96.             return $struct; // XXX could not open error
  97.         }
  98.         $struct['dirs'][] = $sPath; // XXX don't add if '.' or '..' ?
  99.         $list = array();
  100.         while ($file = readdir($dir)) {
  101.             if ($file != '.' && $file != '..') {
  102.                 $list[] = $file;
  103.             }
  104.         }
  105.         closedir($dir);
  106.         sort($list);
  107.         foreach($list as $val) {
  108.             $path = $sPath . DIRECTORY_SEPARATOR . $val;
  109.             if (is_dir($path)) {
  110.                 if ($aktinst < $maxinst || $maxinst == 0) {
  111.                     $tmp = System::_dirToStruct($path, $maxinst, $aktinst+1);
  112.                     $struct = array_merge_recursive($tmp, $struct);
  113.                 }
  114.             } else {
  115.                 $struct['files'][] = $path;
  116.             }
  117.         }
  118.         return $struct;
  119.     }
  120.  
  121.     /**
  122.     * Creates a nested array representing the structure of a directory and files
  123.     *
  124.     * @param    array $files Array listing files and dirs
  125.     * @return   array
  126.     * @see System::_dirToStruct()
  127.     */
  128.     function _multipleToStruct($files)
  129.     {
  130.         $struct = array('dirs' => array(), 'files' => array());
  131.         foreach($files as $file) {
  132.             if (is_dir($file)) {
  133.                 $tmp = System::_dirToStruct($file, 0);
  134.                 $struct = array_merge_recursive($tmp, $struct);
  135.             } else {
  136.                 $struct['files'][] = $file;
  137.             }
  138.         }
  139.         return $struct;
  140.     }
  141.  
  142.     /**
  143.     * The rm command for removing files. 
  144.     * Supports multiple files and dirs and also recursive deletes
  145.     *
  146.     * @param    string  $args   the arguments for rm
  147.     * @return   mixed   PEAR_Error or true for success
  148.     * @access   public    
  149.     */
  150.     function rm($args)
  151.     {
  152.         $opts = System::_parseArgs($args, 'rf'); // "f" do nothing but like it :-)
  153.         if (PEAR::isError($opts)) {
  154.             return $opts;
  155.         }
  156.         foreach($opts[0] as $opt) {
  157.             if ($opt[0] == 'r') {
  158.                 $do_recursive = true;
  159.             }
  160.         }
  161.         if (isset($do_recursive)) {
  162.             $struct = System::_multipleToStruct($opts[1]);
  163.             if (PEAR::isError($struct)) {
  164.                 return $struct;
  165.             }
  166.             foreach($struct['files'] as $file) {
  167.                 unlink($file); // XXXX Works under Windows?
  168.             }
  169.             foreach($struct['dirs'] as $dir) {
  170.                 rmdir($dir);
  171.             }
  172.         } else {
  173.             foreach ($opts[1] as $file) {
  174.                 $delete = (is_dir($file)) ? 'rmdir' : 'unlink'; // XXXX Windows?
  175.                 $delete($file);
  176.             }
  177.         }
  178.         return true;
  179.     }
  180.  
  181.     /**
  182.     * Make directories
  183.     *
  184.     * @param    string  $args    the name of the director(y|ies) to create
  185.     * @return   mixed   PEAR_Error or true for success
  186.     * @access   public    
  187.     */
  188.     function mkDir($args)
  189.     {
  190.         $opts = System::_parseArgs($args, 'pm:');
  191.         if (PEAR::isError($opts)) {
  192.             return $opts;
  193.         }
  194.         $mode = 0777; // default mode
  195.         foreach($opts[0] as $opt) {
  196.             if ($opt[0] == 'p') {
  197.                 $create_parents = true;
  198.             } elseif($opt[0] == 'm') {
  199.                 $mode = $opt[1];
  200.             }
  201.         }
  202.         if (isset($create_parents)) {
  203.             foreach($opts[1] as $dir) {
  204.                 $dirstack = array();
  205.                 while (!@is_dir($dir) && $dir != DIRECTORY_SEPARATOR) {
  206.                     array_unshift($dirstack, $dir);
  207.                     $dir = dirname($dir);
  208.                 }
  209.                 while ($newdir = array_shift($dirstack)) {
  210.                     if (!mkdir($newdir, $mode)) {
  211.                         break; // XXX error
  212.                     }
  213.                 }
  214.             }
  215.         } else {
  216.             foreach($opts[1] as $dir) {
  217.                 if (!mkdir($dir, $mode)) {
  218.                     continue; // XXX error
  219.                 }
  220.             }
  221.         }
  222.         return true;
  223.     }
  224.  
  225.     /**
  226.     * Concatenate files
  227.     *
  228.     * Usage:
  229.     * 1) $var = System::cat('sample.txt test.txt');
  230.     * 2) System::cat('sample.txt test.txt > final.txt');
  231.     * 3) System::cat('sample.txt test.txt >> final.txt');
  232.     *
  233.     * Note: as the class use fopen, urls should work also (test that)
  234.     *
  235.     * @param    string  $args   the arguments
  236.     * @return   boolean true on success
  237.     * @access   public
  238.     */
  239.     function &cat($args)
  240.     {
  241.         $ret = null;
  242.         $files = array();
  243.         if (!is_array($args)) {
  244.             $args = preg_split('/\s+/', $args);
  245.         }
  246.         for($i=0; $i < count($args); $i++) {
  247.             if ($args[$i] == '>') {
  248.                 $mode = 'wb';
  249.                 $outputfile = $args[$i+1];
  250.                 break;
  251.             } elseif ($args[$i] == '>>') {
  252.                 $mode = 'ab+';
  253.                 $outputfile = $args[$i+1];
  254.                 break;
  255.             } else {
  256.                 $files[] = $args[$i];
  257.             }
  258.         }
  259.         if (isset($mode)) {
  260.             if (!$outputfd = fopen($outputfile, $mode)) {
  261.                 return $this->raiseError("Could not open $outputfile");
  262.             }
  263.             $ret = true;
  264.         }
  265.         foreach($files as $file) {
  266.             if (!$fd = fopen($file, 'r')) {
  267.                 return $this->raiseError("Could not open $file");
  268.             }
  269.             while(!feof($fd)) {
  270.                 $cont = fread($fd, 2048);
  271.                 if (isset($outputfd)) {
  272.                     fwrite($outputfd, $cont);
  273.                 } else {
  274.                     $ret .= $cont;
  275.                 }
  276.             }
  277.             fclose($fd);
  278.         }
  279.         if (@is_resource($outputfd)) {
  280.             fclose($outputfd);
  281.         }
  282.         return $ret;
  283.     }
  284.  
  285. }
  286. ?>