CdataStrategy.php
7 years ago
CdataStrategyAlways.php
7 years ago
CdataStrategyFactory.php
7 years ago
CdataStrategyIllegalCharacters.php
7 years ago
CdataStrategyIllegalCharactersHtmlEntities.php
7 years ago
CdataStrategyNever.php
7 years ago
XMLWriter.php
7 years ago
chunk.php
7 years ago
config.php
7 years ago
download.php
7 years ago
handler.php
7 years ago
helper.php
7 years ago
input.php
7 years ago
installer.php
7 years ago
session.php
7 years ago
wpallimport.php
7 years ago
zip.php
7 years ago
helper.php
140 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 PMXE_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 | |
| 40 | if (($dir = @opendir($path . '/')) !== false or ($dir = @opendir($path)) !== false) { |
| 41 | $glob = array(); |
| 42 | while(($file = readdir($dir)) !== false) { |
| 43 | // Recurse subdirectories (self::GLOB_RECURSE) |
| 44 | if (($flags & self::GLOB_RECURSE) && is_dir($path . '/' . $file) && ( ! in_array($file, array('.', '..')))) { |
| 45 | $glob = array_merge($glob, self::array_prepend(self::safe_glob($path . '/' . $file . '/' . $mask, $flags), ($flags & self::GLOB_PATH ? '' : $file . '/'))); |
| 46 | } |
| 47 | // Match file mask |
| 48 | if (self::fnmatch($mask, $file, FNM_CASEFOLD)) { |
| 49 | if ((( ! ($flags & self::GLOB_ONLYDIR)) || is_dir("$path/$file")) |
| 50 | && (( ! ($flags & self::GLOB_NODIR)) || ( ! is_dir($path . '/' . $file))) |
| 51 | && (( ! ($flags & self::GLOB_NODOTS)) || ( ! in_array($file, array('.', '..')))) |
| 52 | ) { |
| 53 | $glob[] = ($flags & self::GLOB_PATH ? $path . '/' : '') . $file . ($flags & self::GLOB_MARK ? '/' : ''); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | closedir($dir); |
| 58 | if ( ! ($flags & self::GLOB_NOSORT)) sort($glob); |
| 59 | return $glob; |
| 60 | } else { |
| 61 | return (strpos($pattern, "*") === false) ? array($pattern) : false; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Prepends $string to each element of $array |
| 67 | * If $deep is true, will indeed also apply to sub-arrays |
| 68 | * @author BigueNique AT yahoo DOT ca |
| 69 | * @since 080324 |
| 70 | */ |
| 71 | public static function array_prepend($array, $string, $deep=false) { |
| 72 | if(empty($array)||empty($string)) { |
| 73 | return $array; |
| 74 | } |
| 75 | foreach ($array as $key => $element) { |
| 76 | if (is_array($element)) { |
| 77 | if ($deep) { |
| 78 | $array[$key] = self::array_prepend($element,$string,$deep); |
| 79 | } else { |
| 80 | trigger_error(__METHOD__ . ': array element', E_USER_WARNING); |
| 81 | } |
| 82 | } else { |
| 83 | $array[$key] = $string.$element; |
| 84 | } |
| 85 | } |
| 86 | return $array; |
| 87 | |
| 88 | } |
| 89 | |
| 90 | const FNM_PATHNAME = 1; |
| 91 | const FNM_NOESCAPE = 2; |
| 92 | const FNM_PERIOD = 4; |
| 93 | const FNM_CASEFOLD = 16; |
| 94 | |
| 95 | /** |
| 96 | * non-POSIX complient remplacement for the fnmatch |
| 97 | */ |
| 98 | public static function fnmatch($pattern, $string, $flags = 0) { |
| 99 | |
| 100 | $modifiers = null; |
| 101 | $transforms = array( |
| 102 | '\*' => '.*', |
| 103 | '\?' => '.', |
| 104 | '\[\!' => '[^', |
| 105 | '\[' => '[', |
| 106 | '\]' => ']', |
| 107 | '\.' => '\.', |
| 108 | '\\' => '\\\\', |
| 109 | '\-' => '-', |
| 110 | ); |
| 111 | |
| 112 | // Forward slash in string must be in pattern: |
| 113 | if ($flags & FNM_PATHNAME) { |
| 114 | $transforms['\*'] = '[^/]*'; |
| 115 | } |
| 116 | |
| 117 | // Back slash should not be escaped: |
| 118 | if ($flags & FNM_NOESCAPE) { |
| 119 | unset($transforms['\\']); |
| 120 | } |
| 121 | |
| 122 | // Perform case insensitive match: |
| 123 | if ($flags & FNM_CASEFOLD) { |
| 124 | $modifiers .= 'i'; |
| 125 | } |
| 126 | |
| 127 | // Period at start must be the same as pattern: |
| 128 | if ($flags & FNM_PERIOD) { |
| 129 | if (strpos($string, '.') === 0 && strpos($pattern, '.') !== 0) return false; |
| 130 | } |
| 131 | |
| 132 | $pattern = '#^' |
| 133 | .strtr(preg_quote($pattern, '#'), $transforms) |
| 134 | .'$#' |
| 135 | .$modifiers; |
| 136 | |
| 137 | return (boolean)preg_match($pattern, $string); |
| 138 | } |
| 139 | } |
| 140 |