blob: 1beee734f114b3bbeffe03f9b687c0af613d3a02 [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 *
132 * @access public
133 * @param string
134 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200135 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200136 public function set_output($output)
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
138 $this->final_output = $output;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000139 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
141
142 // --------------------------------------------------------------------
143
144 /**
145 * Append Output
146 *
147 * Appends data onto the output string
148 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 * @param string
150 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200151 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200152 public function append_output($output)
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 {
154 if ($this->final_output == '')
155 {
156 $this->final_output = $output;
157 }
158 else
159 {
160 $this->final_output .= $output;
161 }
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000162
163 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 }
165
166 // --------------------------------------------------------------------
167
168 /**
169 * Set Header
170 *
171 * Lets you set a server header which will be outputted with the final display.
172 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500173 * Note: If a file is cached, headers will not be sent. We need to figure out
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 * how to permit header data to be saved with the cache data...
175 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 * @param string
David Behler07b53422011-08-15 00:25:06 +0200177 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200179 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200180 public function set_header($header, $replace = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Pascal Kriete676e1cd2010-04-09 21:16:16 +0200182 // If zlib.output_compression is enabled it will compress the output,
183 // but it will not modify the content-length header to compensate for
184 // the reduction, causing the browser to hang waiting for more data.
185 // We'll just skip content-length in those cases.
Pascal Kriete676e1cd2010-04-09 21:16:16 +0200186 if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) == 0)
187 {
188 return;
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 $this->headers[] = array($header, $replace);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000192 return $this;
193 }
194
195 // --------------------------------------------------------------------
196
197 /**
198 * Set Content Type Header
199 *
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000200 * @param string extension of the file we're outputting
201 * @return void
202 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200203 public function set_content_type($mime_type)
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000204 {
205 if (strpos($mime_type, '/') === FALSE)
206 {
207 $extension = ltrim($mime_type, '.');
208
209 // Is this extension supported?
210 if (isset($this->mime_types[$extension]))
211 {
212 $mime_type =& $this->mime_types[$extension];
213
214 if (is_array($mime_type))
215 {
216 $mime_type = current($mime_type);
217 }
218 }
219 }
220
221 $header = 'Content-Type: '.$mime_type;
222
223 $this->headers[] = array($header, TRUE);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000224 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
226
227 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 /**
230 * Set HTTP Status Header
Derek Jones817163a2009-07-11 17:05:58 +0000231 * moved to Common procedural functions in 1.7.2
Barry Mienydd671972010-10-04 16:33:58 +0200232 *
Barry Mienydd671972010-10-04 16:33:58 +0200233 * @param int the status code
234 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200236 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200237 public function set_status_header($code = 200, $text = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
Derek Jones817163a2009-07-11 17:05:58 +0000239 set_status_header($code, $text);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000240 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 /**
246 * Enable/disable Profiler
247 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 * @param bool
249 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200250 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200251 public function enable_profiler($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
253 $this->enable_profiler = (is_bool($val)) ? $val : TRUE;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000254 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 // --------------------------------------------------------------------
Derek Jonesee71c802010-03-10 10:05:05 -0600258
259 /**
260 * Set Profiler Sections
261 *
262 * Allows override of default / config settings for Profiler section display
263 *
Derek Jonesee71c802010-03-10 10:05:05 -0600264 * @param array
265 * @return void
266 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200267 public function set_profiler_sections($sections)
Derek Jonesee71c802010-03-10 10:05:05 -0600268 {
269 foreach ($sections as $section => $enable)
270 {
271 $this->_profiler_sections[$section] = ($enable !== FALSE) ? TRUE : FALSE;
272 }
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000273
274 return $this;
Derek Jonesee71c802010-03-10 10:05:05 -0600275 }
276
277 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 /**
280 * Set Cache
281 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 * @param integer
283 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200284 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200285 publi function cache($time)
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
287 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000288 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 /**
294 * Display Output
295 *
296 * All "view" data is automatically put into this variable by the controller class:
297 *
298 * $this->final_output
299 *
300 * This function sends the finalized output data to the browser along
Derek Jones37f4b9c2011-07-01 17:56:50 -0500301 * with any server headers and profile data. It also stops the
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 * benchmark timer so the page rendering speed and memory usage can be shown.
303 *
David Behler07b53422011-08-15 00:25:06 +0200304 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200306 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200307 public function _display($output = '')
Barry Mienydd671972010-10-04 16:33:58 +0200308 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500309 // Note: We use globals because we can't use $CI =& get_instance()
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 // since this function is sometimes called by the caching mechanism,
311 // which happens before the CI super object is available.
312 global $BM, $CFG;
Derek Jonesd7633492010-09-28 13:14:57 -0500313
314 // Grab the super object if we can.
Greg Akercc922102010-11-17 20:25:08 -0600315 if (class_exists('CI_Controller'))
Derek Jonesd7633492010-09-28 13:14:57 -0500316 {
317 $CI =& get_instance();
318 }
319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // Set the output data
323 if ($output == '')
324 {
325 $output =& $this->final_output;
326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Jones37f4b9c2011-07-01 17:56:50 -0500330 // Do we need to write a cache file? Only if the controller does not have its
Derek Jonesd7633492010-09-28 13:14:57 -0500331 // own _output() method and we are not dealing with a cache file, which we
332 // can determine by the existence of the $CI object above
333 if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
335 $this->_write_cache($output);
336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // --------------------------------------------------------------------
339
340 // Parse out the elapsed time and memory usage,
341 // then swap the pseudo-variables with the data
Barry Mienydd671972010-10-04 16:33:58 +0200342
343 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
Derek Jones46520492010-03-02 13:53:25 -0600344
345 if ($this->parse_exec_vars === TRUE)
346 {
347 $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
Barry Mienydd671972010-10-04 16:33:58 +0200348
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200349 $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output);
Derek Jones46520492010-03-02 13:53:25 -0600350 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000351
352 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // Is compression requested?
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200355 if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE
356 && extension_loaded('zlib')
357 && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200359 ob_start('ob_gzhandler');
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 }
361
362 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 // Are there any server headers to send?
365 if (count($this->headers) > 0)
366 {
367 foreach ($this->headers as $header)
368 {
369 @header($header[0], $header[1]);
370 }
Barry Mienydd671972010-10-04 16:33:58 +0200371 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000372
373 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Jonesd7633492010-09-28 13:14:57 -0500375 // Does the $CI object exist?
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // If not we know we are dealing with a cache file so we'll
377 // simply echo out the data and exit.
Derek Jonesd7633492010-09-28 13:14:57 -0500378 if ( ! isset($CI))
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
380 echo $output;
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200381 log_message('debug', 'Final output sent to browser');
382 log_message('debug', 'Total execution time: '.$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 return TRUE;
384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 // Do we need to generate profile data?
389 // If so, load the Profile class and run it.
390 if ($this->enable_profiler == TRUE)
391 {
Barry Mienydd671972010-10-04 16:33:58 +0200392 $CI->load->library('profiler');
Derek Jonesee71c802010-03-10 10:05:05 -0600393 if ( ! empty($this->_profiler_sections))
394 {
395 $CI->profiler->set_sections($this->_profiler_sections);
Barry Mienydd671972010-10-04 16:33:58 +0200396 }
Derek Jonesee71c802010-03-10 10:05:05 -0600397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 // If the output data contains closing </body> and </html> tags
399 // we will remove them and add them back after we insert the profile data
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200400 $output = preg_replace('|</body>.*?</html>|is', '', $output, $count).$CI->profiler->run();
401 if ($count > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 $output .= '</body></html>';
404 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 // --------------------------------------------------------------------
408
409 // Does the controller contain a function named _output()?
Derek Jones37f4b9c2011-07-01 17:56:50 -0500410 // If so send the output there. Otherwise, echo it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 if (method_exists($CI, '_output'))
412 {
413 $CI->_output($output);
414 }
415 else
416 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500417 echo $output; // Send it to the browser!
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200420 log_message('debug', 'Final output sent to browser');
421 log_message('debug', 'Total execution time: '.$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 /**
427 * Write a Cache File
428 *
David Behler07b53422011-08-15 00:25:06 +0200429 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200431 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200432 public function _write_cache($output)
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
Barry Mienydd671972010-10-04 16:33:58 +0200434 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 $path = $CI->config->item('cache_path');
Greg Aker2eaa4072010-12-21 11:44:08 -0600436 $cache_path = ($path == '') ? APPPATH.'cache/' : $path;
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
439 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200440 log_message('error', 'Unable to write cache file: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 return;
442 }
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 $uri = $CI->config->item('base_url').
445 $CI->config->item('index_page').
446 $CI->uri->uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 $cache_path .= md5($uri);
449
450 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
451 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200452 log_message('error', 'Unable to write cache file: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 return;
454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 $expire = time() + ($this->cache_expiration * 60);
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 if (flock($fp, LOCK_EX))
459 {
460 fwrite($fp, $expire.'TS--->'.$output);
461 flock($fp, LOCK_UN);
462 }
463 else
464 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200465 log_message('error', 'Unable to secure a file lock for file at: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 return;
467 }
468 fclose($fp);
Derek Jones172e1612009-10-13 14:32:48 +0000469 @chmod($cache_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000470
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200471 log_message('debug', 'Cache file written: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
473
474 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 /**
477 * Update/serve a cached file
478 *
David Behler07b53422011-08-15 00:25:06 +0200479 * @param object config class
480 * @param object uri class
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200482 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200483 public function _display_cache(&$CFG, &$URI)
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
Greg Aker2eaa4072010-12-21 11:44:08 -0600485 $cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path');
Barry Mienydd671972010-10-04 16:33:58 +0200486
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200487 // Build the file path. The file name is an MD5 hash of the full URI
488 $uri = $CFG->item('base_url').$CFG->item('index_page').$URI->uri_string;
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 $filepath = $cache_path.md5($uri);
Barry Mienydd671972010-10-04 16:33:58 +0200490
Andrey Andreevc90d6512012-01-08 04:35:02 +0200491 if ( ! @file_exists($filepath) OR ! $fp = @fopen($filepath, FOPEN_READ))
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
493 return FALSE;
494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 flock($fp, LOCK_SH);
Barry Mienydd671972010-10-04 16:33:58 +0200497
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200498 $cache = (filesize($filepath) > 0) ? fread($fp, filesize($filepath)) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 flock($fp, LOCK_UN);
501 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200502
503 // Strip out the embedded timestamp
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200504 if ( ! preg_match('/(\d+TS--->)/', $cache, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 {
506 return FALSE;
507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 // Has the file expired? If so we'll delete it.
Andrey Andreevc90d6512012-01-08 04:35:02 +0200510 if (time() >= trim(str_replace('TS--->', '', $match[1])) && is_really_writable($cache_path))
Derek Jonesd99e6032010-03-19 19:57:33 -0500511 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200512 @unlink($filepath);
513 log_message('debug', 'Cache file has expired. File deleted.');
514 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 }
516
517 // Display the cache
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200518 $this->_display(str_replace($match[0], '', $cache));
519 log_message('debug', 'Cache file is current. Sending it to browser.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 return TRUE;
521 }
522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523}
Derek Allard2067d1a2008-11-13 22:59:24 +0000524
525/* End of file Output.php */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200526/* Location: ./system/core/Output.php */