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 = apache_response_headers();
36 foreach ( $headers as $name => $value ) {
37 if ( strtolower( $name ) == 'content-type' && strpos( $value, 'text/html' ) === false && strpos( $value, 'application/xhtml+xml' ) === false ) {
43 $s = wfHtmlValidationHandler( $s );
46 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
47 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
48 $s = wfGzipHandler( $s );
50 if ( !ini_get( 'output_handler' ) ) {
51 wfDoContentLength( strlen( $s ) );
58 * Get the "file extension" that some client apps will estimate from
59 * the currently-requested URL.
60 * This isn't on WebRequest because we need it when things aren't initialized
65 function wfRequestExtension() {
66 /// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
67 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
68 // Strip the query string...
69 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
70 } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
71 // Probably IIS. QUERY_STRING appears separately.
72 $path = $_SERVER['SCRIPT_NAME'];
74 // Can't get the path from the server? :(
78 $period = strrpos( $path, '.' );
79 if ( $period !== false ) {
80 return strtolower( substr( $path, $period ) );
86 * Handler that compresses data with gzip if allowed by the Accept header.
87 * Unlike ob_gzhandler, it works for HEAD requests too.
93 function wfGzipHandler( $s ) {
94 if ( !function_exists( 'gzencode' ) ) {
95 wfDebug( __FUNCTION__
. "() skipping compression (gzencode unavailable)\n" );
98 if ( headers_sent() ) {
99 wfDebug( __FUNCTION__
. "() skipping compression (headers already sent)\n" );
103 $ext = wfRequestExtension();
104 if ( $ext == '.gz' ||
$ext == '.tgz' ) {
105 // Don't do gzip compression if the URL path ends in .gz or .tgz
106 // This confuses Safari and triggers a download of the page,
107 // even though it's pretty clearly labeled as viewable HTML.
112 if ( wfClientAcceptsGzip() ) {
113 wfDebug( __FUNCTION__
. "() is compressing output\n" );
114 header( 'Content-Encoding: gzip' );
115 $s = gzencode( $s, 6 );
118 // Set vary header if it hasn't been set already
119 $headers = headers_list();
121 foreach ( $headers as $header ) {
122 if ( substr( $header, 0, 5 ) == 'Vary:' ) {
128 header( 'Vary: Accept-Encoding' );
131 header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
138 * Mangle flash policy tags which open up the site to XSS attacks.
144 function wfMangleFlashPolicy( $s ) {
145 # Avoid weird excessive memory usage in PCRE on big articles
146 if ( preg_match( '/\<\s*cross-domain-policy\s*\>/i', $s ) ) {
147 return preg_replace( '/\<\s*cross-domain-policy\s*\>/i', '<NOT-cross-domain-policy>', $s );
154 * Add a Content-Length header if possible. This makes it cooperate with squid better.
158 function wfDoContentLength( $length ) {
159 if ( !headers_sent() && isset( $_SERVER['SERVER_PROTOCOL'] ) && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
160 header( "Content-Length: $length" );
165 * Replace the output with an error if the HTML is not valid
171 function wfHtmlValidationHandler( $s ) {
174 if ( MWTidy
::checkErrors( $s, $errors ) ) {
178 header( 'Cache-Control: no-cache' );
180 $out = Html
::element( 'h1', null, 'HTML validation error' );
181 $out .= Html
::openElement( 'ul' );
183 $error = strtok( $errors, "\n" );
185 while ( $error !== false ) {
186 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
187 $lineNum = intval( $m[1] );
188 $badLines[$lineNum] = true;
189 $out .= Html
::rawElement( 'li', null,
190 Html
::element( 'a', array( 'href' => "#line-{$lineNum}" ), $error ) ) . "\n";
192 $error = strtok( "\n" );
195 $out .= Html
::closeElement( 'ul' );
196 $out .= Html
::element( 'pre', null, $errors );
197 $out .= Html
::openElement( 'ol' ) . "\n";
198 $line = strtok( $s, "\n" );
200 while ( $line !== false ) {
202 if ( isset( $badLines[$i] ) ) {
203 $attrs['class'] = 'highlight';
204 $attrs['id'] = "line-$i";
206 $out .= Html
::element( 'li', $attrs, $line ) . "\n";
207 $line = strtok( "\n" );
210 $out .= Html
::closeElement( 'ol' );
213 .highlight { background-color: #ffc }
214 li { white-space: pre }
217 $out = Html
::htmlHeader( array( 'lang' => 'en', 'dir' => 'ltr' ) ) .
218 Html
::rawElement( 'head', null,
219 Html
::element( 'title', null, 'HTML validation error' ) .
220 Html
::inlineStyle( $style ) ) .
221 Html
::rawElement( 'body', null, $out ) .
222 Html
::closeElement( 'html' );