MediaWiki  1.33.0
OutputHandler.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki;
24 
35  public static function handle( $s ) {
37  if ( $wgMangleFlashPolicy ) {
38  $s = self::mangleFlashPolicy( $s );
39  }
40  if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
41  if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
42  $s = self::handleGzip( $s );
43  }
44  if ( !ini_get( 'output_handler' ) ) {
45  self::emitContentLength( strlen( $s ) );
46  }
47  }
48  return $s;
49  }
50 
61  private static function findUriExtension() {
63  if ( isset( $_SERVER['REQUEST_URI'] ) ) {
64  // Strip the query string...
65  list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
66  } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
67  // Probably IIS. QUERY_STRING appears separately.
68  $path = $_SERVER['SCRIPT_NAME'];
69  } else {
70  // Can't get the path from the server? :(
71  return '';
72  }
73 
74  $period = strrpos( $path, '.' );
75  if ( $period !== false ) {
76  return strtolower( substr( $path, $period ) );
77  }
78  return '';
79  }
80 
89  private static function handleGzip( $s ) {
90  if ( !function_exists( 'gzencode' ) ) {
91  wfDebug( __METHOD__ . "() skipping compression (gzencode unavailable)\n" );
92  return $s;
93  }
94  if ( headers_sent() ) {
95  wfDebug( __METHOD__ . "() skipping compression (headers already sent)\n" );
96  return $s;
97  }
98 
99  $ext = self::findUriExtension();
100  if ( $ext == '.gz' || $ext == '.tgz' ) {
101  // Don't do gzip compression if the URL path ends in .gz or .tgz
102  // This confuses Safari and triggers a download of the page,
103  // even though it's pretty clearly labeled as viewable HTML.
104  // Bad Safari! Bad!
105  return $s;
106  }
107 
108  if ( wfClientAcceptsGzip() ) {
109  wfDebug( __METHOD__ . "() is compressing output\n" );
110  header( 'Content-Encoding: gzip' );
111  $s = gzencode( $s, 6 );
112  }
113 
114  // Set vary header if it hasn't been set already
115  $headers = headers_list();
116  $foundVary = false;
117  foreach ( $headers as $header ) {
118  $headerName = strtolower( substr( $header, 0, 5 ) );
119  if ( $headerName == 'vary:' ) {
120  $foundVary = true;
121  break;
122  }
123  }
124  if ( !$foundVary ) {
125  header( 'Vary: Accept-Encoding' );
126  global $wgUseKeyHeader;
127  if ( $wgUseKeyHeader ) {
128  header( 'Key: Accept-Encoding;match=gzip' );
129  }
130  }
131  return $s;
132  }
133 
140  private static function mangleFlashPolicy( $s ) {
141  # Avoid weird excessive memory usage in PCRE on big articles
142  if ( preg_match( '/<\s*cross-domain-policy(?=\s|>)/i', $s ) ) {
143  return preg_replace( '/<(\s*)(cross-domain-policy(?=\s|>))/i', '<$1NOT-$2', $s );
144  } else {
145  return $s;
146  }
147  }
148 
154  private static function emitContentLength( $length ) {
155  if ( !headers_sent()
156  && isset( $_SERVER['SERVER_PROTOCOL'] )
157  && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0'
158  ) {
159  header( "Content-Length: $length" );
160  }
161  }
162 }
MediaWiki\OutputHandler
Definition: OutputHandler.php:28
MediaWiki\OutputHandler\findUriExtension
static findUriExtension()
Get the "file extension" that some client apps will estimate from the currently-requested URL.
Definition: OutputHandler.php:61
$wgMangleFlashPolicy
$wgMangleFlashPolicy
When OutputHandler is used, mangle any output that contains <cross-domain-policy>.
Definition: DefaultSettings.php:3537
MediaWiki\OutputHandler\handleGzip
static handleGzip( $s)
Handler that compresses data with gzip if allowed by the Accept header.
Definition: OutputHandler.php:89
$s
$s
Definition: mergeMessageFileList.php:186
MediaWiki\OutputHandler\emitContentLength
static emitContentLength( $length)
Add a Content-Length header if possible.
Definition: OutputHandler.php:154
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\OutputHandler\handle
static handle( $s)
Standard output handler for use with ob_start.
Definition: OutputHandler.php:35
$wgUseKeyHeader
$wgUseKeyHeader
Send the Key HTTP header for better caching.
Definition: DefaultSettings.php:2722
MediaWiki
A helper class for throttling authentication attempts.
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:949
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
wfClientAcceptsGzip
wfClientAcceptsGzip( $force=false)
Whether the client accept gzip encoding.
Definition: GlobalFunctions.php:1542
$header
$header
Definition: updateCredits.php:41
$wgDisableOutputCompression
$wgDisableOutputCompression
Disable output compression (enabled by default if zlib is available)
Definition: DefaultSettings.php:3396
$path
$path
Definition: NoLocalSettings.php:25
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48
MediaWiki\OutputHandler\mangleFlashPolicy
static mangleFlashPolicy( $s)
Mangle flash policy tags which open up the site to XSS attacks.
Definition: OutputHandler.php:140