Merge "Make line breaks in <blockquote> behave like <div> (bug 6200)."
[mediawiki.git] / includes / OutputHandler.php
blob3860b8e2b384a69ab1526de947575f44baa2629c
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 = headers_list();
35 $isHTML = false;
36 foreach ( $headers as $header ) {
37 $parts = explode( ':', $header, 2 );
38 if ( count( $parts ) !== 2 ) {
39 continue;
41 $name = strtolower( trim( $parts[0] ) );
42 $value = trim( $parts[1] );
43 if ( $name == 'content-type' && ( strpos( $value, 'text/html' ) === 0
44 || strpos( $value, 'application/xhtml+xml' ) === 0 )
45 ) {
46 $isHTML = true;
47 break;
50 if ( $isHTML ) {
51 $s = wfHtmlValidationHandler( $s );
54 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
55 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
56 $s = wfGzipHandler( $s );
58 if ( !ini_get( 'output_handler' ) ) {
59 wfDoContentLength( strlen( $s ) );
62 return $s;
65 /**
66 * Get the "file extension" that some client apps will estimate from
67 * the currently-requested URL.
68 * This isn't on WebRequest because we need it when things aren't initialized
69 * @private
71 * @return string
73 function wfRequestExtension() {
74 /// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
75 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
76 // Strip the query string...
77 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
78 } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
79 // Probably IIS. QUERY_STRING appears separately.
80 $path = $_SERVER['SCRIPT_NAME'];
81 } else {
82 // Can't get the path from the server? :(
83 return '';
86 $period = strrpos( $path, '.' );
87 if ( $period !== false ) {
88 return strtolower( substr( $path, $period ) );
90 return '';
93 /**
94 * Handler that compresses data with gzip if allowed by the Accept header.
95 * Unlike ob_gzhandler, it works for HEAD requests too.
97 * @param $s string
99 * @return string
101 function wfGzipHandler( $s ) {
102 if ( !function_exists( 'gzencode' ) ) {
103 wfDebug( __FUNCTION__ . "() skipping compression (gzencode unavailable)\n" );
104 return $s;
106 if ( headers_sent() ) {
107 wfDebug( __FUNCTION__ . "() skipping compression (headers already sent)\n" );
108 return $s;
111 $ext = wfRequestExtension();
112 if ( $ext == '.gz' || $ext == '.tgz' ) {
113 // Don't do gzip compression if the URL path ends in .gz or .tgz
114 // This confuses Safari and triggers a download of the page,
115 // even though it's pretty clearly labeled as viewable HTML.
116 // Bad Safari! Bad!
117 return $s;
120 if ( wfClientAcceptsGzip() ) {
121 wfDebug( __FUNCTION__ . "() is compressing output\n" );
122 header( 'Content-Encoding: gzip' );
123 $s = gzencode( $s, 6 );
126 // Set vary header if it hasn't been set already
127 $headers = headers_list();
128 $foundVary = false;
129 foreach ( $headers as $header ) {
130 if ( substr( $header, 0, 5 ) == 'Vary:' ) {
131 $foundVary = true;
132 break;
135 if ( !$foundVary ) {
136 header( 'Vary: Accept-Encoding' );
137 global $wgUseXVO;
138 if ( $wgUseXVO ) {
139 header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
142 return $s;
146 * Mangle flash policy tags which open up the site to XSS attacks.
148 * @param $s string
150 * @return string
152 function wfMangleFlashPolicy( $s ) {
153 # Avoid weird excessive memory usage in PCRE on big articles
154 if ( preg_match( '/\<\s*cross-domain-policy\s*\>/i', $s ) ) {
155 return preg_replace( '/\<\s*cross-domain-policy\s*\>/i', '<NOT-cross-domain-policy>', $s );
156 } else {
157 return $s;
162 * Add a Content-Length header if possible. This makes it cooperate with squid better.
164 * @param $length int
166 function wfDoContentLength( $length ) {
167 if ( !headers_sent() && isset( $_SERVER['SERVER_PROTOCOL'] ) && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
168 header( "Content-Length: $length" );
173 * Replace the output with an error if the HTML is not valid
175 * @param $s string
177 * @return string
179 function wfHtmlValidationHandler( $s ) {
181 $errors = '';
182 if ( MWTidy::checkErrors( $s, $errors ) ) {
183 return $s;
186 header( 'Cache-Control: no-cache' );
188 $out = Html::element( 'h1', null, 'HTML validation error' );
189 $out .= Html::openElement( 'ul' );
191 $error = strtok( $errors, "\n" );
192 $badLines = array();
193 while ( $error !== false ) {
194 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
195 $lineNum = intval( $m[1] );
196 $badLines[$lineNum] = true;
197 $out .= Html::rawElement( 'li', null,
198 Html::element( 'a', array( 'href' => "#line-{$lineNum}" ), $error ) ) . "\n";
200 $error = strtok( "\n" );
203 $out .= Html::closeElement( 'ul' );
204 $out .= Html::element( 'pre', null, $errors );
205 $out .= Html::openElement( 'ol' ) . "\n";
206 $line = strtok( $s, "\n" );
207 $i = 1;
208 while ( $line !== false ) {
209 $attrs = array();
210 if ( isset( $badLines[$i] ) ) {
211 $attrs['class'] = 'highlight';
212 $attrs['id'] = "line-$i";
214 $out .= Html::element( 'li', $attrs, $line ) . "\n";
215 $line = strtok( "\n" );
216 $i++;
218 $out .= Html::closeElement( 'ol' );
220 $style = <<<CSS
221 .highlight { background-color: #ffc }
222 li { white-space: pre }
223 CSS;
225 $out = Html::htmlHeader( array( 'lang' => 'en', 'dir' => 'ltr' ) ) .
226 Html::rawElement( 'head', null,
227 Html::element( 'title', null, 'HTML validation error' ) .
228 Html::inlineStyle( $style ) ) .
229 Html::rawElement( 'body', null, $out ) .
230 Html::closeElement( 'html' );
232 return $out;