Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / libs / Cookie.php
blob21d550ac3c7b2a12c7dace6706a0d10a4411d4d6
1 <?php
2 /**
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
20 * @file
21 * @ingroup HTTP
24 class Cookie {
25 /** @var string */
26 protected $name;
27 /** @var string */
28 protected $value;
29 /** @var int|false */
30 protected $expires;
31 /** @var string|null */
32 protected $path;
33 /** @var string|null */
34 protected $domain;
35 /** @var bool */
36 protected $isSessionKey = true;
37 // TO IMPLEMENT protected $secure
38 // TO IMPLEMENT? protected $maxAge (add onto expires)
39 // TO IMPLEMENT? protected $version
40 // TO IMPLEMENT? protected $comment
42 public function __construct( $name, $value, $attr ) {
43 $this->name = $name;
44 $this->set( $value, $attr );
47 /**
48 * Sets a cookie. Used before a request to set up any individual
49 * cookies. Used internally after a request to parse the
50 * Set-Cookie headers.
52 * @param string $value The value of the cookie
53 * @param array $attr Possible key/values:
54 * expires A date string
55 * path The path this cookie is used on
56 * domain Domain this cookie is used on
58 public function set( $value, $attr ) {
59 $this->value = $value;
61 if ( isset( $attr['expires'] ) ) {
62 $this->isSessionKey = false;
63 $this->expires = strtotime( $attr['expires'] );
66 $this->path = $attr['path'] ?? '/';
68 if ( isset( $attr['domain'] ) ) {
69 if ( self::validateCookieDomain( $attr['domain'] ) ) {
70 $this->domain = $attr['domain'];
72 } else {
73 throw new InvalidArgumentException( '$attr must contain a domain' );
77 /**
78 * Return the true if the cookie is valid is valid. Otherwise,
79 * false. The uses a method similar to IE cookie security
80 * described here:
81 * http://kuza55.blogspot.com/2008/02/understanding-cookie-security.html
82 * A better method might be to use a list like
83 * http://publicsuffix.org/
85 * @todo fixme fails to detect 3-letter top-level domains
86 * @todo fixme fails to detect 2-letter top-level domains for single-domain use (probably
87 * not a big problem in practice, but there are test cases)
89 * @param string $domain The domain to validate
90 * @param string|null $originDomain (optional) the domain the cookie originates from
91 * @return bool
93 public static function validateCookieDomain( $domain, $originDomain = null ) {
94 $dc = explode( ".", $domain );
96 // Don't allow a trailing dot or addresses without a or just a leading dot
97 if ( substr( $domain, -1 ) == '.' ||
98 count( $dc ) <= 1 ||
99 ( count( $dc ) == 2 && $dc[0] === '' )
101 return false;
104 // Only allow full, valid IP addresses
105 if ( preg_match( '/^[0-9.]+$/', $domain ) ) {
106 if ( count( $dc ) !== 4 || ip2long( $domain ) === false ) {
107 return false;
110 if ( $originDomain == null || $originDomain == $domain ) {
111 return true;
115 // Don't allow cookies for "co.uk" or "gov.uk", etc, but allow "supermarket.uk"
116 if ( strrpos( $domain, "." ) - strlen( $domain ) == -3 ) {
117 if ( ( count( $dc ) == 2 && strlen( $dc[0] ) <= 2 )
118 || ( count( $dc ) == 3 && strlen( $dc[0] ) == 0 && strlen( $dc[1] ) <= 2 ) ) {
119 return false;
121 if ( ( count( $dc ) == 2 || ( count( $dc ) == 3 && $dc[0] == '' ) )
122 && preg_match( '/(com|net|org|gov|edu)\...$/', $domain ) ) {
123 return false;
127 if ( $originDomain != null ) {
128 if ( substr( $domain, 0, 1 ) != '.' && $domain != $originDomain ) {
129 return false;
132 if ( substr( $domain, 0, 1 ) == '.'
133 && substr_compare(
134 $originDomain,
135 $domain,
136 -strlen( $domain ),
137 strlen( $domain ),
138 true
139 ) != 0
141 return false;
145 return true;
149 * Serialize the cookie jar into a format useful for HTTP Request headers.
151 * @param string $path The path that will be used. Required.
152 * @param string $domain The domain that will be used. Required.
153 * @return string
155 public function serializeToHttpRequest( $path, $domain ) {
156 $ret = '';
158 if ( $this->canServeDomain( $domain )
159 && $this->canServePath( $path )
160 && $this->isUnExpired() ) {
161 $ret = $this->name . '=' . $this->value;
164 return $ret;
168 * @param string $domain
169 * @return bool
171 protected function canServeDomain( $domain ) {
172 if ( $domain == $this->domain
173 || ( strlen( $domain ) > strlen( $this->domain )
174 && str_starts_with( $this->domain, '.' )
175 && substr_compare(
176 $domain,
177 $this->domain,
178 -strlen( $this->domain ),
179 strlen( $this->domain ),
180 true
181 ) == 0
184 return true;
187 return false;
191 * @param string $path
192 * @return bool
194 protected function canServePath( $path ) {
195 return ( $this->path && substr_compare( $this->path, $path, 0, strlen( $this->path ) ) == 0 );
199 * @return bool
201 protected function isUnExpired() {
202 return $this->isSessionKey || $this->expires > time();