3 * Functions to be used with PHP's output buffer
9 * Standard output handler for use with ob_start
15 function wfOutputHandler( $s ) {
16 global $wgDisableOutputCompression, $wgValidateAllHtml;
17 $s = wfMangleFlashPolicy( $s );
18 if ( $wgValidateAllHtml ) {
19 $headers = apache_response_headers();
21 foreach ( $headers as $name => $value ) {
22 if ( strtolower( $name ) == 'content-type' && strpos( $value, 'text/html' ) === false && strpos( $value, 'application/xhtml+xml' ) === false ) {
28 $s = wfHtmlValidationHandler( $s );
31 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
32 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
33 $s = wfGzipHandler( $s );
35 if ( !ini_get( 'output_handler' ) ) {
36 wfDoContentLength( strlen( $s ) );
43 * Get the "file extension" that some client apps will estimate from
44 * the currently-requested URL.
45 * This isn't on WebRequest because we need it when things aren't initialized
50 function wfRequestExtension() {
51 /// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
52 if( isset( $_SERVER['REQUEST_URI'] ) ) {
53 // Strip the query string...
54 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
55 } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
56 // Probably IIS. QUERY_STRING appears separately.
57 $path = $_SERVER['SCRIPT_NAME'];
59 // Can't get the path from the server? :(
63 $period = strrpos( $path, '.' );
64 if( $period !== false ) {
65 return strtolower( substr( $path, $period ) );
71 * Handler that compresses data with gzip if allowed by the Accept header.
72 * Unlike ob_gzhandler, it works for HEAD requests too.
78 function wfGzipHandler( $s ) {
79 if( !function_exists( 'gzencode' ) ||
headers_sent() ) {
83 $ext = wfRequestExtension();
84 if( $ext == '.gz' ||
$ext == '.tgz' ) {
85 // Don't do gzip compression if the URL path ends in .gz or .tgz
86 // This confuses Safari and triggers a download of the page,
87 // even though it's pretty clearly labeled as viewable HTML.
92 if( wfClientAcceptsGzip() ) {
93 header( 'Content-Encoding: gzip' );
94 $s = gzencode( $s, 6 );
97 // Set vary header if it hasn't been set already
98 $headers = headers_list();
100 foreach ( $headers as $header ) {
101 if ( substr( $header, 0, 5 ) == 'Vary:' ) {
107 header( 'Vary: Accept-Encoding' );
110 header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
117 * Mangle flash policy tags which open up the site to XSS attacks.
123 function wfMangleFlashPolicy( $s ) {
124 # Avoid weird excessive memory usage in PCRE on big articles
125 if ( preg_match( '/\<\s*cross-domain-policy\s*\>/i', $s ) ) {
126 return preg_replace( '/\<\s*cross-domain-policy\s*\>/i', '<NOT-cross-domain-policy>', $s );
133 * Add a Content-Length header if possible. This makes it cooperate with squid better.
137 function wfDoContentLength( $length ) {
138 if ( !headers_sent() && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
139 header( "Content-Length: $length" );
144 * Replace the output with an error if the HTML is not valid
150 function wfHtmlValidationHandler( $s ) {
153 if ( MWTidy
::checkErrors( $s, $errors ) ) {
157 header( 'Cache-Control: no-cache' );
160 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
161 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" dir="ltr">
163 <title>HTML validation error</title>
165 .highlight { background-color: #ffc }
166 li { white-space: pre }
170 <h1>HTML validation error</h1>
174 $error = strtok( $errors, "\n" );
176 while ( $error !== false ) {
177 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
178 $lineNum = intval( $m[1] );
179 $badLines[$lineNum] = true;
180 $out .= "<li><a href=\"#line-{$lineNum}\">" . htmlspecialchars( $error ) . "</a></li>\n";
182 $error = strtok( "\n" );
186 $out .= '<pre>' . htmlspecialchars( $errors ) . '</pre>';
188 $line = strtok( $s, "\n" );
190 while ( $line !== false ) {
191 if ( isset( $badLines[$i] ) ) {
192 $out .= "<li class=\"highlight\" id=\"line-$i\">";
196 $out .= htmlspecialchars( $line ) . "</li>\n";
197 $line = strtok( "\n" );
200 $out .= '</ol></body></html>';