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'] );
144 class FauxResponse
extends WebResponse
{
150 * Stores a HTTP header
151 * @param string $string Header to output
152 * @param bool $replace Replace current similar header
153 * @param null|int $http_response_code Forces the HTTP response code to the specified value.
155 public function header( $string, $replace = true, $http_response_code = null ) {
156 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
157 $parts = explode( ' ', $string, 3 );
158 $this->code
= intval( $parts[1] );
160 list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
162 $key = strtoupper( $key );
164 if ( $replace ||
!isset( $this->headers
[$key] ) ) {
165 $this->headers
[$key] = $val;
169 if ( $http_response_code !== null ) {
170 $this->code
= intval( $http_response_code );
176 * @param int $code Status code
178 public function statusHeader( $code ) {
179 $this->code
= intval( $code );
183 * @param string $key The name of the header to get (case insensitive).
184 * @return string|null The header value (if set); null otherwise.
186 public function getHeader( $key ) {
187 $key = strtoupper( $key );
189 if ( isset( $this->headers
[$key] ) ) {
190 return $this->headers
[$key];
196 * Get the HTTP response code, null if not set
200 public function getStatusCode() {
205 * @param string $name The name of the cookie.
206 * @param string $value The value to be stored in the cookie.
207 * @param int|null $expire Ignored in this faux subclass.
208 * @param array $options Ignored in this faux subclass.
210 public function setcookie( $name, $value, $expire = 0, $options = array() ) {
211 $this->cookies
[$name] = $value;
215 * @param string $name
216 * @return string|null
218 public function getcookie( $name ) {
219 if ( isset( $this->cookies
[$name] ) ) {
220 return $this->cookies
[$name];