Fixes + asserts
[mailer.git] / inc / session-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 02/28/2009 *
4  * ===================                          Last change: 02/28/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : session-functions.php                            *
8  * -------------------------------------------------------------------- *
9  * Short description : Session-relevant functions                       *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Sitzungsrelevante Funktionen                     *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // Unset/set session variables
44 function setSession ($var, $value) {
45         // Abort in CSS mode here
46         if (isCssOutputMode()) {
47                 return TRUE;
48         } // END - if
49
50         // Trim value and session variable
51         $var   = trim(secureString($var));
52         $value = trim($value);
53
54         // Is the session variable set?
55         if (('' . $value . '' == '') && (isSessionVariableSet($var))) {
56                 // Remove the session
57                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'UNSET:' . $var . '=' . getSession($var));
58                 unset($GLOBALS['_SESSION'][$var]);
59                 if (isPhpVersionEqualNewer('5.3.0')) {
60                         // session_unregister() is deprecated as of 5.3.0
61                         return TRUE;
62                 } else {
63                         // PHP version < 5.3.0
64                         return session_unregister($var);
65                 }
66         } elseif (('' . $value . '' != '') && (!isSessionVariableSet($var))) {
67                 // Set session
68                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SET:' . $var . '=' . $value);
69                 $GLOBALS['_SESSION'][$var] = $value;
70                 if (isPhpVersionEqualNewer('5.3.0')) {
71                         // session_unregister() is deprecated as of 5.3.0
72                         return TRUE;
73                 } else {
74                         // PHP version < 5.3.0
75                         return session_register($var);
76                 }
77         } elseif (!empty($value)) {
78                 // Update session
79                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'UPDATE:' . $var . '=' . $value);
80                 $GLOBALS['_SESSION'][$var] = $value;
81                 return TRUE;
82         }
83
84         // Ignored (but valid)
85         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'IGNORED:' . $var . '=' . $value);
86         return TRUE;
87 }
88
89 // Check whether a session variable is set
90 function isSessionVariableSet ($var) {
91         // Warning: DO NOT call logDebugMessage() from here, this will cause an endless loop
92         return (isset($GLOBALS['_SESSION'][$var]));
93 }
94
95 // Returns whether the value of the session variable or NULL if not set
96 function getSession ($var) {
97         // Default is not found ;-)
98         $value = NULL;
99
100         // Is the variable there?
101         if (isSessionVariableSet($var)) {
102                 // Then  get it secured!
103                 $value = sqlEscapeString($GLOBALS['_SESSION'][$var]);
104         } // END - if
105
106         // Return the value
107         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $var . '=' . $value);
108         return $value;
109 }
110
111 // Get whole session array
112 function getSessionArray () {
113         // Simply return it
114         return $GLOBALS['_SESSION'];
115 }
116
117 // Destroy user session
118 function destroyMemberSession ($destroy = FALSE) {
119         // Reset userid
120         initMemberId();
121
122         // Remove all user data from session
123         if ($destroy === TRUE) {
124                 // Destroy whole session
125                 return destroySession();
126         } else {
127                 return ((setSession('userid', '')) && (setSession('u_hash', '')));
128         }
129 }
130
131 // Destroys the admin session
132 function destroyAdminSession ($destroy = FALSE) {
133         // Kill maybe existing session variables including array elements
134         setAdminId(0);
135         setAdminMd5('');
136         setAdminLast(0);
137
138         // Set cache to FALSE
139         $GLOBALS['isAdmin'] = FALSE;
140
141         // Destroy session if requested and return status
142         if ($destroy === TRUE) {
143                 return destroySession();
144         } // END - if
145
146         // All fine if the session shall not really be destroyed
147         return TRUE;
148 }
149
150 // Destroys session and resets some "caches"
151 function destroySession () {
152         // Unset "cache"
153         unset($GLOBALS['isValidSession']);
154
155         // Destroy session
156         return session_destroy();
157 }
158
159 // Checks whether the session is valid
160 function isValidSession () {
161         // Is there cache?
162         if (!isset($GLOBALS[__FUNCTION__])) {
163                 // Then determine it
164                 $GLOBALS[__FUNCTION__] = ((isset($GLOBALS['valid_session'])) && ($GLOBALS['valid_session'] === TRUE) && (isset($_COOKIE[session_name()])));
165         } // END - if
166
167         // Return cache
168         return $GLOBALS[__FUNCTION__];
169 }
170
171 // Checks whether all given session data is set
172 function isSessionDataSet ($sessionData) {
173         // Default is set
174         $isset = TRUE;
175
176         // Check all
177         foreach ($sessionData as $key) {
178                 // Is this element set?
179                 $isset = (($isset) && (isSessionVariableSet($key)));
180         } // END - foreach
181
182         // Return result
183         return $isset;
184 }
185
186 // [EOF]
187 ?>