blob: e25e6219720922bfd349d8895c25088dbde2f3fc [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;
215
216 // --------------------------------------------------------------------
217
218 // Set the output data
219 if ($output == '')
220 {
221 $output =& $this->final_output;
222 }
223
224 // --------------------------------------------------------------------
225
226 // Do we need to write a cache file?
227 if ($this->cache_expiration > 0)
228 {
229 $this->_write_cache($output);
230 }
231
232 // --------------------------------------------------------------------
233
234 // Parse out the elapsed time and memory usage,
235 // then swap the pseudo-variables with the data
Derek Allard2067d1a2008-11-13 22:59:24 +0000236
Derek Jones46520492010-03-02 13:53:25 -0600237 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
238
239 if ($this->parse_exec_vars === TRUE)
240 {
241 $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
242
243 $output = str_replace('{elapsed_time}', $elapsed, $output);
244 $output = str_replace('{memory_usage}', $memory, $output);
245 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000246
247 // --------------------------------------------------------------------
248
249 // Is compression requested?
Pascal Kriete676e1cd2010-04-09 21:16:16 +0200250 if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
252 if (extension_loaded('zlib'))
253 {
254 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
255 {
256 ob_start('ob_gzhandler');
257 }
258 }
259 }
260
261 // --------------------------------------------------------------------
262
263 // Are there any server headers to send?
264 if (count($this->headers) > 0)
265 {
266 foreach ($this->headers as $header)
267 {
268 @header($header[0], $header[1]);
269 }
270 }
271
272 // --------------------------------------------------------------------
273
274 // Does the get_instance() function exist?
275 // If not we know we are dealing with a cache file so we'll
276 // simply echo out the data and exit.
277 if ( ! function_exists('get_instance'))
278 {
279 echo $output;
280 log_message('debug', "Final output sent to browser");
281 log_message('debug', "Total execution time: ".$elapsed);
282 return TRUE;
283 }
284
285 // --------------------------------------------------------------------
286
287 // Grab the super object. We'll need it in a moment...
288 $CI =& get_instance();
289
290 // Do we need to generate profile data?
291 // If so, load the Profile class and run it.
292 if ($this->enable_profiler == TRUE)
293 {
294 $CI->load->library('profiler');
Derek Jonesee71c802010-03-10 10:05:05 -0600295
296 if ( ! empty($this->_profiler_sections))
297 {
298 $CI->profiler->set_sections($this->_profiler_sections);
299 }
300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // If the output data contains closing </body> and </html> tags
302 // we will remove them and add them back after we insert the profile data
303 if (preg_match("|</body>.*?</html>|is", $output))
304 {
305 $output = preg_replace("|</body>.*?</html>|is", '', $output);
306 $output .= $CI->profiler->run();
307 $output .= '</body></html>';
308 }
309 else
310 {
311 $output .= $CI->profiler->run();
312 }
313 }
314
315 // --------------------------------------------------------------------
316
317 // Does the controller contain a function named _output()?
318 // If so send the output there. Otherwise, echo it.
319 if (method_exists($CI, '_output'))
320 {
321 $CI->_output($output);
322 }
323 else
324 {
325 echo $output; // Send it to the browser!
326 }
327
328 log_message('debug', "Final output sent to browser");
329 log_message('debug', "Total execution time: ".$elapsed);
330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Write a Cache File
336 *
337 * @access public
338 * @return void
339 */
340 function _write_cache($output)
341 {
342 $CI =& get_instance();
343 $path = $CI->config->item('cache_path');
344
345 $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
346
347 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
348 {
349 return;
350 }
351
352 $uri = $CI->config->item('base_url').
353 $CI->config->item('index_page').
354 $CI->uri->uri_string();
355
356 $cache_path .= md5($uri);
357
358 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
359 {
360 log_message('error', "Unable to write cache file: ".$cache_path);
361 return;
362 }
363
364 $expire = time() + ($this->cache_expiration * 60);
365
366 if (flock($fp, LOCK_EX))
367 {
368 fwrite($fp, $expire.'TS--->'.$output);
369 flock($fp, LOCK_UN);
370 }
371 else
372 {
373 log_message('error', "Unable to secure a file lock for file at: ".$cache_path);
374 return;
375 }
376 fclose($fp);
Derek Jones172e1612009-10-13 14:32:48 +0000377 @chmod($cache_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000378
379 log_message('debug', "Cache file written: ".$cache_path);
380 }
381
382 // --------------------------------------------------------------------
383
384 /**
385 * Update/serve a cached file
386 *
387 * @access public
388 * @return void
389 */
390 function _display_cache(&$CFG, &$URI)
391 {
392 $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path');
Derek Jonesd99e6032010-03-19 19:57:33 -0500393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 // Build the file path. The file name is an MD5 hash of the full URI
395 $uri = $CFG->item('base_url').
396 $CFG->item('index_page').
397 $URI->uri_string;
398
399 $filepath = $cache_path.md5($uri);
400
401 if ( ! @file_exists($filepath))
402 {
403 return FALSE;
404 }
405
406 if ( ! $fp = @fopen($filepath, FOPEN_READ))
407 {
408 return FALSE;
409 }
410
411 flock($fp, LOCK_SH);
412
413 $cache = '';
414 if (filesize($filepath) > 0)
415 {
416 $cache = fread($fp, filesize($filepath));
417 }
418
419 flock($fp, LOCK_UN);
420 fclose($fp);
Derek Jonesd99e6032010-03-19 19:57:33 -0500421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 // Strip out the embedded timestamp
423 if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
424 {
425 return FALSE;
426 }
427
428 // Has the file expired? If so we'll delete it.
429 if (time() >= trim(str_replace('TS--->', '', $match['1'])))
Derek Jonesd99e6032010-03-19 19:57:33 -0500430 {
431 if (is_really_writable($cache_path))
432 {
433 @unlink($filepath);
434 log_message('debug', "Cache file has expired. File deleted");
435 return FALSE;
436 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
438
439 // Display the cache
440 $this->_display(str_replace($match['0'], '', $cache));
441 log_message('debug', "Cache file is current. Sending it to browser.");
442 return TRUE;
443 }
444
445
446}
447// END Output Class
448
449/* End of file Output.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600450/* Location: ./system/core/Output.php */