API: Fix documentation for ApiBase::require*OneParameter
[lhc/web/wiklou.git] / includes / Cookie.php
1 <?php
2 /**
3 * Cookie for HTTP requests.
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 * @ingroup HTTP
22 */
23
24 class Cookie {
25 protected $name;
26 protected $value;
27 protected $expires;
28 protected $path;
29 protected $domain;
30 protected $isSessionKey = true;
31 // TO IMPLEMENT protected $secure
32 // TO IMPLEMENT? protected $maxAge (add onto expires)
33 // TO IMPLEMENT? protected $version
34 // TO IMPLEMENT? protected $comment
35
36 function __construct( $name, $value, $attr ) {
37 $this->name = $name;
38 $this->set( $value, $attr );
39 }
40
41 /**
42 * Sets a cookie. Used before a request to set up any individual
43 * cookies. Used internally after a request to parse the
44 * Set-Cookie headers.
45 *
46 * @param string $value The value of the cookie
47 * @param array $attr Possible key/values:
48 * expires A date string
49 * path The path this cookie is used on
50 * domain Domain this cookie is used on
51 * @throws MWException
52 */
53 public function set( $value, $attr ) {
54 $this->value = $value;
55
56 if ( isset( $attr['expires'] ) ) {
57 $this->isSessionKey = false;
58 $this->expires = strtotime( $attr['expires'] );
59 }
60
61 if ( isset( $attr['path'] ) ) {
62 $this->path = $attr['path'];
63 } else {
64 $this->path = '/';
65 }
66
67 if ( isset( $attr['domain'] ) ) {
68 if ( self::validateCookieDomain( $attr['domain'] ) ) {
69 $this->domain = $attr['domain'];
70 }
71 } else {
72 throw new MWException( 'You must specify a domain.' );
73 }
74 }
75
76 /**
77 * Return the true if the cookie is valid is valid. Otherwise,
78 * false. The uses a method similar to IE cookie security
79 * described here:
80 * http://kuza55.blogspot.com/2008/02/understanding-cookie-security.html
81 * A better method might be to use a blacklist like
82 * http://publicsuffix.org/
83 *
84 * @todo fixme fails to detect 3-letter top-level domains
85 * @todo fixme fails to detect 2-letter top-level domains for single-domain use (probably
86 * not a big problem in practice, but there are test cases)
87 *
88 * @param string $domain The domain to validate
89 * @param string $originDomain (optional) the domain the cookie originates from
90 * @return bool
91 */
92 public static function validateCookieDomain( $domain, $originDomain = null ) {
93 // Don't allow a trailing dot
94 if ( substr( $domain, -1 ) == '.' ) {
95 return false;
96 }
97
98 $dc = explode( ".", $domain );
99
100 // Only allow full, valid IP addresses
101 if ( preg_match( '/^[0-9.]+$/', $domain ) ) {
102 if ( count( $dc ) != 4 ) {
103 return false;
104 }
105
106 if ( ip2long( $domain ) === false ) {
107 return false;
108 }
109
110 if ( $originDomain == null || $originDomain == $domain ) {
111 return true;
112 }
113
114 }
115
116 // Don't allow cookies for "co.uk" or "gov.uk", etc, but allow "supermarket.uk"
117 if ( strrpos( $domain, "." ) - strlen( $domain ) == -3 ) {
118 if ( ( count( $dc ) == 2 && strlen( $dc[0] ) <= 2 )
119 || ( count( $dc ) == 3 && strlen( $dc[0] ) == "" && strlen( $dc[1] ) <= 2 ) ) {
120 return false;
121 }
122 if ( ( count( $dc ) == 2 || ( count( $dc ) == 3 && $dc[0] == '' ) )
123 && preg_match( '/(com|net|org|gov|edu)\...$/', $domain ) ) {
124 return false;
125 }
126 }
127
128 if ( $originDomain != null ) {
129 if ( substr( $domain, 0, 1 ) != '.' && $domain != $originDomain ) {
130 return false;
131 }
132
133 if ( substr( $domain, 0, 1 ) == '.'
134 && substr_compare(
135 $originDomain,
136 $domain,
137 -strlen( $domain ),
138 strlen( $domain ),
139 true
140 ) != 0
141 ) {
142 return false;
143 }
144 }
145
146 return true;
147 }
148
149 /**
150 * Serialize the cookie jar into a format useful for HTTP Request headers.
151 *
152 * @param string $path The path that will be used. Required.
153 * @param string $domain The domain that will be used. Required.
154 * @return string
155 */
156 public function serializeToHttpRequest( $path, $domain ) {
157 $ret = '';
158
159 if ( $this->canServeDomain( $domain )
160 && $this->canServePath( $path )
161 && $this->isUnExpired() ) {
162 $ret = $this->name . '=' . $this->value;
163 }
164
165 return $ret;
166 }
167
168 /**
169 * @param string $domain
170 * @return bool
171 */
172 protected function canServeDomain( $domain ) {
173 if ( $domain == $this->domain
174 || ( strlen( $domain ) > strlen( $this->domain )
175 && substr( $this->domain, 0, 1 ) == '.'
176 && substr_compare(
177 $domain,
178 $this->domain,
179 -strlen( $this->domain ),
180 strlen( $this->domain ),
181 true
182 ) == 0
183 )
184 ) {
185 return true;
186 }
187
188 return false;
189 }
190
191 /**
192 * @param string $path
193 * @return bool
194 */
195 protected function canServePath( $path ) {
196 return ( $this->path && substr_compare( $this->path, $path, 0, strlen( $this->path ) ) == 0 );
197 }
198
199 /**
200 * @return bool
201 */
202 protected function isUnExpired() {
203 return $this->isSessionKey || $this->expires > time();
204 }
205 }
206
207 class CookieJar {
208 private $cookie = array();
209
210 /**
211 * Set a cookie in the cookie jar. Make sure only one cookie per-name exists.
212 * @see Cookie::set()
213 * @param string $name
214 * @param string $value
215 * @param array $attr
216 */
217 public function setCookie( $name, $value, $attr ) {
218 /* cookies: case insensitive, so this should work.
219 * We'll still send the cookies back in the same case we got them, though.
220 */
221 $index = strtoupper( $name );
222
223 if ( isset( $this->cookie[$index] ) ) {
224 $this->cookie[$index]->set( $value, $attr );
225 } else {
226 $this->cookie[$index] = new Cookie( $name, $value, $attr );
227 }
228 }
229
230 /**
231 * @see Cookie::serializeToHttpRequest
232 * @param string $path
233 * @param string $domain
234 * @return string
235 */
236 public function serializeToHttpRequest( $path, $domain ) {
237 $cookies = array();
238
239 foreach ( $this->cookie as $c ) {
240 $serialized = $c->serializeToHttpRequest( $path, $domain );
241
242 if ( $serialized ) {
243 $cookies[] = $serialized;
244 }
245 }
246
247 return implode( '; ', $cookies );
248 }
249
250 /**
251 * Parse the content of an Set-Cookie HTTP Response header.
252 *
253 * @param string $cookie
254 * @param string $domain Cookie's domain
255 * @return null
256 */
257 public function parseCookieResponseHeader( $cookie, $domain ) {
258 $len = strlen( 'Set-Cookie:' );
259
260 if ( substr_compare( 'Set-Cookie:', $cookie, 0, $len, true ) === 0 ) {
261 $cookie = substr( $cookie, $len );
262 }
263
264 $bit = array_map( 'trim', explode( ';', $cookie ) );
265
266 if ( count( $bit ) >= 1 ) {
267 list( $name, $value ) = explode( '=', array_shift( $bit ), 2 );
268 $attr = array();
269
270 foreach ( $bit as $piece ) {
271 $parts = explode( '=', $piece );
272 if ( count( $parts ) > 1 ) {
273 $attr[strtolower( $parts[0] )] = $parts[1];
274 } else {
275 $attr[strtolower( $parts[0] )] = true;
276 }
277 }
278
279 if ( !isset( $attr['domain'] ) ) {
280 $attr['domain'] = $domain;
281 } elseif ( !Cookie::validateCookieDomain( $attr['domain'], $domain ) ) {
282 return null;
283 }
284
285 $this->setCookie( $name, $value, $attr );
286 }
287 }
288 }