diff options
Diffstat (limited to 'system/core/Output.php')
-rw-r--r-- | system/core/Output.php | 267 |
1 files changed, 43 insertions, 224 deletions
diff --git a/system/core/Output.php b/system/core/Output.php index de07125ad..beac6b377 100644 --- a/system/core/Output.php +++ b/system/core/Output.php @@ -2,26 +2,37 @@ /** * CodeIgniter * - * An open source application development framework for PHP 5.2.4 or newer + * An open source application development framework for PHP * - * NOTICE OF LICENSE + * This content is released under the MIT License (MIT) * - * Licensed under the Open Software License version 3.0 + * Copyright (c) 2014 - 2015, British Columbia Institute of Technology * - * This source file is subject to the Open Software License (OSL 3.0) that is - * bundled with this package in the files license.txt / license.rst. It is - * also available through the world wide web at this URL: - * http://opensource.org/licenses/OSL-3.0 - * If you did not receive a copy of the license and are unable to obtain it - * through the world wide web, please send an email to - * licensing@ellislab.com so we can send you a copy immediately. + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: * - * @package CodeIgniter - * @author EllisLab Dev Team + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @package CodeIgniter + * @author EllisLab Dev Team * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) - * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) - * @link http://codeigniter.com - * @since Version 1.0 + * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/) + * @license http://opensource.org/licenses/MIT MIT License + * @link http://codeigniter.com + * @since Version 1.0.0 * @filesource */ defined('BASEPATH') OR exit('No direct script access allowed'); @@ -415,14 +426,6 @@ class CI_Output { // -------------------------------------------------------------------- - // Is minify requested? - if ($CFG->item('minify_output') === TRUE) - { - $output = $this->minify($output, $this->mime_type); - } - - // -------------------------------------------------------------------- - // Do we need to write a cache file? Only if the controller does not have its // own _output() method and we are not dealing with a cache file, which we // can determine by the existence of the $CI object above @@ -553,6 +556,11 @@ class CI_Output { .$CI->config->item('index_page') .$CI->uri->uri_string(); + if ($CI->config->item('cache_query_string') && ! empty($_SERVER['QUERY_STRING'])) + { + $uri .= '?'.$_SERVER['QUERY_STRING']; + } + $cache_path .= md5($uri); if ( ! $fp = @fopen($cache_path, 'w+b')) @@ -636,7 +644,13 @@ class CI_Output { $cache_path = ($CFG->item('cache_path') === '') ? APPPATH.'cache/' : $CFG->item('cache_path'); // Build the file path. The file name is an MD5 hash of the full URI - $uri = $CFG->item('base_url').$CFG->item('index_page').$URI->uri_string; + $uri = $CFG->item('base_url').$CFG->item('index_page').$URI->uri_string; + + if ($CFG->item('cache_query_string') && ! empty($_SERVER['QUERY_STRING'])) + { + $uri .= '?'.$_SERVER['QUERY_STRING']; + } + $filepath = $cache_path.md5($uri); if ( ! file_exists($filepath) OR ! $fp = @fopen($filepath, 'rb')) @@ -714,6 +728,11 @@ class CI_Output { if (empty($uri)) { $uri = $CI->uri->uri_string(); + + if ($CI->config->item('cache_query_string') && ! empty($_SERVER['QUERY_STRING'])) + { + $uri .= '?'.$_SERVER['QUERY_STRING']; + } } $cache_path .= md5($CI->config->item('base_url').$CI->config->item('index_page').$uri); @@ -757,206 +776,6 @@ class CI_Output { } } - // -------------------------------------------------------------------- - - /** - * Minify - * - * Reduce excessive size of HTML/CSS/JavaScript content. - * - * @param string $output Output to minify - * @param string $type Output content MIME type - * @return string Minified output - */ - public function minify($output, $type = 'text/html') - { - switch ($type) - { - case 'text/html': - - if (($size_before = strlen($output)) === 0) - { - return ''; - } - - // Find all the <pre>,<code>,<textarea>, and <javascript> tags - // We'll want to return them to this unprocessed state later. - preg_match_all('{<pre.+</pre>}msU', $output, $pres_clean); - preg_match_all('{<code.+</code>}msU', $output, $codes_clean); - preg_match_all('{<textarea.+</textarea>}msU', $output, $textareas_clean); - preg_match_all('{<script.+</script>}msU', $output, $javascript_clean); - - // Minify the CSS in all the <style> tags. - preg_match_all('{<style.+</style>}msU', $output, $style_clean); - foreach ($style_clean[0] as $s) - { - $output = str_replace($s, $this->_minify_js_css($s, 'css', TRUE), $output); - } - - // Minify the javascript in <script> tags. - foreach ($javascript_clean[0] as $s) - { - $javascript_mini[] = $this->_minify_js_css($s, 'js', TRUE); - } - - // Replace multiple spaces with a single space. - $output = preg_replace('!\s{2,}!', ' ', $output); - - // Remove comments (non-MSIE conditionals) - $output = preg_replace('{\s*<!--[^\[<>].*(?<!!)-->\s*}msU', '', $output); - - // Remove spaces around block-level elements. - $output = preg_replace('/\s*(<\/?(html|head|title|meta|script|link|style|body|table|thead|tbody|tfoot|tr|th|td|h[1-6]|div|p|br)[^>]*>)\s*/is', '$1', $output); - - // Replace mangled <pre> etc. tags with unprocessed ones. - - if ( ! empty($pres_clean)) - { - preg_match_all('{<pre.+</pre>}msU', $output, $pres_messed); - $output = str_replace($pres_messed[0], $pres_clean[0], $output); - } - - if ( ! empty($codes_clean)) - { - preg_match_all('{<code.+</code>}msU', $output, $codes_messed); - $output = str_replace($codes_messed[0], $codes_clean[0], $output); - } - - if ( ! empty($textareas_clean)) - { - preg_match_all('{<textarea.+</textarea>}msU', $output, $textareas_messed); - $output = str_replace($textareas_messed[0], $textareas_clean[0], $output); - } - - if (isset($javascript_mini)) - { - preg_match_all('{<script.+</script>}msU', $output, $javascript_messed); - $output = str_replace($javascript_messed[0], $javascript_mini, $output); - } - - $size_removed = $size_before - strlen($output); - $savings_percent = round(($size_removed / $size_before * 100)); - - log_message('debug', 'Minifier shaved '.($size_removed / 1000).'KB ('.$savings_percent.'%) off final HTML output.'); - - break; - - case 'text/css': - - return $this->_minify_js_css($output, 'css'); - - case 'text/javascript': - case 'application/javascript': - case 'application/x-javascript': - - return $this->_minify_js_css($output, 'js'); - - default: break; - } - - return $output; - } - - // -------------------------------------------------------------------- - - /** - * Minify JavaScript and CSS code - * - * Strips comments and excessive whitespace characters - * - * @param string $output - * @param string $type 'js' or 'css' - * @param bool $tags Whether $output contains the 'script' or 'style' tag - * @return string - */ - protected function _minify_js_css($output, $type, $tags = FALSE) - { - if ($tags === TRUE) - { - $tags = array('close' => strrchr($output, '<')); - - $open_length = strpos($output, '>') + 1; - $tags['open'] = substr($output, 0, $open_length); - - $output = substr($output, $open_length, -strlen($tags['close'])); - - // Strip spaces from the tags - $tags = preg_replace('#\s{2,}#', ' ', $tags); - } - - $output = trim($output); - - if ($type === 'js') - { - // Catch all string literals and comment blocks - if (preg_match_all('#((?:((?<!\\\)\'|")|(/\*)|(//)).*(?(2)(?<!\\\)\2|(?(3)\*/|\n)))#msuUS', $output, $match, PREG_OFFSET_CAPTURE)) - { - $js_literals = $js_code = array(); - for ($match = $match[0], $c = count($match), $i = $pos = $offset = 0; $i < $c; $i++) - { - $js_code[$pos++] = trim(substr($output, $offset, $match[$i][1] - $offset)); - $offset = $match[$i][1] + strlen($match[$i][0]); - - // Save only if we haven't matched a comment block - if ($match[$i][0][0] !== '/') - { - $js_literals[$pos++] = array_shift($match[$i]); - } - } - $js_code[$pos] = substr($output, $offset); - - // $match might be quite large, so free it up together with other vars that we no longer need - unset($match, $offset, $pos); - } - else - { - $js_code = array($output); - $js_literals = array(); - } - - $varname = 'js_code'; - } - else - { - $varname = 'output'; - } - - // Standartize new lines - $$varname = str_replace(array("\r\n", "\r"), "\n", $$varname); - - if ($type === 'js') - { - $patterns = array( - '#\s*([!\#%&()*+,\-./:;<=>?@\[\]^`{|}~])\s*#' => '$1', // Remove spaces following and preceeding JS-wise non-special & non-word characters - '#\s{2,}#' => ' ' // Reduce the remaining multiple whitespace characters to a single space - ); - } - else - { - $patterns = array( - '#/\*.*(?=\*/)\*/#s' => '', // Remove /* block comments */ - '#\n?//[^\n]*#' => '', // Remove // line comments - '#\s*([^\w.\#%])\s*#U' => '$1', // Remove spaces following and preceeding non-word characters, excluding dots, hashes and the percent sign - '#\s{2,}#' => ' ' // Reduce the remaining multiple space characters to a single space - ); - } - - $$varname = preg_replace(array_keys($patterns), array_values($patterns), $$varname); - - // Glue back JS quoted strings - if ($type === 'js') - { - $js_code += $js_literals; - ksort($js_code); - $output = implode($js_code); - unset($js_code, $js_literals, $varname, $patterns); - } - - return is_array($tags) - ? $tags['open'].$output.$tags['close'] - : $output; - } - } /* End of file Output.php */ |