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