Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * Output Class |
| 32 | * |
| 33 | * Responsible for sending final output to browser |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Libraries |
| 37 | * @category Output |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/libraries/output.html |
| 40 | */ |
| 41 | class CI_Output { |
| 42 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 43 | /** |
| 44 | * Current output string |
| 45 | * |
| 46 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 47 | */ |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 48 | protected $final_output; |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 49 | /** |
| 50 | * Cache expiration time |
| 51 | * |
| 52 | * @var int |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 53 | */ |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 54 | protected $cache_expiration = 0; |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 55 | /** |
| 56 | * List of server headers |
| 57 | * |
| 58 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 59 | */ |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 60 | protected $headers = array(); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 61 | /** |
| 62 | * List of mime types |
| 63 | * |
| 64 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 65 | */ |
| 66 | protected $mime_types = array(); |
| 67 | /** |
| 68 | * Determines wether profiler is enabled |
| 69 | * |
| 70 | * @var book |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 71 | */ |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 72 | protected $enable_profiler = FALSE; |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 73 | /** |
| 74 | * Determines if output compression is enabled |
| 75 | * |
| 76 | * @var bool |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 77 | */ |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 78 | protected $_zlib_oc = FALSE; |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 79 | /** |
| 80 | * List of profiler sections |
| 81 | * |
| 82 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 83 | */ |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 84 | protected $_profiler_sections = array(); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 85 | /** |
| 86 | * Whether or not to parse variables like {elapsed_time} and {memory_usage} |
| 87 | * |
| 88 | * @var bool |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 89 | */ |
| 90 | protected $parse_exec_vars = TRUE; |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 91 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 92 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | { |
Pascal Kriete | 676e1cd | 2010-04-09 21:16:16 +0200 | [diff] [blame] | 94 | $this->_zlib_oc = @ini_get('zlib.output_compression'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 95 | |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 96 | // Get mime types for later |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 97 | if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')) |
| 98 | { |
| 99 | include APPPATH.'config/'.ENVIRONMENT.'/mimes.php'; |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | include APPPATH.'config/mimes.php'; |
| 104 | } |
| 105 | |
Eric Barnes | bb5d4f7 | 2011-03-18 13:39:58 -0400 | [diff] [blame] | 106 | |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 107 | $this->mime_types = $mimes; |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 108 | log_message('debug', 'Output Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 110 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 112 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | /** |
| 114 | * Get Output |
| 115 | * |
| 116 | * Returns the current output string |
| 117 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 119 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 120 | public function get_output() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | { |
| 122 | return $this->final_output; |
| 123 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 124 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 126 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | /** |
| 128 | * Set Output |
| 129 | * |
| 130 | * Sets the output string |
| 131 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | * @param string |
| 133 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 134 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 135 | public function set_output($output) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | { |
| 137 | $this->final_output = $output; |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 138 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // -------------------------------------------------------------------- |
| 142 | |
| 143 | /** |
| 144 | * Append Output |
| 145 | * |
| 146 | * Appends data onto the output string |
| 147 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | * @param string |
| 149 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 151 | public function append_output($output) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | { |
| 153 | if ($this->final_output == '') |
| 154 | { |
| 155 | $this->final_output = $output; |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | $this->final_output .= $output; |
| 160 | } |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 161 | |
| 162 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // -------------------------------------------------------------------- |
| 166 | |
| 167 | /** |
| 168 | * Set Header |
| 169 | * |
| 170 | * Lets you set a server header which will be outputted with the final display. |
| 171 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 172 | * Note: If a file is cached, headers will not be sent. We need to figure out |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | * how to permit header data to be saved with the cache data... |
| 174 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | * @param string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 176 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 178 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 179 | public function set_header($header, $replace = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | { |
Pascal Kriete | 676e1cd | 2010-04-09 21:16:16 +0200 | [diff] [blame] | 181 | // If zlib.output_compression is enabled it will compress the output, |
| 182 | // but it will not modify the content-length header to compensate for |
| 183 | // the reduction, causing the browser to hang waiting for more data. |
| 184 | // We'll just skip content-length in those cases. |
Pascal Kriete | 676e1cd | 2010-04-09 21:16:16 +0200 | [diff] [blame] | 185 | if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) == 0) |
| 186 | { |
| 187 | return; |
| 188 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | $this->headers[] = array($header, $replace); |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 191 | return $this; |
| 192 | } |
| 193 | |
| 194 | // -------------------------------------------------------------------- |
| 195 | |
| 196 | /** |
| 197 | * Set Content Type Header |
| 198 | * |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 199 | * @param string extension of the file we're outputting |
| 200 | * @return void |
| 201 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 202 | public function set_content_type($mime_type) |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 203 | { |
| 204 | if (strpos($mime_type, '/') === FALSE) |
| 205 | { |
| 206 | $extension = ltrim($mime_type, '.'); |
| 207 | |
| 208 | // Is this extension supported? |
| 209 | if (isset($this->mime_types[$extension])) |
| 210 | { |
| 211 | $mime_type =& $this->mime_types[$extension]; |
| 212 | |
| 213 | if (is_array($mime_type)) |
| 214 | { |
| 215 | $mime_type = current($mime_type); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | $header = 'Content-Type: '.$mime_type; |
| 221 | |
| 222 | $this->headers[] = array($header, TRUE); |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 223 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 227 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | /** |
| 229 | * Set HTTP Status Header |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 230 | * moved to Common procedural functions in 1.7.2 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 231 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 232 | * @param int the status code |
| 233 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 235 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 236 | public function set_status_header($code = 200, $text = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | { |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 238 | set_status_header($code, $text); |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 239 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 241 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 243 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 244 | /** |
| 245 | * Enable/disable Profiler |
| 246 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | * @param bool |
| 248 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 249 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 250 | public function enable_profiler($val = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | { |
| 252 | $this->enable_profiler = (is_bool($val)) ? $val : TRUE; |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 253 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 255 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | // -------------------------------------------------------------------- |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 257 | |
| 258 | /** |
| 259 | * Set Profiler Sections |
| 260 | * |
| 261 | * Allows override of default / config settings for Profiler section display |
| 262 | * |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 263 | * @param array |
| 264 | * @return void |
| 265 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 266 | public function set_profiler_sections($sections) |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 267 | { |
| 268 | foreach ($sections as $section => $enable) |
| 269 | { |
| 270 | $this->_profiler_sections[$section] = ($enable !== FALSE) ? TRUE : FALSE; |
| 271 | } |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 272 | |
| 273 | return $this; |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 277 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | /** |
| 279 | * Set Cache |
| 280 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | * @param integer |
| 282 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 283 | */ |
Michiel Vugteveen | 0609d58 | 2012-01-08 13:26:17 +0100 | [diff] [blame] | 284 | public function cache($time) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | { |
| 286 | $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time; |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 287 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 289 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 291 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | /** |
| 293 | * Display Output |
| 294 | * |
| 295 | * All "view" data is automatically put into this variable by the controller class: |
| 296 | * |
| 297 | * $this->final_output |
| 298 | * |
| 299 | * This function sends the finalized output data to the browser along |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 300 | * with any server headers and profile data. It also stops the |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | * benchmark timer so the page rendering speed and memory usage can be shown. |
| 302 | * |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 303 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 305 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 306 | public function _display($output = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 307 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 308 | // Note: We use globals because we can't use $CI =& get_instance() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | // since this function is sometimes called by the caching mechanism, |
| 310 | // which happens before the CI super object is available. |
| 311 | global $BM, $CFG; |
Derek Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame] | 312 | |
| 313 | // Grab the super object if we can. |
Greg Aker | cc92210 | 2010-11-17 20:25:08 -0600 | [diff] [blame] | 314 | if (class_exists('CI_Controller')) |
Derek Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame] | 315 | { |
| 316 | $CI =& get_instance(); |
| 317 | } |
| 318 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 320 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | // Set the output data |
| 322 | if ($output == '') |
| 323 | { |
| 324 | $output =& $this->final_output; |
| 325 | } |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 326 | |
| 327 | // -------------------------------------------------------------------- |
| 328 | |
| 329 | // Is minify requested? |
| 330 | if ($CFG->item('minify_output') === TRUE) |
| 331 | { |
| 332 | $output = $this->minify($output); |
| 333 | } |
| 334 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 335 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 336 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 337 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 338 | // Do we need to write a cache file? Only if the controller does not have its |
Derek Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame] | 339 | // own _output() method and we are not dealing with a cache file, which we |
| 340 | // can determine by the existence of the $CI object above |
| 341 | if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 342 | { |
| 343 | $this->_write_cache($output); |
| 344 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 345 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 346 | // -------------------------------------------------------------------- |
| 347 | |
| 348 | // Parse out the elapsed time and memory usage, |
| 349 | // then swap the pseudo-variables with the data |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 350 | |
| 351 | $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end'); |
Derek Jones | 4652049 | 2010-03-02 13:53:25 -0600 | [diff] [blame] | 352 | |
| 353 | if ($this->parse_exec_vars === TRUE) |
| 354 | { |
| 355 | $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 356 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 357 | $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output); |
Derek Jones | 4652049 | 2010-03-02 13:53:25 -0600 | [diff] [blame] | 358 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | |
| 360 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 361 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 362 | // Is compression requested? |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 363 | if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE |
| 364 | && extension_loaded('zlib') |
| 365 | && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 366 | { |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 367 | ob_start('ob_gzhandler'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 371 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | // Are there any server headers to send? |
| 373 | if (count($this->headers) > 0) |
| 374 | { |
| 375 | foreach ($this->headers as $header) |
| 376 | { |
| 377 | @header($header[0], $header[1]); |
| 378 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 379 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 380 | |
| 381 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 382 | |
Derek Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame] | 383 | // Does the $CI object exist? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | // If not we know we are dealing with a cache file so we'll |
| 385 | // simply echo out the data and exit. |
Derek Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame] | 386 | if ( ! isset($CI)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | { |
| 388 | echo $output; |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 389 | log_message('debug', 'Final output sent to browser'); |
| 390 | log_message('debug', 'Total execution time: '.$elapsed); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 391 | return TRUE; |
| 392 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 393 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 394 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 395 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | // Do we need to generate profile data? |
| 397 | // If so, load the Profile class and run it. |
| 398 | if ($this->enable_profiler == TRUE) |
| 399 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 400 | $CI->load->library('profiler'); |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 401 | if ( ! empty($this->_profiler_sections)) |
| 402 | { |
| 403 | $CI->profiler->set_sections($this->_profiler_sections); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 404 | } |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 405 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | // If the output data contains closing </body> and </html> tags |
| 407 | // we will remove them and add them back after we insert the profile data |
Andrey Andreev | cba20b1 | 2012-01-09 10:16:41 +0200 | [diff] [blame] | 408 | $output = preg_replace('|</body>.*?</html>|is', '', $output, -1, $count).$CI->profiler->run(); |
| 409 | if ($count > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 410 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | $output .= '</body></html>'; |
| 412 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 414 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | // Does the controller contain a function named _output()? |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 416 | // If so send the output there. Otherwise, echo it. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | if (method_exists($CI, '_output')) |
| 418 | { |
| 419 | $CI->_output($output); |
| 420 | } |
| 421 | else |
| 422 | { |
Andrey Andreev | edc8755 | 2012-01-09 09:35:10 +0200 | [diff] [blame] | 423 | echo $output; // Send it to the browser! |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 425 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 426 | log_message('debug', 'Final output sent to browser'); |
| 427 | log_message('debug', 'Total execution time: '.$elapsed); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 429 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 431 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | /** |
| 433 | * Write a Cache File |
| 434 | * |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 435 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 437 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 438 | public function _write_cache($output) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 439 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 440 | $CI =& get_instance(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | $path = $CI->config->item('cache_path'); |
Greg Aker | 2eaa407 | 2010-12-21 11:44:08 -0600 | [diff] [blame] | 442 | $cache_path = ($path == '') ? APPPATH.'cache/' : $path; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 443 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path)) |
| 445 | { |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 446 | log_message('error', 'Unable to write cache file: '.$cache_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 447 | return; |
| 448 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 449 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 450 | $uri = $CI->config->item('base_url'). |
| 451 | $CI->config->item('index_page'). |
| 452 | $CI->uri->uri_string(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 453 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 454 | $cache_path .= md5($uri); |
| 455 | |
| 456 | if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE)) |
| 457 | { |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 458 | log_message('error', 'Unable to write cache file: '.$cache_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 459 | return; |
| 460 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 461 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | $expire = time() + ($this->cache_expiration * 60); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 463 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | if (flock($fp, LOCK_EX)) |
| 465 | { |
| 466 | fwrite($fp, $expire.'TS--->'.$output); |
| 467 | flock($fp, LOCK_UN); |
| 468 | } |
| 469 | else |
| 470 | { |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 471 | log_message('error', 'Unable to secure a file lock for file at: '.$cache_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | return; |
| 473 | } |
| 474 | fclose($fp); |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 475 | @chmod($cache_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 476 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 477 | log_message('debug', 'Cache file written: '.$cache_path); |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 478 | |
| 479 | // Send HTTP cache-control headers to browser to match file cache settings. |
| 480 | $this->set_cache_header($_SERVER['REQUEST_TIME'],$expire); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 484 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 485 | /** |
| 486 | * Update/serve a cached file |
| 487 | * |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 488 | * @param object config class |
| 489 | * @param object uri class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 491 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 492 | public function _display_cache(&$CFG, &$URI) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 493 | { |
Greg Aker | 2eaa407 | 2010-12-21 11:44:08 -0600 | [diff] [blame] | 494 | $cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 495 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 496 | // Build the file path. The file name is an MD5 hash of the full URI |
| 497 | $uri = $CFG->item('base_url').$CFG->item('index_page').$URI->uri_string; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 498 | $filepath = $cache_path.md5($uri); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 499 | |
Andrey Andreev | c90d651 | 2012-01-08 04:35:02 +0200 | [diff] [blame] | 500 | if ( ! @file_exists($filepath) OR ! $fp = @fopen($filepath, FOPEN_READ)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | { |
| 502 | return FALSE; |
| 503 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 504 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 505 | flock($fp, LOCK_SH); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 506 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 507 | $cache = (filesize($filepath) > 0) ? fread($fp, filesize($filepath)) : ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 508 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 509 | flock($fp, LOCK_UN); |
| 510 | fclose($fp); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 511 | |
| 512 | // Strip out the embedded timestamp |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 513 | if ( ! preg_match('/(\d+TS--->)/', $cache, $match)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 514 | { |
| 515 | return FALSE; |
| 516 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 517 | |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 518 | $last_modified = filemtime($cache_path); |
| 519 | $expire = trim(str_replace('TS--->', '', $match[1])); |
| 520 | |
| 521 | // Has the file expired? |
| 522 | if ($_SERVER['REQUEST_TIME'] >= $expire && is_really_writable($cache_path)) |
Derek Jones | d99e603 | 2010-03-19 19:57:33 -0500 | [diff] [blame] | 523 | { |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 524 | // If so we'll delete it. |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 525 | @unlink($filepath); |
| 526 | log_message('debug', 'Cache file has expired. File deleted.'); |
| 527 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 528 | } |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 529 | else |
| 530 | { |
| 531 | // Or else send the HTTP cache control headers. |
| 532 | $this->set_cache_header($last_modified,$expire); |
| 533 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 534 | |
| 535 | // Display the cache |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 536 | $this->_display(str_replace($match[0], '', $cache)); |
| 537 | log_message('debug', 'Cache file is current. Sending it to browser.'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 538 | return TRUE; |
| 539 | } |
| 540 | |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 541 | |
| 542 | // -------------------------------------------------------------------- |
| 543 | /** |
| 544 | * Set the HTTP headers to match the server-side file cache settings |
| 545 | * in order to reduce bandwidth. |
| 546 | * |
| 547 | * @param int timestamp of when the page was last modified |
| 548 | * @param int timestamp of when should the requested page expire from cache |
| 549 | * @return void |
| 550 | */ |
| 551 | public function set_cache_header($last_modified,$expiration) |
| 552 | { |
| 553 | $max_age = $expiration - $_SERVER['REQUEST_TIME']; |
| 554 | |
| 555 | if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && ($last_modified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']))) |
| 556 | { |
| 557 | $this->set_status_header(304); |
| 558 | exit; |
| 559 | } |
| 560 | else |
| 561 | { |
| 562 | header('Pragma: public'); |
| 563 | header('Cache-Control: max-age=' . $max_age . ', public'); |
| 564 | header('Expires: '.gmdate('D, d M Y H:i:s', $expiration).' GMT'); |
| 565 | header('Last-modified: '.gmdate('D, d M Y H:i:s', $last_modified).' GMT'); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 570 | |
| 571 | |
| 572 | // -------------------------------------------------------------------- |
| 573 | /** |
| 574 | * Reduce excessive size of HTML content. |
| 575 | * |
| 576 | * @param string |
| 577 | * @param string |
| 578 | * @return string |
| 579 | */ |
| 580 | public function minify($output,$type='html') |
| 581 | { |
| 582 | switch ($type) |
| 583 | { |
| 584 | case 'html': |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 585 | |
Thor (atiredmachine) | d68c819 | 2012-01-24 20:56:21 -0800 | [diff] [blame] | 586 | // Keep track of <pre> <code> and <textarea> tags as |
| 587 | // they were before processing. |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 588 | // We'll want to return them to this state later. |
| 589 | preg_match_all('{<pre.+</pre>}msU',$output,$pres_clean); |
Thor (atiredmachine) | d68c819 | 2012-01-24 20:56:21 -0800 | [diff] [blame] | 590 | preg_match_all('{<code.+</code>}msU',$output,$codes_clean); |
| 591 | preg_match_all('{<textarea.+</textarea>}msU',$output,$textareas_clean); |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 592 | |
Thor (atiredmachine) | d68c819 | 2012-01-24 20:56:21 -0800 | [diff] [blame] | 593 | // Minify the CSS in all the <style> tags. |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 594 | preg_match_all('{<style.+</style>}msU',$output,$style_clean); |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 595 | foreach ($style_clean[0] as $s) |
| 596 | { |
| 597 | $output = str_replace($s, $this->minify($s,'css'), $output); |
| 598 | } |
| 599 | |
Thor (atiredmachine) | a2ae6e1 | 2012-01-24 20:57:48 -0800 | [diff] [blame^] | 600 | // Replace multiple spaces with a single space. |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 601 | $output = preg_replace('!\s{2,}!',"\n",$output); |
| 602 | |
Thor (atiredmachine) | a2ae6e1 | 2012-01-24 20:57:48 -0800 | [diff] [blame^] | 603 | // Remove spaces around block-level elements. |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 604 | $output = preg_replace('{\s*(</?(html|head|title|meta|script|link|style|body|h[1-6]|div|p|br).*>)\s*}', '$1', $output); |
| 605 | |
Thor (atiredmachine) | d68c819 | 2012-01-24 20:56:21 -0800 | [diff] [blame] | 606 | // Replace mangled <pre> etc. tags with unprocessed ones. |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 607 | preg_match_all('{<pre.+</pre>}msU',$output,$pres_messed); |
Thor (atiredmachine) | d68c819 | 2012-01-24 20:56:21 -0800 | [diff] [blame] | 608 | preg_match_all('{<code.+</code>}msU',$output,$codes_messed); |
| 609 | preg_match_all('{<textarea.+</textarea>}msU',$output,$textareas_messed); |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 610 | $output = str_replace($pres_messed[0],$pres_clean[0],$output); |
Thor (atiredmachine) | d68c819 | 2012-01-24 20:56:21 -0800 | [diff] [blame] | 611 | $output = str_replace($codes_messed[0],$codes_clean[0],$output); |
| 612 | $output = str_replace($textareas_messed[0],$textareas_clean[0],$output); |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 613 | |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 614 | break; |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 615 | |
| 616 | |
| 617 | case 'css': |
| 618 | |
Thor (atiredmachine) | a2ae6e1 | 2012-01-24 20:57:48 -0800 | [diff] [blame^] | 619 | // Remove spaces around curly brackets, colons, and semi-colons |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 620 | $output = preg_replace('!\s*(:|;|}|{)\s*!','$1',$output); |
| 621 | |
| 622 | // Replace spaces with line breaks to limit line lengths |
| 623 | $output = preg_replace('!\s+!',"\n",$output); |
| 624 | |
| 625 | break; |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | return $output; |
| 629 | } |
| 630 | |
| 631 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 632 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 633 | |
| 634 | /* End of file Output.php */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 635 | /* Location: ./system/core/Output.php */ |