9 private static $headersSentException;
10 private static $messageSent =
false;
19 public static function register() {
25 class_exists( \WebRequest::class );
27 header_register_callback( [ __CLASS__,
'callback' ] );
38 foreach ( headers_list() as
$header ) {
43 $headers[ strtolower( trim(
$header[0] ) ) ][] = trim(
$header[1] );
47 if ( isset( $headers[
'set-cookie'] ) ) {
48 $cacheControl = isset( $headers[
'cache-control'] )
49 ? implode(
', ', $headers[
'cache-control'] )
52 if ( !preg_match(
'/(?:^|,)\s*(?:private|no-cache|no-store)\s*(?:$|,)/i',
55 header(
'Expires: Thu, 01 Jan 1970 00:00:00 GMT' );
56 header(
'Cache-Control: private, max-age=0, s-maxage=0' );
58 'Cookies set on {url} with Cache-Control "{cache-control}"', [
60 'set-cookie' => self::sanitizeSetCookie( $headers[
'set-cookie'] ),
61 'cache-control' => $cacheControl ?:
'<not set>',
69 if ( !isset( $headers[
'x-request-id'] ) ) {
74 self::$headersSentException = new \Exception(
'Headers already sent from this point' );
84 if ( headers_sent() && !self::$messageSent ) {
85 self::$messageSent =
true;
89 $logger->error(
'Warning: headers were already sent from the location below', [
90 'exception' => self::$headersSentException,
91 'detection-trace' =>
new \Exception(
'Detected here' ),
102 $sanitizedValues = [];
103 foreach ( $values as $value ) {
105 $parts = explode(
';', $value );
106 [ $name, $value ] = explode(
'=', $parts[0], 2 );
107 if ( strlen( $value ) > 8 ) {
108 $value = substr( $value, 0, 8 ) .
'...';
109 $parts[0] =
"$name=$value";
111 $sanitizedValues[] = implode(
';', $parts );
113 return implode(
"\n", $sanitizedValues );
117 class_alias( HeaderCallback::class,
'MediaWiki\\HeaderCallback' );
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
static warning( $msg, $callerOffset=1, $level=E_USER_NOTICE, $log='auto')
Adds a warning entry to the log.
static getRequestId()
Get the current request ID.
static getGlobalRequestURL()
Return the path and query string portion of the main request URI.