00001 <?php
00002
00006 function wfOutputHandler( $s ) {
00007 global $wgDisableOutputCompression, $wgValidateAllHtml;
00008 $s = wfMangleFlashPolicy( $s );
00009 if ( $wgValidateAllHtml ) {
00010 $headers = apache_response_headers();
00011 $isHTML = true;
00012 foreach ( $headers as $name => $value ) {
00013 if ( strtolower( $name ) == 'content-type' && strpos( $value, 'text/html' ) === false && strpos( $value, 'application/xhtml+xml' ) === false ) {
00014 $isHTML = false;
00015 break;
00016 }
00017 }
00018 if ( $isHTML ) {
00019 $s = wfHtmlValidationHandler( $s );
00020 }
00021 }
00022 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
00023 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
00024 $s = wfGzipHandler( $s );
00025 }
00026 if ( !ini_get( 'output_handler' ) ) {
00027 wfDoContentLength( strlen( $s ) );
00028 }
00029 }
00030 return $s;
00031 }
00032
00039 function wfRequestExtension() {
00041 if( isset( $_SERVER['REQUEST_URI'] ) ) {
00042
00043 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
00044 } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
00045
00046 $path = $_SERVER['SCRIPT_NAME'];
00047 } else {
00048
00049 return '';
00050 }
00051
00052 $period = strrpos( $path, '.' );
00053 if( $period !== false ) {
00054 return strtolower( substr( $path, $period ) );
00055 }
00056 return '';
00057 }
00058
00063 function wfGzipHandler( $s ) {
00064 if( !function_exists( 'gzencode' ) || headers_sent() ) {
00065 return $s;
00066 }
00067
00068 $ext = wfRequestExtension();
00069 if( $ext == '.gz' || $ext == '.tgz' ) {
00070
00071
00072
00073
00074 return $s;
00075 }
00076
00077 if( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
00078 $tokens = preg_split( '/[,; ]/', $_SERVER['HTTP_ACCEPT_ENCODING'] );
00079 if ( in_array( 'gzip', $tokens ) ) {
00080 header( 'Content-Encoding: gzip' );
00081 $s = gzencode( $s, 3 );
00082 }
00083 }
00084
00085
00086 $headers = headers_list();
00087 $foundVary = false;
00088 foreach ( $headers as $header ) {
00089 if ( substr( $header, 0, 5 ) == 'Vary:' ) {
00090 $foundVary = true;
00091 break;
00092 }
00093 }
00094 if ( !$foundVary ) {
00095 header( 'Vary: Accept-Encoding' );
00096 header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
00097 }
00098 return $s;
00099 }
00100
00104 function wfMangleFlashPolicy( $s ) {
00105 # Avoid weird excessive memory usage in PCRE on big articles
00106 if ( preg_match( '/<\s*cross-domain-policy\s*>/i', $s ) ) {
00107 return preg_replace( '/<\s*cross-domain-policy\s*>/i', '<NOT-cross-domain-policy>', $s );
00108 } else {
00109 return $s;
00110 }
00111 }
00112
00116 function wfDoContentLength( $length ) {
00117 if ( !headers_sent() && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
00118 header( "Content-Length: $length" );
00119 }
00120 }
00121
00125 function wfHtmlValidationHandler( $s ) {
00126
00127 $errors = '';
00128 if ( MWTidy::checkErrors( $s, $errors ) ) {
00129 return $s;
00130 }
00131
00132 header( 'Cache-Control: no-cache' );
00133
00134 $out = <<<EOT
00135 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
00136 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
00137 <head>
00138 <title>HTML validation error</title>
00139 <style>
00140 .highlight { background-color: #ffc }
00141 li { white-space: pre }
00142 </style>
00143 </head>
00144 <body>
00145 <h1>HTML validation error</h1>
00146 <ul>
00147 EOT;
00148
00149 $error = strtok( $errors, "\n" );
00150 $badLines = array();
00151 while ( $error !== false ) {
00152 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
00153 $lineNum = intval( $m[1] );
00154 $badLines[$lineNum] = true;
00155 $out .= "<li><a href=\"#line-{$lineNum}\">" . htmlspecialchars( $error ) . "</a></li>\n";
00156 }
00157 $error = strtok( "\n" );
00158 }
00159
00160 $out .= '</ul>';
00161 $out .= '<pre>' . htmlspecialchars( $errors ) . '</pre>';
00162 $out .= "<ol>\n";
00163 $line = strtok( $s, "\n" );
00164 $i = 1;
00165 while ( $line !== false ) {
00166 if ( isset( $badLines[$i] ) ) {
00167 $out .= "<li class=\"highlight\" id=\"line-$i\">";
00168 } else {
00169 $out .= '<li>';
00170 }
00171 $out .= htmlspecialchars( $line ) . "</li>\n";
00172 $line = strtok( "\n" );
00173 $i++;
00174 }
00175 $out .= '</ol></body></html>';
00176 return $out;
00177 }