blob: 09656711bf7a61cfb54373b69a975a1e1e232bc2 [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 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?
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
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 }
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300226
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700227 // --------------------------------------------------------------------
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300228
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700229 /**
230 * Get Current Content Type Header
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700231 *
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300232 * @return string 'text/html', if not already set
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700233 */
Songpol Sripaoeiam614db072012-04-03 01:29:28 +0700234 public function get_content_type()
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700235 {
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300236 for ($i = 0, $c = count($this->headers); $i < $c; $i++)
Songpol Sripao-eiam38c0a722012-04-01 20:10:35 +0700237 {
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300238 if (preg_match('/^Content-Type:\s(.+)$/', $this->headers[$i][0], $matches))
Songpol Sripaoeiamb9667012012-04-01 17:13:44 +0700239 {
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300240 return $matches[1];
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700241 }
Songpol Sripaoeiamb9667012012-04-01 17:13:44 +0700242 }
Andrey Andreev00adf1d2012-04-03 12:30:50 +0300243
Songpol Sripaoeiamb9667012012-04-01 17:13:44 +0700244 return 'text/html';
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +0700245 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000246
247 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 /**
250 * Set HTTP Status Header
Derek Jones817163a2009-07-11 17:05:58 +0000251 * moved to Common procedural functions in 1.7.2
Barry Mienydd671972010-10-04 16:33:58 +0200252 *
Andrey Andreev7b53d042012-03-26 23:02:32 +0300253 * @param int the status code
Barry Mienydd671972010-10-04 16:33:58 +0200254 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200256 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200257 public function set_status_header($code = 200, $text = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
Derek Jones817163a2009-07-11 17:05:58 +0000259 set_status_header($code, $text);
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000260 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 /**
266 * Enable/disable Profiler
267 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 * @param bool
269 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200270 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200271 public function enable_profiler($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
Andrey Andreev7b53d042012-03-26 23:02:32 +0300273 $this->enable_profiler = is_bool($val) ? $val : TRUE;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000274 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 }
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 // --------------------------------------------------------------------
Derek Jonesee71c802010-03-10 10:05:05 -0600278
279 /**
280 * Set Profiler Sections
281 *
282 * Allows override of default / config settings for Profiler section display
283 *
Derek Jonesee71c802010-03-10 10:05:05 -0600284 * @param array
285 * @return void
286 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200287 public function set_profiler_sections($sections)
Derek Jonesee71c802010-03-10 10:05:05 -0600288 {
289 foreach ($sections as $section => $enable)
290 {
Andrey Andreev7b53d042012-03-26 23:02:32 +0300291 $this->_profiler_sections[$section] = ($enable !== FALSE);
Derek Jonesee71c802010-03-10 10:05:05 -0600292 }
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000293
294 return $this;
Derek Jonesee71c802010-03-10 10:05:05 -0600295 }
296
297 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 /**
300 * Set Cache
301 *
Andrey Andreev7b53d042012-03-26 23:02:32 +0300302 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200304 */
Michiel Vugteveen0609d582012-01-08 13:26:17 +0100305 public function cache($time)
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
Andrey Andreev7b53d042012-03-26 23:02:32 +0300307 $this->cache_expiration = is_numeric($time) ? $time : 0;
Phil Sturgeon60ed1a32011-03-08 21:43:54 +0000308 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 /**
314 * Display Output
315 *
316 * All "view" data is automatically put into this variable by the controller class:
317 *
318 * $this->final_output
319 *
320 * This function sends the finalized output data to the browser along
Andrey Andreev7b53d042012-03-26 23:02:32 +0300321 * with any server headers and profile data. It also stops the
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 * benchmark timer so the page rendering speed and memory usage can be shown.
323 *
David Behler07b53422011-08-15 00:25:06 +0200324 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200326 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200327 public function _display($output = '')
Barry Mienydd671972010-10-04 16:33:58 +0200328 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500329 // Note: We use globals because we can't use $CI =& get_instance()
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // since this function is sometimes called by the caching mechanism,
331 // which happens before the CI super object is available.
332 global $BM, $CFG;
Derek Jonesd7633492010-09-28 13:14:57 -0500333
334 // Grab the super object if we can.
Greg Akercc922102010-11-17 20:25:08 -0600335 if (class_exists('CI_Controller'))
Derek Jonesd7633492010-09-28 13:14:57 -0500336 {
337 $CI =& get_instance();
338 }
339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // Set the output data
Alex Bilbieed944a32012-06-02 11:07:47 +0100343 if ($output === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
345 $output =& $this->final_output;
346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Jones37f4b9c2011-07-01 17:56:50 -0500350 // Do we need to write a cache file? Only if the controller does not have its
Derek Jonesd7633492010-09-28 13:14:57 -0500351 // own _output() method and we are not dealing with a cache file, which we
352 // can determine by the existence of the $CI object above
353 if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
355 $this->_write_cache($output);
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // --------------------------------------------------------------------
359
360 // Parse out the elapsed time and memory usage,
361 // then swap the pseudo-variables with the data
Barry Mienydd671972010-10-04 16:33:58 +0200362
363 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
Derek Jones46520492010-03-02 13:53:25 -0600364
365 if ($this->parse_exec_vars === TRUE)
366 {
Andrey Andreev7b53d042012-03-26 23:02:32 +0300367 $memory = function_exists('memory_get_usage') ? round(memory_get_usage()/1024/1024, 2).'MB' : '0';
Barry Mienydd671972010-10-04 16:33:58 +0200368
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200369 $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output);
Derek Jones46520492010-03-02 13:53:25 -0600370 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000371
372 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 // Is compression requested?
Alex Bilbieed944a32012-06-02 11:07:47 +0100375 if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc === FALSE
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200376 && extension_loaded('zlib')
377 && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200379 ob_start('ob_gzhandler');
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 }
381
382 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 // Are there any server headers to send?
385 if (count($this->headers) > 0)
386 {
387 foreach ($this->headers as $header)
388 {
389 @header($header[0], $header[1]);
390 }
Barry Mienydd671972010-10-04 16:33:58 +0200391 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000392
393 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Jonesd7633492010-09-28 13:14:57 -0500395 // Does the $CI object exist?
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 // If not we know we are dealing with a cache file so we'll
397 // simply echo out the data and exit.
Derek Jonesd7633492010-09-28 13:14:57 -0500398 if ( ! isset($CI))
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
400 echo $output;
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200401 log_message('debug', 'Final output sent to browser');
402 log_message('debug', 'Total execution time: '.$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 return TRUE;
404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 // Do we need to generate profile data?
409 // If so, load the Profile class and run it.
Alex Bilbieed944a32012-06-02 11:07:47 +0100410 if ($this->enable_profiler === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
Barry Mienydd671972010-10-04 16:33:58 +0200412 $CI->load->library('profiler');
Derek Jonesee71c802010-03-10 10:05:05 -0600413 if ( ! empty($this->_profiler_sections))
414 {
415 $CI->profiler->set_sections($this->_profiler_sections);
Barry Mienydd671972010-10-04 16:33:58 +0200416 }
Derek Jonesee71c802010-03-10 10:05:05 -0600417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // If the output data contains closing </body> and </html> tags
419 // we will remove them and add them back after we insert the profile data
Andrey Andreevcba20b12012-01-09 10:16:41 +0200420 $output = preg_replace('|</body>.*?</html>|is', '', $output, -1, $count).$CI->profiler->run();
421 if ($count > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 $output .= '</body></html>';
424 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 }
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 // Does the controller contain a function named _output()?
Derek Jones37f4b9c2011-07-01 17:56:50 -0500428 // If so send the output there. Otherwise, echo it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 if (method_exists($CI, '_output'))
430 {
431 $CI->_output($output);
432 }
433 else
434 {
Andrey Andreevedc87552012-01-09 09:35:10 +0200435 echo $output; // Send it to the browser!
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200438 log_message('debug', 'Final output sent to browser');
439 log_message('debug', 'Total execution time: '.$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 /**
445 * Write a Cache File
446 *
David Behler07b53422011-08-15 00:25:06 +0200447 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200449 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200450 public function _write_cache($output)
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
Barry Mienydd671972010-10-04 16:33:58 +0200452 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 $path = $CI->config->item('cache_path');
Alex Bilbieed944a32012-06-02 11:07:47 +0100454 $cache_path = ($path === '') ? APPPATH.'cache/' : $path;
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
457 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200458 log_message('error', 'Unable to write cache file: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 return;
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 $uri = $CI->config->item('base_url').
463 $CI->config->item('index_page').
464 $CI->uri->uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 $cache_path .= md5($uri);
467
468 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
469 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200470 log_message('error', 'Unable to write cache file: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 return;
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 $expire = time() + ($this->cache_expiration * 60);
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 if (flock($fp, LOCK_EX))
477 {
478 fwrite($fp, $expire.'TS--->'.$output);
479 flock($fp, LOCK_UN);
480 }
481 else
482 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200483 log_message('error', 'Unable to secure a file lock for file at: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 return;
485 }
486 fclose($fp);
Derek Jones172e1612009-10-13 14:32:48 +0000487 @chmod($cache_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000488
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200489 log_message('debug', 'Cache file written: '.$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
491
492 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 /**
495 * Update/serve a cached file
496 *
David Behler07b53422011-08-15 00:25:06 +0200497 * @param object config class
498 * @param object uri class
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300499 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200500 */
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200501 public function _display_cache(&$CFG, &$URI)
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100503 $cache_path = ($CFG->item('cache_path') === '') ? APPPATH.'cache/' : $CFG->item('cache_path');
Barry Mienydd671972010-10-04 16:33:58 +0200504
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200505 // Build the file path. The file name is an MD5 hash of the full URI
506 $uri = $CFG->item('base_url').$CFG->item('index_page').$URI->uri_string;
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 $filepath = $cache_path.md5($uri);
Barry Mienydd671972010-10-04 16:33:58 +0200508
Andrey Andreevc90d6512012-01-08 04:35:02 +0200509 if ( ! @file_exists($filepath) OR ! $fp = @fopen($filepath, FOPEN_READ))
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 {
511 return FALSE;
512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 flock($fp, LOCK_SH);
Barry Mienydd671972010-10-04 16:33:58 +0200515
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200516 $cache = (filesize($filepath) > 0) ? fread($fp, filesize($filepath)) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 flock($fp, LOCK_UN);
519 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200520
521 // Strip out the embedded timestamp
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200522 if ( ! preg_match('/(\d+TS--->)/', $cache, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
524 return FALSE;
525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 // Has the file expired? If so we'll delete it.
Andrey Andreevc90d6512012-01-08 04:35:02 +0200528 if (time() >= trim(str_replace('TS--->', '', $match[1])) && is_really_writable($cache_path))
Derek Jonesd99e6032010-03-19 19:57:33 -0500529 {
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200530 @unlink($filepath);
531 log_message('debug', 'Cache file has expired. File deleted.');
532 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 }
534
535 // Display the cache
Andrey Andreev1f5fbb62012-01-07 20:53:29 +0200536 $this->_display(str_replace($match[0], '', $cache));
537 log_message('debug', 'Cache file is current. Sending it to browser.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 return TRUE;
539 }
540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541}
Derek Allard2067d1a2008-11-13 22:59:24 +0000542
543/* End of file Output.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300544/* Location: ./system/core/Output.php */