2007-02-19 23:03:37 +00:00
|
|
|
<?php
|
2010-08-14 17:42:40 +00:00
|
|
|
/**
|
|
|
|
|
* Functions to be used with PHP's output buffer
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2007-02-19 23:03:37 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Standard output handler for use with ob_start
|
2011-05-28 17:18:50 +00:00
|
|
|
*
|
|
|
|
|
* @param $s string
|
|
|
|
|
*
|
2011-05-21 19:07:24 +00:00
|
|
|
* @return string
|
2007-02-19 23:03:37 +00:00
|
|
|
*/
|
|
|
|
|
function wfOutputHandler( $s ) {
|
2008-02-18 07:25:35 +00:00
|
|
|
global $wgDisableOutputCompression, $wgValidateAllHtml;
|
2009-11-14 21:27:13 +00:00
|
|
|
$s = wfMangleFlashPolicy( $s );
|
|
|
|
|
if ( $wgValidateAllHtml ) {
|
2008-02-18 07:25:35 +00:00
|
|
|
$headers = apache_response_headers();
|
|
|
|
|
$isHTML = true;
|
|
|
|
|
foreach ( $headers as $name => $value ) {
|
2009-01-24 16:26:05 +00:00
|
|
|
if ( strtolower( $name ) == 'content-type' && strpos( $value, 'text/html' ) === false && strpos( $value, 'application/xhtml+xml' ) === false ) {
|
2008-02-18 07:25:35 +00:00
|
|
|
$isHTML = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $isHTML ) {
|
|
|
|
|
$s = wfHtmlValidationHandler( $s );
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-02-20 04:46:07 +00:00
|
|
|
if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
|
|
|
|
|
if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
|
2007-02-19 23:03:37 +00:00
|
|
|
$s = wfGzipHandler( $s );
|
|
|
|
|
}
|
|
|
|
|
if ( !ini_get( 'output_handler' ) ) {
|
|
|
|
|
wfDoContentLength( strlen( $s ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $s;
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-19 19:06:32 +00:00
|
|
|
/**
|
|
|
|
|
* Get the "file extension" that some client apps will estimate from
|
|
|
|
|
* the currently-requested URL.
|
|
|
|
|
* This isn't on WebRequest because we need it when things aren't initialized
|
|
|
|
|
* @private
|
2011-05-21 19:07:24 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2007-07-19 19:06:32 +00:00
|
|
|
*/
|
|
|
|
|
function wfRequestExtension() {
|
2011-05-17 22:03:20 +00:00
|
|
|
/// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
|
2007-07-19 19:06:32 +00:00
|
|
|
if( isset( $_SERVER['REQUEST_URI'] ) ) {
|
|
|
|
|
// Strip the query string...
|
|
|
|
|
list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
|
|
|
|
|
} elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
|
|
|
|
|
// Probably IIS. QUERY_STRING appears separately.
|
|
|
|
|
$path = $_SERVER['SCRIPT_NAME'];
|
|
|
|
|
} else {
|
|
|
|
|
// Can't get the path from the server? :(
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-19 19:06:32 +00:00
|
|
|
$period = strrpos( $path, '.' );
|
|
|
|
|
if( $period !== false ) {
|
|
|
|
|
return strtolower( substr( $path, $period ) );
|
|
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-19 23:03:37 +00:00
|
|
|
/**
|
|
|
|
|
* Handler that compresses data with gzip if allowed by the Accept header.
|
|
|
|
|
* Unlike ob_gzhandler, it works for HEAD requests too.
|
2011-05-21 19:07:24 +00:00
|
|
|
*
|
|
|
|
|
* @param $s string
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2007-02-19 23:03:37 +00:00
|
|
|
*/
|
|
|
|
|
function wfGzipHandler( $s ) {
|
2007-07-19 19:06:32 +00:00
|
|
|
if( !function_exists( 'gzencode' ) || headers_sent() ) {
|
|
|
|
|
return $s;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-19 19:06:32 +00:00
|
|
|
$ext = wfRequestExtension();
|
|
|
|
|
if( $ext == '.gz' || $ext == '.tgz' ) {
|
|
|
|
|
// Don't do gzip compression if the URL path ends in .gz or .tgz
|
|
|
|
|
// This confuses Safari and triggers a download of the page,
|
|
|
|
|
// even though it's pretty clearly labeled as viewable HTML.
|
|
|
|
|
// Bad Safari! Bad!
|
|
|
|
|
return $s;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-01-14 16:35:35 +00:00
|
|
|
if( wfClientAcceptsGzip() ) {
|
|
|
|
|
header( 'Content-Encoding: gzip' );
|
|
|
|
|
$s = gzencode( $s, 6 );
|
2007-07-19 19:06:32 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-19 19:06:32 +00:00
|
|
|
// Set vary header if it hasn't been set already
|
|
|
|
|
$headers = headers_list();
|
|
|
|
|
$foundVary = false;
|
|
|
|
|
foreach ( $headers as $header ) {
|
|
|
|
|
if ( substr( $header, 0, 5 ) == 'Vary:' ) {
|
|
|
|
|
$foundVary = true;
|
|
|
|
|
break;
|
2007-02-19 23:03:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-07-19 19:06:32 +00:00
|
|
|
if ( !$foundVary ) {
|
|
|
|
|
header( 'Vary: Accept-Encoding' );
|
2010-07-23 05:00:58 +00:00
|
|
|
global $wgUseXVO;
|
|
|
|
|
if ( $wgUseXVO ) {
|
|
|
|
|
header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
|
|
|
|
|
}
|
2007-07-19 19:06:32 +00:00
|
|
|
}
|
2007-02-19 23:03:37 +00:00
|
|
|
return $s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mangle flash policy tags which open up the site to XSS attacks.
|
2011-05-21 19:07:24 +00:00
|
|
|
*
|
|
|
|
|
* @param $s string
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2007-02-19 23:03:37 +00:00
|
|
|
*/
|
|
|
|
|
function wfMangleFlashPolicy( $s ) {
|
2008-05-30 14:58:29 +00:00
|
|
|
# Avoid weird excessive memory usage in PCRE on big articles
|
|
|
|
|
if ( preg_match( '/\<\s*cross-domain-policy\s*\>/i', $s ) ) {
|
|
|
|
|
return preg_replace( '/\<\s*cross-domain-policy\s*\>/i', '<NOT-cross-domain-policy>', $s );
|
|
|
|
|
} else {
|
|
|
|
|
return $s;
|
|
|
|
|
}
|
2007-02-19 23:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add a Content-Length header if possible. This makes it cooperate with squid better.
|
2011-05-21 19:07:24 +00:00
|
|
|
*
|
|
|
|
|
* @param $length int
|
2007-02-19 23:03:37 +00:00
|
|
|
*/
|
|
|
|
|
function wfDoContentLength( $length ) {
|
|
|
|
|
if ( !headers_sent() && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
|
|
|
|
|
header( "Content-Length: $length" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-18 07:25:35 +00:00
|
|
|
/**
|
|
|
|
|
* Replace the output with an error if the HTML is not valid
|
2011-05-21 19:07:24 +00:00
|
|
|
*
|
|
|
|
|
* @param $s string
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2008-02-18 07:25:35 +00:00
|
|
|
*/
|
|
|
|
|
function wfHtmlValidationHandler( $s ) {
|
2009-01-27 15:17:45 +00:00
|
|
|
|
2009-01-27 15:20:31 +00:00
|
|
|
$errors = '';
|
|
|
|
|
if ( MWTidy::checkErrors( $s, $errors ) ) {
|
|
|
|
|
return $s;
|
2008-02-18 07:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
header( 'Cache-Control: no-cache' );
|
|
|
|
|
|
|
|
|
|
$out = <<<EOT
|
|
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
2010-02-11 17:25:10 +00:00
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" dir="ltr">
|
2008-02-18 07:25:35 +00:00
|
|
|
<head>
|
|
|
|
|
<title>HTML validation error</title>
|
|
|
|
|
<style>
|
|
|
|
|
.highlight { background-color: #ffc }
|
|
|
|
|
li { white-space: pre }
|
|
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<h1>HTML validation error</h1>
|
|
|
|
|
<ul>
|
|
|
|
|
EOT;
|
|
|
|
|
|
2009-01-24 16:26:05 +00:00
|
|
|
$error = strtok( $errors, "\n" );
|
2008-02-18 07:25:35 +00:00
|
|
|
$badLines = array();
|
|
|
|
|
while ( $error !== false ) {
|
|
|
|
|
if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
|
|
|
|
|
$lineNum = intval( $m[1] );
|
|
|
|
|
$badLines[$lineNum] = true;
|
|
|
|
|
$out .= "<li><a href=\"#line-{$lineNum}\">" . htmlspecialchars( $error ) . "</a></li>\n";
|
|
|
|
|
}
|
|
|
|
|
$error = strtok( "\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-24 16:26:05 +00:00
|
|
|
$out .= '</ul>';
|
|
|
|
|
$out .= '<pre>' . htmlspecialchars( $errors ) . '</pre>';
|
2009-01-27 15:20:31 +00:00
|
|
|
$out .= "<ol>\n";
|
2008-02-18 07:25:35 +00:00
|
|
|
$line = strtok( $s, "\n" );
|
|
|
|
|
$i = 1;
|
|
|
|
|
while ( $line !== false ) {
|
|
|
|
|
if ( isset( $badLines[$i] ) ) {
|
|
|
|
|
$out .= "<li class=\"highlight\" id=\"line-$i\">";
|
|
|
|
|
} else {
|
|
|
|
|
$out .= '<li>';
|
|
|
|
|
}
|
2009-01-27 15:20:31 +00:00
|
|
|
$out .= htmlspecialchars( $line ) . "</li>\n";
|
2008-02-18 07:25:35 +00:00
|
|
|
$line = strtok( "\n" );
|
|
|
|
|
$i++;
|
|
|
|
|
}
|
|
|
|
|
$out .= '</ol></body></html>';
|
|
|
|
|
return $out;
|
|
|
|
|
}
|