MediaWiki master
FauxResponse.php
Go to the documentation of this file.
1<?php
2
24namespace MediaWiki\Request;
25
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 ( $this->disableForPostSend ) {
49 return;
50 }
51
52 if ( str_starts_with( $string, 'HTTP/' ) ) {
53 $parts = explode( ' ', $string, 3 );
54 $this->code = intval( $parts[1] );
55 } else {
56 [ $key, $val ] = array_map( 'trim', explode( ":", $string, 2 ) );
57
58 $key = strtoupper( $key );
59
60 if ( $replace || !isset( $this->headers[$key] ) ) {
61 $this->headers[$key] = $val;
62 }
63 }
64
65 if ( $http_response_code ) {
66 $this->code = intval( $http_response_code );
67 }
68 }
69
74 public function statusHeader( $code ) {
75 $this->code = intval( $code );
76 }
77
78 public function headersSent() {
79 return false;
80 }
81
86 public function getHeader( $key ) {
87 $key = strtoupper( $key );
88
89 return $this->headers[$key] ?? null;
90 }
91
97 public function getStatusCode() {
98 return $this->code;
99 }
100
104 private function getCookieConfig(): Config {
105 if ( !$this->cookieConfig ) {
106 $this->cookieConfig = MediaWikiServices::getInstance()->getMainConfig();
107 }
108 return $this->cookieConfig;
109 }
110
114 public function setCookieConfig( Config $cookieConfig ): void {
115 $this->cookieConfig = $cookieConfig;
116 }
117
124 public function setCookie( $name, $value, $expire = 0, $options = [] ) {
125 if ( $this->disableForPostSend ) {
126 return;
127 }
128
129 $cookieConfig = $this->getCookieConfig();
130 $cookiePath = $cookieConfig->get( MainConfigNames::CookiePath );
131 $cookiePrefix = $cookieConfig->get( MainConfigNames::CookiePrefix );
132 $cookieDomain = $cookieConfig->get( MainConfigNames::CookieDomain );
133 $cookieSecure = $cookieConfig->get( MainConfigNames::CookieSecure );
134 $cookieExpiration = $cookieConfig->get( MainConfigNames::CookieExpiration );
135 $cookieHttpOnly = $cookieConfig->get( MainConfigNames::CookieHttpOnly );
136 $options = array_filter( $options, static function ( $a ) {
137 return $a !== null;
138 } ) + [
139 'prefix' => $cookiePrefix,
140 'domain' => $cookieDomain,
141 'path' => $cookiePath,
142 'secure' => $cookieSecure,
143 'httpOnly' => $cookieHttpOnly,
144 'raw' => false,
145 ];
146
147 if ( $expire === null ) {
148 $expire = 0; // Session cookie
149 } elseif ( $expire == 0 && $cookieExpiration != 0 ) {
150 $expire = time() + $cookieExpiration;
151 }
152
153 $this->cookies[$options['prefix'] . $name] = [
154 'value' => (string)$value,
155 'expire' => (int)$expire,
156 'path' => (string)$options['path'],
157 'domain' => (string)$options['domain'],
158 'secure' => (bool)$options['secure'],
159 'httpOnly' => (bool)$options['httpOnly'],
160 'raw' => (bool)$options['raw'],
161 ];
162 }
163
168 public function getCookie( $name ) {
169 if ( isset( $this->cookies[$name] ) ) {
170 return $this->cookies[$name]['value'];
171 }
172 return null;
173 }
174
179 public function getCookieData( $name ) {
180 return $this->cookies[$name] ?? null;
181 }
182
186 public function getCookies() {
187 return $this->cookies;
188 }
189
193 public function hasCookies() {
194 return count( $this->cookies ) > 0;
195 }
196
197}
198
200class_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.".