PMWI_Helper

Helper class which defnes a namespace for some commonly used functions.

Defined (1)

The class is defined in the following location(s).

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