session.inc 5.46 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
Dries's avatar
   
Dries committed
2
// $Id$
Dries's avatar
 
Dries committed
3

Dries's avatar
   
Dries committed
4
5
6
7
8
/**
 * @file
 * User session handling functions.
 */

Dries's avatar
 
Dries committed
9
function sess_open($save_path, $session_name) {
10
  return TRUE;
Dries's avatar
 
Dries committed
11
12
13
}

function sess_close() {
14
  return TRUE;
Dries's avatar
 
Dries committed
15
16
17
18
}

function sess_read($key) {
  global $user;
19

20
21
22
23
24
  // Write and Close handlers are called after destructing objects since PHP 5.0.5
  // Thus destructors can use sessions but session handler can't use objects.
  // So we are moving session closure before destructing objects.
  register_shutdown_function('session_write_close');

25
26
27
28
  // Handle the case of first time visitors and clients that don't store cookies (eg. web crawlers).
  if (!isset($_COOKIE[session_name()])) {
    $user = drupal_anonymous_user();
    return '';
29
  }
30

31
32
  // Otherwise, if the session is still active, we have a record of the client's session in the database.
  $user = db_fetch_object(db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = '%s'", $key));
Dries's avatar
   
Dries committed
33

34
  // We found the client's session record and they are an authenticated user
35
  if ($user && $user->uid > 0) {
36
37
    // This is done to unserialize the data member of $user
    $user = drupal_unpack($user);
38

39
40
41
    // Add roles element to $user
    $user->roles = array();
    $user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
42
43
44
45
    $result = db_query("SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = %d", $user->uid);
    while ($role = db_fetch_object($result)) {
      $user->roles[$role->rid] = $role->name;
    }
46
  }
47
48
  // We didn't find the client's record (session has expired), or they are an anonymous user.
  else  {
49
50
    $session = isset($user->session) ? $user->session : '';
    $user = drupal_anonymous_user($session);
51
  }
Dries's avatar
 
Dries committed
52

53
  return $user->session;
Dries's avatar
 
Dries committed
54
55
56
57
58
}

function sess_write($key, $value) {
  global $user;

59
60
61
62
63
  // If the client doesn't have a session, and one isn't being created ($value), do nothing.
  if (empty($_COOKIE[session_name()]) && empty($value)) {
    return TRUE;
  }

64
65
66
  $result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key);

  if (!db_num_rows($result)) {
67
    // Only save session data when when the browser sends a cookie. This keeps
68
69
70
    // crawlers out of session table. This reduces memory and server load,
    // and gives more useful statistics. We can't eliminate anonymous session
    // table rows without breaking throttle module and "Who's Online" block.
71
    if ($user->uid || $value || count($_COOKIE)) {
72
      db_query("INSERT INTO {sessions} (sid, uid, cache, hostname, session, timestamp) VALUES ('%s', %d, %d, '%s', '%s', %d)", $key, $user->uid, isset($user->cache) ? $user->cache : '', ip_address(), $value, time());
73
74
75
    }
  }
  else {
76
    db_query("UPDATE {sessions} SET uid = %d, cache = %d, hostname = '%s', session = '%s', timestamp = %d WHERE sid = '%s'", $user->uid, isset($user->cache) ? $user->cache : '', ip_address(), $value, time(), $key);
77

78
79
80
    // Last access time is updated no more frequently than once every 180 seconds.
    // This reduces contention in the users table.
    if ($user->uid && time() - $user->access > variable_get('session_write_interval', 180)) {
81
82
      db_query("UPDATE {users} SET access = %d WHERE uid = %d", time(), $user->uid);
    }
83
  }
84
85

  return TRUE;
Dries's avatar
 
Dries committed
86
87
}

88
89
90
91
92
/**
 * Called when an anonymous user becomes authenticated or vice-versa.
 */
function sess_regenerate() {
  $old_session_id = session_id();
93
94
95
96
97
98
99
100
101
102

  // We code around http://bugs.php.net/bug.php?id=32802 by destroying
  // the session cookie by setting expiration in the past (a negative
  // value).  This issue only arises in PHP versions before 4.4.0,
  // regardless of the Drupal configuration.
  // TODO: remove this when we require at least PHP 4.4.0
  if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time() - 42000, '/');
  }

103
104
  session_regenerate_id();

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  db_query("UPDATE {sessions} SET sid = '%s' WHERE sid = '%s'", session_id(), $old_session_id);
}

/**
 * Counts how many users have sessions. Can count either anonymous sessions, authenticated sessions, or both.
 *
 * @param int $timestamp
 *   A Unix timestamp representing a point of time in the past.
 *   The default is 0, which counts all existing sessions.
 * @param int $anonymous
 *   TRUE counts only anonymous users.
 *   FALSE counts only authenticated users.
 *   Any other value will return the count of both authenticated and anonymous users.
 * @return  int
 *   The number of users with sessions.
 */
function sess_count($timestamp = 0, $anonymous = true) {
  $query = $anonymous ? ' AND uid = 0' : ' AND uid > 0';
  return db_result(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d'. $query, $timestamp));
}

/**
 * Called by PHP session handling with the PHP session ID to end a user's session.
128
129
130
131
132
133
134
135
136
137
 *
 * @param  string $sid
 *   the session id
 */
function sess_destroy_sid($sid) {
  db_query("DELETE FROM {sessions} WHERE sid = '%s'", $sid);
}

/**
 * End a specific user's session
138
139
140
141
 *
 * @param  string $uid
 *   the user id
 */
142
function sess_destroy_uid($uid) {
143
  db_query('DELETE FROM {sessions} WHERE uid = %d', $uid);
Dries's avatar
 
Dries committed
144
145
146
}

function sess_gc($lifetime) {
147
  // Be sure to adjust 'php_value session.gc_maxlifetime' to a large enough
148
  // value. For example, if you want user sessions to stay in your database
149
  // for three weeks before deleting them, you need to set gc_maxlifetime
150
  // to '1814400'. At that value, only after a user doesn't log in after
151
  // three weeks (1814400 seconds) will his/her session be removed.
Dries's avatar
 
Dries committed
152
153
  db_query("DELETE FROM {sessions} WHERE timestamp < %d", time() - $lifetime);

154
  return TRUE;
155
}