3 * Cookie for HTTP requests.
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.
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.
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
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
36 function __construct( $name, $value, $attr ) {
38 $this->set( $value, $attr );
42 * Sets a cookie. Used before a request to set up any individual
43 * cookies. Used internally after a request to parse the
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
52 public function set( $value, $attr ) {
53 $this->value
= $value;
55 if ( isset( $attr['expires'] ) ) {
56 $this->isSessionKey
= false;
57 $this->expires
= strtotime( $attr['expires'] );
60 if ( isset( $attr['path'] ) ) {
61 $this->path
= $attr['path'];
66 if ( isset( $attr['domain'] ) ) {
67 if ( self
::validateCookieDomain( $attr['domain'] ) ) {
68 $this->domain
= $attr['domain'];
71 throw new MWException( 'You must specify a domain.' );
76 * Return the true if the cookie is valid is valid. Otherwise,
77 * false. The uses a method similar to IE cookie security
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/
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)
86 * @param $domain String: the domain to validate
87 * @param $originDomain String: (optional) the domain the cookie originates from
90 public static function validateCookieDomain( $domain, $originDomain = null ) {
91 // Don't allow a trailing dot
92 if ( substr( $domain, -1 ) == '.' ) {
96 $dc = explode( ".", $domain );
98 // Only allow full, valid IP addresses
99 if ( preg_match( '/^[0-9.]+$/', $domain ) ) {
100 if ( count( $dc ) != 4 ) {
104 if ( ip2long( $domain ) === false ) {
108 if ( $originDomain == null ||
$originDomain == $domain ) {
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 ) ) {
120 if ( ( count( $dc ) == 2 ||
( count( $dc ) == 3 && $dc[0] == '' ) )
121 && preg_match( '/(com|net|org|gov|edu)\...$/', $domain ) ) {
126 if ( $originDomain != null ) {
127 if ( substr( $domain, 0, 1 ) != '.' && $domain != $originDomain ) {
131 if ( substr( $domain, 0, 1 ) == '.'
132 && substr_compare( $originDomain, $domain, -strlen( $domain ),
133 strlen( $domain ), true ) != 0 ) {
142 * Serialize the cookie jar into a format useful for HTTP Request headers.
144 * @param $path String: the path that will be used. Required.
145 * @param $domain String: the domain that will be used. Required.
148 public function serializeToHttpRequest( $path, $domain ) {
151 if ( $this->canServeDomain( $domain )
152 && $this->canServePath( $path )
153 && $this->isUnExpired() ) {
154 $ret = $this->name
. '=' . $this->value
;
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 ) ) {
180 protected function canServePath( $path ) {
181 return ( $this->path
&& substr_compare( $this->path
, $path, 0, strlen( $this->path
) ) == 0 );
187 protected function isUnExpired() {
188 return $this->isSessionKey ||
$this->expires
> time();
193 private $cookie = array();
196 * Set a cookie in the cookie jar. Make sure only one cookie per-name exists.
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.
203 $index = strtoupper( $name );
205 if ( isset( $this->cookie
[$index] ) ) {
206 $this->cookie
[$index]->set( $value, $attr );
208 $this->cookie
[$index] = new Cookie( $name, $value, $attr );
213 * @see Cookie::serializeToHttpRequest
216 public function serializeToHttpRequest( $path, $domain ) {
219 foreach ( $this->cookie
as $c ) {
220 $serialized = $c->serializeToHttpRequest( $path, $domain );
223 $cookies[] = $serialized;
227 return implode( '; ', $cookies );
231 * Parse the content of an Set-Cookie HTTP Response header.
233 * @param $cookie String
234 * @param $domain String: cookie's domain
237 public function parseCookieResponseHeader ( $cookie, $domain ) {
238 $len = strlen( 'Set-Cookie:' );
240 if ( substr_compare( 'Set-Cookie:', $cookie, 0, $len, true ) === 0 ) {
241 $cookie = substr( $cookie, $len );
244 $bit = array_map( 'trim', explode( ';', $cookie ) );
246 if ( count( $bit ) >= 1 ) {
247 list( $name, $value ) = explode( '=', array_shift( $bit ), 2 );
250 foreach ( $bit as $piece ) {
251 $parts = explode( '=', $piece );
252 if ( count( $parts ) > 1 ) {
253 $attr[strtolower( $parts[0] )] = $parts[1];
255 $attr[strtolower( $parts[0] )] = true;
259 if ( !isset( $attr['domain'] ) ) {
260 $attr['domain'] = $domain;
261 } elseif ( !Cookie
::validateCookieDomain( $attr['domain'], $domain ) ) {
265 $this->setCookie( $name, $value, $attr );