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

Diff of /contributions/modules/session_restore/session_restore.module

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

revision 1.2, Thu Aug 2 19:13:47 2007 UTC revision 1.3, Sat Oct 27 11:53:19 2007 UTC
# Line 1  Line 1 
1  <?php  <?php
2  /* $Id: session_restore.module,v 1.1 2007/07/11 08:17:19 karthik Exp $ */  /* $Id$ */
3    
4  /**  /**
5   * @file   * @file
6   *   Restore previously stored sessions upon login.   *   Restore previously stored sessions upon login.
  *  
  * @author  
  *   Karthik Kumar / Zen [ http://drupal.org/user/21209 ].  
7   */   */
8    
9  /**  /**
10   * Implementation of hook_user().   * Implementation of hook_user().
11   */   */
12  function session_restore_user($op, &$edit, &$account) {  function session_restore_user($op, &$edit, &$account) {
13    switch ($op) {    if ($account->uid) {
14      case 'login':      switch ($op) {
15        $_SESSION = _session_restore_session_get($account->uid);        case 'login':
16        break;          _session_restore_session_get($account->uid);
17      case 'delete':          break;
18        db_query("DELETE FROM {session_restore} WHERE uid = %d", $account->uid);        case 'delete':
19        break;          db_query("DELETE FROM {session_restore} WHERE uid = %d", $account->uid);
20            break;
21        }
22    }    }
   
   return;  
23  }  }
24    
25  /**  /**
26   * Implementation of hook_exit().   * Implementation of hook_exit().
27   */   */
28  function session_restore_exit() {  function session_restore_exit() {
29    global $user;    _session_restore_session_set();
   
   $session = _session_restore_vet($_SESSION);  
   
   if ($user->uid) {  
     // Perform the equivalent of a REPLACE.  
     if (db_result(db_query("SELECT count(uid) FROM {session_restore} WHERE uid = %d", $user->uid))) {  
       db_query("UPDATE {session_restore} SET session = '%s' WHERE uid = %d", $session, $user->uid);  
     }  
     else {  
       db_query("INSERT INTO {session_restore} VALUES (%d, '%s')", $user->uid, $session);  
     }  
   }  
30  }  }
31    
32  /**  /**
# Line 51  function session_restore_exit() { Line 36  function session_restore_exit() {
36   *   The session to vet.   *   The session to vet.
37   *   *
38   * @return   * @return
39   *   Serialized and cleaned-up session array ready for storage.   *   Cleaned-up session array ready for storage.
40   */   */
41  function _session_restore_vet($session) {  function _session_restore_vet($session) {
42    // Unset variables that might break this or other modules upon restore.    // Unset variables that might break this or other modules upon restore.
43    $variables = module_invoke_all('session_restore');    $variables = module_invoke_all('session_restore');
   // session_limit: Session limit module needs this variable unset during login  
   // for it to work.  
   $variables[] = 'session_limit';  
   // lastaccess: An autologout module variable.  
   $variables[] = 'lastaccess';  
44    
45    foreach ($variables as $variable) {    foreach ($variables as $variable) {
46      unset($session[$variable]);      unset($session[$variable]);
47    }    }
48    
49    return serialize($session);    return $session;
50  }  }
51    
52  /**  /**
# Line 74  function _session_restore_vet($session) Line 54  function _session_restore_vet($session)
54   *   *
55   * @param $uid   * @param $uid
56   *   The user ID of the user whose session is to be retrieved.   *   The user ID of the user whose session is to be retrieved.
  *  
  * @return  
  *   If available, the unserialized version of the stored session. If not, just  
  * returns the current $_SESSION.  
57   */   */
58  function _session_restore_session_get($uid) {  function _session_restore_session_get($uid) {
59    $session = db_result(db_query("SELECT session FROM {session_restore} WHERE uid = %d", $uid));    $session = db_result(db_query("SELECT session FROM {session_restore} WHERE uid = %d", $uid));
60    $session = $session ? unserialize($session) : $_SESSION;    // Overwriting $_SESSION does not work reliably in 5.1.6. Manually merge.
61      if ($session) {
62        $session = unserialize($session);
63        foreach ($session as $key => $value) {
64          $_SESSION[$key] = $value;
65        }
66      }
67    }
68    
69    // Unset variables that might break this or other modules upon restore.  /**
70    // See _session_restore_vet for more information.   * Store the current session if available.
71    $variables = module_invoke_all('session_restore');   */
72    $variables[] = 'session_limit';  function _session_restore_session_set() {
73    $variables[] = 'lastaccess';    global $user;
74    
75    // Ensure that special variables (usually set during hook_user are not    if ($user->uid) {
76    // overwritten.      db_query("DELETE FROM {session_restore} WHERE uid = %d", $user->uid);
77    foreach ($variables as $variable) {      $session = _session_restore_vet($_SESSION);
78      if (isset($_SESSION[$variable])) {      if (!empty($session)) {
79        $session[$variable] = $_SESSION[$variable];        // Serialize session and store.
80          db_query("INSERT INTO {session_restore} VALUES (%d, '%s')", $user->uid, serialize($session));
81      }      }
82    }    }
   
   return $session;  
83  }  }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

  ViewVC Help
Powered by ViewVC 1.1.2