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