diff options
author | Peter Zhang <peter@innocraft.com> | 2021-10-26 14:27:14 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-26 14:27:14 +0300 |
commit | 2b72bfe1648f25b76966adc93b6250ed239e0cbe (patch) | |
tree | 18996c7c03e6f1712870165998b49caf110377e3 /core | |
parent | c3b32a5c3a2999c80852ad901a95352462866bfe (diff) |
update big int on cookie expire time to datatime format (#18179)
* update int 30 years to string
update int 30 years to string
* update cookie time
update cookie time
* update one error
update one error
* update comments
update comments
* remove a not used else
remove a not used else
* Update CookieTest.php
group test into simple functions
* Update CookieTest.php
update typo
* Update Cookie.php
update warning to try catch
* add warning to exception handler
add warning to exception handler
* update cookie test
* restore test
restore test after cookie
* remove GMT
remove GMT
* revert test back
revert test back
* remove warning
remove warning
* remove time to fix bit 32
remove time to fix bit 32
* update cookie tests
update cookie, and function
* remove unneeded use statement
* removes unneeded use statements
Co-authored-by: Stefan Giehl <stefan@matomo.org>
Diffstat (limited to 'core')
-rw-r--r-- | core/Cookie.php | 34 | ||||
-rw-r--r-- | core/Tracker/IgnoreCookie.php | 14 |
2 files changed, 32 insertions, 16 deletions
diff --git a/core/Cookie.php b/core/Cookie.php index b1fb97ddc8..ed7d1351ad 100644 --- a/core/Cookie.php +++ b/core/Cookie.php @@ -8,6 +8,7 @@ */ namespace Piwik; +use DateTime; use Piwik\Container\StaticContainer; /** @@ -77,7 +78,7 @@ class Cookie * exists already. * * @param string $cookieName cookie Name - * @param int $expire The timestamp after which the cookie will expire, eg time() + 86400; + * @param int|string $expire The timestamp after which the cookie will expire, eg time() + 86400; * use 0 (int zero) to expire cookie at end of browser session * @param string $path The path on the server in which the cookie will be available on. * @param bool|string $keyStore Will be used to store several bits of data (eg. one array per website) @@ -87,12 +88,6 @@ class Cookie $this->name = $cookieName; $this->path = $path; $this->expire = $expire; - if (is_null($expire) - || !is_numeric($expire) - || $expire < 0 - ) { - $this->expire = $this->getDefaultExpire(); - } $this->keyStore = $keyStore; if ($this->isCookieFound()) { @@ -128,7 +123,7 @@ class Cookie * * @param string $Name Name of cookie * @param string $Value Value of cookie - * @param int $Expires Time the cookie expires + * @param int|string $Expires Time the cookie expires * @param string $Path * @param string $Domain * @param bool $Secure @@ -151,8 +146,10 @@ class Cookie } } + $Expires = $this->formatExpireTime($Expires); + $header = 'Set-Cookie: ' . rawurlencode($Name) . '=' . rawurlencode($Value) - . (empty($Expires) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', (int) $Expires) . ' GMT') + . (empty($Expires) ? '' : '; expires=' . $Expires) . (empty($Path) ? '' : '; path=' . $Path) . (empty($Domain) ? '' : '; domain=' . rawurlencode($Domain)) . (!$Secure ? '' : '; secure') @@ -468,4 +465,23 @@ class Cookie return $sameSite; } + + /** + * extend Cookie by timestamp or sting like + 30 years, + 10 months, default 2 years + * @param $time + * @return string + */ + public function formatExpireTime($time = null) + { + $expireTime = new DateTime(); + if (is_null($time) || (is_int($time) && $time < 0)) { + $expireTime->modify("+2 years"); + } else if (is_int($time)) { + $expireTime->setTimestamp($time); + } else if (!$expireTime->modify($time)) { + $expireTime->modify("+2 years"); + } + return $expireTime->format(DateTime::COOKIE); + + } } diff --git a/core/Tracker/IgnoreCookie.php b/core/Tracker/IgnoreCookie.php index 1520451817..7a48933ccc 100644 --- a/core/Tracker/IgnoreCookie.php +++ b/core/Tracker/IgnoreCookie.php @@ -29,12 +29,12 @@ class IgnoreCookie $cookie_path = @Config::getInstance()->Tracker['cookie_path']; $cookie = new Cookie($cookie_name, null, $cookie_path); - + $domain = @Config::getInstance()->Tracker['cookie_domain']; if (!empty($domain)) { $cookie->setDomain($domain); } - + return $cookie; } @@ -50,21 +50,21 @@ class IgnoreCookie * Get ignore (visit) cookie * * @return Cookie + * @throws \Exception */ public static function getIgnoreCookie() { $cookie_name = @Config::getInstance()->Tracker['ignore_visits_cookie_name']; $cookie_path = @Config::getInstance()->Tracker['cookie_path']; - - $thiryYears = time() + (86400 * 365 * 30); - $cookie = new Cookie($cookie_name, $thiryYears, $cookie_path, false); - + + $cookie = new Cookie($cookie_name, "+ 30 years", $cookie_path, false); + $domain = @Config::getInstance()->Tracker['cookie_domain']; if (!empty($domain)) { $cookie->setDomain($domain); } - + return $cookie; } |