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 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 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 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * Output Class |
| 30 | * |
| 31 | * Responsible for sending final output to browser |
| 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Libraries |
| 35 | * @category Output |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/libraries/output.html |
| 38 | */ |
| 39 | class CI_Output { |
| 40 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 41 | /** |
| 42 | * Current output string |
| 43 | * |
| 44 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 45 | */ |
Andrey Andreev | 0f22117 | 2012-04-13 14:52:16 +0300 | [diff] [blame] | 46 | public $final_output; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 47 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 48 | /** |
| 49 | * Cache expiration time |
| 50 | * |
| 51 | * @var int |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 52 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 53 | public $cache_expiration = 0; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 54 | |
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 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 60 | public $headers = array(); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 61 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 62 | /** |
| 63 | * List of mime types |
| 64 | * |
| 65 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 66 | */ |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 67 | public $mimes = array(); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 68 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 69 | /** |
Thor (atiredmachine) | 242115c | 2012-01-24 23:08:11 -0800 | [diff] [blame] | 70 | * Mime-type for the current page |
| 71 | * |
Thor (atiredmachine) | 5528c0e | 2012-01-25 01:02:28 -0800 | [diff] [blame] | 72 | * @var string |
Thor (atiredmachine) | 242115c | 2012-01-24 23:08:11 -0800 | [diff] [blame] | 73 | */ |
Thor (atiredmachine) | 5528c0e | 2012-01-25 01:02:28 -0800 | [diff] [blame] | 74 | protected $mime_type = 'text/html'; |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 75 | |
Thor (atiredmachine) | 242115c | 2012-01-24 23:08:11 -0800 | [diff] [blame] | 76 | /** |
Alex Bilbie | f512b73 | 2012-06-16 11:15:19 +0100 | [diff] [blame] | 77 | * Determines whether profiler is enabled |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 78 | * |
| 79 | * @var book |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 80 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 81 | public $enable_profiler = FALSE; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 82 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 83 | /** |
| 84 | * Determines if output compression is enabled |
| 85 | * |
| 86 | * @var bool |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 87 | */ |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 88 | protected $_zlib_oc = FALSE; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 89 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 90 | /** |
| 91 | * List of profiler sections |
| 92 | * |
| 93 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 94 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 95 | protected $_profiler_sections = array(); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 96 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 97 | /** |
| 98 | * Whether or not to parse variables like {elapsed_time} and {memory_usage} |
| 99 | * |
| 100 | * @var bool |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 101 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 102 | public $parse_exec_vars = TRUE; |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 103 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 104 | /** |
| 105 | * Set up Output class |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 106 | * |
| 107 | * @return void |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 108 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 109 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | { |
Andrey Andreev | 9ba661b | 2012-06-04 14:44:34 +0300 | [diff] [blame] | 111 | $this->_zlib_oc = (bool) @ini_get('zlib.output_compression'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 112 | |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 113 | // Get mime types for later |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 114 | $this->mimes =& get_mimes(); |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 115 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 116 | log_message('debug', 'Output Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 118 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 120 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | /** |
| 122 | * Get Output |
| 123 | * |
| 124 | * Returns the current output string |
| 125 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 127 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 128 | public function get_output() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | { |
| 130 | return $this->final_output; |
| 131 | } |
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 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 134 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | /** |
| 136 | * Set Output |
| 137 | * |
| 138 | * Sets the output string |
| 139 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | * @param string |
| 141 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 142 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 143 | public function set_output($output) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | { |
| 145 | $this->final_output = $output; |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 146 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // -------------------------------------------------------------------- |
| 150 | |
| 151 | /** |
| 152 | * Append Output |
| 153 | * |
| 154 | * Appends data onto the output string |
| 155 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | * @param string |
| 157 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 159 | public function append_output($output) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | { |
Andrey Andreev | 9ba661b | 2012-06-04 14:44:34 +0300 | [diff] [blame] | 161 | if ($this->final_output == '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | { |
| 163 | $this->final_output = $output; |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | $this->final_output .= $output; |
| 168 | } |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 169 | |
| 170 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // -------------------------------------------------------------------- |
| 174 | |
| 175 | /** |
| 176 | * Set Header |
| 177 | * |
| 178 | * Lets you set a server header which will be outputted with the final display. |
| 179 | * |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 180 | * 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] | 181 | * how to permit header data to be saved with the cache data... |
| 182 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | * @param string |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 184 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 186 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 187 | public function set_header($header, $replace = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | { |
Pascal Kriete | 676e1cd | 2010-04-09 21:16:16 +0200 | [diff] [blame] | 189 | // If zlib.output_compression is enabled it will compress the output, |
| 190 | // but it will not modify the content-length header to compensate for |
| 191 | // the reduction, causing the browser to hang waiting for more data. |
| 192 | // We'll just skip content-length in those cases. |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 193 | if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) === 0) |
Pascal Kriete | 676e1cd | 2010-04-09 21:16:16 +0200 | [diff] [blame] | 194 | { |
| 195 | return; |
| 196 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 197 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | $this->headers[] = array($header, $replace); |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 199 | return $this; |
| 200 | } |
| 201 | |
| 202 | // -------------------------------------------------------------------- |
| 203 | |
| 204 | /** |
| 205 | * Set Content Type Header |
| 206 | * |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 207 | * @param string extension of the file we're outputting |
| 208 | * @return void |
| 209 | */ |
Andrey Andreev | 47b6733 | 2012-06-06 15:58:05 +0300 | [diff] [blame] | 210 | public function set_content_type($mime_type, $charset = NULL) |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 211 | { |
| 212 | if (strpos($mime_type, '/') === FALSE) |
| 213 | { |
| 214 | $extension = ltrim($mime_type, '.'); |
| 215 | |
| 216 | // Is this extension supported? |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 217 | if (isset($this->mimes[$extension])) |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 218 | { |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 219 | $mime_type =& $this->mimes[$extension]; |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 220 | |
| 221 | if (is_array($mime_type)) |
| 222 | { |
| 223 | $mime_type = current($mime_type); |
| 224 | } |
| 225 | } |
| 226 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 227 | |
Thor (atiredmachine) | 242115c | 2012-01-24 23:08:11 -0800 | [diff] [blame] | 228 | $this->mime_type = $mime_type; |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 229 | |
Andrey Andreev | 47b6733 | 2012-06-06 15:58:05 +0300 | [diff] [blame] | 230 | if (empty($charset)) |
| 231 | { |
| 232 | $charset = config_item('charset'); |
| 233 | } |
| 234 | |
| 235 | $header = 'Content-Type: '.$mime_type |
| 236 | .(empty($charset) ? NULL : '; charset='.strtolower($charset)); |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 237 | |
| 238 | $this->headers[] = array($header, TRUE); |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 239 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | } |
Andrey Andreev | 00adf1d | 2012-04-03 12:30:50 +0300 | [diff] [blame] | 241 | |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 242 | // -------------------------------------------------------------------- |
Andrey Andreev | 00adf1d | 2012-04-03 12:30:50 +0300 | [diff] [blame] | 243 | |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 244 | /** |
| 245 | * Get Current Content Type Header |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 246 | * |
Andrey Andreev | 00adf1d | 2012-04-03 12:30:50 +0300 | [diff] [blame] | 247 | * @return string 'text/html', if not already set |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 248 | */ |
Songpol Sripaoeiam | 614db07 | 2012-04-03 01:29:28 +0700 | [diff] [blame] | 249 | public function get_content_type() |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 250 | { |
Andrey Andreev | 00adf1d | 2012-04-03 12:30:50 +0300 | [diff] [blame] | 251 | for ($i = 0, $c = count($this->headers); $i < $c; $i++) |
Songpol Sripao-eiam | 38c0a72 | 2012-04-01 20:10:35 +0700 | [diff] [blame] | 252 | { |
Andrey Andreev | 00adf1d | 2012-04-03 12:30:50 +0300 | [diff] [blame] | 253 | if (preg_match('/^Content-Type:\s(.+)$/', $this->headers[$i][0], $matches)) |
Songpol Sripaoeiam | b966701 | 2012-04-01 17:13:44 +0700 | [diff] [blame] | 254 | { |
Andrey Andreev | 00adf1d | 2012-04-03 12:30:50 +0300 | [diff] [blame] | 255 | return $matches[1]; |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 256 | } |
Songpol Sripaoeiam | b966701 | 2012-04-01 17:13:44 +0700 | [diff] [blame] | 257 | } |
Andrey Andreev | 00adf1d | 2012-04-03 12:30:50 +0300 | [diff] [blame] | 258 | |
Songpol Sripaoeiam | b966701 | 2012-04-01 17:13:44 +0700 | [diff] [blame] | 259 | return 'text/html'; |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 260 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | |
| 262 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 263 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | /** |
| 265 | * Set HTTP Status Header |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 266 | * moved to Common procedural functions in 1.7.2 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 267 | * |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 268 | * @param int the status code |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 269 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 271 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 272 | public function set_status_header($code = 200, $text = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 273 | { |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 274 | set_status_header($code, $text); |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 275 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 277 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 279 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | /** |
| 281 | * Enable/disable Profiler |
| 282 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | * @param bool |
| 284 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 285 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 286 | public function enable_profiler($val = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 287 | { |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 288 | $this->enable_profiler = is_bool($val) ? $val : TRUE; |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 289 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | // -------------------------------------------------------------------- |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 293 | |
| 294 | /** |
| 295 | * Set Profiler Sections |
| 296 | * |
| 297 | * Allows override of default / config settings for Profiler section display |
| 298 | * |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 299 | * @param array |
| 300 | * @return void |
| 301 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 302 | public function set_profiler_sections($sections) |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 303 | { |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 304 | if (isset($sections['query_toggle_count'])) |
| 305 | { |
| 306 | $this->_profiler_sections['query_toggle_count'] = (int) $sections['query_toggle_count']; |
| 307 | unset($sections['query_toggle_count']); |
| 308 | } |
| 309 | |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 310 | foreach ($sections as $section => $enable) |
| 311 | { |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 312 | $this->_profiler_sections[$section] = ($enable !== FALSE); |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 313 | } |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 314 | |
| 315 | return $this; |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 319 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | /** |
| 321 | * Set Cache |
| 322 | * |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 323 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 324 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 325 | */ |
Michiel Vugteveen | 0609d58 | 2012-01-08 13:26:17 +0100 | [diff] [blame] | 326 | public function cache($time) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | { |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 328 | $this->cache_expiration = is_numeric($time) ? $time : 0; |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 329 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 331 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 333 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 334 | /** |
| 335 | * Display Output |
| 336 | * |
| 337 | * All "view" data is automatically put into this variable by the controller class: |
| 338 | * |
| 339 | * $this->final_output |
| 340 | * |
| 341 | * This function sends the finalized output data to the browser along |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 342 | * with any server headers and profile data. It also stops the |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | * benchmark timer so the page rendering speed and memory usage can be shown. |
| 344 | * |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 345 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 346 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 347 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 348 | public function _display($output = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 349 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 350 | // Note: We use globals because we can't use $CI =& get_instance() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | // since this function is sometimes called by the caching mechanism, |
| 352 | // which happens before the CI super object is available. |
| 353 | global $BM, $CFG; |
Derek Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame] | 354 | |
| 355 | // Grab the super object if we can. |
Greg Aker | cc92210 | 2010-11-17 20:25:08 -0600 | [diff] [blame] | 356 | if (class_exists('CI_Controller')) |
Derek Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame] | 357 | { |
| 358 | $CI =& get_instance(); |
| 359 | } |
| 360 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 362 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | // Set the output data |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 364 | if ($output === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | { |
| 366 | $output =& $this->final_output; |
| 367 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 368 | |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 369 | // -------------------------------------------------------------------- |
| 370 | |
| 371 | // Is minify requested? |
| 372 | if ($CFG->item('minify_output') === TRUE) |
| 373 | { |
Thor (atiredmachine) | 5528c0e | 2012-01-25 01:02:28 -0800 | [diff] [blame] | 374 | $output = $this->minify($output, $this->mime_type); |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 377 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 379 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 380 | // 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] | 381 | // own _output() method and we are not dealing with a cache file, which we |
| 382 | // can determine by the existence of the $CI object above |
| 383 | if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | { |
| 385 | $this->_write_cache($output); |
| 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 | // -------------------------------------------------------------------- |
| 389 | |
| 390 | // Parse out the elapsed time and memory usage, |
| 391 | // then swap the pseudo-variables with the data |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 392 | |
| 393 | $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end'); |
Derek Jones | 4652049 | 2010-03-02 13:53:25 -0600 | [diff] [blame] | 394 | |
| 395 | if ($this->parse_exec_vars === TRUE) |
| 396 | { |
Andrey Andreev | c839d28 | 2012-06-07 14:35:27 +0300 | [diff] [blame] | 397 | $memory = round(memory_get_usage() / 1024 / 1024, 2).'MB'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 398 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 399 | $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output); |
Derek Jones | 4652049 | 2010-03-02 13:53:25 -0600 | [diff] [blame] | 400 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | |
| 402 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 403 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | // Is compression requested? |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 405 | if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc === FALSE |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 406 | && extension_loaded('zlib') |
| 407 | && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | { |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 409 | ob_start('ob_gzhandler'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | // -------------------------------------------------------------------- |
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 | // Are there any server headers to send? |
| 415 | if (count($this->headers) > 0) |
| 416 | { |
| 417 | foreach ($this->headers as $header) |
| 418 | { |
| 419 | @header($header[0], $header[1]); |
| 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 | |
| 423 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 424 | |
Derek Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame] | 425 | // Does the $CI object exist? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 426 | // If not we know we are dealing with a cache file so we'll |
| 427 | // simply echo out the data and exit. |
Derek Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame] | 428 | if ( ! isset($CI)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | { |
| 430 | echo $output; |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 431 | log_message('debug', 'Final output sent to browser'); |
| 432 | log_message('debug', 'Total execution time: '.$elapsed); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | return TRUE; |
| 434 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 435 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | // -------------------------------------------------------------------- |
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 | // Do we need to generate profile data? |
| 439 | // If so, load the Profile class and run it. |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 440 | if ($this->enable_profiler === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 442 | $CI->load->library('profiler'); |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 443 | if ( ! empty($this->_profiler_sections)) |
| 444 | { |
| 445 | $CI->profiler->set_sections($this->_profiler_sections); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 446 | } |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 447 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | // If the output data contains closing </body> and </html> tags |
| 449 | // we will remove them and add them back after we insert the profile data |
Andrey Andreev | cba20b1 | 2012-01-09 10:16:41 +0200 | [diff] [blame] | 450 | $output = preg_replace('|</body>.*?</html>|is', '', $output, -1, $count).$CI->profiler->run(); |
| 451 | if ($count > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 453 | $output .= '</body></html>'; |
| 454 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 456 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | // Does the controller contain a function named _output()? |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 458 | // If so send the output there. Otherwise, echo it. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 459 | if (method_exists($CI, '_output')) |
| 460 | { |
| 461 | $CI->_output($output); |
| 462 | } |
| 463 | else |
| 464 | { |
Andrey Andreev | edc8755 | 2012-01-09 09:35:10 +0200 | [diff] [blame] | 465 | echo $output; // Send it to the browser! |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 467 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 468 | log_message('debug', 'Final output sent to browser'); |
| 469 | log_message('debug', 'Total execution time: '.$elapsed); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 471 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | /** |
| 475 | * Write a Cache File |
| 476 | * |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 477 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 479 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 480 | public function _write_cache($output) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 482 | $CI =& get_instance(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 483 | $path = $CI->config->item('cache_path'); |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 484 | $cache_path = ($path === '') ? APPPATH.'cache/' : $path; |
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 | if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path)) |
| 487 | { |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 488 | log_message('error', 'Unable to write cache file: '.$cache_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 489 | return; |
| 490 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 491 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 492 | $uri = $CI->config->item('base_url'). |
| 493 | $CI->config->item('index_page'). |
| 494 | $CI->uri->uri_string(); |
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 | $cache_path .= md5($uri); |
| 497 | |
| 498 | if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE)) |
| 499 | { |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 500 | log_message('error', 'Unable to write cache file: '.$cache_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | return; |
| 502 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 503 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 504 | $expire = time() + ($this->cache_expiration * 60); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 505 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 506 | if (flock($fp, LOCK_EX)) |
| 507 | { |
| 508 | fwrite($fp, $expire.'TS--->'.$output); |
| 509 | flock($fp, LOCK_UN); |
| 510 | } |
| 511 | else |
| 512 | { |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 513 | 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] | 514 | return; |
| 515 | } |
| 516 | fclose($fp); |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 517 | @chmod($cache_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 518 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 519 | log_message('debug', 'Cache file written: '.$cache_path); |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 520 | |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 521 | // Send HTTP cache-control headers to browser to match file cache settings. |
Thor (atiredmachine) | 5528c0e | 2012-01-25 01:02:28 -0800 | [diff] [blame] | 522 | $this->set_cache_header($_SERVER['REQUEST_TIME'], $expire); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 526 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 527 | /** |
| 528 | * Update/serve a cached file |
| 529 | * |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 530 | * @param object config class |
| 531 | * @param object uri class |
Andrey Andreev | 9ba661b | 2012-06-04 14:44:34 +0300 | [diff] [blame] | 532 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 533 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 534 | public function _display_cache(&$CFG, &$URI) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | { |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 536 | $cache_path = ($CFG->item('cache_path') === '') ? APPPATH.'cache/' : $CFG->item('cache_path'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 537 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 538 | // Build the file path. The file name is an MD5 hash of the full URI |
| 539 | $uri = $CFG->item('base_url').$CFG->item('index_page').$URI->uri_string; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 540 | $filepath = $cache_path.md5($uri); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 541 | |
Andrey Andreev | c90d651 | 2012-01-08 04:35:02 +0200 | [diff] [blame] | 542 | if ( ! @file_exists($filepath) OR ! $fp = @fopen($filepath, FOPEN_READ)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 549 | $cache = (filesize($filepath) > 0) ? fread($fp, filesize($filepath)) : ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 550 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 551 | flock($fp, LOCK_UN); |
| 552 | fclose($fp); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 553 | |
| 554 | // Strip out the embedded timestamp |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 555 | if ( ! preg_match('/(\d+TS--->)/', $cache, $match)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 556 | { |
| 557 | return FALSE; |
| 558 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 559 | |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 560 | $last_modified = filemtime($cache_path); |
| 561 | $expire = trim(str_replace('TS--->', '', $match[1])); |
| 562 | |
| 563 | // Has the file expired? |
| 564 | if ($_SERVER['REQUEST_TIME'] >= $expire && is_really_writable($cache_path)) |
Derek Jones | d99e603 | 2010-03-19 19:57:33 -0500 | [diff] [blame] | 565 | { |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 566 | // If so we'll delete it. |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 567 | @unlink($filepath); |
| 568 | log_message('debug', 'Cache file has expired. File deleted.'); |
| 569 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 570 | } |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 571 | else |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 572 | { |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 573 | // Or else send the HTTP cache control headers. |
Thor (atiredmachine) | 5528c0e | 2012-01-25 01:02:28 -0800 | [diff] [blame] | 574 | $this->set_cache_header($last_modified, $expire); |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 575 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 576 | |
| 577 | // Display the cache |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 578 | $this->_display(str_replace($match[0], '', $cache)); |
| 579 | log_message('debug', 'Cache file is current. Sending it to browser.'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 580 | return TRUE; |
| 581 | } |
| 582 | |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 583 | // -------------------------------------------------------------------- |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 584 | |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 585 | /** |
| 586 | * Set the HTTP headers to match the server-side file cache settings |
| 587 | * in order to reduce bandwidth. |
| 588 | * |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 589 | * @param int timestamp of when the page was last modified |
| 590 | * @param int timestamp of when should the requested page expire from cache |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 591 | * @return void |
| 592 | */ |
Thor (atiredmachine) | 5528c0e | 2012-01-25 01:02:28 -0800 | [diff] [blame] | 593 | public function set_cache_header($last_modified, $expiration) |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 594 | { |
| 595 | $max_age = $expiration - $_SERVER['REQUEST_TIME']; |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 596 | |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 597 | if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $last_modified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 598 | { |
| 599 | $this->set_status_header(304); |
| 600 | exit; |
| 601 | } |
| 602 | else |
| 603 | { |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 604 | header('Pragma: public'); |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 605 | header('Cache-Control: max-age=' . $max_age . ', public'); |
| 606 | header('Expires: '.gmdate('D, d M Y H:i:s', $expiration).' GMT'); |
| 607 | header('Last-modified: '.gmdate('D, d M Y H:i:s', $last_modified).' GMT'); |
| 608 | } |
| 609 | } |
| 610 | |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 611 | // -------------------------------------------------------------------- |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 612 | |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 613 | /** |
| 614 | * Reduce excessive size of HTML content. |
| 615 | * |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 616 | * @param string |
| 617 | * @param string |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 618 | * @return string |
| 619 | */ |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 620 | public function minify($output, $type = 'text/html') |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 621 | { |
| 622 | switch ($type) |
| 623 | { |
Thor (atiredmachine) | 1d0661a | 2012-01-24 21:39:16 -0800 | [diff] [blame] | 624 | case 'text/html': |
Thor (atiredmachine) | f59ec6f | 2012-01-24 22:19:14 -0800 | [diff] [blame] | 625 | |
| 626 | $size_before = strlen($output); |
| 627 | |
Phil Sturgeon | 7a744a8 | 2012-06-23 17:21:00 +0100 | [diff] [blame] | 628 | if ($size_before === 0) |
| 629 | { |
| 630 | return ''; |
| 631 | } |
| 632 | |
Thor (atiredmachine) | 5c078ce | 2012-01-26 17:18:35 -0800 | [diff] [blame] | 633 | // Find all the <pre>,<code>,<textarea>, and <javascript> tags |
| 634 | // We'll want to return them to this unprocessed state later. |
Thor (atiredmachine) | 5528c0e | 2012-01-25 01:02:28 -0800 | [diff] [blame] | 635 | preg_match_all('{<pre.+</pre>}msU', $output, $pres_clean); |
| 636 | preg_match_all('{<code.+</code>}msU', $output, $codes_clean); |
| 637 | preg_match_all('{<textarea.+</textarea>}msU', $output, $textareas_clean); |
Thor (atiredmachine) | 5c078ce | 2012-01-26 17:18:35 -0800 | [diff] [blame] | 638 | preg_match_all('{<script.+</script>}msU', $output, $javascript_clean); |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 639 | |
Thor (atiredmachine) | d68c819 | 2012-01-24 20:56:21 -0800 | [diff] [blame] | 640 | // Minify the CSS in all the <style> tags. |
Thor (atiredmachine) | 5528c0e | 2012-01-25 01:02:28 -0800 | [diff] [blame] | 641 | preg_match_all('{<style.+</style>}msU', $output, $style_clean); |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 642 | foreach ($style_clean[0] as $s) |
| 643 | { |
Thor | 1b8d0ef | 2012-01-28 01:48:04 -0800 | [diff] [blame] | 644 | $output = str_replace($s, $this->minify($s, 'text/css'), $output); |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 645 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 646 | |
Thor (atiredmachine) | 5c078ce | 2012-01-26 17:18:35 -0800 | [diff] [blame] | 647 | // Minify the javascript in <script> tags. |
| 648 | foreach ($javascript_clean[0] as $s) |
| 649 | { |
Thor | 1b8d0ef | 2012-01-28 01:48:04 -0800 | [diff] [blame] | 650 | $javascript_mini[] = $this->minify($s, 'text/javascript'); |
Thor (atiredmachine) | 5c078ce | 2012-01-26 17:18:35 -0800 | [diff] [blame] | 651 | } |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 652 | |
Thor (atiredmachine) | a2ae6e1 | 2012-01-24 20:57:48 -0800 | [diff] [blame] | 653 | // Replace multiple spaces with a single space. |
Thor | 1b8d0ef | 2012-01-28 01:48:04 -0800 | [diff] [blame] | 654 | $output = preg_replace('!\s{2,}!', ' ', $output); |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 655 | |
Thor (atiredmachine) | 5de1175 | 2012-01-24 22:08:36 -0800 | [diff] [blame] | 656 | // Remove comments (non-MSIE conditionals) |
Thor (atiredmachine) | 5528c0e | 2012-01-25 01:02:28 -0800 | [diff] [blame] | 657 | $output = preg_replace('{\s*<!--[^\[].*-->\s*}msU', '', $output); |
Thor (atiredmachine) | 5de1175 | 2012-01-24 22:08:36 -0800 | [diff] [blame] | 658 | |
Thor (atiredmachine) | a2ae6e1 | 2012-01-24 20:57:48 -0800 | [diff] [blame] | 659 | // Remove spaces around block-level elements. |
Phil Sturgeon | 3b6af43 | 2012-06-21 09:21:17 -0500 | [diff] [blame] | 660 | $output = preg_replace('/\s*(<\/?(html|head|title|meta|script|link|style|body|h[1-6]|div|p|br)[^>]*>)\s*/is', '$1', $output); |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 661 | |
Thor (atiredmachine) | d68c819 | 2012-01-24 20:56:21 -0800 | [diff] [blame] | 662 | // Replace mangled <pre> etc. tags with unprocessed ones. |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 663 | |
Thor (atiredmachine) | 5c078ce | 2012-01-26 17:18:35 -0800 | [diff] [blame] | 664 | if ( ! empty($pres_clean)) |
| 665 | { |
| 666 | preg_match_all('{<pre.+</pre>}msU', $output, $pres_messed); |
| 667 | $output = str_replace($pres_messed[0], $pres_clean[0], $output); |
| 668 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 669 | |
Thor (atiredmachine) | 5c078ce | 2012-01-26 17:18:35 -0800 | [diff] [blame] | 670 | if ( ! empty($codes_clean)) |
| 671 | { |
| 672 | preg_match_all('{<code.+</code>}msU', $output, $codes_messed); |
| 673 | $output = str_replace($codes_messed[0], $codes_clean[0], $output); |
| 674 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 675 | |
Thor (atiredmachine) | 5c078ce | 2012-01-26 17:18:35 -0800 | [diff] [blame] | 676 | if ( ! empty($codes_clean)) |
| 677 | { |
| 678 | preg_match_all('{<textarea.+</textarea>}msU', $output, $textareas_messed); |
| 679 | $output = str_replace($textareas_messed[0], $textareas_clean[0], $output); |
| 680 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 681 | |
Thor (atiredmachine) | 5c078ce | 2012-01-26 17:18:35 -0800 | [diff] [blame] | 682 | if (isset($javascript_mini)) |
| 683 | { |
| 684 | preg_match_all('{<script.+</script>}msU', $output, $javascript_messed); |
| 685 | $output = str_replace($javascript_messed[0], $javascript_mini, $output); |
| 686 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 687 | |
Thor (atiredmachine) | 5c078ce | 2012-01-26 17:18:35 -0800 | [diff] [blame] | 688 | $size_removed = $size_before - strlen($output); |
| 689 | $savings_percent = round(($size_removed / $size_before * 100)); |
| 690 | |
| 691 | log_message('debug', 'Minifier shaved '.($size_removed / 1000).'KB ('.$savings_percent.'%) off final HTML output.'); |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 692 | |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 693 | break; |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 694 | |
Thor (atiredmachine) | 1d0661a | 2012-01-24 21:39:16 -0800 | [diff] [blame] | 695 | case 'text/css': |
Will Mendes | 11dc139 | 2012-12-14 14:52:49 -0200 | [diff] [blame^] | 696 | case 'text/javascript': |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 697 | |
Thor (atiredmachine) | 5c078ce | 2012-01-26 17:18:35 -0800 | [diff] [blame] | 698 | //Remove CSS comments |
| 699 | $output = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $output); |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 700 | |
Thor (atiredmachine) | 5c078ce | 2012-01-26 17:18:35 -0800 | [diff] [blame] | 701 | // Remove spaces around curly brackets, colons, |
| 702 | // semi-colons, parenthesis, commas |
| 703 | $output = preg_replace('!\s*(:|;|,|}|{|\(|\))\s*!', '$1', $output); |
| 704 | |
Will Mendes | 11dc139 | 2012-12-14 14:52:49 -0200 | [diff] [blame^] | 705 | // Remove spaces |
| 706 | $output = preg_replace('/ /s', ' ', $output); |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 707 | |
Will Mendes | 11dc139 | 2012-12-14 14:52:49 -0200 | [diff] [blame^] | 708 | // Remove breaklines and tabs |
| 709 | $output = preg_replace('/[\r\n\t]/', '', $output); |
Thor (atiredmachine) | 5c078ce | 2012-01-26 17:18:35 -0800 | [diff] [blame] | 710 | |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 711 | break; |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 712 | |
| 713 | default: break; |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 714 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 715 | |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 716 | return $output; |
| 717 | } |
| 718 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 719 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 720 | |
| 721 | /* End of file Output.php */ |
Will Mendes | 11dc139 | 2012-12-14 14:52:49 -0200 | [diff] [blame^] | 722 | /* Location: ./system/core/Output.php */ |