partner-discount-sdk
3 weeks ago
CdataStrategy.php
3 weeks ago
CdataStrategyAlways.php
3 weeks ago
CdataStrategyFactory.php
3 weeks ago
CdataStrategyIllegalCharacters.php
3 weeks ago
CdataStrategyIllegalCharactersHtmlEntities.php
3 weeks ago
CdataStrategyNever.php
3 weeks ago
XMLWriter.php
3 weeks ago
chunk.php
3 weeks ago
config.php
3 years ago
download.php
3 weeks ago
handler.php
3 weeks ago
helper.php
3 weeks ago
input.php
3 weeks ago
installer.php
3 weeks ago
session.php
10 years ago
wpallimport.php
3 weeks ago
zip.php
4 years ago
helper.php
156 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 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error -- intentional runtime warning |
| 81 | trigger_error(__METHOD__ . ': array element', E_USER_WARNING); |
| 82 | } |
| 83 | } else { |
| 84 | $array[$key] = $string.$element; |
| 85 | } |
| 86 | } |
| 87 | return $array; |
| 88 | |
| 89 | } |
| 90 | |
| 91 | const FNM_PATHNAME = 1; |
| 92 | const FNM_NOESCAPE = 2; |
| 93 | const FNM_PERIOD = 4; |
| 94 | const FNM_CASEFOLD = 16; |
| 95 | |
| 96 | /** |
| 97 | * non-POSIX complient remplacement for the fnmatch |
| 98 | */ |
| 99 | public static function fnmatch($pattern, $string, $flags = 0) { |
| 100 | |
| 101 | $modifiers = null; |
| 102 | $transforms = array( |
| 103 | '\*' => '.*', |
| 104 | '\?' => '.', |
| 105 | '\[\!' => '[^', |
| 106 | '\[' => '[', |
| 107 | '\]' => ']', |
| 108 | '\.' => '\.', |
| 109 | '\\' => '\\\\', |
| 110 | '\-' => '-', |
| 111 | ); |
| 112 | |
| 113 | // Forward slash in string must be in pattern: |
| 114 | if ($flags & FNM_PATHNAME) { |
| 115 | $transforms['\*'] = '[^/]*'; |
| 116 | } |
| 117 | |
| 118 | // Back slash should not be escaped: |
| 119 | if ($flags & FNM_NOESCAPE) { |
| 120 | unset($transforms['\\']); |
| 121 | } |
| 122 | |
| 123 | // Perform case insensitive match: |
| 124 | if ($flags & FNM_CASEFOLD) { |
| 125 | $modifiers .= 'i'; |
| 126 | } |
| 127 | |
| 128 | // Period at start must be the same as pattern: |
| 129 | if ($flags & FNM_PERIOD) { |
| 130 | if (strpos($string, '.') === 0 && strpos($pattern, '.') !== 0) return false; |
| 131 | } |
| 132 | |
| 133 | $pattern = '#^' |
| 134 | .strtr(preg_quote($pattern, '#'), $transforms) |
| 135 | .'$#' |
| 136 | .$modifiers; |
| 137 | |
| 138 | return (bool)preg_match($pattern, $string); |
| 139 | } |
| 140 | |
| 141 | public static function is_rapid_export_addon($cpt) |
| 142 | { |
| 143 | if (is_array($cpt)) { |
| 144 | $cpt = $cpt[0]; |
| 145 | } |
| 146 | |
| 147 | $is_rapid_addon_export = true; |
| 148 | |
| 149 | if (strpos($cpt, 'custom_') !== 0) { |
| 150 | $is_rapid_addon_export = false; |
| 151 | } |
| 152 | |
| 153 | return $is_rapid_addon_export; |
| 154 | } |
| 155 | } |
| 156 |