3 * Functions to be used with PHP's output buffer
9 * Standard output handler for use with ob_start
13 function wfOutputHandler( $s ) {
14 global $wgDisableOutputCompression, $wgValidateAllHtml;
15 $s = wfMangleFlashPolicy( $s );
16 if ( $wgValidateAllHtml ) {
17 $headers = apache_response_headers();
19 foreach ( $headers as $name => $value ) {
20 if ( strtolower( $name ) == 'content-type' && strpos( $value, 'text/html' ) === false && strpos( $value, 'application/xhtml+xml' ) === false ) {
26 $s = wfHtmlValidationHandler( $s );
29 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
30 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
31 $s = wfGzipHandler( $s );
33 if ( !ini_get( 'output_handler' ) ) {
34 wfDoContentLength( strlen( $s ) );
41 * Get the "file extension" that some client apps will estimate from
42 * the currently-requested URL.
43 * This isn't on WebRequest because we need it when things aren't initialized
48 function wfRequestExtension() {
49 /// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
50 if( isset( $_SERVER['REQUEST_URI'] ) ) {
51 // Strip the query string...
52 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
53 } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
54 // Probably IIS. QUERY_STRING appears separately.
55 $path = $_SERVER['SCRIPT_NAME'];
57 // Can't get the path from the server? :(
61 $period = strrpos( $path, '.' );
62 if( $period !== false ) {
63 return strtolower( substr( $path, $period ) );
69 * Handler that compresses data with gzip if allowed by the Accept header.
70 * Unlike ob_gzhandler, it works for HEAD requests too.
76 function wfGzipHandler( $s ) {
77 if( !function_exists( 'gzencode' ) ||
headers_sent() ) {
81 $ext = wfRequestExtension();
82 if( $ext == '.gz' ||
$ext == '.tgz' ) {
83 // Don't do gzip compression if the URL path ends in .gz or .tgz
84 // This confuses Safari and triggers a download of the page,
85 // even though it's pretty clearly labeled as viewable HTML.
90 if( wfClientAcceptsGzip() ) {
91 header( 'Content-Encoding: gzip' );
92 $s = gzencode( $s, 6 );
95 // Set vary header if it hasn't been set already
96 $headers = headers_list();
98 foreach ( $headers as $header ) {
99 if ( substr( $header, 0, 5 ) == 'Vary:' ) {
105 header( 'Vary: Accept-Encoding' );
108 header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
115 * Mangle flash policy tags which open up the site to XSS attacks.
121 function wfMangleFlashPolicy( $s ) {
122 # Avoid weird excessive memory usage in PCRE on big articles
123 if ( preg_match( '/\<\s*cross-domain-policy\s*\>/i', $s ) ) {
124 return preg_replace( '/\<\s*cross-domain-policy\s*\>/i', '<NOT-cross-domain-policy>', $s );
131 * Add a Content-Length header if possible. This makes it cooperate with squid better.
135 function wfDoContentLength( $length ) {
136 if ( !headers_sent() && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
137 header( "Content-Length: $length" );
142 * Replace the output with an error if the HTML is not valid
148 function wfHtmlValidationHandler( $s ) {
151 if ( MWTidy
::checkErrors( $s, $errors ) ) {
155 header( 'Cache-Control: no-cache' );
158 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
159 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" dir="ltr">
161 <title>HTML validation error</title>
163 .highlight { background-color: #ffc }
164 li { white-space: pre }
168 <h1>HTML validation error</h1>
172 $error = strtok( $errors, "\n" );
174 while ( $error !== false ) {
175 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
176 $lineNum = intval( $m[1] );
177 $badLines[$lineNum] = true;
178 $out .= "<li><a href=\"#line-{$lineNum}\">" . htmlspecialchars( $error ) . "</a></li>\n";
180 $error = strtok( "\n" );
184 $out .= '<pre>' . htmlspecialchars( $errors ) . '</pre>';
186 $line = strtok( $s, "\n" );
188 while ( $line !== false ) {
189 if ( isset( $badLines[$i] ) ) {
190 $out .= "<li class=\"highlight\" id=\"line-$i\">";
194 $out .= htmlspecialchars( $line ) . "</li>\n";
195 $line = strtok( "\n" );
198 $out .= '</ol></body></html>';