eSystem
[ class tree: eSystem ] [ index: eSystem ] [ all elements ]

Source for file eSystem.php

Documentation is available at eSystem.php

  1. <?php
  2. /**
  3.  * Project: eSystem:: The Extended file system<br>
  4.  * File:    eSystem.php
  5.  *
  6.  * Defines the php extended system mapping function and any utility mapping function
  7.  *
  8.  * This Class requires {@link eFilesystem 'pear.oops.org/eFilesystem'} and
  9.  * {@link oGetopt 'pear.oops.org/oGetopt'}
  10.  *
  11.  * @category    System
  12.  * @package     eSystem
  13.  * @author      JoungKyun.Kim <http://oops.org>
  14.  * @copyright   (c) 2018, OOPS.org
  15.  * @license     BSD
  16.  * @link        http://pear.oops.org/package/eSystem
  17.  * @since       File available since relase 0.8
  18.  * @example     eSystem/test.php Sample codes for eSystem class
  19.  * @filesource
  20.  */
  21.  
  22. /**
  23.  * Dependency on {@link eFilesystem 'pear.oops.org/eFilesystem'} pear package over 1.0.0
  24.  */
  25. require_once 'eFilesystem.php';
  26.  
  27. /**
  28.  * Base class for system mapping api
  29.  *
  30.  * @access public
  31.  * @version $Revision$
  32.  * @package eSystem
  33.  */
  34. class eSystem
  35. {
  36.     // {{{ properties
  37.     private $system;
  38.     //private $man;
  39.  
  40.     /**#@+*/
  41.     /**
  42.      * @access public
  43.      */
  44.     /**
  45.      * Location for create tmp file
  46.      *
  47.      * @var string 
  48.      */
  49.     public $tmpdir = '/tmp';
  50.  
  51.     /**
  52.      * Standard Error file description
  53.      * @var reousrce 
  54.      */
  55.     public $stderr;
  56.     /**
  57.      * Standard Out file description
  58.      * @var reousrce 
  59.      */
  60.     public $stdout;
  61.     /**
  62.      * Shell return code
  63.      * @var integer 
  64.      */
  65.     public $retint;
  66.     /**#@-*/
  67.     // }}}
  68.  
  69.     // {{{ function autoload (&$obj, $f, $cname = '')
  70.     /**
  71.      * Autoload the specify class name
  72.      *
  73.      * @access private
  74.      * @return void 
  75.      * @param  object loading object that call this api
  76.      * @param  string file that is loaded
  77.      * @param  string (optional) name of class
  78.      */
  79.     private function autoload (&$obj$f$cname ''{
  80.         if $cname )
  81.             $cname $f;
  82.  
  83.         require_once 'eSystem/' $f '.php';
  84.         if is_object ($obj) ) {
  85.             $objname "eSystem_" $cname;
  86.             $obj new $objname;
  87.         }
  88.     }
  89.     // }}}
  90.  
  91.     // {{{ function system ($_cmd, &$_returncode = NULL)
  92.     /**
  93.      * Wrapping system function
  94.      *
  95.      * If server admin disables php system() function, this method
  96.      * has same execution with php system().
  97.      *
  98.      * You can use this api that change only system(...) to $obj->system(...)
  99.      *
  100.      * @access public
  101.      * @return string Returns the last line of the command output on success,
  102.      *                 and FALSE  on failure.
  103.      * @param  string The command that will be executed.
  104.      * @param  int    (optional) If the _returncode  argument is present,
  105.      *                 then the return status of the executed command will
  106.      *                 be written to this variable.
  107.      */
  108.     function system ($_cmd&$_returncode NULL{
  109.         $this->autoload ($this->system'system');
  110.  
  111.         $this->system->tmpdir $this->tmpdir;
  112.         $this->system->_stdout array ();
  113.         $this->system->_stderr '';
  114.         $this->system->_retint 0;
  115.  
  116.         $this->system->_system ($_cmd1);
  117.  
  118.         $_returncode $this->system->_retint;
  119.         $this->stderr = $this->system->_stderr;
  120.  
  121.         $_no count ($this->system->_stdout);
  122.         return $this->system->_stdout[--$_no];
  123.     }
  124.     // }}}
  125.  
  126.     // {{{ function exec ($_cmd, &$_output = NULL, &$_returncode = NULL)
  127.     /**
  128.      * Wrapping exec() function
  129.      *
  130.      * If server admin disables php exec() function, this method
  131.      * has same execution with php exec().
  132.      *
  133.      * You can use this api that change only exec(...) to $obj->exec(...)
  134.      *
  135.      * @access public
  136.      * @return string Returns the last line of the command output on success, and FALSE  on failure.
  137.      * @param  string The command that will be executed.
  138.      * @param  array   (optional) If the _output  argument is present, then the specified
  139.      *                  array will be filled with every line of output from the command.
  140.      *                  Trailing whitespace, such as \n, is not included in this array.
  141.      *                  Note that if the array already contains some elements, exec()
  142.      *                  will append to the end of the array. If you do not want the
  143.      *                  function to append elements, call unset() on the array before
  144.      *                  passing it to exec().
  145.      * @param  integer (optional) If the _returncode  argument is present, then the return
  146.      *                  status of the executed command will be written to this variable.
  147.      */
  148.     # mapping php exec function arguments.
  149.     function exec ($_cmd&$_output NULL&$_returncode NULL{
  150.         $this->autoload ($this->system'system');
  151.  
  152.         $this->system->tmpdir $this->tmpdir;
  153.         $this->system->_stdout array ();
  154.         $this->system->_stderr '';
  155.         $this->system->_retint 0;
  156.  
  157.         $this->system->_system ($_cmd);
  158.  
  159.         $_output $this->system->_stdout;
  160.         $_returncode $this->system->_retint;
  161.         $this->stderr = $this->system->_stderr;
  162.         $_no count ($this->system->_stdout);
  163.  
  164.         return $this->system->_stdout[--$_no];
  165.     }
  166.     // }}}
  167.  
  168.     // {{{ function execl ($_cmd, $_output = NULL, $_returncode = NULL)
  169.     /**
  170.      * Wrapping exec() function
  171.      *
  172.      * This method same $this->exec(), but return value of 2th argument is not array,
  173.      * it's plain text strings.
  174.      *
  175.      * @access public
  176.      * @return string Returns the last line of the command output on success, and FALSE  on failure.
  177.      * @param  string The command that will be executed.
  178.      * @param  string  (optional) If the _output  argument is present, this argument contains
  179.      *                  output from the command.
  180.      * @param  integer (optional) If the _returncode  argument is present, then the return
  181.      *                  status of the executed command will be written to this variable.
  182.      */
  183.     function execl ($_cmd$_output NULL$_returncode NULL{
  184.         $this->autoload ($this->system'system');
  185.  
  186.         $r $this->exec ($_cmd$_outputs$_returncode);
  187.  
  188.         if is_array ($_outputs&& ($c count ($_outputs)) ) {
  189.             $_output '';
  190.             for $i=0$i<$c$i++ {
  191.                 $_output .= $_outputs[$i"\n";
  192.             }
  193.         }
  194.  
  195.         $_output preg_replace ("/\n$/"''$_output);
  196.  
  197.         return $r;
  198.     }
  199.     // }}}
  200.  
  201.     // {{{ function mkdir_p ($path, $mode = 0755)
  202.     /**
  203.      * Attempts to create the directory specified by pathname.
  204.      * If does not parent directory, this API create success.
  205.      * This means that same operate with mkdir (path, mode, true) of php
  206.      *
  207.      * @deprecated deprecated since 1.0.1. Use {@link eFilesystem::mkdir_p()} instead
  208.      *              of this method
  209.      * @see     eFilesystem::mkdir_p()
  210.      * @access  public
  211.      * @return  boolean return false, create error by other error.<br>
  212.      *                   return true, create success.
  213.      * @param   string  given path
  214.      * @param   int     (optional) The mode is 0777 by default, which means the widest
  215.      *                   possible access. For more information on modes, read
  216.      *                   the details on the chmod() page.
  217.      */ 
  218.     function mkdir_p ($path$mode 0755{
  219.         return eFilesystem::mkdir_p ($path$mode);
  220.     }
  221.     // }}}
  222.  
  223.     // {{{ function unlink ($path)
  224.     /**
  225.      * Deletes a file. If given file is directory, no error and return false.
  226.      *
  227.      * @deprecated deprecated since 1.0.1. Use {@link eFilesystem::safe_unlnk()} instead
  228.      *              of this method
  229.      * @see     eFilesystem::safe_unlink()
  230.      * @access public
  231.      * @return int  return true, success<br>
  232.      *               return false, remove false<br>
  233.      *               return 2, file not found<br>
  234.      *               return 3, file is directory
  235.      * @param  string  given file path
  236.      */
  237.     function unlink ($path{
  238.         return eFilesystem::safe_unlink ($path);
  239.     }
  240.     // }}}
  241.  
  242.     // {{{ function unlink_r ($path)
  243.     /**
  244.      * Deletes a file or directory that include some files
  245.      *
  246.      * @deprecated deprecated since 1.0.1. Use {@link eFilesystem::unlink_r()} instead
  247.      *              of this method
  248.      * @see     eFilesystem::unlink_r()
  249.      * @access  public
  250.      * @return  boolean 
  251.      * @param   string  Given path.
  252.      *                   You can use Asterisk(*) or brace expand({a,b}) on path.
  253.      */
  254.     function unlink_r ($path{
  255.         return eFilesystem::unlink_r ($path);
  256.     }
  257.     // }}}
  258.  
  259.     // {{{ function tree ($dir = '.')
  260.     /**
  261.      * print directory tree for given path
  262.      *
  263.      * @deprecated deprecated since 1.0.1. Use {@link eFilesystem::tree()} instead
  264.      *              of this method
  265.      * @see     eFilesystem::tree()
  266.      * @access  public
  267.      * @return  object object 
  268.      *                   members are follow:<br>
  269.      *                   obj->file is number of files.<br>
  270.      *                   obj->dir is number of directories.
  271.      * @param   string  (optional) Given path. Defaults to current directory (./).
  272.      */
  273.     function tree ($dir '.'{
  274.         return eFilesystem::tree ($dir);
  275.     }
  276.     // }}}
  277.  
  278.     // {{{ function find ($path = './', $type = '', $norecursive = false)
  279.     /**
  280.      * get file list that under given path
  281.      *
  282.      * @deprecated deprecated since 1.0.1. Use {@link eFilesystem::find()} instead
  283.      *              of this method
  284.      * @see     eFilesystem::find()
  285.      * @access  public
  286.      * @return  array   return array of file list. If given path is null or don't exist, return false.
  287.      * @param   string  (optional) Given path. Defaults to current directory (./)
  288.      * @param   string  (optional) list type. Defaults to all.<br>
  289.      *                   f (get only files),<br>
  290.      *                   d (get only directories),<br>
  291.      *                   l (get only links),<br>
  292.      *                   fd (get only files and directories),<br>
  293.      *                   fl (get only files and links),<br>
  294.      *                   dl (get only directories and links)<br>
  295.      *                   /regex/ (use regular expression)
  296.      * @param   boolean (optional) Defaults to false.
  297.      *                   set true, don't recursive search.
  298.      */
  299.     function find ($path './'$type ''$norecursive false{
  300.         return eFilesystem::find ($path$type$norecursive);
  301.     }
  302.     // }}}
  303.  
  304.     // {{{ function putColor ($str, $color = '')
  305.     /**
  306.      * Return given string with ansi code about specified color
  307.      *
  308.      * Color strings support follows:
  309.      *   gray, red, green, yellow, blue, megenta, cyan, white
  310.      *
  311.      * @deprecated deprecated since 1.0.1. Use {@link ePrint::asPrintf()} instead
  312.      *              of this method
  313.      * @see    ePrint::asPrintf()
  314.      * @access public
  315.      * @return string 
  316.      * @param  string Input strings
  317.      * @param  string color string for anci code. Defaults to 'gray'
  318.      */
  319.     function putColor ($str$color 'gray'{
  320.         $this->__nocli ('putColor');
  321.  
  322.         return ePrint::asPrintf ($color$str);
  323.     }
  324.     // }}}
  325.  
  326.     // {{{ function boldStr ($str)
  327.     /**
  328.      * Return given string with white ansi code
  329.      *
  330.      * @deprecated deprecated since 1.0.1. Use
  331.      *              {@link ePrint::asPrintf() ePrint::asPrintf (string, white)} instead
  332.      *              of this method
  333.      * @see    ePrint::asPrintf()
  334.      * @access public
  335.      * @return string 
  336.      * @param  string Input strings
  337.      */
  338.     function boldStr ($str{
  339.         $this->__nocli('boldStr');
  340.  
  341.         return $this->putColor ($str'white');
  342.     }
  343.     // }}}
  344.  
  345.     // {{{ function makeWhiteSpace ($no)
  346.     /**
  347.      * Print white space about given number
  348.      *
  349.      * @deprecated deprecated since 1.0.1. Use {@link ePrint::whiteSpace()} instead
  350.      *              of this method
  351.      * @see     ePrint::whiteSpace()
  352.      * @access  public
  353.      * @return  strings 
  354.      * @param   integer number of space charactor
  355.      */
  356.     function makeWhiteSpace ($no{
  357.         return ePrint::whiteSpace ($notrue);
  358.     }
  359.     // }}}
  360.  
  361.     // {{{ function backSpace ($no)
  362.     /**
  363.      * print backspace
  364.      *
  365.      * This API is Deprecated. Use ePrint::backSpace instead of this method
  366.      *
  367.      * @deprecated deprecated since 1.0.1. Use {@link ePrint::backSpace()} instead
  368.      *              of this method
  369.      * @see     ePrint::backSpace()
  370.      * @access  public
  371.      * @return  void 
  372.      * @param   integer number of space charactor
  373.      */
  374.     function backSpace ($no{
  375.         ePrint::backSpace ($no);
  376.     }
  377.     // }}}
  378.  
  379.     // {{{ function printe ($format, $msg = '')
  380.     /**
  381.      * Output a formatted string to stderr
  382.      *
  383.      * @deprecated deprecated since 1.0.1. Use {@link ePrint::ePrintf()} instead
  384.      *              of this method
  385.      * @see     ePrint::ePrintf()
  386.      * @access  public
  387.      * @return  int     length of output messages.
  388.      * @param   string  same format of printf
  389.      * @param   mixed   (optional) format value.<br>
  390.      *                   If only one format argument, $msg is strings or array.<br>
  391.      *                   For multiple format arguments, $msg is array.
  392.      */
  393.     function printe ($format$msg ''{
  394.         return ePrint::ePrintf ($format$msg);
  395.     }
  396.     // }}}
  397.  
  398.     // {{{ function print_f ($file, $format, $msg = '')
  399.     /**
  400.      * Save a formatted string to file
  401.      *
  402.      * A newline is not automatically added to the end of the message string.
  403.      *
  404.      * @deprecated deprecated since 1.0.1. Use {@link ePrint::ePrintf()} or
  405.      *              {@link ePrint::lPrintf()} instead of this method
  406.      * @see    ePrint::ePrintf()
  407.      * @see    ePrint::lPrintf()
  408.      * @access  public
  409.      * @return  int     length of print string
  410.      * @param   string  $path   target file
  411.      * @param   string  $format same format of printf
  412.      * @param   mixed   (optional) format value.<br>
  413.      *                   If only one format argument, $msg is strings or array.<br>
  414.      *                   For multiple format arguments, $msg is array.
  415.      */
  416.     function print_f ($file$format$msg ''{
  417.         return ePrint::ePrintf ($format$msg3$file);
  418.     }
  419.     // }}}
  420.  
  421.     // {{{ function print_s ($msg, $width = 75, $indent = 0, $ul = '', $to_stderr = 0)
  422.     /**
  423.      * print with indent.
  424.      *
  425.      * @deprecated deprecated since 1.0.1. Use {@link ePrint::printi()} instead
  426.      *              of this method
  427.      * @see     ePrint::printi()
  428.      * @access  public
  429.      * @return  int     Length of print string. If on error, return false
  430.      * @param   mixed   output string
  431.      * @param   integer (optional) location of line brek. default 80
  432.      * @param   integer (optional) indent of each line
  433.      * @param   string  (optional) list itme
  434.      * @param   boolean (optional) set true, print stderr
  435.      */
  436.     function print_s ($msg$width 75$indent 0$ul ''$to_stderr 0{
  437.         return ePrint::printi ($msg$width$indent$ul($to_stderr === 0false true);
  438.     }
  439.     // }}}
  440.  
  441.     // {{{ function wordwrap ($msg, $width = 75, $break = "\n", $cut = 0)
  442.     /**
  443.      * Wraps a string to a given number of characters. Difference with wordwarp of
  444.      * PHP, wrapped line joins next line and rewraps.
  445.      *
  446.      * @deprecated deprecated since 1.0.1. Use {@link ePrint::wordwrap()} instead
  447.      *              of this method
  448.      * @see     ePrint::wordwrap()
  449.      * @access  public
  450.      * @return  strings 
  451.      * @param   string  input string
  452.      * @param   integer (optional) The column widht. Defaults to 75
  453.      * @param   string  (optional) The line is broken using the optional break parameter.
  454.      *                   Defaults to '\n'.
  455.      * @param   integer (optional) If the cut is set to TRUE, the string is always wrapped
  456.      *                   at or before the specified width. So if you have a word
  457.      *                   that is larger than the given width, it is broken apart.
  458.      *                   Seealso wordwrap of php
  459.      */
  460.     function wordwrap ($msg$width 75$break "\n"$cut 0{
  461.         return ePrint::wordwrap ($msg$width$break$cut);
  462.     }
  463.     // }}}
  464.  
  465.     // {{{ function file_nr ($f, $use_include_path = false, $resource = null)
  466.     /**
  467.      * Reads entire file into an array
  468.      * file_nr api runs same file function of php. But file_nr has
  469.      * no \r\n or \n character on array members.
  470.      *
  471.      * @deprecated deprecated since 1.0.1. Use {@link ePrint::file_nr()} instead
  472.      *              of this method
  473.      * @see     ePrint::file_nr()
  474.      * @access  public
  475.      * @return  array    Array or false if not found file path nor file resource.
  476.      * @param   string   file path
  477.      * @param   boolean  (optional) Search file path on include_path of php.
  478.      *                    Defaults is false.
  479.      * @param   resource (optional) already opend file description resource
  480.      *                    Defaults is null.
  481.      */
  482.     function file_nr ($f$use_include_path false$resource null{
  483.         return eFilesystem::file_nr ($f$use_include_path$resource);
  484.     }
  485.     // }}}
  486.  
  487.     // {{{ function getopt ($argc, $argv, $optstrs)
  488.     /**
  489.      * Wrapping o_getopt on Oops C library
  490.      * This class is supported alternative getopt function.
  491.      *
  492.      * @deprecated deprecated since 1.0.1. Use {@link oGetopt::exec()} instead
  493.      *              of this method
  494.      * @see     oGetopt::exec()
  495.      * @access public
  496.      * @return string return short option.<br>
  497.      *                 If return -1, end of getopt processing.
  498.      *                 If wrong option, print error message and return null
  499.      * @param  integer Number of command line arguments
  500.      * @param  array   Command line arguments
  501.      * @param  string  Option format. See also 'man 3 getopt'
  502.      */
  503.     function getopt ($argc$argv$optstrs{
  504.         global $optarg$optcmd$longopt;
  505.         global $optcno;
  506.  
  507.         if class_exists ('oGetopt') ) {
  508.             /**
  509.              * Dependency on {@link oGetopt 'pear.oops.org/oGetopt'}
  510.              * pear package over 1.0.0
  511.              */
  512.             require_once 'oGetopt.php';
  513.  
  514.             oGetopt::init ();
  515.             $optcno = -1;
  516.  
  517.             if is_object ($longopt) ) {
  518.                 $_longopt = (object) $longopt;
  519.                 unset ($longopt);
  520.                 $longopt $_longopt;
  521.             }
  522.  
  523.             oGetopt::$longopt &$longopt;
  524.             oGetopt::$optcno  &$optcno;
  525.             oGetopt::$optcmd  &$optcmd;
  526.             oGetopt::$optarg  &$optarg;
  527.         }
  528.  
  529.         $r oGetopt::exec ($argc$argv$optstrs);
  530.         if $r === false )
  531.             return -1;
  532.  
  533.         return $r;
  534.     }
  535.     // }}}
  536.  
  537.     // {{{ function manPath ($_name, $_path = '/usr/share/man', $_sec = 0)
  538.     /**
  539.      * Return man page file path with man page section and name
  540.      *
  541.      * The exmaple:
  542.      * {@example eSystem/test.php 170 2}
  543.      *
  544.      * @access public
  545.      * @return string Returns man page file path
  546.      * @param  string Name of man page for searching
  547.      * @param  string (optional) Base man page base path
  548.      * @param  integer (optional) Section of man page
  549.      */
  550.     function manPath ($_name$_path '/usr/share/man'$_sec 0{
  551.         $this->autoload ($this->man'man');
  552.         $this->man->tmpdir $this->tmpdir;
  553.         return $this->man->manPath ($_name$_path$_sec);
  554.     }
  555.     // }}}
  556.  
  557.     // {{{ function man ($_name, $_no, $_int = NULL, $__base = null, $_s = 0)
  558.     /**
  559.      * Return man page contents for human readable
  560.      *
  561.      * The exmaple:
  562.      * {@example eSystem/test.php 173 2}
  563.      *
  564.      * @access public
  565.      * @return string Returns man page file path
  566.      * @param  string  name of man page
  567.      * @param  int     Section of man page
  568.      * @param  string  (optional) L10n code for international man pages
  569.      * @param  string  (optional) Base man page base path
  570.      * @param  boolean (optional) Defaults to 0. Set true, even if result
  571.      *                  is array, force convert plain text strings.
  572.      */
  573.     function man ($_name$_no$_int NULL$__base null$_s false{
  574.         if extension_loaded ("zlib")) {
  575.             echo "Error: man function requires zlib extension!";
  576.             exit (1);
  577.         }
  578.  
  579.         $this->autoload ($this->man'man');
  580.         $this->man->tmpdir $this->tmpdir;
  581.         return $this->man->man ($_name$_no$_int$__base$_s);
  582.     }
  583.     // }}}
  584.  
  585.     // {{{ function __nocli ($n = '')
  586.     /**
  587.      * If call this method, exit when php sapi is not cli.
  588.      *
  589.      * @access private
  590.      * @return void 
  591.      * @param  string (optional) method name
  592.      */
  593.     private function __nocli ($n ''{
  594.         $method $n $n 'this';
  595.         if php_sapi_name (!= 'cli' {
  596.             echo "<script type=\"text/javascript\">\n" .
  597.                 "  alert('{$method} method only used on CLI mode');\n.
  598.                 "  history.back();\n" .
  599.                 "</script>\n";
  600.             exit;
  601.         }
  602.     }
  603.     // }}}
  604. }
  605.  
  606. /*
  607.  * Local variables:
  608.  * tab-width: 4
  609.  * c-basic-offset: 4
  610.  * End:
  611.  * vim600: noet sw=4 ts=4 fdm=marker
  612.  * vim<600: noet sw=4 ts=4
  613.  */
  614. ?>

Documentation generated on Tue, 14 May 2019 02:00:00 +0900 by phpDocumentor 1.4.4