Some cleanups/improvements
[mailer.git] / inc / session-functions.php
index 71f67ca3033d094dc5de2bb4d41b38b23a662721..5421c1f013a55d2512f3c137b3675a4d17e51bb4 100644 (file)
@@ -16,8 +16,8 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
- * For more information visit: http://www.mxchange.org                  *
+ * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
+ * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
  * it under the terms of the GNU General Public License as published by *
 // Some security stuff...
 if (!defined('__SECURITY')) {
        die();
-}
+} // END - if
 
 // Unset/set session variables
 function setSession ($var, $value) {
        // Abort in CSS mode here
-       if (isCssOutputMode()) return true;
+       if (isCssOutputMode()) {
+               return TRUE;
+       } // END - if
 
        // Trim value and session variable
-       $var = trim(secureString($var)); $value = trim($value);
+       $var   = trim(secureString($var));
+       $value = trim($value);
 
        // Is the session variable set?
        if (('' . $value . '' == '') && (isSessionVariableSet($var))) {
                // Remove the session
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'UNSET:' . $var . '=' . getSession($var));
                unset($GLOBALS['_SESSION'][$var]);
-               if (phpversion() >= '5.3.1') {
-                       // session_unregister() is deprecated as of 5.3.1
-                       return true;
+               if (isPhpVersionEqualNewer('5.3.0')) {
+                       // session_unregister() is deprecated as of 5.3.0
+                       return TRUE;
                } else {
-                       // PHP version < 5.3.1
+                       // PHP version < 5.3.0
                        return session_unregister($var);
                }
        } elseif (('' . $value . '' != '') && (!isSessionVariableSet($var))) {
                // Set session
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SET:' . $var . '=' . $value);
-               $GLOBALS['_SESSION'][$var] =  $value;
-               if (phpversion() >= '5.3.1') {
-                       // session_unregister() is deprecated as of 5.3.1
-                       return true;
+               $GLOBALS['_SESSION'][$var] = $value;
+               if (isPhpVersionEqualNewer('5.3.0')) {
+                       // session_unregister() is deprecated as of 5.3.0
+                       return TRUE;
                } else {
-                       // PHP version < 5.3.1
+                       // PHP version < 5.3.0
                        return session_register($var);
                }
        } elseif (!empty($value)) {
                // Update session
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'UPDATE:' . $var . '=' . $value);
                $GLOBALS['_SESSION'][$var] = $value;
-               return true;
+               return TRUE;
        }
 
        // Ignored (but valid)
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'IGNORED:' . $var . '=' . $value);
-       return true;
+       return TRUE;
 }
 
-// Check wether a session variable is set
+// Check whether a session variable is set
 function isSessionVariableSet ($var) {
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "var={$var}");
+       // Warning: DO NOT call logDebugMessage() from here, this will cause an endless loop
        return (isset($GLOBALS['_SESSION'][$var]));
 }
 
-// Returns wether the value of the session variable or NULL if not set
+// Returns whether the value of the session variable or NULL if not set
 function getSession ($var) {
-       // Default is not found! ;-)
-       $value = null;
+       // Default is not found ;-)
+       $value = NULL;
 
        // Is the variable there?
        if (isSessionVariableSet($var)) {
                // Then  get it secured!
-               $value = SQL_ESCAPE($GLOBALS['_SESSION'][$var]);
+               $value = sqlEscapeString($GLOBALS['_SESSION'][$var]);
        } // END - if
 
        // Return the value
@@ -105,29 +108,79 @@ function getSession ($var) {
        return $value;
 }
 
+// Get whole session array
+function getSessionArray () {
+       // Simply return it
+       return $GLOBALS['_SESSION'];
+}
+
 // Destroy user session
-function destroyMemberSession () {
+function destroyMemberSession ($destroy = FALSE) {
        // Reset userid
        initMemberId();
 
        // Remove all user data from session
-       return ((setSession('userid', '')) && (setSession('u_hash', '')));
+       if ($destroy === TRUE) {
+               // Destroy whole session
+               return destroySession();
+       } else {
+               return ((setSession('userid', '')) && (setSession('u_hash', '')));
+       }
 }
 
 // Destroys the admin session
-function destroyAdminSession ($destroy = true) {
+function destroyAdminSession ($destroy = FALSE) {
        // Kill maybe existing session variables including array elements
-       setAdminId('');
+       setAdminId(0);
        setAdminMd5('');
-       setAdminLast('');
+       setAdminLast(0);
+
+       // Set cache to FALSE
+       $GLOBALS['isAdmin'] = FALSE;
 
        // Destroy session if requested and return status
-       if ($destroy === true) {
-               return session_destroy();
+       if ($destroy === TRUE) {
+               return destroySession();
+       } // END - if
+
+       // All fine if the session shall not really be destroyed
+       return TRUE;
+}
+
+// Destroys session and resets some "caches"
+function destroySession () {
+       // Unset "cache"
+       unset($GLOBALS['isValidSession']);
+
+       // Destroy session
+       return session_destroy();
+}
+
+// Checks whether the session is valid
+function isValidSession () {
+       // Is there cache?
+       if (!isset($GLOBALS[__FUNCTION__])) {
+               // Then determine it
+               $GLOBALS[__FUNCTION__] = ((isset($GLOBALS['valid_session'])) && ($GLOBALS['valid_session'] === TRUE) && (isset($_COOKIE[session_name()])));
        } // END - if
 
-       // All fine if we shall not really destroy the session
-       return true;
+       // Return cache
+       return $GLOBALS[__FUNCTION__];
+}
+
+// Checks whether all given session data is set
+function isSessionDataSet ($sessionData) {
+       // Default is set
+       $isset = TRUE;
+
+       // Check all
+       foreach ($sessionData as $key) {
+               // Is this element set?
+               $isset = (($isset) && (isSessionVariableSet($key)));
+       } // END - foreach
+
+       // Return result
+       return $isset;
 }
 
 // [EOF]