* Moved the main ob_start() from the default LocalSettings.php to WebStart.php.
[lhc/web/wiklou.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 ( !ini_get( 'zlib.output_compression' ) ) {
10 if ( $wgDisableOutputCompression || !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 * Handler that compresses data with gzip if allowed by the Accept header.
22 * Unlike ob_gzhandler, it works for HEAD requests too.
23 */
24 function wfGzipHandler( $s ) {
25 if ( $s !== '' && function_exists( 'gzencode' ) && !headers_sent() ) {
26 $tokens = preg_split( '/[,; ]/', $_SERVER['HTTP_ACCEPT_ENCODING'] );
27 if ( in_array( 'gzip', $tokens ) ) {
28 header( 'Content-Encoding: gzip' );
29 $s = gzencode( $s, 3 );
30
31 # Set vary header if it hasn't been set already
32 $headers = headers_list();
33 $foundVary = false;
34 foreach ( $headers as $header ) {
35 if ( substr( $header, 0, 5 ) == 'Vary:' ) {
36 $foundVary == true;
37 break;
38 }
39 }
40 if ( !$foundVary ) {
41 header( 'Vary: Accept-Encoding' );
42 }
43 }
44 }
45 return $s;
46 }
47
48 /**
49 * Mangle flash policy tags which open up the site to XSS attacks.
50 */
51 function wfMangleFlashPolicy( $s ) {
52 return preg_replace( '/\<\s*cross-domain-policy\s*\>/i', '<NOT-cross-domain-policy>', $s );
53 }
54
55 /**
56 * Add a Content-Length header if possible. This makes it cooperate with squid better.
57 */
58 function wfDoContentLength( $length ) {
59 if ( !headers_sent() && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
60 header( "Content-Length: $length" );
61 }
62 }
63
64 ?>