helper.php
137 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Helper class which defnes a namespace for some commonly used functions |
| 4 | * |
| 5 | * @author Pavel Kulbakin <p.kulbakin@gmail.com> |
| 6 | */ |
| 7 | class PMXI_Helper { |
| 8 | const GLOB_MARK = 1; |
| 9 | const GLOB_NOSORT = 2; |
| 10 | const GLOB_ONLYDIR = 4; |
| 11 | |
| 12 | const GLOB_NODIR = 256; |
| 13 | const GLOB_PATH = 512; |
| 14 | const GLOB_NODOTS = 1024; |
| 15 | const GLOB_RECURSE = 2048; |
| 16 | |
| 17 | /** |
| 18 | * A safe empowered glob(). |
| 19 | * |
| 20 | * Function glob() is prohibited on some server (probably in safe mode) |
| 21 | * (Message "Warning: glob() has been disabled for security reasons in |
| 22 | * (script) on line (line)") for security reasons as stated on: |
| 23 | * http://seclists.org/fulldisclosure/2005/Sep/0001.html |
| 24 | * |
| 25 | * safe_glob() intends to replace glob() using readdir() & fnmatch() instead. |
| 26 | * Supported flags: self::GLOB_MARK, self::GLOB_NOSORT, self::GLOB_ONLYDIR |
| 27 | * Additional flags: self::GLOB_NODIR, self::GLOB_PATH, self::GLOB_NODOTS, self::GLOB_RECURSE |
| 28 | * (not original glob() flags) |
| 29 | * @author BigueNique AT yahoo DOT ca |
| 30 | * @updates |
| 31 | * - 080324 Added support for additional flags: self::GLOB_NODIR, self::GLOB_PATH, |
| 32 | * self::GLOB_NODOTS, self::GLOB_RECURSE |
| 33 | * - 100607 Recurse is_dir check fixed by Pavel Kulbakin <p.kulbakin@gmail.com> |
| 34 | */ |
| 35 | public static function safe_glob($pattern, $flags=0) { |
| 36 | $split = explode('/', str_replace('\\', '/', $pattern)); |
| 37 | $mask = array_pop($split); |
| 38 | $path = implode('/', $split); |
| 39 | if (($dir = opendir($path)) !== false) { |
| 40 | $glob = array(); |
| 41 | while(($file = readdir($dir)) !== false) { |
| 42 | // Recurse subdirectories (self::GLOB_RECURSE) |
| 43 | if (($flags & self::GLOB_RECURSE) && is_dir($path . '/' . $file) && ( ! in_array($file, array('.', '..')))) { |
| 44 | $glob = array_merge($glob, self::array_prepend(self::safe_glob($path . '/' . $file . '/' . $mask, $flags), ($flags & self::GLOB_PATH ? '' : $file . '/'))); |
| 45 | } |
| 46 | // Match file mask |
| 47 | if (self::fnmatch($mask, $file)) { |
| 48 | if ((( ! ($flags & self::GLOB_ONLYDIR)) || is_dir("$path/$file")) |
| 49 | && (( ! ($flags & self::GLOB_NODIR)) || ( ! is_dir($path . '/' . $file))) |
| 50 | && (( ! ($flags & self::GLOB_NODOTS)) || ( ! in_array($file, array('.', '..')))) |
| 51 | ) { |
| 52 | $glob[] = ($flags & self::GLOB_PATH ? $path . '/' : '') . $file . ($flags & self::GLOB_MARK ? '/' : ''); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | closedir($dir); |
| 57 | if ( ! ($flags & self::GLOB_NOSORT)) sort($glob); |
| 58 | return $glob; |
| 59 | } else { |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Prepends $string to each element of $array |
| 66 | * If $deep is true, will indeed also apply to sub-arrays |
| 67 | * @author BigueNique AT yahoo DOT ca |
| 68 | * @since 080324 |
| 69 | */ |
| 70 | public static function array_prepend($array, $string, $deep=false) { |
| 71 | if(empty($array)||empty($string)) { |
| 72 | return $array; |
| 73 | } |
| 74 | foreach ($array as $key => $element) { |
| 75 | if (is_array($element)) { |
| 76 | if ($deep) { |
| 77 | $array[$key] = self::array_prepend($element,$string,$deep); |
| 78 | } else { |
| 79 | trigger_error(__METHOD__ . ': array element', E_USER_WARNING); |
| 80 | } |
| 81 | } else { |
| 82 | $array[$key] = $string.$element; |
| 83 | } |
| 84 | } |
| 85 | return $array; |
| 86 | |
| 87 | } |
| 88 | |
| 89 | const FNM_PATHNAME = 1; |
| 90 | const FNM_NOESCAPE = 2; |
| 91 | const FNM_PERIOD = 4; |
| 92 | const FNM_CASEFOLD = 16; |
| 93 | |
| 94 | /** |
| 95 | * non-POSIX complient remplacement for the fnmatch |
| 96 | */ |
| 97 | public static function fnmatch($pattern, $string, $flags = 0) { |
| 98 | $modifiers = null; |
| 99 | $transforms = array( |
| 100 | '\*' => '.*', |
| 101 | '\?' => '.', |
| 102 | '\[\!' => '[^', |
| 103 | '\[' => '[', |
| 104 | '\]' => ']', |
| 105 | '\.' => '\.', |
| 106 | '\\' => '\\\\' |
| 107 | ); |
| 108 | |
| 109 | // Forward slash in string must be in pattern: |
| 110 | if ($flags & FNM_PATHNAME) { |
| 111 | $transforms['\*'] = '[^/]*'; |
| 112 | } |
| 113 | |
| 114 | // Back slash should not be escaped: |
| 115 | if ($flags & FNM_NOESCAPE) { |
| 116 | unset($transforms['\\']); |
| 117 | } |
| 118 | |
| 119 | // Perform case insensitive match: |
| 120 | if ($flags & FNM_CASEFOLD) { |
| 121 | $modifiers .= 'i'; |
| 122 | } |
| 123 | |
| 124 | // Period at start must be the same as pattern: |
| 125 | if ($flags & FNM_PERIOD) { |
| 126 | if (strpos($string, '.') === 0 && strpos($pattern, '.') !== 0) return false; |
| 127 | } |
| 128 | |
| 129 | $pattern = '#^' |
| 130 | .strtr(preg_quote($pattern, '#'), $transforms) |
| 131 | .'$#' |
| 132 | .$modifiers; |
| 133 | |
| 134 | return (boolean)preg_match($pattern, $string); |
| 135 | } |
| 136 | } |
| 137 |