MediaWiki  REL1_24
WebResponse.php
Go to the documentation of this file.
00001 <?php
00028 class WebResponse {
00029 
00036     public function header( $string, $replace = true, $http_response_code = null ) {
00037         header( $string, $replace, $http_response_code );
00038     }
00039 
00059     public function setcookie( $name, $value, $expire = 0, $options = null ) {
00060         global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
00061         global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
00062 
00063         if ( !is_array( $options ) ) {
00064             // Backwards compatibility
00065             $options = array( 'prefix' => $options );
00066             if ( func_num_args() >= 5 ) {
00067                 $options['domain'] = func_get_arg( 4 );
00068             }
00069             if ( func_num_args() >= 6 ) {
00070                 $options['secure'] = func_get_arg( 5 );
00071             }
00072         }
00073         $options = array_filter( $options, function ( $a ) {
00074             return $a !== null;
00075         } ) + array(
00076             'prefix' => $wgCookiePrefix,
00077             'domain' => $wgCookieDomain,
00078             'path' => $wgCookiePath,
00079             'secure' => $wgCookieSecure,
00080             'httpOnly' => $wgCookieHttpOnly,
00081             'raw' => false,
00082         );
00083 
00084         if ( $expire === null ) {
00085             $expire = 0; // Session cookie
00086         } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
00087             $expire = time() + $wgCookieExpiration;
00088         }
00089 
00090         $func = $options['raw'] ? 'setrawcookie' : 'setcookie';
00091 
00092         if ( wfRunHooks( 'WebResponseSetCookie', array( &$name, &$value, &$expire, $options ) ) ) {
00093             wfDebugLog( 'cookie',
00094                 $func . ': "' . implode( '", "',
00095                     array(
00096                         $options['prefix'] . $name,
00097                         $value,
00098                         $expire,
00099                         $options['path'],
00100                         $options['domain'],
00101                         $options['secure'],
00102                         $options['httpOnly'] ) ) . '"' );
00103 
00104             call_user_func( $func,
00105                 $options['prefix'] . $name,
00106                 $value,
00107                 $expire,
00108                 $options['path'],
00109                 $options['domain'],
00110                 $options['secure'],
00111                 $options['httpOnly'] );
00112         }
00113     }
00114 }
00115 
00119 class FauxResponse extends WebResponse {
00120     private $headers;
00121     private $cookies;
00122     private $code;
00123 
00130     public function header( $string, $replace = true, $http_response_code = null ) {
00131         if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
00132             $parts = explode( ' ', $string, 3 );
00133             $this->code = intval( $parts[1] );
00134         } else {
00135             list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
00136 
00137             $key = strtoupper( $key );
00138 
00139             if ( $replace || !isset( $this->headers[$key] ) ) {
00140                 $this->headers[$key] = $val;
00141             }
00142         }
00143 
00144         if ( $http_response_code !== null ) {
00145             $this->code = intval( $http_response_code );
00146         }
00147     }
00148 
00153     public function getheader( $key ) {
00154         $key = strtoupper( $key );
00155 
00156         if ( isset( $this->headers[$key] ) ) {
00157             return $this->headers[$key];
00158         }
00159         return null;
00160     }
00161 
00167     public function getStatusCode() {
00168         return $this->code;
00169     }
00170 
00179     public function setcookie( $name, $value, $expire = 0, $options = null ) {
00180         $this->cookies[$name] = $value;
00181     }
00182 
00187     public function getcookie( $name ) {
00188         if ( isset( $this->cookies[$name] ) ) {
00189             return $this->cookies[$name];
00190         }
00191         return null;
00192     }
00193 }