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
33 * Standard output handler for use with ob_start.
35 * @param string $s Web response output
38 public static function handle( $s ) {
39 global $wgDisableOutputCompression, $wgValidateAllHtml, $wgMangleFlashPolicy;
40 if ( $wgMangleFlashPolicy ) {
41 $s = self
::mangleFlashPolicy( $s );
43 if ( $wgValidateAllHtml ) {
44 $headers = headers_list();
46 foreach ( $headers as $header ) {
47 $parts = explode( ':', $header, 2 );
48 if ( count( $parts ) !== 2 ) {
51 $name = strtolower( trim( $parts[0] ) );
52 $value = trim( $parts[1] );
53 if ( $name == 'content-type' && ( strpos( $value, 'text/html' ) === 0
54 ||
strpos( $value, 'application/xhtml+xml' ) === 0 )
61 $s = self
::validateAllHtml( $s );
64 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
65 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
66 $s = self
::handleGzip( $s );
68 if ( !ini_get( 'output_handler' ) ) {
69 self
::emitContentLength( strlen( $s ) );
76 * Get the "file extension" that some client apps will estimate from
77 * the currently-requested URL.
79 * This isn't a WebRequest method, because we need it before the class loads.
80 * @todo As of 2018, this actually runs after autoloader in Setup.php, so
81 * WebRequest seems like a good place for this.
85 private static function findUriExtension() {
86 /// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
87 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
88 // Strip the query string...
89 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
90 } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
91 // Probably IIS. QUERY_STRING appears separately.
92 $path = $_SERVER['SCRIPT_NAME'];
94 // Can't get the path from the server? :(
98 $period = strrpos( $path, '.' );
99 if ( $period !== false ) {
100 return strtolower( substr( $path, $period ) );
106 * Handler that compresses data with gzip if allowed by the Accept header.
108 * Unlike ob_gzhandler, it works for HEAD requests too.
110 * @param string $s Web response output
113 private static function handleGzip( $s ) {
114 if ( !function_exists( 'gzencode' ) ) {
115 wfDebug( __METHOD__
. "() skipping compression (gzencode unavailable)\n" );
118 if ( headers_sent() ) {
119 wfDebug( __METHOD__
. "() skipping compression (headers already sent)\n" );
123 $ext = self
::findUriExtension();
124 if ( $ext == '.gz' ||
$ext == '.tgz' ) {
125 // Don't do gzip compression if the URL path ends in .gz or .tgz
126 // This confuses Safari and triggers a download of the page,
127 // even though it's pretty clearly labeled as viewable HTML.
132 if ( wfClientAcceptsGzip() ) {
133 wfDebug( __METHOD__
. "() is compressing output\n" );
134 header( 'Content-Encoding: gzip' );
135 $s = gzencode( $s, 6 );
138 // Set vary header if it hasn't been set already
139 $headers = headers_list();
141 foreach ( $headers as $header ) {
142 $headerName = strtolower( substr( $header, 0, 5 ) );
143 if ( $headerName == 'vary:' ) {
149 header( 'Vary: Accept-Encoding' );
150 global $wgUseKeyHeader;
151 if ( $wgUseKeyHeader ) {
152 header( 'Key: Accept-Encoding;match=gzip' );
159 * Mangle flash policy tags which open up the site to XSS attacks.
161 * @param string $s Web response output
164 private static function mangleFlashPolicy( $s ) {
165 # Avoid weird excessive memory usage in PCRE on big articles
166 if ( preg_match( '/\<\s*cross-domain-policy(?=\s|\>)/i', $s ) ) {
167 return preg_replace( '/\<(\s*)(cross-domain-policy(?=\s|\>))/i', '<$1NOT-$2', $s );
174 * Add a Content-Length header if possible. This makes it cooperate with CDN better.
178 private static function emitContentLength( $length ) {
180 && isset( $_SERVER['SERVER_PROTOCOL'] )
181 && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0'
183 header( "Content-Length: $length" );
188 * Replace the output with an error if the HTML is not valid.
193 private static function validateAllHtml( $s ) {
195 if ( MWTidy
::checkErrors( $s, $errors ) ) {
199 header( 'Cache-Control: no-cache' );
201 $out = Html
::element( 'h1', null, 'HTML validation error' );
202 $out .= Html
::openElement( 'ul' );
204 $error = strtok( $errors, "\n" );
206 while ( $error !== false ) {
207 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
208 $lineNum = intval( $m[1] );
209 $badLines[$lineNum] = true;
210 $out .= Html
::rawElement( 'li', null,
211 Html
::element( 'a', [ 'href' => "#line-{$lineNum}" ], $error ) ) . "\n";
213 $error = strtok( "\n" );
216 $out .= Html
::closeElement( 'ul' );
217 $out .= Html
::element( 'pre', null, $errors );
218 $out .= Html
::openElement( 'ol' ) . "\n";
219 $line = strtok( $s, "\n" );
221 while ( $line !== false ) {
223 if ( isset( $badLines[$i] ) ) {
224 $attrs['class'] = 'highlight';
225 $attrs['id'] = "line-$i";
227 $out .= Html
::element( 'li', $attrs, $line ) . "\n";
228 $line = strtok( "\n" );
231 $out .= Html
::closeElement( 'ol' );
234 .highlight { background-color: #ffc }
235 li { white-space: pre }
238 $out = Html
::htmlHeader( [ 'lang' => 'en', 'dir' => 'ltr' ] ) .
239 Html
::rawElement( 'head', null,
240 Html
::element( 'title', null, 'HTML validation error' ) .
241 Html
::inlineStyle( $style ) ) .
242 Html
::rawElement( 'body', null, $out ) .
243 Html
::closeElement( 'html' );