MediaWiki  REL1_21
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 
00054         public function setcookie( $name, $value, $expire = 0, $prefix = null, $domain = null, $forceSecure = null ) {
00055                 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
00056                 global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
00057                 if ( $expire == 0 ) {
00058                         $expire = time() + $wgCookieExpiration;
00059                 }
00060                 if( $prefix === null ) {
00061                         $prefix = $wgCookiePrefix;
00062                 }
00063                 if( $domain === null ) {
00064                         $domain = $wgCookieDomain;
00065                 }
00066 
00067                 if ( is_null( $forceSecure ) ) {
00068                         $secureCookie = $wgCookieSecure;
00069                 } else {
00070                         $secureCookie = $forceSecure;
00071                 }
00072 
00073                 // Mark the cookie as httpOnly if $wgCookieHttpOnly is true,
00074                 // unless the requesting user-agent is known to have trouble with
00075                 // httpOnly cookies.
00076                 $httpOnlySafe = $wgCookieHttpOnly && wfHttpOnlySafe();
00077 
00078                 wfDebugLog( 'cookie',
00079                         'setcookie: "' . implode( '", "',
00080                                 array(
00081                                         $prefix . $name,
00082                                         $value,
00083                                         $expire,
00084                                         $wgCookiePath,
00085                                         $domain,
00086                                         $secureCookie,
00087                                         $httpOnlySafe ) ) . '"' );
00088                 setcookie( $prefix . $name,
00089                         $value,
00090                         $expire,
00091                         $wgCookiePath,
00092                         $domain,
00093                         $secureCookie,
00094                         $httpOnlySafe );
00095         }
00096 }
00097 
00101 class FauxResponse extends WebResponse {
00102         private $headers;
00103         private $cookies;
00104         private $code;
00105 
00112         public function header( $string, $replace = true, $http_response_code = null ) {
00113                 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
00114                         $parts = explode( ' ', $string, 3 );
00115                         $this->code = intval( $parts[1] );
00116                 } else {
00117                         list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
00118 
00119                         if( $replace || !isset( $this->headers[$key] ) ) {
00120                                 $this->headers[$key] = $val;
00121                         }
00122                 }
00123 
00124                 if ( $http_response_code !== null ) {
00125                         $this->code = intval( $http_response_code );
00126                 }
00127         }
00128 
00133         public function getheader( $key ) {
00134                 if ( isset( $this->headers[$key] ) ) {
00135                         return $this->headers[$key];
00136                 }
00137                 return null;
00138         }
00139 
00145         public function getStatusCode() {
00146                 return $this->code;
00147         }
00148 
00159         public function setcookie( $name, $value, $expire = 0, $prefix = null, $domain = null, $forceSecure = null ) {
00160                 $this->cookies[$name] = $value;
00161         }
00162 
00167         public function getcookie( $name ) {
00168                 if ( isset( $this->cookies[$name] ) ) {
00169                         return $this->cookies[$name];
00170                 }
00171                 return null;
00172         }
00173 }