Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
74.58% |
44 / 59 |
|
41.67% |
5 / 12 |
CRAP | |
0.00% |
0 / 1 |
FauxResponse | |
75.86% |
44 / 58 |
|
41.67% |
5 / 12 |
30.44 | |
0.00% |
0 / 1 |
header | |
63.64% |
7 / 11 |
|
0.00% |
0 / 1 |
7.73 | |||
statusHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
headersSent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getHeader | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getStatusCode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCookieConfig | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
setCookieConfig | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setCookie | |
90.62% |
29 / 32 |
|
0.00% |
0 / 1 |
5.02 | |||
getCookie | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getCookieData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCookies | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasCookies | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Classes used to send headers and cookies back to the user |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; if not, write to the Free Software Foundation, Inc., |
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
19 | * http://www.gnu.org/copyleft/gpl.html |
20 | * |
21 | * @file |
22 | */ |
23 | |
24 | namespace MediaWiki\Request; |
25 | |
26 | use MediaWiki\Config\Config; |
27 | use MediaWiki\MainConfigNames; |
28 | use MediaWiki\MediaWikiServices; |
29 | |
30 | /** |
31 | * @ingroup HTTP |
32 | */ |
33 | class FauxResponse extends WebResponse { |
34 | /** @var null|string[] */ |
35 | private $headers; |
36 | /** @var array[] */ |
37 | private $cookies = []; |
38 | /** @var int|null */ |
39 | private $code; |
40 | |
41 | /** @var ?Config */ |
42 | private $cookieConfig = null; |
43 | |
44 | /** |
45 | * Stores a HTTP header |
46 | * @param string $string Header to output |
47 | * @param bool $replace Replace current similar header |
48 | * @param null|int $http_response_code Forces the HTTP response code to the specified value. |
49 | */ |
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 | |
73 | /** |
74 | * @since 1.26 |
75 | * @param int $code Status code |
76 | */ |
77 | public function statusHeader( $code ) { |
78 | $this->code = intval( $code ); |
79 | } |
80 | |
81 | public function headersSent() { |
82 | return false; |
83 | } |
84 | |
85 | /** |
86 | * @param string $key The name of the header to get (case insensitive). |
87 | * @return string|null The header value (if set); null otherwise. |
88 | */ |
89 | public function getHeader( $key ) { |
90 | $key = strtoupper( $key ); |
91 | |
92 | return $this->headers[$key] ?? null; |
93 | } |
94 | |
95 | /** |
96 | * Get the HTTP response code, null if not set |
97 | * |
98 | * @return int|null |
99 | */ |
100 | public function getStatusCode() { |
101 | return $this->code; |
102 | } |
103 | |
104 | /** |
105 | * @return Config |
106 | */ |
107 | private function getCookieConfig(): Config { |
108 | if ( !$this->cookieConfig ) { |
109 | $this->cookieConfig = MediaWikiServices::getInstance()->getMainConfig(); |
110 | } |
111 | return $this->cookieConfig; |
112 | } |
113 | |
114 | /** |
115 | * @param Config $cookieConfig |
116 | */ |
117 | public function setCookieConfig( Config $cookieConfig ): void { |
118 | $this->cookieConfig = $cookieConfig; |
119 | } |
120 | |
121 | /** |
122 | * @param string $name The name of the cookie. |
123 | * @param string $value The value to be stored in the cookie. |
124 | * @param int|null $expire Ignored in this faux subclass. |
125 | * @param array $options Ignored in this faux subclass. |
126 | */ |
127 | public function setCookie( $name, $value, $expire = 0, $options = [] ) { |
128 | if ( $this->disableForPostSend ) { |
129 | return; |
130 | } |
131 | |
132 | $cookieConfig = $this->getCookieConfig(); |
133 | $cookiePath = $cookieConfig->get( MainConfigNames::CookiePath ); |
134 | $cookiePrefix = $cookieConfig->get( MainConfigNames::CookiePrefix ); |
135 | $cookieDomain = $cookieConfig->get( MainConfigNames::CookieDomain ); |
136 | $cookieSecure = $cookieConfig->get( MainConfigNames::CookieSecure ); |
137 | $cookieExpiration = $cookieConfig->get( MainConfigNames::CookieExpiration ); |
138 | $cookieHttpOnly = $cookieConfig->get( MainConfigNames::CookieHttpOnly ); |
139 | $options = array_filter( $options, static function ( $a ) { |
140 | return $a !== null; |
141 | } ) + [ |
142 | 'prefix' => $cookiePrefix, |
143 | 'domain' => $cookieDomain, |
144 | 'path' => $cookiePath, |
145 | 'secure' => $cookieSecure, |
146 | 'httpOnly' => $cookieHttpOnly, |
147 | 'raw' => false, |
148 | ]; |
149 | |
150 | if ( $expire === null ) { |
151 | $expire = 0; // Session cookie |
152 | } elseif ( $expire == 0 && $cookieExpiration != 0 ) { |
153 | $expire = time() + $cookieExpiration; |
154 | } |
155 | |
156 | $this->cookies[$options['prefix'] . $name] = [ |
157 | 'value' => (string)$value, |
158 | 'expire' => (int)$expire, |
159 | 'path' => (string)$options['path'], |
160 | 'domain' => (string)$options['domain'], |
161 | 'secure' => (bool)$options['secure'], |
162 | 'httpOnly' => (bool)$options['httpOnly'], |
163 | 'raw' => (bool)$options['raw'], |
164 | ]; |
165 | } |
166 | |
167 | /** |
168 | * @param string $name |
169 | * @return string|null |
170 | */ |
171 | public function getCookie( $name ) { |
172 | if ( isset( $this->cookies[$name] ) ) { |
173 | return $this->cookies[$name]['value']; |
174 | } |
175 | return null; |
176 | } |
177 | |
178 | /** |
179 | * @param string $name |
180 | * @return array|null |
181 | */ |
182 | public function getCookieData( $name ) { |
183 | return $this->cookies[$name] ?? null; |
184 | } |
185 | |
186 | /** |
187 | * @return array[] |
188 | */ |
189 | public function getCookies() { |
190 | return $this->cookies; |
191 | } |
192 | |
193 | /** |
194 | * @inheritDoc |
195 | */ |
196 | public function hasCookies() { |
197 | return count( $this->cookies ) > 0; |
198 | } |
199 | |
200 | } |
201 | |
202 | /** @deprecated class alias since 1.40 */ |
203 | class_alias( FauxResponse::class, 'FauxResponse' ); |