Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / OutputHandler.php
blob6b40c3076e28d1d113dd213016f6babefdef54e6
1 <?php
2 /**
3 * Functions to be used with PHP's output buffer.
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.
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.
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
20 * @file
23 /**
24 * Standard output handler for use with ob_start
26 * @param $s string
28 * @return string
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;
42 if ( $isHTML ) {
43 $s = wfHtmlValidationHandler( $s );
46 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
47 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
48 $s = wfGzipHandler( $s );
50 if ( !ini_get( 'output_handler' ) ) {
51 wfDoContentLength( strlen( $s ) );
54 return $s;
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
63 * @return string
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 '';
78 $period = strrpos( $path, '.' );
79 if( $period !== false ) {
80 return strtolower( substr( $path, $period ) );
82 return '';
85 /**
86 * Handler that compresses data with gzip if allowed by the Accept header.
87 * Unlike ob_gzhandler, it works for HEAD requests too.
89 * @param $s string
91 * @return string
93 function wfGzipHandler( $s ) {
94 if( !function_exists( 'gzencode' ) ) {
95 wfDebug( __FUNCTION__ . "() skipping compression (gzencode unavailable)\n" );
96 return $s;
98 if( headers_sent() ) {
99 wfDebug( __FUNCTION__ . "() skipping compression (headers already sent)\n" );
100 return $s;
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;
112 if( wfClientAcceptsGzip() ) {
113 wfDebug( __FUNCTION__ . "() is compressing output\n" );
114 header( 'Content-Encoding: gzip' );
115 $s = gzencode( $s, 6 );
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;
127 if ( !$foundVary ) {
128 header( 'Vary: Accept-Encoding' );
129 global $wgUseXVO;
130 if ( $wgUseXVO ) {
131 header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
134 return $s;
138 * Mangle flash policy tags which open up the site to XSS attacks.
140 * @param $s string
142 * @return string
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;
154 * Add a Content-Length header if possible. This makes it cooperate with squid better.
156 * @param $length int
158 function wfDoContentLength( $length ) {
159 if ( !headers_sent() && isset( $_SERVER['SERVER_PROTOCOL'] ) && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
160 header( "Content-Length: $length" );
165 * Replace the output with an error if the HTML is not valid
167 * @param $s string
169 * @return string
171 function wfHtmlValidationHandler( $s ) {
173 $errors = '';
174 if ( MWTidy::checkErrors( $s, $errors ) ) {
175 return $s;
178 header( 'Cache-Control: no-cache' );
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;
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";
203 $error = strtok( "\n" );
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>';
217 $out .= htmlspecialchars( $line ) . "</li>\n";
218 $line = strtok( "\n" );
219 $i++;
221 $out .= '</ol></body></html>';
222 return $out;