From 53c5149a2f2d9569f80bbc885883d7888527c88a Mon Sep 17 00:00:00 2001 From: mystralkk Date: Mon, 19 Aug 2019 20:50:12 +0900 Subject: [PATCH] Added debug feature for the Session class (controlled by $_CONF['developer_mode_log']['session'] in "siteconfig.php') --- system/classes/Session.php | 193 ++++++++++++++++++++++++++++++++----- system/lib-sessions.php | 1 + 2 files changed, 169 insertions(+), 25 deletions(-) diff --git a/system/classes/Session.php b/system/classes/Session.php index b6138aeba..8f5b3e759 100644 --- a/system/classes/Session.php +++ b/system/classes/Session.php @@ -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 * @@ -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) { @@ -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); @@ -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; } @@ -135,10 +149,23 @@ 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 * @@ -146,15 +173,23 @@ public static function init(array $config) */ 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.'); + } } /** @@ -163,6 +198,10 @@ public static function Disable() public static function enable() { self::$isEnabled = true; + + if (self::$isDebug) { + self::log(__METHOD__ . ' was called.'); + } } /** @@ -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); } } @@ -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; } @@ -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; @@ -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; } @@ -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; } /** @@ -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; } /** @@ -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); } } @@ -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); } } @@ -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); } } @@ -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; } /** @@ -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; } /** @@ -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); @@ -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'])); + } } /** @@ -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; } } diff --git a/system/lib-sessions.php b/system/lib-sessions.php index e07b8c1a8..174f59cd4 100644 --- a/system/lib-sessions.php +++ b/system/lib-sessions.php @@ -85,6 +85,7 @@ function SESS_sessionCheck() // Flag indicates if session cookie and session data exist $sessionExists = Session::init(array( + 'debug' => isset($_CONF['developer_mode_log']['session']) && $_CONF['developer_mode_log']['session'], 'logger' => 'COM_errorLog', 'cookie_disabled' => false, 'cookie_lifetime' => $_CONF['session_cookie_timeout'],