PageRenderTime 55ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lib/handlers/output_handler.php

http://textmotion.googlecode.com/
PHP | 73 lines | 39 code | 12 blank | 22 comment | 9 complexity | dee8b280c9bacf0e37df9c991ef51346 MD5 | raw file
Possible License(s): MIT, CC0-1.0
  1. <?php
  2. /**
  3. * Output handler
  4. * Output handler with compression features.
  5. * ---
  6. * Written by Jose Carlos Nieto <xiam@menteslibres.org>
  7. * Copyright (c) 2007 Astrata Software S.A. de C.V.
  8. *
  9. * Licensed under The MIT License
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @author Jose Carlos Nieto <xiam@menteslibres.org>
  13. * @copyright Copyright (c) 2007-2008, Astrata Software S.A. de C.V.
  14. * @link http://opensource.astrata.com.mx Astrata Open Source Projects
  15. * @version $Revision: $
  16. * @modifiedby $LastChangedBy: $
  17. * @lastmodified $Date: $
  18. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  19. *
  20. */
  21. class output_handler extends tm_object {
  22. public $html_compression = false;
  23. private $gzoutput = false;
  24. public function __construct(&$params = null) {
  25. parent::__construct($params);
  26. if (TM_GZIP_COMPRESSION && extension_loaded('zlib') && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && ereg('(gzip)', $_SERVER['HTTP_ACCEPT_ENCODING'])) {
  27. $this->gzoutput = true;
  28. ob_start('ob_gzhandler');
  29. } else {
  30. ob_start();
  31. }
  32. $this->html_compression = TM_COMPACT_SOURCE;
  33. }
  34. public function __destruct() {
  35. $buff = ob_get_clean();
  36. if ($this->html_compression && !defined('TM_AJAX') && !defined('TM_NO_DEBUG')) {
  37. $buff = preg_replace('/>[\s\r\n\t\f]+/m', '> ', $buff);
  38. $buff = preg_replace('/[\s\r\n\t\f]+</m', ' <', $buff);
  39. // TODO skip comments inside scripts, like adsense stuff
  40. //$buff = preg_replace('/<!--.*?-->/ms', '', $buff);
  41. $buff .= "\n<!-- Proudly powered by Textmotion.ORG (compression enabled) -->";
  42. }
  43. if ($this->gzoutput) {
  44. $size = strlen($buff);
  45. header('Content-Encoding: gzip');
  46. header('Vary: Accept-Encoding');
  47. $crc = crc32($buff);
  48. $buff = "\x1f\x8b\x08\x00\x00\x00\x00\x00".substr(gzcompress($buff, 9), 0, -4);
  49. $buff .= pack('V', $crc);
  50. $buff .= pack('V', $size);
  51. }
  52. header('Content-Length: '.strlen($buff).'');
  53. echo $buff;
  54. }
  55. public function run_test() {
  56. $output = new output_handler();
  57. echo str_repeat("A", 500);
  58. }
  59. }
  60. //output_handler::run_test();
  61. ?>