MediaWiki  REL1_22
WebResponse.php
Go to the documentation of this file.
00001 <?php
00028 class WebResponse {
00029 
00037     public function header( $string, $replace = true, $http_response_code = null ) {
00038         header( $string, $replace, $http_response_code );
00039     }
00040 
00060     public function setcookie( $name, $value, $expire = 0, $options = null ) {
00061         global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
00062         global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
00063 
00064         if ( !is_array( $options ) ) {
00065             // Backwards compatability
00066             $options = array( 'prefix' => $options );
00067             if ( func_num_args() >= 5 ) {
00068                 $options['domain'] = func_get_arg( 4 );
00069             }
00070             if ( func_num_args() >= 6 ) {
00071                 $options['secure'] = func_get_arg( 5 );
00072             }
00073         }
00074         $options = array_filter( $options, function ( $a ) {
00075             return $a !== null;
00076         } ) + array(
00077             'prefix' => $wgCookiePrefix,
00078             'domain' => $wgCookieDomain,
00079             'path' => $wgCookiePath,
00080             'secure' => $wgCookieSecure,
00081             'httpOnly' => $wgCookieHttpOnly,
00082             'raw' => false,
00083         );
00084 
00085         if ( $expire === null ) {
00086             $expire = 0; // Session cookie
00087         } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
00088             $expire = time() + $wgCookieExpiration;
00089         }
00090 
00091         // Don't mark the cookie as httpOnly if the requesting user-agent is
00092         // known to have trouble with httpOnly cookies.
00093         if ( !wfHttpOnlySafe() ) {
00094             $options['httpOnly'] = false;
00095         }
00096 
00097         $func = $options['raw'] ? 'setrawcookie' : 'setcookie';
00098 
00099         if ( wfRunHooks( 'WebResponseSetCookie', array( &$name, &$value, &$expire, $options ) ) ) {
00100             wfDebugLog( 'cookie',
00101                 $func . ': "' . implode( '", "',
00102                     array(
00103                         $options['prefix'] . $name,
00104                         $value,
00105                         $expire,
00106                         $options['path'],
00107                         $options['domain'],
00108                         $options['secure'],
00109                         $options['httpOnly'] ) ) . '"' );
00110 
00111             call_user_func( $func,
00112                 $options['prefix'] . $name,
00113                 $value,
00114                 $expire,
00115                 $options['path'],
00116                 $options['domain'],
00117                 $options['secure'],
00118                 $options['httpOnly'] );
00119         }
00120     }
00121 }
00122 
00126 class FauxResponse extends WebResponse {
00127     private $headers;
00128     private $cookies;
00129     private $code;
00130 
00137     public function header( $string, $replace = true, $http_response_code = null ) {
00138         if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
00139             $parts = explode( ' ', $string, 3 );
00140             $this->code = intval( $parts[1] );
00141         } else {
00142             list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
00143 
00144             $key = strtoupper( $key );
00145 
00146             if ( $replace || !isset( $this->headers[$key] ) ) {
00147                 $this->headers[$key] = $val;
00148             }
00149         }
00150 
00151         if ( $http_response_code !== null ) {
00152             $this->code = intval( $http_response_code );
00153         }
00154     }
00155 
00160     public function getheader( $key ) {
00161         $key = strtoupper( $key );
00162 
00163         if ( isset( $this->headers[$key] ) ) {
00164             return $this->headers[$key];
00165         }
00166         return null;
00167     }
00168 
00174     public function getStatusCode() {
00175         return $this->code;
00176     }
00177 
00186     public function setcookie( $name, $value, $expire = 0, $options = null ) {
00187         $this->cookies[$name] = $value;
00188     }
00189 
00194     public function getcookie( $name ) {
00195         if ( isset( $this->cookies[$name] ) ) {
00196             return $this->cookies[$name];
00197         }
00198         return null;
00199     }
00200 }