]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/OutputHandler.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / includes / OutputHandler.php
1 <?php
2 /**
3  * Functions to be used with PHP's output buffer
4  *
5  * @file
6  */
7
8 /**
9  * Standard output handler for use with ob_start
10  */
11 function wfOutputHandler( $s ) {
12         global $wgDisableOutputCompression, $wgValidateAllHtml;
13         $s = wfMangleFlashPolicy( $s );
14         if ( $wgValidateAllHtml ) {
15                 $headers = apache_response_headers();
16                 $isHTML = true;
17                 foreach ( $headers as $name => $value ) {
18                         if ( strtolower( $name ) == 'content-type' && strpos( $value, 'text/html' ) === false && strpos( $value, 'application/xhtml+xml' ) === false ) {
19                                 $isHTML = false;
20                                 break;
21                         }
22                 }
23                 if ( $isHTML ) {
24                         $s = wfHtmlValidationHandler( $s );
25                 }
26         }
27         if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
28                 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
29                         $s = wfGzipHandler( $s );
30                 }
31                 if ( !ini_get( 'output_handler' ) ) {
32                         wfDoContentLength( strlen( $s ) );
33                 }
34         }
35         return $s;
36 }
37
38 /**
39  * Get the "file extension" that some client apps will estimate from
40  * the currently-requested URL.
41  * This isn't on WebRequest because we need it when things aren't initialized
42  * @private
43  */
44 function wfRequestExtension() {
45         /// @todo Fixme: this sort of dupes some code in WebRequest::getRequestUrl()
46         if( isset( $_SERVER['REQUEST_URI'] ) ) {
47                 // Strip the query string...
48                 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
49         } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
50                 // Probably IIS. QUERY_STRING appears separately.
51                 $path = $_SERVER['SCRIPT_NAME'];
52         } else {
53                 // Can't get the path from the server? :(
54                 return '';
55         }
56
57         $period = strrpos( $path, '.' );
58         if( $period !== false ) {
59                 return strtolower( substr( $path, $period ) );
60         }
61         return '';
62 }
63
64 /**
65  * Handler that compresses data with gzip if allowed by the Accept header.
66  * Unlike ob_gzhandler, it works for HEAD requests too.
67  */
68 function wfGzipHandler( $s ) {
69         if( !function_exists( 'gzencode' ) || headers_sent() ) {
70                 return $s;
71         }
72
73         $ext = wfRequestExtension();
74         if( $ext == '.gz' || $ext == '.tgz' ) {
75                 // Don't do gzip compression if the URL path ends in .gz or .tgz
76                 // This confuses Safari and triggers a download of the page,
77                 // even though it's pretty clearly labeled as viewable HTML.
78                 // Bad Safari! Bad!
79                 return $s;
80         }
81
82         if( wfClientAcceptsGzip() ) {
83                 header( 'Content-Encoding: gzip' );
84                 $s = gzencode( $s, 6 );
85         }
86
87         // Set vary header if it hasn't been set already
88         $headers = headers_list();
89         $foundVary = false;
90         foreach ( $headers as $header ) {
91                 if ( substr( $header, 0, 5 ) == 'Vary:' ) {
92                         $foundVary = true;
93                         break;
94                 }
95         }
96         if ( !$foundVary ) {
97                 header( 'Vary: Accept-Encoding' );
98                 global $wgUseXVO;
99                 if ( $wgUseXVO ) {
100                         header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
101                 }
102         }
103         return $s;
104 }
105
106 /**
107  * Mangle flash policy tags which open up the site to XSS attacks.
108  */
109 function wfMangleFlashPolicy( $s ) {
110         # Avoid weird excessive memory usage in PCRE on big articles
111         if ( preg_match( '/\<\s*cross-domain-policy\s*\>/i', $s ) ) {
112                 return preg_replace( '/\<\s*cross-domain-policy\s*\>/i', '<NOT-cross-domain-policy>', $s );
113         } else {
114                 return $s;
115         }
116 }
117
118 /**
119  * Add a Content-Length header if possible. This makes it cooperate with squid better.
120  */
121 function wfDoContentLength( $length ) {
122         if ( !headers_sent() && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
123                 header( "Content-Length: $length" );
124         }
125 }
126
127 /**
128  * Replace the output with an error if the HTML is not valid
129  */
130 function wfHtmlValidationHandler( $s ) {
131
132         $errors = '';
133         if ( MWTidy::checkErrors( $s, $errors ) ) {
134                 return $s;
135         }
136
137         header( 'Cache-Control: no-cache' );
138
139         $out = <<<EOT
140 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
141 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" dir="ltr">
142 <head>
143 <title>HTML validation error</title>
144 <style>
145 .highlight { background-color: #ffc }
146 li { white-space: pre }
147 </style>
148 </head>
149 <body>
150 <h1>HTML validation error</h1>
151 <ul>
152 EOT;
153
154         $error = strtok( $errors, "\n" );
155         $badLines = array();
156         while ( $error !== false ) {
157                 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
158                         $lineNum = intval( $m[1] );
159                         $badLines[$lineNum] = true;
160                         $out .= "<li><a href=\"#line-{$lineNum}\">" . htmlspecialchars( $error ) . "</a></li>\n";
161                 }
162                 $error = strtok( "\n" );
163         }
164
165         $out .= '</ul>';
166         $out .= '<pre>' . htmlspecialchars( $errors ) . '</pre>';
167         $out .= "<ol>\n";
168         $line = strtok( $s, "\n" );
169         $i = 1;
170         while ( $line !== false ) {
171                 if ( isset( $badLines[$i] ) ) {
172                         $out .= "<li class=\"highlight\" id=\"line-$i\">";
173                 } else {
174                         $out .= '<li>';
175                 }
176                 $out .= htmlspecialchars( $line ) . "</li>\n";
177                 $line = strtok( "\n" );
178                 $i++;
179         }
180         $out .= '</ol></body></html>';
181         return $out;
182 }