OutputHandler.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Functions to be used with PHP's output buffer.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. namespace MediaWiki;
  23. /**
  24. * @since 1.31
  25. */
  26. class OutputHandler {
  27. /**
  28. * Standard output handler for use with ob_start.
  29. *
  30. * @param string $s Web response output
  31. * @return string
  32. */
  33. public static function handle( $s ) {
  34. global $wgDisableOutputCompression, $wgMangleFlashPolicy;
  35. if ( $wgMangleFlashPolicy ) {
  36. $s = self::mangleFlashPolicy( $s );
  37. }
  38. if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
  39. if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
  40. $s = self::handleGzip( $s );
  41. }
  42. if ( !ini_get( 'output_handler' ) ) {
  43. self::emitContentLength( strlen( $s ) );
  44. }
  45. }
  46. return $s;
  47. }
  48. /**
  49. * Get the "file extension" that some client apps will estimate from
  50. * the currently-requested URL.
  51. *
  52. * This isn't a WebRequest method, because we need it before the class loads.
  53. * @todo As of 2018, this actually runs after autoloader in Setup.php, so
  54. * WebRequest seems like a good place for this.
  55. *
  56. * @return string
  57. */
  58. private static function findUriExtension() {
  59. /// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
  60. if ( isset( $_SERVER['REQUEST_URI'] ) ) {
  61. // Strip the query string...
  62. $path = explode( '?', $_SERVER['REQUEST_URI'], 2 )[0];
  63. } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
  64. // Probably IIS. QUERY_STRING appears separately.
  65. $path = $_SERVER['SCRIPT_NAME'];
  66. } else {
  67. // Can't get the path from the server? :(
  68. return '';
  69. }
  70. $period = strrpos( $path, '.' );
  71. if ( $period !== false ) {
  72. return strtolower( substr( $path, $period ) );
  73. }
  74. return '';
  75. }
  76. /**
  77. * Handler that compresses data with gzip if allowed by the Accept header.
  78. *
  79. * Unlike ob_gzhandler, it works for HEAD requests too.
  80. *
  81. * @param string $s Web response output
  82. * @return string
  83. */
  84. private static function handleGzip( $s ) {
  85. if ( !function_exists( 'gzencode' ) ) {
  86. wfDebug( __METHOD__ . "() skipping compression (gzencode unavailable)\n" );
  87. return $s;
  88. }
  89. if ( headers_sent() ) {
  90. wfDebug( __METHOD__ . "() skipping compression (headers already sent)\n" );
  91. return $s;
  92. }
  93. $ext = self::findUriExtension();
  94. if ( $ext == '.gz' || $ext == '.tgz' ) {
  95. // Don't do gzip compression if the URL path ends in .gz or .tgz
  96. // This confuses Safari and triggers a download of the page,
  97. // even though it's pretty clearly labeled as viewable HTML.
  98. // Bad Safari! Bad!
  99. return $s;
  100. }
  101. if ( wfClientAcceptsGzip() ) {
  102. wfDebug( __METHOD__ . "() is compressing output\n" );
  103. header( 'Content-Encoding: gzip' );
  104. $s = gzencode( $s, 6 );
  105. }
  106. // Set vary header if it hasn't been set already
  107. $headers = headers_list();
  108. $foundVary = false;
  109. foreach ( $headers as $header ) {
  110. $headerName = strtolower( substr( $header, 0, 5 ) );
  111. if ( $headerName == 'vary:' ) {
  112. $foundVary = true;
  113. break;
  114. }
  115. }
  116. if ( !$foundVary ) {
  117. header( 'Vary: Accept-Encoding' );
  118. }
  119. return $s;
  120. }
  121. /**
  122. * Mangle flash policy tags which open up the site to XSS attacks.
  123. *
  124. * @param string $s Web response output
  125. * @return string
  126. */
  127. private static function mangleFlashPolicy( $s ) {
  128. # Avoid weird excessive memory usage in PCRE on big articles
  129. if ( preg_match( '/\<\s*cross-domain-policy(?=\s|\>)/i', $s ) ) {
  130. return preg_replace( '/\<(\s*)(cross-domain-policy(?=\s|\>))/i', '<$1NOT-$2', $s );
  131. } else {
  132. return $s;
  133. }
  134. }
  135. /**
  136. * Add a Content-Length header if possible. This makes it cooperate with CDN better.
  137. *
  138. * @param int $length
  139. */
  140. private static function emitContentLength( $length ) {
  141. if ( !headers_sent()
  142. && isset( $_SERVER['SERVER_PROTOCOL'] )
  143. && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0'
  144. ) {
  145. header( "Content-Length: $length" );
  146. }
  147. }
  148. }