diff --git a/lib/csslib.php b/lib/csslib.php index 37c5f3f7a72..574d246ade7 100644 --- a/lib/csslib.php +++ b/lib/csslib.php @@ -317,8 +317,8 @@ function css_minify_css($files) { unset($_SERVER['HTTP_IF_NONE_MATCH']); unset($_SERVER['HTTP_IF_MODIFIED_SINCE']); - set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path()); - require_once('Minify.php'); + require_once("$CFG->libdir/minify/lib/Minify/Loader.php"); + Minify_Loader::register(); if (0 === stripos(PHP_OS, 'win')) { Minify::setDocRoot(); // IIS may need help diff --git a/lib/jslib.php b/lib/jslib.php index bbfb895e534..ecfa35d511f 100644 --- a/lib/jslib.php +++ b/lib/jslib.php @@ -99,14 +99,14 @@ function js_send_unmodified($lastmodified, $etag) { * @return string */ function js_minify($files) { - // setup include path - set_include_path(__DIR__ . '/minify/lib' . PATH_SEPARATOR . get_include_path()); - require_once('Minify.php'); + global $CFG; if (empty($files)) { return ''; } + require_once("$CFG->libdir/minify/lib/Minify/Loader.php"); + Minify_Loader::register(); if (0 === stripos(PHP_OS, 'win')) { Minify::setDocRoot(); // IIS may need help } diff --git a/lib/minify/config.php b/lib/minify/config.php old mode 100644 new mode 100755 index 90e557ff588..bf8fc20c9bf --- a/lib/minify/config.php +++ b/lib/minify/config.php @@ -26,15 +26,19 @@ $min_uploaderHoursBehind = 0; $min_libPath = dirname(__FILE__) . '/lib'; // do not change zlib compression or buffering here -// TODO: locking setting, caching setting - return; // end of moodle modification /** * Allow use of the Minify URI Builder app. Only set this to true while you need it. - **/ -$min_enableBuilder = true; + */ +$min_enableBuilder = false; + +/** + * If non-empty, the Builder will be protected with HTTP Digest auth. + * The username is "admin". + */ +$min_builderPassword = 'admin'; /** @@ -124,15 +128,10 @@ $min_serveOptions['maxAge'] = 1800; /** - * To use Google's Closure Compiler API (falling back to JSMin on failure), - * uncomment the following lines: + * To use Google's Closure Compiler API to minify Javascript (falling back to JSMin + * on failure), uncomment the following line: */ -/*function closureCompiler($js) { - require_once 'Minify/JS/ClosureCompiler.php'; - return Minify_JS_ClosureCompiler::minify($js); -} -$min_serveOptions['minifiers']['application/x-javascript'] = 'closureCompiler'; -//*/ +//$min_serveOptions['minifiers']['application/x-javascript'] = array('Minify_JS_ClosureCompiler', 'minify'); /** diff --git a/lib/minify/lib/CSSmin.php b/lib/minify/lib/CSSmin.php new file mode 100644 index 00000000000..a60b4ac5b4a --- /dev/null +++ b/lib/minify/lib/CSSmin.php @@ -0,0 +1,758 @@ +memory_limit = 128 * 1048576; // 128MB in bytes + $this->max_execution_time = 60; // 1 min + $this->pcre_backtrack_limit = 1000 * 1000; + $this->pcre_recursion_limit = 500 * 1000; + + $this->raise_php_limits = (bool) $raise_php_limits; + } + + /** + * Minify a string of CSS + * @param string $css + * @param int|bool $linebreak_pos + * @return string + */ + public function run($css = '', $linebreak_pos = FALSE) + { + if (empty($css)) { + return ''; + } + + if ($this->raise_php_limits) { + $this->do_raise_php_limits(); + } + + $this->comments = array(); + $this->preserved_tokens = array(); + + $start_index = 0; + $length = strlen($css); + + $css = $this->extract_data_urls($css); + + // collect all comment blocks... + while (($start_index = $this->index_of($css, '/*', $start_index)) >= 0) { + $end_index = $this->index_of($css, '*/', $start_index + 2); + if ($end_index < 0) { + $end_index = $length; + } + $comment_found = $this->str_slice($css, $start_index + 2, $end_index); + $this->comments[] = $comment_found; + $comment_preserve_string = self::COMMENT . (count($this->comments) - 1) . '___'; + $css = $this->str_slice($css, 0, $start_index + 2) . $comment_preserve_string . $this->str_slice($css, $end_index); + // Set correct start_index: Fixes issue #2528130 + $start_index = $end_index + 2 + strlen($comment_preserve_string) - strlen($comment_found); + } + + // preserve strings so their content doesn't get accidentally minified + $css = preg_replace_callback('/(?:"(?:[^\\\\"]|\\\\.|\\\\)*")|'."(?:'(?:[^\\\\']|\\\\.|\\\\)*')/S", array($this, 'replace_string'), $css); + + // Let's divide css code in chunks of 25.000 chars aprox. + // Reason: PHP's PCRE functions like preg_replace have a "backtrack limit" + // of 100.000 chars by default (php < 5.3.7) so if we're dealing with really + // long strings and a (sub)pattern matches a number of chars greater than + // the backtrack limit number (i.e. /(.*)/s) PCRE functions may fail silently + // returning NULL and $css would be empty. + $charset = ''; + $charset_regexp = '/(@charset)( [^;]+;)/i'; + $css_chunks = array(); + $css_chunk_length = 25000; // aprox size, not exact + $start_index = 0; + $i = $css_chunk_length; // save initial iterations + $l = strlen($css); + + + // if the number of characters is 25000 or less, do not chunk + if ($l <= $css_chunk_length) { + $css_chunks[] = $css; + } else { + // chunk css code securely + while ($i < $l) { + $i += 50; // save iterations. 500 checks for a closing curly brace } + if ($l - $start_index <= $css_chunk_length || $i >= $l) { + $css_chunks[] = $this->str_slice($css, $start_index); + break; + } + if ($css[$i - 1] === '}' && $i - $start_index > $css_chunk_length) { + // If there are two ending curly braces }} separated or not by spaces, + // join them in the same chunk (i.e. @media blocks) + $next_chunk = substr($css, $i); + if (preg_match('/^\s*\}/', $next_chunk)) { + $i = $i + $this->index_of($next_chunk, '}') + 1; + } + + $css_chunks[] = $this->str_slice($css, $start_index, $i); + $start_index = $i; + } + } + } + + // Minify each chunk + for ($i = 0, $n = count($css_chunks); $i < $n; $i++) { + $css_chunks[$i] = $this->minify($css_chunks[$i], $linebreak_pos); + // Keep the first @charset at-rule found + if (empty($charset) && preg_match($charset_regexp, $css_chunks[$i], $matches)) { + $charset = strtolower($matches[1]) . $matches[2]; + } + // Delete all @charset at-rules + $css_chunks[$i] = preg_replace($charset_regexp, '', $css_chunks[$i]); + } + + // Update the first chunk and push the charset to the top of the file. + $css_chunks[0] = $charset . $css_chunks[0]; + + return implode('', $css_chunks); + } + + /** + * Sets the memory limit for this script + * @param int|string $limit + */ + public function set_memory_limit($limit) + { + $this->memory_limit = $this->normalize_int($limit); + } + + /** + * Sets the maximum execution time for this script + * @param int|string $seconds + */ + public function set_max_execution_time($seconds) + { + $this->max_execution_time = (int) $seconds; + } + + /** + * Sets the PCRE backtrack limit for this script + * @param int $limit + */ + public function set_pcre_backtrack_limit($limit) + { + $this->pcre_backtrack_limit = (int) $limit; + } + + /** + * Sets the PCRE recursion limit for this script + * @param int $limit + */ + public function set_pcre_recursion_limit($limit) + { + $this->pcre_recursion_limit = (int) $limit; + } + + /** + * Try to configure PHP to use at least the suggested minimum settings + */ + private function do_raise_php_limits() + { + $php_limits = array( + 'memory_limit' => $this->memory_limit, + 'max_execution_time' => $this->max_execution_time, + 'pcre.backtrack_limit' => $this->pcre_backtrack_limit, + 'pcre.recursion_limit' => $this->pcre_recursion_limit + ); + + // If current settings are higher respect them. + foreach ($php_limits as $name => $suggested) { + $current = $this->normalize_int(ini_get($name)); + // memory_limit exception: allow -1 for "no memory limit". + if ($current > -1 && ($suggested == -1 || $current < $suggested)) { + ini_set($name, $suggested); + } + } + } + + /** + * Does bulk of the minification + * @param string $css + * @param int|bool $linebreak_pos + * @return string + */ + private function minify($css, $linebreak_pos) + { + // strings are safe, now wrestle the comments + for ($i = 0, $max = count($this->comments); $i < $max; $i++) { + + $token = $this->comments[$i]; + $placeholder = '/' . self::COMMENT . $i . '___/'; + + // ! in the first position of the comment means preserve + // so push to the preserved tokens keeping the ! + if (substr($token, 0, 1) === '!') { + $this->preserved_tokens[] = $token; + $token_tring = self::TOKEN . (count($this->preserved_tokens) - 1) . '___'; + $css = preg_replace($placeholder, $token_tring, $css, 1); + // Preserve new lines for /*! important comments + $css = preg_replace('/\s*[\n\r\f]+\s*(\/\*'. $token_tring .')/S', self::NL.'$1', $css); + $css = preg_replace('/('. $token_tring .'\*\/)\s*[\n\r\f]+\s*/', '$1'.self::NL, $css); + continue; + } + + // \ in the last position looks like hack for Mac/IE5 + // shorten that to /*\*/ and the next one to /**/ + if (substr($token, (strlen($token) - 1), 1) === '\\') { + $this->preserved_tokens[] = '\\'; + $css = preg_replace($placeholder, self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1); + $i = $i + 1; // attn: advancing the loop + $this->preserved_tokens[] = ''; + $css = preg_replace('/' . self::COMMENT . $i . '___/', self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1); + continue; + } + + // keep empty comments after child selectors (IE7 hack) + // e.g. html >/**/ body + if (strlen($token) === 0) { + $start_index = $this->index_of($css, $this->str_slice($placeholder, 1, -1)); + if ($start_index > 2) { + if (substr($css, $start_index - 3, 1) === '>') { + $this->preserved_tokens[] = ''; + $css = preg_replace($placeholder, self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1); + } + } + } + + // in all other cases kill the comment + $css = preg_replace('/\/\*' . $this->str_slice($placeholder, 1, -1) . '\*\//', '', $css, 1); + } + + + // Normalize all whitespace strings to single spaces. Easier to work with that way. + $css = preg_replace('/\s+/', ' ', $css); + + // Shorten & preserve calculations calc(...) since spaces are important + $css = preg_replace_callback('/calc(\(((?:[^\(\)]+|(?1))*)\))/i', array($this, 'replace_calc'), $css); + + // Replace positive sign from numbers preceded by : or a white-space before the leading space is removed + // +1.2em to 1.2em, +.8px to .8px, +2% to 2% + $css = preg_replace('/((? -9.0 to -9 + $css = preg_replace('/((?\+\(\)\]\~\=,])/', '$1', $css); + + // Restore spaces for !important + $css = preg_replace('/\!important/i', ' !important', $css); + + // bring back the colon + $css = preg_replace('/' . self::CLASSCOLON . '/', ':', $css); + + // retain space for special IE6 cases + $css = preg_replace_callback('/\:first\-(line|letter)(\{|,)/i', array($this, 'lowercase_pseudo_first'), $css); + + // no space after the end of a preserved comment + $css = preg_replace('/\*\/ /', '*/', $css); + + // lowercase some popular @directives + $css = preg_replace_callback('/@(font-face|import|(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?keyframe|media|page|namespace)/i', array($this, 'lowercase_directives'), $css); + + // lowercase some more common pseudo-elements + $css = preg_replace_callback('/:(active|after|before|checked|disabled|empty|enabled|first-(?:child|of-type)|focus|hover|last-(?:child|of-type)|link|only-(?:child|of-type)|root|:selection|target|visited)/i', array($this, 'lowercase_pseudo_elements'), $css); + + // lowercase some more common functions + $css = preg_replace_callback('/:(lang|not|nth-child|nth-last-child|nth-last-of-type|nth-of-type|(?:-(?:moz|webkit)-)?any)\(/i', array($this, 'lowercase_common_functions'), $css); + + // lower case some common function that can be values + // NOTE: rgb() isn't useful as we replace with #hex later, as well as and() is already done for us + $css = preg_replace_callback('/([:,\( ]\s*)(attr|color-stop|from|rgba|to|url|(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?(?:calc|max|min|(?:repeating-)?(?:linear|radial)-gradient)|-webkit-gradient)/iS', array($this, 'lowercase_common_functions_values'), $css); + + // Put the space back in some cases, to support stuff like + // @media screen and (-webkit-min-device-pixel-ratio:0){ + $css = preg_replace('/\band\(/i', 'and (', $css); + + // Remove the spaces after the things that should not have spaces after them. + $css = preg_replace('/([\!\{\}\:;\>\+\(\[\~\=,])\s+/S', '$1', $css); + + // remove unnecessary semicolons + $css = preg_replace('/;+\}/', '}', $css); + + // Fix for issue: #2528146 + // Restore semicolon if the last property is prefixed with a `*` (lte IE7 hack) + // to avoid issues on Symbian S60 3.x browsers. + $css = preg_replace('/(\*[a-z0-9\-]+\s*\:[^;\}]+)(\})/', '$1;$2', $css); + + // Replace 0 length units 0(px,em,%) with 0. + $css = preg_replace('/(^|[^0-9])(?:0?\.)?0(?:em|ex|ch|rem|vw|vh|vm|vmin|cm|mm|in|px|pt|pc|%|deg|g?rad|m?s|k?hz)/iS', '${1}0', $css); + + // Replace 0 0; or 0 0 0; or 0 0 0 0; with 0. + $css = preg_replace('/\:0(?: 0){1,3}(;|\}| \!)/', ':0$1', $css); + + // Fix for issue: #2528142 + // Replace text-shadow:0; with text-shadow:0 0 0; + $css = preg_replace('/(text-shadow\:0)(;|\}| \!)/i', '$1 0 0$2', $css); + + // Replace background-position:0; with background-position:0 0; + // same for transform-origin + // Changing -webkit-mask-position: 0 0 to just a single 0 will result in the second parameter defaulting to 50% (center) + $css = preg_replace('/(background\-position|webkit-mask-position|(?:webkit|moz|o|ms|)\-?transform\-origin)\:0(;|\}| \!)/iS', '$1:0 0$2', $css); + + // Shorten colors from rgb(51,102,153) to #336699, rgb(100%,0%,0%) to #ff0000 (sRGB color space) + // Shorten colors from hsl(0, 100%, 50%) to #ff0000 (sRGB color space) + // This makes it more likely that it'll get further compressed in the next step. + $css = preg_replace_callback('/rgb\s*\(\s*([0-9,\s\-\.\%]+)\s*\)(.{1})/i', array($this, 'rgb_to_hex'), $css); + $css = preg_replace_callback('/hsl\s*\(\s*([0-9,\s\-\.\%]+)\s*\)(.{1})/i', array($this, 'hsl_to_hex'), $css); + + // Shorten colors from #AABBCC to #ABC or short color name. + $css = $this->compress_hex_colors($css); + + // border: none to border:0, outline: none to outline:0 + $css = preg_replace('/(border\-?(?:top|right|bottom|left|)|outline)\:none(;|\}| \!)/iS', '$1:0$2', $css); + + // shorter opacity IE filter + $css = preg_replace('/progid\:DXImageTransform\.Microsoft\.Alpha\(Opacity\=/i', 'alpha(opacity=', $css); + + // Find a fraction that is used for Opera's -o-device-pixel-ratio query + // Add token to add the "\" back in later + $css = preg_replace('/\(([a-z\-]+):([0-9]+)\/([0-9]+)\)/i', '($1:$2'. self::QUERY_FRACTION .'$3)', $css); + + // Remove empty rules. + $css = preg_replace('/[^\};\{\/]+\{\}/S', '', $css); + + // Add "/" back to fix Opera -o-device-pixel-ratio query + $css = preg_replace('/'. self::QUERY_FRACTION .'/', '/', $css); + + // Some source control tools don't like it when files containing lines longer + // than, say 8000 characters, are checked in. The linebreak option is used in + // that case to split long lines after a specific column. + if ($linebreak_pos !== FALSE && (int) $linebreak_pos >= 0) { + $linebreak_pos = (int) $linebreak_pos; + $start_index = $i = 0; + while ($i < strlen($css)) { + $i++; + if ($css[$i - 1] === '}' && $i - $start_index > $linebreak_pos) { + $css = $this->str_slice($css, 0, $i) . "\n" . $this->str_slice($css, $i); + $start_index = $i; + } + } + } + + // Replace multiple semi-colons in a row by a single one + // See SF bug #1980989 + $css = preg_replace('/;;+/', ';', $css); + + // Restore new lines for /*! important comments + $css = preg_replace('/'. self::NL .'/', "\n", $css); + + // Lowercase all uppercase properties + $css = preg_replace_callback('/(\{|\;)([A-Z\-]+)(\:)/', array($this, 'lowercase_properties'), $css); + + // restore preserved comments and strings + for ($i = 0, $max = count($this->preserved_tokens); $i < $max; $i++) { + $css = preg_replace('/' . self::TOKEN . $i . '___/', $this->preserved_tokens[$i], $css, 1); + } + + // Trim the final string (for any leading or trailing white spaces) + return trim($css); + } + + /** + * Utility method to replace all data urls with tokens before we start + * compressing, to avoid performance issues running some of the subsequent + * regexes against large strings chunks. + * + * @param string $css + * @return string + */ + private function extract_data_urls($css) + { + // Leave data urls alone to increase parse performance. + $max_index = strlen($css) - 1; + $append_index = $index = $last_index = $offset = 0; + $sb = array(); + $pattern = '/url\(\s*(["\']?)data\:/i'; + + // Since we need to account for non-base64 data urls, we need to handle + // ' and ) being part of the data string. Hence switching to indexOf, + // to determine whether or not we have matching string terminators and + // handling sb appends directly, instead of using matcher.append* methods. + + while (preg_match($pattern, $css, $m, 0, $offset)) { + $index = $this->index_of($css, $m[0], $offset); + $last_index = $index + strlen($m[0]); + $start_index = $index + 4; // "url(".length() + $end_index = $last_index - 1; + $terminator = $m[1]; // ', " or empty (not quoted) + $found_terminator = FALSE; + + if (strlen($terminator) === 0) { + $terminator = ')'; + } + + while ($found_terminator === FALSE && $end_index+1 <= $max_index) { + $end_index = $this->index_of($css, $terminator, $end_index + 1); + + // endIndex == 0 doesn't really apply here + if ($end_index > 0 && substr($css, $end_index - 1, 1) !== '\\') { + $found_terminator = TRUE; + if (')' != $terminator) { + $end_index = $this->index_of($css, ')', $end_index); + } + } + } + + // Enough searching, start moving stuff over to the buffer + $sb[] = $this->str_slice($css, $append_index, $index); + + if ($found_terminator) { + $token = $this->str_slice($css, $start_index, $end_index); + $token = preg_replace('/\s+/', '', $token); + $this->preserved_tokens[] = $token; + + $preserver = 'url(' . self::TOKEN . (count($this->preserved_tokens) - 1) . '___)'; + $sb[] = $preserver; + + $append_index = $end_index + 1; + } else { + // No end terminator found, re-add the whole match. Should we throw/warn here? + $sb[] = $this->str_slice($css, $index, $last_index); + $append_index = $last_index; + } + + $offset = $last_index; + } + + $sb[] = $this->str_slice($css, $append_index); + + return implode('', $sb); + } + + /** + * Utility method to compress hex color values of the form #AABBCC to #ABC or short color name. + * + * DOES NOT compress CSS ID selectors which match the above pattern (which would break things). + * e.g. #AddressForm { ... } + * + * DOES NOT compress IE filters, which have hex color values (which would break things). + * e.g. filter: chroma(color="#FFFFFF"); + * + * DOES NOT compress invalid hex values. + * e.g. background-color: #aabbccdd + * + * @param string $css + * @return string + */ + private function compress_hex_colors($css) + { + // Look for hex colors inside { ... } (to avoid IDs) and which don't have a =, or a " in front of them (to avoid filters) + $pattern = '/(\=\s*?["\']?)?#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])(\}|[^0-9a-f{][^{]*?\})/iS'; + $_index = $index = $last_index = $offset = 0; + $sb = array(); + // See: http://ajaxmin.codeplex.com/wikipage?title=CSS%20Colors + $short_safe = array( + '#808080' => 'gray', + '#008000' => 'green', + '#800000' => 'maroon', + '#000080' => 'navy', + '#808000' => 'olive', + '#ffa500' => 'orange', + '#800080' => 'purple', + '#c0c0c0' => 'silver', + '#008080' => 'teal', + '#f00' => 'red' + ); + + while (preg_match($pattern, $css, $m, 0, $offset)) { + $index = $this->index_of($css, $m[0], $offset); + $last_index = $index + strlen($m[0]); + $is_filter = $m[1] !== null && $m[1] !== ''; + + $sb[] = $this->str_slice($css, $_index, $index); + + if ($is_filter) { + // Restore, maintain case, otherwise filter will break + $sb[] = $m[1] . '#' . $m[2] . $m[3] . $m[4] . $m[5] . $m[6] . $m[7]; + } else { + if (strtolower($m[2]) == strtolower($m[3]) && + strtolower($m[4]) == strtolower($m[5]) && + strtolower($m[6]) == strtolower($m[7])) { + // Compress. + $hex = '#' . strtolower($m[3] . $m[5] . $m[7]); + } else { + // Non compressible color, restore but lower case. + $hex = '#' . strtolower($m[2] . $m[3] . $m[4] . $m[5] . $m[6] . $m[7]); + } + // replace Hex colors to short safe color names + $sb[] = array_key_exists($hex, $short_safe) ? $short_safe[$hex] : $hex; + } + + $_index = $offset = $last_index - strlen($m[8]); + } + + $sb[] = $this->str_slice($css, $_index); + + return implode('', $sb); + } + + /* CALLBACKS + * --------------------------------------------------------------------------------------------- + */ + + private function replace_string($matches) + { + $match = $matches[0]; + $quote = substr($match, 0, 1); + // Must use addcslashes in PHP to avoid parsing of backslashes + $match = addcslashes($this->str_slice($match, 1, -1), '\\'); + + // maybe the string contains a comment-like substring? + // one, maybe more? put'em back then + if (($pos = $this->index_of($match, self::COMMENT)) >= 0) { + for ($i = 0, $max = count($this->comments); $i < $max; $i++) { + $match = preg_replace('/' . self::COMMENT . $i . '___/', $this->comments[$i], $match, 1); + } + } + + // minify alpha opacity in filter strings + $match = preg_replace('/progid\:DXImageTransform\.Microsoft\.Alpha\(Opacity\=/i', 'alpha(opacity=', $match); + + $this->preserved_tokens[] = $match; + return $quote . self::TOKEN . (count($this->preserved_tokens) - 1) . '___' . $quote; + } + + private function replace_colon($matches) + { + return preg_replace('/\:/', self::CLASSCOLON, $matches[0]); + } + + private function replace_calc($matches) + { + $this->preserved_tokens[] = trim(preg_replace('/\s*([\*\/\(\),])\s*/', '$1', $matches[2])); + return 'calc('. self::TOKEN . (count($this->preserved_tokens) - 1) . '___' . ')'; + } + + private function rgb_to_hex($matches) + { + // Support for percentage values rgb(100%, 0%, 45%); + if ($this->index_of($matches[1], '%') >= 0){ + $rgbcolors = explode(',', str_replace('%', '', $matches[1])); + for ($i = 0; $i < count($rgbcolors); $i++) { + $rgbcolors[$i] = $this->round_number(floatval($rgbcolors[$i]) * 2.55); + } + } else { + $rgbcolors = explode(',', $matches[1]); + } + + // Values outside the sRGB color space should be clipped (0-255) + for ($i = 0; $i < count($rgbcolors); $i++) { + $rgbcolors[$i] = $this->clamp_number(intval($rgbcolors[$i], 10), 0, 255); + $rgbcolors[$i] = sprintf("%02x", $rgbcolors[$i]); + } + + // Fix for issue #2528093 + if (!preg_match('/[\s\,\);\}]/', $matches[2])){ + $matches[2] = ' ' . $matches[2]; + } + + return '#' . implode('', $rgbcolors) . $matches[2]; + } + + private function hsl_to_hex($matches) + { + $values = explode(',', str_replace('%', '', $matches[1])); + $h = floatval($values[0]); + $s = floatval($values[1]); + $l = floatval($values[2]); + + // Wrap and clamp, then fraction! + $h = ((($h % 360) + 360) % 360) / 360; + $s = $this->clamp_number($s, 0, 100) / 100; + $l = $this->clamp_number($l, 0, 100) / 100; + + if ($s == 0) { + $r = $g = $b = $this->round_number(255 * $l); + } else { + $v2 = $l < 0.5 ? $l * (1 + $s) : ($l + $s) - ($s * $l); + $v1 = (2 * $l) - $v2; + $r = $this->round_number(255 * $this->hue_to_rgb($v1, $v2, $h + (1/3))); + $g = $this->round_number(255 * $this->hue_to_rgb($v1, $v2, $h)); + $b = $this->round_number(255 * $this->hue_to_rgb($v1, $v2, $h - (1/3))); + } + + return $this->rgb_to_hex(array('', $r.','.$g.','.$b, $matches[2])); + } + + private function lowercase_pseudo_first($matches) + { + return ':first-'. strtolower($matches[1]) .' '. $matches[2]; + } + + private function lowercase_directives($matches) + { + return '@'. strtolower($matches[1]); + } + + private function lowercase_pseudo_elements($matches) + { + return ':'. strtolower($matches[1]); + } + + private function lowercase_common_functions($matches) + { + return ':'. strtolower($matches[1]) .'('; + } + + private function lowercase_common_functions_values($matches) + { + return $matches[1] . strtolower($matches[2]); + } + + private function lowercase_properties($matches) + { + return $matches[1].strtolower($matches[2]).$matches[3]; + } + + /* HELPERS + * --------------------------------------------------------------------------------------------- + */ + + private function hue_to_rgb($v1, $v2, $vh) + { + $vh = $vh < 0 ? $vh + 1 : ($vh > 1 ? $vh - 1 : $vh); + if ($vh * 6 < 1) return $v1 + ($v2 - $v1) * 6 * $vh; + if ($vh * 2 < 1) return $v2; + if ($vh * 3 < 2) return $v1 + ($v2 - $v1) * ((2/3) - $vh) * 6; + return $v1; + } + + private function round_number($n) + { + return intval(floor(floatval($n) + 0.5), 10); + } + + private function clamp_number($n, $min, $max) + { + return min(max($n, $min), $max); + } + + /** + * PHP port of Javascript's "indexOf" function for strings only + * Author: Tubal Martin http://blog.margenn.com + * + * @param string $haystack + * @param string $needle + * @param int $offset index (optional) + * @return int + */ + private function index_of($haystack, $needle, $offset = 0) + { + $index = strpos($haystack, $needle, $offset); + + return ($index !== FALSE) ? $index : -1; + } + + /** + * PHP port of Javascript's "slice" function for strings only + * Author: Tubal Martin http://blog.margenn.com + * Tests: http://margenn.com/tubal/str_slice/ + * + * @param string $str + * @param int $start index + * @param int|bool $end index (optional) + * @return string + */ + private function str_slice($str, $start = 0, $end = FALSE) + { + if ($end !== FALSE && ($start < 0 || $end <= 0)) { + $max = strlen($str); + + if ($start < 0) { + if (($start = $max + $start) < 0) { + return ''; + } + } + + if ($end < 0) { + if (($end = $max + $end) < 0) { + return ''; + } + } + + if ($end <= $start) { + return ''; + } + } + + $slice = ($end === FALSE) ? substr($str, $start) : substr($str, $start, $end - $start); + return ($slice === FALSE) ? '' : $slice; + } + + /** + * Convert strings like "64M" or "30" to int values + * @param mixed $size + * @return int + */ + private function normalize_int($size) + { + if (is_string($size)) { + switch (substr($size, -1)) { + case 'M': case 'm': return $size * 1048576; + case 'K': case 'k': return $size * 1024; + case 'G': case 'g': return $size * 1073741824; + } + } + + return (int) $size; + } +} \ No newline at end of file diff --git a/lib/minify/lib/DooDigestAuth.php b/lib/minify/lib/DooDigestAuth.php new file mode 100644 index 00000000000..69bc4ed4db4 --- /dev/null +++ b/lib/minify/lib/DooDigestAuth.php @@ -0,0 +1,121 @@ + + * @link http://www.doophp.com/ + * @copyright Copyright © 2009 Leng Sheng Hong + * @license http://www.doophp.com/license + */ + +/** + * Handles HTTP digest authentication + * + *

HTTP digest authentication can be used with the URI router. + * HTTP digest is much more recommended over the use of HTTP Basic auth which doesn't provide any encryption. + * If you are running PHP on Apache in CGI/FastCGI mode, you would need to + * add the following line to your .htaccess for digest auth to work correctly.

+ * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L] + * + *

This class is tested under Apache 2.2 and Cherokee web server. It should work in both mod_php and cgi mode.

+ * + * @author Leng Sheng Hong + * @version $Id: DooDigestAuth.php 1000 2009-07-7 18:27:22 + * @package doo.auth + * @since 1.0 + */ +class DooDigestAuth{ + + /** + * Authenticate against a list of username and passwords. + * + *

HTTP Digest Authentication doesn't work with PHP in CGI mode, + * you have to add this into your .htaccess RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

+ * + * @param string $realm Name of the authentication session + * @param array $users An assoc array of username and password: array('uname1'=>'pwd1', 'uname2'=>'pwd2') + * @param string $fail_msg Message to be displayed if the User cancel the login + * @param string $fail_url URL to be redirect if the User cancel the login + * @return string The username if login success. + */ + public static function http_auth($realm, $users, $fail_msg=NULL, $fail_url=NULL){ + $realm = "Restricted area - $realm"; + + //user => password + //$users = array('admin' => '1234', 'guest' => 'guest'); + if(!empty($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) && strpos($_SERVER['REDIRECT_HTTP_AUTHORIZATION'], 'Digest')===0){ + $_SERVER['PHP_AUTH_DIGEST'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; + } + + if (empty($_SERVER['PHP_AUTH_DIGEST'])) { + header('WWW-Authenticate: Digest realm="'.$realm. + '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"'); + header('HTTP/1.1 401 Unauthorized'); + if($fail_msg!=NULL) + die($fail_msg); + if($fail_url!=NULL) + die(""); + exit; + } + + // analyze the PHP_AUTH_DIGEST variable + if (!($data = self::http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) || !isset($users[$data['username']])){ + header('WWW-Authenticate: Digest realm="'.$realm. + '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"'); + header('HTTP/1.1 401 Unauthorized'); + if($fail_msg!=NULL) + die($fail_msg); + if($fail_url!=NULL) + die(""); + exit; + } + + // generate the valid response + $A1 = md5($data['username'] . ':' . $realm . ':' . $users[$data['username']]); + $A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']); + $valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2); + + if ($data['response'] != $valid_response){ + header('HTTP/1.1 401 Unauthorized'); + header('WWW-Authenticate: Digest realm="'.$realm. + '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"'); + if($fail_msg!=NULL) + die($fail_msg); + if($fail_url!=NULL) + die(""); + exit; + } + + // ok, valid username & password + return $data['username']; + } + + /** + * Method to parse the http auth header, works with IE. + * + * Internet Explorer returns a qop="xxxxxxxxxxx" in the header instead of qop=xxxxxxxxxxx as most browsers do. + * + * @param string $txt header string to parse + * @return array An assoc array of the digest auth session + */ + private static function http_digest_parse($txt) + { + $res = preg_match("/username=\"([^\"]+)\"/i", $txt, $match); + $data['username'] = (isset($match[1]))?$match[1]:null; + $res = preg_match('/nonce=\"([^\"]+)\"/i', $txt, $match); + $data['nonce'] = $match[1]; + $res = preg_match('/nc=([0-9]+)/i', $txt, $match); + $data['nc'] = $match[1]; + $res = preg_match('/cnonce=\"([^\"]+)\"/i', $txt, $match); + $data['cnonce'] = $match[1]; + $res = preg_match('/qop=([^,]+)/i', $txt, $match); + $data['qop'] = str_replace('"','',$match[1]); + $res = preg_match('/uri=\"([^\"]+)\"/i', $txt, $match); + $data['uri'] = $match[1]; + $res = preg_match('/response=\"([^\"]+)\"/i', $txt, $match); + $data['response'] = $match[1]; + return $data; + } + + +} diff --git a/lib/minify/lib/Minify.php b/lib/minify/lib/Minify.php index 9634f221fbd..cf05af9d2df 100644 --- a/lib/minify/lib/Minify.php +++ b/lib/minify/lib/Minify.php @@ -3,11 +3,6 @@ * Class Minify * @package Minify */ - -/** - * Minify_Source - */ -require_once 'Minify/Source.php'; /** * Minify - Combines, minifies, and caches JavaScript and CSS files on demand. @@ -29,7 +24,7 @@ require_once 'Minify/Source.php'; */ class Minify { - const VERSION = '2.1.5'; + const VERSION = '2.1.7'; const TYPE_CSS = 'text/css'; const TYPE_HTML = 'text/html'; // there is some debate over the ideal JS Content-Type, but this is the @@ -85,7 +80,6 @@ class Minify { public static function setCache($cache = '', $fileLocking = true) { if (is_string($cache)) { - require_once 'Minify/Cache/File.php'; self::$_cache = new Minify_Cache_File($cache, $fileLocking); } else { self::$_cache = $cache; @@ -161,9 +155,11 @@ class Minify { * * @param array $options controller/serve options * - * @return mixed null, or, if the 'quiet' option is set to true, an array + * @return null|array if the 'quiet' option is set to true, an array * with keys "success" (bool), "statusCode" (int), "content" (string), and * "headers" (array). + * + * @throws Exception */ public static function serve($controller, $options = array()) { @@ -174,10 +170,6 @@ class Minify { if (is_string($controller)) { // make $controller into object $class = 'Minify_Controller_' . $controller; - if (! class_exists($class, false)) { - require_once "Minify/Controller/" - . str_replace('_', '/', $controller) . ".php"; - } $controller = new $class(); /* @var Minify_Controller_Base $controller */ } @@ -219,8 +211,7 @@ class Minify { $contentEncoding = self::$_options['encodeMethod']; } else { // sniff request header - require_once 'HTTP/Encoder.php'; - // depending on what the client accepts, $contentEncoding may be + // 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(self::$_options['encodeMethod'], $contentEncoding) = HTTP_Encoder::getAcceptedEncoding(false, false); @@ -231,7 +222,6 @@ class Minify { } // check client cache - require_once 'HTTP/ConditionalGet.php'; $cgOptions = array( 'lastModifiedTime' => self::$_options['lastModifiedTime'] ,'isPublic' => self::$_options['isPublic'] @@ -300,7 +290,7 @@ class Minify { throw $e; } self::$_cache->store($cacheId, $content); - if (function_exists('gzencode')) { + if (function_exists('gzencode') && self::$_options['encodeMethod']) { self::$_cache->store($cacheId . '.gz', gzencode($content, self::$_options['encodeLevel'])); } } @@ -451,7 +441,7 @@ class Minify { /** * Set up sources to use Minify_Lines * - * @param array $sources Minify_Source instances + * @param Minify_Source[] $sources Minify_Source instances */ protected static function _setupDebug($sources) { @@ -468,6 +458,8 @@ class Minify { * Combines sources and minifies the result. * * @return string + * + * @throws Exception */ protected static function _combineMinify() { @@ -526,7 +518,6 @@ class Minify { $imploded = implode($implodeSeparator, $groupToProcessTogether); $groupToProcessTogether = array(); if ($lastMinifier) { - self::$_controller->loadMinifier($lastMinifier); try { $content[] = call_user_func($lastMinifier, $imploded, $lastOptions); } catch (Exception $e) { diff --git a/lib/minify/lib/Minify/Build.php b/lib/minify/lib/Minify/Build.php index e625165e5a2..1185fbe78b2 100644 --- a/lib/minify/lib/Minify/Build.php +++ b/lib/minify/lib/Minify/Build.php @@ -4,8 +4,6 @@ * @package Minify */ -require_once 'Minify/Source.php'; - /** * Maintain a single last modification time for a group of Minify sources to * allow use of far off Expires headers in Minify. diff --git a/lib/minify/lib/Minify/CSS.php b/lib/minify/lib/Minify/CSS.php index 49882e9d2d5..32414551d9f 100644 --- a/lib/minify/lib/Minify/CSS.php +++ b/lib/minify/lib/Minify/CSS.php @@ -56,6 +56,7 @@ class Minify_CSS { public static function minify($css, $options = array()) { $options = array_merge(array( + 'compress' => true, 'removeCharsets' => true, 'preserveComments' => true, 'currentDir' => null, @@ -67,21 +68,20 @@ class Minify_CSS { if ($options['removeCharsets']) { $css = preg_replace('/@charset[^;]+;\\s*/', '', $css); } - require_once 'Minify/CSS/Compressor.php'; - if (! $options['preserveComments']) { - $css = Minify_CSS_Compressor::process($css, $options); - } else { - require_once 'Minify/CommentPreserver.php'; - $css = Minify_CommentPreserver::process( - $css - ,array('Minify_CSS_Compressor', 'process') - ,array($options) - ); + 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) + ); + } } if (! $options['currentDir'] && ! $options['prependRelativePath']) { return $css; } - require_once 'Minify/CSS/UriRewriter.php'; if ($options['currentDir']) { return Minify_CSS_UriRewriter::rewrite( $css diff --git a/lib/minify/lib/Minify/Cache/File.php b/lib/minify/lib/Minify/Cache/File.php index 3fa6b18fcb2..27b424a6aeb 100644 --- a/lib/minify/lib/Minify/Cache/File.php +++ b/lib/minify/lib/Minify/Cache/File.php @@ -186,7 +186,6 @@ class Minify_Cache_File { */ protected function _log($msg) { - require_once 'Minify/Logger.php'; Minify_Logger::log($msg); } diff --git a/lib/minify/lib/Minify/Cache/XCache.php b/lib/minify/lib/Minify/Cache/XCache.php new file mode 100644 index 00000000000..3039deddcb7 --- /dev/null +++ b/lib/minify/lib/Minify/Cache/XCache.php @@ -0,0 +1,126 @@ + + * Minify::setCache(new Minify_Cache_XCache()); + * + * + * @package Minify + * @author Elan Ruusamäe + **/ +class Minify_Cache_XCache { + + /** + * Create a Minify_Cache_XCache object, to be passed to + * Minify::setCache(). + * + * @param int $expire seconds until expiration (default = 0 + * meaning the item will not get an expiration date) + */ + public function __construct($expire = 0) + { + $this->_exp = $expire; + } + + /** + * Write data to cache. + * + * @param string $id cache id + * @param string $data + * @return bool success + */ + public function store($id, $data) + { + return xcache_set($id, "{$_SERVER['REQUEST_TIME']}|{$data}", $this->_exp); + } + + /** + * Get the size of a cache entry + * + * @param string $id cache id + * @return int size in bytes + */ + public function getSize($id) + { + if (! $this->_fetch($id)) { + return false; + } + return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) + ? mb_strlen($this->_data, '8bit') + : strlen($this->_data); + } + + /** + * Does a valid cache entry exist? + * + * @param string $id cache id + * @param int $srcMtime mtime of the original source file(s) + * @return bool exists + */ + public function isValid($id, $srcMtime) + { + return ($this->_fetch($id) && ($this->_lm >= $srcMtime)); + } + + /** + * Send the cached content to output + * + * @param string $id cache id + */ + public function display($id) + { + echo $this->_fetch($id) + ? $this->_data + : ''; + } + + /** + * Fetch the cached content + * + * @param string $id cache id + * @return string + */ + public function fetch($id) + { + return $this->_fetch($id) + ? $this->_data + : ''; + } + + private $_exp = null; + + // cache of most recently fetched id + private $_lm = null; + private $_data = null; + private $_id = null; + + /** + * Fetch data and timestamp from xcache, store in instance + * + * @param string $id + * @return bool success + */ + private function _fetch($id) + { + 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; + return true; + } +} diff --git a/lib/minify/lib/Minify/ClosureCompiler.php b/lib/minify/lib/Minify/ClosureCompiler.php new file mode 100644 index 00000000000..7856097de5c --- /dev/null +++ b/lib/minify/lib/Minify/ClosureCompiler.php @@ -0,0 +1,123 @@ + + * Minify_ClosureCompiler::$jarFile = '/path/to/closure-compiler-20120123.jar'; + * Minify_ClosureCompiler::$tempDir = '/tmp'; + * $code = Minify_ClosureCompiler::minify( + * $code, + * array('compilation_level' => 'SIMPLE_OPTIMIZATIONS') + * ); + * + * --compilation_level WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS + * + * + * + * @todo unit tests, $options docs + * @todo more options support (or should just passthru them all?) + * + * @package Minify + * @author Stephen Clay + * @author Elan Ruusamäe + */ +class Minify_ClosureCompiler { + + /** + * Filepath of the Closure Compiler jar file. This must be set before + * calling minifyJs(). + * + * @var string + */ + public static $jarFile = null; + + /** + * Writable temp directory. This must be set before calling minifyJs(). + * + * @var string + */ + public static $tempDir = null; + + /** + * Filepath of "java" executable (may be needed if not in shell's PATH) + * + * @var string + */ + public static $javaExecutable = 'java'; + + /** + * Minify a Javascript string + * + * @param string $js + * + * @param array $options (verbose is ignored) + * + * @see https://code.google.com/p/closure-compiler/source/browse/trunk/README + * + * @return string + */ + public static function minify($js, $options = array()) + { + self::_prepare(); + if (! ($tmpFile = tempnam(self::$tempDir, 'cc_'))) { + throw new Exception('Minify_ClosureCompiler : could not create temp file.'); + } + file_put_contents($tmpFile, $js); + exec(self::_getCmd($options, $tmpFile), $output, $result_code); + unlink($tmpFile); + if ($result_code != 0) { + throw new Exception('Minify_ClosureCompiler : Closure Compiler execution failed.'); + } + return implode("\n", $output); + } + + private static function _getCmd($userOptions, $tmpFile) + { + $o = array_merge( + array( + 'charset' => 'utf-8', + 'compilation_level' => 'SIMPLE_OPTIMIZATIONS', + ), + $userOptions + ); + $cmd = self::$javaExecutable . ' -jar ' . escapeshellarg(self::$jarFile) + . (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset']) + ? " --charset {$o['charset']}" + : ''); + + foreach (array('compilation_level') as $opt) { + if ($o[$opt]) { + $cmd .= " --{$opt} ". escapeshellarg($o[$opt]); + } + } + return $cmd . ' ' . escapeshellarg($tmpFile); + } + + private static function _prepare() + { + if (! is_file(self::$jarFile)) { + throw new Exception('Minify_ClosureCompiler : $jarFile('.self::$jarFile.') is not a valid file.'); + } + if (! is_readable(self::$jarFile)) { + throw new Exception('Minify_ClosureCompiler : $jarFile('.self::$jarFile.') is not readable.'); + } + if (! is_dir(self::$tempDir)) { + throw new Exception('Minify_ClosureCompiler : $tempDir('.self::$tempDir.') is not a valid direcotry.'); + } + if (! is_writable(self::$tempDir)) { + throw new Exception('Minify_ClosureCompiler : $tempDir('.self::$tempDir.') is not writable.'); + } + } +} + +/* vim:ts=4:sw=4:et */ diff --git a/lib/minify/lib/Minify/Controller/Base.php b/lib/minify/lib/Minify/Controller/Base.php index 240b54495b8..5a86329033e 100644 --- a/lib/minify/lib/Minify/Controller/Base.php +++ b/lib/minify/lib/Minify/Controller/Base.php @@ -78,33 +78,6 @@ abstract class Minify_Controller_Base { return $ret; } - /** - * Load any code necessary to execute the given minifier callback. - * - * The controller is responsible for loading minification code on demand - * via this method. This built-in function will only load classes for - * static method callbacks where the class isn't already defined. It uses - * the PEAR convention, so, given array('Jimmy_Minifier', 'minCss'), this - * function will include 'Jimmy/Minifier.php'. - * - * If you need code loaded on demand and this doesn't suit you, you'll need - * to override this function in your subclass. - * @see Minify_Controller_Page::loadMinifier() - * - * @param callback $minifierCallback callback of minifier function - * - * @return null - */ - public function loadMinifier($minifierCallback) - { - if (is_array($minifierCallback) - && is_string($minifierCallback[0]) - && !class_exists($minifierCallback[0], false)) { - - require str_replace('_', '/', $minifierCallback[0]) . '.php'; - } - } - /** * Is a user-given file within an allowable directory, existing, * and having an extension js/css/html/txt ? @@ -244,7 +217,6 @@ abstract class Minify_Controller_Base { * @return null */ public function log($msg) { - require_once 'Minify/Logger.php'; Minify_Logger::log($msg); } } diff --git a/lib/minify/lib/Minify/Controller/Files.php b/lib/minify/lib/Minify/Controller/Files.php index 83f028adf74..f084cd07eb6 100644 --- a/lib/minify/lib/Minify/Controller/Files.php +++ b/lib/minify/lib/Minify/Controller/Files.php @@ -4,8 +4,6 @@ * @package Minify */ -require_once 'Minify/Controller/Base.php'; - /** * Controller class for minifying a set of files * diff --git a/lib/minify/lib/Minify/Controller/Groups.php b/lib/minify/lib/Minify/Controller/Groups.php index 2d4e43b2f7b..c4c25db120b 100644 --- a/lib/minify/lib/Minify/Controller/Groups.php +++ b/lib/minify/lib/Minify/Controller/Groups.php @@ -4,8 +4,6 @@ * @package Minify */ -require_once 'Minify/Controller/Base.php'; - /** * Controller class for serving predetermined groups of minimized sets, selected * by PATH_INFO diff --git a/lib/minify/lib/Minify/Controller/MinApp.php b/lib/minify/lib/Minify/Controller/MinApp.php index d47c60d9f78..6943ee6bf8f 100644 --- a/lib/minify/lib/Minify/Controller/MinApp.php +++ b/lib/minify/lib/Minify/Controller/MinApp.php @@ -4,8 +4,6 @@ * @package Minify */ -require_once 'Minify/Controller/Base.php'; - /** * Controller class for requests to /min/index.php * @@ -22,6 +20,13 @@ class Minify_Controller_MinApp extends Minify_Controller_Base { * @return array Minify options */ public function setupSources($options) { + // PHP insecure by default: realpath() and other FS functions can't handle null bytes. + foreach (array('g', 'b', 'f') as $key) { + if (isset($_GET[$key])) { + $_GET[$key] = str_replace("\x00", '', (string)$_GET[$key]); + } + } + // filter controller options $cOptions = array_merge( array( @@ -36,7 +41,6 @@ class Minify_Controller_MinApp extends Minify_Controller_Base { $sources = array(); $this->selectionId = ''; $firstMissingResource = null; - if (isset($_GET['g'])) { // add group(s) $this->selectionId .= 'g=' . $_GET['g']; @@ -195,9 +199,12 @@ class Minify_Controller_MinApp extends Minify_Controller_Base { protected function _getFileSource($file, $cOptions) { $spec['filepath'] = $file; - if ($cOptions['noMinPattern'] - && preg_match($cOptions['noMinPattern'], basename($file))) { - $spec['minifier'] = ''; + if ($cOptions['noMinPattern'] && preg_match($cOptions['noMinPattern'], basename($file))) { + if (preg_match('~\.css$~i', $file)) { + $spec['minifyOptions']['compress'] = false; + } else { + $spec['minifier'] = ''; + } } return new Minify_Source($spec); } diff --git a/lib/minify/lib/Minify/Controller/Page.php b/lib/minify/lib/Minify/Controller/Page.php index de471e1248e..1095fb46fe2 100644 --- a/lib/minify/lib/Minify/Controller/Page.php +++ b/lib/minify/lib/Minify/Controller/Page.php @@ -4,8 +4,6 @@ * @package Minify */ -require_once 'Minify/Controller/Base.php'; - /** * Controller class for serving a single HTML page * @@ -59,7 +57,6 @@ class Minify_Controller_Page extends Minify_Controller_Base { 'cssMinifier' => array('Minify_CSS', 'minify') ,'jsMinifier' => array('JSMin', 'minify') ); - $this->_loadCssJsMinifiers = true; unset($options['minifyAll']); } $this->sources[] = new Minify_Source($sourceSpec); @@ -67,21 +64,5 @@ class Minify_Controller_Page extends Minify_Controller_Base { $options['contentType'] = Minify::TYPE_HTML; return $options; } - - protected $_loadCssJsMinifiers = false; - - /** - * @see Minify_Controller_Base::loadMinifier() - */ - public function loadMinifier($minifierCallback) - { - if ($this->_loadCssJsMinifiers) { - // Minify will not call for these so we must manually load - // them when Minify/HTML.php is called for. - require_once 'Minify/CSS.php'; - require_once 'JSMin.php'; - } - parent::loadMinifier($minifierCallback); // load Minify/HTML.php - } } diff --git a/lib/minify/lib/Minify/Controller/Version1.php b/lib/minify/lib/Minify/Controller/Version1.php index 5279d36dd1a..91fcf61448a 100644 --- a/lib/minify/lib/Minify/Controller/Version1.php +++ b/lib/minify/lib/Minify/Controller/Version1.php @@ -4,8 +4,6 @@ * @package Minify */ -require_once 'Minify/Controller/Base.php'; - /** * Controller class for emulating version 1 of minify.php (mostly a proof-of-concept) * @@ -26,6 +24,11 @@ class Minify_Controller_Version1 extends Minify_Controller_Base { * */ public function setupSources($options) { + // PHP insecure by default: realpath() and other FS functions can't handle null bytes. + if (isset($_GET['files'])) { + $_GET['files'] = str_replace("\x00", '', (string)$_GET['files']); + } + self::_setupDefines(); if (MINIFY_USE_CACHE) { $cacheDir = defined('MINIFY_CACHE_DIR') @@ -51,8 +54,7 @@ class Minify_Controller_Version1 extends Minify_Controller_Base { ) { return $options; } - $extension = $m[1]; - + $files = explode(',', $_GET['files']); if (count($files) > MINIFY_MAX_FILES) { return $options; @@ -63,7 +65,6 @@ class Minify_Controller_Version1 extends Minify_Controller_Base { . DIRECTORY_SEPARATOR; $prependAbsPaths = $_SERVER['DOCUMENT_ROOT']; - $sources = array(); $goodFiles = array(); $hasBadSource = false; diff --git a/lib/minify/lib/Minify/HTML.php b/lib/minify/lib/Minify/HTML.php index e9453ffb419..da356dd0d59 100644 --- a/lib/minify/lib/Minify/HTML.php +++ b/lib/minify/lib/Minify/HTML.php @@ -1,22 +1,26 @@ */ class Minify_HTML { + /** + * @var boolean + */ + protected $_jsCleanComments = true; /** * "Minify" an HTML page @@ -27,21 +31,21 @@ class Minify_HTML { * * 'cssMinifier' : (optional) callback function to process content of STYLE * elements. - * + * * 'jsMinifier' : (optional) callback function to process content of SCRIPT * elements. Note: the type attribute is ignored. - * + * * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If * unset, minify will sniff for an XHTML doctype. - * + * * @return string */ public static function minify($html, $options = array()) { - $min = new Minify_HTML($html, $options); + $min = new self($html, $options); return $min->process(); } - - + + /** * Create a minifier object * @@ -51,13 +55,15 @@ class Minify_HTML { * * 'cssMinifier' : (optional) callback function to process content of STYLE * elements. - * + * * 'jsMinifier' : (optional) callback function to process content of SCRIPT * elements. Note: the type attribute is ignored. - * + * + * 'jsCleanComments' : (optional) whether to remove HTML comments beginning and end of script block + * * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If * unset, minify will sniff for an XHTML doctype. - * + * * @return null */ public function __construct($html, $options = array()) @@ -72,9 +78,12 @@ class Minify_HTML { if (isset($options['jsMinifier'])) { $this->_jsMinifier = $options['jsMinifier']; } + if (isset($options['jsCleanComments'])) { + $this->_jsCleanComments = (bool)$options['jsCleanComments']; + } } - - + + /** * Minify the markeup given in the constructor * @@ -213,17 +222,19 @@ class Minify_HTML { // whitespace surrounding? preserve at least one space $ws1 = ($m[1] === '') ? '' : ' '; $ws2 = ($m[4] === '') ? '' : ' '; - + // remove HTML comments (and ending "//" if present) - $js = preg_replace('/(?:^\\s*\\s*$)/', '', $js); - + if ($this->_jsCleanComments) { + $js = preg_replace('/(?:^\\s*\\s*$)/', '', $js); + } + // remove CDATA section markers $js = $this->_removeCdata($js); // minify $minifier = $this->_jsMinifier ? $this->_jsMinifier - : 'trim'; + : 'trim'; $js = call_user_func($minifier, $js); return $this->_reservePlace($this->_needsCdata($js) diff --git a/lib/minify/lib/Minify/HTML/Helper.php b/lib/minify/lib/Minify/HTML/Helper.php index 807fdc9c197..f92ab854c1d 100644 --- a/lib/minify/lib/Minify/HTML/Helper.php +++ b/lib/minify/lib/Minify/HTML/Helper.php @@ -15,10 +15,10 @@ class Minify_HTML_Helper { public $minAppUri = '/min'; public $groupsConfigFile = ''; - /* + /** * Get an HTML-escaped Minify URI for a group or set of files * - * @param mixed $keyOrFiles a group key or array of filepaths/URIs + * @param string|array $keyOrFiles a group key or array of filepaths/URIs * @param array $opts options: * 'farExpires' : (default true) append a modified timestamp for cache revving * 'debug' : (default false) append debug flag @@ -51,8 +51,12 @@ class Minify_HTML_Helper { return htmlspecialchars($uri, ENT_QUOTES, $opts['charset']); } - /* + /** * Get non-HTML-escaped URI to minify the specified files + * + * @param bool $farExpires + * @param bool $debug + * @return string */ public function getRawUri($farExpires = true, $debug = false) { @@ -74,6 +78,12 @@ class Minify_HTML_Helper { return $path; } + /** + * Set the files that will comprise the URI we're building + * + * @param array $files + * @param bool $checkLastModified + */ public function setFiles($files, $checkLastModified = true) { $this->_groupKey = null; @@ -94,6 +104,12 @@ class Minify_HTML_Helper { $this->_filePaths = $files; } + /** + * Set the group of files that will comprise the URI we're building + * + * @param string $key + * @param bool $checkLastModified + */ public function setGroup($key, $checkLastModified = true) { $this->_groupKey = $key; @@ -103,13 +119,23 @@ class Minify_HTML_Helper { } if (is_file($this->groupsConfigFile)) { $gc = (require $this->groupsConfigFile); - if (isset($gc[$key])) { - $this->_lastModified = self::getLastModified($gc[$key]); + $keys = explode(',', $key); + foreach ($keys as $key) { + if (isset($gc[$key])) { + $this->_lastModified = self::getLastModified($gc[$key], $this->_lastModified); + } } } } } - + + /** + * Get the max(lastModified) of all files + * + * @param array|string $sources + * @param int $lastModified + * @return int + */ public static function getLastModified($sources, $lastModified = 0) { $max = $lastModified; @@ -142,13 +168,19 @@ class Minify_HTML_Helper { * @return mixed a common char or '' if any do not match */ protected static function _getCommonCharAtPos($arr, $pos) { - $l = count($arr); + if (!isset($arr[0][$pos])) { + return ''; + } $c = $arr[0][$pos]; - if ($c === '' || $l === 1) + $l = count($arr); + if ($l === 1) { return $c; - for ($i = 1; $i < $l; ++$i) - if ($arr[$i][$pos] !== $c) + } + for ($i = 1; $i < $l; ++$i) { + if ($arr[$i][$pos] !== $c) { return ''; + } + } return $c; } @@ -157,11 +189,11 @@ class Minify_HTML_Helper { * * @param array $paths root-relative URIs of files * @param string $minRoot root-relative URI of the "min" application + * @return string */ protected static function _getShortestUri($paths, $minRoot = '/min/') { $pos = 0; $base = ''; - $c; while (true) { $c = self::_getCommonCharAtPos($paths, $pos); if ($c === '') { diff --git a/lib/minify/lib/Minify/JS/ClosureCompiler.php b/lib/minify/lib/Minify/JS/ClosureCompiler.php index f4ed5bdb682..51f7cd1a961 100644 --- a/lib/minify/lib/Minify/JS/ClosureCompiler.php +++ b/lib/minify/lib/Minify/JS/ClosureCompiler.php @@ -69,7 +69,7 @@ class Minify_JS_ClosureCompiler { } return $response; } - + protected $_fallbackFunc = null; protected function _getResponse($postBody) @@ -79,7 +79,7 @@ class Minify_JS_ClosureCompiler { $contents = file_get_contents(self::URL, false, stream_context_create(array( 'http' => array( 'method' => 'POST', - 'header' => 'Content-type: application/x-www-form-urlencoded', + 'header' => "Content-type: application/x-www-form-urlencoded\r\nConnection: close\r\n", 'content' => $postBody, 'max_redirects' => 0, 'timeout' => 15, @@ -125,7 +125,6 @@ class Minify_JS_ClosureCompiler { */ protected function _fallback($js) { - require_once 'JSMin.php'; return JSMin::minify($js); } } diff --git a/lib/minify/lib/Minify/Lines.php b/lib/minify/lib/Minify/Lines.php index ca8afa1bc97..e999a6c2b5c 100644 --- a/lib/minify/lib/Minify/Lines.php +++ b/lib/minify/lib/Minify/Lines.php @@ -55,7 +55,11 @@ class Minify_Lines { $newLines = array(); while (null !== ($line = array_shift($lines))) { if (('' !== $id) && (0 == $i % 50)) { - array_push($newLines, '', "/* {$id} */", ''); + if ($inComment) { + array_push($newLines, '', "/* {$id} *|", ''); + } else { + array_push($newLines, '', "/* {$id} */", ''); + } } ++$i; $newLines[] = self::_addNote($line, $i, $inComment, $padTo); @@ -65,7 +69,6 @@ class Minify_Lines { // check for desired URI rewriting if (isset($options['currentDir'])) { - require_once 'Minify/CSS/UriRewriter.php'; Minify_CSS_UriRewriter::$debugText = ''; $content = Minify_CSS_UriRewriter::rewrite( $content @@ -93,6 +96,9 @@ class Minify_Lines { */ private static function _eolInComment($line, $inComment) { + // crude way to avoid things like // */ + $line = preg_replace('~//.*?(\\*/|/\\*).*~', '', $line); + while (strlen($line)) { $search = $inComment ? '*/' diff --git a/lib/minify/lib/Minify/Loader.php b/lib/minify/lib/Minify/Loader.php new file mode 100644 index 00000000000..0a225c056c0 --- /dev/null +++ b/lib/minify/lib/Minify/Loader.php @@ -0,0 +1,28 @@ + + */ +class Minify_Loader { + public function loadClass($class) + { + $file = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR; + $file .= strtr($class, "\\_", DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR) . '.php'; + if (is_readable($file)) { + require $file; + } + } + + static public function register() + { + $inst = new self(); + spl_autoload_register(array($inst, 'loadClass')); + } +} diff --git a/lib/minify/lib/Minify/YUICompressor.php b/lib/minify/lib/Minify/YUICompressor.php index c5bd8a1eea9..8e6bfcb0728 100644 --- a/lib/minify/lib/Minify/YUICompressor.php +++ b/lib/minify/lib/Minify/YUICompressor.php @@ -13,14 +13,17 @@ * Java environment. * * - * Minify_YUICompressor::$jarFile = '/path/to/yuicompressor-2.3.5.jar'; + * Minify_YUICompressor::$jarFile = '/path/to/yuicompressor-2.4.6.jar'; * Minify_YUICompressor::$tempDir = '/tmp'; * $code = Minify_YUICompressor::minifyJs( * $code * ,array('nomunge' => true, 'line-break' => 1000) * ); * - * + * + * Note: In case you run out stack (default is 512k), you may increase stack size in $options: + * array('stack-size' => '2048k') + * * @todo unit tests, $options docs * * @package Minify @@ -108,10 +111,15 @@ class Minify_YUICompressor { ,'nomunge' => false ,'preserve-semi' => false ,'disable-optimizations' => false + ,'stack-size' => '' ) ,$userOptions ); - $cmd = self::$javaExecutable . ' -jar ' . escapeshellarg(self::$jarFile) + $cmd = self::$javaExecutable + . (!empty($o['stack-size']) + ? ' -Xss' . $o['stack-size'] + : '') + . ' -jar ' . escapeshellarg(self::$jarFile) . " --type {$type}" . (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset']) ? " --charset {$o['charset']}" @@ -134,8 +142,8 @@ class Minify_YUICompressor { if (! is_file(self::$jarFile)) { throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not a valid file.'); } - if (! is_executable(self::$jarFile)) { - throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not executable.'); + if (! is_readable(self::$jarFile)) { + throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not readable.'); } if (! is_dir(self::$tempDir)) { throw new Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not a valid direcotry.'); diff --git a/lib/minify/lib/MrClay/Cli.php b/lib/minify/lib/MrClay/Cli.php index ac201582e4f..9aa8e06f29b 100644 --- a/lib/minify/lib/MrClay/Cli.php +++ b/lib/minify/lib/MrClay/Cli.php @@ -2,6 +2,9 @@ namespace MrClay; +use MrClay\Cli\Arg; +use InvalidArgumentException; + /** * Forms a front controller for a console app, handling and validating arguments (options) * @@ -51,7 +54,7 @@ class Cli { public $isHelpRequest = false; /** - * @var array of Cli\Arg + * @var Arg[] */ protected $_args = array(); @@ -80,8 +83,8 @@ class Cli { } /** - * @param Cli\Arg|string $letter - * @return Cli\Arg + * @param Arg|string $letter + * @return Arg */ public function addOptionalArg($letter) { @@ -89,8 +92,8 @@ class Cli { } /** - * @param Cli\Arg|string $letter - * @return Cli\Arg + * @param Arg|string $letter + * @return Arg */ public function addRequiredArg($letter) { @@ -100,17 +103,17 @@ class Cli { /** * @param string $letter * @param bool $required - * @param Cli\Arg|null $arg - * @return Cli\Arg - * @throws \InvalidArgumentException + * @param Arg|null $arg + * @return Arg + * @throws InvalidArgumentException */ - public function addArgument($letter, $required, Cli\Arg $arg = null) + public function addArgument($letter, $required, Arg $arg = null) { if (! preg_match('/^[a-zA-Z]$/', $letter)) { - throw new \InvalidArgumentException('$letter must be in [a-zA-z]'); + throw new InvalidArgumentException('$letter must be in [a-zA-Z]'); } if (! $arg) { - $arg = new Cli\Arg($required); + $arg = new Arg($required); } $this->_args[$letter] = $arg; return $arg; @@ -118,7 +121,7 @@ class Cli { /** * @param string $letter - * @return Cli\Arg|null + * @return Arg|null */ public function getArgument($letter) { @@ -143,7 +146,7 @@ class Cli { $lettersUsed = ''; foreach ($this->_args as $letter => $arg) { - /* @var Cli\Arg $arg */ + /* @var Arg $arg */ $options .= $letter; $lettersUsed .= $letter; @@ -159,7 +162,7 @@ class Cli { $this->debug['getopt_return'] = $o; foreach ($this->_args as $letter => $arg) { - /* @var Cli\Arg $arg */ + /* @var Arg $arg */ $this->values[$letter] = false; if (isset($o[$letter])) { if (is_bool($o[$letter])) { @@ -295,7 +298,7 @@ class Cli { { $r = "\n"; foreach ($this->_args as $letter => $arg) { - /* @var Cli\Arg $arg */ + /* @var Arg $arg */ $desc = $arg->getDescription(); $flag = " -$letter "; if ($arg->mayHaveValue) { diff --git a/lib/minify/lib/MrClay/Cli/Arg.php b/lib/minify/lib/MrClay/Cli/Arg.php index 81146a7f10e..5fa5932734e 100644 --- a/lib/minify/lib/MrClay/Cli/Arg.php +++ b/lib/minify/lib/MrClay/Cli/Arg.php @@ -2,6 +2,8 @@ namespace MrClay\Cli; +use BadMethodCallException; + /** * An argument for a CLI app. This specifies the argument, what values it expects and * how it's treated during validation. @@ -150,7 +152,7 @@ class Arg { * @param string $name * @param array $args * @return Arg - * @throws \BadMethodCallException + * @throws BadMethodCallException */ public function __call($name, array $args = array()) { @@ -160,7 +162,7 @@ class Arg { $this->spec['mustHaveValue'] = true; } } else { - throw new \BadMethodCallException('Method does not exist'); + throw new BadMethodCallException('Method does not exist'); } return $this; } diff --git a/lib/minify/readme_moodle.txt b/lib/minify/readme_moodle.txt index 9ba344d29da..05c03f11a74 100644 --- a/lib/minify/readme_moodle.txt +++ b/lib/minify/readme_moodle.txt @@ -1,13 +1,11 @@ -Description of Minify 2.1.5 import into Moodle +Description of Minify 2.1.7 import into Moodle Notes: - * Uses are required to add minify/lib to the include path - * We ever actually use things within minify/lib/* + * Do not use things within minify/lib/* Usage: - * /lib/javascript.php - * /theme/javascript.php - * /theme/styles.php + * js_minify() from /lib/jslib.php + * css_minify_css() from /lib/csslib.php Changes: * Removed index.php - Is an unused entry point program and could potentially diff --git a/lib/minify/utils.php b/lib/minify/utils.php index a18d7ad10b8..7cf930ac901 100644 --- a/lib/minify/utils.php +++ b/lib/minify/utils.php @@ -2,11 +2,18 @@ /** * Utility functions for generating URIs in HTML files * + * @warning These functions execute min/groupsConfig.php, sometimes multiple times. + * You must make sure that functions are not redefined, and if your use custom sources, + * you must require_once dirname(__FILE__) . '/lib/Minify/Source.php' so that + * class is available. + * * @package Minify */ -require_once dirname(__FILE__) . '/lib/Minify/HTML/Helper.php'; - +if (! class_exists('Minify_Loader', false)) { + require dirname(__FILE__) . '/lib/Minify/Loader.php'; + Minify_Loader::register(); +} /* * Get an HTML-escaped Minify URI for a group or set of files. By default, URIs diff --git a/lib/thirdpartylibs.xml b/lib/thirdpartylibs.xml index f79c6bbd15d..6ec4089fb9f 100644 --- a/lib/thirdpartylibs.xml +++ b/lib/thirdpartylibs.xml @@ -88,7 +88,7 @@ minify Minify BSD - 2.1.5 + 2.1.7