MediaWiki  master
HeaderCallback.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki;
4 
6  private static $headersSentException;
7  private static $messageSent = false;
8 
14  public static function register() {
15  header_register_callback( [ __CLASS__, 'callback' ] );
16  }
17 
21  public static function callback() {
22  // Prevent caching of responses with cookies (T127993)
23  $headers = [];
24  foreach ( headers_list() as $header ) {
25  $header = explode( ':', $header, 2 );
26 
27  // Note: The code below (currently) does not care about value-less headers
28  if ( isset( $header[1] ) ) {
29  $headers[ strtolower( trim( $header[0] ) ) ][] = trim( $header[1] );
30  }
31  }
32 
33  if ( isset( $headers['set-cookie'] ) ) {
34  $cacheControl = isset( $headers['cache-control'] )
35  ? implode( ', ', $headers['cache-control'] )
36  : '';
37 
38  if ( !preg_match( '/(?:^|,)\s*(?:private|no-cache|no-store)\s*(?:$|,)/i',
39  $cacheControl )
40  ) {
41  header( 'Expires: Thu, 01 Jan 1970 00:00:00 GMT' );
42  header( 'Cache-Control: private, max-age=0, s-maxage=0' );
43  \MediaWiki\Logger\LoggerFactory::getInstance( 'cache-cookies' )->warning(
44  'Cookies set on {url} with Cache-Control "{cache-control}"', [
46  'cookies' => $headers['set-cookie'],
47  'cache-control' => $cacheControl ?: '<not set>',
48  ]
49  );
50  }
51  }
52 
53  // Save a backtrace for logging in case it turns out that headers were sent prematurely
54  self::$headersSentException = new \Exception( 'Headers already sent from this point' );
55  }
56 
61  public static function warnIfHeadersSent() {
62  if ( headers_sent() && !self::$messageSent ) {
63  self::$messageSent = true;
64  \MWDebug::warning( 'Headers already sent, should send headers earlier than ' .
65  wfGetCaller( 3 ) );
66  $logger = \MediaWiki\Logger\LoggerFactory::getInstance( 'headers-sent' );
67  $logger->error( 'Warning: headers were already sent from the location below', [
68  'exception' => self::$headersSentException,
69  'detection-trace' => new \Exception( 'Detected here' ),
70  ] );
71  }
72  }
73 }
static getInstance( $channel)
Get a named logger instance from the currently configured logger factory.
A helper class for throttling authentication attempts.
static callback()
The callback, which is called by the transport.
static warning( $msg, $callerOffset=1, $level=E_USER_NOTICE, $log='auto')
Adds a warning entry to the log.
Definition: MWDebug.php:151
static warnIfHeadersSent()
Log a warning message if headers have already been sent.
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
$header
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
static getGlobalRequestURL()
Return the path and query string portion of the main request URI.
Definition: WebRequest.php:796
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