HeaderCallback.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace MediaWiki;
  3. class HeaderCallback {
  4. private static $headersSentException;
  5. private static $messageSent = false;
  6. /**
  7. * Register a callback to be called when headers are sent. There can only
  8. * be one of these handlers active, so all relevant actions have to be in
  9. * here.
  10. */
  11. public static function register() {
  12. header_register_callback( [ __CLASS__, 'callback' ] );
  13. }
  14. /**
  15. * The callback, which is called by the transport
  16. */
  17. public static function callback() {
  18. // Prevent caching of responses with cookies (T127993)
  19. $headers = [];
  20. foreach ( headers_list() as $header ) {
  21. list( $name, $value ) = explode( ':', $header, 2 );
  22. $headers[strtolower( trim( $name ) )][] = trim( $value );
  23. }
  24. if ( isset( $headers['set-cookie'] ) ) {
  25. $cacheControl = isset( $headers['cache-control'] )
  26. ? implode( ', ', $headers['cache-control'] )
  27. : '';
  28. if ( !preg_match( '/(?:^|,)\s*(?:private|no-cache|no-store)\s*(?:$|,)/i',
  29. $cacheControl )
  30. ) {
  31. header( 'Expires: Thu, 01 Jan 1970 00:00:00 GMT' );
  32. header( 'Cache-Control: private, max-age=0, s-maxage=0' );
  33. \MediaWiki\Logger\LoggerFactory::getInstance( 'cache-cookies' )->warning(
  34. 'Cookies set on {url} with Cache-Control "{cache-control}"', [
  35. 'url' => \WebRequest::getGlobalRequestURL(),
  36. 'cookies' => $headers['set-cookie'],
  37. 'cache-control' => $cacheControl ?: '<not set>',
  38. ]
  39. );
  40. }
  41. }
  42. // Save a backtrace for logging in case it turns out that headers were sent prematurely
  43. self::$headersSentException = new \Exception( 'Headers already sent from this point' );
  44. }
  45. /**
  46. * Log a warning message if headers have already been sent. This can be
  47. * called before flushing the output.
  48. */
  49. public static function warnIfHeadersSent() {
  50. if ( headers_sent() && !self::$messageSent ) {
  51. self::$messageSent = true;
  52. \MWDebug::warning( 'Headers already sent, should send headers earlier than ' .
  53. wfGetCaller( 3 ) );
  54. $logger = \MediaWiki\Logger\LoggerFactory::getInstance( 'headers-sent' );
  55. $logger->error( 'Warning: headers were already sent from the location below', [
  56. 'exception' => self::$headersSentException,
  57. 'detection-trace' => new \Exception( 'Detected here' ),
  58. ] );
  59. }
  60. }
  61. }