phpDocumentor default
[ class tree: default ] [ index: default ] [ all elements ]

Source for file safe_glob.php

Documentation is available at safe_glob.php

  1. <?php
  2.  
  3. /**#@+
  4.  * Extra GLOB constant for safe_glob()
  5.  */
  6. define('GLOB_NODIR',256);
  7. define('GLOB_PATH',512);
  8. define('GLOB_NODOTS',1024);
  9. define('GLOB_RECURSE',2048);
  10. /**#@-*/
  11.  
  12. /**
  13.  * A safe empowered glob().
  14.  *
  15.  * Function glob() is prohibited on some server (probably in safe mode)
  16.  * (Message "Warning: glob() has been disabled for security reasons in
  17.  * (script) on line (line)") for security reasons as stated on:
  18.  * http://seclists.org/fulldisclosure/2005/Sep/0001.html
  19.  *
  20.  * safe_glob() intends to replace glob() using readdir() & fnmatch() instead.
  21.  * Supported flags: GLOB_MARK, GLOB_NOSORT, GLOB_ONLYDIR
  22.  * Additional flags: GLOB_NODIR, GLOB_PATH, GLOB_NODOTS, GLOB_RECURSE
  23.  * (not original glob() flags)
  24.  * @author BigueNique AT yahoo DOT ca
  25.  * @updates
  26.  *  - 080324 Added support for additional flags: GLOB_NODIR, GLOB_PATH,
  27.  *    GLOB_NODOTS, GLOB_RECURSE
  28.  * @deprecated
  29.  */
  30. function safe_glob($pattern$flags=0{
  31.  
  32.     $mask "*";
  33.     if(is_dir($pattern)) $path $pattern;
  34.     else
  35.     {
  36.         $split=explode('/',str_replace('\\','/',$pattern));
  37.         $mask=array_pop($split);
  38.         $path=implode('/',$split);
  39.     }
  40.  
  41.     if (($dir=opendir($path))!==false{
  42.         $glob=array();
  43.  
  44.         while(($file=readdir($dir))!==false{
  45.  
  46.             $chars str_split($file);
  47.             
  48.             if($chars[0!= "."  && !in_array($file,array('.','..'))) {
  49.  
  50.                 // Recurse subdirectories (GLOB_RECURSE)
  51.                 if( ($flags&GLOB_RECURSE&& is_dir($path.'/'.$file)){
  52.                     
  53.                     $glob2 safe_glob($path.'/'.$file.'/'.$mask$flags);
  54.  
  55.                     if(count($glob21){
  56.                         $glob[($flags&GLOB_PATH?$path.'/':''$file ($flags&GLOB_MARK?'/':'');
  57.                         $glob array_merge($glob$glob2);
  58.                     }
  59.                 }
  60.  
  61.                 // Match file mask
  62.                 if (fnmatch($mask,$file)) {
  63.                     if (((!($flags&GLOB_ONLYDIR)) || is_dir("$path/$file"))
  64.                       && ( (!($flags&GLOB_NODIR)) || (!is_dir($path.'/'.$file)) )
  65.                       && ( (!($flags&GLOB_NODOTS)) || (!in_array($file,array('.','..')))))
  66.                         $glob[($flags&GLOB_PATH?$path.'/':''$file ($flags&GLOB_MARK?'/':'');
  67.                 }
  68.             }
  69.         }
  70.         closedir($dir);
  71.         if (!($flags&GLOB_NOSORT)) sort($glob);
  72.         return $glob;
  73.     else {
  74.         return false;
  75.     }
  76. }
  77.  
  78. ?>

Documentation generated on Mon, 13 Aug 2012 23:01:11 +0200 by phpDocumentor 1.4.4