[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/misc/ -> glob.class.php (source)

   1  <?php
   2  
   3      /**
   4       * \defgroup Misc
   5       *
   6       * Miscellaneous classes that did not fit anywhere
   7       */
   8  
   9  
  10      
  11  
  12      /**
  13       * \ingroup Misc
  14       * 
  15       * Alternative implementation of the glob() function, since the latter is only
  16       * available in php versions 4.3 or higher and many many hosts have not updated
  17       * yet.
  18       * Original glob function: http://www.php.net/manual/en/function.glob.php.
  19       * Original fnmatch function: http://www.php.net/manual/en/function.fnmatch.php.
  20       *
  21       * The class is capable of detecting the version of php and using the native (and probably
  22       * faster) version instead of the custom one.
  23       */
  24      class Glob  
  25      {
  26  
  27          /**
  28           * This function checks wether we're running a version of php at least or newer than
  29           * 4.3. If its newer, then we will use the native version of glob otherwise we will use
  30           * our own version. The order of the parameters is <b>not</b> the same as the native version
  31           * of glob, but they will be converted. The <i>flags</i> parameter is not used when
  32           * using the custom version of glob.
  33           *
  34           * @param folder The folder where would like to search for files. This function is <b>not</b>
  35           * recursive.
  36           * @param pattern The shell pattern that will match the files we are searching for.
  37           * @param flags This parameter is only used when using the native version of glob. For possible
  38           * values of this parameter, please check the glob function page.
  39           * @return Returns an array of the files that match the given pattern in the given
  40           * folder or false if there was an error.
  41           * @static
  42           */
  43          function Glob( $folder = ".", $pattern = "*", $flags = 0 )
  44          {
  45              if( function_exists("glob")) {
  46                  // call the native glob function with parameters
  47                  $fileName = $folder;
  48                  if( substr($fileName, -1) != "/" )
  49                      $fileName .= "/";
  50                  $fileName .= $pattern;
  51  
  52                  return glob( $fileName, $flags );
  53              }
  54              else {
  55                  // call our own implementation
  56                  return Glob::myGlob( $folder, $pattern );
  57              }
  58          }
  59  
  60          /**
  61           * Front-end function that does the same as the glob function above but this time with the fnmnatch version.
  62           * Checks the php version and if it is at least or greater than 4.3, then we will use
  63           * the native and faster version of fnmatch or otherwise we will fall back to using our
  64           * own custom version.
  65           *
  66           * @param pattern The shell pattern.
  67           * @param file The filename we would like to match.
  68           * @return True if the file matches the pattern or false if not.
  69           * @static
  70           */
  71          function fnmatch( $pattern, $file )
  72          {
  73              if( function_exists("fnmatch")) {
  74                  // use the native fnmatch version
  75                  return fnmatch( $pattern, $file );
  76              }
  77              else {
  78                  // otherwise, use our own
  79                  return Glob::myFnmatch( $pattern, $file );
  80              }
  81          }
  82  
  83          /**
  84           * Our own implementation of the glob function for those sites which run a version
  85           * of php lower than 4.3. For more information on the function glob:
  86           * http://www.php.net/manual/en/function.glob.php
  87           *
  88           * Returns an array with all the files and subdirs that match the given shell expression.
  89           * @param folder Where to start searching.
  90           * @param pattern A shell expression including wildcards '*' and '?' defining which
  91           * files will match and which will not.
  92           * @return An array with the matching files and false if error.
  93           */
  94          function myGlob( $folder = ".", $pattern = "*" )
  95          {
  96              // Note that !== did not exist until 4.0.0-RC2
  97              // what if the temp folder is deleted?  or $folder is not exist? then will raise
  98              // ugly error or warning message. so first check if $folder readable
  99              if( !file_exists( $folder )) return Array();
 100  
 101              if ( $handle = opendir( $folder )) {
 102                  $files = Array();
 103  
 104                  while (false !== ($file = readdir($handle))) {
 105                      if( $file !="." && $file != ".." )    // ignore '.' and '..'
 106                          if( Glob::fnmatch($pattern,$file)) {
 107                              if( $folder[strlen($folder)-1] != "/")
 108                                  $filePath = $folder."/".$file;
 109                              else
 110                                  $filePath = $folder.$file;
 111                              array_push( $files, $filePath );
 112  
 113                          }
 114                  }
 115  
 116                  closedir($handle);
 117              }
 118              else
 119                  return Array();
 120  
 121              return $files;
 122          }
 123  
 124          /**
 125           * Our own equivalent of fnmatch that is only available in php 4.3.x.
 126           *
 127           * Based on a user-contributed code for the fnmatch php function here:
 128           * http://www.php.net/manual/en/function.fnmatch.php
 129             *
 130           * @static
 131           */
 132          function myFnmatch( $pattern, $file )
 133          {
 134              for($i=0,$len = strlen($pattern); $i<$len; $i++) {
 135                  if($pattern[$i] == "*") {
 136                      for($c=$i; $c<max(strlen($pattern), strlen($file)); $c++) {
 137                          if(Glob::myFnmatch(substr($pattern, $i+1), substr($file, $c))) {
 138                              return true;
 139                          }
 140                      }
 141                      return false;
 142                  }
 143                  if($pattern[$i] == "[") {
 144                      $letter_set = array();
 145                      for($c=$i+1, $len2 = strlen($pattern); $c<$len2; $c++) {
 146                          if($pattern[$c] != "]") {
 147                              array_push($letter_set, $pattern[$c]);
 148                          }
 149                          else
 150                              break;
 151                      }
 152                      foreach ($letter_set as $letter) {
 153                          if(Glob::myFnmatch($letter.substr($pattern, $c+1), substr($file, $i))) {
 154                              return true;
 155                          }
 156                      }
 157                      return false;
 158                 }
 159                 if($pattern[$i] == "?") {
 160                        continue;
 161                }
 162                if($pattern[$i] != $file[$i]) {
 163                    return false;
 164                }
 165           }
 166           return true;
 167         }
 168      }
 169  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics