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