mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Starting to implement new session configuration setup. Test case updated.
This commit is contained in:
parent
f05a13a451
commit
b247559e9e
2 changed files with 175 additions and 16 deletions
|
@ -495,18 +495,63 @@ class CakeSession {
|
|||
/**
|
||||
* 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 ($iniSet && (self::$security === 'high' || self::$security === 'medium')) {
|
||||
ini_set('session.referer_check', self::$host);
|
||||
}
|
||||
self::$cookieLifeTime = Configure::read('Session.timeout') * Security::inactiveMins();
|
||||
|
||||
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 (!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';
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -588,6 +633,86 @@ class CakeSession {
|
|||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -39,6 +39,8 @@ class TestCakeSession extends CakeSession {
|
|||
*/
|
||||
class CakeSessionTest extends CakeTestCase {
|
||||
|
||||
protected static $_gcDivisor;
|
||||
|
||||
/**
|
||||
* Fixtures used in the SessionTest
|
||||
*
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -77,6 +79,15 @@ class CakeSessionTest extends CakeTestCase {
|
|||
* @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();
|
||||
|
@ -88,10 +99,33 @@ class CakeSessionTest extends CakeTestCase {
|
|||
* @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
|
||||
|
|
Loading…
Add table
Reference in a new issue