note 97444 added to function.glob
| From: | BigueNique at yahoo dot ca | Date: | Tue, 20 Apr 2010 07:15:47 +0000 |
| Subject: | note 97444 added to function.glob | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168439@lists.php.net to get a copy of this message | ||
Since I discovered my safe_glob() implementation has been widely used by other folks and it has
evolved quite a bit since the time I first posted it, here is an updated version. It has proven
quite handy and robust on my side. That being said, use it at your own risk!
I added a few extra option constants that can make some common operations a breeze. For the rest,
the code speaks for itself.
/**#@+
* Extra GLOB constant for safe_glob()
*/
define('GLOB_NODIR',256);
define('GLOB_PATH',512);
define('GLOB_NODOTS',1024);
define('GLOB_RECURSE',2048);
/**#@-*/
/**
* A safe empowered glob().
*
* Function glob() is prohibited on some server (probably in safe mode)
* (Message "Warning: glob() has been disabled for security reasons in
* (script) on line (line)") for security reasons as stated on:
* http://seclists.org/fulldisclosure/2005/Sep/0001.html
*
* safe_glob() intends to replace glob() using readdir() & fnmatch() instead.
* Supported flags: GLOB_MARK, GLOB_NOSORT, GLOB_ONLYDIR
* Additional flags: GLOB_NODIR, GLOB_PATH, GLOB_NODOTS, GLOB_RECURSE
* (not original glob() flags)
* @author BigueNique AT yahoo DOT ca
* @updates
* - 080324 Added support for additional flags: GLOB_NODIR, GLOB_PATH,
* GLOB_NODOTS, GLOB_RECURSE
*/
function safe_glob($pattern, $flags=0) {
$split=explode('/',str_replace('\\','/',$pattern));
$mask=array_pop($split);
$path=implode('/',$split);
if (($dir=opendir($path))!==false) {
$glob=array();
while(($file=readdir($dir))!==false) {
// Recurse subdirectories (GLOB_RECURSE)
if( ($flags&GLOB_RECURSE) && is_dir($file) &&
(!in_array($file,array('.','..'))) )
$glob = array_merge($glob,
array_prepend(safe_glob($path.'/'.$file.'/'.$mask, $flags),
($flags&GLOB_PATH?'':$file.'/')));
// Match file mask
if (fnmatch($mask,$file)) {
if ( ( (!($flags&GLOB_ONLYDIR)) || is_dir("$path/$file") )
&& ( (!($flags&GLOB_NODIR)) || (!is_dir($path.'/'.$file)) )
&& ( (!($flags&GLOB_NODOTS)) ||
(!in_array($file,array('.','..'))) ) )
$glob[] = ($flags&GLOB_PATH?$path.'/':'') . $file .
($flags&GLOB_MARK?'/':'');
}
}
closedir($dir);
if (!($flags&GLOB_NOSORT)) sort($glob);
return $glob;
} else {
return false;
}
}
/**
* A better "fnmatch" alternative for windows that converts a fnmatch
* pattern into a preg one. It should work on PHP >= 4.0.0.
* @author soywiz at php dot net
* @since 17-Jul-2006 10:12
*/
if (!function_exists('fnmatch')) {
function fnmatch($pattern, $string) {
return @preg_match('/^' . strtr(addcslashes($pattern,
'\\.+^$(){}=!<>|'), array('*' => '.*', '?' =>
'.?')) . '$/i', $string);
}
}
----
Server IP: 69.147.83.197
Probable Submitter: 216.239.85.169
----
Manual Page -- http://www.php.net/manual/en/function.glob.php
Edit -- https://master.php.net/note/edit/97444
Del: integrated -- https://master.php.net/note/delete/97444/integrated
Del: useless -- https://master.php.net/note/delete/97444/useless
Del: bad code -- https://master.php.net/note/delete/97444/bad+code
Del: spam -- https://master.php.net/note/delete/97444/spam
Del: non-english -- https://master.php.net/note/delete/97444/non-english
Del: in docs -- https://master.php.net/note/delete/97444/in+docs
Del: other reasons-- https://master.php.net/note/delete/97444
Reject -- https://master.php.net/note/reject/97444
Search -- https://master.php.net/manage/user-notes.php