MediaWiki REL1_39
FauxResponse.php
Go to the documentation of this file.
1<?php
2
26
31 private $headers;
32 private $cookies = [];
33 private $code;
34
36 private $cookieConfig = null;
37
44 public function header( $string, $replace = true, $http_response_code = null ) {
45 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
46 $parts = explode( ' ', $string, 3 );
47 $this->code = intval( $parts[1] );
48 } else {
49 [ $key, $val ] = array_map( 'trim', explode( ":", $string, 2 ) );
50
51 $key = strtoupper( $key );
52
53 if ( $replace || !isset( $this->headers[$key] ) ) {
54 $this->headers[$key] = $val;
55 }
56 }
57
58 if ( $http_response_code !== null ) {
59 $this->code = intval( $http_response_code );
60 }
61 }
62
67 public function statusHeader( $code ) {
68 $this->code = intval( $code );
69 }
70
71 public function headersSent() {
72 return false;
73 }
74
79 public function getHeader( $key ) {
80 $key = strtoupper( $key );
81
82 return $this->headers[$key] ?? null;
83 }
84
90 public function getStatusCode() {
91 return $this->code;
92 }
93
97 private function getCookieConfig(): Config {
98 if ( !$this->cookieConfig ) {
99 $this->cookieConfig = MediaWikiServices::getInstance()->getMainConfig();
100 }
101 return $this->cookieConfig;
102 }
103
107 public function setCookieConfig( Config $cookieConfig ): void {
108 $this->cookieConfig = $cookieConfig;
109 }
110
117 public function setCookie( $name, $value, $expire = 0, $options = [] ) {
118 $cookieConfig = $this->getCookieConfig();
119 $cookiePath = $cookieConfig->get( MainConfigNames::CookiePath );
120 $cookiePrefix = $cookieConfig->get( MainConfigNames::CookiePrefix );
121 $cookieDomain = $cookieConfig->get( MainConfigNames::CookieDomain );
122 $cookieSecure = $cookieConfig->get( MainConfigNames::CookieSecure );
123 $cookieExpiration = $cookieConfig->get( MainConfigNames::CookieExpiration );
124 $cookieHttpOnly = $cookieConfig->get( MainConfigNames::CookieHttpOnly );
125 $options = array_filter( $options, static function ( $a ) {
126 return $a !== null;
127 } ) + [
128 'prefix' => $cookiePrefix,
129 'domain' => $cookieDomain,
130 'path' => $cookiePath,
131 'secure' => $cookieSecure,
132 'httpOnly' => $cookieHttpOnly,
133 'raw' => false,
134 ];
135
136 if ( $expire === null ) {
137 $expire = 0; // Session cookie
138 } elseif ( $expire == 0 && $cookieExpiration != 0 ) {
139 $expire = time() + $cookieExpiration;
140 }
141
142 $this->cookies[$options['prefix'] . $name] = [
143 'value' => (string)$value,
144 'expire' => (int)$expire,
145 'path' => (string)$options['path'],
146 'domain' => (string)$options['domain'],
147 'secure' => (bool)$options['secure'],
148 'httpOnly' => (bool)$options['httpOnly'],
149 'raw' => (bool)$options['raw'],
150 ];
151 }
152
157 public function getCookie( $name ) {
158 if ( isset( $this->cookies[$name] ) ) {
159 return $this->cookies[$name]['value'];
160 }
161 return null;
162 }
163
168 public function getCookieData( $name ) {
169 return $this->cookies[$name] ?? null;
170 }
171
175 public function getCookies() {
176 return $this->cookies;
177 }
178
182 public function hasCookies() {
183 return count( $this->cookies ) > 0;
184 }
185
186}
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition WebStart.php:82
header( $string, $replace=true, $http_response_code=null)
Stores a HTTP header.
statusHeader( $code)
setCookie( $name, $value, $expire=0, $options=[])
setCookieConfig(Config $cookieConfig)
getStatusCode()
Get the HTTP response code, null if not set.
headersSent()
Test if headers have been sent.
getCookie( $name)
getCookieData( $name)
hasCookies()
Checks whether this request is performing cookie operations.bool 1.27
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
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.".