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