Merge "Factor page name normalization out of MediaWikiSite"
[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 /**
27 * WebRequest clone which takes values from a provided array.
28 *
29 * @ingroup HTTP
30 */
31 class FauxRequest extends WebRequest {
32 private $wasPosted = false;
33 private $session = array();
34 private $requestUrl;
35 protected $cookies = array();
36
37 /**
38 * @param array $data Array of *non*-urlencoded key => value pairs, the
39 * fake GET/POST values
40 * @param bool $wasPosted Whether to treat the data as POST
41 * @param array|null $session Session array or null
42 * @param string $protocol 'http' or 'https'
43 * @throws MWException
44 */
45 public function __construct( $data = array(), $wasPosted = false,
46 $session = null, $protocol = 'http'
47 ) {
48 $this->requestTime = microtime( true );
49
50 if ( is_array( $data ) ) {
51 $this->data = $data;
52 } else {
53 throw new MWException( "FauxRequest() got bogus data" );
54 }
55 $this->wasPosted = $wasPosted;
56 if ( $session ) {
57 $this->session = $session;
58 }
59 $this->protocol = $protocol;
60 }
61
62 /**
63 * Initialise the header list
64 */
65 protected function initHeaders() {
66 // Nothing to init
67 }
68
69 /**
70 * @param string $name
71 * @param string $default
72 * @return string
73 */
74 public function getText( $name, $default = '' ) {
75 # Override; don't recode since we're using internal data
76 return (string)$this->getVal( $name, $default );
77 }
78
79 /**
80 * @return array
81 */
82 public function getValues() {
83 return $this->data;
84 }
85
86 /**
87 * @return array
88 */
89 public function getQueryValues() {
90 if ( $this->wasPosted ) {
91 return array();
92 } else {
93 return $this->data;
94 }
95 }
96
97 public function getMethod() {
98 return $this->wasPosted ? 'POST' : 'GET';
99 }
100
101 /**
102 * @return bool
103 */
104 public function wasPosted() {
105 return $this->wasPosted;
106 }
107
108 public function getCookie( $key, $prefix = null, $default = null ) {
109 if ( $prefix === null ) {
110 global $wgCookiePrefix;
111 $prefix = $wgCookiePrefix;
112 }
113 $name = $prefix . $key;
114 return isset( $this->cookies[$name] ) ? $this->cookies[$name] : $default;
115 }
116
117 /**
118 * @since 1.26
119 * @param string $name Unprefixed name of the cookie to set
120 * @param string|null $value Value of the cookie to set
121 * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
122 */
123 public function setCookie( $key, $value, $prefix = null ) {
124 $this->setCookies( array( $key => $value ), $prefix );
125 }
126
127 /**
128 * @since 1.26
129 * @param array $cookies
130 * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
131 */
132 public function setCookies( $cookies, $prefix = null ) {
133 if ( $prefix === null ) {
134 global $wgCookiePrefix;
135 $prefix = $wgCookiePrefix;
136 }
137 foreach ( $cookies as $key => $value ) {
138 $name = $prefix . $key;
139 $this->cookies[$name] = $value;
140 }
141 }
142
143 public function checkSessionCookie() {
144 return false;
145 }
146
147 /**
148 * @since 1.25
149 */
150 public function setRequestURL( $url ) {
151 $this->requestUrl = $url;
152 }
153
154 /**
155 * @since 1.25 MWException( "getRequestURL not implemented" )
156 * no longer thrown.
157 */
158 public function getRequestURL() {
159 if ( $this->requestUrl === null ) {
160 throw new MWException( 'Request URL not set' );
161 }
162 return $this->requestUrl;
163 }
164
165 public function getProtocol() {
166 return $this->protocol;
167 }
168
169 /**
170 * @param string $name
171 * @param string $val
172 */
173 public function setHeader( $name, $val ) {
174 $this->setHeaders( array( $name => $val ) );
175 }
176
177 /**
178 * @since 1.26
179 * @param array $headers
180 */
181 public function setHeaders( $headers ) {
182 foreach ( $headers as $name => $val ) {
183 $name = strtoupper( $name );
184 $this->headers[$name] = $val;
185 }
186 }
187
188 /**
189 * @param string $key
190 * @return array|null
191 */
192 public function getSessionData( $key ) {
193 if ( isset( $this->session[$key] ) ) {
194 return $this->session[$key];
195 }
196 return null;
197 }
198
199 /**
200 * @param string $key
201 * @param array $data
202 */
203 public function setSessionData( $key, $data ) {
204 $this->session[$key] = $data;
205 }
206
207 /**
208 * @return array|mixed|null
209 */
210 public function getSessionArray() {
211 return $this->session;
212 }
213
214 /**
215 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
216 * @return string
217 */
218 public function getRawQueryString() {
219 return '';
220 }
221
222 /**
223 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
224 * @return string
225 */
226 public function getRawPostString() {
227 return '';
228 }
229
230 /**
231 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
232 * @return string
233 */
234 public function getRawInput() {
235 return '';
236 }
237
238 /**
239 * @param array $extWhitelist
240 * @return bool
241 */
242 public function checkUrlExtension( $extWhitelist = array() ) {
243 return true;
244 }
245
246 /**
247 * @return string
248 */
249 protected function getRawIP() {
250 return '127.0.0.1';
251 }
252 }