blob: abd8a0ea9d19f1ce39045e80b40769b036db4dff [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 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Jones37f4b9c2011-07-01 17:56:50 -0500329 // Do we need to write a cache file? Only if the controller does not have its
Derek Jonesd7633492010-09-28 13:14:57 -0500330 // own _output() method and we are not dealing with a cache file, which we
331 // can determine by the existence of the $CI object above
332 if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 $this->_write_cache($output);
335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // --------------------------------------------------------------------
338
339 // Parse out the elapsed time and memory usage,
340 // then swap the pseudo-variables with the data
Barry Mienydd671972010-10-04 16:33:58 +0200341
342 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
Derek Jones46520492010-03-02 13:53:25 -0600343
344 if ($this->parse_exec_vars === TRUE)
345 {
346 $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
Barry Mienydd671972010-10-04 16:33:58 +0200347
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200348 $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output);
Derek Jones46520492010-03-02 13:53:25 -0600349 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000350
351 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 // Is compression requested?
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200354 if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE
355 && extension_loaded('zlib')
356 && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200358 ob_start('ob_gzhandler');
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
360
361 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // Are there any server headers to send?
364 if (count($this->headers) > 0)
365 {
366 foreach ($this->headers as $header)
367 {
368 @header($header[0], $header[1]);
369 }
Barry Mienydd671972010-10-04 16:33:58 +0200370 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000371
372 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Jonesd7633492010-09-28 13:14:57 -0500374 // Does the $CI object exist?
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 // If not we know we are dealing with a cache file so we'll
376 // simply echo out the data and exit.
Derek Jonesd7633492010-09-28 13:14:57 -0500377 if ( ! isset($CI))
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
379 echo $output;
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200380 log_message('debug', 'Final output sent to browser');
381 log_message('debug', 'Total execution time: '.$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 return TRUE;
383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 // Do we need to generate profile data?
388 // If so, load the Profile class and run it.
389 if ($this->enable_profiler == TRUE)
390 {
Barry Mienydd671972010-10-04 16:33:58 +0200391 $CI->load->library('profiler');
Derek Jonesee71c802010-03-10 10:05:05 -0600392 if ( ! empty($this->_profiler_sections))
393 {
394 $CI->profiler->set_sections($this->_profiler_sections);
Barry Mienydd671972010-10-04 16:33:58 +0200395 }
Derek Jonesee71c802010-03-10 10:05:05 -0600396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 // If the output data contains closing </body> and </html> tags
398 // we will remove them and add them back after we insert the profile data
Andrey Andreevcba20b12012-01-09 10:16:41 +0200399 $output = preg_replace('|</body>.*?</html>|is', '', $output, -1, $count).$CI->profiler->run();
400 if ($count > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 $output .= '</body></html>';
403 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 // Does the controller contain a function named _output()?
Derek Jones37f4b9c2011-07-01 17:56:50 -0500407 // If so send the output there. Otherwise, echo it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 if (method_exists($CI, '_output'))
409 {
410 $CI->_output($output);
411 }
412 else
413 {
Andrey Andreevedc87552012-01-09 09:35:10 +0200414 echo $output; // Send it to the browser!
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200417 log_message('debug', 'Final output sent to browser');
418 log_message('debug', 'Total execution time: '.$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 /**
424 * Write a Cache File
425 *
David Behler07b53422011-08-15 00:25:06 +0200426 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200428 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200429 public function _write_cache($output)
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
Barry Mienydd671972010-10-04 16:33:58 +0200431 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 $path = $CI->config->item('cache_path');
Greg Aker2eaa4072010-12-21 11:44:08 -0600433 $cache_path = ($path == '') ? APPPATH.'cache/' : $path;
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
436 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200437 log_message('error', 'Unable to write cache file: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 return;
439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 $uri = $CI->config->item('base_url').
442 $CI->config->item('index_page').
443 $CI->uri->uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 $cache_path .= md5($uri);
446
447 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
448 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200449 log_message('error', 'Unable to write cache file: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 return;
451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 $expire = time() + ($this->cache_expiration * 60);
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 if (flock($fp, LOCK_EX))
456 {
457 fwrite($fp, $expire.'TS--->'.$output);
458 flock($fp, LOCK_UN);
459 }
460 else
461 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200462 log_message('error', 'Unable to secure a file lock for file at: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 return;
464 }
465 fclose($fp);
Derek Jones172e1612009-10-13 14:32:48 +0000466 @chmod($cache_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000467
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200468 log_message('debug', 'Cache file written: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
470
471 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 /**
474 * Update/serve a cached file
475 *
David Behler07b53422011-08-15 00:25:06 +0200476 * @param object config class
477 * @param object uri class
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200479 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200480 public function _display_cache(&$CFG, &$URI)
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
Greg Aker2eaa4072010-12-21 11:44:08 -0600482 $cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path');
Barry Mienydd671972010-10-04 16:33:58 +0200483
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200484 // Build the file path. The file name is an MD5 hash of the full URI
485 $uri = $CFG->item('base_url').$CFG->item('index_page').$URI->uri_string;
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 $filepath = $cache_path.md5($uri);
Barry Mienydd671972010-10-04 16:33:58 +0200487
Andrey Andreevc90d6512012-01-08 04:35:02 +0200488 if ( ! @file_exists($filepath) OR ! $fp = @fopen($filepath, FOPEN_READ))
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
490 return FALSE;
491 }
Barry Mienydd671972010-10-04 16:33:58 +0200492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 flock($fp, LOCK_SH);
Barry Mienydd671972010-10-04 16:33:58 +0200494
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200495 $cache = (filesize($filepath) > 0) ? fread($fp, filesize($filepath)) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 flock($fp, LOCK_UN);
498 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200499
500 // Strip out the embedded timestamp
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200501 if ( ! preg_match('/(\d+TS--->)/', $cache, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
503 return FALSE;
504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 // Has the file expired? If so we'll delete it.
Andrey Andreevc90d6512012-01-08 04:35:02 +0200507 if (time() >= trim(str_replace('TS--->', '', $match[1])) && is_really_writable($cache_path))
Derek Jonesd99e6032010-03-19 19:57:33 -0500508 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200509 @unlink($filepath);
510 log_message('debug', 'Cache file has expired. File deleted.');
511 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
513
514 // Display the cache
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200515 $this->_display(str_replace($match[0], '', $cache));
516 log_message('debug', 'Cache file is current. Sending it to browser.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 return TRUE;
518 }
519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520}
Derek Allard2067d1a2008-11-13 22:59:24 +0000521
522/* End of file Output.php */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200523/* Location: ./system/core/Output.php */