3 * Functions to be used with PHP's output buffer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
30 * Standard output handler for use with ob_start.
32 * @param string $s Web response output
35 public static function handle( $s ) {
36 global $wgDisableOutputCompression, $wgMangleFlashPolicy;
37 if ( $wgMangleFlashPolicy ) {
38 $s = self
::mangleFlashPolicy( $s );
40 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
41 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
42 $s = self
::handleGzip( $s );
44 if ( !ini_get( 'output_handler' ) ) {
45 self
::emitContentLength( strlen( $s ) );
52 * Get the "file extension" that some client apps will estimate from
53 * the currently-requested URL.
55 * This isn't a WebRequest method, because we need it before the class loads.
56 * @todo As of 2018, this actually runs after autoloader in Setup.php, so
57 * WebRequest seems like a good place for this.
61 private static function findUriExtension() {
62 /// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
63 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
64 // Strip the query string...
65 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
66 } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
67 // Probably IIS. QUERY_STRING appears separately.
68 $path = $_SERVER['SCRIPT_NAME'];
70 // Can't get the path from the server? :(
74 $period = strrpos( $path, '.' );
75 if ( $period !== false ) {
76 return strtolower( substr( $path, $period ) );
82 * Handler that compresses data with gzip if allowed by the Accept header.
84 * Unlike ob_gzhandler, it works for HEAD requests too.
86 * @param string $s Web response output
89 private static function handleGzip( $s ) {
90 if ( !function_exists( 'gzencode' ) ) {
91 wfDebug( __METHOD__
. "() skipping compression (gzencode unavailable)\n" );
94 if ( headers_sent() ) {
95 wfDebug( __METHOD__
. "() skipping compression (headers already sent)\n" );
99 $ext = self
::findUriExtension();
100 if ( $ext == '.gz' ||
$ext == '.tgz' ) {
101 // Don't do gzip compression if the URL path ends in .gz or .tgz
102 // This confuses Safari and triggers a download of the page,
103 // even though it's pretty clearly labeled as viewable HTML.
108 if ( wfClientAcceptsGzip() ) {
109 wfDebug( __METHOD__
. "() is compressing output\n" );
110 header( 'Content-Encoding: gzip' );
111 $s = gzencode( $s, 6 );
114 // Set vary header if it hasn't been set already
115 $headers = headers_list();
117 foreach ( $headers as $header ) {
118 $headerName = strtolower( substr( $header, 0, 5 ) );
119 if ( $headerName == 'vary:' ) {
125 header( 'Vary: Accept-Encoding' );
126 global $wgUseKeyHeader;
127 if ( $wgUseKeyHeader ) {
128 header( 'Key: Accept-Encoding;match=gzip' );
135 * Mangle flash policy tags which open up the site to XSS attacks.
137 * @param string $s Web response output
140 private static function mangleFlashPolicy( $s ) {
141 # Avoid weird excessive memory usage in PCRE on big articles
142 if ( preg_match( '/\<\s*cross-domain-policy(?=\s|\>)/i', $s ) ) {
143 return preg_replace( '/\<(\s*)(cross-domain-policy(?=\s|\>))/i', '<$1NOT-$2', $s );
150 * Add a Content-Length header if possible. This makes it cooperate with CDN better.
154 private static function emitContentLength( $length ) {
156 && isset( $_SERVER['SERVER_PROTOCOL'] )
157 && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0'
159 header( "Content-Length: $length" );