/[drupal]/contributions/modules/session_restore/session_restore.module
ViewVC logotype

Contents of /contributions/modules/session_restore/session_restore.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Sat Oct 27 12:08:50 2007 UTC (2 years, 1 month ago) by karthik
Branch: MAIN
CVS Tags: DRUPAL-5--1-1-BETA, DRUPAL-6--1-0, DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
Changes since 1.3: +4 -1 lines
File MIME type: text/x-php
Leave author attribute in.
1 <?php
2 /* $Id: session_restore.module,v 1.3 2007/10/27 11:53:19 karthik Exp $ */
3
4 /**
5 * @file
6 * Restore previously stored sessions upon login.
7 *
8 * @author
9 * Karthik Kumar / Zen [ http://drupal.org/user/21209 ].
10 */
11
12 /**
13 * Implementation of hook_user().
14 */
15 function session_restore_user($op, &$edit, &$account) {
16 if ($account->uid) {
17 switch ($op) {
18 case 'login':
19 _session_restore_session_get($account->uid);
20 break;
21 case 'delete':
22 db_query("DELETE FROM {session_restore} WHERE uid = %d", $account->uid);
23 break;
24 }
25 }
26 }
27
28 /**
29 * Implementation of hook_exit().
30 */
31 function session_restore_exit() {
32 _session_restore_session_set();
33 }
34
35 /**
36 * Remove unnecessary variables and return a serialized session array.
37 *
38 * @param $session
39 * The session to vet.
40 *
41 * @return
42 * Cleaned-up session array ready for storage.
43 */
44 function _session_restore_vet($session) {
45 // Unset variables that might break this or other modules upon restore.
46 $variables = module_invoke_all('session_restore');
47
48 foreach ($variables as $variable) {
49 unset($session[$variable]);
50 }
51
52 return $session;
53 }
54
55 /**
56 * Retrieve stored session if available.
57 *
58 * @param $uid
59 * The user ID of the user whose session is to be retrieved.
60 */
61 function _session_restore_session_get($uid) {
62 $session = db_result(db_query("SELECT session FROM {session_restore} WHERE uid = %d", $uid));
63 // Overwriting $_SESSION does not work reliably in 5.1.6. Manually merge.
64 if ($session) {
65 $session = unserialize($session);
66 foreach ($session as $key => $value) {
67 $_SESSION[$key] = $value;
68 }
69 }
70 }
71
72 /**
73 * Store the current session if available.
74 */
75 function _session_restore_session_set() {
76 global $user;
77
78 if ($user->uid) {
79 db_query("DELETE FROM {session_restore} WHERE uid = %d", $user->uid);
80 $session = _session_restore_vet($_SESSION);
81 if (!empty($session)) {
82 // Serialize session and store.
83 db_query("INSERT INTO {session_restore} VALUES (%d, '%s')", $user->uid, serialize($session));
84 }
85 }
86 }

  ViewVC Help
Powered by ViewVC 1.1.2