Drop support for XHTML 1.0
[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 = Html::element( 'h1', null, 'HTML validation error' );
181 $out .= Html::openElement( 'ul' );
182
183 $error = strtok( $errors, "\n" );
184 $badLines = array();
185 while ( $error !== false ) {
186 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
187 $lineNum = intval( $m[1] );
188 $badLines[$lineNum] = true;
189 $out .= Html::rawElement( 'li', null,
190 Html::element( 'a', array( 'href' => "#line-{$lineNum}" ), $error ) ) . "\n";
191 }
192 $error = strtok( "\n" );
193 }
194
195 $out .= Html::closeElement( 'ul' );
196 $out .= Html::element( 'pre', null, $errors );
197 $out .= Html::openElement( 'ol' ) . "\n";
198 $line = strtok( $s, "\n" );
199 $i = 1;
200 while ( $line !== false ) {
201 $attrs = array();
202 if ( isset( $badLines[$i] ) ) {
203 $attrs['class'] = 'highlight';
204 $attrs['id'] = "line-$i";
205 }
206 $out .= Html::element( 'li', $attrs, $line ) . "\n";
207 $line = strtok( "\n" );
208 $i++;
209 }
210 $out .= Html::closeElement( 'ol' );
211
212 $style = <<<CSS
213 .highlight { background-color: #ffc }
214 li { white-space: pre }
215 CSS;
216
217 $out = Html::htmlHeader( array( 'lang' => 'en', 'dir' => 'ltr' ) ) .
218 Html::rawElement( 'head', null,
219 Html::element( 'title', null, 'HTML validation error' ) .
220 Html::inlineStyle( $style ) ) .
221 Html::rawElement( 'body', null, $out ) .
222 Html::closeElement( 'html' );
223
224 return $out;
225 }