Http::getProxy() method to get proxy configuration
[lhc/web/wiklou.git] / includes / WebResponse.php
1 <?php
2 /**
3 * Classes used to send headers and cookies back to the user
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
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
27 */
28 class WebResponse {
29
30 /**
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.
35 */
36 public function header( $string, $replace = true, $http_response_code = null ) {
37 header( $string, $replace, $http_response_code );
38 }
39
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
45 */
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 );
51 }
52 }
53 return null;
54 }
55
56 /**
57 * Output an HTTP status code header
58 * @since 1.26
59 * @param int $code Status code
60 */
61 public function statusHeader( $code ) {
62 HttpStatus::header( $code );
63 }
64
65 /**
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
83 */
84 public function setCookie( $name, $value, $expire = 0, $options = array() ) {
85 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
86 global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
87
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 );
93 }
94 if ( func_num_args() >= 6 ) {
95 $options['secure'] = func_get_arg( 5 );
96 }
97 }
98 $options = array_filter( $options, function ( $a ) {
99 return $a !== null;
100 } ) + array(
101 'prefix' => $wgCookiePrefix,
102 'domain' => $wgCookieDomain,
103 'path' => $wgCookiePath,
104 'secure' => $wgCookieSecure,
105 'httpOnly' => $wgCookieHttpOnly,
106 'raw' => false,
107 );
108
109 if ( $expire === null ) {
110 $expire = 0; // Session cookie
111 } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
112 $expire = time() + $wgCookieExpiration;
113 }
114
115 $func = $options['raw'] ? 'setrawcookie' : 'setcookie';
116
117 if ( Hooks::run( 'WebResponseSetCookie', array( &$name, &$value, &$expire, $options ) ) ) {
118 wfDebugLog( 'cookie',
119 $func . ': "' . implode( '", "',
120 array(
121 $options['prefix'] . $name,
122 $value,
123 $expire,
124 $options['path'],
125 $options['domain'],
126 $options['secure'],
127 $options['httpOnly'] ) ) . '"' );
128
129 call_user_func( $func,
130 $options['prefix'] . $name,
131 $value,
132 $expire,
133 $options['path'],
134 $options['domain'],
135 $options['secure'],
136 $options['httpOnly'] );
137 }
138 }
139
140 /**
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()}
147 * @since 1.27
148 */
149 public function clearCookie( $name, $options = array() ) {
150 $this->setCookie( $name, '', time() - 31536000 /* 1 year */, $options );
151 }
152 }
153
154 /**
155 * @ingroup HTTP
156 */
157 class FauxResponse extends WebResponse {
158 private $headers;
159 private $cookies;
160 private $code;
161
162 /**
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.
167 */
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] );
172 } else {
173 list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
174
175 $key = strtoupper( $key );
176
177 if ( $replace || !isset( $this->headers[$key] ) ) {
178 $this->headers[$key] = $val;
179 }
180 }
181
182 if ( $http_response_code !== null ) {
183 $this->code = intval( $http_response_code );
184 }
185 }
186
187 /**
188 * @since 1.26
189 * @param int $code Status code
190 */
191 public function statusHeader( $code ) {
192 $this->code = intval( $code );
193 }
194
195 /**
196 * @param string $key The name of the header to get (case insensitive).
197 * @return string|null The header value (if set); null otherwise.
198 */
199 public function getHeader( $key ) {
200 $key = strtoupper( $key );
201
202 if ( isset( $this->headers[$key] ) ) {
203 return $this->headers[$key];
204 }
205 return null;
206 }
207
208 /**
209 * Get the HTTP response code, null if not set
210 *
211 * @return int|null
212 */
213 public function getStatusCode() {
214 return $this->code;
215 }
216
217 /**
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.
222 */
223 public function setCookie( $name, $value, $expire = 0, $options = array() ) {
224 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
225 global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
226
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 );
232 }
233 if ( func_num_args() >= 6 ) {
234 $options['secure'] = func_get_arg( 5 );
235 }
236 }
237 $options = array_filter( $options, function ( $a ) {
238 return $a !== null;
239 } ) + array(
240 'prefix' => $wgCookiePrefix,
241 'domain' => $wgCookieDomain,
242 'path' => $wgCookiePath,
243 'secure' => $wgCookieSecure,
244 'httpOnly' => $wgCookieHttpOnly,
245 'raw' => false,
246 );
247
248 if ( $expire === null ) {
249 $expire = 0; // Session cookie
250 } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
251 $expire = time() + $wgCookieExpiration;
252 }
253
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'],
262 );
263 }
264
265 /**
266 * @param string $name
267 * @return string|null
268 */
269 public function getCookie( $name ) {
270 if ( isset( $this->cookies[$name] ) ) {
271 return $this->cookies[$name]['value'];
272 }
273 return null;
274 }
275
276 /**
277 * @param string $name
278 * @return array|null
279 */
280 public function getCookieData( $name ) {
281 if ( isset( $this->cookies[$name] ) ) {
282 return $this->cookies[$name];
283 }
284 return null;
285 }
286
287 /**
288 * @return array
289 */
290 public function getCookies() {
291 return $this->cookies;
292 }
293 }