MediaWiki 1.40.4
FauxResponse.php
Go to the documentation of this file.
1<?php
2
24namespace MediaWiki\Request;
25
26use Config;
29
34 private $headers;
35 private $cookies = [];
36 private $code;
37
39 private $cookieConfig = null;
40
47 public function header( $string, $replace = true, $http_response_code = null ) {
48 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
49 $parts = explode( ' ', $string, 3 );
50 $this->code = intval( $parts[1] );
51 } else {
52 [ $key, $val ] = array_map( 'trim', explode( ":", $string, 2 ) );
53
54 $key = strtoupper( $key );
55
56 if ( $replace || !isset( $this->headers[$key] ) ) {
57 $this->headers[$key] = $val;
58 }
59 }
60
61 if ( $http_response_code !== null ) {
62 $this->code = intval( $http_response_code );
63 }
64 }
65
70 public function statusHeader( $code ) {
71 $this->code = intval( $code );
72 }
73
74 public function headersSent() {
75 return false;
76 }
77
82 public function getHeader( $key ) {
83 $key = strtoupper( $key );
84
85 return $this->headers[$key] ?? null;
86 }
87
93 public function getStatusCode() {
94 return $this->code;
95 }
96
100 private function getCookieConfig(): Config {
101 if ( !$this->cookieConfig ) {
102 $this->cookieConfig = MediaWikiServices::getInstance()->getMainConfig();
103 }
104 return $this->cookieConfig;
105 }
106
110 public function setCookieConfig( Config $cookieConfig ): void {
111 $this->cookieConfig = $cookieConfig;
112 }
113
120 public function setCookie( $name, $value, $expire = 0, $options = [] ) {
121 $cookieConfig = $this->getCookieConfig();
122 $cookiePath = $cookieConfig->get( MainConfigNames::CookiePath );
123 $cookiePrefix = $cookieConfig->get( MainConfigNames::CookiePrefix );
124 $cookieDomain = $cookieConfig->get( MainConfigNames::CookieDomain );
125 $cookieSecure = $cookieConfig->get( MainConfigNames::CookieSecure );
126 $cookieExpiration = $cookieConfig->get( MainConfigNames::CookieExpiration );
127 $cookieHttpOnly = $cookieConfig->get( MainConfigNames::CookieHttpOnly );
128 $options = array_filter( $options, static function ( $a ) {
129 return $a !== null;
130 } ) + [
131 'prefix' => $cookiePrefix,
132 'domain' => $cookieDomain,
133 'path' => $cookiePath,
134 'secure' => $cookieSecure,
135 'httpOnly' => $cookieHttpOnly,
136 'raw' => false,
137 ];
138
139 if ( $expire === null ) {
140 $expire = 0; // Session cookie
141 } elseif ( $expire == 0 && $cookieExpiration != 0 ) {
142 $expire = time() + $cookieExpiration;
143 }
144
145 $this->cookies[$options['prefix'] . $name] = [
146 'value' => (string)$value,
147 'expire' => (int)$expire,
148 'path' => (string)$options['path'],
149 'domain' => (string)$options['domain'],
150 'secure' => (bool)$options['secure'],
151 'httpOnly' => (bool)$options['httpOnly'],
152 'raw' => (bool)$options['raw'],
153 ];
154 }
155
160 public function getCookie( $name ) {
161 if ( isset( $this->cookies[$name] ) ) {
162 return $this->cookies[$name]['value'];
163 }
164 return null;
165 }
166
171 public function getCookieData( $name ) {
172 return $this->cookies[$name] ?? null;
173 }
174
178 public function getCookies() {
179 return $this->cookies;
180 }
181
185 public function hasCookies() {
186 return count( $this->cookies ) > 0;
187 }
188
189}
190
191class_alias( FauxResponse::class, 'FauxResponse' );
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition WebStart.php:88
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
header( $string, $replace=true, $http_response_code=null)
Stores a HTTP header.
hasCookies()
Checks whether this request is performing cookie operations.bool 1.27
setCookieConfig(Config $cookieConfig)
getStatusCode()
Get the HTTP response code, null if not set.
setCookie( $name, $value, $expire=0, $options=[])
headersSent()
Test if headers have been sent.
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
Interface for configuration instances.
Definition Config.php:30
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".