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 | |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 37 | var $_profiler_sections = array(); |
| 38 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | function CI_Output() |
| 40 | { |
| 41 | log_message('debug', "Output Class Initialized"); |
| 42 | } |
| 43 | |
| 44 | // -------------------------------------------------------------------- |
| 45 | |
| 46 | /** |
| 47 | * Get Output |
| 48 | * |
| 49 | * Returns the current output string |
| 50 | * |
| 51 | * @access public |
| 52 | * @return string |
| 53 | */ |
| 54 | function get_output() |
| 55 | { |
| 56 | return $this->final_output; |
| 57 | } |
| 58 | |
| 59 | // -------------------------------------------------------------------- |
| 60 | |
| 61 | /** |
| 62 | * Set Output |
| 63 | * |
| 64 | * Sets the output string |
| 65 | * |
| 66 | * @access public |
| 67 | * @param string |
| 68 | * @return void |
| 69 | */ |
| 70 | function set_output($output) |
| 71 | { |
| 72 | $this->final_output = $output; |
| 73 | } |
| 74 | |
| 75 | // -------------------------------------------------------------------- |
| 76 | |
| 77 | /** |
| 78 | * Append Output |
| 79 | * |
| 80 | * Appends data onto the output string |
| 81 | * |
| 82 | * @access public |
| 83 | * @param string |
| 84 | * @return void |
| 85 | */ |
| 86 | function append_output($output) |
| 87 | { |
| 88 | if ($this->final_output == '') |
| 89 | { |
| 90 | $this->final_output = $output; |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | $this->final_output .= $output; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // -------------------------------------------------------------------- |
| 99 | |
| 100 | /** |
| 101 | * Set Header |
| 102 | * |
| 103 | * Lets you set a server header which will be outputted with the final display. |
| 104 | * |
| 105 | * Note: If a file is cached, headers will not be sent. We need to figure out |
| 106 | * how to permit header data to be saved with the cache data... |
| 107 | * |
| 108 | * @access public |
| 109 | * @param string |
| 110 | * @return void |
| 111 | */ |
| 112 | function set_header($header, $replace = TRUE) |
| 113 | { |
| 114 | $this->headers[] = array($header, $replace); |
| 115 | } |
| 116 | |
| 117 | // -------------------------------------------------------------------- |
| 118 | |
| 119 | /** |
| 120 | * Set HTTP Status Header |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 121 | * moved to Common procedural functions in 1.7.2 |
| 122 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | * @access public |
| 124 | * @param int the status code |
| 125 | * @param string |
| 126 | * @return void |
| 127 | */ |
Derek Jones | 4652049 | 2010-03-02 13:53:25 -0600 | [diff] [blame] | 128 | function set_status_header($code = 200, $text = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | { |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 130 | set_status_header($code, $text); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // -------------------------------------------------------------------- |
| 134 | |
| 135 | /** |
| 136 | * Enable/disable Profiler |
| 137 | * |
| 138 | * @access public |
| 139 | * @param bool |
| 140 | * @return void |
| 141 | */ |
| 142 | function enable_profiler($val = TRUE) |
| 143 | { |
| 144 | $this->enable_profiler = (is_bool($val)) ? $val : TRUE; |
| 145 | } |
| 146 | |
| 147 | // -------------------------------------------------------------------- |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Set Profiler Sections |
| 151 | * |
| 152 | * Allows override of default / config settings for Profiler section display |
| 153 | * |
| 154 | * @access public |
| 155 | * @param array |
| 156 | * @return void |
| 157 | */ |
| 158 | function set_profiler_sections($sections) |
| 159 | { |
| 160 | foreach ($sections as $section => $enable) |
| 161 | { |
| 162 | $this->_profiler_sections[$section] = ($enable !== FALSE) ? TRUE : FALSE; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | |
| 168 | /** |
| 169 | * Set Cache |
| 170 | * |
| 171 | * @access public |
| 172 | * @param integer |
| 173 | * @return void |
| 174 | */ |
| 175 | function cache($time) |
| 176 | { |
| 177 | $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time; |
| 178 | } |
| 179 | |
| 180 | // -------------------------------------------------------------------- |
| 181 | |
| 182 | /** |
| 183 | * Display Output |
| 184 | * |
| 185 | * All "view" data is automatically put into this variable by the controller class: |
| 186 | * |
| 187 | * $this->final_output |
| 188 | * |
| 189 | * This function sends the finalized output data to the browser along |
| 190 | * with any server headers and profile data. It also stops the |
| 191 | * benchmark timer so the page rendering speed and memory usage can be shown. |
| 192 | * |
| 193 | * @access public |
| 194 | * @return mixed |
| 195 | */ |
| 196 | function _display($output = '') |
| 197 | { |
| 198 | // Note: We use globals because we can't use $CI =& get_instance() |
| 199 | // since this function is sometimes called by the caching mechanism, |
| 200 | // which happens before the CI super object is available. |
| 201 | global $BM, $CFG; |
| 202 | |
| 203 | // -------------------------------------------------------------------- |
| 204 | |
| 205 | // Set the output data |
| 206 | if ($output == '') |
| 207 | { |
| 208 | $output =& $this->final_output; |
| 209 | } |
| 210 | |
| 211 | // -------------------------------------------------------------------- |
| 212 | |
| 213 | // Do we need to write a cache file? |
| 214 | if ($this->cache_expiration > 0) |
| 215 | { |
| 216 | $this->_write_cache($output); |
| 217 | } |
| 218 | |
| 219 | // -------------------------------------------------------------------- |
| 220 | |
| 221 | // Parse out the elapsed time and memory usage, |
| 222 | // then swap the pseudo-variables with the data |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | |
Derek Jones | 4652049 | 2010-03-02 13:53:25 -0600 | [diff] [blame] | 224 | $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end'); |
| 225 | |
| 226 | if ($this->parse_exec_vars === TRUE) |
| 227 | { |
| 228 | $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB'; |
| 229 | |
| 230 | $output = str_replace('{elapsed_time}', $elapsed, $output); |
| 231 | $output = str_replace('{memory_usage}', $memory, $output); |
| 232 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | |
| 234 | // -------------------------------------------------------------------- |
| 235 | |
| 236 | // Is compression requested? |
| 237 | if ($CFG->item('compress_output') === TRUE) |
| 238 | { |
| 239 | if (extension_loaded('zlib')) |
| 240 | { |
| 241 | if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) |
| 242 | { |
| 243 | ob_start('ob_gzhandler'); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // -------------------------------------------------------------------- |
| 249 | |
| 250 | // Are there any server headers to send? |
| 251 | if (count($this->headers) > 0) |
| 252 | { |
| 253 | foreach ($this->headers as $header) |
| 254 | { |
| 255 | @header($header[0], $header[1]); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // -------------------------------------------------------------------- |
| 260 | |
| 261 | // Does the get_instance() function exist? |
| 262 | // If not we know we are dealing with a cache file so we'll |
| 263 | // simply echo out the data and exit. |
| 264 | if ( ! function_exists('get_instance')) |
| 265 | { |
| 266 | echo $output; |
| 267 | log_message('debug', "Final output sent to browser"); |
| 268 | log_message('debug', "Total execution time: ".$elapsed); |
| 269 | return TRUE; |
| 270 | } |
| 271 | |
| 272 | // -------------------------------------------------------------------- |
| 273 | |
| 274 | // Grab the super object. We'll need it in a moment... |
| 275 | $CI =& get_instance(); |
| 276 | |
| 277 | // Do we need to generate profile data? |
| 278 | // If so, load the Profile class and run it. |
| 279 | if ($this->enable_profiler == TRUE) |
| 280 | { |
| 281 | $CI->load->library('profiler'); |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 282 | |
| 283 | if ( ! empty($this->_profiler_sections)) |
| 284 | { |
| 285 | $CI->profiler->set_sections($this->_profiler_sections); |
| 286 | } |
| 287 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | // If the output data contains closing </body> and </html> tags |
| 289 | // we will remove them and add them back after we insert the profile data |
| 290 | if (preg_match("|</body>.*?</html>|is", $output)) |
| 291 | { |
| 292 | $output = preg_replace("|</body>.*?</html>|is", '', $output); |
| 293 | $output .= $CI->profiler->run(); |
| 294 | $output .= '</body></html>'; |
| 295 | } |
| 296 | else |
| 297 | { |
| 298 | $output .= $CI->profiler->run(); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | // -------------------------------------------------------------------- |
| 303 | |
| 304 | // Does the controller contain a function named _output()? |
| 305 | // If so send the output there. Otherwise, echo it. |
| 306 | if (method_exists($CI, '_output')) |
| 307 | { |
| 308 | $CI->_output($output); |
| 309 | } |
| 310 | else |
| 311 | { |
| 312 | echo $output; // Send it to the browser! |
| 313 | } |
| 314 | |
| 315 | log_message('debug', "Final output sent to browser"); |
| 316 | log_message('debug', "Total execution time: ".$elapsed); |
| 317 | } |
| 318 | |
| 319 | // -------------------------------------------------------------------- |
| 320 | |
| 321 | /** |
| 322 | * Write a Cache File |
| 323 | * |
| 324 | * @access public |
| 325 | * @return void |
| 326 | */ |
| 327 | function _write_cache($output) |
| 328 | { |
| 329 | $CI =& get_instance(); |
| 330 | $path = $CI->config->item('cache_path'); |
| 331 | |
| 332 | $cache_path = ($path == '') ? BASEPATH.'cache/' : $path; |
| 333 | |
| 334 | if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path)) |
| 335 | { |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | $uri = $CI->config->item('base_url'). |
| 340 | $CI->config->item('index_page'). |
| 341 | $CI->uri->uri_string(); |
| 342 | |
| 343 | $cache_path .= md5($uri); |
| 344 | |
| 345 | if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE)) |
| 346 | { |
| 347 | log_message('error', "Unable to write cache file: ".$cache_path); |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | $expire = time() + ($this->cache_expiration * 60); |
| 352 | |
| 353 | if (flock($fp, LOCK_EX)) |
| 354 | { |
| 355 | fwrite($fp, $expire.'TS--->'.$output); |
| 356 | flock($fp, LOCK_UN); |
| 357 | } |
| 358 | else |
| 359 | { |
| 360 | log_message('error', "Unable to secure a file lock for file at: ".$cache_path); |
| 361 | return; |
| 362 | } |
| 363 | fclose($fp); |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 364 | @chmod($cache_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | |
| 366 | log_message('debug', "Cache file written: ".$cache_path); |
| 367 | } |
| 368 | |
| 369 | // -------------------------------------------------------------------- |
| 370 | |
| 371 | /** |
| 372 | * Update/serve a cached file |
| 373 | * |
| 374 | * @access public |
| 375 | * @return void |
| 376 | */ |
| 377 | function _display_cache(&$CFG, &$URI) |
| 378 | { |
| 379 | $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path'); |
| 380 | |
| 381 | if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path)) |
| 382 | { |
| 383 | return FALSE; |
| 384 | } |
| 385 | |
| 386 | // Build the file path. The file name is an MD5 hash of the full URI |
| 387 | $uri = $CFG->item('base_url'). |
| 388 | $CFG->item('index_page'). |
| 389 | $URI->uri_string; |
| 390 | |
| 391 | $filepath = $cache_path.md5($uri); |
| 392 | |
| 393 | if ( ! @file_exists($filepath)) |
| 394 | { |
| 395 | return FALSE; |
| 396 | } |
| 397 | |
| 398 | if ( ! $fp = @fopen($filepath, FOPEN_READ)) |
| 399 | { |
| 400 | return FALSE; |
| 401 | } |
| 402 | |
| 403 | flock($fp, LOCK_SH); |
| 404 | |
| 405 | $cache = ''; |
| 406 | if (filesize($filepath) > 0) |
| 407 | { |
| 408 | $cache = fread($fp, filesize($filepath)); |
| 409 | } |
| 410 | |
| 411 | flock($fp, LOCK_UN); |
| 412 | fclose($fp); |
| 413 | |
| 414 | // Strip out the embedded timestamp |
| 415 | if ( ! preg_match("/(\d+TS--->)/", $cache, $match)) |
| 416 | { |
| 417 | return FALSE; |
| 418 | } |
| 419 | |
| 420 | // Has the file expired? If so we'll delete it. |
| 421 | if (time() >= trim(str_replace('TS--->', '', $match['1']))) |
| 422 | { |
| 423 | @unlink($filepath); |
| 424 | log_message('debug', "Cache file has expired. File deleted"); |
| 425 | return FALSE; |
| 426 | } |
| 427 | |
| 428 | // Display the cache |
| 429 | $this->_display(str_replace($match['0'], '', $cache)); |
| 430 | log_message('debug', "Cache file is current. Sending it to browser."); |
| 431 | return TRUE; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | } |
| 436 | // END Output Class |
| 437 | |
| 438 | /* End of file Output.php */ |
Derek Jones | c68dfbf | 2010-03-02 12:59:23 -0600 | [diff] [blame] | 439 | /* Location: ./system/core/Output.php */ |