|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00020 class Minify_ImportProcessor { 00021 00022 public static $filesIncluded = array(); 00023 00024 public static function process($file) 00025 { 00026 self::$filesIncluded = array(); 00027 self::$_isCss = (strtolower(substr($file, -4)) === '.css'); 00028 $obj = new Minify_ImportProcessor(dirname($file)); 00029 return $obj->_getContent($file); 00030 } 00031 00032 // allows callback funcs to know the current directory 00033 private $_currentDir = null; 00034 00035 // allows _importCB to write the fetched content back to the obj 00036 private $_importedContent = ''; 00037 00038 private static $_isCss = null; 00039 00040 private function __construct($currentDir) 00041 { 00042 $this->_currentDir = $currentDir; 00043 } 00044 00045 private function _getContent($file) 00046 { 00047 $file = realpath($file); 00048 if (! $file 00049 || in_array($file, self::$filesIncluded) 00050 || false === ($content = @file_get_contents($file)) 00051 ) { 00052 // file missing, already included, or failed read 00053 return ''; 00054 } 00055 self::$filesIncluded[] = realpath($file); 00056 $this->_currentDir = dirname($file); 00057 00058 // remove UTF-8 BOM if present 00059 if (pack("CCC",0xef,0xbb,0xbf) === substr($content, 0, 3)) { 00060 $content = substr($content, 3); 00061 } 00062 // ensure uniform EOLs 00063 $content = str_replace("\r\n", "\n", $content); 00064 00065 // process @imports 00066 $content = preg_replace_callback( 00067 '/ 00068 @import\\s+ 00069 (?:url\\(\\s*)? # maybe url( 00070 [\'"]? # maybe quote 00071 (.*?) # 1 = URI 00072 [\'"]? # maybe end quote 00073 (?:\\s*\\))? # maybe ) 00074 ([a-zA-Z,\\s]*)? # 2 = media list 00075 ; # end token 00076 /x' 00077 ,array($this, '_importCB') 00078 ,$content 00079 ); 00080 00081 if (self::$_isCss) { 00082 // rewrite remaining relative URIs 00083 $content = preg_replace_callback( 00084 '/url\\(\\s*([^\\)\\s]+)\\s*\\)/' 00085 ,array($this, '_urlCB') 00086 ,$content 00087 ); 00088 } 00089 00090 return $this->_importedContent . $content; 00091 } 00092 00093 private function _importCB($m) 00094 { 00095 $url = $m[1]; 00096 $mediaList = preg_replace('/\\s+/', '', $m[2]); 00097 00098 if (strpos($url, '://') > 0) { 00099 // protocol, leave in place for CSS, comment for JS 00100 return self::$_isCss 00101 ? $m[0] 00102 : "/* Minify_ImportProcessor will not include remote content */"; 00103 } 00104 if ('/' === $url[0]) { 00105 // protocol-relative or root path 00106 $url = ltrim($url, '/'); 00107 $file = realpath($_SERVER['DOCUMENT_ROOT']) . DIRECTORY_SEPARATOR 00108 . strtr($url, '/', DIRECTORY_SEPARATOR); 00109 } else { 00110 // relative to current path 00111 $file = $this->_currentDir . DIRECTORY_SEPARATOR 00112 . strtr($url, '/', DIRECTORY_SEPARATOR); 00113 } 00114 $obj = new Minify_ImportProcessor(dirname($file)); 00115 $content = $obj->_getContent($file); 00116 if ('' === $content) { 00117 // failed. leave in place for CSS, comment for JS 00118 return self::$_isCss 00119 ? $m[0] 00120 : "/* Minify_ImportProcessor could not fetch '{$file}' */";; 00121 } 00122 return (!self::$_isCss || preg_match('@(?:^$|\\ball\\b)@', $mediaList)) 00123 ? $content 00124 : "@media {$mediaList} {\n{$content}\n}\n"; 00125 } 00126 00127 private function _urlCB($m) 00128 { 00129 // $m[1] is either quoted or not 00130 $quote = ($m[1][0] === "'" || $m[1][0] === '"') 00131 ? $m[1][0] 00132 : ''; 00133 $url = ($quote === '') 00134 ? $m[1] 00135 : substr($m[1], 1, strlen($m[1]) - 2); 00136 if ('/' !== $url[0]) { 00137 if (strpos($url, '//') > 0) { 00138 // probably starts with protocol, do not alter 00139 } else { 00140 // prepend path with current dir separator (OS-independent) 00141 $path = $this->_currentDir 00142 . DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR); 00143 // strip doc root 00144 $path = substr($path, strlen(realpath($_SERVER['DOCUMENT_ROOT']))); 00145 // fix to absolute URL 00146 $url = strtr($path, '/\\', '//'); 00147 // remove /./ and /../ where possible 00148 $url = str_replace('/./', '/', $url); 00149 // inspired by patch from Oleg Cherniy 00150 do { 00151 $url = preg_replace('@/[^/]+/\\.\\./@', '/', $url, 1, $changed); 00152 } while ($changed); 00153 } 00154 } 00155 return "url({$quote}{$url}{$quote})"; 00156 } 00157 }