MediaWiki  master
FauxResponse.php
Go to the documentation of this file.
1 <?php
26 class FauxResponse extends WebResponse {
27  private $headers;
28  private $cookies = [];
29  private $code;
30 
37  public function header( $string, $replace = true, $http_response_code = null ) {
38  if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
39  $parts = explode( ' ', $string, 3 );
40  $this->code = intval( $parts[1] );
41  } else {
42  list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
43 
44  $key = strtoupper( $key );
45 
46  if ( $replace || !isset( $this->headers[$key] ) ) {
47  $this->headers[$key] = $val;
48  }
49  }
50 
51  if ( $http_response_code !== null ) {
52  $this->code = intval( $http_response_code );
53  }
54  }
55 
60  public function statusHeader( $code ) {
61  $this->code = intval( $code );
62  }
63 
64  public function headersSent() {
65  return false;
66  }
67 
72  public function getHeader( $key ) {
73  $key = strtoupper( $key );
74 
75  return $this->headers[$key] ?? null;
76  }
77 
83  public function getStatusCode() {
84  return $this->code;
85  }
86 
93  public function setCookie( $name, $value, $expire = 0, $options = [] ) {
96 
97  $options = array_filter( $options, function ( $a ) {
98  return $a !== null;
99  } ) + [
100  'prefix' => $wgCookiePrefix,
101  'domain' => $wgCookieDomain,
102  'path' => $wgCookiePath,
103  'secure' => $wgCookieSecure,
104  'httpOnly' => $wgCookieHttpOnly,
105  'raw' => false,
106  ];
107 
108  if ( $expire === null ) {
109  $expire = 0; // Session cookie
110  } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
111  $expire = time() + $wgCookieExpiration;
112  }
113 
114  $this->cookies[$options['prefix'] . $name] = [
115  'value' => (string)$value,
116  'expire' => (int)$expire,
117  'path' => (string)$options['path'],
118  'domain' => (string)$options['domain'],
119  'secure' => (bool)$options['secure'],
120  'httpOnly' => (bool)$options['httpOnly'],
121  'raw' => (bool)$options['raw'],
122  ];
123  }
124 
129  public function getCookie( $name ) {
130  if ( isset( $this->cookies[$name] ) ) {
131  return $this->cookies[$name]['value'];
132  }
133  return null;
134  }
135 
140  public function getCookieData( $name ) {
141  return $this->cookies[$name] ?? null;
142  }
143 
147  public function getCookies() {
148  return $this->cookies;
149  }
150 }
getCookie( $name)
$wgCookieDomain
Set to set an explicit domain on the login cookies eg, "justthis.domain.org" or ".any.subdomain.net".
$wgCookiePrefix
Cookies generated by MediaWiki have names starting with this prefix.
$wgCookieHttpOnly
Set authentication cookies to HttpOnly to prevent access by JavaScript, in browsers that support this...
setCookie( $name, $value, $expire=0, $options=[])
$wgCookieSecure
Whether the "secure" flag should be set on the cookie.
getStatusCode()
Get the HTTP response code, null if not set.
getCookieData( $name)
$wgCookieExpiration
Default cookie lifetime, in seconds.
statusHeader( $code)
$wgCookiePath
Set this variable if you want to restrict cookies to a certain path within the domain specified by $w...
header( $string, $replace=true, $http_response_code=null)
Stores a HTTP header.
getHeader( $key)