Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / WebResponse.php
blobf977c205aa6fff6cc8748eb6d5e92e233a01998f
1 <?php
2 /**
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
20 * @file
23 /**
24 * Allow programs to request this object from WebRequest::response()
25 * and handle all outputting (or lack of outputting) via it.
26 * @ingroup HTTP
28 class WebResponse {
30 /**
31 * Output a 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 );
40 /**
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.
44 * @since 1.25
46 public function getHeader( $key ) {
47 foreach ( headers_list() as $header ) {
48 list( $name, $val ) = explode( ':', $header, 2 );
49 if ( !strcasecmp( $name, $key ) ) {
50 return trim( $val );
53 return null;
56 /**
57 * Set the browser cookie
58 * @param string $name Name of cookie
59 * @param string $value Value to give cookie
60 * @param int|null $expire Unix timestamp (in seconds) when the cookie should expire.
61 * 0 (the default) causes it to expire $wgCookieExpiration seconds from now.
62 * null causes it to be a session cookie.
63 * @param array $options Assoc of additional cookie options:
64 * prefix: string, name prefix ($wgCookiePrefix)
65 * domain: string, cookie domain ($wgCookieDomain)
66 * path: string, cookie path ($wgCookiePath)
67 * secure: bool, secure attribute ($wgCookieSecure)
68 * httpOnly: bool, httpOnly attribute ($wgCookieHttpOnly)
69 * raw: bool, if true uses PHP's setrawcookie() instead of setcookie()
70 * For backwards compatibility, if $options is not an array then it and
71 * the following two parameters will be interpreted as values for
72 * 'prefix', 'domain', and 'secure'
73 * @since 1.22 Replaced $prefix, $domain, and $forceSecure with $options
75 public function setcookie( $name, $value, $expire = 0, $options = null ) {
76 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
77 global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
79 if ( !is_array( $options ) ) {
80 // Backwards compatibility
81 $options = array( 'prefix' => $options );
82 if ( func_num_args() >= 5 ) {
83 $options['domain'] = func_get_arg( 4 );
85 if ( func_num_args() >= 6 ) {
86 $options['secure'] = func_get_arg( 5 );
89 $options = array_filter( $options, function ( $a ) {
90 return $a !== null;
91 } ) + array(
92 'prefix' => $wgCookiePrefix,
93 'domain' => $wgCookieDomain,
94 'path' => $wgCookiePath,
95 'secure' => $wgCookieSecure,
96 'httpOnly' => $wgCookieHttpOnly,
97 'raw' => false,
100 if ( $expire === null ) {
101 $expire = 0; // Session cookie
102 } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
103 $expire = time() + $wgCookieExpiration;
106 $func = $options['raw'] ? 'setrawcookie' : 'setcookie';
108 if ( Hooks::run( 'WebResponseSetCookie', array( &$name, &$value, &$expire, $options ) ) ) {
109 wfDebugLog( 'cookie',
110 $func . ': "' . implode( '", "',
111 array(
112 $options['prefix'] . $name,
113 $value,
114 $expire,
115 $options['path'],
116 $options['domain'],
117 $options['secure'],
118 $options['httpOnly'] ) ) . '"' );
120 call_user_func( $func,
121 $options['prefix'] . $name,
122 $value,
123 $expire,
124 $options['path'],
125 $options['domain'],
126 $options['secure'],
127 $options['httpOnly'] );
133 * @ingroup HTTP
135 class FauxResponse extends WebResponse {
136 private $headers;
137 private $cookies;
138 private $code;
141 * Stores a HTTP header
142 * @param string $string Header to output
143 * @param bool $replace Replace current similar header
144 * @param null|int $http_response_code Forces the HTTP response code to the specified value.
146 public function header( $string, $replace = true, $http_response_code = null ) {
147 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
148 $parts = explode( ' ', $string, 3 );
149 $this->code = intval( $parts[1] );
150 } else {
151 list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
153 $key = strtoupper( $key );
155 if ( $replace || !isset( $this->headers[$key] ) ) {
156 $this->headers[$key] = $val;
160 if ( $http_response_code !== null ) {
161 $this->code = intval( $http_response_code );
166 * @param string $key The name of the header to get (case insensitive).
167 * @return string
169 public function getHeader( $key ) {
170 $key = strtoupper( $key );
172 if ( isset( $this->headers[$key] ) ) {
173 return $this->headers[$key];
175 return null;
179 * Get the HTTP response code, null if not set
181 * @return int|null
183 public function getStatusCode() {
184 return $this->code;
188 * @todo document. It just ignore optional parameters.
190 * @param string $name Name of cookie
191 * @param string $value Value to give cookie
192 * @param int $expire Number of seconds til cookie expires (Default: 0)
193 * @param array $options Ignored
195 public function setcookie( $name, $value, $expire = 0, $options = null ) {
196 $this->cookies[$name] = $value;
200 * @param string $name
201 * @return string
203 public function getcookie( $name ) {
204 if ( isset( $this->cookies[$name] ) ) {
205 return $this->cookies[$name];
207 return null;