(bug 19441) Updated date formatting for Lithuanian
[mediawiki.git] / includes / OutputHandler.php
blob1f4798b7cc237dfdf3fc9de1c45b405585c5ac84
1 <?php
3 /**
4 * Standard output handler for use with ob_start
5 */
6 function wfOutputHandler( $s ) {
7 global $wgDisableOutputCompression, $wgValidateAllHtml;
8 $s = wfMangleFlashPolicy( $s );
9 if ( $wgValidateAllHtml ) {
10 $headers = apache_response_headers();
11 $isHTML = true;
12 foreach ( $headers as $name => $value ) {
13 if ( strtolower( $name ) == 'content-type' && strpos( $value, 'text/html' ) === false && strpos( $value, 'application/xhtml+xml' ) === false ) {
14 $isHTML = false;
15 break;
18 if ( $isHTML ) {
19 $s = wfHtmlValidationHandler( $s );
22 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
23 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
24 $s = wfGzipHandler( $s );
26 if ( !ini_get( 'output_handler' ) ) {
27 wfDoContentLength( strlen( $s ) );
30 return $s;
33 /**
34 * Get the "file extension" that some client apps will estimate from
35 * the currently-requested URL.
36 * This isn't on WebRequest because we need it when things aren't initialized
37 * @private
39 function wfRequestExtension() {
40 /// @fixme -- this sort of dupes some code in WebRequest::getRequestUrl()
41 if( isset( $_SERVER['REQUEST_URI'] ) ) {
42 // Strip the query string...
43 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
44 } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
45 // Probably IIS. QUERY_STRING appears separately.
46 $path = $_SERVER['SCRIPT_NAME'];
47 } else {
48 // Can't get the path from the server? :(
49 return '';
52 $period = strrpos( $path, '.' );
53 if( $period !== false ) {
54 return strtolower( substr( $path, $period ) );
56 return '';
59 /**
60 * Handler that compresses data with gzip if allowed by the Accept header.
61 * Unlike ob_gzhandler, it works for HEAD requests too.
63 function wfGzipHandler( $s ) {
64 if( !function_exists( 'gzencode' ) || headers_sent() ) {
65 return $s;
68 $ext = wfRequestExtension();
69 if( $ext == '.gz' || $ext == '.tgz' ) {
70 // Don't do gzip compression if the URL path ends in .gz or .tgz
71 // This confuses Safari and triggers a download of the page,
72 // even though it's pretty clearly labeled as viewable HTML.
73 // Bad Safari! Bad!
74 return $s;
77 if( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
78 $tokens = preg_split( '/[,; ]/', $_SERVER['HTTP_ACCEPT_ENCODING'] );
79 if ( in_array( 'gzip', $tokens ) ) {
80 header( 'Content-Encoding: gzip' );
81 $s = gzencode( $s, 3 );
85 // Set vary header if it hasn't been set already
86 $headers = headers_list();
87 $foundVary = false;
88 foreach ( $headers as $header ) {
89 if ( substr( $header, 0, 5 ) == 'Vary:' ) {
90 $foundVary = true;
91 break;
94 if ( !$foundVary ) {
95 header( 'Vary: Accept-Encoding' );
96 header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
98 return $s;
102 * Mangle flash policy tags which open up the site to XSS attacks.
104 function wfMangleFlashPolicy( $s ) {
105 # Avoid weird excessive memory usage in PCRE on big articles
106 if ( preg_match( '/\<\s*cross-domain-policy\s*\>/i', $s ) ) {
107 return preg_replace( '/\<\s*cross-domain-policy\s*\>/i', '<NOT-cross-domain-policy>', $s );
108 } else {
109 return $s;
114 * Add a Content-Length header if possible. This makes it cooperate with squid better.
116 function wfDoContentLength( $length ) {
117 if ( !headers_sent() && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
118 header( "Content-Length: $length" );
123 * Replace the output with an error if the HTML is not valid
125 function wfHtmlValidationHandler( $s ) {
127 $errors = '';
128 if ( MWTidy::checkErrors( $s, $errors ) ) {
129 return $s;
132 header( 'Cache-Control: no-cache' );
134 $out = <<<EOT
135 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
136 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
137 <head>
138 <title>HTML validation error</title>
139 <style>
140 .highlight { background-color: #ffc }
141 li { white-space: pre }
142 </style>
143 </head>
144 <body>
145 <h1>HTML validation error</h1>
146 <ul>
147 EOT;
149 $error = strtok( $errors, "\n" );
150 $badLines = array();
151 while ( $error !== false ) {
152 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
153 $lineNum = intval( $m[1] );
154 $badLines[$lineNum] = true;
155 $out .= "<li><a href=\"#line-{$lineNum}\">" . htmlspecialchars( $error ) . "</a></li>\n";
157 $error = strtok( "\n" );
160 $out .= '</ul>';
161 $out .= '<pre>' . htmlspecialchars( $errors ) . '</pre>';
162 $out .= "<ol>\n";
163 $line = strtok( $s, "\n" );
164 $i = 1;
165 while ( $line !== false ) {
166 if ( isset( $badLines[$i] ) ) {
167 $out .= "<li class=\"highlight\" id=\"line-$i\">";
168 } else {
169 $out .= '<li>';
171 $out .= htmlspecialchars( $line ) . "</li>\n";
172 $line = strtok( "\n" );
173 $i++;
175 $out .= '</ol></body></html>';
176 return $out;