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