* Don't create a WebRequest obhject in CLI mode but a FauxRequest; avoids some useles...
[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: number of seconds til cookie expires
46 */
47 public function setcookie( $name, $value, $expire = 0 ) {
48 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
49 global $wgCookieSecure,$wgCookieExpiration, $wgCookieHttpOnly;
50 if ( $expire == 0 ) {
51 $expire = time() + $wgCookieExpiration;
52 }
53 $httpOnlySafe = wfHttpOnlySafe() && $wgCookieHttpOnly;
54 wfDebugLog( 'cookie',
55 'setcookie: "' . implode( '", "',
56 array(
57 $wgCookiePrefix . $name,
58 $value,
59 $expire,
60 $wgCookiePath,
61 $wgCookieDomain,
62 $wgCookieSecure,
63 $httpOnlySafe ) ) . '"' );
64 setcookie( $wgCookiePrefix . $name,
65 $value,
66 $expire,
67 $wgCookiePath,
68 $wgCookieDomain,
69 $wgCookieSecure,
70 $httpOnlySafe );
71 }
72 }
73
74 /**
75 * @ingroup HTTP
76 */
77 class FauxResponse extends WebResponse {
78 private $headers;
79 private $cookies;
80 private $code;
81
82 /**
83 * Stores a HTTP header
84 * @param $string String: header to output
85 * @param $replace Bool: replace current similar header
86 * @param $http_response_code null|int Forces the HTTP response code to the specified value.
87 */
88 public function header( $string, $replace = true, $http_response_code = null ) {
89 $match = array();
90 if ( preg_match( '~^HTTP/1.\d (\d+)\D*$~', $string, $match ) ) {
91 $this->code = intval( $match[1] );
92 } else {
93 list( $key, $val ) = explode( ":", $string, 2 );
94
95 if( $replace || !isset( $this->headers[$key] ) ) {
96 $this->headers[$key] = $val;
97 }
98 }
99
100 if ( $http_response_code !== null ) {
101 $this->code = intval( $http_response_code );
102 }
103 }
104
105 /**
106 * @param $key string
107 * @return string
108 */
109 public function getheader( $key ) {
110 return $this->headers[$key];
111 }
112
113 /**
114 * Get the HTTP response code, null if not set
115 *
116 * @return Int or null
117 */
118 public function getStatusCode() {
119 return $this->code;
120 }
121
122 /**
123 * @param $name String: name of cookie
124 * @param $value String: value to give cookie
125 * @param $expire Int: number of seconds til cookie expires
126 */
127 public function setcookie( $name, $value, $expire = 0 ) {
128 $this->cookies[$name] = $value;
129 }
130
131 /**
132 * @param $name string
133 * @return string
134 */
135 public function getcookie( $name ) {
136 if ( isset( $this->cookies[$name] ) ) {
137 return $this->cookies[$name];
138 }
139 return null;
140 }
141 }