Followup rr86304
[lhc/web/wiklou.git] / includes / WebResponse.php
1 <?php
2 /**
3 * Classes used to send headers and cookies back to the user
4 *
5 * @file
6 */
7
8 /**
9 * Allow programs to request this object from WebRequest::response()
10 * and handle all outputting (or lack of outputting) via it.
11 * @ingroup HTTP
12 */
13 class WebResponse {
14
15 /**
16 * Output a HTTP header, wrapper for PHP's
17 * header()
18 * @param $string String: header to output
19 * @param $replace Bool: replace current similar header
20 */
21 public function header($string, $replace=true) {
22 header($string,$replace);
23 }
24
25 /** Set the browser cookie
26 * @param $name String: name of cookie
27 * @param $value String: value to give cookie
28 * @param $expire Int: number of seconds til cookie expires
29 */
30 public function setcookie( $name, $value, $expire = 0 ) {
31 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
32 global $wgCookieSecure,$wgCookieExpiration, $wgCookieHttpOnly;
33 if ( $expire == 0 ) {
34 $expire = time() + $wgCookieExpiration;
35 }
36 $httpOnlySafe = wfHttpOnlySafe() && $wgCookieHttpOnly;
37 wfDebugLog( 'cookie',
38 'setcookie: "' . implode( '", "',
39 array(
40 $wgCookiePrefix . $name,
41 $value,
42 $expire,
43 $wgCookiePath,
44 $wgCookieDomain,
45 $wgCookieSecure,
46 $httpOnlySafe ) ) . '"' );
47 setcookie( $wgCookiePrefix . $name,
48 $value,
49 $expire,
50 $wgCookiePath,
51 $wgCookieDomain,
52 $wgCookieSecure,
53 $httpOnlySafe );
54 }
55 }
56
57
58 class FauxResponse extends WebResponse {
59 private $headers;
60 private $cookies;
61
62 public function header($string, $replace=true) {
63 list($key, $val) = explode(":", $string, 2);
64
65 if($replace || !isset($this->headers[$key])) {
66 $this->headers[$key] = $val;
67 }
68 }
69
70 public function getheader($key) {
71 return $this->headers[$key];
72 }
73
74 public function setcookie( $name, $value, $expire = 0 ) {
75 $this->cookies[$name] = $value;
76 }
77
78 public function getcookie( $name ) {
79 if ( isset($this->cookies[$name]) ) {
80 return $this->cookies[$name];
81 }
82 }
83 }