blob: 7fb9f79165accdc8a67c1b0fa5c7db94df36f32f [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Output Class
20 *
21 * Responsible for sending final output to browser
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @category Output
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/output.html
28 */
29class CI_Output {
30
31 var $final_output;
32 var $cache_expiration = 0;
Barry Mienydd671972010-10-04 16:33:58 +020033 var $headers = array();
34 var $enable_profiler = FALSE;
Derek Jones46520492010-03-02 13:53:25 -060035 var $parse_exec_vars = TRUE; // whether or not to parse variables like {elapsed_time} and {memory_usage}
Derek Allard2067d1a2008-11-13 22:59:24 +000036
Pascal Kriete676e1cd2010-04-09 21:16:16 +020037 var $_zlib_oc = FALSE;
Derek Jonesee71c802010-03-10 10:05:05 -060038 var $_profiler_sections = array();
39
Greg Akera9263282010-11-10 15:26:43 -060040 function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000041 {
Pascal Kriete676e1cd2010-04-09 21:16:16 +020042 $this->_zlib_oc = @ini_get('zlib.output_compression');
Barry Mienydd671972010-10-04 16:33:58 +020043
Derek Allard2067d1a2008-11-13 22:59:24 +000044 log_message('debug', "Output Class Initialized");
45 }
Barry Mienydd671972010-10-04 16:33:58 +020046
Derek Allard2067d1a2008-11-13 22:59:24 +000047 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020048
Derek Allard2067d1a2008-11-13 22:59:24 +000049 /**
50 * Get Output
51 *
52 * Returns the current output string
53 *
54 * @access public
55 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020056 */
Derek Allard2067d1a2008-11-13 22:59:24 +000057 function get_output()
58 {
59 return $this->final_output;
60 }
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064 /**
65 * Set Output
66 *
67 * Sets the output string
68 *
69 * @access public
70 * @param string
71 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020072 */
Derek Allard2067d1a2008-11-13 22:59:24 +000073 function set_output($output)
74 {
75 $this->final_output = $output;
76 }
77
78 // --------------------------------------------------------------------
79
80 /**
81 * Append Output
82 *
83 * Appends data onto the output string
84 *
85 * @access public
86 * @param string
87 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020088 */
Derek Allard2067d1a2008-11-13 22:59:24 +000089 function append_output($output)
90 {
91 if ($this->final_output == '')
92 {
93 $this->final_output = $output;
94 }
95 else
96 {
97 $this->final_output .= $output;
98 }
99 }
100
101 // --------------------------------------------------------------------
102
103 /**
104 * Set Header
105 *
106 * Lets you set a server header which will be outputted with the final display.
107 *
108 * Note: If a file is cached, headers will not be sent. We need to figure out
109 * how to permit header data to be saved with the cache data...
110 *
111 * @access public
112 * @param string
113 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200114 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 function set_header($header, $replace = TRUE)
116 {
Pascal Kriete676e1cd2010-04-09 21:16:16 +0200117 // If zlib.output_compression is enabled it will compress the output,
118 // but it will not modify the content-length header to compensate for
119 // the reduction, causing the browser to hang waiting for more data.
120 // We'll just skip content-length in those cases.
Barry Mienydd671972010-10-04 16:33:58 +0200121
Pascal Kriete676e1cd2010-04-09 21:16:16 +0200122 if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) == 0)
123 {
124 return;
125 }
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 $this->headers[] = array($header, $replace);
128 }
129
130 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 /**
133 * Set HTTP Status Header
Derek Jones817163a2009-07-11 17:05:58 +0000134 * moved to Common procedural functions in 1.7.2
Barry Mienydd671972010-10-04 16:33:58 +0200135 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200137 * @param int the status code
138 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200140 */
Derek Jones46520492010-03-02 13:53:25 -0600141 function set_status_header($code = 200, $text = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Derek Jones817163a2009-07-11 17:05:58 +0000143 set_status_header($code, $text);
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 /**
149 * Enable/disable Profiler
150 *
151 * @access public
152 * @param bool
153 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200154 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 function enable_profiler($val = TRUE)
156 {
157 $this->enable_profiler = (is_bool($val)) ? $val : TRUE;
158 }
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 // --------------------------------------------------------------------
Derek Jonesee71c802010-03-10 10:05:05 -0600161
162 /**
163 * Set Profiler Sections
164 *
165 * Allows override of default / config settings for Profiler section display
166 *
167 * @access public
168 * @param array
169 * @return void
170 */
171 function set_profiler_sections($sections)
172 {
173 foreach ($sections as $section => $enable)
174 {
175 $this->_profiler_sections[$section] = ($enable !== FALSE) ? TRUE : FALSE;
176 }
177 }
178
179 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 /**
182 * Set Cache
183 *
184 * @access public
185 * @param integer
186 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200187 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 function cache($time)
189 {
190 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
191 }
Barry Mienydd671972010-10-04 16:33:58 +0200192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 /**
196 * Display Output
197 *
198 * All "view" data is automatically put into this variable by the controller class:
199 *
200 * $this->final_output
201 *
202 * This function sends the finalized output data to the browser along
203 * with any server headers and profile data. It also stops the
204 * benchmark timer so the page rendering speed and memory usage can be shown.
205 *
206 * @access public
207 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200208 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 function _display($output = '')
Barry Mienydd671972010-10-04 16:33:58 +0200210 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 // Note: We use globals because we can't use $CI =& get_instance()
212 // since this function is sometimes called by the caching mechanism,
213 // which happens before the CI super object is available.
214 global $BM, $CFG;
Derek Jonesd7633492010-09-28 13:14:57 -0500215
216 // Grab the super object if we can.
Greg Akercc922102010-11-17 20:25:08 -0600217 if (class_exists('CI_Controller'))
Derek Jonesd7633492010-09-28 13:14:57 -0500218 {
219 $CI =& get_instance();
220 }
221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // Set the output data
225 if ($output == '')
226 {
227 $output =& $this->final_output;
228 }
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Jonesd7633492010-09-28 13:14:57 -0500232 // Do we need to write a cache file? Only if the controller does not have its
233 // own _output() method and we are not dealing with a cache file, which we
234 // can determine by the existence of the $CI object above
235 if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
237 $this->_write_cache($output);
238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 // --------------------------------------------------------------------
241
242 // Parse out the elapsed time and memory usage,
243 // then swap the pseudo-variables with the data
Barry Mienydd671972010-10-04 16:33:58 +0200244
245 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
Derek Jones46520492010-03-02 13:53:25 -0600246
247 if ($this->parse_exec_vars === TRUE)
248 {
249 $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Jones46520492010-03-02 13:53:25 -0600251 $output = str_replace('{elapsed_time}', $elapsed, $output);
252 $output = str_replace('{memory_usage}', $memory, $output);
253 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000254
255 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 // Is compression requested?
Pascal Kriete676e1cd2010-04-09 21:16:16 +0200258 if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
260 if (extension_loaded('zlib'))
261 {
262 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
263 {
264 ob_start('ob_gzhandler');
265 }
266 }
267 }
268
269 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // Are there any server headers to send?
272 if (count($this->headers) > 0)
273 {
274 foreach ($this->headers as $header)
275 {
276 @header($header[0], $header[1]);
277 }
Barry Mienydd671972010-10-04 16:33:58 +0200278 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000279
280 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Jonesd7633492010-09-28 13:14:57 -0500282 // Does the $CI object exist?
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 // If not we know we are dealing with a cache file so we'll
284 // simply echo out the data and exit.
Derek Jonesd7633492010-09-28 13:14:57 -0500285 if ( ! isset($CI))
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
287 echo $output;
288 log_message('debug', "Final output sent to browser");
289 log_message('debug', "Total execution time: ".$elapsed);
290 return TRUE;
291 }
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 // Do we need to generate profile data?
296 // If so, load the Profile class and run it.
297 if ($this->enable_profiler == TRUE)
298 {
Barry Mienydd671972010-10-04 16:33:58 +0200299 $CI->load->library('profiler');
300
Derek Jonesee71c802010-03-10 10:05:05 -0600301 if ( ! empty($this->_profiler_sections))
302 {
303 $CI->profiler->set_sections($this->_profiler_sections);
Barry Mienydd671972010-10-04 16:33:58 +0200304 }
Derek Jonesee71c802010-03-10 10:05:05 -0600305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // If the output data contains closing </body> and </html> tags
307 // we will remove them and add them back after we insert the profile data
308 if (preg_match("|</body>.*?</html>|is", $output))
309 {
310 $output = preg_replace("|</body>.*?</html>|is", '', $output);
311 $output .= $CI->profiler->run();
312 $output .= '</body></html>';
313 }
314 else
315 {
316 $output .= $CI->profiler->run();
317 }
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // --------------------------------------------------------------------
321
322 // Does the controller contain a function named _output()?
323 // If so send the output there. Otherwise, echo it.
324 if (method_exists($CI, '_output'))
325 {
326 $CI->_output($output);
327 }
328 else
329 {
330 echo $output; // Send it to the browser!
331 }
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 log_message('debug', "Final output sent to browser");
Barry Mienydd671972010-10-04 16:33:58 +0200334 log_message('debug', "Total execution time: ".$elapsed);
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 /**
340 * Write a Cache File
341 *
342 * @access public
343 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200344 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 function _write_cache($output)
346 {
Barry Mienydd671972010-10-04 16:33:58 +0200347 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 $path = $CI->config->item('cache_path');
Barry Mienydd671972010-10-04 16:33:58 +0200349
Greg Aker2eaa4072010-12-21 11:44:08 -0600350 $cache_path = ($path == '') ? APPPATH.'cache/' : $path;
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
353 {
Greg Akera7f5a942010-09-14 17:20:53 -0500354 log_message('error', "Unable to write cache file: ".$cache_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 return;
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 $uri = $CI->config->item('base_url').
359 $CI->config->item('index_page').
360 $CI->uri->uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 $cache_path .= md5($uri);
363
364 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
365 {
366 log_message('error', "Unable to write cache file: ".$cache_path);
367 return;
368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 $expire = time() + ($this->cache_expiration * 60);
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 if (flock($fp, LOCK_EX))
373 {
374 fwrite($fp, $expire.'TS--->'.$output);
375 flock($fp, LOCK_UN);
376 }
377 else
378 {
379 log_message('error', "Unable to secure a file lock for file at: ".$cache_path);
380 return;
381 }
382 fclose($fp);
Derek Jones172e1612009-10-13 14:32:48 +0000383 @chmod($cache_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000384
385 log_message('debug', "Cache file written: ".$cache_path);
386 }
387
388 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 /**
391 * Update/serve a cached file
392 *
393 * @access public
394 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200395 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 function _display_cache(&$CFG, &$URI)
397 {
Greg Aker2eaa4072010-12-21 11:44:08 -0600398 $cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path');
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 // Build the file path. The file name is an MD5 hash of the full URI
401 $uri = $CFG->item('base_url').
402 $CFG->item('index_page').
403 $URI->uri_string;
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 $filepath = $cache_path.md5($uri);
Barry Mienydd671972010-10-04 16:33:58 +0200406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 if ( ! @file_exists($filepath))
408 {
409 return FALSE;
410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 if ( ! $fp = @fopen($filepath, FOPEN_READ))
413 {
414 return FALSE;
415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 flock($fp, LOCK_SH);
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 $cache = '';
420 if (filesize($filepath) > 0)
421 {
422 $cache = fread($fp, filesize($filepath));
423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 flock($fp, LOCK_UN);
426 fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200427
428 // Strip out the embedded timestamp
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
430 {
431 return FALSE;
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 // Has the file expired? If so we'll delete it.
435 if (time() >= trim(str_replace('TS--->', '', $match['1'])))
Derek Jonesd99e6032010-03-19 19:57:33 -0500436 {
437 if (is_really_writable($cache_path))
438 {
439 @unlink($filepath);
440 log_message('debug', "Cache file has expired. File deleted");
Barry Mienydd671972010-10-04 16:33:58 +0200441 return FALSE;
Derek Jonesd99e6032010-03-19 19:57:33 -0500442 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
444
445 // Display the cache
446 $this->_display(str_replace($match['0'], '', $cache));
Barry Mienydd671972010-10-04 16:33:58 +0200447 log_message('debug', "Cache file is current. Sending it to browser.");
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 return TRUE;
449 }
450
451
452}
453// END Output Class
454
455/* End of file Output.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600456/* Location: ./system/core/Output.php */