Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 1 | <?php |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 5.2.4 or newer |
| 6 | * |
| 7 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
| 19 | * @package CodeIgniter |
| 20 | * @author EllisLab Dev Team |
| 21 | * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/) |
| 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
| 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
| 28 | |
| 29 | /** |
| 30 | * Output Class |
| 31 | * |
| 32 | * Responsible for sending final output to the browser. |
| 33 | * |
| 34 | * @package CodeIgniter |
| 35 | * @subpackage Libraries |
| 36 | * @category Output |
| 37 | * @author EllisLab Dev Team |
| 38 | * @link http://codeigniter.com/user_guide/libraries/output.html |
| 39 | */ |
| 40 | class CI_Output { |
| 41 | |
| 42 | /** |
| 43 | * Final output string |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | public $final_output; |
| 48 | |
| 49 | /** |
| 50 | * Cache expiration time |
| 51 | * |
| 52 | * @var int |
| 53 | */ |
| 54 | public $cache_expiration = 0; |
| 55 | |
| 56 | /** |
| 57 | * List of server headers |
| 58 | * |
| 59 | * @var array |
| 60 | */ |
Andrey Andreev | 155ee72 | 2014-01-10 15:50:54 +0200 | [diff] [blame] | 61 | public $headers = array(); |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * List of mime types |
| 65 | * |
| 66 | * @var array |
| 67 | */ |
Andrey Andreev | 155ee72 | 2014-01-10 15:50:54 +0200 | [diff] [blame] | 68 | public $mimes = array(); |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Mime-type for the current page |
| 72 | * |
| 73 | * @var string |
| 74 | */ |
Andrey Andreev | 155ee72 | 2014-01-10 15:50:54 +0200 | [diff] [blame] | 75 | protected $mime_type = 'text/html'; |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Enable Profiler flag |
| 79 | * |
| 80 | * @var bool |
| 81 | */ |
| 82 | public $enable_profiler = FALSE; |
| 83 | |
| 84 | /** |
Andrey Andreev | 155ee72 | 2014-01-10 15:50:54 +0200 | [diff] [blame] | 85 | * php.ini zlib.output_compression flag |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 86 | * |
| 87 | * @var bool |
| 88 | */ |
Andrey Andreev | 155ee72 | 2014-01-10 15:50:54 +0200 | [diff] [blame] | 89 | protected $_zlib_oc = FALSE; |
| 90 | |
| 91 | /** |
| 92 | * CI output compression flag |
| 93 | * |
| 94 | * @var bool |
| 95 | */ |
| 96 | protected $_compress_output = FALSE; |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * List of profiler sections |
| 100 | * |
| 101 | * @var array |
| 102 | */ |
Eric Roberts | 3e6b582 | 2013-01-17 18:30:25 -0600 | [diff] [blame] | 103 | protected $_profiler_sections = array(); |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Parse markers flag |
| 107 | * |
| 108 | * Whether or not to parse variables like {elapsed_time} and {memory_usage}. |
| 109 | * |
| 110 | * @var bool |
| 111 | */ |
Andrey Andreev | 155ee72 | 2014-01-10 15:50:54 +0200 | [diff] [blame] | 112 | public $parse_exec_vars = TRUE; |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * Class constructor |
| 116 | * |
| 117 | * Determines whether zLib output compression will be used. |
| 118 | * |
| 119 | * @return void |
| 120 | */ |
| 121 | public function __construct() |
| 122 | { |
| 123 | $this->_zlib_oc = (bool) @ini_get('zlib.output_compression'); |
Andrey Andreev | 155ee72 | 2014-01-10 15:50:54 +0200 | [diff] [blame] | 124 | $this->_compress_output = ( |
| 125 | $this->_zlib_oc === FALSE |
Andrey Andreev | 9916bfc | 2014-01-10 16:21:07 +0200 | [diff] [blame^] | 126 | && config_item('compress_output') === TRUE |
Andrey Andreev | 155ee72 | 2014-01-10 15:50:54 +0200 | [diff] [blame] | 127 | && extension_loaded('zlib') |
| 128 | ); |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 129 | |
| 130 | // Get mime types for later |
| 131 | $this->mimes =& get_mimes(); |
| 132 | |
| 133 | log_message('debug', 'Output Class Initialized'); |
| 134 | } |
| 135 | |
| 136 | // -------------------------------------------------------------------- |
| 137 | |
| 138 | /** |
| 139 | * Get Output |
| 140 | * |
| 141 | * Returns the current output string. |
| 142 | * |
| 143 | * @return string |
| 144 | */ |
| 145 | public function get_output() |
| 146 | { |
| 147 | return $this->final_output; |
| 148 | } |
| 149 | |
| 150 | // -------------------------------------------------------------------- |
| 151 | |
| 152 | /** |
| 153 | * Set Output |
| 154 | * |
| 155 | * Sets the output string. |
| 156 | * |
| 157 | * @param string $output Output data |
| 158 | * @return CI_Output |
| 159 | */ |
| 160 | public function set_output($output) |
| 161 | { |
| 162 | $this->final_output = $output; |
| 163 | return $this; |
| 164 | } |
| 165 | |
| 166 | // -------------------------------------------------------------------- |
| 167 | |
| 168 | /** |
| 169 | * Append Output |
| 170 | * |
| 171 | * Appends data onto the output string. |
| 172 | * |
| 173 | * @param string $output Data to append |
| 174 | * @return CI_Output |
| 175 | */ |
| 176 | public function append_output($output) |
| 177 | { |
| 178 | if (empty($this->final_output)) |
| 179 | { |
| 180 | $this->final_output = $output; |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | $this->final_output .= $output; |
| 185 | } |
| 186 | |
| 187 | return $this; |
| 188 | } |
| 189 | |
| 190 | // -------------------------------------------------------------------- |
| 191 | |
| 192 | /** |
| 193 | * Set Header |
| 194 | * |
| 195 | * Lets you set a server header which will be sent with the final output. |
| 196 | * |
| 197 | * Note: If a file is cached, headers will not be sent. |
| 198 | * @todo We need to figure out how to permit headers to be cached. |
| 199 | * |
| 200 | * @param string $header Header |
| 201 | * @param bool $replace Whether to replace the old header value, if already set |
| 202 | * @return CI_Output |
| 203 | */ |
| 204 | public function set_header($header, $replace = TRUE) |
| 205 | { |
| 206 | // If zlib.output_compression is enabled it will compress the output, |
| 207 | // but it will not modify the content-length header to compensate for |
| 208 | // the reduction, causing the browser to hang waiting for more data. |
| 209 | // We'll just skip content-length in those cases. |
| 210 | if ($this->_zlib_oc && strncasecmp($header, 'content-length', 14) === 0) |
| 211 | { |
| 212 | return $this; |
| 213 | } |
| 214 | |
| 215 | $this->headers[] = array($header, $replace); |
| 216 | return $this; |
| 217 | } |
| 218 | |
| 219 | // -------------------------------------------------------------------- |
| 220 | |
| 221 | /** |
| 222 | * Set Content-Type Header |
| 223 | * |
| 224 | * @param string $mime_type Extension of the file we're outputting |
| 225 | * @param string $charset Character set (default: NULL) |
| 226 | * @return CI_Output |
| 227 | */ |
| 228 | public function set_content_type($mime_type, $charset = NULL) |
| 229 | { |
| 230 | if (strpos($mime_type, '/') === FALSE) |
| 231 | { |
| 232 | $extension = ltrim($mime_type, '.'); |
| 233 | |
| 234 | // Is this extension supported? |
| 235 | if (isset($this->mimes[$extension])) |
| 236 | { |
| 237 | $mime_type =& $this->mimes[$extension]; |
| 238 | |
| 239 | if (is_array($mime_type)) |
| 240 | { |
| 241 | $mime_type = current($mime_type); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | $this->mime_type = $mime_type; |
| 247 | |
| 248 | if (empty($charset)) |
| 249 | { |
| 250 | $charset = config_item('charset'); |
| 251 | } |
| 252 | |
| 253 | $header = 'Content-Type: '.$mime_type |
| 254 | .(empty($charset) ? NULL : '; charset='.$charset); |
| 255 | |
| 256 | $this->headers[] = array($header, TRUE); |
| 257 | return $this; |
| 258 | } |
| 259 | |
| 260 | // -------------------------------------------------------------------- |
| 261 | |
| 262 | /** |
| 263 | * Get Current Content-Type Header |
| 264 | * |
| 265 | * @return string 'text/html', if not already set |
| 266 | */ |
| 267 | public function get_content_type() |
| 268 | { |
| 269 | for ($i = 0, $c = count($this->headers); $i < $c; $i++) |
| 270 | { |
| 271 | if (sscanf($this->headers[$i][0], 'Content-Type: %[^;]', $content_type) === 1) |
| 272 | { |
| 273 | return $content_type; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return 'text/html'; |
| 278 | } |
| 279 | |
| 280 | // -------------------------------------------------------------------- |
| 281 | |
| 282 | /** |
| 283 | * Get Header |
| 284 | * |
| 285 | * @param string $header_name |
| 286 | * @return string |
| 287 | */ |
| 288 | public function get_header($header) |
| 289 | { |
| 290 | // Combine headers already sent with our batched headers |
| 291 | $headers = array_merge( |
| 292 | // We only need [x][0] from our multi-dimensional array |
| 293 | array_map('array_shift', $this->headers), |
| 294 | headers_list() |
| 295 | ); |
| 296 | |
| 297 | if (empty($headers) OR empty($header)) |
| 298 | { |
| 299 | return NULL; |
| 300 | } |
| 301 | |
| 302 | for ($i = 0, $c = count($headers); $i < $c; $i++) |
| 303 | { |
| 304 | if (strncasecmp($header, $headers[$i], $l = strlen($header)) === 0) |
| 305 | { |
| 306 | return trim(substr($headers[$i], $l+1)); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return NULL; |
| 311 | } |
| 312 | |
| 313 | // -------------------------------------------------------------------- |
| 314 | |
| 315 | /** |
| 316 | * Set HTTP Status Header |
| 317 | * |
| 318 | * As of version 1.7.2, this is an alias for common function |
| 319 | * set_status_header(). |
| 320 | * |
| 321 | * @param int $code Status code (default: 200) |
| 322 | * @param string $text Optional message |
| 323 | * @return CI_Output |
| 324 | */ |
| 325 | public function set_status_header($code = 200, $text = '') |
| 326 | { |
| 327 | set_status_header($code, $text); |
| 328 | return $this; |
| 329 | } |
| 330 | |
| 331 | // -------------------------------------------------------------------- |
| 332 | |
| 333 | /** |
| 334 | * Enable/disable Profiler |
| 335 | * |
| 336 | * @param bool $val TRUE to enable or FALSE to disable |
| 337 | * @return CI_Output |
| 338 | */ |
| 339 | public function enable_profiler($val = TRUE) |
| 340 | { |
| 341 | $this->enable_profiler = is_bool($val) ? $val : TRUE; |
| 342 | return $this; |
| 343 | } |
| 344 | |
| 345 | // -------------------------------------------------------------------- |
| 346 | |
| 347 | /** |
| 348 | * Set Profiler Sections |
| 349 | * |
| 350 | * Allows override of default/config settings for |
| 351 | * Profiler section display. |
| 352 | * |
| 353 | * @param array $sections Profiler sections |
| 354 | * @return CI_Output |
| 355 | */ |
| 356 | public function set_profiler_sections($sections) |
| 357 | { |
| 358 | if (isset($sections['query_toggle_count'])) |
| 359 | { |
| 360 | $this->_profiler_sections['query_toggle_count'] = (int) $sections['query_toggle_count']; |
| 361 | unset($sections['query_toggle_count']); |
| 362 | } |
| 363 | |
| 364 | foreach ($sections as $section => $enable) |
| 365 | { |
| 366 | $this->_profiler_sections[$section] = ($enable !== FALSE); |
| 367 | } |
| 368 | |
| 369 | return $this; |
| 370 | } |
| 371 | |
| 372 | // -------------------------------------------------------------------- |
| 373 | |
| 374 | /** |
| 375 | * Set Cache |
| 376 | * |
| 377 | * @param int $time Cache expiration time in seconds |
| 378 | * @return CI_Output |
| 379 | */ |
| 380 | public function cache($time) |
| 381 | { |
| 382 | $this->cache_expiration = is_numeric($time) ? $time : 0; |
| 383 | return $this; |
| 384 | } |
| 385 | |
| 386 | // -------------------------------------------------------------------- |
| 387 | |
| 388 | /** |
| 389 | * Display Output |
| 390 | * |
| 391 | * Processes sends the sends finalized output data to the browser along |
| 392 | * with any server headers and profile data. It also stops benchmark |
| 393 | * timers so the page rendering speed and memory usage can be shown. |
| 394 | * |
| 395 | * Note: All "view" data is automatically put into $this->final_output |
| 396 | * by controller class. |
| 397 | * |
| 398 | * @uses CI_Output::$final_output |
| 399 | * @param string $output Output data override |
| 400 | * @return void |
| 401 | */ |
| 402 | public function _display($output = '') |
| 403 | { |
| 404 | // Note: We use globals because we can't use $CI =& get_instance() |
| 405 | // since this function is sometimes called by the caching mechanism, |
| 406 | // which happens before the CI super object is available. |
| 407 | global $BM, $CFG; |
| 408 | |
| 409 | // Grab the super object if we can. |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 410 | if (class_exists('CI_Controller', FALSE)) |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 411 | { |
| 412 | $CI =& get_instance(); |
| 413 | } |
| 414 | |
| 415 | // -------------------------------------------------------------------- |
| 416 | |
| 417 | // Set the output data |
| 418 | if ($output === '') |
| 419 | { |
| 420 | $output =& $this->final_output; |
| 421 | } |
| 422 | |
| 423 | // -------------------------------------------------------------------- |
| 424 | |
| 425 | // Is minify requested? |
| 426 | if ($CFG->item('minify_output') === TRUE) |
| 427 | { |
| 428 | $output = $this->minify($output, $this->mime_type); |
| 429 | } |
| 430 | |
| 431 | // -------------------------------------------------------------------- |
| 432 | |
| 433 | // Do we need to write a cache file? Only if the controller does not have its |
| 434 | // own _output() method and we are not dealing with a cache file, which we |
| 435 | // can determine by the existence of the $CI object above |
| 436 | if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output')) |
| 437 | { |
| 438 | $this->_write_cache($output); |
| 439 | } |
| 440 | |
| 441 | // -------------------------------------------------------------------- |
| 442 | |
| 443 | // Parse out the elapsed time and memory usage, |
| 444 | // then swap the pseudo-variables with the data |
| 445 | |
| 446 | $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end'); |
| 447 | |
| 448 | if ($this->parse_exec_vars === TRUE) |
| 449 | { |
| 450 | $memory = round(memory_get_usage() / 1024 / 1024, 2).'MB'; |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 451 | $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output); |
| 452 | } |
| 453 | |
| 454 | // -------------------------------------------------------------------- |
| 455 | |
| 456 | // Is compression requested? |
Andrey Andreev | 155ee72 | 2014-01-10 15:50:54 +0200 | [diff] [blame] | 457 | if (isset($CI) // This means that we're not serving a cache file, if we were, it would already be compressed |
| 458 | && $this->_compress_output === TRUE |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 459 | && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) |
| 460 | { |
| 461 | ob_start('ob_gzhandler'); |
| 462 | } |
| 463 | |
| 464 | // -------------------------------------------------------------------- |
| 465 | |
| 466 | // Are there any server headers to send? |
| 467 | if (count($this->headers) > 0) |
| 468 | { |
| 469 | foreach ($this->headers as $header) |
| 470 | { |
| 471 | @header($header[0], $header[1]); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | // -------------------------------------------------------------------- |
| 476 | |
| 477 | // Does the $CI object exist? |
| 478 | // If not we know we are dealing with a cache file so we'll |
| 479 | // simply echo out the data and exit. |
| 480 | if ( ! isset($CI)) |
| 481 | { |
Andrey Andreev | 155ee72 | 2014-01-10 15:50:54 +0200 | [diff] [blame] | 482 | if ($this->_compress_output === TRUE) |
| 483 | { |
| 484 | if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) |
| 485 | { |
| 486 | header('Content-Encoding: gzip'); |
| 487 | header('Content-Length: '.strlen($output)); |
| 488 | } |
| 489 | else |
| 490 | { |
| 491 | // User agent doesn't support gzip compression, |
| 492 | // so we'll have to decompress our cache |
| 493 | $output = gzinflate(substr($output, 10, -8)); |
| 494 | } |
| 495 | } |
| 496 | |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 497 | echo $output; |
| 498 | log_message('debug', 'Final output sent to browser'); |
| 499 | log_message('debug', 'Total execution time: '.$elapsed); |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | // -------------------------------------------------------------------- |
| 504 | |
| 505 | // Do we need to generate profile data? |
| 506 | // If so, load the Profile class and run it. |
| 507 | if ($this->enable_profiler === TRUE) |
| 508 | { |
| 509 | $CI->load->library('profiler'); |
| 510 | if ( ! empty($this->_profiler_sections)) |
| 511 | { |
| 512 | $CI->profiler->set_sections($this->_profiler_sections); |
| 513 | } |
| 514 | |
| 515 | // If the output data contains closing </body> and </html> tags |
| 516 | // we will remove them and add them back after we insert the profile data |
| 517 | $output = preg_replace('|</body>.*?</html>|is', '', $output, -1, $count).$CI->profiler->run(); |
| 518 | if ($count > 0) |
| 519 | { |
| 520 | $output .= '</body></html>'; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // Does the controller contain a function named _output()? |
| 525 | // If so send the output there. Otherwise, echo it. |
| 526 | if (method_exists($CI, '_output')) |
| 527 | { |
| 528 | $CI->_output($output); |
| 529 | } |
| 530 | else |
| 531 | { |
| 532 | echo $output; // Send it to the browser! |
| 533 | } |
| 534 | |
| 535 | log_message('debug', 'Final output sent to browser'); |
| 536 | log_message('debug', 'Total execution time: '.$elapsed); |
| 537 | } |
| 538 | |
| 539 | // -------------------------------------------------------------------- |
| 540 | |
| 541 | /** |
| 542 | * Write Cache |
| 543 | * |
| 544 | * @param string $output Output data to cache |
| 545 | * @return void |
| 546 | */ |
| 547 | public function _write_cache($output) |
| 548 | { |
| 549 | $CI =& get_instance(); |
| 550 | $path = $CI->config->item('cache_path'); |
| 551 | $cache_path = ($path === '') ? APPPATH.'cache/' : $path; |
| 552 | |
| 553 | if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path)) |
| 554 | { |
| 555 | log_message('error', 'Unable to write cache file: '.$cache_path); |
| 556 | return; |
| 557 | } |
| 558 | |
Andrey Andreev | 155ee72 | 2014-01-10 15:50:54 +0200 | [diff] [blame] | 559 | $uri = $CI->config->item('base_url') |
| 560 | .$CI->config->item('index_page') |
| 561 | .$CI->uri->uri_string(); |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 562 | |
| 563 | $cache_path .= md5($uri); |
| 564 | |
| 565 | if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE)) |
| 566 | { |
| 567 | log_message('error', 'Unable to write cache file: '.$cache_path); |
| 568 | return; |
| 569 | } |
| 570 | |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 571 | if (flock($fp, LOCK_EX)) |
| 572 | { |
Andrey Andreev | 155ee72 | 2014-01-10 15:50:54 +0200 | [diff] [blame] | 573 | // If output compression is enabled, compress the cache |
| 574 | // itself, so that we don't have to do that each time |
| 575 | // we're serving it |
| 576 | if ($this->_compress_output === TRUE) |
| 577 | { |
| 578 | $output = gzencode($output); |
| 579 | |
| 580 | if ($this->get_header('content-type') === NULL) |
| 581 | { |
| 582 | $this->set_content_type($this->mime_type); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | $expire = time() + ($this->cache_expiration * 60); |
| 587 | |
| 588 | // Put together our serialized info. |
| 589 | $cache_info = serialize(array( |
| 590 | 'expire' => $expire, |
| 591 | 'headers' => $this->headers |
| 592 | )); |
| 593 | |
Eric Roberts | c90e67e | 2013-01-11 21:20:54 -0600 | [diff] [blame] | 594 | fwrite($fp, $cache_info.'ENDCI--->'.$output); |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 595 | flock($fp, LOCK_UN); |
| 596 | } |
| 597 | else |
| 598 | { |
| 599 | log_message('error', 'Unable to secure a file lock for file at: '.$cache_path); |
| 600 | return; |
| 601 | } |
Andrey Andreev | 155ee72 | 2014-01-10 15:50:54 +0200 | [diff] [blame] | 602 | |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 603 | fclose($fp); |
| 604 | @chmod($cache_path, FILE_WRITE_MODE); |
| 605 | |
| 606 | log_message('debug', 'Cache file written: '.$cache_path); |
| 607 | |
| 608 | // Send HTTP cache-control headers to browser to match file cache settings. |
| 609 | $this->set_cache_header($_SERVER['REQUEST_TIME'], $expire); |
| 610 | } |
| 611 | |
| 612 | // -------------------------------------------------------------------- |
| 613 | |
| 614 | /** |
| 615 | * Update/serve cached output |
| 616 | * |
| 617 | * @uses CI_Config |
| 618 | * @uses CI_URI |
| 619 | * |
| 620 | * @param object &$CFG CI_Config class instance |
| 621 | * @param object &$URI CI_URI class instance |
| 622 | * @return bool TRUE on success or FALSE on failure |
| 623 | */ |
| 624 | public function _display_cache(&$CFG, &$URI) |
| 625 | { |
| 626 | $cache_path = ($CFG->item('cache_path') === '') ? APPPATH.'cache/' : $CFG->item('cache_path'); |
| 627 | |
| 628 | // Build the file path. The file name is an MD5 hash of the full URI |
| 629 | $uri = $CFG->item('base_url').$CFG->item('index_page').$URI->uri_string; |
| 630 | $filepath = $cache_path.md5($uri); |
| 631 | |
| 632 | if ( ! @file_exists($filepath) OR ! $fp = @fopen($filepath, FOPEN_READ)) |
| 633 | { |
| 634 | return FALSE; |
| 635 | } |
| 636 | |
| 637 | flock($fp, LOCK_SH); |
| 638 | |
| 639 | $cache = (filesize($filepath) > 0) ? fread($fp, filesize($filepath)) : ''; |
| 640 | |
| 641 | flock($fp, LOCK_UN); |
| 642 | fclose($fp); |
| 643 | |
Eric Roberts | c90e67e | 2013-01-11 21:20:54 -0600 | [diff] [blame] | 644 | // Look for embedded serialized file info. |
| 645 | if ( ! preg_match('/^(.*)ENDCI--->/', $cache, $match)) |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 646 | { |
| 647 | return FALSE; |
| 648 | } |
Purwandi | 5dc6d51 | 2013-01-19 17:43:08 +0700 | [diff] [blame] | 649 | |
Eric Roberts | c90e67e | 2013-01-11 21:20:54 -0600 | [diff] [blame] | 650 | $cache_info = unserialize($match[1]); |
| 651 | $expire = $cache_info['expire']; |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 652 | |
| 653 | $last_modified = filemtime($cache_path); |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 654 | |
| 655 | // Has the file expired? |
| 656 | if ($_SERVER['REQUEST_TIME'] >= $expire && is_really_writable($cache_path)) |
| 657 | { |
| 658 | // If so we'll delete it. |
| 659 | @unlink($filepath); |
| 660 | log_message('debug', 'Cache file has expired. File deleted.'); |
| 661 | return FALSE; |
| 662 | } |
| 663 | else |
| 664 | { |
| 665 | // Or else send the HTTP cache control headers. |
| 666 | $this->set_cache_header($last_modified, $expire); |
| 667 | } |
Purwandi | 5dc6d51 | 2013-01-19 17:43:08 +0700 | [diff] [blame] | 668 | |
Eric Roberts | c90e67e | 2013-01-11 21:20:54 -0600 | [diff] [blame] | 669 | // Add headers from cache file. |
| 670 | foreach ($cache_info['headers'] as $header) |
| 671 | { |
| 672 | $this->set_header($header[0], $header[1]); |
| 673 | } |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 674 | |
| 675 | // Display the cache |
| 676 | $this->_display(substr($cache, strlen($match[0]))); |
| 677 | log_message('debug', 'Cache file is current. Sending it to browser.'); |
| 678 | return TRUE; |
| 679 | } |
| 680 | |
| 681 | // -------------------------------------------------------------------- |
| 682 | |
| 683 | /** |
| 684 | * Delete cache |
| 685 | * |
| 686 | * @param string $uri URI string |
| 687 | * @return bool |
| 688 | */ |
| 689 | public function delete_cache($uri = '') |
| 690 | { |
| 691 | $CI =& get_instance(); |
| 692 | $cache_path = $CI->config->item('cache_path'); |
| 693 | if ($cache_path === '') |
| 694 | { |
| 695 | $cache_path = APPPATH.'cache/'; |
| 696 | } |
| 697 | |
| 698 | if ( ! is_dir($cache_path)) |
| 699 | { |
| 700 | log_message('error', 'Unable to find cache path: '.$cache_path); |
| 701 | return FALSE; |
| 702 | } |
| 703 | |
| 704 | if (empty($uri)) |
| 705 | { |
| 706 | $uri = $CI->uri->uri_string(); |
| 707 | } |
| 708 | |
| 709 | $cache_path .= md5($CI->config->item('base_url').$CI->config->item('index_page').$uri); |
| 710 | |
| 711 | if ( ! @unlink($cache_path)) |
| 712 | { |
| 713 | log_message('error', 'Unable to delete cache file for '.$uri); |
| 714 | return FALSE; |
| 715 | } |
| 716 | |
| 717 | return TRUE; |
| 718 | } |
| 719 | |
| 720 | // -------------------------------------------------------------------- |
| 721 | |
| 722 | /** |
| 723 | * Set Cache Header |
| 724 | * |
| 725 | * Set the HTTP headers to match the server-side file cache settings |
| 726 | * in order to reduce bandwidth. |
| 727 | * |
| 728 | * @param int $last_modified Timestamp of when the page was last modified |
| 729 | * @param int $expiration Timestamp of when should the requested page expire from cache |
| 730 | * @return void |
| 731 | */ |
| 732 | public function set_cache_header($last_modified, $expiration) |
| 733 | { |
| 734 | $max_age = $expiration - $_SERVER['REQUEST_TIME']; |
| 735 | |
| 736 | if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $last_modified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) |
| 737 | { |
| 738 | $this->set_status_header(304); |
| 739 | exit; |
| 740 | } |
| 741 | else |
| 742 | { |
| 743 | header('Pragma: public'); |
Andrey Andreev | 3ca060a | 2013-11-27 16:30:31 +0200 | [diff] [blame] | 744 | header('Cache-Control: max-age='.$max_age.', public'); |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 745 | header('Expires: '.gmdate('D, d M Y H:i:s', $expiration).' GMT'); |
| 746 | header('Last-modified: '.gmdate('D, d M Y H:i:s', $last_modified).' GMT'); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | // -------------------------------------------------------------------- |
| 751 | |
| 752 | /** |
| 753 | * Minify |
| 754 | * |
| 755 | * Reduce excessive size of HTML/CSS/JavaScript content. |
| 756 | * |
| 757 | * @param string $output Output to minify |
| 758 | * @param string $type Output content MIME type |
| 759 | * @return string Minified output |
| 760 | */ |
| 761 | public function minify($output, $type = 'text/html') |
| 762 | { |
| 763 | switch ($type) |
| 764 | { |
| 765 | case 'text/html': |
| 766 | |
| 767 | if (($size_before = strlen($output)) === 0) |
| 768 | { |
| 769 | return ''; |
| 770 | } |
| 771 | |
| 772 | // Find all the <pre>,<code>,<textarea>, and <javascript> tags |
| 773 | // We'll want to return them to this unprocessed state later. |
| 774 | preg_match_all('{<pre.+</pre>}msU', $output, $pres_clean); |
| 775 | preg_match_all('{<code.+</code>}msU', $output, $codes_clean); |
| 776 | preg_match_all('{<textarea.+</textarea>}msU', $output, $textareas_clean); |
| 777 | preg_match_all('{<script.+</script>}msU', $output, $javascript_clean); |
| 778 | |
| 779 | // Minify the CSS in all the <style> tags. |
| 780 | preg_match_all('{<style.+</style>}msU', $output, $style_clean); |
| 781 | foreach ($style_clean[0] as $s) |
| 782 | { |
Andrey Andreev | 6a42490 | 2013-10-28 14:16:18 +0200 | [diff] [blame] | 783 | $output = str_replace($s, $this->_minify_js_css($s, 'css', TRUE), $output); |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | // Minify the javascript in <script> tags. |
| 787 | foreach ($javascript_clean[0] as $s) |
| 788 | { |
Andrey Andreev | 6a42490 | 2013-10-28 14:16:18 +0200 | [diff] [blame] | 789 | $javascript_mini[] = $this->_minify_js_css($s, 'js', TRUE); |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | // Replace multiple spaces with a single space. |
| 793 | $output = preg_replace('!\s{2,}!', ' ', $output); |
| 794 | |
| 795 | // Remove comments (non-MSIE conditionals) |
Michael Dodge | 4d02e35 | 2013-01-04 23:22:51 -0700 | [diff] [blame] | 796 | $output = preg_replace('{\s*<!--[^\[<>].*(?<!!)-->\s*}msU', '', $output); |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 797 | |
| 798 | // Remove spaces around block-level elements. |
Purwandi | 5dc6d51 | 2013-01-19 17:43:08 +0700 | [diff] [blame] | 799 | $output = preg_replace('/\s*(<\/?(html|head|title|meta|script|link|style|body|table|thead|tbody|tfoot|tr|th|td|h[1-6]|div|p|br)[^>]*>)\s*/is', '$1', $output); |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 800 | |
| 801 | // Replace mangled <pre> etc. tags with unprocessed ones. |
| 802 | |
| 803 | if ( ! empty($pres_clean)) |
| 804 | { |
| 805 | preg_match_all('{<pre.+</pre>}msU', $output, $pres_messed); |
| 806 | $output = str_replace($pres_messed[0], $pres_clean[0], $output); |
| 807 | } |
| 808 | |
| 809 | if ( ! empty($codes_clean)) |
| 810 | { |
| 811 | preg_match_all('{<code.+</code>}msU', $output, $codes_messed); |
| 812 | $output = str_replace($codes_messed[0], $codes_clean[0], $output); |
| 813 | } |
| 814 | |
Andrey Andreev | 3ffce98 | 2013-01-21 15:24:09 +0200 | [diff] [blame] | 815 | if ( ! empty($textareas_clean)) |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 816 | { |
| 817 | preg_match_all('{<textarea.+</textarea>}msU', $output, $textareas_messed); |
| 818 | $output = str_replace($textareas_messed[0], $textareas_clean[0], $output); |
| 819 | } |
| 820 | |
| 821 | if (isset($javascript_mini)) |
| 822 | { |
| 823 | preg_match_all('{<script.+</script>}msU', $output, $javascript_messed); |
| 824 | $output = str_replace($javascript_messed[0], $javascript_mini, $output); |
| 825 | } |
| 826 | |
| 827 | $size_removed = $size_before - strlen($output); |
| 828 | $savings_percent = round(($size_removed / $size_before * 100)); |
| 829 | |
| 830 | log_message('debug', 'Minifier shaved '.($size_removed / 1000).'KB ('.$savings_percent.'%) off final HTML output.'); |
| 831 | |
| 832 | break; |
| 833 | |
| 834 | case 'text/css': |
Andrey Andreev | 6a42490 | 2013-10-28 14:16:18 +0200 | [diff] [blame] | 835 | |
| 836 | return $this->_minify_js_css($output, 'css'); |
| 837 | |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 838 | case 'text/javascript': |
bayssmekanique | 7b90325 | 2013-03-12 13:25:24 -0700 | [diff] [blame] | 839 | case 'application/javascript': |
| 840 | case 'application/x-javascript': |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 841 | |
Andrey Andreev | 6a42490 | 2013-10-28 14:16:18 +0200 | [diff] [blame] | 842 | return $this->_minify_js_css($output, 'js'); |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 843 | |
| 844 | default: break; |
| 845 | } |
| 846 | |
| 847 | return $output; |
| 848 | } |
| 849 | |
| 850 | // -------------------------------------------------------------------- |
| 851 | |
Andrey Andreev | 3c3bbac | 2013-10-31 15:10:49 +0200 | [diff] [blame] | 852 | /** |
| 853 | * Minify JavaScript and CSS code |
| 854 | * |
| 855 | * Strips comments and excessive whitespace characters |
| 856 | * |
| 857 | * @param string $output |
| 858 | * @param string $type 'js' or 'css' |
| 859 | * @param bool $tags Whether $output contains the 'script' or 'style' tag |
| 860 | * @return string |
| 861 | */ |
Andrey Andreev | 6a42490 | 2013-10-28 14:16:18 +0200 | [diff] [blame] | 862 | protected function _minify_js_css($output, $type, $tags = FALSE) |
| 863 | { |
| 864 | if ($tags === TRUE) |
| 865 | { |
| 866 | $tags = array('close' => strrchr($output, '<')); |
| 867 | |
| 868 | $open_length = strpos($output, '>') + 1; |
| 869 | $tags['open'] = substr($output, 0, $open_length); |
| 870 | |
| 871 | $output = substr($output, $open_length, -strlen($tags['close'])); |
| 872 | |
| 873 | // Strip spaces from the tags |
| 874 | $tags = preg_replace('#\s{2,}#', ' ', $tags); |
| 875 | } |
| 876 | |
| 877 | $output = trim($output); |
| 878 | |
| 879 | if ($type === 'js') |
| 880 | { |
| 881 | // Catch all string literals and comment blocks |
Andrey Andreev | 0949b36 | 2013-10-31 16:07:40 +0200 | [diff] [blame] | 882 | if (preg_match_all('#((?:((?<!\\\)\'|")|(/\*)|(//)).*(?(2)(?<!\\\)\2|(?(3)\*/|\n)))#msuUS', $output, $match, PREG_OFFSET_CAPTURE)) |
Andrey Andreev | 6a42490 | 2013-10-28 14:16:18 +0200 | [diff] [blame] | 883 | { |
| 884 | $js_literals = $js_code = array(); |
| 885 | for ($match = $match[0], $c = count($match), $i = $pos = $offset = 0; $i < $c; $i++) |
| 886 | { |
Andrey Andreev | 3c3bbac | 2013-10-31 15:10:49 +0200 | [diff] [blame] | 887 | $js_code[$pos++] = trim(substr($output, $offset, $match[$i][1] - $offset)); |
Andrey Andreev | 6a42490 | 2013-10-28 14:16:18 +0200 | [diff] [blame] | 888 | $offset = $match[$i][1] + strlen($match[$i][0]); |
| 889 | |
| 890 | // Save only if we haven't matched a comment block |
| 891 | if ($match[$i][0][0] !== '/') |
| 892 | { |
| 893 | $js_literals[$pos++] = array_shift($match[$i]); |
| 894 | } |
| 895 | } |
| 896 | $js_code[$pos] = substr($output, $offset); |
| 897 | |
| 898 | // $match might be quite large, so free it up together with other vars that we no longer need |
| 899 | unset($match, $offset, $pos); |
| 900 | } |
| 901 | else |
| 902 | { |
| 903 | $js_code = array($output); |
| 904 | $js_literals = array(); |
| 905 | } |
| 906 | |
| 907 | $varname = 'js_code'; |
| 908 | } |
| 909 | else |
| 910 | { |
| 911 | $varname = 'output'; |
| 912 | } |
| 913 | |
| 914 | // Standartize new lines |
| 915 | $$varname = str_replace(array("\r\n", "\r"), "\n", $$varname); |
| 916 | |
| 917 | if ($type === 'js') |
| 918 | { |
| 919 | $patterns = array( |
Andrey Andreev | 99f9b9a | 2013-10-30 23:07:23 +0200 | [diff] [blame] | 920 | '#\s*([!\#%&()*+,\-./:;<=>?@\[\]^`{|}~])\s*#' => '$1', // Remove spaces following and preceeding JS-wise non-special & non-word characters |
Andrey Andreev | 6a42490 | 2013-10-28 14:16:18 +0200 | [diff] [blame] | 921 | '#\s{2,}#' => ' ' // Reduce the remaining multiple whitespace characters to a single space |
| 922 | ); |
| 923 | } |
| 924 | else |
| 925 | { |
| 926 | $patterns = array( |
| 927 | '#/\*.*(?=\*/)\*/#s' => '', // Remove /* block comments */ |
| 928 | '#\n?//[^\n]*#' => '', // Remove // line comments |
Andrey Andreev | 99f9b9a | 2013-10-30 23:07:23 +0200 | [diff] [blame] | 929 | '#\s*([^\w.\#%])\s*#U' => '$1', // Remove spaces following and preceeding non-word characters, excluding dots, hashes and the percent sign |
Andrey Andreev | 6a42490 | 2013-10-28 14:16:18 +0200 | [diff] [blame] | 930 | '#\s{2,}#' => ' ' // Reduce the remaining multiple space characters to a single space |
| 931 | ); |
| 932 | } |
| 933 | |
| 934 | $$varname = preg_replace(array_keys($patterns), array_values($patterns), $$varname); |
| 935 | |
| 936 | // Glue back JS quoted strings |
| 937 | if ($type === 'js') |
| 938 | { |
| 939 | $js_code += $js_literals; |
| 940 | ksort($js_code); |
| 941 | $output = implode($js_code); |
| 942 | unset($js_code, $js_literals, $varname, $patterns); |
| 943 | } |
| 944 | |
| 945 | return is_array($tags) |
| 946 | ? $tags['open'].$output.$tags['close'] |
| 947 | : $output; |
| 948 | } |
| 949 | |
Michael Dodge | 362b800 | 2013-01-04 23:18:39 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | /* End of file Output.php */ |
Andrey Andreev | d8dba5d | 2012-12-17 15:42:01 +0200 | [diff] [blame] | 953 | /* Location: ./system/core/Output.php */ |