blob: ad9ffbabe07c182da6a9d88fec1cec370f1206a5 [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 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, 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;
33 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
Derek Allard2067d1a2008-11-13 22:59:24 +000040 function CI_Output()
41 {
Pascal Kriete676e1cd2010-04-09 21:16:16 +020042 $this->_zlib_oc = @ini_get('zlib.output_compression');
43
Derek Allard2067d1a2008-11-13 22:59:24 +000044 log_message('debug', "Output Class Initialized");
45 }
46
47 // --------------------------------------------------------------------
48
49 /**
50 * Get Output
51 *
52 * Returns the current output string
53 *
54 * @access public
55 * @return string
56 */
57 function get_output()
58 {
59 return $this->final_output;
60 }
61
62 // --------------------------------------------------------------------
63
64 /**
65 * Set Output
66 *
67 * Sets the output string
68 *
69 * @access public
70 * @param string
71 * @return void
72 */
73 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
88 */
89 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
114 */
115 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.
121
122 if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) == 0)
123 {
124 return;
125 }
126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 $this->headers[] = array($header, $replace);
128 }
129
130 // --------------------------------------------------------------------
131
132 /**
133 * Set HTTP Status Header
Derek Jones817163a2009-07-11 17:05:58 +0000134 * moved to Common procedural functions in 1.7.2
135 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 * @access public
137 * @param int the status code
138 * @param string
139 * @return void
140 */
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 }
145
146 // --------------------------------------------------------------------
147
148 /**
149 * Enable/disable Profiler
150 *
151 * @access public
152 * @param bool
153 * @return void
154 */
155 function enable_profiler($val = TRUE)
156 {
157 $this->enable_profiler = (is_bool($val)) ? $val : TRUE;
158 }
159
160 // --------------------------------------------------------------------
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 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000180
181 /**
182 * Set Cache
183 *
184 * @access public
185 * @param integer
186 * @return void
187 */
188 function cache($time)
189 {
190 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
191 }
192
193 // --------------------------------------------------------------------
194
195 /**
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
208 */
209 function _display($output = '')
210 {
211 // 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.
217 if (function_exists('get_instance'))
218 {
219 $CI =& get_instance();
220 }
221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 // --------------------------------------------------------------------
223
224 // Set the output data
225 if ($output == '')
226 {
227 $output =& $this->final_output;
228 }
229
230 // --------------------------------------------------------------------
231
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 }
239
240 // --------------------------------------------------------------------
241
242 // Parse out the elapsed time and memory usage,
243 // then swap the pseudo-variables with the data
Derek Allard2067d1a2008-11-13 22:59:24 +0000244
Derek Jones46520492010-03-02 13:53:25 -0600245 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
246
247 if ($this->parse_exec_vars === TRUE)
248 {
249 $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
250
251 $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 // --------------------------------------------------------------------
256
257 // 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 // --------------------------------------------------------------------
270
271 // 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 }
278 }
279
280 // --------------------------------------------------------------------
281
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 }
292
293 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000294
295 // Do we need to generate profile data?
296 // If so, load the Profile class and run it.
297 if ($this->enable_profiler == TRUE)
298 {
299 $CI->load->library('profiler');
Derek Jonesee71c802010-03-10 10:05:05 -0600300
301 if ( ! empty($this->_profiler_sections))
302 {
303 $CI->profiler->set_sections($this->_profiler_sections);
304 }
305
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 }
319
320 // --------------------------------------------------------------------
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 }
332
333 log_message('debug', "Final output sent to browser");
334 log_message('debug', "Total execution time: ".$elapsed);
335 }
336
337 // --------------------------------------------------------------------
338
339 /**
340 * Write a Cache File
341 *
342 * @access public
343 * @return void
344 */
345 function _write_cache($output)
346 {
347 $CI =& get_instance();
348 $path = $CI->config->item('cache_path');
349
350 $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
351
352 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 }
357
358 $uri = $CI->config->item('base_url').
359 $CI->config->item('index_page').
360 $CI->uri->uri_string();
361
362 $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 }
369
370 $expire = time() + ($this->cache_expiration * 60);
371
372 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 // --------------------------------------------------------------------
389
390 /**
391 * Update/serve a cached file
392 *
393 * @access public
394 * @return void
395 */
396 function _display_cache(&$CFG, &$URI)
397 {
398 $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path');
Derek Jonesd99e6032010-03-19 19:57:33 -0500399
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;
404
405 $filepath = $cache_path.md5($uri);
406
407 if ( ! @file_exists($filepath))
408 {
409 return FALSE;
410 }
411
412 if ( ! $fp = @fopen($filepath, FOPEN_READ))
413 {
414 return FALSE;
415 }
416
417 flock($fp, LOCK_SH);
418
419 $cache = '';
420 if (filesize($filepath) > 0)
421 {
422 $cache = fread($fp, filesize($filepath));
423 }
424
425 flock($fp, LOCK_UN);
426 fclose($fp);
Derek Jonesd99e6032010-03-19 19:57:33 -0500427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 // Strip out the embedded timestamp
429 if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
430 {
431 return FALSE;
432 }
433
434 // 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");
441 return FALSE;
442 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
444
445 // Display the cache
446 $this->_display(str_replace($match['0'], '', $cache));
447 log_message('debug', "Cache file is current. Sending it to browser.");
448 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 */