[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/ -> OutputHandler.php (source)

   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  
  23  /**
  24   * Standard output handler for use with ob_start
  25   *
  26   * @param string $s
  27   *
  28   * @return string
  29   */
  30  function wfOutputHandler( $s ) {
  31      global $wgDisableOutputCompression, $wgValidateAllHtml, $wgMangleFlashPolicy;
  32      if ( $wgMangleFlashPolicy ) {
  33          $s = wfMangleFlashPolicy( $s );
  34      }
  35      if ( $wgValidateAllHtml ) {
  36          $headers = headers_list();
  37          $isHTML = false;
  38          foreach ( $headers as $header ) {
  39              $parts = explode( ':', $header, 2 );
  40              if ( count( $parts ) !== 2 ) {
  41                  continue;
  42              }
  43              $name = strtolower( trim( $parts[0] ) );
  44              $value = trim( $parts[1] );
  45              if ( $name == 'content-type' && ( strpos( $value, 'text/html' ) === 0
  46                  || strpos( $value, 'application/xhtml+xml' ) === 0 )
  47              ) {
  48                  $isHTML = true;
  49                  break;
  50              }
  51          }
  52          if ( $isHTML ) {
  53              $s = wfHtmlValidationHandler( $s );
  54          }
  55      }
  56      if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
  57          if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
  58              $s = wfGzipHandler( $s );
  59          }
  60          if ( !ini_get( 'output_handler' ) ) {
  61              wfDoContentLength( strlen( $s ) );
  62          }
  63      }
  64      return $s;
  65  }
  66  
  67  /**
  68   * Get the "file extension" that some client apps will estimate from
  69   * the currently-requested URL.
  70   * This isn't on WebRequest because we need it when things aren't initialized
  71   * @private
  72   *
  73   * @return string
  74   */
  75  function wfRequestExtension() {
  76      /// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
  77      if ( isset( $_SERVER['REQUEST_URI'] ) ) {
  78          // Strip the query string...
  79          list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
  80      } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
  81          // Probably IIS. QUERY_STRING appears separately.
  82          $path = $_SERVER['SCRIPT_NAME'];
  83      } else {
  84          // Can't get the path from the server? :(
  85          return '';
  86      }
  87  
  88      $period = strrpos( $path, '.' );
  89      if ( $period !== false ) {
  90          return strtolower( substr( $path, $period ) );
  91      }
  92      return '';
  93  }
  94  
  95  /**
  96   * Handler that compresses data with gzip if allowed by the Accept header.
  97   * Unlike ob_gzhandler, it works for HEAD requests too.
  98   *
  99   * @param string $s
 100   *
 101   * @return string
 102   */
 103  function wfGzipHandler( $s ) {
 104      if ( !function_exists( 'gzencode' ) ) {
 105          wfDebug( __FUNCTION__ . "() skipping compression (gzencode unavailable)\n" );
 106          return $s;
 107      }
 108      if ( headers_sent() ) {
 109          wfDebug( __FUNCTION__ . "() skipping compression (headers already sent)\n" );
 110          return $s;
 111      }
 112  
 113      $ext = wfRequestExtension();
 114      if ( $ext == '.gz' || $ext == '.tgz' ) {
 115          // Don't do gzip compression if the URL path ends in .gz or .tgz
 116          // This confuses Safari and triggers a download of the page,
 117          // even though it's pretty clearly labeled as viewable HTML.
 118          // Bad Safari! Bad!
 119          return $s;
 120      }
 121  
 122      if ( wfClientAcceptsGzip() ) {
 123          wfDebug( __FUNCTION__ . "() is compressing output\n" );
 124          header( 'Content-Encoding: gzip' );
 125          $s = gzencode( $s, 6 );
 126      }
 127  
 128      // Set vary header if it hasn't been set already
 129      $headers = headers_list();
 130      $foundVary = false;
 131      foreach ( $headers as $header ) {
 132          if ( substr( $header, 0, 5 ) == 'Vary:' ) {
 133              $foundVary = true;
 134              break;
 135          }
 136      }
 137      if ( !$foundVary ) {
 138          header( 'Vary: Accept-Encoding' );
 139          global $wgUseXVO;
 140          if ( $wgUseXVO ) {
 141              header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
 142          }
 143      }
 144      return $s;
 145  }
 146  
 147  /**
 148   * Mangle flash policy tags which open up the site to XSS attacks.
 149   *
 150   * @param string $s
 151   *
 152   * @return string
 153   */
 154  function wfMangleFlashPolicy( $s ) {
 155      # Avoid weird excessive memory usage in PCRE on big articles
 156      if ( preg_match( '/\<\s*cross-domain-policy\s*\>/i', $s ) ) {
 157          return preg_replace( '/\<\s*cross-domain-policy\s*\>/i', '<NOT-cross-domain-policy>', $s );
 158      } else {
 159          return $s;
 160      }
 161  }
 162  
 163  /**
 164   * Add a Content-Length header if possible. This makes it cooperate with squid better.
 165   *
 166   * @param int $length
 167   */
 168  function wfDoContentLength( $length ) {
 169      if ( !headers_sent()
 170          && isset( $_SERVER['SERVER_PROTOCOL'] )
 171          && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0'
 172      ) {
 173          header( "Content-Length: $length" );
 174      }
 175  }
 176  
 177  /**
 178   * Replace the output with an error if the HTML is not valid
 179   *
 180   * @param string $s
 181   *
 182   * @return string
 183   */
 184  function wfHtmlValidationHandler( $s ) {
 185  
 186      $errors = '';
 187      if ( MWTidy::checkErrors( $s, $errors ) ) {
 188          return $s;
 189      }
 190  
 191      header( 'Cache-Control: no-cache' );
 192  
 193      $out = Html::element( 'h1', null, 'HTML validation error' );
 194      $out .= Html::openElement( 'ul' );
 195  
 196      $error = strtok( $errors, "\n" );
 197      $badLines = array();
 198      while ( $error !== false ) {
 199          if ( preg_match( '/^line (\d+)/', $error, $m ) ) {
 200              $lineNum = intval( $m[1] );
 201              $badLines[$lineNum] = true;
 202              $out .= Html::rawElement( 'li', null,
 203                  Html::element( 'a', array( 'href' => "#line-{$lineNum}" ), $error ) ) . "\n";
 204          }
 205          $error = strtok( "\n" );
 206      }
 207  
 208      $out .= Html::closeElement( 'ul' );
 209      $out .= Html::element( 'pre', null, $errors );
 210      $out .= Html::openElement( 'ol' ) . "\n";
 211      $line = strtok( $s, "\n" );
 212      $i = 1;
 213      while ( $line !== false ) {
 214          $attrs = array();
 215          if ( isset( $badLines[$i] ) ) {
 216              $attrs['class'] = 'highlight';
 217              $attrs['id'] = "line-$i";
 218          }
 219          $out .= Html::element( 'li', $attrs, $line ) . "\n";
 220          $line = strtok( "\n" );
 221          $i++;
 222      }
 223      $out .= Html::closeElement( 'ol' );
 224  
 225      $style = <<<CSS
 226  .highlight { background-color: #ffc }
 227  li { white-space: pre }
 228  CSS;
 229  
 230      $out = Html::htmlHeader( array( 'lang' => 'en', 'dir' => 'ltr' ) ) .
 231          Html::rawElement( 'head', null,
 232              Html::element( 'title', null, 'HTML validation error' ) .
 233              Html::inlineStyle( $style ) ) .
 234          Html::rawElement( 'body', null, $out ) .
 235          Html::closeElement( 'html' );
 236  
 237      return $out;
 238  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1