* Introduced OutputPage::addWikiMsg() and OutputPage::wrapWikiMsg(), to make it easie...
[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, $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 ) {
14 $isHTML = false;
15 break;
16 }
17 }
18 if ( $isHTML ) {
19 $s = wfHtmlValidationHandler( $s );
20 }
21 }
22 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
23 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
24 $s = wfGzipHandler( $s );
25 }
26 if ( !ini_get( 'output_handler' ) ) {
27 wfDoContentLength( strlen( $s ) );
28 }
29 }
30 return $s;
31 }
32
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
38 */
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 '';
50 }
51
52 $period = strrpos( $path, '.' );
53 if( $period !== false ) {
54 return strtolower( substr( $path, $period ) );
55 }
56 return '';
57 }
58
59 /**
60 * Handler that compresses data with gzip if allowed by the Accept header.
61 * Unlike ob_gzhandler, it works for HEAD requests too.
62 */
63 function wfGzipHandler( $s ) {
64 if( !function_exists( 'gzencode' ) || headers_sent() ) {
65 return $s;
66 }
67
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;
75 }
76
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 );
82 }
83 }
84
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;
92 }
93 }
94 if ( !$foundVary ) {
95 header( 'Vary: Accept-Encoding' );
96 header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
97 }
98 return $s;
99 }
100
101 /**
102 * Mangle flash policy tags which open up the site to XSS attacks.
103 */
104 function wfMangleFlashPolicy( $s ) {
105 return preg_replace( '/\<\s*cross-domain-policy\s*\>/i', '<NOT-cross-domain-policy>', $s );
106 }
107
108 /**
109 * Add a Content-Length header if possible. This makes it cooperate with squid better.
110 */
111 function wfDoContentLength( $length ) {
112 if ( !headers_sent() && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
113 header( "Content-Length: $length" );
114 }
115 }
116
117 /**
118 * Replace the output with an error if the HTML is not valid
119 */
120 function wfHtmlValidationHandler( $s ) {
121 global $IP;
122 $tidy = new tidy;
123 $tidy->parseString( $s, "$IP/includes/tidy.conf", 'utf8' );
124 if ( $tidy->getStatus() == 0 ) {
125 return $s;
126 }
127
128 header( 'Cache-Control: no-cache' );
129
130 $out = <<<EOT
131 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
132 <html>
133 <head>
134 <title>HTML validation error</title>
135 <style>
136 .highlight { background-color: #ffc }
137 li { white-space: pre }
138 </style>
139 </head>
140 <body>
141 <h1>HTML validation error</h1>
142 <ul>
143 EOT;
144
145 $error = strtok( $tidy->errorBuffer, "\n" );
146 $badLines = array();
147 while ( $error !== false ) {
148 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
149 $lineNum = intval( $m[1] );
150 $badLines[$lineNum] = true;
151 $out .= "<li><a href=\"#line-{$lineNum}\">" . htmlspecialchars( $error ) . "</a></li>\n";
152 }
153 $error = strtok( "\n" );
154 }
155
156 $out .= '<pre>' . htmlspecialchars( $tidy->errorBuffer ) . '</pre>';
157 $out .= '<ol>';
158 $line = strtok( $s, "\n" );
159 $i = 1;
160 while ( $line !== false ) {
161 if ( isset( $badLines[$i] ) ) {
162 $out .= "<li class=\"highlight\" id=\"line-$i\">";
163 } else {
164 $out .= '<li>';
165 }
166 $out .= htmlspecialchars( $line ) . '</li>';
167 $line = strtok( "\n" );
168 $i++;
169 }
170 $out .= '</ol></body></html>';
171 return $out;
172 }
173