3862354acaafa2346d5c437a8e1e83b4aa02f7f6
[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 a HTTP header, wrapper for PHP's
32 * header()
33 * @param $string String: header to output
34 * @param $replace Bool: replace current similar header
35 * @param $http_response_code null|int Forces the HTTP response code to the specified value.
36 */
37 public function header( $string, $replace = true, $http_response_code = null ) {
38 header( $string, $replace, $http_response_code );
39 }
40
41 /**
42 * Set the browser cookie
43 * @param $name String: name of cookie
44 * @param $value String: value to give cookie
45 * @param $expire Int: Unix timestamp (in seconds) when the cookie should expire.
46 * 0 (the default) causes it to expire $wgCookieExpiration seconds from now.
47 * @param $prefix String: Prefix to use, if not $wgCookiePrefix (use '' for no prefix)
48 * @param $domain String: Cookie domain to use, if not $wgCookieDomain
49 * @param $forceSecure Bool:
50 * true: force the cookie to be set with the secure attribute
51 * false: force the cookie to be set without the secure attribute
52 * null: use the value from $wgCookieSecure
53 */
54 public function setcookie( $name, $value, $expire = 0, $prefix = null, $domain = null, $forceSecure = null ) {
55 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
56 global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
57 if ( $expire == 0 ) {
58 $expire = time() + $wgCookieExpiration;
59 }
60 if( $prefix === null ) {
61 $prefix = $wgCookiePrefix;
62 }
63 if( $domain === null ) {
64 $domain = $wgCookieDomain;
65 }
66
67 if ( is_null( $forceSecure ) ) {
68 $secureCookie = $wgCookieSecure;
69 } else {
70 $secureCookie = $forceSecure;
71 }
72
73 $httpOnlySafe = wfHttpOnlySafe() && $wgCookieHttpOnly;
74 wfDebugLog( 'cookie',
75 'setcookie: "' . implode( '", "',
76 array(
77 $prefix . $name,
78 $value,
79 $expire,
80 $wgCookiePath,
81 $domain,
82 $secureCookie,
83 $httpOnlySafe ) ) . '"' );
84 setcookie( $prefix . $name,
85 $value,
86 $expire,
87 $wgCookiePath,
88 $domain,
89 $secureCookie,
90 $httpOnlySafe );
91 }
92 }
93
94 /**
95 * @ingroup HTTP
96 */
97 class FauxResponse extends WebResponse {
98 private $headers;
99 private $cookies;
100 private $code;
101
102 /**
103 * Stores a HTTP header
104 * @param $string String: header to output
105 * @param $replace Bool: replace current similar header
106 * @param $http_response_code null|int Forces the HTTP response code to the specified value.
107 */
108 public function header( $string, $replace = true, $http_response_code = null ) {
109 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
110 $parts = explode( ' ', $string, 3 );
111 $this->code = intval( $parts[1] );
112 } else {
113 list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
114
115 if( $replace || !isset( $this->headers[$key] ) ) {
116 $this->headers[$key] = $val;
117 }
118 }
119
120 if ( $http_response_code !== null ) {
121 $this->code = intval( $http_response_code );
122 }
123 }
124
125 /**
126 * @param $key string
127 * @return string
128 */
129 public function getheader( $key ) {
130 if ( isset( $this->headers[$key] ) ) {
131 return $this->headers[$key];
132 }
133 return null;
134 }
135
136 /**
137 * Get the HTTP response code, null if not set
138 *
139 * @return Int or null
140 */
141 public function getStatusCode() {
142 return $this->code;
143 }
144
145 /**
146 * @todo document. It just ignore optional parameters.
147 *
148 * @param $name String: name of cookie
149 * @param $value String: value to give cookie
150 * @param $expire Int: number of seconds til cookie expires (Default: 0)
151 * @param $prefix TODO DOCUMENT (Default: null)
152 * @param $domain TODO DOCUMENT (Default: null)
153 * @param $forceSecure TODO DOCUMENT (Default: null)
154 */
155 public function setcookie( $name, $value, $expire = 0, $prefix = null, $domain = null, $forceSecure = null ) {
156 $this->cookies[$name] = $value;
157 }
158
159 /**
160 * @param $name string
161 * @return string
162 */
163 public function getcookie( $name ) {
164 if ( isset( $this->cookies[$name] ) ) {
165 return $this->cookies[$name];
166 }
167 return null;
168 }
169 }