1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008, 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 var $cache_expiration = 0;
33 var $headers = array();
34 var $enable_profiler = FALSE;
39 log_message('debug', "Output Class Initialized");
42 // --------------------------------------------------------------------
47 * Returns the current output string
54 return $this->final_output;
57 // --------------------------------------------------------------------
62 * Sets the output string
68 function set_output($output)
70 $this->final_output = $output;
73 // --------------------------------------------------------------------
78 * Appends data onto the output string
84 function append_output($output)
86 if ($this->final_output == '')
88 $this->final_output = $output;
92 $this->final_output .= $output;
96 // --------------------------------------------------------------------
101 * Lets you set a server header which will be outputted with the final display.
103 * Note: If a file is cached, headers will not be sent. We need to figure out
104 * how to permit header data to be saved with the cache data...
110 function set_header($header, $replace = TRUE)
112 $this->headers[] = array($header, $replace);
115 // --------------------------------------------------------------------
118 * Set HTTP Status Header
121 * @param int the status code
125 function set_status_header($code = '200', $text = '')
131 '203' => 'Non-Authoritative Information',
132 '204' => 'No Content',
133 '205' => 'Reset Content',
134 '206' => 'Partial Content',
136 '300' => 'Multiple Choices',
137 '301' => 'Moved Permanently',
139 '304' => 'Not Modified',
140 '305' => 'Use Proxy',
141 '307' => 'Temporary Redirect',
143 '400' => 'Bad Request',
144 '401' => 'Unauthorized',
145 '403' => 'Forbidden',
146 '404' => 'Not Found',
147 '405' => 'Method Not Allowed',
148 '406' => 'Not Acceptable',
149 '407' => 'Proxy Authentication Required',
150 '408' => 'Request Timeout',
153 '411' => 'Length Required',
154 '412' => 'Precondition Failed',
155 '413' => 'Request Entity Too Large',
156 '414' => 'Request-URI Too Long',
157 '415' => 'Unsupported Media Type',
158 '416' => 'Requested Range Not Satisfiable',
159 '417' => 'Expectation Failed',
161 '500' => 'Internal Server Error',
162 '501' => 'Not Implemented',
163 '502' => 'Bad Gateway',
164 '503' => 'Service Unavailable',
165 '504' => 'Gateway Timeout',
166 '505' => 'HTTP Version Not Supported'
169 if ($code == '' OR ! is_numeric($code))
171 show_error('Status codes must be numeric');
174 if (isset($stati[$code]) AND $text == '')
176 $text = $stati[$code];
181 show_error('No status text available. Please check your status code number or supply your own message text.');
184 $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
186 if (substr(php_sapi_name(), 0, 3) == 'cgi')
188 header("Status: {$code} {$text}", TRUE);
190 elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
192 header($server_protocol." {$code} {$text}", TRUE, $code);
196 header("HTTP/1.1 {$code} {$text}", TRUE, $code);
200 // --------------------------------------------------------------------
203 * Enable/disable Profiler
209 function enable_profiler($val = TRUE)
211 $this->enable_profiler = (is_bool($val)) ? $val : TRUE;
214 // --------------------------------------------------------------------
223 function cache($time)
225 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
228 // --------------------------------------------------------------------
233 * All "view" data is automatically put into this variable by the controller class:
235 * $this->final_output
237 * This function sends the finalized output data to the browser along
238 * with any server headers and profile data. It also stops the
239 * benchmark timer so the page rendering speed and memory usage can be shown.
244 function _display($output = '')
246 // Note: We use globals because we can't use $CI =& get_instance()
247 // since this function is sometimes called by the caching mechanism,
248 // which happens before the CI super object is available.
251 // --------------------------------------------------------------------
253 // Set the output data
256 $output =& $this->final_output;
259 // --------------------------------------------------------------------
261 // Do we need to write a cache file?
262 if ($this->cache_expiration > 0)
264 $this->_write_cache($output);
267 // --------------------------------------------------------------------
269 // Parse out the elapsed time and memory usage,
270 // then swap the pseudo-variables with the data
272 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
273 $output = str_replace('{elapsed_time}', $elapsed, $output);
275 $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
276 $output = str_replace('{memory_usage}', $memory, $output);
278 // --------------------------------------------------------------------
280 // Is compression requested?
281 if ($CFG->item('compress_output') === TRUE)
283 if (extension_loaded('zlib'))
285 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
287 ob_start('ob_gzhandler');
292 // --------------------------------------------------------------------
294 // Are there any server headers to send?
295 if (count($this->headers) > 0)
297 foreach ($this->headers as $header)
299 @header($header[0], $header[1]);
303 // --------------------------------------------------------------------
305 // Does the get_instance() function exist?
306 // If not we know we are dealing with a cache file so we'll
307 // simply echo out the data and exit.
308 if ( ! function_exists('get_instance'))
311 log_message('debug', "Final output sent to browser");
312 log_message('debug', "Total execution time: ".$elapsed);
316 // --------------------------------------------------------------------
318 // Grab the super object. We'll need it in a moment...
319 $CI =& get_instance();
321 // Do we need to generate profile data?
322 // If so, load the Profile class and run it.
323 if ($this->enable_profiler == TRUE)
325 $CI->load->library('profiler');
327 // If the output data contains closing </body> and </html> tags
328 // we will remove them and add them back after we insert the profile data
329 if (preg_match("|</body>.*?</html>|is", $output))
331 $output = preg_replace("|</body>.*?</html>|is", '', $output);
332 $output .= $CI->profiler->run();
333 $output .= '</body></html>';
337 $output .= $CI->profiler->run();
341 // --------------------------------------------------------------------
343 // Does the controller contain a function named _output()?
344 // If so send the output there. Otherwise, echo it.
345 if (method_exists($CI, '_output'))
347 $CI->_output($output);
351 echo $output; // Send it to the browser!
354 log_message('debug', "Final output sent to browser");
355 log_message('debug', "Total execution time: ".$elapsed);
358 // --------------------------------------------------------------------
366 function _write_cache($output)
368 $CI =& get_instance();
369 $path = $CI->config->item('cache_path');
371 $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
373 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
378 $uri = $CI->config->item('base_url').
379 $CI->config->item('index_page').
380 $CI->uri->uri_string();
382 $cache_path .= md5($uri);
384 if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
386 log_message('error', "Unable to write cache file: ".$cache_path);
390 $expire = time() + ($this->cache_expiration * 60);
392 if (flock($fp, LOCK_EX))
394 fwrite($fp, $expire.'TS--->'.$output);
399 log_message('error', "Unable to secure a file lock for file at: ".$cache_path);
403 @chmod($cache_path, DIR_WRITE_MODE);
405 log_message('debug', "Cache file written: ".$cache_path);
408 // --------------------------------------------------------------------
411 * Update/serve a cached file
416 function _display_cache(&$CFG, &$URI)
418 $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path');
420 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
425 // Build the file path. The file name is an MD5 hash of the full URI
426 $uri = $CFG->item('base_url').
427 $CFG->item('index_page').
430 $filepath = $cache_path.md5($uri);
432 if ( ! @file_exists($filepath))
437 if ( ! $fp = @fopen($filepath, FOPEN_READ))
445 if (filesize($filepath) > 0)
447 $cache = fread($fp, filesize($filepath));
453 // Strip out the embedded timestamp
454 if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
459 // Has the file expired? If so we'll delete it.
460 if (time() >= trim(str_replace('TS--->', '', $match['1'])))
463 log_message('debug', "Cache file has expired. File deleted");
468 $this->_display(str_replace($match['0'], '', $cache));
469 log_message('debug', "Cache file is current. Sending it to browser.");
477 /* End of file Output.php */
478 /* Location: ./system/libraries/Output.php */