Bug 35034 - moved autocomment-prefix between the prefix and the arrow. Follow up...
[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 * @param $prefix String: Prefix to use, if not $wgCookiePrefix (use '' for no prefix)
47 * @param @domain String: Cookie domain to use, if not $wgCookieDomain
48 */
49 public function setcookie( $name, $value, $expire = 0, $prefix = null, $domain = null ) {
50 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
51 global $wgCookieSecure,$wgCookieExpiration, $wgCookieHttpOnly;
52 if ( $expire == 0 ) {
53 $expire = time() + $wgCookieExpiration;
54 }
55 if( $prefix === null ) {
56 $prefix = $wgCookiePrefix;
57 }
58 if( $domain === null ) {
59 $domain = $wgCookieDomain;
60 }
61 $httpOnlySafe = wfHttpOnlySafe() && $wgCookieHttpOnly;
62 wfDebugLog( 'cookie',
63 'setcookie: "' . implode( '", "',
64 array(
65 $prefix . $name,
66 $value,
67 $expire,
68 $wgCookiePath,
69 $domain,
70 $wgCookieSecure,
71 $httpOnlySafe ) ) . '"' );
72 setcookie( $prefix . $name,
73 $value,
74 $expire,
75 $wgCookiePath,
76 $domain,
77 $wgCookieSecure,
78 $httpOnlySafe );
79 }
80 }
81
82 /**
83 * @ingroup HTTP
84 */
85 class FauxResponse extends WebResponse {
86 private $headers;
87 private $cookies;
88 private $code;
89
90 /**
91 * Stores a HTTP header
92 * @param $string String: header to output
93 * @param $replace Bool: replace current similar header
94 * @param $http_response_code null|int Forces the HTTP response code to the specified value.
95 */
96 public function header( $string, $replace = true, $http_response_code = null ) {
97 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
98 $parts = explode( ' ', $string, 3 );
99 $this->code = intval( $parts[1] );
100 } else {
101 list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
102
103 if( $replace || !isset( $this->headers[$key] ) ) {
104 $this->headers[$key] = $val;
105 }
106 }
107
108 if ( $http_response_code !== null ) {
109 $this->code = intval( $http_response_code );
110 }
111 }
112
113 /**
114 * @param $key string
115 * @return string
116 */
117 public function getheader( $key ) {
118 if ( isset( $this->headers[$key] ) ) {
119 return $this->headers[$key];
120 }
121 return null;
122 }
123
124 /**
125 * Get the HTTP response code, null if not set
126 *
127 * @return Int or null
128 */
129 public function getStatusCode() {
130 return $this->code;
131 }
132
133 /**
134 * @todo document. It just ignore optional parameters.
135 *
136 * @param $name String: name of cookie
137 * @param $value String: value to give cookie
138 * @param $expire Int: number of seconds til cookie expires (Default: 0)
139 * @param $prefix TODO DOCUMENT (Default: null)
140 * @param $domain TODO DOCUMENT (Default: null)
141 *
142 */
143 public function setcookie( $name, $value, $expire = 0, $prefix = null, $domain = null ) {
144 $this->cookies[$name] = $value;
145 }
146
147 /**
148 * @param $name string
149 * @return string
150 */
151 public function getcookie( $name ) {
152 if ( isset( $this->cookies[$name] ) ) {
153 return $this->cookies[$name];
154 }
155 return null;
156 }
157 }