]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/WebResponse.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.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();
37                 wfDebugLog( 'cookie',
38                         'setcookie: "' . implode( '", "',
39                                 array(
40                                         $wgCookiePrefix . $name,
41                                         $value,
42                                         $expire,
43                                         $wgCookiePath,
44                                         $wgCookieDomain,
45                                         $wgCookieSecure,
46                                         $httpOnlySafe && $wgCookieHttpOnly ) ) . '"' );
47                 if( $httpOnlySafe && isset( $wgCookieHttpOnly ) ) {
48                         setcookie( $wgCookiePrefix . $name,
49                                 $value,
50                                 $expire,
51                                 $wgCookiePath,
52                                 $wgCookieDomain,
53                                 $wgCookieSecure,
54                                 $wgCookieHttpOnly );
55                 } else {
56                         // setcookie() fails on PHP 5.1 if you give it future-compat paramters.
57                         // stab stab!
58                         setcookie( $wgCookiePrefix . $name,
59                                 $value,
60                                 $expire,
61                                 $wgCookiePath,
62                                 $wgCookieDomain,
63                                 $wgCookieSecure );
64                 }
65         }
66 }
67
68
69 class FauxResponse extends WebResponse {
70         private $headers;
71         private $cookies;
72
73         public function header($string, $replace=true) {
74                 list($key, $val) = explode(":", $string, 2);
75
76                 if($replace || !isset($this->headers[$key])) {
77                         $this->headers[$key] = $val;
78                 }
79         }
80
81         public function getheader($key) {
82                 return $this->headers[$key];
83         }
84
85         public function setcookie( $name, $value, $expire = 0 ) {
86                 $this->cookies[$name] = $value;
87         }
88
89         public function getcookie( $name )  {
90                 if ( isset($this->cookies[$name]) ) {
91                         return $this->cookies[$name];
92                 }
93         }
94 }