9baaf3ba869189fb23e6b7367dfde9fe64aba623
[lhc/web/wiklou.git] / 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 * @param $http_response_code null|int Forces the HTTP response code to the specified value.
21 */
22 public function header( $string, $replace = true, $http_response_code = null ) {
23 header( $string, $replace, $http_response_code );
24 }
25
26 /**
27 * Set the browser cookie
28 * @param $name String: name of cookie
29 * @param $value String: value to give cookie
30 * @param $expire Int: number of seconds til cookie expires
31 */
32 public function setcookie( $name, $value, $expire = 0 ) {
33 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
34 global $wgCookieSecure,$wgCookieExpiration, $wgCookieHttpOnly;
35 if ( $expire == 0 ) {
36 $expire = time() + $wgCookieExpiration;
37 }
38 $httpOnlySafe = wfHttpOnlySafe() && $wgCookieHttpOnly;
39 wfDebugLog( 'cookie',
40 'setcookie: "' . implode( '", "',
41 array(
42 $wgCookiePrefix . $name,
43 $value,
44 $expire,
45 $wgCookiePath,
46 $wgCookieDomain,
47 $wgCookieSecure,
48 $httpOnlySafe ) ) . '"' );
49 setcookie( $wgCookiePrefix . $name,
50 $value,
51 $expire,
52 $wgCookiePath,
53 $wgCookieDomain,
54 $wgCookieSecure,
55 $httpOnlySafe );
56 }
57 }
58
59 /**
60 * @ingroup HTTP
61 */
62 class FauxResponse extends WebResponse {
63 private $headers;
64 private $cookies;
65
66 /**
67 * Stores a HTTP header
68 * @param $string String: header to output
69 * @param $replace Bool: replace current similar header
70 * @param $http_response_code null|int Forces the HTTP response code to the specified value.
71 */
72 public function header( $string, $replace = true, $http_response_code = null ) {
73 list( $key, $val ) = explode( ":", $string, 2 );
74
75 if( $replace || !isset( $this->headers[$key] ) ) {
76 $this->headers[$key] = $val;
77 }
78 }
79
80 /**
81 * @param $key string
82 * @return string
83 */
84 public function getheader( $key ) {
85 return $this->headers[$key];
86 }
87
88 /**
89 * @param $name String: name of cookie
90 * @param $value String: value to give cookie
91 * @param $expire Int: number of seconds til cookie expires
92 */
93 public function setcookie( $name, $value, $expire = 0 ) {
94 $this->cookies[$name] = $value;
95 }
96
97 /**
98 * @param $name string
99 * @return string
100 */
101 public function getcookie( $name ) {
102 if ( isset( $this->cookies[$name] ) ) {
103 return $this->cookies[$name];
104 }
105 return null;
106 }
107 }