PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/SeekExtJSBook/include/function.gzip.php

http://seekstudio.googlecode.com/
PHP | 93 lines | 72 code | 21 blank | 0 comment | 15 complexity | 4841047c6a13bcd552545db5b3a4b998 MD5 | raw file
  1. <?php
  2. function gzip_ob_start($val=null) {
  3. global $gz_kind;
  4. if (headers_sent()) {
  5. return false;
  6. }
  7. if (!isset($gz_kind)) {
  8. if (ob_start($val)){
  9. $gz_kind = true;
  10. } else {
  11. $gz_kind = false;
  12. }
  13. }
  14. return $gz_kind;
  15. }
  16. function chk_gzip() {
  17. if (!gzip_ob_start()) {
  18. return false;
  19. } elseif (strpos(' '.$_SERVER["HTTP_ACCEPT_ENCODING"], 'gzip') !== false && function_exists('gzcompress')) {
  20. if (strpos(' '.$_SERVER["HTTP_ACCEPT_ENCODING"], 'x-gzip') !== false) {
  21. return "x-gzip";
  22. } else {
  23. return "gzip";
  24. }
  25. } else {
  26. return false;
  27. }
  28. }
  29. function do_gzip($level=3, $debug=1) {
  30. global $db_debug;
  31. $ENCODING = chk_gzip();
  32. if ( $ENCODING) {
  33. $contents = ob_get_contents();
  34. $debug_contents = '';
  35. ob_end_clean();
  36. if ( $debug) {
  37. $debug_contents .= " Not Compress Length: ".sizeTranslate(strlen($contents)).", ";
  38. $debug_contents .= " Compressed Length: ".sizeTranslate(strlen(gzcompress($contents,$level))).", ";
  39. $debug_contents .= " Compressed Level: $level";
  40. }else{
  41. $contents .= "\n<!-- Use compress $ENCODING($level) -->\n";
  42. }
  43. $contents = str_replace('<gz_debug></gz_debug>',$debug_contents,$contents);
  44. header("Content-Encoding: $ENCODING");
  45. echo pack('cccccccc', 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00);
  46. $size = strlen($contents);
  47. $crc = crc32($contents);
  48. $contents = gzcompress($contents, $level);
  49. $contents = substr($contents, 0, strlen($contents) - 4);
  50. echo $contents;
  51. echo pack('V',$crc);
  52. echo pack('V',$size);
  53. exit;
  54. } else {
  55. echo "\n<!-- Use compress false -->\n";
  56. ob_end_flush();
  57. exit();
  58. }
  59. }
  60. function output() {
  61. global $gz_type, $gz_level, $gz_debug, $debug, $tpl, $db, $version;
  62. if ($debug == 1) {
  63. $gz_contents = ob_get_contents();
  64. ob_end_clean();
  65. include_once $tpl->display('debugpage.tpl');
  66. }else{
  67. if ($gz_type) {
  68. do_gzip($gz_level, $gz_debug);
  69. }
  70. }
  71. }
  72. ?>