blob: 9b85b3ec4183ab3c241465493a8e44db73226bf3 [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 */
Andrey Andreev0f221172012-04-13 14:52:16 +030046 public $final_output;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030047
David Behler07b53422011-08-15 00:25:06 +020048 /**
49 * Cache expiration time
50 *
51 * @var int
David Behler07b53422011-08-15 00:25:06 +020052 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040053 public $cache_expiration = 0;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030054
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 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040060 public $headers = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030061
David Behler07b53422011-08-15 00:25:06 +020062 /**
63 * List of mime types
64 *
65 * @var array
David Behler07b53422011-08-15 00:25:06 +020066 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040067 public $mime_types = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030068
David Behler07b53422011-08-15 00:25:06 +020069 /**
70 * Determines wether profiler is enabled
71 *
72 * @var book
David Behler07b53422011-08-15 00:25:06 +020073 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040074 public $enable_profiler = FALSE;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030075
David Behler07b53422011-08-15 00:25:06 +020076 /**
77 * Determines if output compression is enabled
78 *
79 * @var bool
David Behler07b53422011-08-15 00:25:06 +020080 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040081 protected $_zlib_oc = FALSE;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030082
David Behler07b53422011-08-15 00:25:06 +020083 /**
84 * List of profiler sections
85 *
86 * @var array
David Behler07b53422011-08-15 00:25:06 +020087 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040088 protected $_profiler_sections = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030089
David Behler07b53422011-08-15 00:25:06 +020090 /**
91 * Whether or not to parse variables like {elapsed_time} and {memory_usage}
92 *
93 * @var bool
David Behler07b53422011-08-15 00:25:06 +020094 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040095 public $parse_exec_vars = TRUE;
Derek Jonesee71c802010-03-10 10:05:05 -060096
Timothy Warrenad475052012-04-19 13:21:06 -040097 /**
98 * Set up Output class
Andrey Andreev92ebfb62012-05-17 12:49:24 +030099 *
100 * @return void
Timothy Warrenad475052012-04-19 13:21:06 -0400101 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200102 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 {
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300104 $this->_zlib_oc = (bool) @ini_get('zlib.output_compression');
Barry Mienydd671972010-10-04 16:33:58 +0200105
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000106 // Get mime types for later
Andrey Andreev7b53d042012-03-26 23:02:32 +0300107 if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600108 {
109 include APPPATH.'config/'.ENVIRONMENT.'/mimes.php';
110 }
111 else
112 {
113 include APPPATH.'config/mimes.php';
114 }
115
Eric Barnesbb5d4f72011-03-18 13:39:58 -0400116
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000117 $this->mime_types = $mimes;
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200118 log_message('debug', 'Output Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 /**
124 * Get Output
125 *
126 * Returns the current output string
127 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200129 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200130 public function get_output()
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
132 return $this->final_output;
133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 /**
138 * Set Output
139 *
140 * Sets the output string
141 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 * @param string
143 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200144 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200145 public function set_output($output)
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
147 $this->final_output = $output;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000148 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * Append Output
155 *
156 * Appends data onto the output string
157 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 * @param string
159 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200160 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200161 public function append_output($output)
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300163 if ($this->final_output == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
165 $this->final_output = $output;
166 }
167 else
168 {
169 $this->final_output .= $output;
170 }
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000171
172 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
174
175 // --------------------------------------------------------------------
176
177 /**
178 * Set Header
179 *
180 * Lets you set a server header which will be outputted with the final display.
181 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300182 * Note: If a file is cached, headers will not be sent. We need to figure out
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 * how to permit header data to be saved with the cache data...
184 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 * @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 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200189 public function set_header($header, $replace = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
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.
Alex Bilbieed944a32012-06-02 11:07:47 +0100195 if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) === 0)
Pascal Kriete676e1cd2010-04-09 21:16:16 +0200196 {
197 return;
198 }
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 $this->headers[] = array($header, $replace);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000201 return $this;
202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Set Content Type Header
208 *
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000209 * @param string extension of the file we're outputting
210 * @return void
211 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200212 public function set_content_type($mime_type)
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000213 {
214 if (strpos($mime_type, '/') === FALSE)
215 {
216 $extension = ltrim($mime_type, '.');
217
218 // Is this extension supported?
219 if (isset($this->mime_types[$extension]))
220 {
221 $mime_type =& $this->mime_types[$extension];
222
223 if (is_array($mime_type))
224 {
225 $mime_type = current($mime_type);
226 }
227 }
228 }
229
230 $header = 'Content-Type: '.$mime_type;
231
232 $this->headers[] = array($header, TRUE);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000233 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300235
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700236 // --------------------------------------------------------------------
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300237
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700238 /**
239 * Get Current Content Type Header
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700240 *
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300241 * @return string 'text/html', if not already set
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700242 */
Songpol Sripaoeiam614db072012-04-03 01:29:28 +0700243 public function get_content_type()
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700244 {
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300245 for ($i = 0, $c = count($this->headers); $i < $c; $i++)
Songpol Sripao-eiam38c0a722012-04-01 20:10:35 +0700246 {
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300247 if (preg_match('/^Content-Type:\s(.+)$/', $this->headers[$i][0], $matches))
Songpol Sripaoeiamb9667012012-04-01 17:13:44 +0700248 {
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300249 return $matches[1];
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700250 }
Songpol Sripaoeiamb9667012012-04-01 17:13:44 +0700251 }
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300252
Songpol Sripaoeiamb9667012012-04-01 17:13:44 +0700253 return 'text/html';
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700254 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000255
256 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 /**
259 * Set HTTP Status Header
Derek Jones817163a2009-07-11 17:05:58 +0000260 * moved to Common procedural functions in 1.7.2
Barry Mienydd671972010-10-04 16:33:58 +0200261 *
Andrey Andreev7b53d042012-03-26 23:02:32 +0300262 * @param int the status code
Barry Mienydd671972010-10-04 16:33:58 +0200263 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200265 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200266 public function set_status_header($code = 200, $text = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
Derek Jones817163a2009-07-11 17:05:58 +0000268 set_status_header($code, $text);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000269 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 /**
275 * Enable/disable Profiler
276 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 * @param bool
278 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200279 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200280 public function enable_profiler($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
Andrey Andreev7b53d042012-03-26 23:02:32 +0300282 $this->enable_profiler = is_bool($val) ? $val : TRUE;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000283 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 }
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 // --------------------------------------------------------------------
Derek Jonesee71c802010-03-10 10:05:05 -0600287
288 /**
289 * Set Profiler Sections
290 *
291 * Allows override of default / config settings for Profiler section display
292 *
Derek Jonesee71c802010-03-10 10:05:05 -0600293 * @param array
294 * @return void
295 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200296 public function set_profiler_sections($sections)
Derek Jonesee71c802010-03-10 10:05:05 -0600297 {
298 foreach ($sections as $section => $enable)
299 {
Andrey Andreev7b53d042012-03-26 23:02:32 +0300300 $this->_profiler_sections[$section] = ($enable !== FALSE);
Derek Jonesee71c802010-03-10 10:05:05 -0600301 }
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000302
303 return $this;
Derek Jonesee71c802010-03-10 10:05:05 -0600304 }
305
306 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 /**
309 * Set Cache
310 *
Andrey Andreev7b53d042012-03-26 23:02:32 +0300311 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200313 */
Michiel Vugteveen0609d582012-01-08 13:26:17 +0100314 public function cache($time)
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
Andrey Andreev7b53d042012-03-26 23:02:32 +0300316 $this->cache_expiration = is_numeric($time) ? $time : 0;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000317 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 /**
323 * Display Output
324 *
325 * All "view" data is automatically put into this variable by the controller class:
326 *
327 * $this->final_output
328 *
329 * This function sends the finalized output data to the browser along
Andrey Andreev7b53d042012-03-26 23:02:32 +0300330 * with any server headers and profile data. It also stops the
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 * benchmark timer so the page rendering speed and memory usage can be shown.
332 *
David Behler07b53422011-08-15 00:25:06 +0200333 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200335 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200336 public function _display($output = '')
Barry Mienydd671972010-10-04 16:33:58 +0200337 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500338 // Note: We use globals because we can't use $CI =& get_instance()
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // since this function is sometimes called by the caching mechanism,
340 // which happens before the CI super object is available.
341 global $BM, $CFG;
Derek Jonesd7633492010-09-28 13:14:57 -0500342
343 // Grab the super object if we can.
Greg Akercc922102010-11-17 20:25:08 -0600344 if (class_exists('CI_Controller'))
Derek Jonesd7633492010-09-28 13:14:57 -0500345 {
346 $CI =& get_instance();
347 }
348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // Set the output data
Alex Bilbieed944a32012-06-02 11:07:47 +0100352 if ($output === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
354 $output =& $this->final_output;
355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Jones37f4b9c2011-07-01 17:56:50 -0500359 // Do we need to write a cache file? Only if the controller does not have its
Derek Jonesd7633492010-09-28 13:14:57 -0500360 // own _output() method and we are not dealing with a cache file, which we
361 // can determine by the existence of the $CI object above
362 if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
364 $this->_write_cache($output);
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // --------------------------------------------------------------------
368
369 // Parse out the elapsed time and memory usage,
370 // then swap the pseudo-variables with the data
Barry Mienydd671972010-10-04 16:33:58 +0200371
372 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
Derek Jones46520492010-03-02 13:53:25 -0600373
374 if ($this->parse_exec_vars === TRUE)
375 {
Andrey Andreev7b53d042012-03-26 23:02:32 +0300376 $memory = function_exists('memory_get_usage') ? round(memory_get_usage()/1024/1024, 2).'MB' : '0';
Barry Mienydd671972010-10-04 16:33:58 +0200377
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200378 $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output);
Derek Jones46520492010-03-02 13:53:25 -0600379 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000380
381 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 // Is compression requested?
Alex Bilbieed944a32012-06-02 11:07:47 +0100384 if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc === FALSE
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200385 && extension_loaded('zlib')
386 && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200388 ob_start('ob_gzhandler');
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 }
390
391 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 // Are there any server headers to send?
394 if (count($this->headers) > 0)
395 {
396 foreach ($this->headers as $header)
397 {
398 @header($header[0], $header[1]);
399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000401
402 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Jonesd7633492010-09-28 13:14:57 -0500404 // Does the $CI object exist?
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 // If not we know we are dealing with a cache file so we'll
406 // simply echo out the data and exit.
Derek Jonesd7633492010-09-28 13:14:57 -0500407 if ( ! isset($CI))
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
409 echo $output;
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200410 log_message('debug', 'Final output sent to browser');
411 log_message('debug', 'Total execution time: '.$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 return TRUE;
413 }
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 // Do we need to generate profile data?
418 // If so, load the Profile class and run it.
Alex Bilbieed944a32012-06-02 11:07:47 +0100419 if ($this->enable_profiler === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
Barry Mienydd671972010-10-04 16:33:58 +0200421 $CI->load->library('profiler');
Derek Jonesee71c802010-03-10 10:05:05 -0600422 if ( ! empty($this->_profiler_sections))
423 {
424 $CI->profiler->set_sections($this->_profiler_sections);
Barry Mienydd671972010-10-04 16:33:58 +0200425 }
Derek Jonesee71c802010-03-10 10:05:05 -0600426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 // If the output data contains closing </body> and </html> tags
428 // we will remove them and add them back after we insert the profile data
Andrey Andreevcba20b12012-01-09 10:16:41 +0200429 $output = preg_replace('|</body>.*?</html>|is', '', $output, -1, $count).$CI->profiler->run();
430 if ($count > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 $output .= '</body></html>';
433 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 // Does the controller contain a function named _output()?
Derek Jones37f4b9c2011-07-01 17:56:50 -0500437 // If so send the output there. Otherwise, echo it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 if (method_exists($CI, '_output'))
439 {
440 $CI->_output($output);
441 }
442 else
443 {
Andrey Andreevedc87552012-01-09 09:35:10 +0200444 echo $output; // Send it to the browser!
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
Barry Mienydd671972010-10-04 16:33:58 +0200446
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200447 log_message('debug', 'Final output sent to browser');
448 log_message('debug', 'Total execution time: '.$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 /**
454 * Write a Cache File
455 *
David Behler07b53422011-08-15 00:25:06 +0200456 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200458 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200459 public function _write_cache($output)
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
Barry Mienydd671972010-10-04 16:33:58 +0200461 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 $path = $CI->config->item('cache_path');
Alex Bilbieed944a32012-06-02 11:07:47 +0100463 $cache_path = ($path === '') ? APPPATH.'cache/' : $path;
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
466 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200467 log_message('error', 'Unable to write cache file: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 return;
469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 $uri = $CI->config->item('base_url').
472 $CI->config->item('index_page').
473 $CI->uri->uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 $cache_path .= md5($uri);
476
477 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
478 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200479 log_message('error', 'Unable to write cache file: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 return;
481 }
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 $expire = time() + ($this->cache_expiration * 60);
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 if (flock($fp, LOCK_EX))
486 {
487 fwrite($fp, $expire.'TS--->'.$output);
488 flock($fp, LOCK_UN);
489 }
490 else
491 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200492 log_message('error', 'Unable to secure a file lock for file at: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 return;
494 }
495 fclose($fp);
Derek Jones172e1612009-10-13 14:32:48 +0000496 @chmod($cache_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000497
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200498 log_message('debug', 'Cache file written: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 }
500
501 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 /**
504 * Update/serve a cached file
505 *
David Behler07b53422011-08-15 00:25:06 +0200506 * @param object config class
507 * @param object uri class
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300508 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200509 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200510 public function _display_cache(&$CFG, &$URI)
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100512 $cache_path = ($CFG->item('cache_path') === '') ? APPPATH.'cache/' : $CFG->item('cache_path');
Barry Mienydd671972010-10-04 16:33:58 +0200513
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200514 // Build the file path. The file name is an MD5 hash of the full URI
515 $uri = $CFG->item('base_url').$CFG->item('index_page').$URI->uri_string;
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 $filepath = $cache_path.md5($uri);
Barry Mienydd671972010-10-04 16:33:58 +0200517
Andrey Andreevc90d6512012-01-08 04:35:02 +0200518 if ( ! @file_exists($filepath) OR ! $fp = @fopen($filepath, FOPEN_READ))
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
520 return FALSE;
521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 flock($fp, LOCK_SH);
Barry Mienydd671972010-10-04 16:33:58 +0200524
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200525 $cache = (filesize($filepath) > 0) ? fread($fp, filesize($filepath)) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 flock($fp, LOCK_UN);
528 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200529
530 // Strip out the embedded timestamp
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200531 if ( ! preg_match('/(\d+TS--->)/', $cache, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
533 return FALSE;
534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 // Has the file expired? If so we'll delete it.
Andrey Andreevc90d6512012-01-08 04:35:02 +0200537 if (time() >= trim(str_replace('TS--->', '', $match[1])) && is_really_writable($cache_path))
Derek Jonesd99e6032010-03-19 19:57:33 -0500538 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200539 @unlink($filepath);
540 log_message('debug', 'Cache file has expired. File deleted.');
541 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 }
543
544 // Display the cache
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200545 $this->_display(str_replace($match[0], '', $cache));
546 log_message('debug', 'Cache file is current. Sending it to browser.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 return TRUE;
548 }
549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550}
Derek Allard2067d1a2008-11-13 22:59:24 +0000551
552/* End of file Output.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300553/* Location: ./system/core/Output.php */