More adding, updating and tweaking of documentation
[lhc/web/wiklou.git] / includes / OutputHandler.php
1 <?php
2 /**
3 * Functions to be used with PHP's output buffer
4 *
5 * @file
6 */
7
8 /**
9 * Standard output handler for use with ob_start
10 *
11 * @return string
12 */
13 function wfOutputHandler( $s ) {
14 global $wgDisableOutputCompression, $wgValidateAllHtml;
15 $s = wfMangleFlashPolicy( $s );
16 if ( $wgValidateAllHtml ) {
17 $headers = apache_response_headers();
18 $isHTML = true;
19 foreach ( $headers as $name => $value ) {
20 if ( strtolower( $name ) == 'content-type' && strpos( $value, 'text/html' ) === false && strpos( $value, 'application/xhtml+xml' ) === false ) {
21 $isHTML = false;
22 break;
23 }
24 }
25 if ( $isHTML ) {
26 $s = wfHtmlValidationHandler( $s );
27 }
28 }
29 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
30 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
31 $s = wfGzipHandler( $s );
32 }
33 if ( !ini_get( 'output_handler' ) ) {
34 wfDoContentLength( strlen( $s ) );
35 }
36 }
37 return $s;
38 }
39
40 /**
41 * Get the "file extension" that some client apps will estimate from
42 * the currently-requested URL.
43 * This isn't on WebRequest because we need it when things aren't initialized
44 * @private
45 *
46 * @return string
47 */
48 function wfRequestExtension() {
49 /// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
50 if( isset( $_SERVER['REQUEST_URI'] ) ) {
51 // Strip the query string...
52 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
53 } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
54 // Probably IIS. QUERY_STRING appears separately.
55 $path = $_SERVER['SCRIPT_NAME'];
56 } else {
57 // Can't get the path from the server? :(
58 return '';
59 }
60
61 $period = strrpos( $path, '.' );
62 if( $period !== false ) {
63 return strtolower( substr( $path, $period ) );
64 }
65 return '';
66 }
67
68 /**
69 * Handler that compresses data with gzip if allowed by the Accept header.
70 * Unlike ob_gzhandler, it works for HEAD requests too.
71 *
72 * @param $s string
73 *
74 * @return string
75 */
76 function wfGzipHandler( $s ) {
77 if( !function_exists( 'gzencode' ) || headers_sent() ) {
78 return $s;
79 }
80
81 $ext = wfRequestExtension();
82 if( $ext == '.gz' || $ext == '.tgz' ) {
83 // Don't do gzip compression if the URL path ends in .gz or .tgz
84 // This confuses Safari and triggers a download of the page,
85 // even though it's pretty clearly labeled as viewable HTML.
86 // Bad Safari! Bad!
87 return $s;
88 }
89
90 if( wfClientAcceptsGzip() ) {
91 header( 'Content-Encoding: gzip' );
92 $s = gzencode( $s, 6 );
93 }
94
95 // Set vary header if it hasn't been set already
96 $headers = headers_list();
97 $foundVary = false;
98 foreach ( $headers as $header ) {
99 if ( substr( $header, 0, 5 ) == 'Vary:' ) {
100 $foundVary = true;
101 break;
102 }
103 }
104 if ( !$foundVary ) {
105 header( 'Vary: Accept-Encoding' );
106 global $wgUseXVO;
107 if ( $wgUseXVO ) {
108 header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
109 }
110 }
111 return $s;
112 }
113
114 /**
115 * Mangle flash policy tags which open up the site to XSS attacks.
116 *
117 * @param $s string
118 *
119 * @return string
120 */
121 function wfMangleFlashPolicy( $s ) {
122 # Avoid weird excessive memory usage in PCRE on big articles
123 if ( preg_match( '/\<\s*cross-domain-policy\s*\>/i', $s ) ) {
124 return preg_replace( '/\<\s*cross-domain-policy\s*\>/i', '<NOT-cross-domain-policy>', $s );
125 } else {
126 return $s;
127 }
128 }
129
130 /**
131 * Add a Content-Length header if possible. This makes it cooperate with squid better.
132 *
133 * @param $length int
134 */
135 function wfDoContentLength( $length ) {
136 if ( !headers_sent() && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
137 header( "Content-Length: $length" );
138 }
139 }
140
141 /**
142 * Replace the output with an error if the HTML is not valid
143 *
144 * @param $s string
145 *
146 * @return string
147 */
148 function wfHtmlValidationHandler( $s ) {
149
150 $errors = '';
151 if ( MWTidy::checkErrors( $s, $errors ) ) {
152 return $s;
153 }
154
155 header( 'Cache-Control: no-cache' );
156
157 $out = <<<EOT
158 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
159 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" dir="ltr">
160 <head>
161 <title>HTML validation error</title>
162 <style>
163 .highlight { background-color: #ffc }
164 li { white-space: pre }
165 </style>
166 </head>
167 <body>
168 <h1>HTML validation error</h1>
169 <ul>
170 EOT;
171
172 $error = strtok( $errors, "\n" );
173 $badLines = array();
174 while ( $error !== false ) {
175 if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
176 $lineNum = intval( $m[1] );
177 $badLines[$lineNum] = true;
178 $out .= "<li><a href=\"#line-{$lineNum}\">" . htmlspecialchars( $error ) . "</a></li>\n";
179 }
180 $error = strtok( "\n" );
181 }
182
183 $out .= '</ul>';
184 $out .= '<pre>' . htmlspecialchars( $errors ) . '</pre>';
185 $out .= "<ol>\n";
186 $line = strtok( $s, "\n" );
187 $i = 1;
188 while ( $line !== false ) {
189 if ( isset( $badLines[$i] ) ) {
190 $out .= "<li class=\"highlight\" id=\"line-$i\">";
191 } else {
192 $out .= '<li>';
193 }
194 $out .= htmlspecialchars( $line ) . "</li>\n";
195 $line = strtok( "\n" );
196 $i++;
197 }
198 $out .= '</ol></body></html>';
199 return $out;
200 }