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