blob: 5588ffe8e96e31d9be6f3d27ed5882b1c3d8b732 [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 */
Andrey Andreev6ef498b2012-06-05 22:01:58 +030067 public $mimes = 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 Andreev6ef498b2012-06-05 22:01:58 +0300107 $this->mimes =& get_mimes();
Greg Akerd96f8822011-12-27 16:23:47 -0600108
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200109 log_message('debug', 'Output Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 }
Barry Mienydd671972010-10-04 16:33:58 +0200111
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 /**
115 * Get Output
116 *
117 * Returns the current output string
118 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200120 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200121 public function get_output()
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
123 return $this->final_output;
124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 /**
129 * Set Output
130 *
131 * Sets the output string
132 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 * @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 {
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300154 if ($this->final_output == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
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 *
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300173 * 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.
Alex Bilbieed944a32012-06-02 11:07:47 +0100186 if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) === 0)
Pascal Kriete676e1cd2010-04-09 21:16:16 +0200187 {
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 Andreev47b67332012-06-06 15:58:05 +0300203 public function set_content_type($mime_type, $charset = NULL)
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?
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300210 if (isset($this->mimes[$extension]))
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000211 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300212 $mime_type =& $this->mimes[$extension];
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000213
214 if (is_array($mime_type))
215 {
216 $mime_type = current($mime_type);
217 }
218 }
219 }
220
Andrey Andreev47b67332012-06-06 15:58:05 +0300221 if (empty($charset))
222 {
223 $charset = config_item('charset');
224 }
225
226 $header = 'Content-Type: '.$mime_type
227 .(empty($charset) ? NULL : '; charset='.strtolower($charset));
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000228
229 $this->headers[] = array($header, TRUE);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000230 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300232
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700233 // --------------------------------------------------------------------
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300234
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700235 /**
236 * Get Current Content Type Header
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700237 *
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300238 * @return string 'text/html', if not already set
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700239 */
Songpol Sripaoeiam614db072012-04-03 01:29:28 +0700240 public function get_content_type()
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700241 {
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300242 for ($i = 0, $c = count($this->headers); $i < $c; $i++)
Songpol Sripao-eiam38c0a722012-04-01 20:10:35 +0700243 {
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300244 if (preg_match('/^Content-Type:\s(.+)$/', $this->headers[$i][0], $matches))
Songpol Sripaoeiamb9667012012-04-01 17:13:44 +0700245 {
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300246 return $matches[1];
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700247 }
Songpol Sripaoeiamb9667012012-04-01 17:13:44 +0700248 }
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300249
Songpol Sripaoeiamb9667012012-04-01 17:13:44 +0700250 return 'text/html';
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700251 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000252
253 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 /**
256 * Set HTTP Status Header
Derek Jones817163a2009-07-11 17:05:58 +0000257 * moved to Common procedural functions in 1.7.2
Barry Mienydd671972010-10-04 16:33:58 +0200258 *
Andrey Andreev7b53d042012-03-26 23:02:32 +0300259 * @param int the status code
Barry Mienydd671972010-10-04 16:33:58 +0200260 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200262 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200263 public function set_status_header($code = 200, $text = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
Derek Jones817163a2009-07-11 17:05:58 +0000265 set_status_header($code, $text);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000266 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 /**
272 * Enable/disable Profiler
273 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 * @param bool
275 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200276 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200277 public function enable_profiler($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
Andrey Andreev7b53d042012-03-26 23:02:32 +0300279 $this->enable_profiler = is_bool($val) ? $val : TRUE;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000280 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 // --------------------------------------------------------------------
Derek Jonesee71c802010-03-10 10:05:05 -0600284
285 /**
286 * Set Profiler Sections
287 *
288 * Allows override of default / config settings for Profiler section display
289 *
Derek Jonesee71c802010-03-10 10:05:05 -0600290 * @param array
291 * @return void
292 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200293 public function set_profiler_sections($sections)
Derek Jonesee71c802010-03-10 10:05:05 -0600294 {
295 foreach ($sections as $section => $enable)
296 {
Andrey Andreev7b53d042012-03-26 23:02:32 +0300297 $this->_profiler_sections[$section] = ($enable !== FALSE);
Derek Jonesee71c802010-03-10 10:05:05 -0600298 }
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000299
300 return $this;
Derek Jonesee71c802010-03-10 10:05:05 -0600301 }
302
303 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 /**
306 * Set Cache
307 *
Andrey Andreev7b53d042012-03-26 23:02:32 +0300308 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200310 */
Michiel Vugteveen0609d582012-01-08 13:26:17 +0100311 public function cache($time)
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
Andrey Andreev7b53d042012-03-26 23:02:32 +0300313 $this->cache_expiration = is_numeric($time) ? $time : 0;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000314 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 /**
320 * Display Output
321 *
322 * All "view" data is automatically put into this variable by the controller class:
323 *
324 * $this->final_output
325 *
326 * This function sends the finalized output data to the browser along
Andrey Andreev7b53d042012-03-26 23:02:32 +0300327 * with any server headers and profile data. It also stops the
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * benchmark timer so the page rendering speed and memory usage can be shown.
329 *
David Behler07b53422011-08-15 00:25:06 +0200330 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200332 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200333 public function _display($output = '')
Barry Mienydd671972010-10-04 16:33:58 +0200334 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500335 // Note: We use globals because we can't use $CI =& get_instance()
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 // since this function is sometimes called by the caching mechanism,
337 // which happens before the CI super object is available.
338 global $BM, $CFG;
Derek Jonesd7633492010-09-28 13:14:57 -0500339
340 // Grab the super object if we can.
Greg Akercc922102010-11-17 20:25:08 -0600341 if (class_exists('CI_Controller'))
Derek Jonesd7633492010-09-28 13:14:57 -0500342 {
343 $CI =& get_instance();
344 }
345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // Set the output data
Alex Bilbieed944a32012-06-02 11:07:47 +0100349 if ($output === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
351 $output =& $this->final_output;
352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Jones37f4b9c2011-07-01 17:56:50 -0500356 // Do we need to write a cache file? Only if the controller does not have its
Derek Jonesd7633492010-09-28 13:14:57 -0500357 // own _output() method and we are not dealing with a cache file, which we
358 // can determine by the existence of the $CI object above
359 if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
361 $this->_write_cache($output);
362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 // --------------------------------------------------------------------
365
366 // Parse out the elapsed time and memory usage,
367 // then swap the pseudo-variables with the data
Barry Mienydd671972010-10-04 16:33:58 +0200368
369 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
Derek Jones46520492010-03-02 13:53:25 -0600370
371 if ($this->parse_exec_vars === TRUE)
372 {
Andrey Andreevc839d282012-06-07 14:35:27 +0300373 $memory = round(memory_get_usage() / 1024 / 1024, 2).'MB';
Barry Mienydd671972010-10-04 16:33:58 +0200374
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200375 $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output);
Derek Jones46520492010-03-02 13:53:25 -0600376 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000377
378 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 // Is compression requested?
Alex Bilbieed944a32012-06-02 11:07:47 +0100381 if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc === FALSE
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200382 && extension_loaded('zlib')
383 && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200385 ob_start('ob_gzhandler');
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
387
388 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 // Are there any server headers to send?
391 if (count($this->headers) > 0)
392 {
393 foreach ($this->headers as $header)
394 {
395 @header($header[0], $header[1]);
396 }
Barry Mienydd671972010-10-04 16:33:58 +0200397 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000398
399 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Jonesd7633492010-09-28 13:14:57 -0500401 // Does the $CI object exist?
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 // If not we know we are dealing with a cache file so we'll
403 // simply echo out the data and exit.
Derek Jonesd7633492010-09-28 13:14:57 -0500404 if ( ! isset($CI))
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
406 echo $output;
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200407 log_message('debug', 'Final output sent to browser');
408 log_message('debug', 'Total execution time: '.$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 return TRUE;
410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 // Do we need to generate profile data?
415 // If so, load the Profile class and run it.
Alex Bilbieed944a32012-06-02 11:07:47 +0100416 if ($this->enable_profiler === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Barry Mienydd671972010-10-04 16:33:58 +0200418 $CI->load->library('profiler');
Derek Jonesee71c802010-03-10 10:05:05 -0600419 if ( ! empty($this->_profiler_sections))
420 {
421 $CI->profiler->set_sections($this->_profiler_sections);
Barry Mienydd671972010-10-04 16:33:58 +0200422 }
Derek Jonesee71c802010-03-10 10:05:05 -0600423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 // If the output data contains closing </body> and </html> tags
425 // we will remove them and add them back after we insert the profile data
Andrey Andreevcba20b12012-01-09 10:16:41 +0200426 $output = preg_replace('|</body>.*?</html>|is', '', $output, -1, $count).$CI->profiler->run();
427 if ($count > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 $output .= '</body></html>';
430 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 // Does the controller contain a function named _output()?
Derek Jones37f4b9c2011-07-01 17:56:50 -0500434 // If so send the output there. Otherwise, echo it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 if (method_exists($CI, '_output'))
436 {
437 $CI->_output($output);
438 }
439 else
440 {
Andrey Andreevedc87552012-01-09 09:35:10 +0200441 echo $output; // Send it to the browser!
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
Barry Mienydd671972010-10-04 16:33:58 +0200443
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200444 log_message('debug', 'Final output sent to browser');
445 log_message('debug', 'Total execution time: '.$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 /**
451 * Write a Cache File
452 *
David Behler07b53422011-08-15 00:25:06 +0200453 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200455 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200456 public function _write_cache($output)
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
Barry Mienydd671972010-10-04 16:33:58 +0200458 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 $path = $CI->config->item('cache_path');
Alex Bilbieed944a32012-06-02 11:07:47 +0100460 $cache_path = ($path === '') ? APPPATH.'cache/' : $path;
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
463 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200464 log_message('error', 'Unable to write cache file: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 return;
466 }
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 $uri = $CI->config->item('base_url').
469 $CI->config->item('index_page').
470 $CI->uri->uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 $cache_path .= md5($uri);
473
474 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
475 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200476 log_message('error', 'Unable to write cache file: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 return;
478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 $expire = time() + ($this->cache_expiration * 60);
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 if (flock($fp, LOCK_EX))
483 {
484 fwrite($fp, $expire.'TS--->'.$output);
485 flock($fp, LOCK_UN);
486 }
487 else
488 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200489 log_message('error', 'Unable to secure a file lock for file at: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 return;
491 }
492 fclose($fp);
Derek Jones172e1612009-10-13 14:32:48 +0000493 @chmod($cache_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000494
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200495 log_message('debug', 'Cache file written: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 }
497
498 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 /**
501 * Update/serve a cached file
502 *
David Behler07b53422011-08-15 00:25:06 +0200503 * @param object config class
504 * @param object uri class
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300505 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200506 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200507 public function _display_cache(&$CFG, &$URI)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100509 $cache_path = ($CFG->item('cache_path') === '') ? APPPATH.'cache/' : $CFG->item('cache_path');
Barry Mienydd671972010-10-04 16:33:58 +0200510
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200511 // Build the file path. The file name is an MD5 hash of the full URI
512 $uri = $CFG->item('base_url').$CFG->item('index_page').$URI->uri_string;
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 $filepath = $cache_path.md5($uri);
Barry Mienydd671972010-10-04 16:33:58 +0200514
Andrey Andreevc90d6512012-01-08 04:35:02 +0200515 if ( ! @file_exists($filepath) OR ! $fp = @fopen($filepath, FOPEN_READ))
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
517 return FALSE;
518 }
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 flock($fp, LOCK_SH);
Barry Mienydd671972010-10-04 16:33:58 +0200521
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200522 $cache = (filesize($filepath) > 0) ? fread($fp, filesize($filepath)) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 flock($fp, LOCK_UN);
525 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200526
527 // Strip out the embedded timestamp
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200528 if ( ! preg_match('/(\d+TS--->)/', $cache, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 {
530 return FALSE;
531 }
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 // Has the file expired? If so we'll delete it.
Andrey Andreevc90d6512012-01-08 04:35:02 +0200534 if (time() >= trim(str_replace('TS--->', '', $match[1])) && is_really_writable($cache_path))
Derek Jonesd99e6032010-03-19 19:57:33 -0500535 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200536 @unlink($filepath);
537 log_message('debug', 'Cache file has expired. File deleted.');
538 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 }
540
541 // Display the cache
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200542 $this->_display(str_replace($match[0], '', $cache));
543 log_message('debug', 'Cache file is current. Sending it to browser.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 return TRUE;
545 }
546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547}
Derek Allard2067d1a2008-11-13 22:59:24 +0000548
549/* End of file Output.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300550/* Location: ./system/core/Output.php */