Localisation updates from https://translatewiki.net.
[lhc/web/wiklou.git] / includes / FauxRequest.php
1 <?php
2 /**
3 * Deal with importing all those nasty globals and things
4 *
5 * Copyright © 2003 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 use MediaWiki\Session\SessionManager;
27
28 /**
29 * WebRequest clone which takes values from a provided array.
30 *
31 * @ingroup HTTP
32 */
33 class FauxRequest extends WebRequest {
34 private $wasPosted = false;
35 private $requestUrl;
36 protected $cookies = [];
37
38 /**
39 * @param array $data Array of *non*-urlencoded key => value pairs, the
40 * fake GET/POST values
41 * @param bool $wasPosted Whether to treat the data as POST
42 * @param MediaWiki\Session\Session|array|null $session Session, session
43 * data array, or null
44 * @param string $protocol 'http' or 'https'
45 * @throws MWException
46 */
47 public function __construct( $data = [], $wasPosted = false,
48 $session = null, $protocol = 'http'
49 ) {
50 $this->requestTime = microtime( true );
51
52 if ( is_array( $data ) ) {
53 $this->data = $data;
54 } else {
55 throw new MWException( "FauxRequest() got bogus data" );
56 }
57 $this->wasPosted = $wasPosted;
58 if ( $session instanceof MediaWiki\Session\Session ) {
59 $this->sessionId = $session->getSessionId();
60 } elseif ( is_array( $session ) ) {
61 $mwsession = SessionManager::singleton()->getEmptySession( $this );
62 $this->sessionId = $mwsession->getSessionId();
63 foreach ( $session as $key => $value ) {
64 $mwsession->set( $key, $value );
65 }
66 } elseif ( $session !== null ) {
67 throw new MWException( "FauxRequest() got bogus session" );
68 }
69 $this->protocol = $protocol;
70 }
71
72 /**
73 * Initialise the header list
74 */
75 protected function initHeaders() {
76 // Nothing to init
77 }
78
79 /**
80 * @param string $name
81 * @param string $default
82 * @return string
83 */
84 public function getText( $name, $default = '' ) {
85 # Override; don't recode since we're using internal data
86 return (string)$this->getVal( $name, $default );
87 }
88
89 /**
90 * @return array
91 * @suppress PhanParamSignatureMismatch
92 */
93 public function getValues() {
94 return $this->data;
95 }
96
97 /**
98 * @return array
99 */
100 public function getQueryValues() {
101 if ( $this->wasPosted ) {
102 return [];
103 } else {
104 return $this->data;
105 }
106 }
107
108 public function getMethod() {
109 return $this->wasPosted ? 'POST' : 'GET';
110 }
111
112 /**
113 * @return bool
114 */
115 public function wasPosted() {
116 return $this->wasPosted;
117 }
118
119 public function getCookie( $key, $prefix = null, $default = null ) {
120 if ( $prefix === null ) {
121 global $wgCookiePrefix;
122 $prefix = $wgCookiePrefix;
123 }
124 $name = $prefix . $key;
125 return $this->cookies[$name] ?? $default;
126 }
127
128 /**
129 * @since 1.26
130 * @param string $key Unprefixed name of the cookie to set
131 * @param string|null $value Value of the cookie to set
132 * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
133 */
134 public function setCookie( $key, $value, $prefix = null ) {
135 $this->setCookies( [ $key => $value ], $prefix );
136 }
137
138 /**
139 * @since 1.26
140 * @param array $cookies
141 * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
142 */
143 public function setCookies( $cookies, $prefix = null ) {
144 if ( $prefix === null ) {
145 global $wgCookiePrefix;
146 $prefix = $wgCookiePrefix;
147 }
148 foreach ( $cookies as $key => $value ) {
149 $name = $prefix . $key;
150 $this->cookies[$name] = $value;
151 }
152 }
153
154 /**
155 * @since 1.25
156 * @param string $url
157 */
158 public function setRequestURL( $url ) {
159 $this->requestUrl = $url;
160 }
161
162 /**
163 * @since 1.25 MWException( "getRequestURL not implemented" )
164 * no longer thrown.
165 * @return string
166 */
167 public function getRequestURL() {
168 if ( $this->requestUrl === null ) {
169 throw new MWException( 'Request URL not set' );
170 }
171 return $this->requestUrl;
172 }
173
174 public function getProtocol() {
175 return $this->protocol;
176 }
177
178 /**
179 * @param string $name
180 * @param string $val
181 */
182 public function setHeader( $name, $val ) {
183 $this->setHeaders( [ $name => $val ] );
184 }
185
186 /**
187 * @since 1.26
188 * @param array $headers
189 */
190 public function setHeaders( $headers ) {
191 foreach ( $headers as $name => $val ) {
192 $name = strtoupper( $name );
193 $this->headers[$name] = $val;
194 }
195 }
196
197 /**
198 * @return array|null
199 */
200 public function getSessionArray() {
201 if ( $this->sessionId !== null ) {
202 return iterator_to_array( $this->getSession() );
203 }
204 return null;
205 }
206
207 /**
208 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
209 * @return string
210 */
211 public function getRawQueryString() {
212 return '';
213 }
214
215 /**
216 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
217 * @return string
218 */
219 public function getRawPostString() {
220 return '';
221 }
222
223 /**
224 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
225 * @return string
226 */
227 public function getRawInput() {
228 return '';
229 }
230
231 /**
232 * @codeCoverageIgnore
233 * @param array $extWhitelist
234 * @return bool
235 */
236 public function checkUrlExtension( $extWhitelist = [] ) {
237 return true;
238 }
239
240 /**
241 * @codeCoverageIgnore
242 * @return string
243 */
244 protected function getRawIP() {
245 return '127.0.0.1';
246 }
247 }