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
32 * Current output string
37 protected $final_output;
39 * Cache expiration time
44 protected $cache_expiration = 0;
46 * List of server headers
51 protected $headers = array();
58 protected $mime_types = array();
60 * Determines wether profiler is enabled
65 protected $enable_profiler = FALSE;
67 * Determines if output compression is enabled
72 protected $_zlib_oc = FALSE;
74 * List of profiler sections
79 protected $_profiler_sections = array();
81 * Whether or not to parse variables like {elapsed_time} and {memory_usage}
86 protected $parse_exec_vars = TRUE;
92 function __construct()
94 $this->_zlib_oc
= @ini_get('zlib.output_compression');
96 // Get mime types for later
97 if (defined('ENVIRONMENT') AND file_exists(APPPATH
.'config/'.ENVIRONMENT
.'/mimes.php'))
99 include APPPATH
.'config/'.ENVIRONMENT
.'/mimes.php';
103 include APPPATH
.'config/mimes.php';
107 $this->mime_types
= $mimes;
109 log_message('debug', "Output Class Initialized");
112 // --------------------------------------------------------------------
117 * Returns the current output string
122 function get_output()
124 return $this->final_output
;
127 // --------------------------------------------------------------------
132 * Sets the output string
138 function set_output($output)
140 $this->final_output
= $output;
145 // --------------------------------------------------------------------
150 * Appends data onto the output string
156 function append_output($output)
158 if ($this->final_output
== '')
160 $this->final_output
= $output;
164 $this->final_output
.= $output;
170 // --------------------------------------------------------------------
175 * Lets you set a server header which will be outputted with the final display.
177 * Note: If a file is cached, headers will not be sent. We need to figure out
178 * how to permit header data to be saved with the cache data...
185 function set_header($header, $replace = TRUE)
187 // If zlib.output_compression is enabled it will compress the output,
188 // but it will not modify the content-length header to compensate for
189 // the reduction, causing the browser to hang waiting for more data.
190 // We'll just skip content-length in those cases.
192 if ($this->_zlib_oc
&& strncasecmp($header, 'content-length', 14) == 0)
197 $this->headers
[] = array($header, $replace);
202 // --------------------------------------------------------------------
205 * Set Content Type Header
208 * @param string extension of the file we're outputting
211 function set_content_type($mime_type)
213 if (strpos($mime_type, '/') === FALSE)
215 $extension = ltrim($mime_type, '.');
217 // Is this extension supported?
218 if (isset($this->mime_types
[$extension]))
220 $mime_type =& $this->mime_types
[$extension];
222 if (is_array($mime_type))
224 $mime_type = current($mime_type);
229 $header = 'Content-Type: '.$mime_type;
231 $this->headers
[] = array($header, TRUE);
236 // --------------------------------------------------------------------
239 * Set HTTP Status Header
240 * moved to Common procedural functions in 1.7.2
243 * @param int the status code
247 function set_status_header($code = 200, $text = '')
249 set_status_header($code, $text);
254 // --------------------------------------------------------------------
257 * Enable/disable Profiler
263 function enable_profiler($val = TRUE)
265 $this->enable_profiler
= (is_bool($val)) ? $val : TRUE;
270 // --------------------------------------------------------------------
273 * Set Profiler Sections
275 * Allows override of default / config settings for Profiler section display
281 function set_profiler_sections($sections)
283 foreach ($sections as $section => $enable)
285 $this->_profiler_sections
[$section] = ($enable !== FALSE) ? TRUE : FALSE;
291 // --------------------------------------------------------------------
300 function cache($time)
302 $this->cache_expiration
= ( ! is_numeric($time)) ? 0 : $time;
307 // --------------------------------------------------------------------
312 * All "view" data is automatically put into this variable by the controller class:
314 * $this->final_output
316 * This function sends the finalized output data to the browser along
317 * with any server headers and profile data. It also stops the
318 * benchmark timer so the page rendering speed and memory usage can be shown.
324 function _display($output = '')
326 // Note: We use globals because we can't use $CI =& get_instance()
327 // since this function is sometimes called by the caching mechanism,
328 // which happens before the CI super object is available.
331 // Grab the super object if we can.
332 if (class_exists('CI_Controller'))
334 $CI =& get_instance();
337 // --------------------------------------------------------------------
339 // Set the output data
342 $output =& $this->final_output
;
345 // --------------------------------------------------------------------
347 // Do we need to write a cache file? Only if the controller does not have its
348 // own _output() method and we are not dealing with a cache file, which we
349 // can determine by the existence of the $CI object above
350 if ($this->cache_expiration
> 0 && isset($CI) && ! method_exists($CI, '_output'))
352 $this->_write_cache($output);
355 // --------------------------------------------------------------------
357 // Parse out the elapsed time and memory usage,
358 // then swap the pseudo-variables with the data
360 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
362 if ($this->parse_exec_vars
=== TRUE)
364 $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
366 $output = str_replace('{elapsed_time}', $elapsed, $output);
367 $output = str_replace('{memory_usage}', $memory, $output);
370 // --------------------------------------------------------------------
372 // Is compression requested?
373 if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc
== FALSE)
375 if (extension_loaded('zlib'))
377 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
379 ob_start('ob_gzhandler');
384 // --------------------------------------------------------------------
386 // Are there any server headers to send?
387 if (count($this->headers
) > 0)
389 foreach ($this->headers
as $header)
391 @header($header[0], $header[1]);
395 // --------------------------------------------------------------------
397 // Does the $CI object exist?
398 // If not we know we are dealing with a cache file so we'll
399 // simply echo out the data and exit.
403 log_message('debug', "Final output sent to browser");
404 log_message('debug', "Total execution time: ".$elapsed);
408 // --------------------------------------------------------------------
410 // Do we need to generate profile data?
411 // If so, load the Profile class and run it.
412 if ($this->enable_profiler
== TRUE)
414 $CI->load
->library('profiler');
416 if ( ! empty($this->_profiler_sections
))
418 $CI->profiler
->set_sections($this->_profiler_sections
);
421 // If the output data contains closing </body> and </html> tags
422 // we will remove them and add them back after we insert the profile data
423 if (preg_match("|</body>.*?</html>|is", $output))
425 $output = preg_replace("|</body>.*?</html>|is", '', $output);
426 $output .= $CI->profiler
->run();
427 $output .= '</body></html>';
431 $output .= $CI->profiler
->run();
435 // --------------------------------------------------------------------
437 // Does the controller contain a function named _output()?
438 // If so send the output there. Otherwise, echo it.
439 if (method_exists($CI, '_output'))
441 $CI->_output($output);
445 echo $output; // Send it to the browser!
448 log_message('debug', "Final output sent to browser");
449 log_message('debug', "Total execution time: ".$elapsed);
452 // --------------------------------------------------------------------
461 function _write_cache($output)
463 $CI =& get_instance();
464 $path = $CI->config
->item('cache_path');
466 $cache_path = ($path == '') ? APPPATH
.'cache/' : $path;
468 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
470 log_message('error', "Unable to write cache file: ".$cache_path);
474 $uri = $CI->config
->item('base_url').
475 $CI->config
->item('index_page').
476 $CI->uri
->uri_string();
478 $cache_path .= md5($uri);
480 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE
))
482 log_message('error', "Unable to write cache file: ".$cache_path);
486 $expire = time() +
($this->cache_expiration
* 60);
488 if (flock($fp, LOCK_EX
))
490 fwrite($fp, $expire.'TS--->'.$output);
495 log_message('error', "Unable to secure a file lock for file at: ".$cache_path);
499 @chmod($cache_path, FILE_WRITE_MODE
);
501 log_message('debug', "Cache file written: ".$cache_path);
504 // --------------------------------------------------------------------
507 * Update/serve a cached file
510 * @param object config class
511 * @param object uri class
514 function _display_cache(&$CFG, &$URI)
516 $cache_path = ($CFG->item('cache_path') == '') ? APPPATH
.'cache/' : $CFG->item('cache_path');
518 // Build the file path. The file name is an MD5 hash of the full URI
519 $uri = $CFG->item('base_url').
520 $CFG->item('index_page').
523 $filepath = $cache_path.md5($uri);
525 if ( ! @file_exists($filepath))
530 if ( ! $fp = @fopen($filepath, FOPEN_READ
))
538 if (filesize($filepath) > 0)
540 $cache = fread($fp, filesize($filepath));
546 // Strip out the embedded timestamp
547 if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
552 // Has the file expired? If so we'll delete it.
553 if (time() >= trim(str_replace('TS--->', '', $match['1'])))
555 if (is_really_writable($cache_path))
558 log_message('debug', "Cache file has expired. File deleted");
564 $this->_display(str_replace($match['0'], '', $cache));
565 log_message('debug', "Cache file is current. Sending it to browser.");
573 /* End of file Output.php */
574 /* Location: ./system/core/Output.php */