1
2
3
4 """
5 User session handling functions.
6
7 @package includes
8 @see <a href='http://drupy.net'>Drupy Homepage</a>
9 @see <a href='http://drupal.org'>Drupal Homepage</a>
10 @note Drupy is a port of the Drupal project.
11 @note This file was ported from Drupal's includes/session.inc
12 @author Brendon Crawford
13 @copyright 2008 Brendon Crawford
14 @contact message144 at users dot sourceforge dot net
15 @created 2008-05-25
16 @version 0.1
17 @note License:
18
19 This program is free software; you can redistribute it and/or
20 modify it under the terms of the GNU General Public License
21 as published by the Free Software Foundation; either version 2
22 of the License, or (at your option) any later version.
23
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
28
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to:
31
32 The Free Software Foundation, Inc.,
33 51 Franklin Street, Fifth Floor,
34 Boston, MA 02110-1301,
35 USA
36 """
37
38 __version__ = "$Revision: 1 $"
39
40 from lib.drupy import DrupyPHP as php
41
42 -def open_(save_path, session_name):
44
47
86
87
88
90
91
92
93
94
95
96
97
98
99 if (not session_save_session() or \
100 (php.empty(php.COOKIE[php.session_name()]) and php.empty(value))):
101 return True
102 result = db_result(db_query("SELECT COUNT(*) FROM {sessions} " + \
103 "WHERE sid = '%s'", key))
104 lib_database.query(\
105 "UPDATE {sessions} SET " + \
106 "uid = %d, cache = %d, hostname = '%s', " + \
107 "session = '%s', timestamp = %d WHERE sid = '%s'", \
108 lib_appglobals.user.uid, (lib_appglobals.user.cache if \
109 php.isset(lib_appglobals.user.cache) else ''), \
110 ip_address(), value, php.time_(), key)
111 if (lib_database.affected_rows()):
112
113
114
115 if (lib_appglobals.user.uid and \
116 drupy_time() - lib_appglobals.user.access > \
117 variable_get('session_write_interval', 180)):
118 db_query("UPDATE {users} SET access = %d WHERE uid = %d", \
119 php.time_(), lib_appglobals.user.uid)
120 else:
121
122
123 lib_databae.query(\
124 "INSERT INTO {sessions} " + \
125 "(sid, uid, cache, hostname, session, timestamp) " + \
126 "VALUES ('%s', %d, %d, '%s', '%s', %d)", \
127 key, lib_appglobals.user.uid, (lib_appglobals.user.cache if \
128 php.isset(lib_appglobals.user.cache) else ''), \
129 ip_address(), value, php.time_())
130 return True
131
132
133
135 """
136 Called when an anonymous user becomes authenticated or vice-versa.
137 """
138 old_session_id = session_id()
139 session_regenerate_id()
140 db_query("UPDATE {sessions} SET sid = '%s' WHERE sid = '%s'", \
141 session_id(), old_session_id)
142
143
144 -def count(timestamp = 0, anonymous = True):
145 """
146 Counts how many users have sessions. Can count either anonymous
147 sessions, authenticated sessions, or both.
148
149 @param int timestamp
150 A Unix timestamp representing a point of time in the past.
151 The default is 0, which counts all existing sessions.
152 @param boolean anonymous
153 True counts only anonymous users.
154 False counts only authenticated users.
155 @return int
156 The number of users with sessions.
157 """
158 query = (' AND uid = 0' if anonymous else ' AND uid > 0')
159 return db_result(db_query('SELECT COUNT(sid) AS count FROM {sessions} ' + \
160 'WHERE timestamp >= %d' + query, timestamp))
161
162
164 """
165 Called by PHP session handling with the PHP session ID to
166 end a user's session.
167
168 @param string sid
169 the session id
170 """
171 db_query("DELETE FROM {sessions} WHERE sid = '%s'", sid)
172
173
174
176 """
177 End a specific user's session
178
179 @param string uid
180 the user id
181 """
182 db_query('DELETE FROM {sessions} WHERE uid = %d', uid)
183
184
185
186
188
189
190
191
192
193 db_query("DELETE FROM {sessions} WHERE timestamp < %d", time() - lifetime)
194 return True
195
196
197
199 """
200 Determine whether to save session data of the current request.
201
202 This function allows the caller to temporarily disable
203 writing of session data,
204 should the request end while performing potentially dangerous
205 operations, such as
206 manipulating the global user object.
207 See http://drupal.org/node/218104 for usage
208
209 @param status
210 Disables writing of session data when False,
211 (re-)enables writing when True.
212 @return
213 False if writing session data has been disabled. Otherwise, True.
214 """
215 php.static(session_save_session, 'save_session', True)
216 if status != None:
217 session_save_session.save_session = status
218 return session_save_session.save_session
219
220
221
222
223 name = php.session_name
224