PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/mantisbt-1.2.8/core/compress_api.php

#
PHP | 138 lines | 50 code | 15 blank | 73 comment | 20 complexity | 7ffb5c5b929a1165f76cbb1d8f6a37e9 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. # MantisBT - a php based bugtracking system
  3. # MantisBT is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # MantisBT is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Compression API
  17. *
  18. * This API handles the ob_gzhandler in php.
  19. *
  20. * @package CoreAPI
  21. * @subpackage CompressionAPI
  22. * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  23. * @copyright Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  24. * @link http://www.mantisbt.org
  25. */
  26. /**
  27. * Starts the buffering/compression (only if the compression option is ON)
  28. * This variable is used internally. It is not used for configuration
  29. * @global bool $g_compression_started
  30. */
  31. $g_compression_started = false;
  32. /**
  33. * Check if compression handler (ob_gzhandler) should be enabled. Note: this should not be used
  34. * as an indicator of whether output received by a client will be compressed, only whether an
  35. * output handler is used to compress output.
  36. * @return bool
  37. * @access public
  38. */
  39. function compress_handler_is_enabled() {
  40. global $g_compress_html, $g_use_iis;
  41. // indicates compression should be disabled for a page. Note: php.ini may still enable zlib.output_compression.
  42. // it may be possible to turn this off through the use of ini_set within that specific page.
  43. if ( defined( 'COMPRESSION_DISABLED' ) ) {
  44. return false;
  45. }
  46. // Dont use config_get here so only dependency is on consant.inc.php in this module
  47. // We only actively compress html if global configuration compress_html is set.
  48. if ( ON == $g_compress_html ) {
  49. // both compression handlers require zlib module to be loaded
  50. if( !extension_loaded( 'zlib' ) ) {
  51. return false;
  52. }
  53. if ( ini_get( 'zlib.output_compression' ) ) {
  54. /* zlib output compression is already enabled - we can't load the gzip output handler */
  55. return false;
  56. }
  57. // Since php 5.2.10, it's possible to set zlib.output_compression via ini_set.
  58. // This method is preferred over ob_gzhandler
  59. if( php_version_at_least( '5.2.10' ) && ini_get( 'output_handler' ) == '' && function_exists( 'ini_set' ) ) {
  60. ini_set( 'zlib.output_compression', true );
  61. // do it transparently
  62. return false;
  63. }
  64. if ( OFF == $g_use_iis ) {
  65. // disable compression when using IIS because of issue #2953.
  66. // For windows compression, use zlib.output_compression in php.ini or a later version of php
  67. return false;
  68. }
  69. // if php.ini does not already use ob_gzhandler by default, return true.
  70. return ( 'ob_gzhandler' != ini_get( 'output_handler' ) );
  71. }
  72. }
  73. /**
  74. * Start compression handler if required
  75. * @return null
  76. * @access public
  77. */
  78. function compress_start_handler() {
  79. if( compress_handler_is_enabled() ) {
  80. # Before doing anything else, start output buffering so we don't prevent
  81. # headers from being sent if there's a blank line in an included file
  82. ob_start( 'compress_handler' );
  83. } else if ( ini_get_bool( 'zlib.output_compression' ) == true ) {
  84. if( defined( 'COMPRESSION_DISABLED' ) ) {
  85. return;
  86. }
  87. ob_start();
  88. }
  89. }
  90. /**
  91. * Output Buffering handler that either compresses the buffer or just.
  92. * returns it, depending on the return value of compress_handler_is_enabled()
  93. * @param string $p_buffer
  94. * @param int $p_mode
  95. * @return string
  96. * @access public
  97. */
  98. function compress_handler( & $p_buffer, $p_mode ) {
  99. global $g_compression_started;
  100. if( $g_compression_started && compress_handler_is_enabled() ) {
  101. return ob_gzhandler( $p_buffer, $p_mode );
  102. } else {
  103. return $p_buffer;
  104. }
  105. }
  106. /**
  107. * Enable output buffering with compression.
  108. * @return null
  109. * @access public
  110. */
  111. function compress_enable() {
  112. global $g_compression_started;
  113. $g_compression_started = true;
  114. }
  115. /**
  116. * Disable output buffering with compression.
  117. * @return null
  118. * @access public
  119. */
  120. function compress_disable() {
  121. global $g_compression_started;
  122. $g_compression_started = false;
  123. }