Properly deprecate srprop=score|hasrelated
[mediawiki.git] / includes / OutputHandler.php
blobb3b3b889a70aef50030ab3dc886d128afc5d07cf
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 string $s
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 string $s
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 string $s
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 int $length
166 function wfDoContentLength( $length ) {
167 if ( !headers_sent()
168 && isset( $_SERVER['SERVER_PROTOCOL'] )
169 && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0'
171 header( "Content-Length: $length" );
176 * Replace the output with an error if the HTML is not valid
178 * @param string $s
180 * @return string
182 function wfHtmlValidationHandler( $s ) {
184 $errors = '';
185 if ( MWTidy::checkErrors( $s, $errors ) ) {
186 return $s;
189 header( 'Cache-Control: no-cache' );
191 $out = Html::element( 'h1', null, 'HTML validation error' );
192 $out .= Html::openElement( 'ul' );
194 $error = strtok( $errors, "\n" );
195 $badLines = array();
196 while ( $error !== false ) {
197 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
198 $lineNum = intval( $m[1] );
199 $badLines[$lineNum] = true;
200 $out .= Html::rawElement( 'li', null,
201 Html::element( 'a', array( 'href' => "#line-{$lineNum}" ), $error ) ) . "\n";
203 $error = strtok( "\n" );
206 $out .= Html::closeElement( 'ul' );
207 $out .= Html::element( 'pre', null, $errors );
208 $out .= Html::openElement( 'ol' ) . "\n";
209 $line = strtok( $s, "\n" );
210 $i = 1;
211 while ( $line !== false ) {
212 $attrs = array();
213 if ( isset( $badLines[$i] ) ) {
214 $attrs['class'] = 'highlight';
215 $attrs['id'] = "line-$i";
217 $out .= Html::element( 'li', $attrs, $line ) . "\n";
218 $line = strtok( "\n" );
219 $i++;
221 $out .= Html::closeElement( 'ol' );
223 $style = <<<CSS
224 .highlight { background-color: #ffc }
225 li { white-space: pre }
226 CSS;
228 $out = Html::htmlHeader( array( 'lang' => 'en', 'dir' => 'ltr' ) ) .
229 Html::rawElement( 'head', null,
230 Html::element( 'title', null, 'HTML validation error' ) .
231 Html::inlineStyle( $style ) ) .
232 Html::rawElement( 'body', null, $out ) .
233 Html::closeElement( 'html' );
235 return $out;