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
24 * Standard output handler for use with ob_start
30 function wfOutputHandler( $s ) {
31 global $wgDisableOutputCompression, $wgValidateAllHtml, $wgMangleFlashPolicy;
32 if ( $wgMangleFlashPolicy ) {
33 $s = wfMangleFlashPolicy( $s );
35 if ( $wgValidateAllHtml ) {
36 $headers = headers_list();
38 foreach ( $headers as $header ) {
39 $parts = explode( ':', $header, 2 );
40 if ( count( $parts ) !== 2 ) {
43 $name = strtolower( trim( $parts[0] ) );
44 $value = trim( $parts[1] );
45 if ( $name == 'content-type' && ( strpos( $value, 'text/html' ) === 0
46 ||
strpos( $value, 'application/xhtml+xml' ) === 0 )
53 $s = wfHtmlValidationHandler( $s );
56 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
57 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
58 $s = wfGzipHandler( $s );
60 if ( !ini_get( 'output_handler' ) ) {
61 wfDoContentLength( strlen( $s ) );
68 * Get the "file extension" that some client apps will estimate from
69 * the currently-requested URL.
70 * This isn't on WebRequest because we need it when things aren't initialized
75 function wfRequestExtension() {
76 /// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
77 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
78 // Strip the query string...
79 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
80 } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
81 // Probably IIS. QUERY_STRING appears separately.
82 $path = $_SERVER['SCRIPT_NAME'];
84 // Can't get the path from the server? :(
88 $period = strrpos( $path, '.' );
89 if ( $period !== false ) {
90 return strtolower( substr( $path, $period ) );
96 * Handler that compresses data with gzip if allowed by the Accept header.
97 * Unlike ob_gzhandler, it works for HEAD requests too.
103 function wfGzipHandler( $s ) {
104 if ( !function_exists( 'gzencode' ) ) {
105 wfDebug( __FUNCTION__
. "() skipping compression (gzencode unavailable)\n" );
108 if ( headers_sent() ) {
109 wfDebug( __FUNCTION__
. "() skipping compression (headers already sent)\n" );
113 $ext = wfRequestExtension();
114 if ( $ext == '.gz' ||
$ext == '.tgz' ) {
115 // Don't do gzip compression if the URL path ends in .gz or .tgz
116 // This confuses Safari and triggers a download of the page,
117 // even though it's pretty clearly labeled as viewable HTML.
122 if ( wfClientAcceptsGzip() ) {
123 wfDebug( __FUNCTION__
. "() is compressing output\n" );
124 header( 'Content-Encoding: gzip' );
125 $s = gzencode( $s, 6 );
128 // Set vary header if it hasn't been set already
129 $headers = headers_list();
131 foreach ( $headers as $header ) {
132 $headerName = strtolower( substr( $header, 0, 5 ) );
133 if ( $headerName == 'vary:' ) {
139 header( 'Vary: Accept-Encoding' );
140 global $wgUseKeyHeader;
141 if ( $wgUseKeyHeader ) {
142 header( 'Key: Accept-Encoding;match=gzip' );
149 * Mangle flash policy tags which open up the site to XSS attacks.
155 function wfMangleFlashPolicy( $s ) {
156 # Avoid weird excessive memory usage in PCRE on big articles
157 if ( preg_match( '/\<\s*cross-domain-policy(?=\s|\>)/i', $s ) ) {
158 return preg_replace( '/\<(\s*)(cross-domain-policy(?=\s|\>))/i', '<$1NOT-$2', $s );
165 * Add a Content-Length header if possible. This makes it cooperate with CDN better.
169 function wfDoContentLength( $length ) {
171 && isset( $_SERVER['SERVER_PROTOCOL'] )
172 && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0'
174 header( "Content-Length: $length" );
179 * Replace the output with an error if the HTML is not valid
185 function wfHtmlValidationHandler( $s ) {
188 if ( MWTidy
::checkErrors( $s, $errors ) ) {
192 header( 'Cache-Control: no-cache' );
194 $out = Html
::element( 'h1', null, 'HTML validation error' );
195 $out .= Html
::openElement( 'ul' );
197 $error = strtok( $errors, "\n" );
199 while ( $error !== false ) {
200 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
201 $lineNum = intval( $m[1] );
202 $badLines[$lineNum] = true;
203 $out .= Html
::rawElement( 'li', null,
204 Html
::element( 'a', [ 'href' => "#line-{$lineNum}" ], $error ) ) . "\n";
206 $error = strtok( "\n" );
209 $out .= Html
::closeElement( 'ul' );
210 $out .= Html
::element( 'pre', null, $errors );
211 $out .= Html
::openElement( 'ol' ) . "\n";
212 $line = strtok( $s, "\n" );
214 while ( $line !== false ) {
216 if ( isset( $badLines[$i] ) ) {
217 $attrs['class'] = 'highlight';
218 $attrs['id'] = "line-$i";
220 $out .= Html
::element( 'li', $attrs, $line ) . "\n";
221 $line = strtok( "\n" );
224 $out .= Html
::closeElement( 'ol' );
227 .highlight { background-color: #ffc }
228 li { white-space: pre }
231 $out = Html
::htmlHeader( [ 'lang' => 'en', 'dir' => 'ltr' ] ) .
232 Html
::rawElement( 'head', null,
233 Html
::element( 'title', null, 'HTML validation error' ) .
234 Html
::inlineStyle( $style ) ) .
235 Html
::rawElement( 'body', null, $out ) .
236 Html
::closeElement( 'html' );