1dded190eb6f71bb7390bc6ab9deae7dc14f3329
[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 * @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 class FauxResponse extends WebResponse {
61 private $headers;
62 private $cookies;
63
64 public function header( $string, $replace = true, $http_response_code = null ) {
65 list( $key, $val ) = explode( ":", $string, 2 );
66
67 if( $replace || !isset( $this->headers[$key] ) ) {
68 $this->headers[$key] = $val;
69 }
70 }
71
72 public function getheader( $key ) {
73 return $this->headers[$key];
74 }
75
76 public function setcookie( $name, $value, $expire = 0 ) {
77 $this->cookies[$name] = $value;
78 }
79
80 public function getcookie( $name ) {
81 if ( isset( $this->cookies[$name] ) ) {
82 return $this->cookies[$name];
83 }
84 }
85 }