diff --git a/lib/Minify.php b/lib/Minify.php index f6a532b..c42e60e 100644 --- a/lib/Minify.php +++ b/lib/Minify.php @@ -274,7 +274,8 @@ class Minify // depending on what the client accepts, $contentEncoding may be // 'x-gzip' while our internal encodeMethod is 'gzip'. Calling // getAcceptedEncoding(false, false) leaves out compress and deflate as options. - list($this->options['encodeMethod'], $contentEncoding) = HTTP_Encoder::getAcceptedEncoding(false, false); + $list = HTTP_Encoder::getAcceptedEncoding(false, false); + list($this->options['encodeMethod'], $contentEncoding) = $list; $sendVary = ! HTTP_Encoder::isBuggyIe(); } } else { @@ -515,8 +516,8 @@ class Minify if ($file && !isset($minifyOptions['currentDir']) - && !isset($minifyOptions['prependRelativePath']) - ) { + && !isset($minifyOptions['prependRelativePath'])) { + $minifyOptions['currentDir'] = dirname($file); $source->setMinifierOptions($minifyOptions); } @@ -596,8 +597,7 @@ class Minify ! $source // yes, we ran out of sources || $type === self::TYPE_CSS // yes, to process CSS individually (avoiding PCRE bugs/limits) || $minifier !== $lastMinifier // yes, minifier changed - || $options !== $lastOptions) // yes, options changed - ) { + || $options !== $lastOptions)) { // yes, options changed // minify previous sources with last settings $imploded = implode($implodeSeparator, $groupToProcessTogether); $groupToProcessTogether = array(); diff --git a/lib/Minify/App.php b/lib/Minify/App.php index ecab7ef..840f364 100644 --- a/lib/Minify/App.php +++ b/lib/Minify/App.php @@ -64,9 +64,10 @@ class App extends Container $propNames = array_keys(get_object_vars($config)); - $varNames = array_map(function ($name) { + $prefixer = function ($name) { return "min_$name"; - }, $propNames); + }; + $varNames = array_map($prefixer, $propNames); $vars = compact($varNames); diff --git a/lib/Minify/Build.php b/lib/Minify/Build.php index dcfd8e2..bf9b487 100644 --- a/lib/Minify/Build.php +++ b/lib/Minify/Build.php @@ -70,9 +70,7 @@ class Minify_Build */ public function uri($uri, $forceAmpersand = false) { - $sep = ($forceAmpersand || strpos($uri, '?') !== false) - ? self::$ampersand - : '?'; + $sep = ($forceAmpersand || strpos($uri, '?') !== false) ? self::$ampersand : '?'; return "{$uri}{$sep}{$this->lastModified}"; } diff --git a/lib/Minify/CSS.php b/lib/Minify/CSS.php index bc3a3ad..c311f2e 100644 --- a/lib/Minify/CSS.php +++ b/lib/Minify/CSS.php @@ -71,32 +71,27 @@ class Minify_CSS if ($options['removeCharsets']) { $css = preg_replace('/@charset[^;]+;\\s*/', '', $css); } + if ($options['compress']) { if (! $options['preserveComments']) { $css = Minify_CSS_Compressor::process($css, $options); } else { - $css = Minify_CommentPreserver::process( - $css - ,array('Minify_CSS_Compressor', 'process') - ,array($options) - ); + $processor = array('Minify_CSS_Compressor', 'process'); + $css = Minify_CommentPreserver::process($css, $processor, array($options)); } } + if (! $options['currentDir'] && ! $options['prependRelativePath']) { return $css; } + if ($options['currentDir']) { - return Minify_CSS_UriRewriter::rewrite( - $css - ,$options['currentDir'] - ,$options['docRoot'] - ,$options['symlinks'] - ); + $currentDir = $options['currentDir']; + $docRoot = $options['docRoot']; + $symlinks = $options['symlinks']; + return Minify_CSS_UriRewriter::rewrite($css, $currentDir, $docRoot, $symlinks); } else { - return Minify_CSS_UriRewriter::prepend( - $css - ,$options['prependRelativePath'] - ); + return Minify_CSS_UriRewriter::prepend($css, $options['prependRelativePath']); } } } diff --git a/lib/Minify/CSS/Compressor.php b/lib/Minify/CSS/Compressor.php index 5fba844..ee3c7d6 100644 --- a/lib/Minify/CSS/Compressor.php +++ b/lib/Minify/CSS/Compressor.php @@ -88,8 +88,8 @@ class Minify_CSS_Compressor $css = preg_replace('@:\\s*/\\*\\s*\\*/@', ':/*keep*/', $css); // apply callback to all valid comments (and strip out surrounding ws - $css = preg_replace_callback('@\\s*/\\*([\\s\\S]*?)\\*/\\s*@' - ,array($this, '_commentCB'), $css); + $pattern = '@\\s*/\\*([\\s\\S]*?)\\*/\\s*@'; + $css = preg_replace_callback($pattern, array($this, '_commentCB'), $css); // remove ws around { } and last semicolon in declaration block $css = preg_replace('/\\s*{\\s*/', '{', $css); @@ -99,16 +99,17 @@ class Minify_CSS_Compressor $css = preg_replace('/\\s*;\\s*/', ';', $css); // remove ws around urls - $css = preg_replace('/ + $pattern = '/ url\\( # url( \\s* ([^\\)]+?) # 1 = the URL (really just a bunch of non right parenthesis) \\s* \\) # ) - /x', 'url($1)', $css); + /x'; + $css = preg_replace($pattern, 'url($1)', $css); // remove ws between rules and colons - $css = preg_replace('/ + $pattern = '/ \\s* ([{;]) # 1 = beginning of block or rule separator \\s* @@ -117,10 +118,11 @@ class Minify_CSS_Compressor : \\s* (\\b|[#\'"-]) # 3 = first character of a value - /x', '$1$2:$3', $css); + /x'; + $css = preg_replace($pattern, '$1$2:$3', $css); // remove ws in selectors - $css = preg_replace_callback('/ + $pattern = '/ (?: # non-capture \\s* [^~>+,\\s]+ # selector part @@ -130,16 +132,16 @@ class Minify_CSS_Compressor \\s* [^~>+,\\s]+ # selector part { # open declaration block - /x' - ,array($this, '_selectorsCB'), $css); + /x'; + $css = preg_replace_callback($pattern, array($this, '_selectorsCB'), $css); // minimize hex colors - $css = preg_replace('/([^=])#([a-f\\d])\\2([a-f\\d])\\3([a-f\\d])\\4([\\s;\\}])/i' - , '$1#$2$3$4$5', $css); + $pattern = '/([^=])#([a-f\\d])\\2([a-f\\d])\\3([a-f\\d])\\4([\\s;\\}])/i'; + $css = preg_replace($pattern, '$1#$2$3$4$5', $css); // remove spaces between font families - $css = preg_replace_callback('/font-family:([^;}]+)([;}])/' - ,array($this, '_fontFamilyCB'), $css); + $pattern = '/font-family:([^;}]+)([;}])/'; + $css = preg_replace_callback($pattern, array($this, '_fontFamilyCB'), $css); $css = preg_replace('/@import\\s+url/', '@import url', $css); @@ -147,14 +149,15 @@ class Minify_CSS_Compressor $css = preg_replace('/[ \\t]*\\n+\\s*/', "\n", $css); // separate common descendent selectors w/ newlines (to limit line lengths) - $css = preg_replace('/([\\w#\\.\\*]+)\\s+([\\w#\\.\\*]+){/', "$1\n$2{", $css); + $pattern = '/([\\w#\\.\\*]+)\\s+([\\w#\\.\\*]+){/'; + $css = preg_replace($pattern, "$1\n$2{", $css); // Use newline after 1st numeric value (to limit line lengths). - $css = preg_replace('/ + $pattern = '/ ((?:padding|margin|border|outline):\\d+(?:px|em)?) # 1 = prop : 1st numeric value \\s+ - /x' - ,"$1\n", $css); + /x'; + $css = preg_replace($pattern, "$1\n", $css); // prevent triggering IE6 bug: http://www.crankygeek.com/ie6pebug/ $css = preg_replace('/:first-l(etter|ine)\\{/', ':first-l$1 {', $css); @@ -191,52 +194,54 @@ class Minify_CSS_Compressor if ($m === 'keep') { return '/**/'; } + if ($m === '" "') { // component of http://tantek.com/CSS/Examples/midpass.html return '/*" "*/'; } + if (preg_match('@";\\}\\s*\\}/\\*\\s+@', $m)) { // component of http://tantek.com/CSS/Examples/midpass.html return '/*";}}/* */'; } + if ($this->_inHack) { // inversion: feeding only to one browser - if (preg_match('@ + $pattern = '@ ^/ # comment started like /*/ \\s* (\\S[\\s\\S]+?) # has at least some non-ws content \\s* /\\* # ends like /*/ or /**/ - @x', $m, $n)) { + @x'; + if (preg_match($pattern, $m, $n)) { // end hack mode after this comment, but preserve the hack and comment content $this->_inHack = false; - return "/*/{$n[1]}/**/"; } } + if (substr($m, -1) === '\\') { // comment ends like \*/ // begin hack mode and preserve hack $this->_inHack = true; - return '/*\\*/'; } + if ($m !== '' && $m[0] === '/') { // comment looks like /*/ foo */ // begin hack mode and preserve hack $this->_inHack = true; - return '/*/*/'; } + if ($this->_inHack) { // a regular comment ends hack mode but should be preserved $this->_inHack = false; - return '/**/'; } + // Issue 107: if there's any surrounding whitespace, it may be important, so // replace the comment with a single space - return $hasSurroundingWs // remove all other comments - ? ' ' - : ''; + return $hasSurroundingWs ? ' ' : ''; // remove all other comments } /** @@ -249,8 +254,10 @@ class Minify_CSS_Compressor protected function _fontFamilyCB($m) { // Issue 210: must not eliminate WS between words in unquoted families - $pieces = preg_split('/(\'[^\']+\'|"[^"]+")/', $m[1], null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); + $flags = PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY; + $pieces = preg_split('/(\'[^\']+\'|"[^"]+")/', $m[1], null, $flags); $out = 'font-family:'; + while (null !== ($piece = array_shift($pieces))) { if ($piece[0] !== '"' && $piece[0] !== "'") { $piece = preg_replace('/\\s+/', ' ', $piece); diff --git a/lib/Minify/CSS/UriRewriter.php b/lib/Minify/CSS/UriRewriter.php index 4fe7b46..248875c 100644 --- a/lib/Minify/CSS/UriRewriter.php +++ b/lib/Minify/CSS/UriRewriter.php @@ -52,10 +52,10 @@ class Minify_CSS_UriRewriter // normalize symlinks in order to map to link foreach ($symlinks as $link => $target) { - $link = ($link === '//') - ? self::$_docRoot - : str_replace('//', self::$_docRoot . '/', $link); + + $link = ($link === '//') ? self::$_docRoot : str_replace('//', self::$_docRoot . '/', $link); $link = strtr($link, '/', DIRECTORY_SEPARATOR); + self::$_symlinks[$link] = self::_realpath($target); } @@ -71,10 +71,11 @@ class Minify_CSS_UriRewriter $css = self::_owlifySvgPaths($css); // rewrite - $css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/' - ,array(self::$className, '_processUriCB'), $css); - $css = preg_replace_callback('/url\\(\\s*([\'"](.*?)[\'"]|[^\\)\\s]+)\\s*\\)/' - ,array(self::$className, '_processUriCB'), $css); + $pattern = '/@import\\s+([\'"])(.*?)[\'"]/'; + $css = preg_replace_callback($pattern, array(self::$className, '_processUriCB'), $css); + + $pattern = '/url\\(\\s*([\'"](.*?)[\'"]|[^\\)\\s]+)\\s*\\)/'; + $css = preg_replace_callback($pattern, array(self::$className, '_processUriCB'), $css); $css = self::_unOwlify($css); @@ -99,10 +100,11 @@ class Minify_CSS_UriRewriter $css = self::_owlifySvgPaths($css); // append - $css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/' - ,array(self::$className, '_processUriCB'), $css); - $css = preg_replace_callback('/url\\(\\s*([\'"](.*?)[\'"]|[^\\)\\s]+)\\s*\\)/' - ,array(self::$className, '_processUriCB'), $css); + $pattern = '/@import\\s+([\'"])(.*?)[\'"]/'; + $css = preg_replace_callback($pattern, array(self::$className, '_processUriCB'), $css); + + $pattern = '/url\\(\\s*([\'"](.*?)[\'"]|[^\\)\\s]+)\\s*\\)/'; + $css = preg_replace_callback($pattern, array(self::$className, '_processUriCB'), $css); $css = self::_unOwlify($css); @@ -152,8 +154,8 @@ class Minify_CSS_UriRewriter public static function rewriteRelative($uri, $realCurrentDir, $realDocRoot, $symlinks = array()) { // prepend path with current dir separator (OS-independent) - $path = strtr($realCurrentDir, '/', DIRECTORY_SEPARATOR) - . DIRECTORY_SEPARATOR . strtr($uri, '/', DIRECTORY_SEPARATOR); + $path = strtr($realCurrentDir, '/', DIRECTORY_SEPARATOR); + $path .= DIRECTORY_SEPARATOR . strtr($uri, '/', DIRECTORY_SEPARATOR); self::$debugText .= "file-relative URI : {$uri}\n" . "path prepended : {$path}\n"; @@ -263,13 +265,14 @@ class Minify_CSS_UriRewriter */ private static function _trimUrls($css) { - return preg_replace('/ + $pattern = '/ url\\( # url( \\s* ([^\\)]+?) # 1 = URI (assuming does not contain ")") \\s* \\) # ) - /x', 'url($1)', $css); + /x'; + return preg_replace($pattern, 'url($1)', $css); } /** @@ -287,12 +290,9 @@ class Minify_CSS_UriRewriter $uri = $m[2]; } else { // $m[1] is either quoted or not - $quoteChar = ($m[1][0] === "'" || $m[1][0] === '"') - ? $m[1][0] - : ''; - $uri = ($quoteChar === '') - ? $m[1] - : substr($m[1], 1, strlen($m[1]) - 2); + $quoteChar = ($m[1][0] === "'" || $m[1][0] === '"') ? $m[1][0] : ''; + + $uri = ($quoteChar === '') ? $m[1] : substr($m[1], 1, strlen($m[1]) - 2); } // if not root/scheme relative and not starts with scheme if (!preg_match('~^(/|[a-z]+\:)~', $uri)) { @@ -313,9 +313,11 @@ class Minify_CSS_UriRewriter } } - return $isImport - ? "@import {$quoteChar}{$uri}{$quoteChar}" - : "url({$quoteChar}{$uri}{$quoteChar})"; + if ($isImport) { + return "@import {$quoteChar}{$uri}{$quoteChar}"; + } else { + return "url({$quoteChar}{$uri}{$quoteChar})"; + } } /** @@ -329,7 +331,8 @@ class Minify_CSS_UriRewriter */ private static function _owlifySvgPaths($css) { - return preg_replace('~\b((?:clip-path|mask|-webkit-mask)\s*\:\s*)url(\(\s*#\w+\s*\))~', '$1owl$2', $css); + $pattern = '~\b((?:clip-path|mask|-webkit-mask)\s*\:\s*)url(\(\s*#\w+\s*\))~'; + return preg_replace($pattern, '$1owl$2', $css); } /** @@ -342,6 +345,7 @@ class Minify_CSS_UriRewriter */ private static function _unOwlify($css) { - return preg_replace('~\b((?:clip-path|mask|-webkit-mask)\s*\:\s*)owl~', '$1url', $css); + $pattern = '~\b((?:clip-path|mask|-webkit-mask)\s*\:\s*)owl~'; + return preg_replace($pattern, '$1url', $css); } } diff --git a/lib/Minify/Cache/APC.php b/lib/Minify/Cache/APC.php index 94c2871..479938d 100644 --- a/lib/Minify/Cache/APC.php +++ b/lib/Minify/Cache/APC.php @@ -59,9 +59,11 @@ class Minify_Cache_APC implements Minify_CacheInterface return false; } - return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) - ? mb_strlen($this->_data, '8bit') - : strlen($this->_data); + if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) { + return mb_strlen($this->_data, '8bit'); + } else { + return strlen($this->_data);; + } } /** @@ -85,9 +87,7 @@ class Minify_Cache_APC implements Minify_CacheInterface */ public function display($id) { - echo $this->_fetch($id) - ? $this->_data - : ''; + echo $this->_fetch($id) ? $this->_data : ''; } /** @@ -99,9 +99,7 @@ class Minify_Cache_APC implements Minify_CacheInterface */ public function fetch($id) { - return $this->_fetch($id) - ? $this->_data - : ''; + return $this->_fetch($id) ? $this->_data : ''; } private $_exp = null; @@ -126,9 +124,9 @@ class Minify_Cache_APC implements Minify_CacheInterface $ret = apc_fetch($id); if (false === $ret) { $this->_id = null; - return false; } + list($this->_lm, $this->_data) = explode('|', $ret, 2); $this->_id = $id; diff --git a/lib/Minify/Cache/File.php b/lib/Minify/Cache/File.php index eeba305..7f57472 100644 --- a/lib/Minify/Cache/File.php +++ b/lib/Minify/Cache/File.php @@ -54,13 +54,13 @@ class Minify_Cache_File implements Minify_CacheInterface */ public function store($id, $data) { - $flag = $this->locking - ? LOCK_EX - : null; + $flag = $this->locking ? LOCK_EX : null; $file = $this->path . '/' . $id; + if (! @file_put_contents($file, $data, $flag)) { $this->logger->warning("Minify_Cache_File: Write failed to '$file'"); } + // write control if ($data !== $this->fetch($id)) { @unlink($file); @@ -107,15 +107,16 @@ class Minify_Cache_File implements Minify_CacheInterface */ public function display($id) { - if ($this->locking) { - $fp = fopen($this->path . '/' . $id, 'rb'); - flock($fp, LOCK_SH); - fpassthru($fp); - flock($fp, LOCK_UN); - fclose($fp); - } else { + if (!$this->locking) { readfile($this->path . '/' . $id); + return; } + + $fp = fopen($this->path . '/' . $id, 'rb'); + flock($fp, LOCK_SH); + fpassthru($fp); + flock($fp, LOCK_UN); + fclose($fp); } /** @@ -127,20 +128,21 @@ class Minify_Cache_File implements Minify_CacheInterface */ public function fetch($id) { - if ($this->locking) { - $fp = fopen($this->path . '/' . $id, 'rb'); - if (!$fp) { - return false; - } - flock($fp, LOCK_SH); - $ret = stream_get_contents($fp); - flock($fp, LOCK_UN); - fclose($fp); - - return $ret; - } else { + if (!$this->locking) { return file_get_contents($this->path . '/' . $id); } + + $fp = fopen($this->path . '/' . $id, 'rb'); + if (!$fp) { + return false; + } + + flock($fp, LOCK_SH); + $ret = stream_get_contents($fp); + flock($fp, LOCK_UN); + fclose($fp); + + return $ret; } /** diff --git a/lib/Minify/Cache/Memcache.php b/lib/Minify/Cache/Memcache.php index 180b607..d70dd07 100644 --- a/lib/Minify/Cache/Memcache.php +++ b/lib/Minify/Cache/Memcache.php @@ -62,9 +62,11 @@ class Minify_Cache_Memcache implements Minify_CacheInterface return false; } - return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) - ? mb_strlen($this->_data, '8bit') - : strlen($this->_data); + if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) { + return mb_strlen($this->_data, '8bit'); + } else { + return strlen($this->_data); + } } /** @@ -88,9 +90,7 @@ class Minify_Cache_Memcache implements Minify_CacheInterface */ public function display($id) { - echo $this->_fetch($id) - ? $this->_data - : ''; + echo $this->_fetch($id) ? $this->_data : ''; } /** @@ -102,9 +102,7 @@ class Minify_Cache_Memcache implements Minify_CacheInterface */ public function fetch($id) { - return $this->_fetch($id) - ? $this->_data - : ''; + return $this->_fetch($id) ? $this->_data : ''; } private $_mc = null; @@ -127,12 +125,14 @@ class Minify_Cache_Memcache implements Minify_CacheInterface if ($this->_id === $id) { return true; } + $ret = $this->_mc->get($id); if (false === $ret) { $this->_id = null; return false; } + list($this->_lm, $this->_data) = explode('|', $ret, 2); $this->_id = $id; diff --git a/lib/Minify/Cache/WinCache.php b/lib/Minify/Cache/WinCache.php index 26132bf..46e285a 100644 --- a/lib/Minify/Cache/WinCache.php +++ b/lib/Minify/Cache/WinCache.php @@ -60,7 +60,11 @@ class Minify_Cache_WinCache implements Minify_CacheInterface return false; } - return (function_exists('mb_strlen') && ((int) ini_get('mbstring.func_overload') & 2)) ? mb_strlen($this->_data, '8bit') : strlen($this->_data); + if (function_exists('mb_strlen') && ((int) ini_get('mbstring.func_overload') & 2)) { + return mb_strlen($this->_data, '8bit'); + } else { + return strlen($this->_data); + } } /** @@ -118,6 +122,7 @@ class Minify_Cache_WinCache implements Minify_CacheInterface if ($this->_id === $id) { return true; } + $suc = false; $ret = wincache_ucache_get($id, $suc); if (!$suc) { @@ -125,6 +130,7 @@ class Minify_Cache_WinCache implements Minify_CacheInterface return false; } + list($this->_lm, $this->_data) = explode('|', $ret, 2); $this->_id = $id; diff --git a/lib/Minify/Cache/XCache.php b/lib/Minify/Cache/XCache.php index a5585bb..f293cb3 100644 --- a/lib/Minify/Cache/XCache.php +++ b/lib/Minify/Cache/XCache.php @@ -56,9 +56,11 @@ class Minify_Cache_XCache implements Minify_CacheInterface return false; } - return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) - ? mb_strlen($this->_data, '8bit') - : strlen($this->_data); + if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) { + return mb_strlen($this->_data, '8bit'); + } else { + return strlen($this->_data); + } } /** @@ -80,9 +82,7 @@ class Minify_Cache_XCache implements Minify_CacheInterface */ public function display($id) { - echo $this->_fetch($id) - ? $this->_data - : ''; + echo $this->_fetch($id) ? $this->_data : ''; } /** @@ -93,9 +93,7 @@ class Minify_Cache_XCache implements Minify_CacheInterface */ public function fetch($id) { - return $this->_fetch($id) - ? $this->_data - : ''; + return $this->_fetch($id) ? $this->_data : ''; } private $_exp = null; @@ -116,12 +114,14 @@ class Minify_Cache_XCache implements Minify_CacheInterface if ($this->_id === $id) { return true; } + $ret = xcache_get($id); if (false === $ret) { $this->_id = null; return false; } + list($this->_lm, $this->_data) = explode('|', $ret, 2); $this->_id = $id; diff --git a/lib/Minify/Cache/ZendPlatform.php b/lib/Minify/Cache/ZendPlatform.php index b1f6d22..76e9920 100644 --- a/lib/Minify/Cache/ZendPlatform.php +++ b/lib/Minify/Cache/ZendPlatform.php @@ -55,9 +55,7 @@ class Minify_Cache_ZendPlatform implements Minify_CacheInterface */ public function getSize($id) { - return $this->_fetch($id) - ? strlen($this->_data) - : false; + return $this->_fetch($id) ? strlen($this->_data) : false; } /** @@ -71,9 +69,7 @@ class Minify_Cache_ZendPlatform implements Minify_CacheInterface */ public function isValid($id, $srcMtime) { - $ret = ($this->_fetch($id) && ($this->_lm >= $srcMtime)); - - return $ret; + return ($this->_fetch($id) && ($this->_lm >= $srcMtime)); } /** @@ -83,9 +79,7 @@ class Minify_Cache_ZendPlatform implements Minify_CacheInterface */ public function display($id) { - echo $this->_fetch($id) - ? $this->_data - : ''; + echo $this->_fetch($id) ? $this->_data : ''; } /** @@ -97,9 +91,7 @@ class Minify_Cache_ZendPlatform implements Minify_CacheInterface */ public function fetch($id) { - return $this->_fetch($id) - ? $this->_data - : ''; + return $this->_fetch($id) ? $this->_data : ''; } private $_exp = null; @@ -121,12 +113,14 @@ class Minify_Cache_ZendPlatform implements Minify_CacheInterface if ($this->_id === $id) { return true; } + $ret = output_cache_get($id, $this->_exp); if (false === $ret) { $this->_id = null; return false; } + list($this->_lm, $this->_data) = explode('|', $ret, 2); $this->_id = $id; diff --git a/lib/Minify/CommentPreserver.php b/lib/Minify/CommentPreserver.php index f295839..6bdb6f3 100644 --- a/lib/Minify/CommentPreserver.php +++ b/lib/Minify/CommentPreserver.php @@ -72,21 +72,16 @@ class Minify_CommentPreserver */ private static function _nextComment($in) { - if ( - false === ($start = strpos($in, '/*!')) - || false === ($end = strpos($in, '*/', $start + 3)) - ) { + if (false === ($start = strpos($in, '/*!')) || false === ($end = strpos($in, '*/', $start + 3))) { return array($in, false, false); } - $ret = array( - substr($in, 0, $start) - ,self::$prepend . '/*!' . substr($in, $start + 3, $end - $start - 1) . self::$append - ); - $endChars = (strlen($in) - $end - 2); - $ret[] = (0 === $endChars) - ? '' - : substr($in, -$endChars); - return $ret; + $beforeComment = substr($in, 0, $start); + $comment = self::$prepend . '/*!' . substr($in, $start + 3, $end - $start - 1) . self::$append; + + $endChars = (strlen($in) - $end - 2); + $afterComment = (0 === $endChars) ? '' : substr($in, -$endChars); + + return array($beforeComment, $comment, $afterComment); } } diff --git a/lib/Minify/Controller/Groups.php b/lib/Minify/Controller/Groups.php index a115a00..8938288 100644 --- a/lib/Minify/Controller/Groups.php +++ b/lib/Minify/Controller/Groups.php @@ -45,12 +45,14 @@ class Minify_Controller_Groups extends Minify_Controller_Files $server = $this->env->server(); // mod_fcgid places PATH_INFO in ORIG_PATH_INFO - $pathInfo = isset($server['ORIG_PATH_INFO']) - ? substr($server['ORIG_PATH_INFO'], 1) - : (isset($server['PATH_INFO']) - ? substr($server['PATH_INFO'], 1) - : false - ); + if (isset($server['ORIG_PATH_INFO'])) { + $pathInfo = substr($server['ORIG_PATH_INFO'], 1); + } elseif (isset($server['PATH_INFO'])) { + $pathInfo = substr($server['PATH_INFO'], 1) + } else { + $pathInfo = false; + } + if (false === $pathInfo || ! isset($groups[$pathInfo])) { // no PATH_INFO or not a valid group $this->logger->info("Missing PATH_INFO or no group set for \"$pathInfo\""); diff --git a/lib/Minify/Controller/MinApp.php b/lib/Minify/Controller/MinApp.php index b09ac81..7606982 100644 --- a/lib/Minify/Controller/MinApp.php +++ b/lib/Minify/Controller/MinApp.php @@ -31,14 +31,14 @@ class Minify_Controller_MinApp extends Minify_Controller_Base } // filter controller options - $localOptions = array_merge( - array( - 'groupsOnly' => false, - 'groups' => array(), - 'symlinks' => array(), - ) - ,(isset($options['minApp']) ? $options['minApp'] : array()) + $defaults = array( + 'groupsOnly' => false, + 'groups' => array(), + 'symlinks' => array(), ); + $minApp = isset($options['minApp']) ? $options['minApp'] : array(); + $localOptions = array_merge($defaults, $minApp); + unset($options['minApp']); // normalize $symlinks in order to map to target @@ -53,7 +53,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base $sources = array(); $selectionId = ''; - $firstMissingResource = null; + $firstMissing = null; if (isset($get['g'])) { // add group(s) @@ -83,12 +83,12 @@ class Minify_Controller_MinApp extends Minify_Controller_Base $sources[] = $source; } catch (Minify_Source_FactoryException $e) { $this->logger->error($e->getMessage()); - if (null === $firstMissingResource) { - $firstMissingResource = basename($file); + if (null === $firstMissing) { + $firstMissing = basename($file); continue; } else { - $secondMissingResource = basename($file); - $this->logger->info("More than one file was missing: '$firstMissingResource', '$secondMissingResource'"); + $secondMissing = basename($file); + $this->logger->info("More than one file was missing: '$firstMissing', '$secondMissing'"); return new Minify_ServeConfiguration($options); } @@ -100,18 +100,18 @@ class Minify_Controller_MinApp extends Minify_Controller_Base // try user files // The following restrictions are to limit the URLs that minify will // respond to. - if (// verify at least one file, files are single comma separated, - // and are all same extension - ! preg_match('/^[^,]+\\.(css|less|js)(?:,[^,]+\\.\\1)*$/', $get['f'], $m) - // no "//" - || strpos($get['f'], '//') !== false - // no "\" - || strpos($get['f'], '\\') !== false - ) { + + // verify at least one file, files are single comma separated, and are all same extension + $validPattern = preg_match('/^[^,]+\\.(css|less|js)(?:,[^,]+\\.\\1)*$/', $get['f'], $m); + $hasComment = strpos($get['f'], '//') !== false; + $hasEscape = strpos($get['f'], '\\') !== false; + + if (!$validPattern || $hasComment || $hasEscape) { $this->logger->info("GET param 'f' was invalid"); return new Minify_ServeConfiguration($options); } + $ext = ".{$m[1]}"; $files = explode(',', $get['f']); if ($files != array_unique($files)) { @@ -119,11 +119,14 @@ class Minify_Controller_MinApp extends Minify_Controller_Base return new Minify_ServeConfiguration($options); } + if (isset($get['b'])) { // check for validity - if (preg_match('@^[^/]+(?:/[^/]+)*$@', $get['b']) - && false === strpos($get['b'], '..') - && $get['b'] !== '.') { + $isValidBase = preg_match('@^[^/]+(?:/[^/]+)*$@', $get['b']); + $hasDots = false !== strpos($get['b'], '..'); + $isDot = $get['b'] === '.'; + + if ($isValidBase && !$hasDots && !$isDot) { // valid base $base = "/{$get['b']}/"; } else { @@ -154,12 +157,12 @@ class Minify_Controller_MinApp extends Minify_Controller_Base $basenames[] = basename($path, $ext); } catch (Minify_Source_FactoryException $e) { $this->logger->error($e->getMessage()); - if (null === $firstMissingResource) { - $firstMissingResource = $uri; + if (null === $firstMissing) { + $firstMissing = $uri; continue; } else { - $secondMissingResource = $uri; - $this->logger->info("More than one file was missing: '$firstMissingResource', '$secondMissingResource`'"); + $secondMissing = $uri; + $this->logger->info("More than one file was missing: '$firstMissing', '$secondMissing`'"); return new Minify_ServeConfiguration($options); } @@ -177,14 +180,14 @@ class Minify_Controller_MinApp extends Minify_Controller_Base return new Minify_ServeConfiguration($options); } - if (null !== $firstMissingResource) { + if (null !== $firstMissing) { array_unshift($sources, new Minify_Source(array( - 'id' => 'missingFile' + 'id' => 'missingFile', // should not cause cache invalidation - ,'lastModified' => 0 + 'lastModified' => 0, // due to caching, filename is unreliable. - ,'content' => "/* Minify: at least one missing file. See " . Minify::URL_DEBUG . " */\n" - ,'minifier' => 'Minify::nullMinifier' + 'content' => "/* Minify: at least one missing file. See " . Minify::URL_DEBUG . " */\n", + 'minifier' => 'Minify::nullMinifier', ))); } diff --git a/lib/Minify/Controller/Page.php b/lib/Minify/Controller/Page.php index 36d9714..7001fe0 100644 --- a/lib/Minify/Controller/Page.php +++ b/lib/Minify/Controller/Page.php @@ -42,8 +42,8 @@ class Minify_Controller_Page extends Minify_Controller_Base } else { // strip controller options $sourceSpec = array( - 'content' => $options['content'] - ,'id' => $options['id'] + 'content' => $options['content'], + 'id' => $options['id'], ); $f = $options['id']; unset($options['content'], $options['id']); @@ -54,8 +54,8 @@ class Minify_Controller_Page extends Minify_Controller_Base if (isset($options['minifyAll'])) { // this will be the 2nd argument passed to Minify_HTML::minify() $sourceSpec['minifyOptions'] = array( - 'cssMinifier' => array('Minify_CSSmin', 'minify') - ,'jsMinifier' => array('JSMin\\JSMin', 'minify') + 'cssMinifier' => array('Minify_CSSmin', 'minify'), + 'jsMinifier' => array('JSMin\\JSMin', 'minify'), ); unset($options['minifyAll']); } diff --git a/lib/Minify/DebugDetector.php b/lib/Minify/DebugDetector.php index 10b19a7..19d03d0 100644 --- a/lib/Minify/DebugDetector.php +++ b/lib/Minify/DebugDetector.php @@ -13,6 +13,7 @@ class Minify_DebugDetector if ($env->get('debug') !== null) { return true; } + $cookieValue = $env->cookie('minifyDebug'); if ($cookieValue) { foreach (preg_split('/\\s+/', $cookieValue) as $debugUri) { diff --git a/lib/Minify/Env.php b/lib/Minify/Env.php index d8274e5..25490bc 100644 --- a/lib/Minify/Env.php +++ b/lib/Minify/Env.php @@ -34,6 +34,7 @@ class Minify_Env } else { $this->server['DOCUMENT_ROOT'] = rtrim($this->server['DOCUMENT_ROOT'], '/\\'); } + $this->server['DOCUMENT_ROOT'] = $this->normalizePath($this->server['DOCUMENT_ROOT']); $this->get = $options['get']; $this->post = $options['post']; @@ -46,9 +47,7 @@ class Minify_Env return $this->server; } - return isset($this->server[$key]) - ? $this->server[$key] - : null; + return isset($this->server[$key]) ? $this->server[$key] : null; } public function cookie($key = null, $default = null) @@ -93,6 +92,7 @@ class Minify_Env if ($realpath) { $path = $realpath; } + $path = str_replace('\\', '/', $path); $path = rtrim($path, '/'); if (substr($path, 1, 1) === ':') { @@ -118,11 +118,9 @@ class Minify_Env if (isset($server['SERVER_SOFTWARE']) && 0 !== strpos($server['SERVER_SOFTWARE'], 'Microsoft-IIS/')) { throw new InvalidArgumentException('DOCUMENT_ROOT is not provided and could not be computed'); } - $docRoot = substr( - $server['SCRIPT_FILENAME'] - ,0 - ,strlen($server['SCRIPT_FILENAME']) - strlen($server['SCRIPT_NAME']) - ); + + $substrLength = strlen($server['SCRIPT_FILENAME']) - strlen($server['SCRIPT_NAME']); + $docRoot = substr($server['SCRIPT_FILENAME'], 0, $substrLength); return rtrim($docRoot, '\\'); } diff --git a/lib/Minify/HTML/Helper.php b/lib/Minify/HTML/Helper.php index 498d3c7..bfded8b 100644 --- a/lib/Minify/HTML/Helper.php +++ b/lib/Minify/HTML/Helper.php @@ -99,8 +99,7 @@ class Minify_HTML_Helper foreach ($files as $k => $file) { if (0 === strpos($file, '//')) { $file = substr($file, 2); - } elseif (0 === strpos($file, '/') - || 1 === strpos($file, ':\\')) { + } elseif (0 === strpos($file, '/') || 1 === strpos($file, ':\\')) { $file = substr($file, strlen(self::app()->env->getDocRoot()) + 1); } $file = strtr($file, '\\', '/'); @@ -243,9 +242,7 @@ class Minify_HTML_Helper $base = substr($base, 0, strlen($base) - 1); $bUri = $minRoot . 'b=' . $base . '&f=' . implode(',', $basedPaths); - $uri = strlen($uri) < strlen($bUri) - ? $uri - : $bUri; + $uri = strlen($uri) < strlen($bUri) ? $uri : $bUri; } return $uri; diff --git a/lib/Minify/ImportProcessor.php b/lib/Minify/ImportProcessor.php index 1ed707b..09ec64c 100644 --- a/lib/Minify/ImportProcessor.php +++ b/lib/Minify/ImportProcessor.php @@ -57,8 +57,7 @@ class Minify_ImportProcessor $file = realpath($file); if (! $file || in_array($file, self::$filesIncluded) - || false === ($content = @file_get_contents($file)) - ) { + || false === ($content = @file_get_contents($file))) { // file missing, already included, or failed read return ''; } @@ -73,8 +72,7 @@ class Minify_ImportProcessor $content = str_replace("\r\n", "\n", $content); // process @imports - $content = preg_replace_callback( - '/ + $pattern = '/ @import\\s+ (?:url\\(\\s*)? # maybe url( [\'"]? # maybe quote @@ -83,19 +81,14 @@ class Minify_ImportProcessor (?:\\s*\\))? # maybe ) ([a-zA-Z,\\s]*)? # 2 = media list ; # end token - /x' - ,array($this, '_importCB') - ,$content - ); + /x'; + $content = preg_replace_callback($pattern, array($this, '_importCB'), $content); // You only need to rework the import-path if the script is imported if (self::$_isCss && $is_imported) { // rewrite remaining relative URIs - $content = preg_replace_callback( - '/url\\(\\s*([^\\)\\s]+)\\s*\\)/' - ,array($this, '_urlCB') - ,$content - ); + $pattern = '/url\\(\\s*([^\\)\\s]+)\\s*\\)/'; + $content = preg_replace_callback($pattern, array($this, '_urlCB'), $content); } return $this->_importedContent . $content; @@ -139,12 +132,10 @@ class Minify_ImportProcessor private function _urlCB($m) { // $m[1] is either quoted or not - $quote = ($m[1][0] === "'" || $m[1][0] === '"') - ? $m[1][0] - : ''; - $url = ($quote === '') - ? $m[1] - : substr($m[1], 1, strlen($m[1]) - 2); + $quote = ($m[1][0] === "'" || $m[1][0] === '"') ? $m[1][0] : ''; + + $url = ($quote === '') ? $m[1] : substr($m[1], 1, strlen($m[1]) - 2); + if ('/' !== $url[0]) { if (strpos($url, '//') > 0) { // probably starts with protocol, do not alter @@ -190,8 +181,8 @@ class Minify_ImportProcessor private function truepath($path) { // whether $path is unix or not - $unipath = strlen($path) == 0 || $path{0} - != '/'; + $unipath = (strlen($path) == 0) || ($path{0} != '/'); + // attempts to detect if path is relative in which case, add cwd if (strpos($path, ':') === false && $unipath) { $path = $this->_currentDir . DIRECTORY_SEPARATOR . $path; @@ -211,6 +202,7 @@ class Minify_ImportProcessor $absolutes[] = $part; } } + $path = implode(DIRECTORY_SEPARATOR, $absolutes); // resolve any symlinks if (file_exists($path) && linkinfo($path) > 0) { diff --git a/lib/Minify/Lines.php b/lib/Minify/Lines.php index db85d85..499a9cc 100644 --- a/lib/Minify/Lines.php +++ b/lib/Minify/Lines.php @@ -37,9 +37,7 @@ class Minify_Lines */ public static function minify($content, $options = array()) { - $id = (isset($options['id']) && $options['id']) - ? $options['id'] - : ''; + $id = (isset($options['id']) && $options['id']) ? $options['id'] : ''; $content = str_replace("\r\n", "\n", $content); $lines = explode("\n", $content); @@ -49,6 +47,7 @@ class Minify_Lines $inComment = false; $i = 0; $newLines = array(); + while (null !== ($line = array_shift($lines))) { if (('' !== $id) && (0 == $i % 50)) { if ($inComment) { @@ -57,24 +56,25 @@ class Minify_Lines array_push($newLines, '', "/* {$id} */", ''); } } + ++$i; $newLines[] = self::_addNote($line, $i, $inComment, $padTo); $inComment = self::_eolInComment($line, $inComment); } + $content = implode("\n", $newLines) . "\n"; // check for desired URI rewriting if (isset($options['currentDir'])) { Minify_CSS_UriRewriter::$debugText = ''; - $content = Minify_CSS_UriRewriter::rewrite( - $content - ,$options['currentDir'] - ,isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT'] - ,isset($options['symlinks']) ? $options['symlinks'] : array() - ); + $docRoot = isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT']; + $symlinks = isset($options['symlinks']) ? $options['symlinks'] : array(); + + $content = Minify_CSS_UriRewriter::rewrite($content, $options['currentDir'], $docRoot, $symlinks); + $content = "/* Minify_CSS_UriRewriter::\$debugText\n\n" - . Minify_CSS_UriRewriter::$debugText . "*/\n" - . $content; + . Minify_CSS_UriRewriter::$debugText . "*/\n" + . $content; } return $content; @@ -143,9 +143,11 @@ class Minify_Lines */ private static function _addNote($line, $note, $inComment, $padTo) { - $line = $inComment - ? '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' *| ' . $line - : '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' */ ' . $line; + if ($inComment) { + $line = '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' *| ' . $line; + } else { + $line = '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' */ ' . $line; + } return rtrim($line); } diff --git a/lib/Minify/Source.php b/lib/Minify/Source.php index 39597a6..a4fc4d7 100644 --- a/lib/Minify/Source.php +++ b/lib/Minify/Source.php @@ -72,13 +72,13 @@ class Minify_Source implements Minify_SourceInterface $ext = pathinfo($spec['filepath'], PATHINFO_EXTENSION); switch ($ext) { case 'js': $this->contentType = Minify::TYPE_JS; - break; + break; case 'less': // fallthrough case 'css': $this->contentType = Minify::TYPE_CSS; - break; + break; case 'htm': // fallthrough case 'html': $this->contentType = Minify::TYPE_HTML; - break; + break; } $this->filepath = $spec['filepath']; $this->id = $spec['filepath']; @@ -97,9 +97,7 @@ class Minify_Source implements Minify_SourceInterface } else { $this->getContentFunc = $spec['getContentFunc']; } - $this->lastModified = isset($spec['lastModified']) - ? $spec['lastModified'] - : time(); + $this->lastModified = isset($spec['lastModified']) ? $spec['lastModified'] : time(); } if (isset($spec['contentType'])) { $this->contentType = $spec['contentType']; @@ -208,8 +206,8 @@ class Minify_Source implements Minify_SourceInterface { if ($this->filepath && !isset($this->minifyOptions['currentDir']) - && !isset($this->minifyOptions['prependRelativePath']) - ) { + && !isset($this->minifyOptions['prependRelativePath'])) { + $this->minifyOptions['currentDir'] = dirname($this->filepath); } } diff --git a/lib/Minify/YUICompressor.php b/lib/Minify/YUICompressor.php index 1614893..4c3c53b 100644 --- a/lib/Minify/YUICompressor.php +++ b/lib/Minify/YUICompressor.php @@ -93,6 +93,7 @@ class Minify_YUICompressor if (! ($tmpFile = tempnam(self::$tempDir, 'yuic_'))) { throw new Exception('Minify_YUICompressor : could not create temp file in "'.self::$tempDir.'".'); } + file_put_contents($tmpFile, $content); exec(self::_getCmd($options, $type, $tmpFile), $output, $result_code); unlink($tmpFile); @@ -105,22 +106,19 @@ class Minify_YUICompressor private static function _getCmd($userOptions, $type, $tmpFile) { - $o = array_merge( - array( - 'charset' => '' - ,'line-break' => 5000 - ,'type' => $type - ,'nomunge' => false - ,'preserve-semi' => false - ,'disable-optimizations' => false - ,'stack-size' => '' - ) - ,$userOptions + $defaults = array( + 'charset' => '', + 'line-break' => 5000, + 'type' => $type, + 'nomunge' => false, + 'preserve-semi' => false, + 'disable-optimizations' => false, + 'stack-size' => '', ); + $o = array_merge($defaults, $userOptions); + $cmd = self::$javaExecutable - . (!empty($o['stack-size']) - ? ' -Xss' . $o['stack-size'] - : '') + . (!empty($o['stack-size']) ? ' -Xss' . $o['stack-size'] : '') . ' -jar ' . escapeshellarg(self::$jarFile) . " --type {$type}" . (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset'])