[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/ -> WebResponse.php (source)

   1  <?php
   2  /**
   3   * Classes used to send headers and cookies back to the user
   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   * Allow programs to request this object from WebRequest::response()
  25   * and handle all outputting (or lack of outputting) via it.
  26   * @ingroup HTTP
  27   */
  28  class WebResponse {
  29  
  30      /**
  31       * Output a HTTP header, wrapper for PHP's header()
  32       * @param string $string Header to output
  33       * @param bool $replace Replace current similar header
  34       * @param null|int $http_response_code Forces the HTTP response code to the specified value.
  35       */
  36  	public function header( $string, $replace = true, $http_response_code = null ) {
  37          header( $string, $replace, $http_response_code );
  38      }
  39  
  40      /**
  41       * Set the browser cookie
  42       * @param string $name Name of cookie
  43       * @param string $value Value to give cookie
  44       * @param int|null $expire Unix timestamp (in seconds) when the cookie should expire.
  45       *        0 (the default) causes it to expire $wgCookieExpiration seconds from now.
  46       *        null causes it to be a session cookie.
  47       * @param array $options Assoc of additional cookie options:
  48       *     prefix: string, name prefix ($wgCookiePrefix)
  49       *     domain: string, cookie domain ($wgCookieDomain)
  50       *     path: string, cookie path ($wgCookiePath)
  51       *     secure: bool, secure attribute ($wgCookieSecure)
  52       *     httpOnly: bool, httpOnly attribute ($wgCookieHttpOnly)
  53       *     raw: bool, if true uses PHP's setrawcookie() instead of setcookie()
  54       *   For backwards compatibility, if $options is not an array then it and
  55       *   the following two parameters will be interpreted as values for
  56       *   'prefix', 'domain', and 'secure'
  57       * @since 1.22 Replaced $prefix, $domain, and $forceSecure with $options
  58       */
  59  	public function setcookie( $name, $value, $expire = 0, $options = null ) {
  60          global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
  61          global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
  62  
  63          if ( !is_array( $options ) ) {
  64              // Backwards compatibility
  65              $options = array( 'prefix' => $options );
  66              if ( func_num_args() >= 5 ) {
  67                  $options['domain'] = func_get_arg( 4 );
  68              }
  69              if ( func_num_args() >= 6 ) {
  70                  $options['secure'] = func_get_arg( 5 );
  71              }
  72          }
  73          $options = array_filter( $options, function ( $a ) {
  74              return $a !== null;
  75          } ) + array(
  76              'prefix' => $wgCookiePrefix,
  77              'domain' => $wgCookieDomain,
  78              'path' => $wgCookiePath,
  79              'secure' => $wgCookieSecure,
  80              'httpOnly' => $wgCookieHttpOnly,
  81              'raw' => false,
  82          );
  83  
  84          if ( $expire === null ) {
  85              $expire = 0; // Session cookie
  86          } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
  87              $expire = time() + $wgCookieExpiration;
  88          }
  89  
  90          $func = $options['raw'] ? 'setrawcookie' : 'setcookie';
  91  
  92          if ( wfRunHooks( 'WebResponseSetCookie', array( &$name, &$value, &$expire, $options ) ) ) {
  93              wfDebugLog( 'cookie',
  94                  $func . ': "' . implode( '", "',
  95                      array(
  96                          $options['prefix'] . $name,
  97                          $value,
  98                          $expire,
  99                          $options['path'],
 100                          $options['domain'],
 101                          $options['secure'],
 102                          $options['httpOnly'] ) ) . '"' );
 103  
 104              call_user_func( $func,
 105                  $options['prefix'] . $name,
 106                  $value,
 107                  $expire,
 108                  $options['path'],
 109                  $options['domain'],
 110                  $options['secure'],
 111                  $options['httpOnly'] );
 112          }
 113      }
 114  }
 115  
 116  /**
 117   * @ingroup HTTP
 118   */
 119  class FauxResponse extends WebResponse {
 120      private $headers;
 121      private $cookies;
 122      private $code;
 123  
 124      /**
 125       * Stores a HTTP header
 126       * @param string $string Header to output
 127       * @param bool $replace Replace current similar header
 128       * @param null|int $http_response_code Forces the HTTP response code to the specified value.
 129       */
 130  	public function header( $string, $replace = true, $http_response_code = null ) {
 131          if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
 132              $parts = explode( ' ', $string, 3 );
 133              $this->code = intval( $parts[1] );
 134          } else {
 135              list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
 136  
 137              $key = strtoupper( $key );
 138  
 139              if ( $replace || !isset( $this->headers[$key] ) ) {
 140                  $this->headers[$key] = $val;
 141              }
 142          }
 143  
 144          if ( $http_response_code !== null ) {
 145              $this->code = intval( $http_response_code );
 146          }
 147      }
 148  
 149      /**
 150       * @param string $key The name of the header to get (case insensitive).
 151       * @return string
 152       */
 153  	public function getheader( $key ) {
 154          $key = strtoupper( $key );
 155  
 156          if ( isset( $this->headers[$key] ) ) {
 157              return $this->headers[$key];
 158          }
 159          return null;
 160      }
 161  
 162      /**
 163       * Get the HTTP response code, null if not set
 164       *
 165       * @return int|null
 166       */
 167  	public function getStatusCode() {
 168          return $this->code;
 169      }
 170  
 171      /**
 172       * @todo document. It just ignore optional parameters.
 173       *
 174       * @param string $name Name of cookie
 175       * @param string $value Value to give cookie
 176       * @param int $expire Number of seconds til cookie expires (Default: 0)
 177       * @param array $options Ignored
 178       */
 179  	public function setcookie( $name, $value, $expire = 0, $options = null ) {
 180          $this->cookies[$name] = $value;
 181      }
 182  
 183      /**
 184       * @param string $name
 185       * @return string
 186       */
 187  	public function getcookie( $name ) {
 188          if ( isset( $this->cookies[$name] ) ) {
 189              return $this->cookies[$name];
 190          }
 191          return null;
 192      }
 193  }


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