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