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