From 3aa189eb3a49abfb31cd1b25b32505d249e95044 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 3 Jun 2013 20:16:18 -0400 Subject: [PATCH] Fix cookie expiry time calculation on 32bit systems. strtotime() misbehaves on 32bit systems when the resulting timestamp would overflow an integer. Use a DateTime to workaround this issue. Fixes #3868 --- .../Controller/Component/CookieComponent.php | 8 +++--- .../Component/CookieComponentTest.php | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Controller/Component/CookieComponent.php b/lib/Cake/Controller/Component/CookieComponent.php index a7c856a40..1e0660dd5 100644 --- a/lib/Cake/Controller/Component/CookieComponent.php +++ b/lib/Cake/Controller/Component/CookieComponent.php @@ -387,20 +387,20 @@ class CookieComponent extends Component { * @return integer Unix timestamp */ protected function _expire($expires = null) { - $now = time(); if (is_null($expires)) { return $this->_expires; } $this->_reset = $this->_expires; - if (!$expires) { return $this->_expires = 0; } + $now = new DateTime(); if (is_int($expires) || is_numeric($expires)) { - return $this->_expires = $now + intval($expires); + return $this->_expires = $now->format('U') + intval($expires); } - return $this->_expires = strtotime($expires, $now); + $now->modify($expires); + return $this->_expires = $now->format('U'); } /** diff --git a/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php b/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php index 5b1beeb4c..aeb0e2503 100644 --- a/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php @@ -201,6 +201,31 @@ class CookieComponentTest extends CakeTestCase { $this->assertEquals('value', $result); } +/** + * test write with distant future cookies + * + * @return void + */ + public function testWriteFarFuture() { + $this->Cookie->write('Testing', 'value', false, '+90 years'); + $future = new DateTime('now'); + $future->modify('+90 years'); + + $expected = array( + 'name' => $this->Cookie->name . '[Testing]', + 'value' => 'value', + 'path' => '/', + 'domain' => '', + 'secure' => false, + 'httpOnly' => false); + $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]'); + + $this->assertEquals($future->format('U'), $result['expire'], '', 3); + unset($result['expire']); + + $this->assertEquals($expected, $result); + } + /** * test write with httpOnly cookies *