MediaWiki  REL1_20
OutputHandler.php
Go to the documentation of this file.
00001 <?php
00030 function wfOutputHandler( $s ) {
00031         global $wgDisableOutputCompression, $wgValidateAllHtml;
00032         $s = wfMangleFlashPolicy( $s );
00033         if ( $wgValidateAllHtml ) {
00034                 $headers = apache_response_headers();
00035                 $isHTML = true;
00036                 foreach ( $headers as $name => $value ) {
00037                         if ( strtolower( $name ) == 'content-type' && strpos( $value, 'text/html' ) === false && strpos( $value, 'application/xhtml+xml' ) === false ) {
00038                                 $isHTML = false;
00039                                 break;
00040                         }
00041                 }
00042                 if ( $isHTML ) {
00043                         $s = wfHtmlValidationHandler( $s );
00044                 }
00045         }
00046         if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
00047                 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
00048                         $s = wfGzipHandler( $s );
00049                 }
00050                 if ( !ini_get( 'output_handler' ) ) {
00051                         wfDoContentLength( strlen( $s ) );
00052                 }
00053         }
00054         return $s;
00055 }
00056 
00065 function wfRequestExtension() {
00067         if( isset( $_SERVER['REQUEST_URI'] ) ) {
00068                 // Strip the query string...
00069                 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
00070         } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
00071                 // Probably IIS. QUERY_STRING appears separately.
00072                 $path = $_SERVER['SCRIPT_NAME'];
00073         } else {
00074                 // Can't get the path from the server? :(
00075                 return '';
00076         }
00077 
00078         $period = strrpos( $path, '.' );
00079         if( $period !== false ) {
00080                 return strtolower( substr( $path, $period ) );
00081         }
00082         return '';
00083 }
00084 
00093 function wfGzipHandler( $s ) {
00094         if( !function_exists( 'gzencode' ) ) {
00095                 wfDebug( __FUNCTION__ . "() skipping compression (gzencode unavaible)\n" );
00096                 return $s;
00097         }
00098         if( headers_sent() ) {
00099                 wfDebug( __FUNCTION__ . "() skipping compression (headers already sent)\n" );
00100                 return $s;
00101         }
00102 
00103         $ext = wfRequestExtension();
00104         if( $ext == '.gz' || $ext == '.tgz' ) {
00105                 // Don't do gzip compression if the URL path ends in .gz or .tgz
00106                 // This confuses Safari and triggers a download of the page,
00107                 // even though it's pretty clearly labeled as viewable HTML.
00108                 // Bad Safari! Bad!
00109                 return $s;
00110         }
00111 
00112         if( wfClientAcceptsGzip() ) {
00113                 wfDebug( __FUNCTION__ . "() is compressing output\n" );
00114                 header( 'Content-Encoding: gzip' );
00115                 $s = gzencode( $s, 6 );
00116         }
00117 
00118         // Set vary header if it hasn't been set already
00119         $headers = headers_list();
00120         $foundVary = false;
00121         foreach ( $headers as $header ) {
00122                 if ( substr( $header, 0, 5 ) == 'Vary:' ) {
00123                         $foundVary = true;
00124                         break;
00125                 }
00126         }
00127         if ( !$foundVary ) {
00128                 header( 'Vary: Accept-Encoding' );
00129                 global $wgUseXVO;
00130                 if ( $wgUseXVO ) {
00131                         header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
00132                 }
00133         }
00134         return $s;
00135 }
00136 
00144 function wfMangleFlashPolicy( $s ) {
00145         # Avoid weird excessive memory usage in PCRE on big articles
00146         if ( preg_match( '/<\s*cross-domain-policy\s*>/i', $s ) ) {
00147                 return preg_replace( '/<\s*cross-domain-policy\s*>/i', '<NOT-cross-domain-policy>', $s );
00148         } else {
00149                 return $s;
00150         }
00151 }
00152 
00158 function wfDoContentLength( $length ) {
00159         if ( !headers_sent() && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
00160                 header( "Content-Length: $length" );
00161         }
00162 }
00163 
00171 function wfHtmlValidationHandler( $s ) {
00172 
00173         $errors = '';
00174         if ( MWTidy::checkErrors( $s, $errors ) ) {
00175                 return $s;
00176         }
00177 
00178         header( 'Cache-Control: no-cache' );
00179 
00180         $out = <<<EOT
00181 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
00182 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" dir="ltr">
00183 <head>
00184 <title>HTML validation error</title>
00185 <style>
00186 .highlight { background-color: #ffc }
00187 li { white-space: pre }
00188 </style>
00189 </head>
00190 <body>
00191 <h1>HTML validation error</h1>
00192 <ul>
00193 EOT;
00194 
00195         $error = strtok( $errors, "\n" );
00196         $badLines = array();
00197         while ( $error !== false ) {
00198                 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
00199                         $lineNum = intval( $m[1] );
00200                         $badLines[$lineNum] = true;
00201                         $out .= "<li><a href=\"#line-{$lineNum}\">" . htmlspecialchars( $error ) . "</a></li>\n";
00202                 }
00203                 $error = strtok( "\n" );
00204         }
00205 
00206         $out .= '</ul>';
00207         $out .= '<pre>' . htmlspecialchars( $errors ) . '</pre>';
00208         $out .= "<ol>\n";
00209         $line = strtok( $s, "\n" );
00210         $i = 1;
00211         while ( $line !== false ) {
00212                 if ( isset( $badLines[$i] ) ) {
00213                         $out .= "<li class=\"highlight\" id=\"line-$i\">";
00214                 } else {
00215                         $out .= '<li>';
00216                 }
00217                 $out .= htmlspecialchars( $line ) . "</li>\n";
00218                 $line = strtok( "\n" );
00219                 $i++;
00220         }
00221         $out .= '</ol></body></html>';
00222         return $out;
00223 }