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.
31 * Output an HTTP header, wrapper for PHP's header()
32 * @param string $string Header to output
33 * @param bool $replace Replace current similar header
34 * @param null|int $http_response_code Forces the HTTP response code to the specified value.
36 public function header( $string, $replace = true, $http_response_code = null ) {
37 header( $string, $replace, $http_response_code );
41 * Get a response header
42 * @param string $key The name of the header to get (case insensitive).
43 * @return string|null The header value (if set); null otherwise.
46 public function getHeader( $key ) {
47 foreach ( headers_list() as $header ) {
48 list( $name, $val ) = explode( ':', $header, 2 );
49 if ( !strcasecmp( $name, $key ) ) {
57 * Output an HTTP status code header
59 * @param int $code Status code
61 public function statusHeader( $code ) {
62 HttpStatus
::header( $code );
66 * Set the browser cookie
67 * @param string $name The name of the cookie.
68 * @param string $value The value to be stored in the cookie.
69 * @param int|null $expire Unix timestamp (in seconds) when the cookie should expire.
70 * 0 (the default) causes it to expire $wgCookieExpiration seconds from now.
71 * null causes it to be a session cookie.
72 * @param array $options Assoc of additional cookie options:
73 * prefix: string, name prefix ($wgCookiePrefix)
74 * domain: string, cookie domain ($wgCookieDomain)
75 * path: string, cookie path ($wgCookiePath)
76 * secure: bool, secure attribute ($wgCookieSecure)
77 * httpOnly: bool, httpOnly attribute ($wgCookieHttpOnly)
78 * raw: bool, if true uses PHP's setrawcookie() instead of setcookie()
79 * For backwards compatibility, if $options is not an array then it and
80 * the following two parameters will be interpreted as values for
81 * 'prefix', 'domain', and 'secure'
82 * @since 1.22 Replaced $prefix, $domain, and $forceSecure with $options
84 public function setCookie( $name, $value, $expire = 0, $options = array() ) {
85 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
86 global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
88 if ( !is_array( $options ) ) {
89 // Backwards compatibility
90 $options = array( 'prefix' => $options );
91 if ( func_num_args() >= 5 ) {
92 $options['domain'] = func_get_arg( 4 );
94 if ( func_num_args() >= 6 ) {
95 $options['secure'] = func_get_arg( 5 );
98 $options = array_filter( $options, function ( $a ) {
101 'prefix' => $wgCookiePrefix,
102 'domain' => $wgCookieDomain,
103 'path' => $wgCookiePath,
104 'secure' => $wgCookieSecure,
105 'httpOnly' => $wgCookieHttpOnly,
109 if ( $expire === null ) {
110 $expire = 0; // Session cookie
111 } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
112 $expire = time() +
$wgCookieExpiration;
115 $func = $options['raw'] ?
'setrawcookie' : 'setcookie';
117 if ( Hooks
::run( 'WebResponseSetCookie', array( &$name, &$value, &$expire, $options ) ) ) {
118 wfDebugLog( 'cookie',
119 $func . ': "' . implode( '", "',
121 $options['prefix'] . $name,
127 $options['httpOnly'] ) ) . '"' );
129 call_user_func( $func,
130 $options['prefix'] . $name,
136 $options['httpOnly'] );
141 * Unset a browser cookie.
142 * This sets the cookie with an empty value and an expiry set to a time in the past,
143 * which will cause the browser to remove any cookie with the given name, domain and
144 * path from its cookie store. Options other than these (and prefix) have no effect.
145 * @param string $name Cookie name
146 * @param array $options Cookie options, see {@link setCookie()}
149 public function clearCookie( $name, $options = array() ) {
150 $this->setCookie( $name, '', time() - 31536000 /* 1 year */, $options );
157 class FauxResponse
extends WebResponse
{
163 * Stores a HTTP header
164 * @param string $string Header to output
165 * @param bool $replace Replace current similar header
166 * @param null|int $http_response_code Forces the HTTP response code to the specified value.
168 public function header( $string, $replace = true, $http_response_code = null ) {
169 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
170 $parts = explode( ' ', $string, 3 );
171 $this->code
= intval( $parts[1] );
173 list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
175 $key = strtoupper( $key );
177 if ( $replace ||
!isset( $this->headers
[$key] ) ) {
178 $this->headers
[$key] = $val;
182 if ( $http_response_code !== null ) {
183 $this->code
= intval( $http_response_code );
189 * @param int $code Status code
191 public function statusHeader( $code ) {
192 $this->code
= intval( $code );
196 * @param string $key The name of the header to get (case insensitive).
197 * @return string|null The header value (if set); null otherwise.
199 public function getHeader( $key ) {
200 $key = strtoupper( $key );
202 if ( isset( $this->headers
[$key] ) ) {
203 return $this->headers
[$key];
209 * Get the HTTP response code, null if not set
213 public function getStatusCode() {
218 * @param string $name The name of the cookie.
219 * @param string $value The value to be stored in the cookie.
220 * @param int|null $expire Ignored in this faux subclass.
221 * @param array $options Ignored in this faux subclass.
223 public function setCookie( $name, $value, $expire = 0, $options = array() ) {
224 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
225 global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
227 if ( !is_array( $options ) ) {
228 // Backwards compatibility
229 $options = array( 'prefix' => $options );
230 if ( func_num_args() >= 5 ) {
231 $options['domain'] = func_get_arg( 4 );
233 if ( func_num_args() >= 6 ) {
234 $options['secure'] = func_get_arg( 5 );
237 $options = array_filter( $options, function ( $a ) {
240 'prefix' => $wgCookiePrefix,
241 'domain' => $wgCookieDomain,
242 'path' => $wgCookiePath,
243 'secure' => $wgCookieSecure,
244 'httpOnly' => $wgCookieHttpOnly,
248 if ( $expire === null ) {
249 $expire = 0; // Session cookie
250 } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
251 $expire = time() +
$wgCookieExpiration;
254 $this->cookies
[$options['prefix'] . $name] = array(
255 'value' => (string)$value,
256 'expire' => (int)$expire,
257 'path' => (string)$options['path'],
258 'domain' => (string)$options['domain'],
259 'secure' => (bool)$options['secure'],
260 'httpOnly' => (bool)$options['httpOnly'],
261 'raw' => (bool)$options['raw'],
266 * @param string $name
267 * @return string|null
269 public function getCookie( $name ) {
270 if ( isset( $this->cookies
[$name] ) ) {
271 return $this->cookies
[$name]['value'];
277 * @param string $name
280 public function getCookieData( $name ) {
281 if ( isset( $this->cookies
[$name] ) ) {
282 return $this->cookies
[$name];
290 public function getCookies() {
291 return $this->cookies
;