Chaned CakeSessn o use static approache.

This commit is contained in:
predominant 2010-06-23 11:25:04 +10:00 committed by mark_story
parent 0a72de6438
commit 17688a6002

View file

@ -39,56 +39,56 @@ class CakeSession extends Object {
* *
* @var boolean * @var boolean
*/ */
public $valid = false; public static $valid = false;
/** /**
* Error messages for this session * Error messages for this session
* *
* @var array * @var array
*/ */
public $error = false; public static $error = false;
/** /**
* User agent string * User agent string
* *
* @var string * @var string
*/ */
protected $_userAgent = ''; protected static $_userAgent = '';
/** /**
* Path to where the session is active. * Path to where the session is active.
* *
* @var string * @var string
*/ */
public $path = '/'; public static $path = '/';
/** /**
* Error number of last occurred error * Error number of last occurred error
* *
* @var integer * @var integer
*/ */
public $lastError = null; public static $lastError = null;
/** /**
* 'Security.level' setting, "high", "medium", or "low". * 'Security.level' setting, "high", "medium", or "low".
* *
* @var string * @var string
*/ */
public $security = null; public static $security = null;
/** /**
* Start time for this session. * Start time for this session.
* *
* @var integer * @var integer
*/ */
public $time = false; public static $time = false;
/** /**
* Time when this session becomes invalid. * Time when this session becomes invalid.
* *
* @var integer * @var integer
*/ */
public $sessionTime = false; public static $sessionTime = false;
/** /**
* The number of seconds to set for session.cookie_lifetime. 0 means * The number of seconds to set for session.cookie_lifetime. 0 means
@ -103,28 +103,28 @@ class CakeSession extends Object {
* *
* @var array * @var array
*/ */
public $watchKeys = array(); public static $watchKeys = array();
/** /**
* Current Session id * Current Session id
* *
* @var string * @var string
*/ */
public $id = null; public static $id = null;
/** /**
* Hostname * Hostname
* *
* @var string * @var string
*/ */
public $host = null; public static $host = null;
/** /**
* Session timeout multiplier factor * Session timeout multiplier factor
* *
* @var integer * @var integer
*/ */
public $timeout = null; public static $timeout = null;
/** /**
* Constructor. * Constructor.
@ -134,11 +134,11 @@ class CakeSession extends Object {
*/ */
public function __construct($base = null, $start = true) { public function __construct($base = null, $start = true) {
App::import('Core', array('Set', 'Security')); App::import('Core', array('Set', 'Security'));
$this->time = time(); self::$time = time();
if (Configure::read('Session.checkAgent') === true || Configure::read('Session.checkAgent') === null) { if (Configure::read('Session.checkAgent') === true || Configure::read('Session.checkAgent') === null) {
if (env('HTTP_USER_AGENT') != null) { if (env('HTTP_USER_AGENT') != null) {
$this->_userAgent = md5(env('HTTP_USER_AGENT') . Configure::read('Security.salt')); self::$_userAgent = md5(env('HTTP_USER_AGENT') . Configure::read('Security.salt'));
} }
} }
if (Configure::read('Session.save') === 'database') { if (Configure::read('Session.save') === 'database') {
@ -165,26 +165,26 @@ class CakeSession extends Object {
} }
if ($start === true) { if ($start === true) {
if (!empty($base)) { if (!empty($base)) {
$this->path = $base; self::$path = $base;
if (strpos($base, 'index.php') !== false) { if (strpos($base, 'index.php') !== false) {
$this->path = str_replace('index.php', '', $base); self::$path = str_replace('index.php', '', $base);
} }
if (strpos($base, '?') !== false) { if (strpos($base, '?') !== false) {
$this->path = str_replace('?', '', $base); self::$path = str_replace('?', '', $base);
} }
} }
$this->host = env('HTTP_HOST'); self::$host = env('HTTP_HOST');
if (strpos($this->host, ':') !== false) { if (strpos(self::$host, ':') !== false) {
$this->host = substr($this->host, 0, strpos($this->host, ':')); self::$host = substr(self::$host, 0, strpos(self::$host, ':'));
} }
} }
if (isset($_SESSION) || $start === true) { if (isset($_SESSION) || $start === true) {
if (!class_exists('Security')) { if (!class_exists('Security')) {
App::import('Core', 'Security'); App::import('Core', 'Security');
} }
$this->sessionTime = $this->time + (Security::inactiveMins() * Configure::read('Session.timeout')); self::$sessionTime = self::$time + (Security::inactiveMins() * Configure::read('Session.timeout'));
$this->security = Configure::read('Security.level'); self::$security = Configure::read('Security.level');
} }
parent::__construct(); parent::__construct();
} }
@ -194,14 +194,14 @@ class CakeSession extends Object {
* *
* @return boolean True if session was started * @return boolean True if session was started
*/ */
public function start() { public static function start() {
if ($this->started()) { if (self::started()) {
return true; return true;
} }
session_write_close(); session_write_close();
$this->__initSession(); self::__initSession();
$this->__startSession(); self::$_started = self::__startSession();
return $this->started(); return self::started();
} }
/** /**
@ -209,8 +209,8 @@ class CakeSession extends Object {
* *
* @return boolean True if session has been started. * @return boolean True if session has been started.
*/ */
function started() { public static function started() {
if (!empty($_SESSION) && session_id()) { if (isset($_SESSION) && self::$_started) {
return true; return true;
} }
return false; return false;
@ -222,7 +222,7 @@ class CakeSession extends Object {
* @param string $name Variable name to check for * @param string $name Variable name to check for
* @return boolean True if variable is there * @return boolean True if variable is there
*/ */
public function check($name) { public static function check($name) {
if (empty($name)) { if (empty($name)) {
return false; return false;
} }
@ -236,16 +236,15 @@ class CakeSession extends Object {
* @param id $name string * @param id $name string
* @return string Session id * @return string Session id
*/ */
public function id($id = null) { public static function id($id = null) {
if ($id) { if ($id) {
$this->id = $id; self::$id = $id;
session_id($this->id); session_id(self::$id);
} }
if ($this->started()) { if (self::started()) {
return session_id(); return session_id();
} else {
return $this->id;
} }
return self::$id;
} }
/** /**
@ -254,15 +253,15 @@ class CakeSession extends Object {
* @param string $name Session variable to remove * @param string $name Session variable to remove
* @return boolean Success * @return boolean Success
*/ */
public function delete($name) { public static function delete($name) {
if ($this->check($name)) { if (self::check($name)) {
if (in_array($name, $this->watchKeys)) { if (in_array($name, self::$watchKeys)) {
trigger_error(sprintf(__('Deleting session key {%s}'), $name), E_USER_NOTICE); trigger_error(sprintf(__('Deleting session key {%s}'), $name), E_USER_NOTICE);
} }
$this->__overwrite($_SESSION, Set::remove($_SESSION, $name)); self::__overwrite($_SESSION, Set::remove($_SESSION, $name));
return ($this->check($name) == false); return (self::check($name) == false);
} }
$this->__setError(2, sprintf(__("%s doesn't exist"), $name)); self::__setError(2, sprintf(__("%s doesn't exist"), $name));
return false; return false;
} }
@ -294,10 +293,10 @@ class CakeSession extends Object {
* @access private * @access private
*/ */
function __error($errorNumber) { function __error($errorNumber) {
if (!is_array($this->error) || !array_key_exists($errorNumber, $this->error)) { if (!is_array(self::$error) || !array_key_exists($errorNumber, self::$error)) {
return false; return false;
} else { } else {
return $this->error[$errorNumber]; return self::$error[$errorNumber];
} }
} }
@ -306,12 +305,11 @@ class CakeSession extends Object {
* *
* @return mixed Error description as a string, or false. * @return mixed Error description as a string, or false.
*/ */
public function error() { public static function error() {
if ($this->lastError) { if (self::$lastError) {
return $this->__error($this->lastError); return self::$__error(self::$lastError);
} else {
return false;
} }
return false;
} }
/** /**
@ -319,18 +317,18 @@ class CakeSession extends Object {
* *
* @return boolean Success * @return boolean Success
*/ */
public function valid() { public static function valid() {
if ($this->read('Config')) { if (self::read('Config')) {
if ((Configure::read('Session.checkAgent') === false || $this->_userAgent == $this->read('Config.userAgent')) && $this->time <= $this->read('Config.time')) { if ((Configure::read('Session.checkAgent') === false || self::$_userAgent == self::read('Config.userAgent')) && self::$time <= self::read('Config.time')) {
if ($this->error === false) { if (self::$error === false) {
$this->valid = true; self::$valid = true;
} }
} else { } else {
$this->valid = false; self::$valid = false;
$this->__setError(1, 'Session Highjacking Attempted !!!'); self::__setError(1, 'Session Highjacking Attempted !!!');
} }
} }
return $this->valid; return self::$valid;
} }
/** /**
@ -339,9 +337,9 @@ class CakeSession extends Object {
* @param mixed $name The name of the session variable (or a path as sent to Set.extract) * @param mixed $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
*/ */
public function read($name = null) { public static function read($name = null) {
if (is_null($name)) { if (is_null($name)) {
return $this->__returnSessionVars(); return self::__returnSessionVars();
} }
if (empty($name)) { if (empty($name)) {
return false; return false;
@ -351,7 +349,7 @@ class CakeSession extends Object {
if (!is_null($result)) { if (!is_null($result)) {
return $result; return $result;
} }
$this->__setError(2, "$name doesn't exist"); self::__setError(2, "$name doesn't exist");
return null; return null;
} }
@ -365,7 +363,7 @@ class CakeSession extends Object {
if (!empty($_SESSION)) { if (!empty($_SESSION)) {
return $_SESSION; return $_SESSION;
} }
$this->__setError(2, 'No Session vars set'); self::__setError(2, 'No Session vars set');
return false; return false;
} }
@ -375,12 +373,12 @@ class CakeSession extends Object {
* @param mixed $var The variable path to watch * @param mixed $var The variable path to watch
* @return void * @return void
*/ */
public function watch($var) { public static function watch($var) {
if (empty($var)) { if (empty($var)) {
return false; return false;
} }
if (!in_array($var, $this->watchKeys, true)) { if (!in_array($var, self::$watchKeys, true)) {
$this->watchKeys[] = $var; self::$watchKeys[] = $var;
} }
} }
@ -390,14 +388,14 @@ class CakeSession extends Object {
* @param mixed $var The variable path to watch * @param mixed $var The variable path to watch
* @return void * @return void
*/ */
public function ignore($var) { public static function ignore($var) {
if (!in_array($var, $this->watchKeys)) { if (!in_array($var, self::$watchKeys)) {
return; return;
} }
foreach ($this->watchKeys as $i => $key) { foreach (self::$watchKeys as $i => $key) {
if ($key == $var) { if ($key == $var) {
unset($this->watchKeys[$i]); unset(self::$watchKeys[$i]);
$this->watchKeys = array_values($this->watchKeys); self::$watchKeys = array_values(self::$watchKeys);
return; return;
} }
} }
@ -410,14 +408,14 @@ class CakeSession extends Object {
* @param string $value Value to write * @param string $value Value to write
* @return boolean True if the write was successful, false if the write failed * @return boolean True if the write was successful, false if the write failed
*/ */
public function write($name, $value) { public static function write($name, $value) {
if (empty($name)) { if (empty($name)) {
return false; return false;
} }
if (in_array($name, $this->watchKeys)) { if (in_array($name, self::$watchKeys)) {
trigger_error(sprintf(__('Writing session key {%s}: %s'), $name, Debugger::exportVar($value)), E_USER_NOTICE); trigger_error(sprintf(__('Writing session key {%s}: %s'), $name, Debugger::exportVar($value)), E_USER_NOTICE);
} }
$this->__overwrite($_SESSION, Set::insert($_SESSION, $name, $value)); self::__overwrite($_SESSION, Set::insert($_SESSION, $name, $value));
return (Set::classicExtract($_SESSION, $name) === $value); return (Set::classicExtract($_SESSION, $name) === $value);
} }
@ -428,10 +426,10 @@ class CakeSession extends Object {
*/ */
public function destroy() { public function destroy() {
$_SESSION = array(); $_SESSION = array();
$this->__construct($this->path); self::__construct(self::$path);
$this->start(); self::start();
$this->renew(); self::renew();
$this->_checkValid(); self::_checkValid();
} }
/** /**
@ -444,15 +442,10 @@ class CakeSession extends Object {
if ($iniSet && env('HTTPS')) { if ($iniSet && env('HTTPS')) {
ini_set('session.cookie_secure', 1); ini_set('session.cookie_secure', 1);
} }
if ($iniSet && ($this->security === 'high' || $this->security === 'medium')) { if ($iniSet && (self::$security === 'high' || self::$security === 'medium')) {
ini_set('session.referer_check', $this->host); ini_set('session.referer_check', self::$host);
}
if ($this->security == 'high') {
$this->cookieLifeTime = 0;
} else {
$this->cookieLifeTime = Configure::read('Session.timeout') * (Security::inactiveMins() * 60);
} }
self::$cookieLifeTime = Configure::read('Session.timeout') * Security::inactiveMins();
switch (Configure::read('Session.save')) { switch (Configure::read('Session.save')) {
case 'cake': case 'cake':
@ -463,8 +456,8 @@ class CakeSession extends Object {
ini_set('session.serialize_handler', 'php'); ini_set('session.serialize_handler', 'php');
ini_set('session.use_cookies', 1); ini_set('session.use_cookies', 1);
ini_set('session.name', Configure::read('Session.cookie')); ini_set('session.name', Configure::read('Session.cookie'));
ini_set('session.cookie_lifetime', $this->cookieLifeTime); ini_set('session.cookie_lifetime', self::$cookieLifeTime);
ini_set('session.cookie_path', $this->path); ini_set('session.cookie_path', self::$path);
ini_set('session.auto_start', 0); ini_set('session.auto_start', 0);
ini_set('session.save_path', TMP . 'sessions'); ini_set('session.save_path', TMP . 'sessions');
} }
@ -474,7 +467,7 @@ class CakeSession extends Object {
if (empty($_SESSION)) { if (empty($_SESSION)) {
if (Configure::read('Session.model') === null) { if (Configure::read('Session.model') === null) {
trigger_error(__("You must set the all Configure::write('Session.*') in core.php to use database storage"), E_USER_WARNING); trigger_error(__("You must set the all Configure::write('Session.*') in core.php to use database storage"), E_USER_WARNING);
$this->_stop(); self::_stop();
} }
if ($iniSet) { if ($iniSet) {
ini_set('session.use_trans_sid', 0); ini_set('session.use_trans_sid', 0);
@ -483,8 +476,8 @@ class CakeSession extends Object {
ini_set('session.serialize_handler', 'php'); ini_set('session.serialize_handler', 'php');
ini_set('session.use_cookies', 1); ini_set('session.use_cookies', 1);
ini_set('session.name', Configure::read('Session.cookie')); ini_set('session.name', Configure::read('Session.cookie'));
ini_set('session.cookie_lifetime', $this->cookieLifeTime); ini_set('session.cookie_lifetime', self::$cookieLifeTime);
ini_set('session.cookie_path', $this->path); ini_set('session.cookie_path', self::$path);
ini_set('session.auto_start', 0); ini_set('session.auto_start', 0);
} }
} }
@ -502,8 +495,8 @@ class CakeSession extends Object {
if ($iniSet) { if ($iniSet) {
ini_set('session.use_trans_sid', 0); ini_set('session.use_trans_sid', 0);
ini_set('session.name', Configure::read('Session.cookie')); ini_set('session.name', Configure::read('Session.cookie'));
ini_set('session.cookie_lifetime', $this->cookieLifeTime); ini_set('session.cookie_lifetime', self::$cookieLifeTime);
ini_set('session.cookie_path', $this->path); ini_set('session.cookie_path', self::$path);
} }
} }
break; break;
@ -518,8 +511,8 @@ class CakeSession extends Object {
ini_set('session.save_handler', 'user'); ini_set('session.save_handler', 'user');
ini_set('session.use_cookies', 1); ini_set('session.use_cookies', 1);
ini_set('session.name', Configure::read('Session.cookie')); ini_set('session.name', Configure::read('Session.cookie'));
ini_set('session.cookie_lifetime', $this->cookieLifeTime); ini_set('session.cookie_lifetime', self::$cookieLifeTime);
ini_set('session.cookie_path', $this->path); ini_set('session.cookie_path', self::$path);
} }
} }
session_set_save_handler( session_set_save_handler(
@ -568,33 +561,33 @@ class CakeSession extends Object {
* *
* @return void * @return void
*/ */
protected function _checkValid() { protected static function _checkValid() {
if ($this->read('Config')) { if (self::read('Config')) {
if ((Configure::read('Session.checkAgent') === false || $this->_userAgent == $this->read('Config.userAgent')) && $this->time <= $this->read('Config.time')) { if ((Configure::read('Session.checkAgent') === false || self::$_userAgent == self::read('Config.userAgent')) && self::$time <= self::read('Config.time')) {
$time = $this->read('Config.time'); $time = self::read('Config.time');
$this->write('Config.time', $this->sessionTime); self::write('Config.time', self::$sessionTime);
if (Configure::read('Security.level') === 'high') { if (Configure::read('Security.level') === 'high') {
$check = $this->read('Config.timeout'); $check = self::read('Config.timeout');
$check -= 1; $check -= 1;
$this->write('Config.timeout', $check); self::write('Config.timeout', $check);
if (time() > ($time - (Security::inactiveMins() * Configure::read('Session.timeout')) + 2) || $check < 1) { if (time() > ($time - (Security::inactiveMins() * Configure::read('Session.timeout')) + 2) || $check < 1) {
$this->renew(); self::renew();
$this->write('Config.timeout', Security::inactiveMins()); self::write('Config.timeout', Security::inactiveMins());
} }
} }
$this->valid = true; self::$valid = true;
} else { } else {
$this->destroy(); self::destroy();
$this->valid = false; self::$valid = false;
$this->__setError(1, 'Session Highjacking Attempted !!!'); self::__setError(1, 'Session Highjacking Attempted !!!');
} }
} else { } else {
$this->write('Config.userAgent', $this->_userAgent); self::write('Config.userAgent', self::$_userAgent);
$this->write('Config.time', $this->sessionTime); self::write('Config.time', self::$sessionTime);
$this->write('Config.timeout', Security::inactiveMins()); self::write('Config.timeout', Security::inactiveMins());
$this->valid = true; self::$valid = true;
$this->__setError(1, 'Session is valid'); self::__setError(1, 'Session is valid');
} }
} }
@ -608,7 +601,7 @@ class CakeSession extends Object {
$oldSessionId = session_id(); $oldSessionId = session_id();
if ($oldSessionId) { if ($oldSessionId) {
if (session_id() != ''|| isset($_COOKIE[session_name()])) { if (session_id() != ''|| isset($_COOKIE[session_name()])) {
setcookie(Configure::read('Session.cookie'), '', time() - 42000, $this->path); setcookie(Configure::read('Session.cookie'), '', time() - 42000, self::$path);
} }
session_regenerate_id(true); session_regenerate_id(true);
if (PHP_VERSION < 5.1) { if (PHP_VERSION < 5.1) {
@ -621,13 +614,13 @@ class CakeSession extends Object {
if (function_exists('session_write_close')) { if (function_exists('session_write_close')) {
session_write_close(); session_write_close();
} }
$this->__initSession(); self::__initSession();
session_id($oldSessionId); session_id($oldSessionId);
session_start(); session_start();
session_destroy(); session_destroy();
$file = $sessionPath . DS . 'sess_' . $oldSessionId; $file = $sessionPath . DS . 'sess_' . $oldSessionId;
@unlink($file); @unlink($file);
$this->__initSession(); self::__initSession();
session_id($newSessid); session_id($newSessid);
session_start(); session_start();
} }
@ -639,7 +632,7 @@ class CakeSession extends Object {
* *
*/ */
public function renew() { public function renew() {
$this->__regenerateId(); self::__regenerateId();
} }
/** /**
@ -651,11 +644,11 @@ class CakeSession extends Object {
* @access private * @access private
*/ */
function __setError($errorNumber, $errorMessage) { function __setError($errorNumber, $errorMessage) {
if ($this->error === false) { if (self::$error === false) {
$this->error = array(); self::$error = array();
} }
$this->error[$errorNumber] = $errorMessage; self::$error[$errorNumber] = $errorMessage;
$this->lastError = $errorNumber; self::$lastError = $errorNumber;
} }
/** /**