Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Output Class |
| 31 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 32 | * Responsible for sending final output to the browser. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 33 | * |
| 34 | * @package CodeIgniter |
| 35 | * @subpackage Libraries |
| 36 | * @category Output |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 37 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | * @link http://codeigniter.com/user_guide/libraries/output.html |
| 39 | */ |
| 40 | class CI_Output { |
| 41 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 42 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 43 | * Final output string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 44 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 45 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 46 | */ |
Andrey Andreev | 0f22117 | 2012-04-13 14:52:16 +0300 | [diff] [blame] | 47 | public $final_output; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 48 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 49 | /** |
| 50 | * Cache expiration time |
| 51 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 52 | * @var int |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 53 | */ |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 54 | public $cache_expiration = 0; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 55 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 56 | /** |
| 57 | * List of server headers |
| 58 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 59 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 60 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 61 | public $headers = array(); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 62 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 63 | /** |
| 64 | * List of mime types |
| 65 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 66 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 67 | */ |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 68 | public $mimes = array(); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 69 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 70 | /** |
Thor (atiredmachine) | 242115c | 2012-01-24 23:08:11 -0800 | [diff] [blame] | 71 | * Mime-type for the current page |
| 72 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 73 | * @var string |
Thor (atiredmachine) | 242115c | 2012-01-24 23:08:11 -0800 | [diff] [blame] | 74 | */ |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 75 | protected $mime_type = 'text/html'; |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 76 | |
Thor (atiredmachine) | 242115c | 2012-01-24 23:08:11 -0800 | [diff] [blame] | 77 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 78 | * Enable Profiler flag |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 79 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 80 | * @var bool |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 81 | */ |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 82 | public $enable_profiler = FALSE; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 83 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 84 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 85 | * zLib output compression flag |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 86 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 87 | * @var bool |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 88 | */ |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 89 | protected $_zlib_oc = FALSE; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 90 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 91 | /** |
| 92 | * List of profiler sections |
| 93 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 94 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 95 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 96 | protected $_profiler_sections = array(); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 97 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 98 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 99 | * Parse markers flag |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 100 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 101 | * Whether or not to parse variables like {elapsed_time} and {memory_usage}. |
| 102 | * |
| 103 | * @var bool |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 104 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 105 | public $parse_exec_vars = TRUE; |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 106 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 107 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 108 | * Class constructor |
| 109 | * |
| 110 | * Determines whether zLib output compression will be used. |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 111 | * |
| 112 | * @return void |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 113 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 114 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | { |
Andrey Andreev | 9ba661b | 2012-06-04 14:44:34 +0300 | [diff] [blame] | 116 | $this->_zlib_oc = (bool) @ini_get('zlib.output_compression'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 117 | |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 118 | // Get mime types for later |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 119 | $this->mimes =& get_mimes(); |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 120 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 121 | log_message('debug', 'Output Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 129 | * Returns the current output string. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 132 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 133 | public function get_output() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | { |
| 135 | return $this->final_output; |
| 136 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 137 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 139 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | /** |
| 141 | * Set Output |
| 142 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 143 | * Sets the output string. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 145 | * @param string $output Output data |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 146 | * @return CI_Output |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 147 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 148 | public function set_output($output) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | { |
| 150 | $this->final_output = $output; |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 151 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // -------------------------------------------------------------------- |
| 155 | |
| 156 | /** |
| 157 | * Append Output |
| 158 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 159 | * Appends data onto the output string. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 161 | * @param string $output Data to append |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 162 | * @return CI_Output |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 164 | public function append_output($output) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | { |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 166 | if (empty($this->final_output)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | { |
| 168 | $this->final_output = $output; |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | $this->final_output .= $output; |
| 173 | } |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 174 | |
| 175 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // -------------------------------------------------------------------- |
| 179 | |
| 180 | /** |
| 181 | * Set Header |
| 182 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 183 | * Lets you set a server header which will be sent with the final output. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 185 | * Note: If a file is cached, headers will not be sent. |
| 186 | * @todo We need to figure out how to permit headers to be cached. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 188 | * @param string $header Header |
| 189 | * @param bool $replace Whether to replace the old header value, if already set |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 190 | * @return CI_Output |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | */ |
Andrey Andreev | 44dc50e | 2012-10-28 13:30:21 +0200 | [diff] [blame] | 192 | public function set_header($header, $replace = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | { |
Pascal Kriete | 676e1cd | 2010-04-09 21:16:16 +0200 | [diff] [blame] | 194 | // If zlib.output_compression is enabled it will compress the output, |
| 195 | // but it will not modify the content-length header to compensate for |
| 196 | // the reduction, causing the browser to hang waiting for more data. |
| 197 | // We'll just skip content-length in those cases. |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 198 | if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) === 0) |
Pascal Kriete | 676e1cd | 2010-04-09 21:16:16 +0200 | [diff] [blame] | 199 | { |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 200 | return $this; |
Pascal Kriete | 676e1cd | 2010-04-09 21:16:16 +0200 | [diff] [blame] | 201 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 202 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | $this->headers[] = array($header, $replace); |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 204 | return $this; |
| 205 | } |
| 206 | |
| 207 | // -------------------------------------------------------------------- |
| 208 | |
| 209 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 210 | * Set Content-Type Header |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 211 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 212 | * @param string $mime_type Extension of the file we're outputting |
| 213 | * @param string $charset Character set (default: NULL) |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 214 | * @return CI_Output |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 215 | */ |
Andrey Andreev | 47b6733 | 2012-06-06 15:58:05 +0300 | [diff] [blame] | 216 | public function set_content_type($mime_type, $charset = NULL) |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 217 | { |
| 218 | if (strpos($mime_type, '/') === FALSE) |
| 219 | { |
| 220 | $extension = ltrim($mime_type, '.'); |
| 221 | |
| 222 | // Is this extension supported? |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 223 | if (isset($this->mimes[$extension])) |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 224 | { |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 225 | $mime_type =& $this->mimes[$extension]; |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 226 | |
| 227 | if (is_array($mime_type)) |
| 228 | { |
| 229 | $mime_type = current($mime_type); |
| 230 | } |
| 231 | } |
| 232 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 233 | |
Thor (atiredmachine) | 242115c | 2012-01-24 23:08:11 -0800 | [diff] [blame] | 234 | $this->mime_type = $mime_type; |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 235 | |
Andrey Andreev | 47b6733 | 2012-06-06 15:58:05 +0300 | [diff] [blame] | 236 | if (empty($charset)) |
| 237 | { |
| 238 | $charset = config_item('charset'); |
| 239 | } |
| 240 | |
| 241 | $header = 'Content-Type: '.$mime_type |
| 242 | .(empty($charset) ? NULL : '; charset='.strtolower($charset)); |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 243 | |
| 244 | $this->headers[] = array($header, TRUE); |
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 | } |
Andrey Andreev | 00adf1d | 2012-04-03 12:30:50 +0300 | [diff] [blame] | 247 | |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 248 | // -------------------------------------------------------------------- |
Andrey Andreev | 00adf1d | 2012-04-03 12:30:50 +0300 | [diff] [blame] | 249 | |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 250 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 251 | * Get Current Content-Type Header |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 252 | * |
Andrey Andreev | 00adf1d | 2012-04-03 12:30:50 +0300 | [diff] [blame] | 253 | * @return string 'text/html', if not already set |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 254 | */ |
Songpol Sripaoeiam | 614db07 | 2012-04-03 01:29:28 +0700 | [diff] [blame] | 255 | public function get_content_type() |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 256 | { |
Andrey Andreev | 00adf1d | 2012-04-03 12:30:50 +0300 | [diff] [blame] | 257 | for ($i = 0, $c = count($this->headers); $i < $c; $i++) |
Songpol Sripao-eiam | 38c0a72 | 2012-04-01 20:10:35 +0700 | [diff] [blame] | 258 | { |
Andrey Andreev | cc4b003 | 2012-11-29 17:21:43 +0200 | [diff] [blame] | 259 | if (sscanf($this->headers[$i][0], 'Content-Type: %[^;]', $content_type) === 1) |
Songpol Sripaoeiam | b966701 | 2012-04-01 17:13:44 +0700 | [diff] [blame] | 260 | { |
Andrey Andreev | 7a7ad78 | 2012-11-12 17:21:01 +0200 | [diff] [blame] | 261 | return $content_type; |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 262 | } |
Songpol Sripaoeiam | b966701 | 2012-04-01 17:13:44 +0700 | [diff] [blame] | 263 | } |
Andrey Andreev | 00adf1d | 2012-04-03 12:30:50 +0300 | [diff] [blame] | 264 | |
Songpol Sripaoeiam | b966701 | 2012-04-01 17:13:44 +0700 | [diff] [blame] | 265 | return 'text/html'; |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 266 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | |
| 268 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 269 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | /** |
Andrey Andreev | cc4b003 | 2012-11-29 17:21:43 +0200 | [diff] [blame] | 271 | * Get Header |
| 272 | * |
| 273 | * @param string $header_name |
| 274 | * @return string |
| 275 | */ |
| 276 | public function get_header($header) |
| 277 | { |
| 278 | // Combine headers already sent with our batched headers |
| 279 | $headers = array_merge( |
| 280 | // We only need [x][0] from our multi-dimensional array |
| 281 | array_map('array_shift', $this->headers), |
| 282 | headers_list() |
| 283 | ); |
| 284 | |
| 285 | if (empty($headers) OR empty($header)) |
| 286 | { |
| 287 | return NULL; |
| 288 | } |
| 289 | |
| 290 | for ($i = 0, $c = count($headers); $i < $c; $i++) |
| 291 | { |
| 292 | if (strncasecmp($header, $headers[$i], $l = strlen($header)) === 0) |
| 293 | { |
| 294 | return trim(substr($headers[$i], $l+1)); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | return NULL; |
| 299 | } |
| 300 | |
| 301 | // -------------------------------------------------------------------- |
| 302 | |
| 303 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | * Set HTTP Status Header |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 305 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 306 | * As of version 1.7.2, this is an alias for common function |
| 307 | * set_status_header(). |
| 308 | * |
| 309 | * @param int $code Status code (default: 200) |
| 310 | * @param string $text Optional message |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 311 | * @return CI_Output |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 312 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 313 | public function set_status_header($code = 200, $text = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | { |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 315 | set_status_header($code, $text); |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 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 | * Enable/disable Profiler |
| 323 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 324 | * @param bool $val TRUE to enable or FALSE to disable |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 325 | * @return CI_Output |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 326 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 327 | public function enable_profiler($val = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | { |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 329 | $this->enable_profiler = is_bool($val) ? $val : TRUE; |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 330 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 331 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 332 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | // -------------------------------------------------------------------- |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 334 | |
| 335 | /** |
| 336 | * Set Profiler Sections |
| 337 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 338 | * Allows override of default/config settings for |
| 339 | * Profiler section display. |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 340 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 341 | * @param array $sections Profiler sections |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 342 | * @return CI_Output |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 343 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 344 | public function set_profiler_sections($sections) |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 345 | { |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 346 | if (isset($sections['query_toggle_count'])) |
| 347 | { |
| 348 | $this->_profiler_sections['query_toggle_count'] = (int) $sections['query_toggle_count']; |
| 349 | unset($sections['query_toggle_count']); |
| 350 | } |
| 351 | |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 352 | foreach ($sections as $section => $enable) |
| 353 | { |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 354 | $this->_profiler_sections[$section] = ($enable !== FALSE); |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 355 | } |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 356 | |
| 357 | return $this; |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 361 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 362 | /** |
| 363 | * Set Cache |
| 364 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 365 | * @param int $time Cache expiration time in seconds |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 366 | * @return CI_Output |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 367 | */ |
Michiel Vugteveen | 0609d58 | 2012-01-08 13:26:17 +0100 | [diff] [blame] | 368 | public function cache($time) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | { |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 370 | $this->cache_expiration = is_numeric($time) ? $time : 0; |
Phil Sturgeon | 60ed1a3 | 2011-03-08 21:43:54 +0000 | [diff] [blame] | 371 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 373 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 375 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | /** |
| 377 | * Display Output |
| 378 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 379 | * Processes sends the sends finalized output data to the browser along |
| 380 | * with any server headers and profile data. It also stops benchmark |
| 381 | * timers so the page rendering speed and memory usage can be shown. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 383 | * Note: All "view" data is automatically put into $this->final_output |
| 384 | * by controller class. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 386 | * @uses CI_Output::$final_output |
| 387 | * @param string $output Output data override |
| 388 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 389 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 390 | public function _display($output = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 391 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 392 | // Note: We use globals because we can't use $CI =& get_instance() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | // since this function is sometimes called by the caching mechanism, |
| 394 | // which happens before the CI super object is available. |
| 395 | global $BM, $CFG; |
Derek Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame] | 396 | |
| 397 | // Grab the super object if we can. |
Greg Aker | cc92210 | 2010-11-17 20:25:08 -0600 | [diff] [blame] | 398 | if (class_exists('CI_Controller')) |
Derek Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame] | 399 | { |
| 400 | $CI =& get_instance(); |
| 401 | } |
| 402 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 404 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | // Set the output data |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 406 | if ($output === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | { |
| 408 | $output =& $this->final_output; |
| 409 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 410 | |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 411 | // -------------------------------------------------------------------- |
| 412 | |
| 413 | // Is minify requested? |
| 414 | if ($CFG->item('minify_output') === TRUE) |
| 415 | { |
Thor (atiredmachine) | 5528c0e | 2012-01-25 01:02:28 -0800 | [diff] [blame] | 416 | $output = $this->minify($output, $this->mime_type); |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 420 | |
vkeranov | a8349bc | 2012-10-27 18:07:59 +0300 | [diff] [blame] | 421 | // 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] | 422 | // own _output() method and we are not dealing with a cache file, which we |
| 423 | // can determine by the existence of the $CI object above |
| 424 | if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | { |
| 426 | $this->_write_cache($output); |
| 427 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 428 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | // -------------------------------------------------------------------- |
| 430 | |
| 431 | // Parse out the elapsed time and memory usage, |
| 432 | // then swap the pseudo-variables with the data |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 433 | |
| 434 | $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end'); |
Derek Jones | 4652049 | 2010-03-02 13:53:25 -0600 | [diff] [blame] | 435 | |
| 436 | if ($this->parse_exec_vars === TRUE) |
| 437 | { |
Andrey Andreev | c839d28 | 2012-06-07 14:35:27 +0300 | [diff] [blame] | 438 | $memory = round(memory_get_usage() / 1024 / 1024, 2).'MB'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 439 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 440 | $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output); |
Derek Jones | 4652049 | 2010-03-02 13:53:25 -0600 | [diff] [blame] | 441 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 442 | |
| 443 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 444 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | // Is compression requested? |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 446 | if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc === FALSE |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 447 | && extension_loaded('zlib') |
| 448 | && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | { |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 450 | ob_start('ob_gzhandler'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 454 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | // Are there any server headers to send? |
| 456 | if (count($this->headers) > 0) |
| 457 | { |
| 458 | foreach ($this->headers as $header) |
| 459 | { |
| 460 | @header($header[0], $header[1]); |
| 461 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 462 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | |
| 464 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 465 | |
Derek Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame] | 466 | // Does the $CI object exist? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | // If not we know we are dealing with a cache file so we'll |
| 468 | // simply echo out the data and exit. |
Derek Jones | d763349 | 2010-09-28 13:14:57 -0500 | [diff] [blame] | 469 | if ( ! isset($CI)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | { |
| 471 | echo $output; |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 472 | log_message('debug', 'Final output sent to browser'); |
| 473 | log_message('debug', 'Total execution time: '.$elapsed); |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 474 | return; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 475 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 476 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 477 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 478 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 479 | // Do we need to generate profile data? |
| 480 | // If so, load the Profile class and run it. |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 481 | if ($this->enable_profiler === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 483 | $CI->load->library('profiler'); |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 484 | if ( ! empty($this->_profiler_sections)) |
| 485 | { |
| 486 | $CI->profiler->set_sections($this->_profiler_sections); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 487 | } |
Derek Jones | ee71c80 | 2010-03-10 10:05:05 -0600 | [diff] [blame] | 488 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 489 | // If the output data contains closing </body> and </html> tags |
| 490 | // 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] | 491 | $output = preg_replace('|</body>.*?</html>|is', '', $output, -1, $count).$CI->profiler->run(); |
| 492 | if ($count > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 493 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 494 | $output .= '</body></html>'; |
| 495 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | // Does the controller contain a function named _output()? |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 499 | // If so send the output there. Otherwise, echo it. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | if (method_exists($CI, '_output')) |
| 501 | { |
| 502 | $CI->_output($output); |
| 503 | } |
| 504 | else |
| 505 | { |
Andrey Andreev | edc8755 | 2012-01-09 09:35:10 +0200 | [diff] [blame] | 506 | echo $output; // Send it to the browser! |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 507 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 508 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 509 | log_message('debug', 'Final output sent to browser'); |
| 510 | log_message('debug', 'Total execution time: '.$elapsed); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 511 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 512 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 513 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 514 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 515 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 516 | * Write Cache |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 517 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 518 | * @param string $output Output data to cache |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 519 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 520 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 521 | public function _write_cache($output) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 522 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 523 | $CI =& get_instance(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 524 | $path = $CI->config->item('cache_path'); |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 525 | $cache_path = ($path === '') ? APPPATH.'cache/' : $path; |
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 | if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path)) |
| 528 | { |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 529 | log_message('error', 'Unable to write cache file: '.$cache_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 530 | return; |
| 531 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 532 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 533 | $uri = $CI->config->item('base_url'). |
| 534 | $CI->config->item('index_page'). |
| 535 | $CI->uri->uri_string(); |
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 | $cache_path .= md5($uri); |
| 538 | |
| 539 | if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE)) |
| 540 | { |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 541 | log_message('error', 'Unable to write cache file: '.$cache_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 542 | return; |
| 543 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 544 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 545 | $expire = time() + ($this->cache_expiration * 60); |
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 | if (flock($fp, LOCK_EX)) |
| 548 | { |
| 549 | fwrite($fp, $expire.'TS--->'.$output); |
| 550 | flock($fp, LOCK_UN); |
| 551 | } |
| 552 | else |
| 553 | { |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 554 | 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] | 555 | return; |
| 556 | } |
| 557 | fclose($fp); |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 558 | @chmod($cache_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 559 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 560 | log_message('debug', 'Cache file written: '.$cache_path); |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 561 | |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 562 | // Send HTTP cache-control headers to browser to match file cache settings. |
Thor (atiredmachine) | 5528c0e | 2012-01-25 01:02:28 -0800 | [diff] [blame] | 563 | $this->set_cache_header($_SERVER['REQUEST_TIME'], $expire); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 567 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 568 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 569 | * Update/serve cached output |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 570 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 571 | * @uses CI_Config |
| 572 | * @uses CI_URI |
| 573 | * |
| 574 | * @param object &$CFG CI_Config class instance |
| 575 | * @param object &$URI CI_URI class instance |
| 576 | * @return bool TRUE on success or FALSE on failure |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 577 | */ |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 578 | public function _display_cache(&$CFG, &$URI) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 579 | { |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 580 | $cache_path = ($CFG->item('cache_path') === '') ? APPPATH.'cache/' : $CFG->item('cache_path'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 581 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 582 | // Build the file path. The file name is an MD5 hash of the full URI |
| 583 | $uri = $CFG->item('base_url').$CFG->item('index_page').$URI->uri_string; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 584 | $filepath = $cache_path.md5($uri); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 585 | |
Andrey Andreev | c90d651 | 2012-01-08 04:35:02 +0200 | [diff] [blame] | 586 | if ( ! @file_exists($filepath) OR ! $fp = @fopen($filepath, FOPEN_READ)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 587 | { |
| 588 | return FALSE; |
| 589 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 590 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 591 | flock($fp, LOCK_SH); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 592 | |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 593 | $cache = (filesize($filepath) > 0) ? fread($fp, filesize($filepath)) : ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 594 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 595 | flock($fp, LOCK_UN); |
| 596 | fclose($fp); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 597 | |
| 598 | // Strip out the embedded timestamp |
vlakoff | 239e0e5 | 2012-09-16 19:16:33 +0200 | [diff] [blame] | 599 | if ( ! preg_match('/^(\d+)TS--->/', $cache, $match)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 600 | { |
| 601 | return FALSE; |
| 602 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 603 | |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 604 | $last_modified = filemtime($cache_path); |
vlakoff | 239e0e5 | 2012-09-16 19:16:33 +0200 | [diff] [blame] | 605 | $expire = $match[1]; |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 606 | |
| 607 | // Has the file expired? |
| 608 | if ($_SERVER['REQUEST_TIME'] >= $expire && is_really_writable($cache_path)) |
Derek Jones | d99e603 | 2010-03-19 19:57:33 -0500 | [diff] [blame] | 609 | { |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 610 | // If so we'll delete it. |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 611 | @unlink($filepath); |
| 612 | log_message('debug', 'Cache file has expired. File deleted.'); |
| 613 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 614 | } |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 615 | else |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 616 | { |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 617 | // Or else send the HTTP cache control headers. |
Thor (atiredmachine) | 5528c0e | 2012-01-25 01:02:28 -0800 | [diff] [blame] | 618 | $this->set_cache_header($last_modified, $expire); |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 619 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 620 | |
| 621 | // Display the cache |
vlakoff | 1288fe3 | 2012-07-23 16:22:06 +0200 | [diff] [blame] | 622 | $this->_display(substr($cache, strlen($match[0]))); |
Andrey Andreev | 1f5fbb6 | 2012-01-07 20:53:29 +0200 | [diff] [blame] | 623 | log_message('debug', 'Cache file is current. Sending it to browser.'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 624 | return TRUE; |
| 625 | } |
| 626 | |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 627 | // -------------------------------------------------------------------- |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 628 | |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 629 | /** |
Andrey Andreev | b37d2bc | 2012-11-30 02:19:35 +0200 | [diff] [blame] | 630 | * Delete cache |
| 631 | * |
| 632 | * @param string $uri URI string |
| 633 | * @return bool |
| 634 | */ |
| 635 | public function delete_cache($uri = '') |
| 636 | { |
| 637 | $CI =& get_instance(); |
| 638 | $cache_path = $CI->config->item('cache_path'); |
| 639 | if ($cache_path === '') |
| 640 | { |
| 641 | $cache_path = APPPATH.'cache/'; |
| 642 | } |
| 643 | |
| 644 | if ( ! is_dir($cache_path)) |
| 645 | { |
| 646 | log_message('error', 'Unable to find cache path: '.$cache_path); |
| 647 | return FALSE; |
| 648 | } |
| 649 | |
| 650 | if (empty($uri)) |
| 651 | { |
| 652 | $uri = $CI->uri->uri_string(); |
| 653 | } |
| 654 | |
| 655 | $cache_path .= md5($CI->config->item('base_url').$CI->config->item('index_page').$uri); |
| 656 | |
| 657 | if ( ! @unlink($cache_path)) |
| 658 | { |
| 659 | log_message('error', 'Unable to delete cache file for '.$uri); |
| 660 | return FALSE; |
| 661 | } |
| 662 | |
| 663 | return TRUE; |
| 664 | } |
| 665 | |
| 666 | // -------------------------------------------------------------------- |
| 667 | |
| 668 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 669 | * Set Cache Header |
| 670 | * |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 671 | * Set the HTTP headers to match the server-side file cache settings |
| 672 | * in order to reduce bandwidth. |
| 673 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 674 | * @param int $last_modified Timestamp of when the page was last modified |
| 675 | * @param int $expiration Timestamp of when should the requested page expire from cache |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 676 | * @return void |
| 677 | */ |
Thor (atiredmachine) | 5528c0e | 2012-01-25 01:02:28 -0800 | [diff] [blame] | 678 | public function set_cache_header($last_modified, $expiration) |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 679 | { |
| 680 | $max_age = $expiration - $_SERVER['REQUEST_TIME']; |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 681 | |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 682 | 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] | 683 | { |
| 684 | $this->set_status_header(304); |
| 685 | exit; |
| 686 | } |
| 687 | else |
| 688 | { |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 689 | header('Pragma: public'); |
Thor (atiredmachine) | c8efb80 | 2012-01-24 13:33:39 -0800 | [diff] [blame] | 690 | header('Cache-Control: max-age=' . $max_age . ', public'); |
| 691 | header('Expires: '.gmdate('D, d M Y H:i:s', $expiration).' GMT'); |
| 692 | header('Last-modified: '.gmdate('D, d M Y H:i:s', $last_modified).' GMT'); |
| 693 | } |
| 694 | } |
| 695 | |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 696 | // -------------------------------------------------------------------- |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 697 | |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 698 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 699 | * Minify |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 700 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 701 | * Reduce excessive size of HTML/CSS/JavaScript content. |
| 702 | * |
| 703 | * @param string $output Output to minify |
| 704 | * @param string $type Output content MIME type |
| 705 | * @return string Minified output |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 706 | */ |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 707 | public function minify($output, $type = 'text/html') |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 708 | { |
| 709 | switch ($type) |
| 710 | { |
| 711 | case 'text/html': |
Thor (atiredmachine) | f59ec6f | 2012-01-24 22:19:14 -0800 | [diff] [blame] | 712 | |
Andrey Andreev | 72ed4c3 | 2012-12-19 17:07:54 +0200 | [diff] [blame] | 713 | if (($size_before = strlen($output)) === 0) |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 714 | { |
| 715 | return ''; |
| 716 | } |
Phil Sturgeon | 7a744a8 | 2012-06-23 17:21:00 +0100 | [diff] [blame] | 717 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 718 | // Find all the <pre>,<code>,<textarea>, and <javascript> tags |
| 719 | // We'll want to return them to this unprocessed state later. |
| 720 | preg_match_all('{<pre.+</pre>}msU', $output, $pres_clean); |
| 721 | preg_match_all('{<code.+</code>}msU', $output, $codes_clean); |
| 722 | preg_match_all('{<textarea.+</textarea>}msU', $output, $textareas_clean); |
| 723 | preg_match_all('{<script.+</script>}msU', $output, $javascript_clean); |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 724 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 725 | // Minify the CSS in all the <style> tags. |
| 726 | preg_match_all('{<style.+</style>}msU', $output, $style_clean); |
| 727 | foreach ($style_clean[0] as $s) |
| 728 | { |
Andrew Podner | 9dfceda | 2012-12-18 19:37:22 -0500 | [diff] [blame] | 729 | $output = str_replace($s, $this->_minify_script_style($s, TRUE), $output); |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 730 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 731 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 732 | // Minify the javascript in <script> tags. |
| 733 | foreach ($javascript_clean[0] as $s) |
| 734 | { |
Andrew Podner | 9dfceda | 2012-12-18 19:37:22 -0500 | [diff] [blame] | 735 | $javascript_mini[] = $this->_minify_script_style($s, TRUE); |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 736 | } |
Thor (atiredmachine) | 79db4cd | 2012-01-24 20:44:51 -0800 | [diff] [blame] | 737 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 738 | // Replace multiple spaces with a single space. |
| 739 | $output = preg_replace('!\s{2,}!', ' ', $output); |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 740 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 741 | // Remove comments (non-MSIE conditionals) |
| 742 | $output = preg_replace('{\s*<!--[^\[].*-->\s*}msU', '', $output); |
Thor (atiredmachine) | 5de1175 | 2012-01-24 22:08:36 -0800 | [diff] [blame] | 743 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 744 | // Remove spaces around block-level elements. |
| 745 | $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] | 746 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 747 | // Replace mangled <pre> etc. tags with unprocessed ones. |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 748 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 749 | if ( ! empty($pres_clean)) |
| 750 | { |
| 751 | preg_match_all('{<pre.+</pre>}msU', $output, $pres_messed); |
| 752 | $output = str_replace($pres_messed[0], $pres_clean[0], $output); |
| 753 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 754 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 755 | if ( ! empty($codes_clean)) |
| 756 | { |
| 757 | preg_match_all('{<code.+</code>}msU', $output, $codes_messed); |
| 758 | $output = str_replace($codes_messed[0], $codes_clean[0], $output); |
| 759 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 760 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 761 | if ( ! empty($codes_clean)) |
| 762 | { |
| 763 | preg_match_all('{<textarea.+</textarea>}msU', $output, $textareas_messed); |
| 764 | $output = str_replace($textareas_messed[0], $textareas_clean[0], $output); |
| 765 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 766 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 767 | if (isset($javascript_mini)) |
| 768 | { |
| 769 | preg_match_all('{<script.+</script>}msU', $output, $javascript_messed); |
| 770 | $output = str_replace($javascript_messed[0], $javascript_mini, $output); |
| 771 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 772 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 773 | $size_removed = $size_before - strlen($output); |
| 774 | $savings_percent = round(($size_removed / $size_before * 100)); |
Thor (atiredmachine) | 5c078ce | 2012-01-26 17:18:35 -0800 | [diff] [blame] | 775 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 776 | 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] | 777 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 778 | break; |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 779 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 780 | case 'text/css': |
| 781 | case 'text/javascript': |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 782 | |
Andrew Podner | 9dfceda | 2012-12-18 19:37:22 -0500 | [diff] [blame] | 783 | $output = $this->_minify_script_style($output); |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 784 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 785 | break; |
Thor (atiredmachine) | 5c078ce | 2012-01-26 17:18:35 -0800 | [diff] [blame] | 786 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 787 | default: break; |
| 788 | } |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 789 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 790 | return $output; |
| 791 | } |
Thor (atiredmachine) | 5c078ce | 2012-01-26 17:18:35 -0800 | [diff] [blame] | 792 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 793 | // -------------------------------------------------------------------- |
Andrey Andreev | 0140ddd | 2012-06-16 01:12:56 +0300 | [diff] [blame] | 794 | |
Andrew Podner | 369b314 | 2012-12-17 22:32:02 -0500 | [diff] [blame] | 795 | /** |
| 796 | * Minify Style and Script |
| 797 | * |
| 798 | * Reduce excessive size of CSS/JavaScript content. To remove spaces this |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 799 | * script walks the string as an array and determines if the pointer is inside |
| 800 | * a string created by single quotes or double quotes. spaces inside those |
| 801 | * strings are not stripped. Opening and closing tags are severed from |
| 802 | * the string initially and saved without stripping whitespace to preserve |
| 803 | * the tags and any associated properties if tags are present |
Andrew Podner | 369b314 | 2012-12-17 22:32:02 -0500 | [diff] [blame] | 804 | * |
Andrew Podner | 79b7027 | 2012-12-25 12:04:01 -0500 | [diff] [blame] | 805 | * Minification logic/workflow is similar to methods used by Douglas Crockford |
| 806 | * in JSMIN. http://www.crockford.com/javascript/jsmin.html |
| 807 | * |
| 808 | * KNOWN ISSUE: ending a line with a closing parenthesis ')' and no semicolon |
| 809 | * where there should be one will break the Javascript. New lines after a |
| 810 | * closing parenthesis are not recognized by the script. For best results |
| 811 | * be sure to terminate lines with a semicolon when appropriate. |
| 812 | * |
| 813 | * |
| 814 | * @param string $output Output to minify |
Andrey Andreev | 72ed4c3 | 2012-12-19 17:07:54 +0200 | [diff] [blame] | 815 | * @param bool $has_tags Specify if the output has style or script tags |
Andrew Podner | 369b314 | 2012-12-17 22:32:02 -0500 | [diff] [blame] | 816 | * @return string Minified output |
| 817 | */ |
Andrew Podner | 9dfceda | 2012-12-18 19:37:22 -0500 | [diff] [blame] | 818 | protected function _minify_script_style($output, $has_tags = FALSE) |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 819 | { |
| 820 | // We only need this if there are tags in the file |
Andrew Podner | 9dfceda | 2012-12-18 19:37:22 -0500 | [diff] [blame] | 821 | if ($has_tags === TRUE) |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 822 | { |
| 823 | // Remove opening tag and save for later |
Andrew Podner | 79b7027 | 2012-12-25 12:04:01 -0500 | [diff] [blame] | 824 | $pos = strpos($output, '>') + 1; |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 825 | $open_tag = substr($output, 0, $pos); |
| 826 | $output = substr_replace($output, '', 0, $pos); |
Andrew Podner | 369b314 | 2012-12-17 22:32:02 -0500 | [diff] [blame] | 827 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 828 | // Remove closing tag and save it for later |
| 829 | $end_pos = strlen($output); |
| 830 | $pos = strpos($output, '</'); |
| 831 | $closing_tag = substr($output, $pos, $end_pos); |
| 832 | $output = substr_replace($output, '', $pos); |
| 833 | } |
Andrew Podner | 369b314 | 2012-12-17 22:32:02 -0500 | [diff] [blame] | 834 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 835 | // Remove CSS comments |
Andrey Andreev | 72ed4c3 | 2012-12-19 17:07:54 +0200 | [diff] [blame] | 836 | $output = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!i', '', $output); |
Andrew Podner | 369b314 | 2012-12-17 22:32:02 -0500 | [diff] [blame] | 837 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 838 | // Remove spaces around curly brackets, colons, |
| 839 | // semi-colons, parenthesis, commas |
Andrey Andreev | 72ed4c3 | 2012-12-19 17:07:54 +0200 | [diff] [blame] | 840 | $output = preg_replace('!\s*(:|;|,|}|{|\(|\))\s*!i', '$1', $output); |
Andrew Podner | 369b314 | 2012-12-17 22:32:02 -0500 | [diff] [blame] | 841 | |
Andrew Podner | 79b7027 | 2012-12-25 12:04:01 -0500 | [diff] [blame] | 842 | // Replace tabs with spaces |
Andrew Podner | 464a7b7 | 2012-12-25 21:43:55 -0500 | [diff] [blame] | 843 | // Replace carriage returns & multiple new lines with single new line |
Andrew Podner | 79b7027 | 2012-12-25 12:04:01 -0500 | [diff] [blame] | 844 | // and trim any leading or trailing whitespace |
Andrew Podner | 96b9501 | 2012-12-28 08:30:43 -0500 | [diff] [blame^] | 845 | $output = trim(preg_replace(array('/\t+/', '/\r/', '/\n+/'), array(' ', "\n", "\n"), $output)); |
Andrew Podner | 79b7027 | 2012-12-25 12:04:01 -0500 | [diff] [blame] | 846 | |
| 847 | // Remove spaces when safe to do so. |
| 848 | $in_string = $in_dstring = $prev = FALSE; |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 849 | $array_output = str_split($output); |
| 850 | foreach ($array_output as $key => $value) |
| 851 | { |
Andrew Podner | 7747f0a | 2012-12-18 13:13:15 -0500 | [diff] [blame] | 852 | if ($in_string === FALSE && $in_dstring === FALSE) |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 853 | { |
Andrew Podner | 7747f0a | 2012-12-18 13:13:15 -0500 | [diff] [blame] | 854 | if ($value === ' ') |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 855 | { |
Andrew Podner | 79b7027 | 2012-12-25 12:04:01 -0500 | [diff] [blame] | 856 | // Get the next element in the array for comparisons |
| 857 | $next = $array_output[$key + 1]; |
| 858 | |
| 859 | // Strip spaces preceded/followed by a non-ASCII character |
| 860 | // or not preceded/followed by an alphanumeric |
| 861 | // or not preceded/followed \ $ and _ |
Andrew Podner | ae8c2d9 | 2012-12-26 08:05:44 -0500 | [diff] [blame] | 862 | if ((preg_match('/^[\x20-\x7f]*$/D', $next) OR preg_match('/^[\x20-\x7f]*$/D', $prev)) |
| 863 | && ( ! ctype_alnum($next) OR ! ctype_alnum($prev)) |
Andrew Podner | 464a7b7 | 2012-12-25 21:43:55 -0500 | [diff] [blame] | 864 | && ( ! in_array($next, array('\\', '_', '$')) && ! in_array($prev, array('\\', '_', '$')))) |
Andrew Podner | 79b7027 | 2012-12-25 12:04:01 -0500 | [diff] [blame] | 865 | { |
| 866 | unset($array_output[$key]); |
| 867 | } |
| 868 | } |
| 869 | else |
| 870 | { |
| 871 | // Save this value as previous for the next iteration |
| 872 | // if it is not a blank space |
| 873 | $prev = $value; |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 874 | } |
| 875 | } |
Andrew Podner | 369b314 | 2012-12-17 22:32:02 -0500 | [diff] [blame] | 876 | |
Andrew Podner | 7747f0a | 2012-12-18 13:13:15 -0500 | [diff] [blame] | 877 | if ($value === "'") |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 878 | { |
Andrew Podner | 7747f0a | 2012-12-18 13:13:15 -0500 | [diff] [blame] | 879 | $in_string = ! $in_string; |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 880 | } |
Andrey Andreev | 72ed4c3 | 2012-12-19 17:07:54 +0200 | [diff] [blame] | 881 | elseif ($value === '"') |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 882 | { |
Andrew Podner | 7747f0a | 2012-12-18 13:13:15 -0500 | [diff] [blame] | 883 | $in_dstring = ! $in_dstring; |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 884 | } |
| 885 | } |
Andrew Podner | 369b314 | 2012-12-17 22:32:02 -0500 | [diff] [blame] | 886 | |
Andrew Podner | 79b7027 | 2012-12-25 12:04:01 -0500 | [diff] [blame] | 887 | // Put the string back together after spaces have been stripped |
| 888 | $output = implode($array_output); |
| 889 | |
| 890 | // Remove new line characters unless previous or next character is |
| 891 | // printable or Non-ASCII |
| 892 | preg_match_all('/[\n]/', $output, $lf, PREG_OFFSET_CAPTURE); |
Andrew Podner | 464a7b7 | 2012-12-25 21:43:55 -0500 | [diff] [blame] | 893 | $removed_lf = 0; |
Andrew Podner | 79b7027 | 2012-12-25 12:04:01 -0500 | [diff] [blame] | 894 | foreach ($lf as $feed_position) |
| 895 | { |
Andrew Podner | 464a7b7 | 2012-12-25 21:43:55 -0500 | [diff] [blame] | 896 | foreach ($feed_position as $position) |
Andrew Podner | 79b7027 | 2012-12-25 12:04:01 -0500 | [diff] [blame] | 897 | { |
Andrew Podner | 464a7b7 | 2012-12-25 21:43:55 -0500 | [diff] [blame] | 898 | $next_char = substr($output, $position[1] - $removed_lf + 1, 1); |
| 899 | $prev_char = substr($output, $position[1] - $removed_lf - 1, 1); |
| 900 | if ( ! ctype_print($next_char) && ! ctype_print($prev_char) |
Andrew Podner | 96b9501 | 2012-12-28 08:30:43 -0500 | [diff] [blame^] | 901 | && ! preg_match('/^[\x20-\x7f]*$/D', $next_char) |
| 902 | && ! preg_match('/^[\x20-\x7f]*$/D', $prev_char)) |
Andrew Podner | 79b7027 | 2012-12-25 12:04:01 -0500 | [diff] [blame] | 903 | { |
Andrew Podner | 96b9501 | 2012-12-28 08:30:43 -0500 | [diff] [blame^] | 904 | $output = substr_replace($output, '', $position[1] - $removed_lf++, 1); |
Andrew Podner | 79b7027 | 2012-12-25 12:04:01 -0500 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | } |
Andrew Podner | 369b314 | 2012-12-17 22:32:02 -0500 | [diff] [blame] | 908 | |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 909 | // Put the opening and closing tags back if applicable |
Andrey Andreev | 72ed4c3 | 2012-12-19 17:07:54 +0200 | [diff] [blame] | 910 | return isset($open_tag) |
| 911 | ? $open_tag.$output.$closing_tag |
| 912 | : $output; |
Andrew Podner | 79503c5 | 2012-12-18 07:47:38 -0500 | [diff] [blame] | 913 | } |
Thor (atiredmachine) | 63678a2 | 2012-01-24 16:56:01 -0800 | [diff] [blame] | 914 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 915 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 916 | |
| 917 | /* End of file Output.php */ |
Andrey Andreev | d8dba5d | 2012-12-17 15:42:01 +0200 | [diff] [blame] | 918 | /* Location: ./system/core/Output.php */ |