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
104 private function getCookieConfig(): Config {
105 if ( !$this->cookieConfig ) {
106 $this->cookieConfig = MediaWikiServices::getInstance()->getMainConfig();
107 }
108 return $this->cookieConfig;
109 }
110
111 public function setCookieConfig( Config $cookieConfig ): void {
112 $this->cookieConfig = $cookieConfig;
113 }
114
121 public function setCookie( $name, $value, $expire = 0, $options = [] ) {
122 if ( $this->disableForPostSend ) {
123 return;
124 }
125
126 $cookieConfig = $this->getCookieConfig();
127 $cookiePath = $cookieConfig->get( MainConfigNames::CookiePath );
128 $cookiePrefix = $cookieConfig->get( MainConfigNames::CookiePrefix );
129 $cookieDomain = $cookieConfig->get( MainConfigNames::CookieDomain );
130 $cookieSecure = $cookieConfig->get( MainConfigNames::CookieSecure );
131 $cookieExpiration = $cookieConfig->get( MainConfigNames::CookieExpiration );
132 $cookieHttpOnly = $cookieConfig->get( MainConfigNames::CookieHttpOnly );
133 $options = array_filter( $options, static function ( $a ) {
134 return $a !== null;
135 } ) + [
136 'prefix' => $cookiePrefix,
137 'domain' => $cookieDomain,
138 'path' => $cookiePath,
139 'secure' => $cookieSecure,
140 'httpOnly' => $cookieHttpOnly,
141 'raw' => false,
142 ];
143
144 if ( $expire === null ) {
145 $expire = 0; // Session cookie
146 } elseif ( $expire == 0 && $cookieExpiration != 0 ) {
147 $expire = time() + $cookieExpiration;
148 }
149
150 $this->cookies[$options['prefix'] . $name] = [
151 'value' => (string)$value,
152 'expire' => (int)$expire,
153 'path' => (string)$options['path'],
154 'domain' => (string)$options['domain'],
155 'secure' => (bool)$options['secure'],
156 'httpOnly' => (bool)$options['httpOnly'],
157 'raw' => (bool)$options['raw'],
158 ];
159 }
160
165 public function getCookie( $name ) {
166 if ( isset( $this->cookies[$name] ) ) {
167 return $this->cookies[$name]['value'];
168 }
169 return null;
170 }
171
176 public function getCookieData( $name ) {
177 return $this->cookies[$name] ?? null;
178 }
179
183 public function getCookies() {
184 return $this->cookies;
185 }
186
190 public function hasCookies() {
191 return count( $this->cookies ) > 0;
192 }
193
194}
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.".