]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/OutputHandler.php
MediaWiki 1.11.0
[autoinstalls/mediawiki.git] / includes / OutputHandler.php
1 <?php
2
3 /**
4  * Standard output handler for use with ob_start
5  */
6 function wfOutputHandler( $s ) {
7         global $wgDisableOutputCompression;
8         $s = wfMangleFlashPolicy( $s );
9         if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
10                 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
11                         $s = wfGzipHandler( $s );
12                 }
13                 if ( !ini_get( 'output_handler' ) ) {
14                         wfDoContentLength( strlen( $s ) );
15                 }
16         }
17         return $s;
18 }
19
20 /**
21  * Get the "file extension" that some client apps will estimate from
22  * the currently-requested URL.
23  * This isn't on WebRequest because we need it when things aren't initialized
24  * @private
25  */
26 function wfRequestExtension() {
27         /// @fixme -- this sort of dupes some code in WebRequest::getRequestUrl()
28         if( isset( $_SERVER['REQUEST_URI'] ) ) {
29                 // Strip the query string...
30                 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
31         } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
32                 // Probably IIS. QUERY_STRING appears separately.
33                 $path = $_SERVER['SCRIPT_NAME'];
34         } else {
35                 // Can't get the path from the server? :(
36                 return '';
37         }
38         
39         $period = strrpos( $path, '.' );
40         if( $period !== false ) {
41                 return strtolower( substr( $path, $period ) );
42         }
43         return '';
44 }
45
46 /**
47  * Handler that compresses data with gzip if allowed by the Accept header.
48  * Unlike ob_gzhandler, it works for HEAD requests too.
49  */
50 function wfGzipHandler( $s ) {
51         if( !function_exists( 'gzencode' ) || headers_sent() ) {
52                 return $s;
53         }
54         
55         $ext = wfRequestExtension();
56         if( $ext == '.gz' || $ext == '.tgz' ) {
57                 // Don't do gzip compression if the URL path ends in .gz or .tgz
58                 // This confuses Safari and triggers a download of the page,
59                 // even though it's pretty clearly labeled as viewable HTML.
60                 // Bad Safari! Bad!
61                 return $s;
62         }
63         
64         $tokens = preg_split( '/[,; ]/', $_SERVER['HTTP_ACCEPT_ENCODING'] );
65         if ( in_array( 'gzip', $tokens ) ) {
66                 header( 'Content-Encoding: gzip' );
67                 $s = gzencode( $s, 3 );
68         }
69         
70         // Set vary header if it hasn't been set already
71         $headers = headers_list();
72         $foundVary = false;
73         foreach ( $headers as $header ) {
74                 if ( substr( $header, 0, 5 ) == 'Vary:' ) {
75                         $foundVary = true;
76                         break;
77                 }
78         }
79         if ( !$foundVary ) {
80                 header( 'Vary: Accept-Encoding' );
81         }
82         return $s;
83 }
84
85 /**
86  * Mangle flash policy tags which open up the site to XSS attacks.
87  */
88 function wfMangleFlashPolicy( $s ) {
89         return preg_replace( '/\<\s*cross-domain-policy\s*\>/i', '<NOT-cross-domain-policy>', $s );
90 }
91
92 /**
93  * Add a Content-Length header if possible. This makes it cooperate with squid better.
94  */
95 function wfDoContentLength( $length ) {
96         if ( !headers_sent() && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
97                 header( "Content-Length: $length" );
98         }
99 }
100
101