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;
32 $s = wfMangleFlashPolicy( $s );
33 if ( $wgValidateAllHtml ) {
34 $headers = headers_list();
36 foreach ( $headers as $header ) {
37 $parts = explode( ':', $header, 2 );
38 if ( count( $parts ) !== 2 ) {
41 $name = strtolower( trim( $parts[0] ) );
42 $value = trim( $parts[1] );
43 if ( $name == 'content-type' && ( strpos( $value, 'text/html' ) === 0
44 ||
strpos( $value, 'application/xhtml+xml' ) === 0 )
51 $s = wfHtmlValidationHandler( $s );
54 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
55 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
56 $s = wfGzipHandler( $s );
58 if ( !ini_get( 'output_handler' ) ) {
59 wfDoContentLength( strlen( $s ) );
66 * Get the "file extension" that some client apps will estimate from
67 * the currently-requested URL.
68 * This isn't on WebRequest because we need it when things aren't initialized
73 function wfRequestExtension() {
74 /// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
75 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
76 // Strip the query string...
77 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
78 } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
79 // Probably IIS. QUERY_STRING appears separately.
80 $path = $_SERVER['SCRIPT_NAME'];
82 // Can't get the path from the server? :(
86 $period = strrpos( $path, '.' );
87 if ( $period !== false ) {
88 return strtolower( substr( $path, $period ) );
94 * Handler that compresses data with gzip if allowed by the Accept header.
95 * Unlike ob_gzhandler, it works for HEAD requests too.
101 function wfGzipHandler( $s ) {
102 if ( !function_exists( 'gzencode' ) ) {
103 wfDebug( __FUNCTION__
. "() skipping compression (gzencode unavailable)\n" );
106 if ( headers_sent() ) {
107 wfDebug( __FUNCTION__
. "() skipping compression (headers already sent)\n" );
111 $ext = wfRequestExtension();
112 if ( $ext == '.gz' ||
$ext == '.tgz' ) {
113 // Don't do gzip compression if the URL path ends in .gz or .tgz
114 // This confuses Safari and triggers a download of the page,
115 // even though it's pretty clearly labeled as viewable HTML.
120 if ( wfClientAcceptsGzip() ) {
121 wfDebug( __FUNCTION__
. "() is compressing output\n" );
122 header( 'Content-Encoding: gzip' );
123 $s = gzencode( $s, 6 );
126 // Set vary header if it hasn't been set already
127 $headers = headers_list();
129 foreach ( $headers as $header ) {
130 if ( substr( $header, 0, 5 ) == 'Vary:' ) {
136 header( 'Vary: Accept-Encoding' );
139 header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
146 * Mangle flash policy tags which open up the site to XSS attacks.
152 function wfMangleFlashPolicy( $s ) {
153 # Avoid weird excessive memory usage in PCRE on big articles
154 if ( preg_match( '/\<\s*cross-domain-policy\s*\>/i', $s ) ) {
155 return preg_replace( '/\<\s*cross-domain-policy\s*\>/i', '<NOT-cross-domain-policy>', $s );
162 * Add a Content-Length header if possible. This makes it cooperate with squid better.
166 function wfDoContentLength( $length ) {
167 if ( !headers_sent() && isset( $_SERVER['SERVER_PROTOCOL'] ) && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
168 header( "Content-Length: $length" );
173 * Replace the output with an error if the HTML is not valid
179 function wfHtmlValidationHandler( $s ) {
182 if ( MWTidy
::checkErrors( $s, $errors ) ) {
186 header( 'Cache-Control: no-cache' );
188 $out = Html
::element( 'h1', null, 'HTML validation error' );
189 $out .= Html
::openElement( 'ul' );
191 $error = strtok( $errors, "\n" );
193 while ( $error !== false ) {
194 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
195 $lineNum = intval( $m[1] );
196 $badLines[$lineNum] = true;
197 $out .= Html
::rawElement( 'li', null,
198 Html
::element( 'a', array( 'href' => "#line-{$lineNum}" ), $error ) ) . "\n";
200 $error = strtok( "\n" );
203 $out .= Html
::closeElement( 'ul' );
204 $out .= Html
::element( 'pre', null, $errors );
205 $out .= Html
::openElement( 'ol' ) . "\n";
206 $line = strtok( $s, "\n" );
208 while ( $line !== false ) {
210 if ( isset( $badLines[$i] ) ) {
211 $attrs['class'] = 'highlight';
212 $attrs['id'] = "line-$i";
214 $out .= Html
::element( 'li', $attrs, $line ) . "\n";
215 $line = strtok( "\n" );
218 $out .= Html
::closeElement( 'ol' );
221 .highlight { background-color: #ffc }
222 li { white-space: pre }
225 $out = Html
::htmlHeader( array( 'lang' => 'en', 'dir' => 'ltr' ) ) .
226 Html
::rawElement( 'head', null,
227 Html
::element( 'title', null, 'HTML validation error' ) .
228 Html
::inlineStyle( $style ) ) .
229 Html
::rawElement( 'body', null, $out ) .
230 Html
::closeElement( 'html' );