blob: 9bf818e8838faa0eea678357cd2ec5620a520738 [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Output Class
30 *
31 * Responsible for sending final output to browser
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Output
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/output.html
38 */
39class CI_Output {
40
David Behler07b53422011-08-15 00:25:06 +020041 /**
42 * Current output string
43 *
44 * @var string
David Behler07b53422011-08-15 00:25:06 +020045 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000046 protected $final_output;
David Behler07b53422011-08-15 00:25:06 +020047 /**
48 * Cache expiration time
49 *
50 * @var int
David Behler07b53422011-08-15 00:25:06 +020051 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000052 protected $cache_expiration = 0;
David Behler07b53422011-08-15 00:25:06 +020053 /**
54 * List of server headers
55 *
56 * @var array
David Behler07b53422011-08-15 00:25:06 +020057 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000058 protected $headers = array();
David Behler07b53422011-08-15 00:25:06 +020059 /**
60 * List of mime types
61 *
62 * @var array
David Behler07b53422011-08-15 00:25:06 +020063 */
64 protected $mime_types = array();
65 /**
66 * Determines wether profiler is enabled
67 *
68 * @var book
David Behler07b53422011-08-15 00:25:06 +020069 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000070 protected $enable_profiler = FALSE;
David Behler07b53422011-08-15 00:25:06 +020071 /**
72 * Determines if output compression is enabled
73 *
74 * @var bool
David Behler07b53422011-08-15 00:25:06 +020075 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000076 protected $_zlib_oc = FALSE;
David Behler07b53422011-08-15 00:25:06 +020077 /**
78 * List of profiler sections
79 *
80 * @var array
David Behler07b53422011-08-15 00:25:06 +020081 */
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000082 protected $_profiler_sections = array();
David Behler07b53422011-08-15 00:25:06 +020083 /**
84 * Whether or not to parse variables like {elapsed_time} and {memory_usage}
85 *
86 * @var bool
David Behler07b53422011-08-15 00:25:06 +020087 */
88 protected $parse_exec_vars = TRUE;
Derek Jonesee71c802010-03-10 10:05:05 -060089
Andrey Andreev1f5fbb62012-01-07 20:53:29 +020090 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Pascal Kriete676e1cd2010-04-09 21:16:16 +020092 $this->_zlib_oc = @ini_get('zlib.output_compression');
Barry Mienydd671972010-10-04 16:33:58 +020093
Phil Sturgeon60ed1a32011-03-08 21:43:54 +000094 // Get mime types for later
Andrey Andreev7b53d042012-03-26 23:02:32 +030095 if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -060096 {
97 include APPPATH.'config/'.ENVIRONMENT.'/mimes.php';
98 }
99 else
100 {
101 include APPPATH.'config/mimes.php';
102 }
103
Eric Barnesbb5d4f72011-03-18 13:39:58 -0400104
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000105 $this->mime_types = $mimes;
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200106 log_message('debug', 'Output Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 /**
112 * Get Output
113 *
114 * Returns the current output string
115 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200117 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200118 public function get_output()
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 {
120 return $this->final_output;
121 }
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 /**
126 * Set Output
127 *
128 * Sets the output string
129 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 * @param string
131 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200132 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200133 public function set_output($output)
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
135 $this->final_output = $output;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000136 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
138
139 // --------------------------------------------------------------------
140
141 /**
142 * Append Output
143 *
144 * Appends data onto the output string
145 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 * @param string
147 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200148 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200149 public function append_output($output)
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
151 if ($this->final_output == '')
152 {
153 $this->final_output = $output;
154 }
155 else
156 {
157 $this->final_output .= $output;
158 }
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000159
160 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
162
163 // --------------------------------------------------------------------
164
165 /**
166 * Set Header
167 *
168 * Lets you set a server header which will be outputted with the final display.
169 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500170 * Note: If a file is cached, headers will not be sent. We need to figure out
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 * how to permit header data to be saved with the cache data...
172 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 * @param string
David Behler07b53422011-08-15 00:25:06 +0200174 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200176 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200177 public function set_header($header, $replace = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
Pascal Kriete676e1cd2010-04-09 21:16:16 +0200179 // If zlib.output_compression is enabled it will compress the output,
180 // but it will not modify the content-length header to compensate for
181 // the reduction, causing the browser to hang waiting for more data.
182 // We'll just skip content-length in those cases.
Pascal Kriete676e1cd2010-04-09 21:16:16 +0200183 if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) == 0)
184 {
185 return;
186 }
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 $this->headers[] = array($header, $replace);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000189 return $this;
190 }
191
192 // --------------------------------------------------------------------
193
194 /**
195 * Set Content Type Header
196 *
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000197 * @param string extension of the file we're outputting
198 * @return void
199 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200200 public function set_content_type($mime_type)
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000201 {
202 if (strpos($mime_type, '/') === FALSE)
203 {
204 $extension = ltrim($mime_type, '.');
205
206 // Is this extension supported?
207 if (isset($this->mime_types[$extension]))
208 {
209 $mime_type =& $this->mime_types[$extension];
210
211 if (is_array($mime_type))
212 {
213 $mime_type = current($mime_type);
214 }
215 }
216 }
217
218 $header = 'Content-Type: '.$mime_type;
219
220 $this->headers[] = array($header, TRUE);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000221 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
223
224 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 /**
227 * Set HTTP Status Header
Derek Jones817163a2009-07-11 17:05:58 +0000228 * moved to Common procedural functions in 1.7.2
Barry Mienydd671972010-10-04 16:33:58 +0200229 *
Andrey Andreev7b53d042012-03-26 23:02:32 +0300230 * @param int the status code
Barry Mienydd671972010-10-04 16:33:58 +0200231 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200233 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200234 public function set_status_header($code = 200, $text = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
Derek Jones817163a2009-07-11 17:05:58 +0000236 set_status_header($code, $text);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000237 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 /**
243 * Enable/disable Profiler
244 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 * @param bool
246 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200247 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200248 public function enable_profiler($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
Andrey Andreev7b53d042012-03-26 23:02:32 +0300250 $this->enable_profiler = is_bool($val) ? $val : TRUE;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000251 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // --------------------------------------------------------------------
Derek Jonesee71c802010-03-10 10:05:05 -0600255
256 /**
257 * Set Profiler Sections
258 *
259 * Allows override of default / config settings for Profiler section display
260 *
Derek Jonesee71c802010-03-10 10:05:05 -0600261 * @param array
262 * @return void
263 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200264 public function set_profiler_sections($sections)
Derek Jonesee71c802010-03-10 10:05:05 -0600265 {
266 foreach ($sections as $section => $enable)
267 {
Andrey Andreev7b53d042012-03-26 23:02:32 +0300268 $this->_profiler_sections[$section] = ($enable !== FALSE);
Derek Jonesee71c802010-03-10 10:05:05 -0600269 }
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000270
271 return $this;
Derek Jonesee71c802010-03-10 10:05:05 -0600272 }
273
274 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 /**
277 * Set Cache
278 *
Andrey Andreev7b53d042012-03-26 23:02:32 +0300279 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200281 */
Michiel Vugteveen0609d582012-01-08 13:26:17 +0100282 public function cache($time)
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 {
Andrey Andreev7b53d042012-03-26 23:02:32 +0300284 $this->cache_expiration = is_numeric($time) ? $time : 0;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000285 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 /**
291 * Display Output
292 *
293 * All "view" data is automatically put into this variable by the controller class:
294 *
295 * $this->final_output
296 *
297 * This function sends the finalized output data to the browser along
Andrey Andreev7b53d042012-03-26 23:02:32 +0300298 * with any server headers and profile data. It also stops the
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 * benchmark timer so the page rendering speed and memory usage can be shown.
300 *
David Behler07b53422011-08-15 00:25:06 +0200301 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200303 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200304 public function _display($output = '')
Barry Mienydd671972010-10-04 16:33:58 +0200305 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500306 // Note: We use globals because we can't use $CI =& get_instance()
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 // since this function is sometimes called by the caching mechanism,
308 // which happens before the CI super object is available.
309 global $BM, $CFG;
Derek Jonesd7633492010-09-28 13:14:57 -0500310
311 // Grab the super object if we can.
Greg Akercc922102010-11-17 20:25:08 -0600312 if (class_exists('CI_Controller'))
Derek Jonesd7633492010-09-28 13:14:57 -0500313 {
314 $CI =& get_instance();
315 }
316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // Set the output data
320 if ($output == '')
321 {
322 $output =& $this->final_output;
323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Jones37f4b9c2011-07-01 17:56:50 -0500327 // Do we need to write a cache file? Only if the controller does not have its
Derek Jonesd7633492010-09-28 13:14:57 -0500328 // own _output() method and we are not dealing with a cache file, which we
329 // can determine by the existence of the $CI object above
330 if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 $this->_write_cache($output);
333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // --------------------------------------------------------------------
336
337 // Parse out the elapsed time and memory usage,
338 // then swap the pseudo-variables with the data
Barry Mienydd671972010-10-04 16:33:58 +0200339
340 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
Derek Jones46520492010-03-02 13:53:25 -0600341
342 if ($this->parse_exec_vars === TRUE)
343 {
Andrey Andreev7b53d042012-03-26 23:02:32 +0300344 $memory = function_exists('memory_get_usage') ? round(memory_get_usage()/1024/1024, 2).'MB' : '0';
Barry Mienydd671972010-10-04 16:33:58 +0200345
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200346 $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output);
Derek Jones46520492010-03-02 13:53:25 -0600347 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000348
349 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // Is compression requested?
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200352 if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE
353 && extension_loaded('zlib')
354 && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200356 ob_start('ob_gzhandler');
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 }
358
359 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // Are there any server headers to send?
362 if (count($this->headers) > 0)
363 {
364 foreach ($this->headers as $header)
365 {
366 @header($header[0], $header[1]);
367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000369
370 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Jonesd7633492010-09-28 13:14:57 -0500372 // Does the $CI object exist?
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 // If not we know we are dealing with a cache file so we'll
374 // simply echo out the data and exit.
Derek Jonesd7633492010-09-28 13:14:57 -0500375 if ( ! isset($CI))
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
377 echo $output;
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200378 log_message('debug', 'Final output sent to browser');
379 log_message('debug', 'Total execution time: '.$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 return TRUE;
381 }
Barry Mienydd671972010-10-04 16:33:58 +0200382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 // Do we need to generate profile data?
386 // If so, load the Profile class and run it.
387 if ($this->enable_profiler == TRUE)
388 {
Barry Mienydd671972010-10-04 16:33:58 +0200389 $CI->load->library('profiler');
Derek Jonesee71c802010-03-10 10:05:05 -0600390 if ( ! empty($this->_profiler_sections))
391 {
392 $CI->profiler->set_sections($this->_profiler_sections);
Barry Mienydd671972010-10-04 16:33:58 +0200393 }
Derek Jonesee71c802010-03-10 10:05:05 -0600394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 // If the output data contains closing </body> and </html> tags
396 // we will remove them and add them back after we insert the profile data
Andrey Andreevcba20b12012-01-09 10:16:41 +0200397 $output = preg_replace('|</body>.*?</html>|is', '', $output, -1, $count).$CI->profiler->run();
398 if ($count > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 $output .= '</body></html>';
401 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 // Does the controller contain a function named _output()?
Derek Jones37f4b9c2011-07-01 17:56:50 -0500405 // If so send the output there. Otherwise, echo it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 if (method_exists($CI, '_output'))
407 {
408 $CI->_output($output);
409 }
410 else
411 {
Andrey Andreevedc87552012-01-09 09:35:10 +0200412 echo $output; // Send it to the browser!
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 }
Barry Mienydd671972010-10-04 16:33:58 +0200414
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200415 log_message('debug', 'Final output sent to browser');
416 log_message('debug', 'Total execution time: '.$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 /**
422 * Write a Cache File
423 *
David Behler07b53422011-08-15 00:25:06 +0200424 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200426 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200427 public function _write_cache($output)
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
Barry Mienydd671972010-10-04 16:33:58 +0200429 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 $path = $CI->config->item('cache_path');
Greg Aker2eaa4072010-12-21 11:44:08 -0600431 $cache_path = ($path == '') ? APPPATH.'cache/' : $path;
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
434 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200435 log_message('error', 'Unable to write cache file: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 return;
437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 $uri = $CI->config->item('base_url').
440 $CI->config->item('index_page').
441 $CI->uri->uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 $cache_path .= md5($uri);
444
445 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
446 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200447 log_message('error', 'Unable to write cache file: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 return;
449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 $expire = time() + ($this->cache_expiration * 60);
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 if (flock($fp, LOCK_EX))
454 {
455 fwrite($fp, $expire.'TS--->'.$output);
456 flock($fp, LOCK_UN);
457 }
458 else
459 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200460 log_message('error', 'Unable to secure a file lock for file at: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 return;
462 }
463 fclose($fp);
Derek Jones172e1612009-10-13 14:32:48 +0000464 @chmod($cache_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000465
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200466 log_message('debug', 'Cache file written: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
468
469 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 /**
472 * Update/serve a cached file
473 *
David Behler07b53422011-08-15 00:25:06 +0200474 * @param object config class
475 * @param object uri class
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200477 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200478 public function _display_cache(&$CFG, &$URI)
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
Greg Aker2eaa4072010-12-21 11:44:08 -0600480 $cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path');
Barry Mienydd671972010-10-04 16:33:58 +0200481
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200482 // Build the file path. The file name is an MD5 hash of the full URI
483 $uri = $CFG->item('base_url').$CFG->item('index_page').$URI->uri_string;
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 $filepath = $cache_path.md5($uri);
Barry Mienydd671972010-10-04 16:33:58 +0200485
Andrey Andreevc90d6512012-01-08 04:35:02 +0200486 if ( ! @file_exists($filepath) OR ! $fp = @fopen($filepath, FOPEN_READ))
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
488 return FALSE;
489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 flock($fp, LOCK_SH);
Barry Mienydd671972010-10-04 16:33:58 +0200492
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200493 $cache = (filesize($filepath) > 0) ? fread($fp, filesize($filepath)) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 flock($fp, LOCK_UN);
496 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200497
498 // Strip out the embedded timestamp
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200499 if ( ! preg_match('/(\d+TS--->)/', $cache, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
501 return FALSE;
502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 // Has the file expired? If so we'll delete it.
Andrey Andreevc90d6512012-01-08 04:35:02 +0200505 if (time() >= trim(str_replace('TS--->', '', $match[1])) && is_really_writable($cache_path))
Derek Jonesd99e6032010-03-19 19:57:33 -0500506 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200507 @unlink($filepath);
508 log_message('debug', 'Cache file has expired. File deleted.');
509 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
511
512 // Display the cache
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200513 $this->_display(str_replace($match[0], '', $cache));
514 log_message('debug', 'Cache file is current. Sending it to browser.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 return TRUE;
516 }
517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518}
Derek Allard2067d1a2008-11-13 22:59:24 +0000519
520/* End of file Output.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300521/* Location: ./system/core/Output.php */