MediaWiki
master
Cookie.php
Go to the documentation of this file.
1
<?php
24
class
Cookie
{
26
protected
$name
;
28
protected
$value
;
30
protected
$expires
;
32
protected
$path
;
34
protected
$domain
;
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
41
42
public
function
__construct
(
$name
,
$value
, $attr ) {
43
$this->name =
$name
;
44
$this->
set
(
$value
, $attr );
45
}
46
58
public
function
set
(
$value
, $attr ) {
59
$this->value =
$value
;
60
61
if
( isset( $attr[
'expires'
] ) ) {
62
$this->isSessionKey =
false
;
63
$this->expires = strtotime( $attr[
'expires'
] );
64
}
65
66
$this->path = $attr[
'path'
] ??
'/'
;
67
68
if
( isset( $attr[
'domain'
] ) ) {
69
if
( self::validateCookieDomain( $attr[
'domain'
] ) ) {
70
$this->domain = $attr[
'domain'
];
71
}
72
}
else
{
73
throw
new
InvalidArgumentException(
'$attr must contain a domain'
);
74
}
75
}
76
93
public
static
function
validateCookieDomain
(
$domain
, $originDomain =
null
) {
94
$dc = explode(
"."
,
$domain
);
95
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] ===
''
)
100
) {
101
return
false
;
102
}
103
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
;
108
}
109
110
if
( $originDomain ==
null
|| $originDomain ==
$domain
) {
111
return
true
;
112
}
113
}
114
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
;
120
}
121
if
( ( count( $dc ) == 2 || ( count( $dc ) == 3 && $dc[0] ==
''
) )
122
&& preg_match(
'/(com|net|org|gov|edu)\...$/'
,
$domain
) ) {
123
return
false
;
124
}
125
}
126
127
if
( $originDomain !=
null
) {
128
if
( substr(
$domain
, 0, 1 ) !=
'.'
&&
$domain
!= $originDomain ) {
129
return
false
;
130
}
131
132
if
( substr(
$domain
, 0, 1 ) ==
'.'
133
&& substr_compare(
134
$originDomain,
135
$domain
,
136
-strlen(
$domain
),
137
strlen(
$domain
),
138
true
139
) != 0
140
) {
141
return
false
;
142
}
143
}
144
145
return
true
;
146
}
147
155
public
function
serializeToHttpRequest
(
$path
,
$domain
) {
156
$ret =
''
;
157
158
if
( $this->
canServeDomain
(
$domain
)
159
&& $this->
canServePath
(
$path
)
160
&& $this->
isUnExpired
() ) {
161
$ret = $this->name .
'='
.
$this->value
;
162
}
163
164
return
$ret;
165
}
166
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
182
)
183
) {
184
return
true
;
185
}
186
187
return
false
;
188
}
189
194
protected
function
canServePath
(
$path
) {
195
return
( $this->path && substr_compare( $this->path,
$path
, 0, strlen( $this->path ) ) == 0 );
196
}
197
201
protected
function
isUnExpired
() {
202
return
$this->isSessionKey || $this->expires > time();
203
}
204
}
Cookie
Definition
Cookie.php:24
Cookie\serializeToHttpRequest
serializeToHttpRequest( $path, $domain)
Serialize the cookie jar into a format useful for HTTP Request headers.
Definition
Cookie.php:155
Cookie\canServeDomain
canServeDomain( $domain)
Definition
Cookie.php:171
Cookie\$domain
string null $domain
Definition
Cookie.php:34
Cookie\$path
string null $path
Definition
Cookie.php:32
Cookie\isUnExpired
isUnExpired()
Definition
Cookie.php:201
Cookie\__construct
__construct( $name, $value, $attr)
Definition
Cookie.php:42
Cookie\canServePath
canServePath( $path)
Definition
Cookie.php:194
Cookie\$expires
int false $expires
Definition
Cookie.php:30
Cookie\$name
string $name
Definition
Cookie.php:26
Cookie\$isSessionKey
bool $isSessionKey
Definition
Cookie.php:36
Cookie\validateCookieDomain
static validateCookieDomain( $domain, $originDomain=null)
Return the true if the cookie is valid is valid.
Definition
Cookie.php:93
Cookie\$value
string $value
Definition
Cookie.php:28
includes
libs
Cookie.php
Generated on Sat Dec 21 2024 11:24:25 for MediaWiki by
1.10.0