Skip to content

Commit

Permalink
Added debug feature for the Session class (controlled by $_CONF['deve…
Browse files Browse the repository at this point in the history
…loper_mode_log']['session'] in "siteconfig.php')
  • Loading branch information
mystralkk committed Aug 19, 2019
1 parent 79e6d94 commit 53c5149
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 25 deletions.
193 changes: 168 additions & 25 deletions system/classes/Session.php
Expand Up @@ -29,6 +29,13 @@ abstract class Session
*/
private static $isInitialized = false;

/**
* The flag to show if the debug mode is on
*
* @var bool
*/
private static $isDebug = false;

/**
* The flag to show if the session is enabled
*
Expand Down Expand Up @@ -65,6 +72,10 @@ abstract class Session
*/
public static function init(array $config)
{
if (self::$isDebug) {
self::log(__METHOD__ . ': started');
}

$retval = true;

if (self::$isInitialized) {
Expand Down Expand Up @@ -93,6 +104,9 @@ public static function init(array $config)
ini_set('session.cookie_lifetime', 0);
}

// Set debug mode
self::setDebug(isset($config['debug']) && $config['debug']);

// Set session cookie parameters
self::setSessionCookieParameters($config);

Expand All @@ -101,7 +115,7 @@ public static function init(array $config)
self::$logFunction = $config['logger'];
}

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

Expand Down Expand Up @@ -135,26 +149,47 @@ public static function init(array $config)

// Finish initialization
self::$isInitialized = true;
if (self::$isDebug) {
self::log(__METHOD__ . ': finished');
}

return $retval;
}

/**
* Set debug mode
*
* @param bool $switch
*/
public static function setDebug($switch)
{
self::$isDebug = (bool) $switch;
}

/**
* Return if session is enabled
*
* @return bool
*/
public static function isEnabled()
{
if (self::$isDebug) {
self::log(__METHOD__ . ': ' . (self::$isEnabled ? 'true' : 'false'));
}

return self::$isEnabled;
}

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

if (self::$isDebug) {
self::log(__METHOD__ . ' was called.');
}
}

/**
Expand All @@ -163,6 +198,10 @@ public static function Disable()
public static function enable()
{
self::$isEnabled = true;

if (self::$isDebug) {
self::log(__METHOD__ . ' was called.');
}
}

/**
Expand All @@ -172,28 +211,42 @@ public static function enable()
*/
private static function check()
{
$retval = false;
$msg = '';

if (self::$isEnabled) {
if (self::$isSessionHasStarted) {
return true;
if (self::$isDebug) {
$msg = 'session is valid.';
}

$retval = true;
} else {
self::log('Session has not started yet.');
if (self::$isDebug) {
$msg = 'session has not started yet.';
}
}
} else {
self::log('Session is disabled.');
$msg = 'session is disabled.';
}

if (self::$isDebug) {
self::log(__METHOD__ . ': ' . $msg);
}

return false;
return $retval;
}

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

if (is_callable($f)) {
$f($entry);
}
}
Expand All @@ -205,13 +258,19 @@ private static function log($entry)
*/
public static function start()
{
if (!self::$isEnabled ) {
self::log('Session is disabled.');
if (!self::$isEnabled) {
if (self::$isDebug) {
self::log(__METHOD__ . ': session is disabled.');
}

return false;
}

if (self::$isSessionHasStarted) {
if (self::$isDebug) {
self::log(__METHOD__ . ': session has already started.');
}

return true;
}

Expand All @@ -220,6 +279,10 @@ public static function start()
if (!self::$isSessionHasStarted) {
self::disable();
self::log(__METHOD__ . ': Cannot start a new session. Session was disabled.');
} else {
if (self::$isDebug) {
self::log(__METHOD__ . ': session has successfully started.');
}
}

return self::$isSessionHasStarted;
Expand All @@ -232,19 +295,29 @@ public static function start()
*/
public static function end()
{
if (!self::$isEnabled ) {
self::log('Session is disabled.');
if (!self::$isEnabled) {
if (self::$isDebug) {
self::log(__METHOD__ . ': session is disabled.');
}

return false;
}

if (!self::$isSessionHasStarted) {
if (self::$isDebug) {
self::log(__METHOD__ . ': session has not started.');
}

return true;
}

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

if (self::$isDebug) {
self::log(__METHOD__ . ': session has successfully ended.');
}

return true;
}

Expand All @@ -256,10 +329,17 @@ public static function end()
public static function isLoggedIn()
{
if (self::check()) {
return (self::getUid() > self::ANON_USER_ID);
$retval = (self::getUid() > self::ANON_USER_ID);
} else {
return false;
$retval = false;
}

// Debug info
if (self::$isDebug) {
self::log(__METHOD__ . ': login status = ' . ($retval ? 'true' : 'false'));
}

return $retval;
}

/**
Expand All @@ -270,10 +350,17 @@ public static function isLoggedIn()
public static function getUid()
{
if (self::check()) {
return $_SESSION[self::NS_GL][self::NS_VAR]['uid'];
$retval = $_SESSION[self::NS_GL][self::NS_VAR]['uid'];
} else {
return self::ANON_USER_ID;
$retval = self::ANON_USER_ID;
}

// Debug info
if (self::$isDebug) {
self::log(sprintf(__METHOD__ . ': uid = %d', $retval));
}

return $retval;
}

/**
Expand All @@ -286,12 +373,20 @@ public static function setUid($uid)
{
if (self::check()) {
$uid = (int) $uid;
$msg = 'uid = ' . $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.');
}
} else {
$msg = '(skipped)';
}

// Debug info
if (self::$isDebug) {
self::log(__METHOD__ . ': ' . $msg);
}
}

Expand All @@ -305,6 +400,14 @@ public static function setVar($name, $value)
{
if (self::check()) {
$_SESSION[self::NS_GL][self::NS_VAR][$name] = $value;
$msg = sprintf('name = %s, value = %s', $name, $value);
} else {
$msg = '(skipped)';
}

// Debug info
if (self::$isDebug) {
self::log(__METHOD__ . ': ' . $msg);
}
}

Expand All @@ -318,6 +421,14 @@ public static function setFlashVar($name, $value)
{
if (self::check()) {
$_SESSION[self::NS_GL][self::NS_FLASH_VAR][$name] = $value;
$msg = sprintf('name = %s, value = %s', $name, $value);
} else {
$msg = '(skipped)';
}

// Debug info
if (self::$isDebug) {
self::log(__METHOD__ . ': ' . $msg);
}
}

Expand All @@ -331,12 +442,19 @@ public static function setFlashVar($name, $value)
public static function getVar($name, $defaultValue = null)
{
if (self::check()) {
return isset($_SESSION[self::NS_GL][self::NS_VAR][$name])
$retval = isset($_SESSION[self::NS_GL][self::NS_VAR][$name])
? $_SESSION[self::NS_GL][self::NS_VAR][$name]
: $defaultValue;
} else {
return $defaultValue;
$retval = $defaultValue;
}

// Debug info
if (self::$isDebug) {
self::log(sprintf(__METHOD__ . ': name = "%s", value = "%s"', $name, $retval));
}

return $retval;
}

/**
Expand All @@ -349,10 +467,17 @@ public static function getVar($name, $defaultValue = null)
public static function getFlashVar($name, $defaultValue = null)
{
if (self::check()) {
return isset(self::$flashVars[$name]) ? self::$flashVars[$name] : $defaultValue;
$retval = isset(self::$flashVars[$name]) ? self::$flashVars[$name] : $defaultValue;
} else {
return $defaultValue;
$retval = $defaultValue;
}

// Debug info
if (self::$isDebug) {
self::log(sprintf(__METHOD__ . ': name = "%s", value = "%s"', $name, $retval));
}

return $retval;
}

/**
Expand All @@ -362,6 +487,11 @@ public static function getFlashVar($name, $defaultValue = null)
*/
public static function regenerateId()
{
// Debug info
if (self::$isDebug) {
self::log(__METHOD__ . ' was called');
}

if (self::check()) {
session_regenerate_id(false);

Expand Down Expand Up @@ -405,6 +535,16 @@ private static function setSessionCookieParameters(array $config)
$args['httponly'] = true;
session_set_cookie_params($args['lifetime'], $args['path'], $args['domain'], $args['secure'], $args['httponly']);
session_name($config['session_name']);

// Debug info
if (self::$isDebug) {
self::log(sprintf(
__METHOD__ . ': lifetime = %d, path = %s, domain = %s, secure = %s, httponly = %s',
$args['lifetime'], $args['path'], $args['domain'], ($args['secure'] ? 'true' : 'false'),
($args['httponly'] ? 'true' : 'false')
));
self::log(sprintf(__METHOD__ . ': session name = %s', $config['session_name']));
}
}

/**
Expand All @@ -414,10 +554,13 @@ private static function setSessionCookieParameters(array $config)
*/
public static function getSessionId()
{
if (self::check()) {
return session_id();
} else {
return null;
$retval = self::check() ? session_id() : '';

// Debug info
if (self::$isDebug) {
self::log(sprintf(__METHOD__ . ': session id = %s', $retval));
}

return $retval;
}
}

0 comments on commit 53c5149

Please sign in to comment.