Skip to content

Commit

Permalink
Updated the Session classs to handle a case where cookies are disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
mystralkk committed Aug 18, 2019
1 parent c9ffcf9 commit 3018541
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 24 deletions.
217 changes: 193 additions & 24 deletions system/classes/Session.php
Expand Up @@ -23,18 +23,39 @@ abstract class Session
const ANON_USER_ID = 1;

/**
* "flash", i.e., one-time session variables
* The flag to show if the class is initialized
*
* @var array
* @var bool
*/
private static $flashVars = array();
private static $isInitialized = false;

/**
* The flag to show if the class is initialized
* The flag to show if the session is enabled
*
* @var bool
*/
private static $isInitialized = false;
private static $isEnabled = false;

/**
* The flag to show if the session has started
*
* @var bool
*/
private static $isSessionHasStarted = false;

/**
* Log function
*
* @var callable
*/
private static $logFunction;

/**
* "flash", i.e., one-time session variables
*
* @var array
*/
private static $flashVars = array();

/**
* Init the Session class
Expand Down Expand Up @@ -75,9 +96,18 @@ public static function init(array $config)
// Set session cookie parameters
self::setSessionCookieParameters($config);

// Start a new session
if (!session_start()) {
die(__METHOD__ . ': Cannot start session.');
// Set logger
if (isset($config['logger']) && is_callable($config['logger'])) {
self::$logFunction = $config['logger'];
}

if (isset($config['cookie_disabled']) && !$config['cookie_disabled']) {
return false;
}

self::enable();
if (!self::start()) {
return false;
}

// Initialize the $_SESSION var if this is the first visit to the site
Expand Down Expand Up @@ -109,14 +139,127 @@ public static function init(array $config)
return $retval;
}

/**
* Return if session is enabled
*
* @return bool
*/
public static function isEnabled()
{
return self::$isEnabled;
}

/**
* Disable session
*/
public static function Disable()
{
self::$isEnabled = false;
}

/**
* Enable session
*/
public static function enable()
{
self::$isEnabled = true;
}

/**
* Check if session is enabled and has started normally
*
* @return bool
*/
private static function check()
{
if (self::$isEnabled) {
if (self::$isSessionHasStarted) {
return true;
} else {
self::log('Session has not started yet.');
}
} else {
self::log('Session is disabled.');
}

return false;
}

/**
* Log an entry
*
* @param string $entry
*/
private static function log($entry)
{
if (is_callable(self::$logFunction)) {
$f = self::$logFunction;
$f($entry);
}
}

/**
* Start a new session
*
* @return bool true if session started successfully, false otherwise
*/
public static function start()
{
if (!self::$isEnabled ) {
self::log('Session is disabled.');

return false;
}

if (self::$isSessionHasStarted) {
return true;
}

// Start a new session
self::$isSessionHasStarted = session_start();
if (!self::$isSessionHasStarted) {
self::disable();
self::log(__METHOD__ . ': Cannot start a new session. Session was disabled.');
}

return self::$isSessionHasStarted;
}

/**
* End the current session
*
* @return bool true on success, false otherwise
*/
public static function end()
{
if (!self::$isEnabled ) {
self::log('Session is disabled.');

return false;
}

if (!self::$isSessionHasStarted) {
return true;
}

session_write_close();
self::$isSessionHasStarted = false;

return true;
}

/**
* Return if the current user is logged in to the site
*
* @return bool
*/
public static function isLoggedIn()
{
return (self::getUid() > self::ANON_USER_ID);
if (self::check()) {
return (self::getUid() > self::ANON_USER_ID);
} else {
return false;
}
}

/**
Expand All @@ -126,7 +269,11 @@ public static function isLoggedIn()
*/
public static function getUid()
{
return $_SESSION[self::NS_GL][self::NS_VAR]['uid'];
if (self::check()) {
return $_SESSION[self::NS_GL][self::NS_VAR]['uid'];
} else {
return self::ANON_USER_ID;
}
}

/**
Expand All @@ -137,12 +284,14 @@ public static function getUid()
*/
public static function setUid($uid)
{
$uid = (int) $uid;
if (self::check()) {
$uid = (int) $uid;

if ($uid >= self::ANON_USER_ID) {
$_SESSION[self::NS_GL][self::NS_VAR]['uid'] = $uid;
} else {
throw new InvalidArgumentException('User id must be ' . self::ANON_USER_ID . ' or greater.');
if ($uid >= self::ANON_USER_ID) {
$_SESSION[self::NS_GL][self::NS_VAR]['uid'] = $uid;
} else {
throw new InvalidArgumentException('User id must be ' . self::ANON_USER_ID . ' or greater.');
}
}
}

Expand All @@ -154,7 +303,9 @@ public static function setUid($uid)
*/
public static function setVar($name, $value)
{
$_SESSION[self::NS_GL][self::NS_VAR][$name] = $value;
if (self::check()) {
$_SESSION[self::NS_GL][self::NS_VAR][$name] = $value;
}
}

/**
Expand All @@ -165,7 +316,9 @@ public static function setVar($name, $value)
*/
public static function setFlashVar($name, $value)
{
$_SESSION[self::NS_GL][self::NS_FLASH_VAR][$name] = $value;
if (self::check()) {
$_SESSION[self::NS_GL][self::NS_FLASH_VAR][$name] = $value;
}
}

/**
Expand All @@ -177,9 +330,13 @@ public static function setFlashVar($name, $value)
*/
public static function getVar($name, $defaultValue = null)
{
return isset($_SESSION[self::NS_GL][self::NS_VAR][$name])
? $_SESSION[self::NS_GL][self::NS_VAR][$name]
: $defaultValue;
if (self::check()) {
return isset($_SESSION[self::NS_GL][self::NS_VAR][$name])
? $_SESSION[self::NS_GL][self::NS_VAR][$name]
: $defaultValue;
} else {
return $defaultValue;
}
}

/**
Expand All @@ -191,7 +348,11 @@ public static function getVar($name, $defaultValue = null)
*/
public static function getFlashVar($name, $defaultValue = null)
{
return isset(self::$flashVars[$name]) ? self::$flashVars[$name] : $defaultValue;
if (self::check()) {
return isset(self::$flashVars[$name]) ? self::$flashVars[$name] : $defaultValue;
} else {
return $defaultValue;
}
}

/**
Expand All @@ -201,9 +362,13 @@ public static function getFlashVar($name, $defaultValue = null)
*/
public static function regenerateId()
{
session_regenerate_id(false);
if (self::check()) {
session_regenerate_id(false);

return self::getSessionId();
return self::getSessionId();
} else {
return null;
}
}

/**
Expand Down Expand Up @@ -249,6 +414,10 @@ private static function setSessionCookieParameters(array $config)
*/
public static function getSessionId()
{
return session_id();
if (self::check()) {
return session_id();
} else {
return null;
}
}
}
2 changes: 2 additions & 0 deletions system/lib-sessions.php
Expand Up @@ -85,6 +85,8 @@ function SESS_sessionCheck()

// Flag indicates if session cookie and session data exist
$sessionExists = Session::init(array(
'logger' => 'COM_errorLog',
'cookie_disabled' => false,
'cookie_lifetime' => $_CONF['session_cookie_timeout'],
'cookie_path' => $_CONF['cookie_path'],
'cookie_domain' => $_CONF['cookiedomain'],
Expand Down

0 comments on commit 3018541

Please sign in to comment.