Skip to content

Commit

Permalink
Update session configuration to not depend on Configure.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Sep 18, 2012
1 parent b4d655a commit 701cf08
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
23 changes: 12 additions & 11 deletions App/Config/session.php
@@ -1,7 +1,7 @@
<?php
namespace App\Config;

use Cake\Core\Configure;
use Cake\Model\Datasource\Session;

/**
* Session configuration.
Expand All @@ -12,19 +12,19 @@
*
* ## Options
*
* - `Session.cookie` - 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? You might want to set the
* - `cookie` - The name of the cookie to use. Defaults to 'CAKEPHP'
* - `timeout` - The number of minutes you want sessions to live for. This timeout is handled by CakePHP
* - `cookieTimeout` - The number of minutes you want session cookies to live for.
* - `checkAgent` - Do you want the user agent to be checked when starting sessions? You might want to set the
* value to false, when dealing with older versions of IE, Chrome Frame or certain web-browsing devices and AJAX
* - `Session.defaults` - The default configuration set to use as a basis for your 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,
* - `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.autoRegenerate` - Enabling this setting, turns on automatic renewal of sessions, and
* - `autoRegenerate` - Enabling this setting, turns on automatic renewal of sessions, and
* sessionids that change frequently. See Cake\Model\Datasource\Session::$requestCountdown.
* - `Session.ini` - An associative array of additional ini values to set.
* - `ini` - An associative array of additional ini values to set.
*
* The built in defaults are:
*
Expand All @@ -34,12 +34,13 @@
* - 'cache' - Use the Cache class to save sessions.
*
* To define a custom session handler, save it at /app/Model/Datasource/Session/<name>.php.
* Make sure the class implements `Cake\Model\Datasource\Session\SessionHandlerInterface` and set Session.handler to <name>
* Make sure the class implements `Cake\Model\Datasource\Session\SessionHandlerInterface`
* and set Session.handler to <name>
*
* To use database sessions, run the app/Config/Schema/sessions.php schema using
* the cake shell command: cake schema create Sessions
*
*/
Configure::write('Session', array(
Session::config(array(
'defaults' => 'php'
));
46 changes: 30 additions & 16 deletions lib/Cake/Model/Datasource/Session.php
@@ -1,14 +1,5 @@
<?php
/**
* Session class for Cake.
*
* Cake abstracts the handling of sessions.
* There are several convenient methods to access session information.
* This class is the implementation of those methods.
* They are mostly used by the Session Component.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -21,8 +12,8 @@
* @since CakePHP(tm) v .0.10.0.1222
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

namespace Cake\Model\Datasource;

use Cake\Core\Configure;
use Cake\Utility\Hash;

Expand Down Expand Up @@ -57,6 +48,13 @@ class Session {
*/
protected static $_userAgent = '';

/**
* Configuration data set at bootstrapping.
*
* @var array
*/
protected static $_config = array();

/**
* Path to where the session is active.
*
Expand Down Expand Up @@ -122,6 +120,18 @@ class Session {
*/
public static $requestCountdown = 10;

/**
* Used to set configuration data into Session from the application
* bootstrap. The session is not started or configuration completed until
* Session::start() is called.
*
* @param array $config
* @return void
*/
public static function config(array $config) {
static::$_config = $config;
}

/**
* Pseudo constructor.
*
Expand All @@ -131,7 +141,7 @@ class Session {
public static function init($base = null) {
static::$time = time();

$checkAgent = Configure::read('Session.checkAgent');
$checkAgent = array_key_exists('checkAgent', static::$_config) ? static::$_config['checkAgent'] : null;
if (($checkAgent === true || $checkAgent === null) && env('HTTP_USER_AGENT') != null) {
static::$_userAgent = md5(env('HTTP_USER_AGENT') . Configure::read('Security.salt'));
}
Expand Down Expand Up @@ -327,7 +337,7 @@ public static function valid() {
protected static function _validAgentAndTime() {
$config = static::read('Config');
$validAgent = (
Configure::read('Session.checkAgent') === false ||
static::$_config['checkAgent'] === false ||
static::$_userAgent == $config['userAgent']
);
return ($validAgent && static::$time <= $config['time']);
Expand Down Expand Up @@ -447,7 +457,7 @@ public static function clear() {
* @throws Cake\Error\SessionException Throws exceptions when ini_set() fails.
*/
protected static function _configureSession() {
$sessionConfig = Configure::read('Session');
$sessionConfig = static::$_config;
$iniSet = function_exists('ini_set');

if (isset($sessionConfig['defaults'])) {
Expand Down Expand Up @@ -504,7 +514,7 @@ protected static function _configureSession() {
array($handler, 'gc')
);
}
Configure::write('Session', $sessionConfig);
static::$_config = $sessionConfig;
static::$sessionTime = static::$time + ($sessionConfig['timeout'] * 60);
}

Expand Down Expand Up @@ -535,6 +545,7 @@ protected static function _getHandler($class) {
protected static function _defaultConfig($name) {
$defaults = array(
'php' => array(
'checkAgent' => false,
'cookie' => 'CAKEPHP',
'timeout' => 240,
'ini' => array(
Expand All @@ -543,6 +554,7 @@ protected static function _defaultConfig($name) {
)
),
'cake' => array(
'checkAgent' => false,
'cookie' => 'CAKEPHP',
'timeout' => 240,
'ini' => array(
Expand All @@ -557,6 +569,7 @@ protected static function _defaultConfig($name) {
)
),
'cache' => array(
'checkAgent' => false,
'cookie' => 'CAKEPHP',
'timeout' => 240,
'ini' => array(
Expand All @@ -573,6 +586,7 @@ protected static function _defaultConfig($name) {
)
),
'database' => array(
'checkAgent' => false,
'cookie' => 'CAKEPHP',
'timeout' => 240,
'ini' => array(
Expand Down Expand Up @@ -625,7 +639,7 @@ protected static function _checkValid() {
return false;
}
if ($config = static::read('Config')) {
$sessionConfig = Configure::read('Session');
$sessionConfig = static::$_config;

if (static::_validAgentAndTime()) {
static::write('Config.time', static::$sessionTime);
Expand Down Expand Up @@ -661,7 +675,7 @@ protected static function _checkValid() {
public static function renew() {
if (session_id()) {
if (session_id() != '' || isset($_COOKIE[session_name()])) {
setcookie(Configure::read('Session.cookie'), '', time() - 42000, static::$path);
setcookie(self::$_config['cookie'], '', time() - 42000, static::$path);
}
session_regenerate_id(true);
}
Expand Down

0 comments on commit 701cf08

Please sign in to comment.