diff msession.inc.php @ 51:7bdf89601ba0

Work on session stuff.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 05 Oct 2013 06:55:58 +0300
parents 5bf22431176c
children 70c0b21f0781
line wrap: on
line diff
--- a/msession.inc.php	Sat Oct 05 04:14:21 2013 +0300
+++ b/msession.inc.php	Sat Oct 05 06:55:58 2013 +0300
@@ -5,13 +5,26 @@
 // (C) Copyright 2012 Matti 'ccr' Hamalainen <ccr@tnsp.org>
 //
 
+function stDebug($msg)
+{
+  if (stGetSetting("debug"))
+    error_log($msg);
+}
+
+
+function stGetSpecSessionItem($stype, $name, $default = "")
+{
+  if (isset($stype))
+    return (isset($_SESSION[$stype]) && isset($_SESSION[$stype][$name])) ? $_SESSION[$stype][$name] : $default;
+  else
+    return $default;
+}
+
+
 function stGetSessionItem($name, $default = "")
 {
   global $sessionType;
-  if (isset($sessionType))
-    return (isset($_SESSION[$sessionType]) && isset($_SESSION[$sessionType][$name])) ? trim($_SESSION[$sessionType][$name]) : $default;
-  else
-    return isset($_SESSION[$name]) ? trim($_SESSION[$name]) : $default;
+  return stGetSpecSessionItem($sessionType, $name, $default);
 }
 
 
@@ -25,55 +38,52 @@
 }
 
 
-function stSessionExpire()
+function stSessionExpire($stype)
 {
-  global $sessionType;
-
   // Check for session expiration
-  if (!isset($_SESSION[$sessionType]) || !isset($_SESSION[$sessionType]["expires"]))
+  if (!isset($_SESSION[$stype]) || !isset($_SESSION[$stype]["expires"]))
   {
-    if (stGetSetting("debug")) error_log("Session ".$sessionType." expires due to expire time not set.");
-    stSessionEnd();
+    stDebug("Session ".$stype." expires due to expire time not set.");
+    stSessionEnd($stype);
     return FALSE;
   }
   
-  if ($_SESSION[$sessionType]["expires"] < time())
+  if ($_SESSION[$stype]["expires"] < time())
   {
-    if (stGetSetting("debug")) error_log("Session ".$sessionType." / ".session_id()." expires due to timeout ".$_SESSION[$sessionType]["expires"]." < ".time());
-    stSessionEnd();
+    stDebug("Session ".$stype." / ".session_id()." expires due to timeout ".$_SESSION[$stype]["expires"]." < ".time());
+    stSessionEnd($stype);
     return FALSE;
   }
 
   // Add more time to expiration
-  $timeout = stGetSetting($_SESSION[$sessionType]["timeout"], 0);
-  if (stGetSetting("debug")) error_log("Adding more time to ".$sessionType." session ".session_id()." :: ".$timeout);
-  $_SESSION[$sessionType]["expires"] = time() + $timeout * 60;
+  $timeout = stGetSetting($_SESSION[$stype]["timeout"], 0);
+  stDebug("Adding more time to ".$stype." session ".session_id()." :: ".$timeout);
+  $_SESSION[$stype]["expires"] = time() + $timeout * 60;
   return TRUE;
 }
 
 
-function stSessionEnd()
+function stSessionEnd($stype)
 {
-  global $sessionType;
   $result = FALSE;
 
-  if (stGetSetting("debug")) error_log("Request END session ".$sessionType);
+  stDebug("Request END session ".$stype);
 
   if (@session_start() === TRUE && isset($_SESSION))
   {
     // End current session type
-    if (isset($_SESSION[$sessionType]))
+    if (isset($_SESSION[$stype]))
     {
-      if (stGetSetting("debug")) error_log("END session ".$sessionType." / ".$_SESSION[$sessionType]["expires"]);
-      $_SESSION[$sessionType] = array();
-      unset($_SESSION[$sessionType]);
+      stDebug("END session ".$stype." / ".$_SESSION[$stype]["expires"]);
+      $_SESSION[$stype] = array();
+      unset($_SESSION[$stype]);
       $result = TRUE;
     }
 
     // If all session types are ended, clear the cookies etc
-    if (!isset($_SESSION["user"]) && !isset($_SESSION["admin"]))
+    if (!isset($_SESSION[SESS_USER]) && !isset($_SESSION[SESS_ADMIN]))
     {
-      if (stGetSetting("debug")) error_log("Clearing all session data.");
+      stDebug("Clearing all session data.");
       $_SESSION = array();
 
       if (ini_get("session.use_cookies"))
@@ -93,14 +103,12 @@
 }
 
 
-function stSessionStart($key, $timeout)
+function stSessionStart($stype, $key, $timeout)
 {
-  global $sessionType;
-
   if (@session_start() === TRUE)
   {
-    if (stGetSetting("debug")) error_log("START ".$sessionType." session OK.");
-    $_SESSION[$sessionType] = array(
+    stDebug("START ".$stype." session OK.");
+    $_SESSION[$stype] = array(
       "key" => $key,
       "timeout" => $timeout,
       "expires" => time() + stGetSetting($timeout) * 60,
@@ -111,7 +119,7 @@
   }
   else
   {
-    if (stGetSetting("debug")) error_log("START ".$sessionType." session --FAILED--");
+    stDebug("START ".$stype." session --FAILED--");
     return FALSE;
   }
 }
@@ -120,14 +128,14 @@
 function stAdmSessionAuth()
 {
   if (@session_start() === TRUE &&
-    stGetSessionItem("key", FALSE) == stGetSetting("admPassword"))
+    stGetSpecSessionItem(SESS_ADMIN, "key", FALSE) == stGetSetting("admPassword"))
   {
-    if (stGetSetting("debug")) error_log("AUTH admin session OK.");
-    return stSessionExpire();
+    stDebug("AUTH admin session OK.");
+    return stSessionExpire(SESS_ADMIN);
   }
   else
   {
-    if (stGetSetting("debug")) error_log("AUTH admin session FAIL.");
+    stDebug("AUTH admin session FAIL.");
     return FALSE;
   }
 }
@@ -135,14 +143,17 @@
 
 function stUserSessionAuth()
 {
-  global $sessionType;
-
   if (@session_start() === TRUE &&
-    isset($_SESSION[$sessionType]) &&
-    isset($_SESSION[$sessionType]["key"]))
-    return stSessionExpire();
+    stGetSpecSessionItem(SESS_USER, "key", FALSE) !== FALSE)
+  {
+    stDebug("AUTH user session OK.");
+    return stSessionExpire(SESS_ADMIN);
+  }
   else
+  {
+    stDebug("AUTH user session FAIL.");
     return FALSE;
+  }
 }