Skip to content

Commit

Permalink
Starting to implement new session configuration setup. Test case upda…
Browse files Browse the repository at this point in the history
…ted.
  • Loading branch information
markstory committed Jul 28, 2010
1 parent f05a13a commit b247559
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 14 deletions.
135 changes: 130 additions & 5 deletions cake/libs/cake_session.php
Expand Up @@ -495,18 +495,63 @@ public static function destroy() {
/**
* Helper method to initialize a session, based on Cake core settings.
*
* Sessions can be configured with a few shortcut names as well as have any number of ini settings declared.
*
* ## Options
*
* - `Session.name` - The name of the cookie to use. Defaults to 'CAKEPHP'
* - `Session.timeout` - The number of minutes you want sessions to live for. This timeout is handled by CakePHP
* - `Session.cookieTimeout` - The number of minutes you want session cookies to live for.
* - `Session.checkAgent` - Do you want the user agent to be checked when starting sessions?
* - `Session.defaults` - The default configuration set to use as a basis for your session.
* There are four builtins: php, cake, cache, database.
* - `Session.handler` - Can be used to enable a custom session handler. Expects an array of of callables,
* that can be used with `session_save_handler`. Using this option will automatically add `session.save_handler`
* to the ini array.
* - `Session.ini` - An associative array of additional ini values to set.
*
* @access private
*/
function __initSession() {
$sessionConfig = Configure::read('Session');
$iniSet = function_exists('ini_set');
if ($iniSet && env('HTTPS')) {
ini_set('session.cookie_secure', 1);

if (isset($sessionConfig['defaults'])) {
$defaults = self::_defaultConfig($sessionConfig['defaults']);
if ($defaults) {
$sessionConfig = Set::merge($defaults, $sessionConfig);
}
}
if (!isset($sessionConfig['ini']['session.cookie_secure']) && env('HTTPS')) {
$sessionConfig['ini']['session.cookie_secure'] = 1;
}
if (isset($sessionConfig['timeout']) && !isset($sessionConfig['cookieTimeout'])) {
$sessionConfig['cookieTimeout'] = $sessionConfig['timeout'];
}
if ($iniSet && (self::$security === 'high' || self::$security === 'medium')) {
ini_set('session.referer_check', self::$host);
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'];
}
if (!empty($sessionConfig['handler'])) {
$sessionConfig['ini']['sesssion.save_handler'] = 'user';
}
self::$cookieLifeTime = Configure::read('Session.timeout') * Security::inactiveMins();

if (empty($_SESSION)) {
if (!empty($sessionConfig['ini']) && is_array($sessionConfig['ini'])) {
foreach ($sessionConfig['ini'] as $setting => $value) {
if (ini_set($setting, $value) === false) {
throw new Exception(__('Unable to configure the session.'));
}
}
}
}
if (!empty($sessionConfig['handler']) && !isset($sessionConfig['handler']['engine'])) {
call_user_func_array('session_set_save_handler', $sessionConfig['handler']);
}

/*
switch (Configure::read('Session.save')) {
case 'cake':
if (empty($_SESSION) && $iniSet) {
Expand Down Expand Up @@ -588,6 +633,86 @@ function __initSession() {
}
break;
}
*/
}

/**
* Get one of the prebaked default session configurations.
*
* @return void
*/
protected static function _defaultConfig($name) {
$defaults = array(
'php' => array(
'cookie' => 'CAKEPHP',
'timeout' => 240,
'cookieTimeout' => 240,
'ini' => array(
'session.use_trans_sid' => 0,
'session.cookie_path' => self::$path
)
),
'cake' => array(
'cookie' => 'CAKEPHP',
'timeout' => 240,
'cookieTimeout' => 240,
'ini' => array(
'session.use_trans_sid' => 0,
'url_rewriter.tags' => '',
'session.serialize_handler' => 'php',
'session.use_cookies' => 1,
'session.cookie_path' => self::$path,
'session.auto_start' => 0,
'session.save_path' => TMP . 'sessions'
)
),
'cache' => array(
'cookie' => 'CAKEPHP',
'timeout' => 240,
'cookieTimeout' => 240,
'ini' => array(
'session.use_trans_sid' => 0,
'url_rewriter.tags' => '',
'session.use_cookies' => 1,
'session.cookie_path' => self::$path,
'session.save_handler' => 'user',
),
'handler' => array(
array('CakeSession','__open'),
array('CakeSession', '__close'),
array('Cache', 'read'),
array('Cache', 'write'),
array('Cache', 'delete'),
array('Cache', 'gc')
)
),
'database' => array(
'cookie' => 'CAKEPHP',
'timeout' => 240,
'cookieTimeout' => 240,
'ini' => array(
'session.use_trans_sid' => 0,
'url_rewriter.tags' => '',
'session.auto_start' => 0,
'session.use_cookies' => 1,
'session.cookie_path' => self::$path,
'session.save_handler' => 'user',
'session.serialize_handler' => 'php',
),
'handler' => array(
array('CakeSession','__open'),
array('CakeSession', '__close'),
array('CakeSession', '__read'),
array('CakeSession', '__write'),
array('CakeSession', '__destroy'),
array('CakeSession', '__gc')
)
)
);
if (isset($defaults[$name])) {
return $defaults[$name];
}
return false;
}

/**
Expand Down
52 changes: 43 additions & 9 deletions cake/tests/cases/libs/cake_session.test.php
Expand Up @@ -39,6 +39,8 @@ public static function setHost($host) {
*/
class CakeSessionTest extends CakeTestCase {

protected static $_gcDivisor;

/**
* Fixtures used in the SessionTest
*
Expand All @@ -48,26 +50,26 @@ class CakeSessionTest extends CakeTestCase {
public $fixtures = array('core.session');

/**
* startCase method
* setup before class.
*
* @access public
* @return void
*/
function startCase() {
public static function setupBeforeClass() {
// Make sure garbage colector will be called
$this->__gc_divisor = ini_get('session.gc_divisor');
self::$_gcDivisor = ini_get('session.gc_divisor');
ini_set('session.gc_divisor', '1');
}

/**
* endCase method
* teardown after class
*
* @access public
* @return void
*/
function endCase() {
public static function teardownAfterClass() {
// Revert to the default setting
ini_set('session.gc_divisor', $this->__gc_divisor);
ini_set('session.gc_divisor', self::$_gcDivisor);
}

/**
Expand All @@ -77,6 +79,15 @@ function endCase() {
* @return void
*/
function startTest() {
Configure::write('Session', array(
'defaults' => 'php',
'cookie' => 'cakephp',
'timeout' => 120,
'cookieTimeout' => 120,
'ini' => array(),
'handler' => null
));

TestCakeSession::init();
TestCakeSession::destroy();
TestCakeSession::$watchKeys = array();
Expand All @@ -88,10 +99,33 @@ function startTest() {
* @access public
* @return void
*/
function endTest() {
unset($_SESSION);
function endTest() {
unset($_SESSION);
@session_destroy();
}
}

/**
* test setting ini properties with Session configuration.
*
* @return void
*/
function testSessionConfigIniSetting() {
$_SESSION = array();
session_destroy();

Configure::write('Session', array(
'cookie' => 'test_suite',
'timeout' => 86400,
'ini' => array(
'session.referer_check' => 'example.com',
'session.use_trans_sid' => false
)
));
TestCakeSession::start();
$this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect');
$this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect');
$this->assertEquals('test_suite', ini_get('session.name'), 'Ini value is incorrect');
}

/**
* testSessionPath
Expand Down

0 comments on commit b247559

Please sign in to comment.