Don't use `phpcs:ignoreFile` to selectively ignore sniffs
[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 namespace MediaWiki;
24
25 use MWTidy;
26 use Html;
27
28 /**
29 * @since 1.31
30 */
31 class OutputHandler {
32 /**
33 * Standard output handler for use with ob_start.
34 *
35 * @param string $s Web response output
36 * @return string
37 */
38 public static function handle( $s ) {
39 global $wgDisableOutputCompression, $wgValidateAllHtml, $wgMangleFlashPolicy;
40 if ( $wgMangleFlashPolicy ) {
41 $s = self::mangleFlashPolicy( $s );
42 }
43 if ( $wgValidateAllHtml ) {
44 $headers = headers_list();
45 $isHTML = false;
46 foreach ( $headers as $header ) {
47 $parts = explode( ':', $header, 2 );
48 if ( count( $parts ) !== 2 ) {
49 continue;
50 }
51 $name = strtolower( trim( $parts[0] ) );
52 $value = trim( $parts[1] );
53 if ( $name == 'content-type' && ( strpos( $value, 'text/html' ) === 0
54 || strpos( $value, 'application/xhtml+xml' ) === 0 )
55 ) {
56 $isHTML = true;
57 break;
58 }
59 }
60 if ( $isHTML ) {
61 $s = self::validateAllHtml( $s );
62 }
63 }
64 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
65 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
66 $s = self::handleGzip( $s );
67 }
68 if ( !ini_get( 'output_handler' ) ) {
69 self::emitContentLength( strlen( $s ) );
70 }
71 }
72 return $s;
73 }
74
75 /**
76 * Get the "file extension" that some client apps will estimate from
77 * the currently-requested URL.
78 *
79 * This isn't a WebRequest method, because we need it before the class loads.
80 * @todo As of 2018, this actually runs after autoloader in Setup.php, so
81 * WebRequest seems like a good place for this.
82 *
83 * @return string
84 */
85 private static function findUriExtension() {
86 /// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
87 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
88 // Strip the query string...
89 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
90 } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
91 // Probably IIS. QUERY_STRING appears separately.
92 $path = $_SERVER['SCRIPT_NAME'];
93 } else {
94 // Can't get the path from the server? :(
95 return '';
96 }
97
98 $period = strrpos( $path, '.' );
99 if ( $period !== false ) {
100 return strtolower( substr( $path, $period ) );
101 }
102 return '';
103 }
104
105 /**
106 * Handler that compresses data with gzip if allowed by the Accept header.
107 *
108 * Unlike ob_gzhandler, it works for HEAD requests too.
109 *
110 * @param string $s Web response output
111 * @return string
112 */
113 private static function handleGzip( $s ) {
114 if ( !function_exists( 'gzencode' ) ) {
115 wfDebug( __METHOD__ . "() skipping compression (gzencode unavailable)\n" );
116 return $s;
117 }
118 if ( headers_sent() ) {
119 wfDebug( __METHOD__ . "() skipping compression (headers already sent)\n" );
120 return $s;
121 }
122
123 $ext = self::findUriExtension();
124 if ( $ext == '.gz' || $ext == '.tgz' ) {
125 // Don't do gzip compression if the URL path ends in .gz or .tgz
126 // This confuses Safari and triggers a download of the page,
127 // even though it's pretty clearly labeled as viewable HTML.
128 // Bad Safari! Bad!
129 return $s;
130 }
131
132 if ( wfClientAcceptsGzip() ) {
133 wfDebug( __METHOD__ . "() is compressing output\n" );
134 header( 'Content-Encoding: gzip' );
135 $s = gzencode( $s, 6 );
136 }
137
138 // Set vary header if it hasn't been set already
139 $headers = headers_list();
140 $foundVary = false;
141 foreach ( $headers as $header ) {
142 $headerName = strtolower( substr( $header, 0, 5 ) );
143 if ( $headerName == 'vary:' ) {
144 $foundVary = true;
145 break;
146 }
147 }
148 if ( !$foundVary ) {
149 header( 'Vary: Accept-Encoding' );
150 global $wgUseKeyHeader;
151 if ( $wgUseKeyHeader ) {
152 header( 'Key: Accept-Encoding;match=gzip' );
153 }
154 }
155 return $s;
156 }
157
158 /**
159 * Mangle flash policy tags which open up the site to XSS attacks.
160 *
161 * @param string $s Web response output
162 * @return string
163 */
164 private static function mangleFlashPolicy( $s ) {
165 # Avoid weird excessive memory usage in PCRE on big articles
166 if ( preg_match( '/\<\s*cross-domain-policy(?=\s|\>)/i', $s ) ) {
167 return preg_replace( '/\<(\s*)(cross-domain-policy(?=\s|\>))/i', '<$1NOT-$2', $s );
168 } else {
169 return $s;
170 }
171 }
172
173 /**
174 * Add a Content-Length header if possible. This makes it cooperate with CDN better.
175 *
176 * @param int $length
177 */
178 private static function emitContentLength( $length ) {
179 if ( !headers_sent()
180 && isset( $_SERVER['SERVER_PROTOCOL'] )
181 && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0'
182 ) {
183 header( "Content-Length: $length" );
184 }
185 }
186
187 /**
188 * Replace the output with an error if the HTML is not valid.
189 *
190 * @param string $s
191 * @return string
192 */
193 private static function validateAllHtml( $s ) {
194 $errors = '';
195 if ( MWTidy::checkErrors( $s, $errors ) ) {
196 return $s;
197 }
198
199 header( 'Cache-Control: no-cache' );
200
201 $out = Html::element( 'h1', null, 'HTML validation error' );
202 $out .= Html::openElement( 'ul' );
203
204 $error = strtok( $errors, "\n" );
205 $badLines = [];
206 while ( $error !== false ) {
207 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
208 $lineNum = intval( $m[1] );
209 $badLines[$lineNum] = true;
210 $out .= Html::rawElement( 'li', null,
211 Html::element( 'a', [ 'href' => "#line-{$lineNum}" ], $error ) ) . "\n";
212 }
213 $error = strtok( "\n" );
214 }
215
216 $out .= Html::closeElement( 'ul' );
217 $out .= Html::element( 'pre', null, $errors );
218 $out .= Html::openElement( 'ol' ) . "\n";
219 $line = strtok( $s, "\n" );
220 $i = 1;
221 while ( $line !== false ) {
222 $attrs = [];
223 if ( isset( $badLines[$i] ) ) {
224 $attrs['class'] = 'highlight';
225 $attrs['id'] = "line-$i";
226 }
227 $out .= Html::element( 'li', $attrs, $line ) . "\n";
228 $line = strtok( "\n" );
229 $i++;
230 }
231 $out .= Html::closeElement( 'ol' );
232
233 $style = <<<CSS
234 .highlight { background-color: #ffc }
235 li { white-space: pre }
236 CSS;
237
238 $out = Html::htmlHeader( [ 'lang' => 'en', 'dir' => 'ltr' ] ) .
239 Html::rawElement( 'head', null,
240 Html::element( 'title', null, 'HTML validation error' ) .
241 Html::inlineStyle( $style ) ) .
242 Html::rawElement( 'body', null, $out ) .
243 Html::closeElement( 'html' );
244
245 return $out;
246 }
247 }