blob: 8992fc1f1198f1e11979068d36baff0379050451 [file] [log] [blame]
Andrey Andreev1f5fbb62012-01-07 20:53:29 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev1f5fbb62012-01-07 20:53:29 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev1f5fbb62012-01-07 20:53:29 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @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 Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/libraries/output.html
40 */
41class CI_Output {
42
David Behler07b53422011-08-15 00:25:06 +020043 /**
44 * Current output string
45 *
46 * @var string
David Behler07b53422011-08-15 00:25:06 +020047 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000048 protected $final_output;
David Behler07b53422011-08-15 00:25:06 +020049 /**
50 * Cache expiration time
51 *
52 * @var int
David Behler07b53422011-08-15 00:25:06 +020053 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000054 protected $cache_expiration = 0;
David Behler07b53422011-08-15 00:25:06 +020055 /**
56 * List of server headers
57 *
58 * @var array
David Behler07b53422011-08-15 00:25:06 +020059 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000060 protected $headers = array();
David Behler07b53422011-08-15 00:25:06 +020061 /**
62 * List of mime types
63 *
64 * @var array
David Behler07b53422011-08-15 00:25:06 +020065 */
66 protected $mime_types = array();
67 /**
68 * Determines wether profiler is enabled
69 *
70 * @var book
David Behler07b53422011-08-15 00:25:06 +020071 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000072 protected $enable_profiler = FALSE;
David Behler07b53422011-08-15 00:25:06 +020073 /**
74 * Determines if output compression is enabled
75 *
76 * @var bool
David Behler07b53422011-08-15 00:25:06 +020077 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000078 protected $_zlib_oc = FALSE;
David Behler07b53422011-08-15 00:25:06 +020079 /**
80 * List of profiler sections
81 *
82 * @var array
David Behler07b53422011-08-15 00:25:06 +020083 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000084 protected $_profiler_sections = array();
David Behler07b53422011-08-15 00:25:06 +020085 /**
86 * Whether or not to parse variables like {elapsed_time} and {memory_usage}
87 *
88 * @var bool
David Behler07b53422011-08-15 00:25:06 +020089 */
90 protected $parse_exec_vars = TRUE;
Derek Jonesee71c802010-03-10 10:05:05 -060091
Andrey Andreev1f5fbb62012-01-07 20:53:29 +020092 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000093 {
Pascal Kriete676e1cd2010-04-09 21:16:16 +020094 $this->_zlib_oc = @ini_get('zlib.output_compression');
Barry Mienydd671972010-10-04 16:33:58 +020095
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000096 // Get mime types for later
Greg Akerd96f8822011-12-27 16:23:47 -060097 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 Barnesbb5d4f72011-03-18 13:39:58 -0400106
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000107 $this->mime_types = $mimes;
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200108 log_message('debug', 'Output Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 /**
114 * Get Output
115 *
116 * Returns the current output string
117 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200119 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200120 public function get_output()
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
122 return $this->final_output;
123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 /**
128 * Set Output
129 *
130 * Sets the output string
131 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 * @param string
133 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200134 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200135 public function set_output($output)
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
137 $this->final_output = $output;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000138 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Append Output
145 *
146 * Appends data onto the output string
147 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 * @param string
149 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200150 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200151 public function append_output($output)
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 {
153 if ($this->final_output == '')
154 {
155 $this->final_output = $output;
156 }
157 else
158 {
159 $this->final_output .= $output;
160 }
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000161
162 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
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 Jones37f4b9c2011-07-01 17:56:50 -0500172 * Note: If a file is cached, headers will not be sent. We need to figure out
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 * how to permit header data to be saved with the cache data...
174 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 * @param string
David Behler07b53422011-08-15 00:25:06 +0200176 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200178 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200179 public function set_header($header, $replace = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Pascal Kriete676e1cd2010-04-09 21:16:16 +0200181 // 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 Kriete676e1cd2010-04-09 21:16:16 +0200185 if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) == 0)
186 {
187 return;
188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 $this->headers[] = array($header, $replace);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000191 return $this;
192 }
193
194 // --------------------------------------------------------------------
195
196 /**
197 * Set Content Type Header
198 *
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000199 * @param string extension of the file we're outputting
200 * @return void
201 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200202 public function set_content_type($mime_type)
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000203 {
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 Sturgeon60ed1a32011-03-08 21:43:54 +0000223 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
225
226 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 /**
229 * Set HTTP Status Header
Derek Jones817163a2009-07-11 17:05:58 +0000230 * moved to Common procedural functions in 1.7.2
Barry Mienydd671972010-10-04 16:33:58 +0200231 *
Barry Mienydd671972010-10-04 16:33:58 +0200232 * @param int the status code
233 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200235 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200236 public function set_status_header($code = 200, $text = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Derek Jones817163a2009-07-11 17:05:58 +0000238 set_status_header($code, $text);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000239 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 /**
245 * Enable/disable Profiler
246 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 * @param bool
248 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200249 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200250 public function enable_profiler($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
252 $this->enable_profiler = (is_bool($val)) ? $val : TRUE;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000253 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 // --------------------------------------------------------------------
Derek Jonesee71c802010-03-10 10:05:05 -0600257
258 /**
259 * Set Profiler Sections
260 *
261 * Allows override of default / config settings for Profiler section display
262 *
Derek Jonesee71c802010-03-10 10:05:05 -0600263 * @param array
264 * @return void
265 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200266 public function set_profiler_sections($sections)
Derek Jonesee71c802010-03-10 10:05:05 -0600267 {
268 foreach ($sections as $section => $enable)
269 {
270 $this->_profiler_sections[$section] = ($enable !== FALSE) ? TRUE : FALSE;
271 }
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000272
273 return $this;
Derek Jonesee71c802010-03-10 10:05:05 -0600274 }
275
276 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 /**
279 * Set Cache
280 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 * @param integer
282 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200283 */
Michiel Vugteveen0609d582012-01-08 13:26:17 +0100284 public function cache($time)
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
286 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000287 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 /**
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 Jones37f4b9c2011-07-01 17:56:50 -0500300 * with any server headers and profile data. It also stops the
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 * benchmark timer so the page rendering speed and memory usage can be shown.
302 *
David Behler07b53422011-08-15 00:25:06 +0200303 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200305 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200306 public function _display($output = '')
Barry Mienydd671972010-10-04 16:33:58 +0200307 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500308 // Note: We use globals because we can't use $CI =& get_instance()
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 // 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 Jonesd7633492010-09-28 13:14:57 -0500312
313 // Grab the super object if we can.
Greg Akercc922102010-11-17 20:25:08 -0600314 if (class_exists('CI_Controller'))
Derek Jonesd7633492010-09-28 13:14:57 -0500315 {
316 $CI =& get_instance();
317 }
318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 // Set the output data
322 if ($output == '')
323 {
324 $output =& $this->final_output;
325 }
Thor (atiredmachine)63678a22012-01-24 16:56:01 -0800326
327 // --------------------------------------------------------------------
328
329 // Is minify requested?
330 if ($CFG->item('minify_output') === TRUE)
331 {
332 $output = $this->minify($output);
333 }
334
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Jones37f4b9c2011-07-01 17:56:50 -0500338 // Do we need to write a cache file? Only if the controller does not have its
Derek Jonesd7633492010-09-28 13:14:57 -0500339 // 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 Allard2067d1a2008-11-13 22:59:24 +0000342 {
343 $this->_write_cache($output);
344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
347
348 // Parse out the elapsed time and memory usage,
349 // then swap the pseudo-variables with the data
Barry Mienydd671972010-10-04 16:33:58 +0200350
351 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
Derek Jones46520492010-03-02 13:53:25 -0600352
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 Mienydd671972010-10-04 16:33:58 +0200356
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200357 $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output);
Derek Jones46520492010-03-02 13:53:25 -0600358 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000359
360 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // Is compression requested?
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200363 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 Allard2067d1a2008-11-13 22:59:24 +0000366 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200367 ob_start('ob_gzhandler');
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 }
369
370 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 // 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 Mienydd671972010-10-04 16:33:58 +0200379 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000380
381 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200382
Derek Jonesd7633492010-09-28 13:14:57 -0500383 // Does the $CI object exist?
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 // If not we know we are dealing with a cache file so we'll
385 // simply echo out the data and exit.
Derek Jonesd7633492010-09-28 13:14:57 -0500386 if ( ! isset($CI))
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
388 echo $output;
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200389 log_message('debug', 'Final output sent to browser');
390 log_message('debug', 'Total execution time: '.$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 return TRUE;
392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 // 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 Mienydd671972010-10-04 16:33:58 +0200400 $CI->load->library('profiler');
Derek Jonesee71c802010-03-10 10:05:05 -0600401 if ( ! empty($this->_profiler_sections))
402 {
403 $CI->profiler->set_sections($this->_profiler_sections);
Barry Mienydd671972010-10-04 16:33:58 +0200404 }
Derek Jonesee71c802010-03-10 10:05:05 -0600405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 // 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 Andreevcba20b12012-01-09 10:16:41 +0200408 $output = preg_replace('|</body>.*?</html>|is', '', $output, -1, $count).$CI->profiler->run();
409 if ($count > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 $output .= '</body></html>';
412 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 }
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 // Does the controller contain a function named _output()?
Derek Jones37f4b9c2011-07-01 17:56:50 -0500416 // If so send the output there. Otherwise, echo it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 if (method_exists($CI, '_output'))
418 {
419 $CI->_output($output);
420 }
421 else
422 {
Andrey Andreevedc87552012-01-09 09:35:10 +0200423 echo $output; // Send it to the browser!
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 }
Barry Mienydd671972010-10-04 16:33:58 +0200425
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200426 log_message('debug', 'Final output sent to browser');
427 log_message('debug', 'Total execution time: '.$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 /**
433 * Write a Cache File
434 *
David Behler07b53422011-08-15 00:25:06 +0200435 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200437 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200438 public function _write_cache($output)
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
Barry Mienydd671972010-10-04 16:33:58 +0200440 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 $path = $CI->config->item('cache_path');
Greg Aker2eaa4072010-12-21 11:44:08 -0600442 $cache_path = ($path == '') ? APPPATH.'cache/' : $path;
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
445 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200446 log_message('error', 'Unable to write cache file: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 return;
448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 $uri = $CI->config->item('base_url').
451 $CI->config->item('index_page').
452 $CI->uri->uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 $cache_path .= md5($uri);
455
456 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
457 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200458 log_message('error', 'Unable to write cache file: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 return;
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 $expire = time() + ($this->cache_expiration * 60);
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 if (flock($fp, LOCK_EX))
465 {
466 fwrite($fp, $expire.'TS--->'.$output);
467 flock($fp, LOCK_UN);
468 }
469 else
470 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200471 log_message('error', 'Unable to secure a file lock for file at: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 return;
473 }
474 fclose($fp);
Derek Jones172e1612009-10-13 14:32:48 +0000475 @chmod($cache_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000476
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200477 log_message('debug', 'Cache file written: '.$cache_path);
Thor (atiredmachine)c8efb802012-01-24 13:33:39 -0800478
479 // Send HTTP cache-control headers to browser to match file cache settings.
480 $this->set_cache_header($_SERVER['REQUEST_TIME'],$expire);
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
482
483 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 /**
486 * Update/serve a cached file
487 *
David Behler07b53422011-08-15 00:25:06 +0200488 * @param object config class
489 * @param object uri class
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200491 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200492 public function _display_cache(&$CFG, &$URI)
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
Greg Aker2eaa4072010-12-21 11:44:08 -0600494 $cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path');
Barry Mienydd671972010-10-04 16:33:58 +0200495
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200496 // 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 Allard2067d1a2008-11-13 22:59:24 +0000498 $filepath = $cache_path.md5($uri);
Barry Mienydd671972010-10-04 16:33:58 +0200499
Andrey Andreevc90d6512012-01-08 04:35:02 +0200500 if ( ! @file_exists($filepath) OR ! $fp = @fopen($filepath, FOPEN_READ))
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
502 return FALSE;
503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 flock($fp, LOCK_SH);
Barry Mienydd671972010-10-04 16:33:58 +0200506
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200507 $cache = (filesize($filepath) > 0) ? fread($fp, filesize($filepath)) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 flock($fp, LOCK_UN);
510 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200511
512 // Strip out the embedded timestamp
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200513 if ( ! preg_match('/(\d+TS--->)/', $cache, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
515 return FALSE;
516 }
Barry Mienydd671972010-10-04 16:33:58 +0200517
Thor (atiredmachine)c8efb802012-01-24 13:33:39 -0800518 $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 Jonesd99e6032010-03-19 19:57:33 -0500523 {
Thor (atiredmachine)c8efb802012-01-24 13:33:39 -0800524 // If so we'll delete it.
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200525 @unlink($filepath);
526 log_message('debug', 'Cache file has expired. File deleted.');
527 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
Thor (atiredmachine)c8efb802012-01-24 13:33:39 -0800529 else
530 {
531 // Or else send the HTTP cache control headers.
532 $this->set_cache_header($last_modified,$expire);
533 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000534
535 // Display the cache
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200536 $this->_display(str_replace($match[0], '', $cache));
537 log_message('debug', 'Cache file is current. Sending it to browser.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 return TRUE;
539 }
540
Thor (atiredmachine)c8efb802012-01-24 13:33:39 -0800541
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)63678a22012-01-24 16:56:01 -0800570
571
572 // --------------------------------------------------------------------
573 /**
574 * Reduce excessive size of HTML content.
575 *
576 * @param string
577 * @param string
578 * @return string
579 */
Thor (atiredmachine)1d0661a2012-01-24 21:39:16 -0800580 public function minify($output,$type='text/html')
Thor (atiredmachine)63678a22012-01-24 16:56:01 -0800581 {
582 switch ($type)
583 {
Thor (atiredmachine)1d0661a2012-01-24 21:39:16 -0800584 case 'text/html':
Thor (atiredmachine)79db4cd2012-01-24 20:44:51 -0800585
Thor (atiredmachine)d68c8192012-01-24 20:56:21 -0800586 // Keep track of <pre> <code> and <textarea> tags as
587 // they were before processing.
Thor (atiredmachine)79db4cd2012-01-24 20:44:51 -0800588 // We'll want to return them to this state later.
589 preg_match_all('{<pre.+</pre>}msU',$output,$pres_clean);
Thor (atiredmachine)d68c8192012-01-24 20:56:21 -0800590 preg_match_all('{<code.+</code>}msU',$output,$codes_clean);
591 preg_match_all('{<textarea.+</textarea>}msU',$output,$textareas_clean);
Thor (atiredmachine)79db4cd2012-01-24 20:44:51 -0800592
Thor (atiredmachine)d68c8192012-01-24 20:56:21 -0800593 // Minify the CSS in all the <style> tags.
Thor (atiredmachine)79db4cd2012-01-24 20:44:51 -0800594 preg_match_all('{<style.+</style>}msU',$output,$style_clean);
Thor (atiredmachine)79db4cd2012-01-24 20:44:51 -0800595 foreach ($style_clean[0] as $s)
596 {
Thor (atiredmachine)1d0661a2012-01-24 21:39:16 -0800597 $output = str_replace($s, $this->minify($s,'text/css'), $output);
Thor (atiredmachine)79db4cd2012-01-24 20:44:51 -0800598 }
599
Thor (atiredmachine)a2ae6e12012-01-24 20:57:48 -0800600 // Replace multiple spaces with a single space.
Thor (atiredmachine)79db4cd2012-01-24 20:44:51 -0800601 $output = preg_replace('!\s{2,}!',"\n",$output);
602
Thor (atiredmachine)5de11752012-01-24 22:08:36 -0800603 // Remove comments (non-MSIE conditionals)
604 $output = preg_replace('{\s*<!--[^\[].*-->\s*}msU','',$output);
605
Thor (atiredmachine)a2ae6e12012-01-24 20:57:48 -0800606 // Remove spaces around block-level elements.
Thor (atiredmachine)79db4cd2012-01-24 20:44:51 -0800607 $output = preg_replace('{\s*(</?(html|head|title|meta|script|link|style|body|h[1-6]|div|p|br).*>)\s*}', '$1', $output);
608
Thor (atiredmachine)d68c8192012-01-24 20:56:21 -0800609 // Replace mangled <pre> etc. tags with unprocessed ones.
Thor (atiredmachine)79db4cd2012-01-24 20:44:51 -0800610 preg_match_all('{<pre.+</pre>}msU',$output,$pres_messed);
Thor (atiredmachine)d68c8192012-01-24 20:56:21 -0800611 preg_match_all('{<code.+</code>}msU',$output,$codes_messed);
612 preg_match_all('{<textarea.+</textarea>}msU',$output,$textareas_messed);
Thor (atiredmachine)79db4cd2012-01-24 20:44:51 -0800613 $output = str_replace($pres_messed[0],$pres_clean[0],$output);
Thor (atiredmachine)d68c8192012-01-24 20:56:21 -0800614 $output = str_replace($codes_messed[0],$codes_clean[0],$output);
615 $output = str_replace($textareas_messed[0],$textareas_clean[0],$output);
Thor (atiredmachine)63678a22012-01-24 16:56:01 -0800616
Thor (atiredmachine)63678a22012-01-24 16:56:01 -0800617 break;
Thor (atiredmachine)79db4cd2012-01-24 20:44:51 -0800618
619
Thor (atiredmachine)1d0661a2012-01-24 21:39:16 -0800620 case 'text/css':
Thor (atiredmachine)79db4cd2012-01-24 20:44:51 -0800621
Thor (atiredmachine)a2ae6e12012-01-24 20:57:48 -0800622 // Remove spaces around curly brackets, colons, and semi-colons
Thor (atiredmachine)79db4cd2012-01-24 20:44:51 -0800623 $output = preg_replace('!\s*(:|;|}|{)\s*!','$1',$output);
624
625 // Replace spaces with line breaks to limit line lengths
626 $output = preg_replace('!\s+!',"\n",$output);
627
628 break;
Thor (atiredmachine)63678a22012-01-24 16:56:01 -0800629 }
630
631 return $output;
632 }
633
634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635}
Derek Allard2067d1a2008-11-13 22:59:24 +0000636
637/* End of file Output.php */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200638/* Location: ./system/core/Output.php */