diff msession.inc.php @ 33:5bf22431176c

Modularize.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 11 Dec 2012 11:46:47 +0200
parents
children 7bdf89601ba0
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/msession.inc.php	Tue Dec 11 11:46:47 2012 +0200
@@ -0,0 +1,161 @@
+<?
+//
+// FAPWEB - Demo Party Website System System
+// Session management and authentication
+// (C) Copyright 2012 Matti 'ccr' Hamalainen <ccr@tnsp.org>
+//
+
+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;
+}
+
+
+function stSetSessionItem($name, $value)
+{
+  global $sessionType;
+  if (!isset($sessionType))
+    die("Session type not set.");
+  
+  $_SESSION[$sessionType][$name] = $value;
+}
+
+
+function stSessionExpire()
+{
+  global $sessionType;
+
+  // Check for session expiration
+  if (!isset($_SESSION[$sessionType]) || !isset($_SESSION[$sessionType]["expires"]))
+  {
+    if (stGetSetting("debug")) error_log("Session ".$sessionType." expires due to expire time not set.");
+    stSessionEnd();
+    return FALSE;
+  }
+  
+  if ($_SESSION[$sessionType]["expires"] < time())
+  {
+    if (stGetSetting("debug")) error_log("Session ".$sessionType." / ".session_id()." expires due to timeout ".$_SESSION[$sessionType]["expires"]." < ".time());
+    stSessionEnd();
+    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;
+  return TRUE;
+}
+
+
+function stSessionEnd()
+{
+  global $sessionType;
+  $result = FALSE;
+
+  if (stGetSetting("debug")) error_log("Request END session ".$sessionType);
+
+  if (@session_start() === TRUE && isset($_SESSION))
+  {
+    // End current session type
+    if (isset($_SESSION[$sessionType]))
+    {
+      if (stGetSetting("debug")) error_log("END session ".$sessionType." / ".$_SESSION[$sessionType]["expires"]);
+      $_SESSION[$sessionType] = array();
+      unset($_SESSION[$sessionType]);
+      $result = TRUE;
+    }
+
+    // If all session types are ended, clear the cookies etc
+    if (!isset($_SESSION["user"]) && !isset($_SESSION["admin"]))
+    {
+      if (stGetSetting("debug")) error_log("Clearing all session data.");
+      $_SESSION = array();
+
+      if (ini_get("session.use_cookies"))
+      {
+        $params = session_get_cookie_params();
+        setcookie(session_name(), "", time() - 242000,
+          $params["path"], $params["domain"],
+          $params["secure"], $params["httponly"]
+        );
+      }
+
+      @session_destroy();
+    }
+  }
+
+  return $result;
+}
+
+
+function stSessionStart($key, $timeout)
+{
+  global $sessionType;
+
+  if (@session_start() === TRUE)
+  {
+    if (stGetSetting("debug")) error_log("START ".$sessionType." session OK.");
+    $_SESSION[$sessionType] = array(
+      "key" => $key,
+      "timeout" => $timeout,
+      "expires" => time() + stGetSetting($timeout) * 60,
+      "message" => "",
+      "status" => 0,
+    );
+    return TRUE;
+  }
+  else
+  {
+    if (stGetSetting("debug")) error_log("START ".$sessionType." session --FAILED--");
+    return FALSE;
+  }
+}
+
+
+function stAdmSessionAuth()
+{
+  if (@session_start() === TRUE &&
+    stGetSessionItem("key", FALSE) == stGetSetting("admPassword"))
+  {
+    if (stGetSetting("debug")) error_log("AUTH admin session OK.");
+    return stSessionExpire();
+  }
+  else
+  {
+    if (stGetSetting("debug")) error_log("AUTH admin session FAIL.");
+    return FALSE;
+  }
+}
+
+
+function stUserSessionAuth()
+{
+  global $sessionType;
+
+  if (@session_start() === TRUE &&
+    isset($_SESSION[$sessionType]) &&
+    isset($_SESSION[$sessionType]["key"]))
+    return stSessionExpire();
+  else
+    return FALSE;
+}
+
+
+function stSetSessionStatus($status)
+{
+  global $sessionType;
+  if (isset($_SESSION[$sessionType]) || session_start() === TRUE)
+  {
+    if ($status >= 0)
+      stSetSessionItem("prevstatus", stGetSessionItem("status", FALSE));
+
+    stSetSessionItem("status", $status);
+  }
+}
+
+?>
\ No newline at end of file