Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?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 Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @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 | */ |
| 29 | class CI_Output { |
| 30 | |
| 31 | var $final_output; |
| 32 | var $cache_expiration = 0; |
| 33 | var $headers = array(); |
| 34 | var $enable_profiler = FALSE; |
Derek Jones | 4652049 | 2010-03-02 13:53:25 -0600 | [diff] [blame] | 35 | var $parse_exec_vars = TRUE; // whether or not to parse variables like {elapsed_time} and {memory_usage} |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | |
Pascal Kriete | 676e1cd | 2010-04-09 21:16:16 +0200 | [diff] [blame] | 37 | var $_zlib_oc = FALSE; |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 38 | var $_profiler_sections = array(); |
| 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | function CI_Output() |
| 41 | { |
Pascal Kriete | 676e1cd | 2010-04-09 21:16:16 +0200 | [diff] [blame] | 42 | $this->_zlib_oc = @ini_get('zlib.output_compression'); |
| 43 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | 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 Kriete | 676e1cd | 2010-04-09 21:16:16 +0200 | [diff] [blame] | 117 | // 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | $this->headers[] = array($header, $replace); |
| 128 | } |
| 129 | |
| 130 | // -------------------------------------------------------------------- |
| 131 | |
| 132 | /** |
| 133 | * Set HTTP Status Header |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 134 | * moved to Common procedural functions in 1.7.2 |
| 135 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | * @access public |
| 137 | * @param int the status code |
| 138 | * @param string |
| 139 | * @return void |
| 140 | */ |
Derek Jones | 4652049 | 2010-03-02 13:53:25 -0600 | [diff] [blame] | 141 | function set_status_header($code = 200, $text = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | { |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 143 | set_status_header($code, $text); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | } |
| 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 Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 161 | |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | |
| 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 Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame^] | 215 | |
| 216 | // Grab the super object if we can. |
| 217 | if (function_exists('get_instance')) |
| 218 | { |
| 219 | $CI =& get_instance(); |
| 220 | } |
| 221 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | // -------------------------------------------------------------------- |
| 223 | |
| 224 | // Set the output data |
| 225 | if ($output == '') |
| 226 | { |
| 227 | $output =& $this->final_output; |
| 228 | } |
| 229 | |
| 230 | // -------------------------------------------------------------------- |
| 231 | |
Derek Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame^] | 232 | // 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 244 | |
Derek Jones | 4652049 | 2010-03-02 13:53:25 -0600 | [diff] [blame] | 245 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | |
| 255 | // -------------------------------------------------------------------- |
| 256 | |
| 257 | // Is compression requested? |
Pascal Kriete | 676e1cd | 2010-04-09 21:16:16 +0200 | [diff] [blame] | 258 | if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | { |
| 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 Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame^] | 282 | // Does the $CI object exist? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | // If not we know we are dealing with a cache file so we'll |
| 284 | // simply echo out the data and exit. |
Derek Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame^] | 285 | if ( ! isset($CI)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | |
| 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 Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 300 | |
| 301 | if ( ! empty($this->_profiler_sections)) |
| 302 | { |
| 303 | $CI->profiler->set_sections($this->_profiler_sections); |
| 304 | } |
| 305 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 306 | // 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 Aker | a7f5a94 | 2010-09-14 17:20:53 -0500 | [diff] [blame] | 354 | log_message('error', "Unable to write cache file: ".$cache_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | 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 Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 383 | @chmod($cache_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | |
| 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 Jones | d99e603 | 2010-03-19 19:57:33 -0500 | [diff] [blame] | 399 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | // 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 Jones | d99e603 | 2010-03-19 19:57:33 -0500 | [diff] [blame] | 427 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | // 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 Jones | d99e603 | 2010-03-19 19:57:33 -0500 | [diff] [blame] | 436 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | } |
| 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 Jones | c68dfbf | 2010-03-02 12:59:23 -0600 | [diff] [blame] | 456 | /* Location: ./system/core/Output.php */ |