3 * Classes used to send headers and cookies back to the user
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * Allow programs to request this object from WebRequest::response()
25 * and handle all outputting (or lack of outputting) via it.
30 /** @var array Used to record set cookies, because PHP's setcookie() will
31 * happily send an identical Set-Cookie to the client.
33 protected static $setCookies = [];
36 * Output an HTTP header, wrapper for PHP's header()
37 * @param string $string Header to output
38 * @param bool $replace Replace current similar header
39 * @param null|int $http_response_code Forces the HTTP response code to the specified value.
41 public function header( $string, $replace = true, $http_response_code = null ) {
42 header( $string, $replace, $http_response_code );
46 * Get a response header
47 * @param string $key The name of the header to get (case insensitive).
48 * @return string|null The header value (if set); null otherwise.
51 public function getHeader( $key ) {
52 foreach ( headers_list() as $header ) {
53 list( $name, $val ) = explode( ':', $header, 2 );
54 if ( !strcasecmp( $name, $key ) ) {
62 * Output an HTTP status code header
64 * @param int $code Status code
66 public function statusHeader( $code ) {
67 HttpStatus
::header( $code );
71 * Test if headers have been sent
75 public function headersSent() {
76 return headers_sent();
80 * Set the browser cookie
81 * @param string $name The name of the cookie.
82 * @param string $value The value to be stored in the cookie.
83 * @param int|null $expire Unix timestamp (in seconds) when the cookie should expire.
84 * 0 (the default) causes it to expire $wgCookieExpiration seconds from now.
85 * null causes it to be a session cookie.
86 * @param array $options Assoc of additional cookie options:
87 * prefix: string, name prefix ($wgCookiePrefix)
88 * domain: string, cookie domain ($wgCookieDomain)
89 * path: string, cookie path ($wgCookiePath)
90 * secure: bool, secure attribute ($wgCookieSecure)
91 * httpOnly: bool, httpOnly attribute ($wgCookieHttpOnly)
92 * raw: bool, if true uses PHP's setrawcookie() instead of setcookie()
93 * For backwards compatibility, if $options is not an array then it and
94 * the following two parameters will be interpreted as values for
95 * 'prefix', 'domain', and 'secure'
96 * @since 1.22 Replaced $prefix, $domain, and $forceSecure with $options
98 public function setCookie( $name, $value, $expire = 0, $options = [] ) {
99 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
100 global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
102 if ( !is_array( $options ) ) {
103 // Backwards compatibility
104 $options = [ 'prefix' => $options ];
105 if ( func_num_args() >= 5 ) {
106 $options['domain'] = func_get_arg( 4 );
108 if ( func_num_args() >= 6 ) {
109 $options['secure'] = func_get_arg( 5 );
112 $options = array_filter( $options, function ( $a ) {
115 'prefix' => $wgCookiePrefix,
116 'domain' => $wgCookieDomain,
117 'path' => $wgCookiePath,
118 'secure' => $wgCookieSecure,
119 'httpOnly' => $wgCookieHttpOnly,
123 if ( $expire === null ) {
124 $expire = 0; // Session cookie
125 } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
126 $expire = time() +
$wgCookieExpiration;
129 $func = $options['raw'] ?
'setrawcookie' : 'setcookie';
131 if ( Hooks
::run( 'WebResponseSetCookie', [ &$name, &$value, &$expire, &$options ] ) ) {
132 $cookie = $options['prefix'] . $name;
134 'name' => (string)$cookie,
135 'value' => (string)$value,
136 'expire' => (int)$expire,
137 'path' => (string)$options['path'],
138 'domain' => (string)$options['domain'],
139 'secure' => (bool)$options['secure'],
140 'httpOnly' => (bool)$options['httpOnly'],
143 // Per RFC 6265, key is name + domain + path
144 $key = "{$data['name']}\n{$data['domain']}\n{$data['path']}";
146 // If this cookie name was in the request, fake an entry in
147 // self::$setCookies for it so the deleting check works right.
148 if ( isset( $_COOKIE[$cookie] ) && !array_key_exists( $key, self
::$setCookies ) ) {
149 self
::$setCookies[$key] = [];
152 // PHP deletes if value is the empty string; also, a past expiry is deleting
153 $deleting = ( $data['value'] === '' ||
$data['expire'] > 0 && $data['expire'] <= time() );
155 if ( $deleting && !isset( self
::$setCookies[$key] ) ) { // isset( null ) is false
156 wfDebugLog( 'cookie', 'already deleted ' . $func . ': "' . implode( '", "', $data ) . '"' );
157 } elseif ( !$deleting && isset( self
::$setCookies[$key] ) &&
158 self
::$setCookies[$key] === [ $func, $data ]
160 wfDebugLog( 'cookie', 'already set ' . $func . ': "' . implode( '", "', $data ) . '"' );
162 wfDebugLog( 'cookie', $func . ': "' . implode( '", "', $data ) . '"' );
163 if ( call_user_func_array( $func, array_values( $data ) ) ) {
164 self
::$setCookies[$key] = $deleting ?
null : [ $func, $data ];
171 * Unset a browser cookie.
172 * This sets the cookie with an empty value and an expiry set to a time in the past,
173 * which will cause the browser to remove any cookie with the given name, domain and
174 * path from its cookie store. Options other than these (and prefix) have no effect.
175 * @param string $name Cookie name
176 * @param array $options Cookie options, see {@link setCookie()}
179 public function clearCookie( $name, $options = [] ) {
180 $this->setCookie( $name, '', time() - 31536000 /* 1 year */, $options );
184 * Checks whether this request is performing cookie operations
189 public function hasCookies() {
190 return (bool)self
::$setCookies;
197 class FauxResponse
extends WebResponse
{
199 private $cookies = [];
203 * Stores a HTTP header
204 * @param string $string Header to output
205 * @param bool $replace Replace current similar header
206 * @param null|int $http_response_code Forces the HTTP response code to the specified value.
208 public function header( $string, $replace = true, $http_response_code = null ) {
209 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
210 $parts = explode( ' ', $string, 3 );
211 $this->code
= intval( $parts[1] );
213 list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
215 $key = strtoupper( $key );
217 if ( $replace ||
!isset( $this->headers
[$key] ) ) {
218 $this->headers
[$key] = $val;
222 if ( $http_response_code !== null ) {
223 $this->code
= intval( $http_response_code );
229 * @param int $code Status code
231 public function statusHeader( $code ) {
232 $this->code
= intval( $code );
235 public function headersSent() {
240 * @param string $key The name of the header to get (case insensitive).
241 * @return string|null The header value (if set); null otherwise.
243 public function getHeader( $key ) {
244 $key = strtoupper( $key );
246 if ( isset( $this->headers
[$key] ) ) {
247 return $this->headers
[$key];
253 * Get the HTTP response code, null if not set
257 public function getStatusCode() {
262 * @param string $name The name of the cookie.
263 * @param string $value The value to be stored in the cookie.
264 * @param int|null $expire Ignored in this faux subclass.
265 * @param array $options Ignored in this faux subclass.
267 public function setCookie( $name, $value, $expire = 0, $options = [] ) {
268 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
269 global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
271 if ( !is_array( $options ) ) {
272 // Backwards compatibility
273 $options = [ 'prefix' => $options ];
274 if ( func_num_args() >= 5 ) {
275 $options['domain'] = func_get_arg( 4 );
277 if ( func_num_args() >= 6 ) {
278 $options['secure'] = func_get_arg( 5 );
281 $options = array_filter( $options, function ( $a ) {
284 'prefix' => $wgCookiePrefix,
285 'domain' => $wgCookieDomain,
286 'path' => $wgCookiePath,
287 'secure' => $wgCookieSecure,
288 'httpOnly' => $wgCookieHttpOnly,
292 if ( $expire === null ) {
293 $expire = 0; // Session cookie
294 } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
295 $expire = time() +
$wgCookieExpiration;
298 $this->cookies
[$options['prefix'] . $name] = [
299 'value' => (string)$value,
300 'expire' => (int)$expire,
301 'path' => (string)$options['path'],
302 'domain' => (string)$options['domain'],
303 'secure' => (bool)$options['secure'],
304 'httpOnly' => (bool)$options['httpOnly'],
305 'raw' => (bool)$options['raw'],
310 * @param string $name
311 * @return string|null
313 public function getCookie( $name ) {
314 if ( isset( $this->cookies
[$name] ) ) {
315 return $this->cookies
[$name]['value'];
321 * @param string $name
324 public function getCookieData( $name ) {
325 if ( isset( $this->cookies
[$name] ) ) {
326 return $this->cookies
[$name];
334 public function getCookies() {
335 return $this->cookies
;