| 1 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 |
/** |
| 3 |
* CodeIgniter |
| 4 |
* |
| 5 |
* An open source application development framework for PHP 5.1.6 or newer |
| 6 |
* |
| 7 |
* @package CodeIgniter |
| 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 |
| 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 |
/** |
| 32 |
* Current output string |
| 33 |
* |
| 34 |
* @var string |
| 35 |
* @access protected |
| 36 |
*/ |
| 37 |
protected $final_output; |
| 38 |
/** |
| 39 |
* Cache expiration time |
| 40 |
* |
| 41 |
* @var int |
| 42 |
* @access protected |
| 43 |
*/ |
| 44 |
protected $cache_expiration = 0; |
| 45 |
/** |
| 46 |
* List of server headers |
| 47 |
* |
| 48 |
* @var array |
| 49 |
* @access protected |
| 50 |
*/ |
| 51 |
protected $headers = array(); |
| 52 |
/** |
| 53 |
* List of mime types |
| 54 |
* |
| 55 |
* @var array |
| 56 |
* @access protected |
| 57 |
*/ |
| 58 |
protected $mime_types = array(); |
| 59 |
/** |
| 60 |
* Determines wether profiler is enabled |
| 61 |
* |
| 62 |
* @var book |
| 63 |
* @access protected |
| 64 |
*/ |
| 65 |
protected $enable_profiler = FALSE; |
| 66 |
/** |
| 67 |
* Determines if output compression is enabled |
| 68 |
* |
| 69 |
* @var bool |
| 70 |
* @access protected |
| 71 |
*/ |
| 72 |
protected $_zlib_oc = FALSE; |
| 73 |
/** |
| 74 |
* List of profiler sections |
| 75 |
* |
| 76 |
* @var array |
| 77 |
* @access protected |
| 78 |
*/ |
| 79 |
protected $_profiler_sections = array(); |
| 80 |
/** |
| 81 |
* Whether or not to parse variables like {elapsed_time} and {memory_usage} |
| 82 |
* |
| 83 |
* @var bool |
| 84 |
* @access protected |
| 85 |
*/ |
| 86 |
protected $parse_exec_vars = TRUE; |
| 87 |
|
| 88 |
/** |
| 89 |
* Constructor |
| 90 |
* |
| 91 |
*/ |
| 92 |
function __construct() |
| 93 |
{ |
| 94 |
$this->_zlib_oc = @ini_get('zlib.output_compression'); |
| 95 |
|
| 96 |
// Get mime types for later |
| 97 |
if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')) |
| 98 |
{ |
| 99 |
include APPPATH.'config/'.ENVIRONMENT.'/mimes.php'; |
| 100 |
} |
| 101 |
else |
| 102 |
{ |
| 103 |
if( file_exists(APPPATH.'config/mimes.php') ) |
| 104 |
include APPPATH.'config/mimes.php'; |
| 105 |
else |
| 106 |
include NTS_SYSTEM_APPPATH.'config/mimes.php'; |
| 107 |
} |
| 108 |
|
| 109 |
$this->mime_types = $mimes; |
| 110 |
|
| 111 |
log_message('debug', "Output Class Initialized"); |
| 112 |
} |
| 113 |
|
| 114 |
// -------------------------------------------------------------------- |
| 115 |
|
| 116 |
/** |
| 117 |
* Get Output |
| 118 |
* |
| 119 |
* Returns the current output string |
| 120 |
* |
| 121 |
* @access public |
| 122 |
* @return string |
| 123 |
*/ |
| 124 |
function get_output() |
| 125 |
{ |
| 126 |
return $this->final_output; |
| 127 |
} |
| 128 |
|
| 129 |
// -------------------------------------------------------------------- |
| 130 |
|
| 131 |
/** |
| 132 |
* Set Output |
| 133 |
* |
| 134 |
* Sets the output string |
| 135 |
* |
| 136 |
* @access public |
| 137 |
* @param string |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
function set_output($output) |
| 141 |
{ |
| 142 |
$this->final_output = $output; |
| 143 |
|
| 144 |
return $this; |
| 145 |
} |
| 146 |
|
| 147 |
// -------------------------------------------------------------------- |
| 148 |
|
| 149 |
/** |
| 150 |
* Append Output |
| 151 |
* |
| 152 |
* Appends data onto the output string |
| 153 |
* |
| 154 |
* @access public |
| 155 |
* @param string |
| 156 |
* @return void |
| 157 |
*/ |
| 158 |
function append_output($output) |
| 159 |
{ |
| 160 |
if ($this->final_output == '') |
| 161 |
{ |
| 162 |
$this->final_output = $output; |
| 163 |
} |
| 164 |
else |
| 165 |
{ |
| 166 |
$this->final_output .= $output; |
| 167 |
} |
| 168 |
|
| 169 |
return $this; |
| 170 |
} |
| 171 |
|
| 172 |
// -------------------------------------------------------------------- |
| 173 |
|
| 174 |
/** |
| 175 |
* Set Header |
| 176 |
* |
| 177 |
* Lets you set a server header which will be outputted with the final display. |
| 178 |
* |
| 179 |
* Note: If a file is cached, headers will not be sent. We need to figure out |
| 180 |
* how to permit header data to be saved with the cache data... |
| 181 |
* |
| 182 |
* @access public |
| 183 |
* @param string |
| 184 |
* @param bool |
| 185 |
* @return void |
| 186 |
*/ |
| 187 |
function set_header($header, $replace = TRUE) |
| 188 |
{ |
| 189 |
// If zlib.output_compression is enabled it will compress the output, |
| 190 |
// but it will not modify the content-length header to compensate for |
| 191 |
// the reduction, causing the browser to hang waiting for more data. |
| 192 |
// We'll just skip content-length in those cases. |
| 193 |
|
| 194 |
if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) == 0) |
| 195 |
{ |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
$this->headers[] = array($header, $replace); |
| 200 |
|
| 201 |
return $this; |
| 202 |
} |
| 203 |
|
| 204 |
// -------------------------------------------------------------------- |
| 205 |
|
| 206 |
/** |
| 207 |
* Set Content Type Header |
| 208 |
* |
| 209 |
* @access public |
| 210 |
* @param string extension of the file we're outputting |
| 211 |
* @return void |
| 212 |
*/ |
| 213 |
function set_content_type($mime_type) |
| 214 |
{ |
| 215 |
if (strpos($mime_type, '/') === FALSE) |
| 216 |
{ |
| 217 |
$extension = ltrim($mime_type, '.'); |
| 218 |
|
| 219 |
// Is this extension supported? |
| 220 |
if (isset($this->mime_types[$extension])) |
| 221 |
{ |
| 222 |
$mime_type =& $this->mime_types[$extension]; |
| 223 |
|
| 224 |
if (is_array($mime_type)) |
| 225 |
{ |
| 226 |
$mime_type = current($mime_type); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
$header = 'Content-Type: '.$mime_type; |
| 232 |
|
| 233 |
$this->headers[] = array($header, TRUE); |
| 234 |
|
| 235 |
return $this; |
| 236 |
} |
| 237 |
|
| 238 |
// -------------------------------------------------------------------- |
| 239 |
|
| 240 |
/** |
| 241 |
* Set HTTP Status Header |
| 242 |
* moved to Common procedural functions in 1.7.2 |
| 243 |
* |
| 244 |
* @access public |
| 245 |
* @param int the status code |
| 246 |
* @param string |
| 247 |
* @return void |
| 248 |
*/ |
| 249 |
function set_status_header($code = 200, $text = '') |
| 250 |
{ |
| 251 |
set_status_header($code, $text); |
| 252 |
|
| 253 |
return $this; |
| 254 |
} |
| 255 |
|
| 256 |
// -------------------------------------------------------------------- |
| 257 |
|
| 258 |
/** |
| 259 |
* Enable/disable Profiler |
| 260 |
* |
| 261 |
* @access public |
| 262 |
* @param bool |
| 263 |
* @return void |
| 264 |
*/ |
| 265 |
function enable_profiler($val = TRUE) |
| 266 |
{ |
| 267 |
$this->enable_profiler = (is_bool($val)) ? $val : TRUE; |
| 268 |
|
| 269 |
return $this; |
| 270 |
} |
| 271 |
|
| 272 |
// -------------------------------------------------------------------- |
| 273 |
|
| 274 |
/** |
| 275 |
* Set Profiler Sections |
| 276 |
* |
| 277 |
* Allows override of default / config settings for Profiler section display |
| 278 |
* |
| 279 |
* @access public |
| 280 |
* @param array |
| 281 |
* @return void |
| 282 |
*/ |
| 283 |
function set_profiler_sections($sections) |
| 284 |
{ |
| 285 |
foreach ($sections as $section => $enable) |
| 286 |
{ |
| 287 |
$this->_profiler_sections[$section] = ($enable !== FALSE) ? TRUE : FALSE; |
| 288 |
} |
| 289 |
|
| 290 |
return $this; |
| 291 |
} |
| 292 |
|
| 293 |
// -------------------------------------------------------------------- |
| 294 |
|
| 295 |
/** |
| 296 |
* Set Cache |
| 297 |
* |
| 298 |
* @access public |
| 299 |
* @param integer |
| 300 |
* @return void |
| 301 |
*/ |
| 302 |
function cache($time) |
| 303 |
{ |
| 304 |
$this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time; |
| 305 |
|
| 306 |
return $this; |
| 307 |
} |
| 308 |
|
| 309 |
// -------------------------------------------------------------------- |
| 310 |
|
| 311 |
/** |
| 312 |
* Display Output |
| 313 |
* |
| 314 |
* All "view" data is automatically put into this variable by the controller class: |
| 315 |
* |
| 316 |
* $this->final_output |
| 317 |
* |
| 318 |
* This function sends the finalized output data to the browser along |
| 319 |
* with any server headers and profile data. It also stops the |
| 320 |
* benchmark timer so the page rendering speed and memory usage can be shown. |
| 321 |
* |
| 322 |
* @access public |
| 323 |
* @param string |
| 324 |
* @return mixed |
| 325 |
*/ |
| 326 |
function _display($output = '') |
| 327 |
{ |
| 328 |
// Note: We use globals because we can't use $CI =& ci_get_instance() |
| 329 |
// since this function is sometimes called by the caching mechanism, |
| 330 |
// which happens before the CI super object is available. |
| 331 |
global $BM, $CFG; |
| 332 |
|
| 333 |
// Grab the super object if we can. |
| 334 |
if (class_exists('CI_Controller')) |
| 335 |
{ |
| 336 |
$CI =& ci_get_instance(); |
| 337 |
} |
| 338 |
|
| 339 |
// -------------------------------------------------------------------- |
| 340 |
|
| 341 |
// Set the output data |
| 342 |
if ($output == '') |
| 343 |
{ |
| 344 |
$output =& $this->final_output; |
| 345 |
} |
| 346 |
|
| 347 |
// -------------------------------------------------------------------- |
| 348 |
|
| 349 |
// Do we need to write a cache file? Only if the controller does not have its |
| 350 |
// own _output() method and we are not dealing with a cache file, which we |
| 351 |
// can determine by the existence of the $CI object above |
| 352 |
if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output')) |
| 353 |
{ |
| 354 |
$this->_write_cache($output); |
| 355 |
} |
| 356 |
|
| 357 |
// -------------------------------------------------------------------- |
| 358 |
|
| 359 |
// Parse out the elapsed time and memory usage, |
| 360 |
// then swap the pseudo-variables with the data |
| 361 |
|
| 362 |
$elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end'); |
| 363 |
|
| 364 |
if ($this->parse_exec_vars === TRUE) |
| 365 |
{ |
| 366 |
$memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB'; |
| 367 |
|
| 368 |
$output = str_replace('{elapsed_time}', $elapsed, $output); |
| 369 |
$output = str_replace('{memory_usage}', $memory, $output); |
| 370 |
} |
| 371 |
|
| 372 |
// -------------------------------------------------------------------- |
| 373 |
|
| 374 |
// Is compression requested? |
| 375 |
if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE) |
| 376 |
{ |
| 377 |
if (extension_loaded('zlib')) |
| 378 |
{ |
| 379 |
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) |
| 380 |
{ |
| 381 |
ob_start('ob_gzhandler'); |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
// -------------------------------------------------------------------- |
| 387 |
|
| 388 |
// Are there any server headers to send? |
| 389 |
if (count($this->headers) > 0) |
| 390 |
{ |
| 391 |
foreach ($this->headers as $header) |
| 392 |
{ |
| 393 |
@header($header[0], $header[1]); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
// -------------------------------------------------------------------- |
| 398 |
|
| 399 |
// Does the $CI object exist? |
| 400 |
// If not we know we are dealing with a cache file so we'll |
| 401 |
// simply echo out the data and exit. |
| 402 |
if ( ! isset($CI)) |
| 403 |
{ |
| 404 |
echo $output; |
| 405 |
log_message('debug', "Final output sent to browser"); |
| 406 |
log_message('debug', "Total execution time: ".$elapsed); |
| 407 |
return TRUE; |
| 408 |
} |
| 409 |
|
| 410 |
// -------------------------------------------------------------------- |
| 411 |
|
| 412 |
// Do we need to generate profile data? |
| 413 |
// If so, load the Profile class and run it. |
| 414 |
if ($this->enable_profiler == TRUE) |
| 415 |
{ |
| 416 |
$CI->load->library('profiler'); |
| 417 |
|
| 418 |
if ( ! empty($this->_profiler_sections)) |
| 419 |
{ |
| 420 |
$CI->profiler->set_sections($this->_profiler_sections); |
| 421 |
} |
| 422 |
|
| 423 |
// If the output data contains closing </body> and </html> tags |
| 424 |
// we will remove them and add them back after we insert the profile data |
| 425 |
if (preg_match("|</body>.*?</html>|is", $output)) |
| 426 |
{ |
| 427 |
$output = preg_replace("|</body>.*?</html>|is", '', $output); |
| 428 |
$output .= $CI->profiler->run(); |
| 429 |
$output .= '</body></html>'; |
| 430 |
} |
| 431 |
else |
| 432 |
{ |
| 433 |
$output .= $CI->profiler->run(); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
// -------------------------------------------------------------------- |
| 438 |
|
| 439 |
// Does the controller contain a function named _output()? |
| 440 |
// If so send the output there. Otherwise, echo it. |
| 441 |
if (method_exists($CI, '_output')) |
| 442 |
{ |
| 443 |
$CI->_output($output); |
| 444 |
} |
| 445 |
else |
| 446 |
{ |
| 447 |
echo $output; // Send it to the browser! |
| 448 |
} |
| 449 |
|
| 450 |
log_message('debug', "Final output sent to browser"); |
| 451 |
log_message('debug', "Total execution time: ".$elapsed); |
| 452 |
} |
| 453 |
|
| 454 |
// -------------------------------------------------------------------- |
| 455 |
|
| 456 |
/** |
| 457 |
* Write a Cache File |
| 458 |
* |
| 459 |
* @access public |
| 460 |
* @param string |
| 461 |
* @return void |
| 462 |
*/ |
| 463 |
function _write_cache($output) |
| 464 |
{ |
| 465 |
$CI =& ci_get_instance(); |
| 466 |
$path = $CI->config->item('cache_path'); |
| 467 |
|
| 468 |
$cache_path = ($path == '') ? APPPATH.'cache/' : $path; |
| 469 |
|
| 470 |
if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path)) |
| 471 |
{ |
| 472 |
log_message('error', "Unable to write cache file: ".$cache_path); |
| 473 |
return; |
| 474 |
} |
| 475 |
|
| 476 |
$uri = $CI->config->item('base_url'). |
| 477 |
$CI->config->item('index_page'). |
| 478 |
$CI->uri->uri_string(); |
| 479 |
|
| 480 |
$cache_path .= md5($uri); |
| 481 |
|
| 482 |
if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE)) |
| 483 |
{ |
| 484 |
log_message('error', "Unable to write cache file: ".$cache_path); |
| 485 |
return; |
| 486 |
} |
| 487 |
|
| 488 |
$expire = time() + ($this->cache_expiration * 60); |
| 489 |
|
| 490 |
if (flock($fp, LOCK_EX)) |
| 491 |
{ |
| 492 |
fwrite($fp, $expire.'TS--->'.$output); |
| 493 |
flock($fp, LOCK_UN); |
| 494 |
} |
| 495 |
else |
| 496 |
{ |
| 497 |
log_message('error', "Unable to secure a file lock for file at: ".$cache_path); |
| 498 |
return; |
| 499 |
} |
| 500 |
fclose($fp); |
| 501 |
@chmod($cache_path, FILE_WRITE_MODE); |
| 502 |
|
| 503 |
log_message('debug', "Cache file written: ".$cache_path); |
| 504 |
} |
| 505 |
|
| 506 |
// -------------------------------------------------------------------- |
| 507 |
|
| 508 |
/** |
| 509 |
* Update/serve a cached file |
| 510 |
* |
| 511 |
* @access public |
| 512 |
* @param object config class |
| 513 |
* @param object uri class |
| 514 |
* @return void |
| 515 |
*/ |
| 516 |
function _display_cache(&$CFG, &$URI) |
| 517 |
{ |
| 518 |
$cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path'); |
| 519 |
|
| 520 |
// Build the file path. The file name is an MD5 hash of the full URI |
| 521 |
$uri = $CFG->item('base_url'). |
| 522 |
$CFG->item('index_page'). |
| 523 |
$URI->uri_string; |
| 524 |
|
| 525 |
$filepath = $cache_path.md5($uri); |
| 526 |
|
| 527 |
if ( ! @file_exists($filepath)) |
| 528 |
{ |
| 529 |
return FALSE; |
| 530 |
} |
| 531 |
|
| 532 |
if ( ! $fp = @fopen($filepath, FOPEN_READ)) |
| 533 |
{ |
| 534 |
return FALSE; |
| 535 |
} |
| 536 |
|
| 537 |
flock($fp, LOCK_SH); |
| 538 |
|
| 539 |
$cache = ''; |
| 540 |
if (filesize($filepath) > 0) |
| 541 |
{ |
| 542 |
$cache = fread($fp, filesize($filepath)); |
| 543 |
} |
| 544 |
|
| 545 |
flock($fp, LOCK_UN); |
| 546 |
fclose($fp); |
| 547 |
|
| 548 |
// Strip out the embedded timestamp |
| 549 |
if ( ! preg_match("/(\d+TS--->)/", $cache, $match)) |
| 550 |
{ |
| 551 |
return FALSE; |
| 552 |
} |
| 553 |
|
| 554 |
// Has the file expired? If so we'll delete it. |
| 555 |
if (time() >= trim(str_replace('TS--->', '', $match['1']))) |
| 556 |
{ |
| 557 |
if (is_really_writable($cache_path)) |
| 558 |
{ |
| 559 |
@unlink($filepath); |
| 560 |
log_message('debug', "Cache file has expired. File deleted"); |
| 561 |
return FALSE; |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
// Display the cache |
| 566 |
$this->_display(str_replace($match['0'], '', $cache)); |
| 567 |
log_message('debug', "Cache file is current. Sending it to browser."); |
| 568 |
return TRUE; |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
} |
| 573 |
// END Output Class |
| 574 |
|
| 575 |
/* End of file Output.php */ |
| 576 |
/* Location: ./system/core/Output.php */ |