MediaWiki REL1_31
HeaderCallback.php
Go to the documentation of this file.
1<?php
2
3namespace 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 list( $name, $value ) = explode( ':', $header, 2 );
26 $headers[strtolower( trim( $name ) )][] = trim( $value );
27 }
28
29 if ( isset( $headers['set-cookie'] ) ) {
30 $cacheControl = isset( $headers['cache-control'] )
31 ? implode( ', ', $headers['cache-control'] )
32 : '';
33
34 if ( !preg_match( '/(?:^|,)\s*(?:private|no-cache|no-store)\s*(?:$|,)/i',
35 $cacheControl )
36 ) {
37 header( 'Expires: Thu, 01 Jan 1970 00:00:00 GMT' );
38 header( 'Cache-Control: private, max-age=0, s-maxage=0' );
39 \MediaWiki\Logger\LoggerFactory::getInstance( 'cache-cookies' )->warning(
40 'Cookies set on {url} with Cache-Control "{cache-control}"', [
41 'url' => \WebRequest::getGlobalRequestURL(),
42 'cookies' => $headers['set-cookie'],
43 'cache-control' => $cacheControl ?: '<not set>',
44 ]
45 );
46 }
47 }
48
49 // Save a backtrace for logging in case it turns out that headers were sent prematurely
50 self::$headersSentException = new \Exception( 'Headers already sent from this point' );
51 }
52
57 public static function warnIfHeadersSent() {
58 if ( headers_sent() && !self::$messageSent ) {
59 self::$messageSent = true;
60 \MWDebug::warning( 'Headers already sent, should send headers earlier than ' .
61 wfGetCaller( 3 ) );
62 $logger = \MediaWiki\Logger\LoggerFactory::getInstance( 'headers-sent' );
63 $logger->error( 'Warning: headers were already sent from the location below', [
64 'exception' => self::$headersSentException,
65 'detection-trace' => new \Exception( 'Detected here' ),
66 ] );
67 }
68 }
69}
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
static warnIfHeadersSent()
Log a warning message if headers have already been sent.
static callback()
The callback, which is called by the transport.
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
A helper class for throttling authentication attempts.
$header