diff --git a/lib/Cake/Model/Datasource/CakeSession.php b/lib/Cake/Model/Datasource/CakeSession.php index a423a39e4..a6109dae8 100644 --- a/lib/Cake/Model/Datasource/CakeSession.php +++ b/lib/Cake/Model/Datasource/CakeSession.php @@ -127,6 +127,13 @@ class CakeSession { */ protected static $_initialized = false; +/** + * Session cookie name + * + * @var string + */ + protected static $_cookieName = null; + /** * Pseudo constructor. * @@ -222,12 +229,10 @@ class CakeSession { * @return boolean True if variable is there */ public static function check($name = null) { - if (!self::start()) { - return false; - } - if (empty($name)) { + if (empty($name) || !self::_hasSession() || !self::start()) { return false; } + return Hash::get($_SESSION, $name) !== null; } @@ -321,7 +326,7 @@ class CakeSession { * @return boolean Success */ public static function valid() { - if (self::read('Config')) { + if (self::start() && self::read('Config')) { if (self::_validAgentAndTime() && self::$error === false) { self::$valid = true; } else { @@ -369,18 +374,19 @@ class CakeSession { * Returns given session variable, or all of them, if no parameters given. * * @param string|array $name The name of the session variable (or a path as sent to Set.extract) - * @return mixed The value of the session variable + * @return mixed The value of the session variable, null if session not available, + * session not started, or provided name not found in the session. */ public static function read($name = null) { - if (!self::start()) { + if (empty($name) && $name !== null) { return false; } + if (!self::_hasSession() || !self::start()) { + return null; + } if ($name === null) { return self::_returnSessionVars(); } - if (empty($name)) { - return false; - } $result = Hash::get($_SESSION, $name); if (isset($result)) { @@ -410,12 +416,10 @@ class CakeSession { * @return boolean True if the write was successful, false if the write failed */ public static function write($name, $value = null) { - if (!self::start()) { - return false; - } - if (empty($name)) { + if (empty($name) || !self::start()) { return false; } + $write = $name; if (!is_array($name)) { $write = array($name => $value); @@ -443,6 +447,7 @@ class CakeSession { $_SESSION = null; self::$id = null; + self::$_cookieName = null; } /** @@ -482,9 +487,12 @@ class CakeSession { if (!isset($sessionConfig['ini']['session.cookie_lifetime'])) { $sessionConfig['ini']['session.cookie_lifetime'] = $sessionConfig['cookieTimeout'] * 60; } + if (!isset($sessionConfig['ini']['session.name'])) { $sessionConfig['ini']['session.name'] = $sessionConfig['cookie']; } + self::$_cookieName = $sessionConfig['ini']['session.name']; + if (!empty($sessionConfig['handler'])) { $sessionConfig['ini']['session.save_handler'] = 'user'; } @@ -522,6 +530,30 @@ class CakeSession { self::$sessionTime = self::$time + ($sessionConfig['timeout'] * 60); } +/** + * Get session cookie name. + * + * @return string + */ + protected static function _cookieName() { + if (self::$_cookieName !== null) { + return self::$_cookieName; + } + + self::init(); + self::_configureSession(); + + return self::$_cookieName = session_name(); + } + +/** + * Returns whether a session exists + * @return boolean + */ + protected static function _hasSession() { + return self::started() || isset($_COOKIE[self::_cookieName()]); + } + /** * Find the handler class and make sure it implements the correct interface. * diff --git a/lib/Cake/Test/Case/Controller/Component/SessionComponentTest.php b/lib/Cake/Test/Case/Controller/Component/SessionComponentTest.php index d0a56ba6e..214ee9d3a 100644 --- a/lib/Cake/Test/Case/Controller/Component/SessionComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/SessionComponentTest.php @@ -135,10 +135,6 @@ class SessionComponentTest extends CakeTestCase { * @return void */ public function testSessionIdConsistentAcrossRequestAction() { - $Session = new SessionComponent($this->ComponentCollection); - $Session->check('Test'); - $this->assertTrue(isset($_SESSION)); - $Object = new Object(); $Session = new SessionComponent($this->ComponentCollection); $expected = $Session->id(); @@ -274,7 +270,7 @@ class SessionComponentTest extends CakeTestCase { public function testSessionId() { unset($_SESSION); $Session = new SessionComponent($this->ComponentCollection); - $Session->check('test'); + CakeSession::start(); $this->assertEquals(session_id(), $Session->id()); } diff --git a/lib/Cake/Test/Case/I18n/I18nTest.php b/lib/Cake/Test/Case/I18n/I18nTest.php index 52182ecb0..5f310b086 100644 --- a/lib/Cake/Test/Case/I18n/I18nTest.php +++ b/lib/Cake/Test/Case/I18n/I18nTest.php @@ -17,6 +17,7 @@ */ App::uses('I18n', 'I18n'); +App::uses('CakeSession', 'Model/Datasource'); /** * I18nTest class @@ -1517,7 +1518,7 @@ class I18nTest extends CakeTestCase { * @return void */ public function testSetLanguageWithSession() { - $_SESSION['Config']['language'] = 'po'; + CakeSession::write('Config.language', 'po'); $singular = $this->_singular(); $this->assertEquals('Po (translated)', $singular); @@ -1548,7 +1549,7 @@ class I18nTest extends CakeTestCase { $this->assertTrue(in_array('23 everything else (po translated)', $plurals)); $this->assertTrue(in_array('24 everything else (po translated)', $plurals)); $this->assertTrue(in_array('25 everything else (po translated)', $plurals)); - unset($_SESSION['Config']['language']); + CakeSession::delete('Config.language'); } /** diff --git a/lib/Cake/Test/Case/Model/Datasource/CakeSessionTest.php b/lib/Cake/Test/Case/Model/Datasource/CakeSessionTest.php index d0b00c8ae..dc0e8c8cd 100644 --- a/lib/Cake/Test/Case/Model/Datasource/CakeSessionTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/CakeSessionTest.php @@ -114,7 +114,6 @@ class CakeSessionTest extends CakeTestCase { 'cookieTimeout' => 120, 'ini' => array(), )); - TestCakeSession::init(); } /**