MediaWiki master
FauxResponse.php
Go to the documentation of this file.
1<?php
2
24namespace MediaWiki\Request;
25
29
35 private $headers;
37 private $cookies = [];
39 private $code;
40
42 private $cookieConfig = null;
43
50 public function header( $string, $replace = true, $http_response_code = null ) {
51 if ( $this->disableForPostSend ) {
52 return;
53 }
54
55 if ( str_starts_with( $string, 'HTTP/' ) ) {
56 $parts = explode( ' ', $string, 3 );
57 $this->code = intval( $parts[1] );
58 } else {
59 [ $key, $val ] = array_map( 'trim', explode( ":", $string, 2 ) );
60
61 $key = strtoupper( $key );
62
63 if ( $replace || !isset( $this->headers[$key] ) ) {
64 $this->headers[$key] = $val;
65 }
66 }
67
68 if ( $http_response_code ) {
69 $this->code = intval( $http_response_code );
70 }
71 }
72
77 public function statusHeader( $code ) {
78 $this->code = intval( $code );
79 }
80
81 public function headersSent() {
82 return false;
83 }
84
89 public function getHeader( $key ) {
90 $key = strtoupper( $key );
91
92 return $this->headers[$key] ?? null;
93 }
94
100 public function getStatusCode() {
101 return $this->code;
102 }
103
107 private function getCookieConfig(): Config {
108 if ( !$this->cookieConfig ) {
109 $this->cookieConfig = MediaWikiServices::getInstance()->getMainConfig();
110 }
111 return $this->cookieConfig;
112 }
113
117 public function setCookieConfig( Config $cookieConfig ): void {
118 $this->cookieConfig = $cookieConfig;
119 }
120
127 public function setCookie( $name, $value, $expire = 0, $options = [] ) {
128 if ( $this->disableForPostSend ) {
129 return;
130 }
131
132 $cookieConfig = $this->getCookieConfig();
133 $cookiePath = $cookieConfig->get( MainConfigNames::CookiePath );
134 $cookiePrefix = $cookieConfig->get( MainConfigNames::CookiePrefix );
135 $cookieDomain = $cookieConfig->get( MainConfigNames::CookieDomain );
136 $cookieSecure = $cookieConfig->get( MainConfigNames::CookieSecure );
137 $cookieExpiration = $cookieConfig->get( MainConfigNames::CookieExpiration );
138 $cookieHttpOnly = $cookieConfig->get( MainConfigNames::CookieHttpOnly );
139 $options = array_filter( $options, static function ( $a ) {
140 return $a !== null;
141 } ) + [
142 'prefix' => $cookiePrefix,
143 'domain' => $cookieDomain,
144 'path' => $cookiePath,
145 'secure' => $cookieSecure,
146 'httpOnly' => $cookieHttpOnly,
147 'raw' => false,
148 ];
149
150 if ( $expire === null ) {
151 $expire = 0; // Session cookie
152 } elseif ( $expire == 0 && $cookieExpiration != 0 ) {
153 $expire = time() + $cookieExpiration;
154 }
155
156 $this->cookies[$options['prefix'] . $name] = [
157 'value' => (string)$value,
158 'expire' => (int)$expire,
159 'path' => (string)$options['path'],
160 'domain' => (string)$options['domain'],
161 'secure' => (bool)$options['secure'],
162 'httpOnly' => (bool)$options['httpOnly'],
163 'raw' => (bool)$options['raw'],
164 ];
165 }
166
171 public function getCookie( $name ) {
172 if ( isset( $this->cookies[$name] ) ) {
173 return $this->cookies[$name]['value'];
174 }
175 return null;
176 }
177
182 public function getCookieData( $name ) {
183 return $this->cookies[$name] ?? null;
184 }
185
189 public function getCookies() {
190 return $this->cookies;
191 }
192
196 public function hasCookies() {
197 return count( $this->cookies ) > 0;
198 }
199
200}
201
203class_alias( FauxResponse::class, 'FauxResponse' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
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 ...
disableForPostSend()
Disable setters for post-send processing.
Interface for configuration instances.
Definition Config.php:32
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".