MediaWiki  master
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 
00053         public function setcookie( $name, $value, $expire = 0, $prefix = null, $domain = null, $forceSecure = null ) {
00054                 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
00055                 global $wgCookieSecure,$wgCookieExpiration, $wgCookieHttpOnly;
00056                 if ( $expire == 0 ) {
00057                         $expire = time() + $wgCookieExpiration;
00058                 }
00059                 if( $prefix === null ) {
00060                         $prefix = $wgCookiePrefix;
00061                 }
00062                 if( $domain === null ) {
00063                         $domain = $wgCookieDomain;
00064                 }
00065 
00066                 if ( is_null( $forceSecure ) ) {
00067                         $secureCookie = $wgCookieSecure;
00068                 } else {
00069                         $secureCookie = $forceSecure;
00070                 }
00071 
00072                 $httpOnlySafe = wfHttpOnlySafe() && $wgCookieHttpOnly;
00073                 wfDebugLog( 'cookie',
00074                         'setcookie: "' . implode( '", "',
00075                                 array(
00076                                         $prefix . $name,
00077                                         $value,
00078                                         $expire,
00079                                         $wgCookiePath,
00080                                         $domain,
00081                                         $secureCookie,
00082                                         $httpOnlySafe ) ) . '"' );
00083                 setcookie( $prefix . $name,
00084                         $value,
00085                         $expire,
00086                         $wgCookiePath,
00087                         $domain,
00088                         $secureCookie,
00089                         $httpOnlySafe );
00090         }
00091 }
00092 
00096 class FauxResponse extends WebResponse {
00097         private $headers;
00098         private $cookies;
00099         private $code;
00100 
00107         public function header( $string, $replace = true, $http_response_code = null ) {
00108                 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
00109                         $parts = explode( ' ', $string, 3 );
00110                         $this->code = intval( $parts[1] );
00111                 } else {
00112                         list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
00113 
00114                         if( $replace || !isset( $this->headers[$key] ) ) {
00115                                 $this->headers[$key] = $val;
00116                         }
00117                 }
00118 
00119                 if ( $http_response_code !== null ) {
00120                         $this->code = intval( $http_response_code );
00121                 }
00122         }
00123 
00128         public function getheader( $key ) {
00129                 if ( isset( $this->headers[$key] ) ) {
00130                         return $this->headers[$key];
00131                 }
00132                 return null;
00133         }
00134 
00140         public function getStatusCode() {
00141                 return $this->code;
00142         }
00143 
00154         public function setcookie( $name, $value, $expire = 0, $prefix = null, $domain = null, $forceSecure = null ) {
00155                 $this->cookies[$name] = $value;
00156         }
00157 
00162         public function getcookie( $name )  {
00163                 if ( isset( $this->cookies[$name] ) ) {
00164                         return $this->cookies[$name];
00165                 }
00166                 return null;
00167         }
00168 }