blob: 4d1036ceb5413a0da8b341f48d6d25f1d99fa666 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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
47 * @access protected
48 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000049 protected $final_output;
David Behler07b53422011-08-15 00:25:06 +020050 /**
51 * Cache expiration time
52 *
53 * @var int
54 * @access protected
55 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000056 protected $cache_expiration = 0;
David Behler07b53422011-08-15 00:25:06 +020057 /**
58 * List of server headers
59 *
60 * @var array
61 * @access protected
62 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000063 protected $headers = array();
David Behler07b53422011-08-15 00:25:06 +020064 /**
65 * List of mime types
66 *
67 * @var array
68 * @access protected
69 */
70 protected $mime_types = array();
71 /**
72 * Determines wether profiler is enabled
73 *
74 * @var book
75 * @access protected
76 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000077 protected $enable_profiler = FALSE;
David Behler07b53422011-08-15 00:25:06 +020078 /**
79 * Determines if output compression is enabled
80 *
81 * @var bool
82 * @access protected
83 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000084 protected $_zlib_oc = FALSE;
David Behler07b53422011-08-15 00:25:06 +020085 /**
86 * List of profiler sections
87 *
88 * @var array
89 * @access protected
90 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000091 protected $_profiler_sections = array();
David Behler07b53422011-08-15 00:25:06 +020092 /**
93 * Whether or not to parse variables like {elapsed_time} and {memory_usage}
94 *
95 * @var bool
96 * @access protected
97 */
98 protected $parse_exec_vars = TRUE;
Derek Jonesee71c802010-03-10 10:05:05 -060099
David Behler07b53422011-08-15 00:25:06 +0200100 /**
101 * Constructor
102 *
103 */
Greg Akera9263282010-11-10 15:26:43 -0600104 function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
Pascal Kriete676e1cd2010-04-09 21:16:16 +0200106 $this->_zlib_oc = @ini_get('zlib.output_compression');
Barry Mienydd671972010-10-04 16:33:58 +0200107
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000108 // Get mime types for later
Greg Aker5c1aa632011-12-25 01:24:29 -0600109 load_environ_config('mimes');
Eric Barnesbb5d4f72011-03-18 13:39:58 -0400110
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000111 $this->mime_types = $mimes;
Eric Barnesbb5d4f72011-03-18 13:39:58 -0400112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 log_message('debug', "Output Class Initialized");
114 }
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 /**
119 * Get Output
120 *
121 * Returns the current output string
122 *
123 * @access public
124 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200125 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 function get_output()
127 {
128 return $this->final_output;
129 }
Barry Mienydd671972010-10-04 16:33:58 +0200130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 /**
134 * Set Output
135 *
136 * Sets the output string
137 *
138 * @access public
139 * @param string
140 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200141 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 function set_output($output)
143 {
144 $this->final_output = $output;
Eric Barnesbb5d4f72011-03-18 13:39:58 -0400145
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000146 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 }
148
149 // --------------------------------------------------------------------
150
151 /**
152 * Append Output
153 *
154 * Appends data onto the output string
155 *
156 * @access public
157 * @param string
158 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200159 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 function append_output($output)
161 {
162 if ($this->final_output == '')
163 {
164 $this->final_output = $output;
165 }
166 else
167 {
168 $this->final_output .= $output;
169 }
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000170
171 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 }
173
174 // --------------------------------------------------------------------
175
176 /**
177 * Set Header
178 *
179 * Lets you set a server header which will be outputted with the final display.
180 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500181 * Note: If a file is cached, headers will not be sent. We need to figure out
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 * how to permit header data to be saved with the cache data...
183 *
184 * @access public
185 * @param string
David Behler07b53422011-08-15 00:25:06 +0200186 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200188 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 function set_header($header, $replace = TRUE)
190 {
Pascal Kriete676e1cd2010-04-09 21:16:16 +0200191 // If zlib.output_compression is enabled it will compress the output,
192 // but it will not modify the content-length header to compensate for
193 // the reduction, causing the browser to hang waiting for more data.
194 // We'll just skip content-length in those cases.
Barry Mienydd671972010-10-04 16:33:58 +0200195
Pascal Kriete676e1cd2010-04-09 21:16:16 +0200196 if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) == 0)
197 {
198 return;
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 $this->headers[] = array($header, $replace);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000202
203 return $this;
204 }
205
206 // --------------------------------------------------------------------
207
208 /**
209 * Set Content Type Header
210 *
211 * @access public
212 * @param string extension of the file we're outputting
213 * @return void
214 */
215 function set_content_type($mime_type)
216 {
217 if (strpos($mime_type, '/') === FALSE)
218 {
219 $extension = ltrim($mime_type, '.');
220
221 // Is this extension supported?
222 if (isset($this->mime_types[$extension]))
223 {
224 $mime_type =& $this->mime_types[$extension];
225
226 if (is_array($mime_type))
227 {
228 $mime_type = current($mime_type);
229 }
230 }
231 }
232
233 $header = 'Content-Type: '.$mime_type;
234
235 $this->headers[] = array($header, TRUE);
Eric Barnesbb5d4f72011-03-18 13:39:58 -0400236
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000237 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
239
240 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 /**
243 * Set HTTP Status Header
Derek Jones817163a2009-07-11 17:05:58 +0000244 * moved to Common procedural functions in 1.7.2
Barry Mienydd671972010-10-04 16:33:58 +0200245 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200247 * @param int the status code
248 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200250 */
Derek Jones46520492010-03-02 13:53:25 -0600251 function set_status_header($code = 200, $text = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
Derek Jones817163a2009-07-11 17:05:58 +0000253 set_status_header($code, $text);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000254
255 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 /**
261 * Enable/disable Profiler
262 *
263 * @access public
264 * @param bool
265 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200266 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 function enable_profiler($val = TRUE)
268 {
269 $this->enable_profiler = (is_bool($val)) ? $val : TRUE;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000270
271 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
Barry Mienydd671972010-10-04 16:33:58 +0200273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 // --------------------------------------------------------------------
Derek Jonesee71c802010-03-10 10:05:05 -0600275
276 /**
277 * Set Profiler Sections
278 *
279 * Allows override of default / config settings for Profiler section display
280 *
281 * @access public
282 * @param array
283 * @return void
284 */
285 function set_profiler_sections($sections)
286 {
287 foreach ($sections as $section => $enable)
288 {
289 $this->_profiler_sections[$section] = ($enable !== FALSE) ? TRUE : FALSE;
290 }
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000291
292 return $this;
Derek Jonesee71c802010-03-10 10:05:05 -0600293 }
294
295 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 /**
298 * Set Cache
299 *
300 * @access public
301 * @param integer
302 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200303 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 function cache($time)
305 {
306 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000307
308 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 /**
314 * Display Output
315 *
316 * All "view" data is automatically put into this variable by the controller class:
317 *
318 * $this->final_output
319 *
320 * This function sends the finalized output data to the browser along
Derek Jones37f4b9c2011-07-01 17:56:50 -0500321 * with any server headers and profile data. It also stops the
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 * benchmark timer so the page rendering speed and memory usage can be shown.
323 *
324 * @access public
David Behler07b53422011-08-15 00:25:06 +0200325 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200327 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 function _display($output = '')
Barry Mienydd671972010-10-04 16:33:58 +0200329 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500330 // Note: We use globals because we can't use $CI =& get_instance()
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // since this function is sometimes called by the caching mechanism,
332 // which happens before the CI super object is available.
333 global $BM, $CFG;
Derek Jonesd7633492010-09-28 13:14:57 -0500334
335 // Grab the super object if we can.
Greg Akercc922102010-11-17 20:25:08 -0600336 if (class_exists('CI_Controller'))
Derek Jonesd7633492010-09-28 13:14:57 -0500337 {
338 $CI =& get_instance();
339 }
340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 // Set the output data
344 if ($output == '')
345 {
346 $output =& $this->final_output;
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Jones37f4b9c2011-07-01 17:56:50 -0500351 // Do we need to write a cache file? Only if the controller does not have its
Derek Jonesd7633492010-09-28 13:14:57 -0500352 // own _output() method and we are not dealing with a cache file, which we
353 // can determine by the existence of the $CI object above
354 if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
356 $this->_write_cache($output);
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // --------------------------------------------------------------------
360
361 // Parse out the elapsed time and memory usage,
362 // then swap the pseudo-variables with the data
Barry Mienydd671972010-10-04 16:33:58 +0200363
364 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
Derek Jones46520492010-03-02 13:53:25 -0600365
366 if ($this->parse_exec_vars === TRUE)
367 {
368 $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Jones46520492010-03-02 13:53:25 -0600370 $output = str_replace('{elapsed_time}', $elapsed, $output);
371 $output = str_replace('{memory_usage}', $memory, $output);
372 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000373
374 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // Is compression requested?
Pascal Kriete676e1cd2010-04-09 21:16:16 +0200377 if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
379 if (extension_loaded('zlib'))
380 {
381 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
382 {
383 ob_start('ob_gzhandler');
384 }
385 }
386 }
387
388 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 // Are there any server headers to send?
391 if (count($this->headers) > 0)
392 {
393 foreach ($this->headers as $header)
394 {
395 @header($header[0], $header[1]);
396 }
Barry Mienydd671972010-10-04 16:33:58 +0200397 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000398
399 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Jonesd7633492010-09-28 13:14:57 -0500401 // Does the $CI object exist?
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 // If not we know we are dealing with a cache file so we'll
403 // simply echo out the data and exit.
Derek Jonesd7633492010-09-28 13:14:57 -0500404 if ( ! isset($CI))
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
406 echo $output;
407 log_message('debug', "Final output sent to browser");
408 log_message('debug', "Total execution time: ".$elapsed);
409 return TRUE;
410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 // Do we need to generate profile data?
415 // If so, load the Profile class and run it.
416 if ($this->enable_profiler == TRUE)
417 {
Barry Mienydd671972010-10-04 16:33:58 +0200418 $CI->load->library('profiler');
419
Derek Jonesee71c802010-03-10 10:05:05 -0600420 if ( ! empty($this->_profiler_sections))
421 {
422 $CI->profiler->set_sections($this->_profiler_sections);
Barry Mienydd671972010-10-04 16:33:58 +0200423 }
Derek Jonesee71c802010-03-10 10:05:05 -0600424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 // If the output data contains closing </body> and </html> tags
426 // we will remove them and add them back after we insert the profile data
427 if (preg_match("|</body>.*?</html>|is", $output))
428 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500429 $output = preg_replace("|</body>.*?</html>|is", '', $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 $output .= $CI->profiler->run();
431 $output .= '</body></html>';
432 }
433 else
434 {
435 $output .= $CI->profiler->run();
436 }
437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 // --------------------------------------------------------------------
440
441 // Does the controller contain a function named _output()?
Derek Jones37f4b9c2011-07-01 17:56:50 -0500442 // If so send the output there. Otherwise, echo it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 if (method_exists($CI, '_output'))
444 {
445 $CI->_output($output);
446 }
447 else
448 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500449 echo $output; // Send it to the browser!
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 log_message('debug', "Final output sent to browser");
Barry Mienydd671972010-10-04 16:33:58 +0200453 log_message('debug', "Total execution time: ".$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 /**
459 * Write a Cache File
460 *
461 * @access public
David Behler07b53422011-08-15 00:25:06 +0200462 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200464 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 function _write_cache($output)
466 {
Barry Mienydd671972010-10-04 16:33:58 +0200467 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 $path = $CI->config->item('cache_path');
Barry Mienydd671972010-10-04 16:33:58 +0200469
Greg Aker2eaa4072010-12-21 11:44:08 -0600470 $cache_path = ($path == '') ? APPPATH.'cache/' : $path;
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
473 {
Greg Akera7f5a942010-09-14 17:20:53 -0500474 log_message('error', "Unable to write cache file: ".$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 return;
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 $uri = $CI->config->item('base_url').
479 $CI->config->item('index_page').
480 $CI->uri->uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 $cache_path .= md5($uri);
483
484 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
485 {
486 log_message('error', "Unable to write cache file: ".$cache_path);
487 return;
488 }
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 $expire = time() + ($this->cache_expiration * 60);
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 if (flock($fp, LOCK_EX))
493 {
494 fwrite($fp, $expire.'TS--->'.$output);
495 flock($fp, LOCK_UN);
496 }
497 else
498 {
499 log_message('error', "Unable to secure a file lock for file at: ".$cache_path);
500 return;
501 }
502 fclose($fp);
Derek Jones172e1612009-10-13 14:32:48 +0000503 @chmod($cache_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000504
505 log_message('debug', "Cache file written: ".$cache_path);
506 }
507
508 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 /**
511 * Update/serve a cached file
512 *
513 * @access public
David Behler07b53422011-08-15 00:25:06 +0200514 * @param object config class
515 * @param object uri class
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200517 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 function _display_cache(&$CFG, &$URI)
519 {
Greg Aker2eaa4072010-12-21 11:44:08 -0600520 $cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path');
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Jones37f4b9c2011-07-01 17:56:50 -0500522 // Build the file path. The file name is an MD5 hash of the full URI
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 $uri = $CFG->item('base_url').
524 $CFG->item('index_page').
525 $URI->uri_string;
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 $filepath = $cache_path.md5($uri);
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 if ( ! @file_exists($filepath))
530 {
531 return FALSE;
532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 if ( ! $fp = @fopen($filepath, FOPEN_READ))
535 {
536 return FALSE;
537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 flock($fp, LOCK_SH);
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 $cache = '';
542 if (filesize($filepath) > 0)
543 {
544 $cache = fread($fp, filesize($filepath));
545 }
Barry Mienydd671972010-10-04 16:33:58 +0200546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 flock($fp, LOCK_UN);
548 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200549
550 // Strip out the embedded timestamp
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
552 {
553 return FALSE;
554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 // Has the file expired? If so we'll delete it.
557 if (time() >= trim(str_replace('TS--->', '', $match['1'])))
Derek Jonesd99e6032010-03-19 19:57:33 -0500558 {
559 if (is_really_writable($cache_path))
560 {
561 @unlink($filepath);
562 log_message('debug', "Cache file has expired. File deleted");
Barry Mienydd671972010-10-04 16:33:58 +0200563 return FALSE;
Derek Jonesd99e6032010-03-19 19:57:33 -0500564 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 }
566
567 // Display the cache
568 $this->_display(str_replace($match['0'], '', $cache));
Barry Mienydd671972010-10-04 16:33:58 +0200569 log_message('debug', "Cache file is current. Sending it to browser.");
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 return TRUE;
571 }
572
573
574}
575// END Output Class
576
577/* End of file Output.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600578/* Location: ./system/core/Output.php */