Merge "Use MediaWiki\SuppressWarnings around trigger_error('') instead @"
[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 /**
26 * @since 1.31
27 */
28 class OutputHandler {
29 /**
30 * Standard output handler for use with ob_start.
31 *
32 * @param string $s Web response output
33 * @return string
34 */
35 public static function handle( $s ) {
36 global $wgDisableOutputCompression, $wgMangleFlashPolicy;
37 if ( $wgMangleFlashPolicy ) {
38 $s = self::mangleFlashPolicy( $s );
39 }
40 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
41 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
42 $s = self::handleGzip( $s );
43 }
44 if ( !ini_get( 'output_handler' ) ) {
45 self::emitContentLength( strlen( $s ) );
46 }
47 }
48 return $s;
49 }
50
51 /**
52 * Get the "file extension" that some client apps will estimate from
53 * the currently-requested URL.
54 *
55 * This isn't a WebRequest method, because we need it before the class loads.
56 * @todo As of 2018, this actually runs after autoloader in Setup.php, so
57 * WebRequest seems like a good place for this.
58 *
59 * @return string
60 */
61 private static function findUriExtension() {
62 /// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
63 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
64 // Strip the query string...
65 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
66 } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
67 // Probably IIS. QUERY_STRING appears separately.
68 $path = $_SERVER['SCRIPT_NAME'];
69 } else {
70 // Can't get the path from the server? :(
71 return '';
72 }
73
74 $period = strrpos( $path, '.' );
75 if ( $period !== false ) {
76 return strtolower( substr( $path, $period ) );
77 }
78 return '';
79 }
80
81 /**
82 * Handler that compresses data with gzip if allowed by the Accept header.
83 *
84 * Unlike ob_gzhandler, it works for HEAD requests too.
85 *
86 * @param string $s Web response output
87 * @return string
88 */
89 private static function handleGzip( $s ) {
90 if ( !function_exists( 'gzencode' ) ) {
91 wfDebug( __METHOD__ . "() skipping compression (gzencode unavailable)\n" );
92 return $s;
93 }
94 if ( headers_sent() ) {
95 wfDebug( __METHOD__ . "() skipping compression (headers already sent)\n" );
96 return $s;
97 }
98
99 $ext = self::findUriExtension();
100 if ( $ext == '.gz' || $ext == '.tgz' ) {
101 // Don't do gzip compression if the URL path ends in .gz or .tgz
102 // This confuses Safari and triggers a download of the page,
103 // even though it's pretty clearly labeled as viewable HTML.
104 // Bad Safari! Bad!
105 return $s;
106 }
107
108 if ( wfClientAcceptsGzip() ) {
109 wfDebug( __METHOD__ . "() is compressing output\n" );
110 header( 'Content-Encoding: gzip' );
111 $s = gzencode( $s, 6 );
112 }
113
114 // Set vary header if it hasn't been set already
115 $headers = headers_list();
116 $foundVary = false;
117 foreach ( $headers as $header ) {
118 $headerName = strtolower( substr( $header, 0, 5 ) );
119 if ( $headerName == 'vary:' ) {
120 $foundVary = true;
121 break;
122 }
123 }
124 if ( !$foundVary ) {
125 header( 'Vary: Accept-Encoding' );
126 global $wgUseKeyHeader;
127 if ( $wgUseKeyHeader ) {
128 header( 'Key: Accept-Encoding;match=gzip' );
129 }
130 }
131 return $s;
132 }
133
134 /**
135 * Mangle flash policy tags which open up the site to XSS attacks.
136 *
137 * @param string $s Web response output
138 * @return string
139 */
140 private static function mangleFlashPolicy( $s ) {
141 # Avoid weird excessive memory usage in PCRE on big articles
142 if ( preg_match( '/\<\s*cross-domain-policy(?=\s|\>)/i', $s ) ) {
143 return preg_replace( '/\<(\s*)(cross-domain-policy(?=\s|\>))/i', '<$1NOT-$2', $s );
144 } else {
145 return $s;
146 }
147 }
148
149 /**
150 * Add a Content-Length header if possible. This makes it cooperate with CDN better.
151 *
152 * @param int $length
153 */
154 private static function emitContentLength( $length ) {
155 if ( !headers_sent()
156 && isset( $_SERVER['SERVER_PROTOCOL'] )
157 && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0'
158 ) {
159 header( "Content-Length: $length" );
160 }
161 }
162 }