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

Source for file Util.php

Documentation is available at Util.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: File :: Util                                                 |
  4. // +----------------------------------------------------------------------+
  5. // | This source file is subject to version 3.0 of the PHP license,       |
  6. // | that is available at http://www.php.net/license/3_0.txt              |
  7. // | If you did not receive a copy of the PHP license and are unable      |
  8. // | to obtain it through the world-wide-web, please send a note to       |
  9. // | license@php.net so we can mail you a copy immediately.               |
  10. // +----------------------------------------------------------------------+
  11. // | Copyright (c) 2005 Michael Wallner <mike@iworks.at>                  |
  12. // +----------------------------------------------------------------------+
  13. //
  14. // $Id: Util.php,v 1.11 2005/02/02 11:24:31 mike Exp $
  15.  
  16. /**#@+
  17.  * Sorting Constants
  18.  */
  19. define('FILE_SORT_NONE',    0);
  20. define('FILE_SORT_REVERSE'1);
  21. define('FILE_SORT_NAME',    2);
  22. define('FILE_SORT_SIZE',    4);
  23. define('FILE_SORT_DATE',    8);
  24. /**#@-*/
  25.  
  26. /**#@+
  27.  * Listing Constants
  28.  */
  29. define('FILE_LIST_FILES',   1);
  30. define('FILE_LIST_DIRS',    2);
  31. define('FILE_LIST_DOTS',    4);
  32. define('FILE_LIST_ALL',     FILE_LIST_FILES | FILE_LIST_DIRS | FILE_LIST_DOTS);
  33. /**#@-*/
  34.  
  35. /**
  36.  * @ignore
  37.  */
  38. define('FILE_WIN32'defined('OS_WINDOWS'? OS_WINDOWS : !strncasecmp(PHP_OS'win'3));
  39.  
  40. /** 
  41.  * File_Util
  42.  *
  43.  * File and directory utility functions.
  44.  */
  45. class File_Util
  46. {
  47.     /**
  48.      * Returns a string path built from the array $pathParts. Where a join
  49.      * occurs multiple separators are removed. Joins using the optional
  50.      * separator, defaulting to the PHP DIRECTORY_SEPARATOR constant.
  51.      * 
  52.      * @static
  53.      * @access  public
  54.      * @param   array   $parts Array containing the parts to be joined
  55.      * @param   string  $separator The directory seperator
  56.      */
  57.     function buildPath($parts$separator = DIRECTORY_SEPARATOR)
  58.     {
  59.         /*
  60.          * @FIXXME: maybe better use foreach
  61.          */
  62.         
  63.         for ($i = 0$c count($parts)$i $c$i++{
  64.             if (    !strlen($parts[$i]|| 
  65.                     preg_match('/^'preg_quote($separator'/'.'+$/'$parts[$i])) {
  66.                 unset($parts[$i]);
  67.             elseif (0 == $i{
  68.                 $parts[$irtrim($parts[$i]$separator);
  69.             elseif ($c - 1 == $i{
  70.                 $parts[$iltrim($parts[$i]$separator);
  71.             else {
  72.                 $parts[$itrim($parts[$i]$separator);
  73.             
  74.         
  75.         return implode($separator$parts);
  76.     
  77.  
  78.     /**
  79.      * Returns a path without leading / or C:\. If this is not
  80.      * present the path is returned as is.
  81.      * 
  82.      * @static
  83.      * @access  public
  84.      * @param   string  $path The path to be processed
  85.      * @return  string  The processed path or the path as is
  86.      */
  87.     function skipRoot($path)
  88.     {
  89.         if (File_Util::isAbsolute($path)) {
  90.             if (FILE_WIN32{
  91.                 return substr($path$path{3== '\\' ? 4 : 3);
  92.             }
  93.             return ltrim($path'/');
  94.         }
  95.         return $path;
  96.     
  97.  
  98.     /**
  99.      * Returns the temp directory according to either the TMP, TMPDIR, or
  100.      * TEMP env variables. If these are not set it will also check for the
  101.      * existence of /tmp, %WINDIR%\temp
  102.      * 
  103.      * @static
  104.      * @access  public
  105.      * @return  string  The system tmp directory
  106.      */
  107.     function tmpDir()
  108.     {
  109.         if (FILE_WIN32{
  110.             if (isset($_ENV['TEMP'])) {
  111.                 return $_ENV['TEMP'];
  112.             }
  113.             if (isset($_ENV['TMP'])) {
  114.                 return $_ENV['TMP'];
  115.             }
  116.             if (isset($_ENV['windir'])) {
  117.                 return $_ENV['windir''\\temp';
  118.             }
  119.             if (isset($_ENV['SystemRoot'])) {
  120.                 return $_ENV['SystemRoot''\\temp';
  121.             }
  122.             if (isset($_SERVER['TEMP'])) {
  123.                 return $_SERVER['TEMP'];
  124.             }
  125.             if (isset($_SERVER['TMP'])) {
  126.                 return $_SERVER['TMP'];
  127.             }
  128.             if (isset($_SERVER['windir'])) {
  129.                 return $_SERVER['windir''\\temp';
  130.             }
  131.             if (isset($_SERVER['SystemRoot'])) {
  132.                 return $_SERVER['SystemRoot''\\temp';
  133.             }
  134.             return '\temp';
  135.         }
  136.         if (isset($_ENV['TMPDIR'])) {
  137.             return $_ENV['TMPDIR'];
  138.         }
  139.         if (isset($_SERVER['TMPDIR'])) {
  140.             return $_SERVER['TMPDIR'];
  141.         }
  142.         return '/tmp';
  143.     }
  144.  
  145.     /**
  146.      * Returns a temporary filename using tempnam() and File::tmpDir().
  147.      * 
  148.      * @static
  149.      * @access  public
  150.      * @param   string  $dirname Optional directory name for the tmp file
  151.      * @return  string  Filename and path of the tmp file
  152.      */
  153.     function tmpFile($dirname = null)
  154.     {
  155.         if (!isset($dirname)) {
  156.             $dirname = File_Util::tmpDir();
  157.         
  158.         return tempnam($dirname'temp.');
  159.     
  160.  
  161.     /**
  162.      * Returns boolean based on whether given path is absolute or not.
  163.      * 
  164.      * @static
  165.      * @access  public
  166.      * @param   string  $path Given path
  167.      * @return  boolean True if the path is absolute, false if it is not
  168.      */
  169.     function isAbsolute($path)
  170.     {
  171.         if (preg_match('/\.\./'$path)) {
  172.             return false;
  173.         
  174.         if (FILE_WIN32{
  175.             return preg_match('/^[a-zA-Z]:(\\\|\/)/'$path);
  176.         }
  177.         return ($path{0== '/'|| ($path{0== '~');
  178.     
  179.  
  180.     /**
  181.      * Get path relative to another path
  182.      *
  183.      * @static
  184.      * @access  public
  185.      * @return  string 
  186.      * @param   string  $path 
  187.      * @param   string  $root 
  188.      * @param   string  $separator 
  189.      */
  190.     function relativePath($path$root$separator = DIRECTORY_SEPARATOR)
  191.     {
  192.         $path = File_Util::realpath($path$separator);
  193.         $root = File_Util::realpath($root$separator);
  194.         $dirs explode($separator$path);
  195.         $comp explode($separator$root);
  196.         
  197.         if (FILE_WIN32{
  198.             if (strcasecmp($dirs[0]$comp[0])) {
  199.                 return $path;
  200.             }
  201.             unset($dirs[0]$comp[0]);
  202.         }
  203.         
  204.         foreach ($comp as $i => $part{
  205.             if (isset($dirs[$i]&& $part == $dirs[$i]{
  206.                 unset($dirs[$i]$comp[$i]);
  207.             else {
  208.                 break;
  209.             }
  210.         }
  211.          
  212.         return str_repeat('..' $separatorcount($comp)) implode($separator$dirs);
  213.     }
  214.  
  215.     /**
  216.      * Get real path (works with non-existant paths)
  217.      *
  218.      * @static
  219.      * @access  public
  220.      * @return  string 
  221.      * @param   string  $path 
  222.      * @param   string  $separator 
  223.      */
  224.     function realPath($path$separator = DIRECTORY_SEPARATOR)
  225.     {
  226.         if (!strlen($path)) {
  227.             return $separator;
  228.         }
  229.         
  230.         $drive '';
  231.         if (FILE_WIN32{
  232.             $path preg_replace('/[\\\\\/]/'$separator$path);
  233.             if (preg_match('/([a-zA-Z]\:)(.*)/'$path$matches)) {
  234.                 $drive $matches[1];
  235.                 $path  $matches[2];
  236.             else {
  237.                 $cwd   getcwd();
  238.                 $drive substr($cwd02);
  239.                 if ($path{0!== $separator{0}{
  240.                     $path  substr($cwd3$separator $path;
  241.                 }
  242.             }
  243.         elseif ($path{0!== $separator{
  244.             $path getcwd($separator $path;
  245.         }
  246.         
  247.         $dirStack = array();
  248.         foreach (explode($separator$pathas $dir{
  249.             if (strlen($dir&& $dir !== '.'{
  250.                 if ($dir == '..'{
  251.                     array_pop($dirStack);
  252.                 else {
  253.                     $dirStack[$dir;
  254.                 }
  255.             }
  256.         }
  257.         
  258.         return $drive $separator implode($separator$dirStack);
  259.     }
  260.  
  261.     /**
  262.      * Check whether path is in root path
  263.      *
  264.      * @static
  265.      * @access  public
  266.      * @return  bool 
  267.      * @param   string  $path 
  268.      * @param   string  $root 
  269.      */
  270.     function pathInRoot($path$root)
  271.     {
  272.         static $realPaths = array();
  273.         
  274.         if (!isset($realPaths[$root])) {
  275.             $realPaths[$root= File_Util::realPath($root);
  276.         }
  277.         
  278.         return false !== strstr(File_Util::realPath($path)$realPaths[$root]);
  279.     }
  280.  
  281.     /**
  282.      * List Directory
  283.      * 
  284.      * @static
  285.      * @access  public
  286.      * @return  array 
  287.      * @param   string  $path 
  288.      * @param   int     $list 
  289.      * @param   int     $sort 
  290.      * @param   mixed   $cb 
  291.      */
  292.     function listDir($path$list = FILE_LIST_ALL$sort = FILE_SORT_NONE$cb = null)
  293.     {
  294.         if (!strlen($path|| !is_dir($path)) {
  295.             return null;
  296.         }
  297.         
  298.         $entries = array();
  299.         for ($dir dir($path); false !== $entry $dir->read(){
  300.             if ($list FILE_LIST_DOTS || $entry{0!== '.'{
  301.                 $isRef ($entry === '.' || $entry === '..');
  302.                 $isDir $isRef || is_dir($path .'/'$entry);
  303.                 if (    ((!$isDir && $list FILE_LIST_FILES)   ||
  304.                          ($isDir  && $list FILE_LIST_DIRS))   &&
  305.                         (!is_callable($cb|| call_user_func($cb$entry))) {
  306.                     $entries[= (object) array(
  307.                         'name'  => $entry,
  308.                         'size'  => $isDir ? null : filesize($path .'/'$entry),
  309.                         'date'  => filemtime($path .'/'$entry),
  310.                     );
  311.                 }
  312.             }
  313.         }
  314.         $dir->close();
  315.         
  316.         if ($sort{
  317.             $entries = File_Util::sortFiles($entries$sort);
  318.         }
  319.         
  320.         return $entries;
  321.     }
  322.     
  323.     /**
  324.      * Sort Files
  325.      * 
  326.      * @static
  327.      * @access  public
  328.      * @return  array 
  329.      * @param   array   $files 
  330.      * @param   int     $sort 
  331.      */
  332.     function sortFiles($files$sort)
  333.     {
  334.         if (!$files{
  335.             return array();
  336.         }
  337.         
  338.         if (!$sort{
  339.             return $files;
  340.         }
  341.         
  342.         if ($sort == 1{
  343.             return array_reverse($files);
  344.         }
  345.         
  346.         $sortFlags = array(
  347.             FILE_SORT_NAME => SORT_STRING
  348.             FILE_SORT_DATE => SORT_NUMERIC
  349.             FILE_SORT_SIZE => SORT_NUMERIC,
  350.         );
  351.         
  352.         foreach ($files as $file{
  353.             $names[$file['name'];
  354.             $sizes[$file['size'];
  355.             $dates[$file['date'];
  356.         }
  357.     
  358.         if ($sort FILE_SORT_NAME{
  359.             $r &$names;
  360.         elseif ($sort FILE_SORT_DATE{
  361.             $r &$dates;
  362.         elseif ($sort FILE_SORT_SIZE{
  363.             $r &$sizes;
  364.         }
  365.         
  366.         if ($sort FILE_SORT_REVERSE{
  367.             arsort($r$sortFlags[$sort ~1]);
  368.         else {
  369.             asort($r$sortFlags[$sort]);
  370.         }
  371.     
  372.         $result = array();
  373.         foreach ($r as $i => $f{
  374.             $result[$files[$i];
  375.         }
  376.     
  377.         return $result;
  378.     }
  379. }
  380.  
  381. ?>

Documentation generated on Mon, 11 Mar 2019 14:22:48 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.