22 require_once
'HTTP/Request2.php';
69 protected static $psl = array();
80 $serializeSessionCookies =
false, $usePublicSuffixList =
true
91 protected function now()
94 $dt->setTimezone(
new DateTimeZone(
'UTC'));
95 return $dt->format(DateTime::ISO8601);
124 if ($missing = array_diff(array(
'name',
'value'), array_keys($cookie))) {
126 "Cookie array should contain 'name' and 'value' fields",
132 "Invalid cookie name: '{$cookie['name']}'",
138 "Invalid cookie value: '{$cookie['value']}'",
142 $cookie += array(
'domain' =>
'',
'path' =>
'',
'expires' => null,
'secure' =>
false);
145 if (!empty($cookie[
'expires'])
146 && !preg_match(
'/^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\+0000$/', $cookie[
'expires'])
149 $dt =
new DateTime($cookie[
'expires']);
150 $dt->setTimezone(
new DateTimeZone(
'UTC'));
151 $cookie[
'expires'] = $dt->format(DateTime::ISO8601);
152 }
catch (Exception $e) {
157 if (empty($cookie[
'domain']) || empty($cookie[
'path'])) {
160 'Cookie misses domain and/or path component, cookie setter URL needed',
164 if (empty($cookie[
'domain'])) {
165 if ($host = $setter->getHost()) {
166 $cookie[
'domain'] = $host;
169 'Setter URL does not contain host part, can\'t set cookie domain',
174 if (empty($cookie[
'path'])) {
175 $path = $setter->getPath();
176 $cookie[
'path'] = empty($path)?
'/': substr($path, 0, strrpos($path,
'/') + 1);
180 if ($setter && !$this->
domainMatch($setter->getHost(), $cookie[
'domain'])) {
182 "Domain " . $setter->getHost() .
" cannot set cookies for "
203 if (strlen($cookie[
'value'])
204 && (is_null($cookie[
'expires']) || $cookie[
'expires'] > $this->
now())
206 if (!isset($this->cookies[$cookie[
'domain']])) {
207 $this->cookies[$cookie[
'domain']] = array();
209 if (!isset($this->cookies[$cookie[
'domain']][$cookie[
'path']])) {
210 $this->cookies[$cookie[
'domain']][$cookie[
'path']] = array();
212 $this->cookies[$cookie[
'domain']][$cookie[
'path']][$cookie[
'name']] = $cookie;
214 }
elseif (isset($this->cookies[$cookie[
'domain']][$cookie[
'path']][$cookie[
'name']])) {
215 unset($this->cookies[$cookie[
'domain']][$cookie[
'path']][$cookie[
'name']]);
228 foreach ($response->
getCookies() as $cookie) {
229 $this->
store($cookie, $setter);
250 $secure = 0 == strcasecmp($url->
getScheme(),
'https');
252 $matched = $ret = array();
253 foreach (array_keys($this->cookies) as $domain) {
255 foreach (array_keys($this->cookies[$domain]) as $cPath) {
256 if (0 === strpos($path, $cPath)) {
257 foreach ($this->cookies[$domain][$cPath] as $name => $cookie) {
258 if (!$cookie[
'secure'] || $secure) {
259 $matched[$name][strlen($cookie[
'path'])] = $cookie;
268 $ret = array_merge($ret, $cookies);
274 foreach ($ret as $c) {
275 $str .= (empty($str)?
'':
'; ') . $c[
'name'] .
'=' . $c[
'value'];
289 foreach (array_keys($this->cookies) as $domain) {
290 foreach (array_keys($this->cookies[$domain]) as $path) {
291 foreach ($this->cookies[$domain][$path] as $name => $cookie) {
306 $this->serializeSession = (bool)$serialize;
345 if (!$this->serializeSession) {
346 for ($i = count(
$cookies) - 1; $i >= 0; $i--) {
347 if (empty(
$cookies[$i][
'expires'])) {
354 'serializeSession' => $this->serializeSession,
355 'useList' => $this->useList
372 foreach ($data[
'cookies'] as $cookie) {
373 if (!empty($cookie[
'expires']) && $cookie[
'expires'] <= $now) {
376 if (!isset($this->cookies[$cookie[
'domain']])) {
377 $this->cookies[$cookie[
'domain']] = array();
379 if (!isset($this->cookies[$cookie[
'domain']][$cookie[
'path']])) {
380 $this->cookies[$cookie[
'domain']][$cookie[
'path']] = array();
382 $this->cookies[$cookie[
'domain']][$cookie[
'path']][$cookie[
'name']] = $cookie;
400 if ($requestHost == $cookieDomain) {
404 if (preg_match(
'/^(?:\d{1,3}\.){3}\d{1,3}$/', $requestHost)) {
407 if (
'.' != $cookieDomain[0]) {
408 $cookieDomain =
'.' . $cookieDomain;
411 if (!$this->useList && substr_count($cookieDomain,
'.') < 2
412 || $this->useList && !self::getRegisteredDomain($cookieDomain)
416 return substr(
'.' . $requestHost, -strlen($cookieDomain)) == $cookieDomain;
433 $domainParts = explode(
'.', ltrim($domain,
'.'));
436 if (empty(self::$psl)) {
437 $path =
'@data_dir@' . DIRECTORY_SEPARATOR .
'HTTP_Request2';
438 if (0 === strpos($path,
'@' .
'data_dir@')) {
440 dirname(__FILE__) . DIRECTORY_SEPARATOR .
'..'
441 . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'data'
444 self::$psl = include_once $path . DIRECTORY_SEPARATOR .
'public-suffix-list.php';
447 if (!($result = self::checkDomainsList($domainParts, self::$psl))) {
453 if (!strpos($result,
'.')) {
455 if (2 > ($count = count($domainParts))) {
458 return $domainParts[$count - 2] .
'.' . $domainParts[$count - 1];
473 $sub = array_pop($domainParts);
476 if (!is_array($listNode) || is_null($sub)
477 || array_key_exists(
'!' . $sub, $listNode)
481 }
elseif (array_key_exists($sub, $listNode)) {
482 $result = self::checkDomainsList($domainParts, $listNode[$sub]);
484 }
elseif (array_key_exists(
'*', $listNode)) {
485 $result = self::checkDomainsList($domainParts, $listNode[
'*']);
491 return (strlen($result) > 0) ? ($result .
'.' . $sub) : null;