/src/lib/handlers/output_handler.php
PHP | 73 lines | 39 code | 12 blank | 22 comment | 9 complexity | dee8b280c9bacf0e37df9c991ef51346 MD5 | raw file
Possible License(s): MIT, CC0-1.0
- <?php
- /**
- * Output handler
- * Output handler with compression features.
- * ---
- * Written by Jose Carlos Nieto <xiam@menteslibres.org>
- * Copyright (c) 2007 Astrata Software S.A. de C.V.
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice.
- *
- * @author Jose Carlos Nieto <xiam@menteslibres.org>
- * @copyright Copyright (c) 2007-2008, Astrata Software S.A. de C.V.
- * @link http://opensource.astrata.com.mx Astrata Open Source Projects
- * @version $Revision: $
- * @modifiedby $LastChangedBy: $
- * @lastmodified $Date: $
- * @license http://www.opensource.org/licenses/mit-license.php The MIT License
- *
- */
- class output_handler extends tm_object {
-
- public $html_compression = false;
-
- private $gzoutput = false;
- public function __construct(&$params = null) {
- parent::__construct($params);
- if (TM_GZIP_COMPRESSION && extension_loaded('zlib') && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && ereg('(gzip)', $_SERVER['HTTP_ACCEPT_ENCODING'])) {
- $this->gzoutput = true;
- ob_start('ob_gzhandler');
- } else {
- ob_start();
- }
- $this->html_compression = TM_COMPACT_SOURCE;
- }
- public function __destruct() {
- $buff = ob_get_clean();
- if ($this->html_compression && !defined('TM_AJAX') && !defined('TM_NO_DEBUG')) {
- $buff = preg_replace('/>[\s\r\n\t\f]+/m', '> ', $buff);
- $buff = preg_replace('/[\s\r\n\t\f]+</m', ' <', $buff);
- // TODO skip comments inside scripts, like adsense stuff
- //$buff = preg_replace('/<!--.*?-->/ms', '', $buff);
- $buff .= "\n<!-- Proudly powered by Textmotion.ORG (compression enabled) -->";
- }
- if ($this->gzoutput) {
- $size = strlen($buff);
- header('Content-Encoding: gzip');
- header('Vary: Accept-Encoding');
-
- $crc = crc32($buff);
- $buff = "\x1f\x8b\x08\x00\x00\x00\x00\x00".substr(gzcompress($buff, 9), 0, -4);
- $buff .= pack('V', $crc);
- $buff .= pack('V', $size);
- }
- header('Content-Length: '.strlen($buff).'');
- echo $buff;
- }
- public function run_test() {
- $output = new output_handler();
- echo str_repeat("A", 500);
- }
- }
- //output_handler::run_test();
- ?>