MediaWiki  REL1_19
OutputHandler.php
Go to the documentation of this file.
00001 <?php
00015 function wfOutputHandler( $s ) {
00016         global $wgDisableOutputCompression, $wgValidateAllHtml;
00017         $s = wfMangleFlashPolicy( $s );
00018         if ( $wgValidateAllHtml ) {
00019                 $headers = apache_response_headers();
00020                 $isHTML = true;
00021                 foreach ( $headers as $name => $value ) {
00022                         if ( strtolower( $name ) == 'content-type' && strpos( $value, 'text/html' ) === false && strpos( $value, 'application/xhtml+xml' ) === false ) {
00023                                 $isHTML = false;
00024                                 break;
00025                         }
00026                 }
00027                 if ( $isHTML ) {
00028                         $s = wfHtmlValidationHandler( $s );
00029                 }
00030         }
00031         if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
00032                 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
00033                         $s = wfGzipHandler( $s );
00034                 }
00035                 if ( !ini_get( 'output_handler' ) ) {
00036                         wfDoContentLength( strlen( $s ) );
00037                 }
00038         }
00039         return $s;
00040 }
00041 
00050 function wfRequestExtension() {
00052         if( isset( $_SERVER['REQUEST_URI'] ) ) {
00053                 // Strip the query string...
00054                 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
00055         } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
00056                 // Probably IIS. QUERY_STRING appears separately.
00057                 $path = $_SERVER['SCRIPT_NAME'];
00058         } else {
00059                 // Can't get the path from the server? :(
00060                 return '';
00061         }
00062 
00063         $period = strrpos( $path, '.' );
00064         if( $period !== false ) {
00065                 return strtolower( substr( $path, $period ) );
00066         }
00067         return '';
00068 }
00069 
00078 function wfGzipHandler( $s ) {
00079         if( !function_exists( 'gzencode' ) || headers_sent() ) {
00080                 return $s;
00081         }
00082 
00083         $ext = wfRequestExtension();
00084         if( $ext == '.gz' || $ext == '.tgz' ) {
00085                 // Don't do gzip compression if the URL path ends in .gz or .tgz
00086                 // This confuses Safari and triggers a download of the page,
00087                 // even though it's pretty clearly labeled as viewable HTML.
00088                 // Bad Safari! Bad!
00089                 return $s;
00090         }
00091 
00092         if( wfClientAcceptsGzip() ) {
00093                 header( 'Content-Encoding: gzip' );
00094                 $s = gzencode( $s, 6 );
00095         }
00096 
00097         // Set vary header if it hasn't been set already
00098         $headers = headers_list();
00099         $foundVary = false;
00100         foreach ( $headers as $header ) {
00101                 if ( substr( $header, 0, 5 ) == 'Vary:' ) {
00102                         $foundVary = true;
00103                         break;
00104                 }
00105         }
00106         if ( !$foundVary ) {
00107                 header( 'Vary: Accept-Encoding' );
00108                 global $wgUseXVO;
00109                 if ( $wgUseXVO ) {
00110                         header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
00111                 }
00112         }
00113         return $s;
00114 }
00115 
00123 function wfMangleFlashPolicy( $s ) {
00124         # Avoid weird excessive memory usage in PCRE on big articles
00125         if ( preg_match( '/<\s*cross-domain-policy\s*>/i', $s ) ) {
00126                 return preg_replace( '/<\s*cross-domain-policy\s*>/i', '<NOT-cross-domain-policy>', $s );
00127         } else {
00128                 return $s;
00129         }
00130 }
00131 
00137 function wfDoContentLength( $length ) {
00138         if ( !headers_sent() && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
00139                 header( "Content-Length: $length" );
00140         }
00141 }
00142 
00150 function wfHtmlValidationHandler( $s ) {
00151 
00152         $errors = '';
00153         if ( MWTidy::checkErrors( $s, $errors ) ) {
00154                 return $s;
00155         }
00156 
00157         header( 'Cache-Control: no-cache' );
00158 
00159         $out = <<<EOT
00160 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
00161 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" dir="ltr">
00162 <head>
00163 <title>HTML validation error</title>
00164 <style>
00165 .highlight { background-color: #ffc }
00166 li { white-space: pre }
00167 </style>
00168 </head>
00169 <body>
00170 <h1>HTML validation error</h1>
00171 <ul>
00172 EOT;
00173 
00174         $error = strtok( $errors, "\n" );
00175         $badLines = array();
00176         while ( $error !== false ) {
00177                 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
00178                         $lineNum = intval( $m[1] );
00179                         $badLines[$lineNum] = true;
00180                         $out .= "<li><a href=\"#line-{$lineNum}\">" . htmlspecialchars( $error ) . "</a></li>\n";
00181                 }
00182                 $error = strtok( "\n" );
00183         }
00184 
00185         $out .= '</ul>';
00186         $out .= '<pre>' . htmlspecialchars( $errors ) . '</pre>';
00187         $out .= "<ol>\n";
00188         $line = strtok( $s, "\n" );
00189         $i = 1;
00190         while ( $line !== false ) {
00191                 if ( isset( $badLines[$i] ) ) {
00192                         $out .= "<li class=\"highlight\" id=\"line-$i\">";
00193                 } else {
00194                         $out .= '<li>';
00195                 }
00196                 $out .= htmlspecialchars( $line ) . "</li>\n";
00197                 $line = strtok( "\n" );
00198                 $i++;
00199         }
00200         $out .= '</ol></body></html>';
00201         return $out;
00202 }