1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
21 * Responsible for sending final output to browser
23 * @package CodeIgniter
24 * @subpackage Libraries
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/output.html
31 protected $final_output;
32 protected $cache_expiration = 0;
33 protected $headers = array();
34 protected $mime_types = array();
35 protected $enable_profiler = FALSE;
36 protected $_zlib_oc = FALSE;
37 protected $_profiler_sections = array();
38 protected $parse_exec_vars = TRUE; // whether or not to parse variables like {elapsed_time} and {memory_usage}
40 function __construct()
42 $this->_zlib_oc = @ini_get('zlib.output_compression');
44 // Get mime types for later
45 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes'.EXT))
47 include APPPATH.'config/'.ENVIRONMENT.'/mimes'.EXT;
51 include APPPATH.'config/mimes'.EXT;
55 $this->mime_types = $mimes;
57 log_message('debug', "Output Class Initialized");
60 // --------------------------------------------------------------------
65 * Returns the current output string
72 return $this->final_output;
75 // --------------------------------------------------------------------
80 * Sets the output string
86 function set_output($output)
88 $this->final_output = $output;
93 // --------------------------------------------------------------------
98 * Appends data onto the output string
104 function append_output($output)
106 if ($this->final_output == '')
108 $this->final_output = $output;
112 $this->final_output .= $output;
118 // --------------------------------------------------------------------
123 * Lets you set a server header which will be outputted with the final display.
125 * Note: If a file is cached, headers will not be sent. We need to figure out
126 * how to permit header data to be saved with the cache data...
132 function set_header($header, $replace = TRUE)
134 // If zlib.output_compression is enabled it will compress the output,
135 // but it will not modify the content-length header to compensate for
136 // the reduction, causing the browser to hang waiting for more data.
137 // We'll just skip content-length in those cases.
139 if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) == 0)
144 $this->headers[] = array($header, $replace);
149 // --------------------------------------------------------------------
152 * Set Content Type Header
155 * @param string extension of the file we're outputting
158 function set_content_type($mime_type)
160 if (strpos($mime_type, '/') === FALSE)
162 $extension = ltrim($mime_type, '.');
164 // Is this extension supported?
165 if (isset($this->mime_types[$extension]))
167 $mime_type =& $this->mime_types[$extension];
169 if (is_array($mime_type))
171 $mime_type = current($mime_type);
176 $header = 'Content-Type: '.$mime_type;
178 $this->headers[] = array($header, TRUE);
183 // --------------------------------------------------------------------
186 * Set HTTP Status Header
187 * moved to Common procedural functions in 1.7.2
190 * @param int the status code
194 function set_status_header($code = 200, $text = '')
196 set_status_header($code, $text);
201 // --------------------------------------------------------------------
204 * Enable/disable Profiler
210 function enable_profiler($val = TRUE)
212 $this->enable_profiler = (is_bool($val)) ? $val : TRUE;
217 // --------------------------------------------------------------------
220 * Set Profiler Sections
222 * Allows override of default / config settings for Profiler section display
228 function set_profiler_sections($sections)
230 foreach ($sections as $section => $enable)
232 $this->_profiler_sections[$section] = ($enable !== FALSE) ? TRUE : FALSE;
238 // --------------------------------------------------------------------
247 function cache($time)
249 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
254 // --------------------------------------------------------------------
259 * All "view" data is automatically put into this variable by the controller class:
261 * $this->final_output
263 * This function sends the finalized output data to the browser along
264 * with any server headers and profile data. It also stops the
265 * benchmark timer so the page rendering speed and memory usage can be shown.
270 function _display($output = '')
272 // Note: We use globals because we can't use $CI =& get_instance()
273 // since this function is sometimes called by the caching mechanism,
274 // which happens before the CI super object is available.
277 // Grab the super object if we can.
278 if (class_exists('CI_Controller'))
280 $CI =& get_instance();
283 // --------------------------------------------------------------------
285 // Set the output data
288 $output =& $this->final_output;
291 // --------------------------------------------------------------------
293 // Do we need to write a cache file? Only if the controller does not have its
294 // own _output() method and we are not dealing with a cache file, which we
295 // can determine by the existence of the $CI object above
296 if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output'))
298 $this->_write_cache($output);
301 // --------------------------------------------------------------------
303 // Parse out the elapsed time and memory usage,
304 // then swap the pseudo-variables with the data
306 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
308 if ($this->parse_exec_vars === TRUE)
310 $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
312 $output = str_replace('{elapsed_time}', $elapsed, $output);
313 $output = str_replace('{memory_usage}', $memory, $output);
316 // --------------------------------------------------------------------
318 // Is compression requested?
319 if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE)
321 if (extension_loaded('zlib'))
323 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
325 ob_start('ob_gzhandler');
330 // --------------------------------------------------------------------
332 // Are there any server headers to send?
333 if (count($this->headers) > 0)
335 foreach ($this->headers as $header)
337 @header($header[0], $header[1]);
341 // --------------------------------------------------------------------
343 // Does the $CI object exist?
344 // If not we know we are dealing with a cache file so we'll
345 // simply echo out the data and exit.
349 log_message('debug', "Final output sent to browser");
350 log_message('debug', "Total execution time: ".$elapsed);
354 // --------------------------------------------------------------------
356 // Do we need to generate profile data?
357 // If so, load the Profile class and run it.
358 if ($this->enable_profiler == TRUE)
360 $CI->load->library('profiler');
362 if ( ! empty($this->_profiler_sections))
364 $CI->profiler->set_sections($this->_profiler_sections);
367 // If the output data contains closing </body> and </html> tags
368 // we will remove them and add them back after we insert the profile data
369 if (preg_match("|</body>.*?</html>|is", $output))
371 $output = preg_replace("|</body>.*?</html>|is", '', $output);
372 $output .= $CI->profiler->run();
373 $output .= '</body></html>';
377 $output .= $CI->profiler->run();
381 // --------------------------------------------------------------------
383 // Does the controller contain a function named _output()?
384 // If so send the output there. Otherwise, echo it.
385 if (method_exists($CI, '_output'))
387 $CI->_output($output);
391 echo $output; // Send it to the browser!
394 log_message('debug', "Final output sent to browser");
395 log_message('debug', "Total execution time: ".$elapsed);
398 // --------------------------------------------------------------------
406 function _write_cache($output)
408 $CI =& get_instance();
409 $path = $CI->config->item('cache_path');
411 $cache_path = ($path == '') ? APPPATH.'cache/' : $path;
413 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
415 log_message('error', "Unable to write cache file: ".$cache_path);
419 $uri = $CI->config->item('base_url').
420 $CI->config->item('index_page').
421 $CI->uri->uri_string();
423 $cache_path .= md5($uri);
425 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
427 log_message('error', "Unable to write cache file: ".$cache_path);
431 $expire = time() + ($this->cache_expiration * 60);
433 if (flock($fp, LOCK_EX))
435 fwrite($fp, $expire.'TS--->'.$output);
440 log_message('error', "Unable to secure a file lock for file at: ".$cache_path);
444 @chmod($cache_path, FILE_WRITE_MODE);
446 log_message('debug', "Cache file written: ".$cache_path);
449 // --------------------------------------------------------------------
452 * Update/serve a cached file
457 function _display_cache(&$CFG, &$URI)
459 $cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path');
461 // Build the file path. The file name is an MD5 hash of the full URI
462 $uri = $CFG->item('base_url').
463 $CFG->item('index_page').
466 $filepath = $cache_path.md5($uri);
468 if ( ! @file_exists($filepath))
473 if ( ! $fp = @fopen($filepath, FOPEN_READ))
481 if (filesize($filepath) > 0)
483 $cache = fread($fp, filesize($filepath));
489 // Strip out the embedded timestamp
490 if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
495 // Has the file expired? If so we'll delete it.
496 if (time() >= trim(str_replace('TS--->', '', $match['1'])))
498 if (is_really_writable($cache_path))
501 log_message('debug', "Cache file has expired. File deleted");
507 $this->_display(str_replace($match['0'], '', $cache));
508 log_message('debug', "Cache file is current. Sending it to browser.");
516 /* End of file Output.php */
517 /* Location: ./system/core/Output.php */