session.inc 5.75 KB
Newer Older
1
<?php
2
// $Id$
3

4 5 6 7 8
/**
 * @file
 * User session handling functions.
 */

9
function sess_open($save_path, $session_name) {
10
  return TRUE;
11 12 13
}

function sess_close() {
14
  return TRUE;
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));
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
  // We didn't find the client's record (session has expired), or they are an anonymous user.
48
  else {
49 50
    $session = isset($user->session) ? $user->session : '';
    $user = drupal_anonymous_user($session);
51
  }
52

53
  return $user->session;
54 55 56 57 58
}

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

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

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

67
  if (!$result) {
68
    // Only save session data when when the browser sends a cookie. This keeps
69 70 71
    // 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.
72
    if ($user->uid || $value || count($_COOKIE)) {
73
      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());
74 75 76
    }
  }
  else {
77
    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);
78

79 80 81
    // 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)) {
82 83
      db_query("UPDATE {users} SET access = %d WHERE uid = %d", time(), $user->uid);
    }
84
  }
85 86

  return TRUE;
87 88
}

89 90 91 92 93
/**
 * Called when an anonymous user becomes authenticated or vice-versa.
 */
function sess_regenerate() {
  $old_session_id = session_id();
94
  session_regenerate_id();
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  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.
118 119 120 121 122 123 124 125 126 127
 *
 * @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
128 129 130 131
 *
 * @param  string $uid
 *   the user id
 */
132
function sess_destroy_uid($uid) {
133
  db_query('DELETE FROM {sessions} WHERE uid = %d', $uid);
134 135 136
}

function sess_gc($lifetime) {
137
  // Be sure to adjust 'php_value session.gc_maxlifetime' to a large enough
138
  // value. For example, if you want user sessions to stay in your database
139
  // for three weeks before deleting them, you need to set gc_maxlifetime
140
  // to '1814400'. At that value, only after a user doesn't log in after
141
  // three weeks (1814400 seconds) will his/her session be removed.
142 143
  db_query("DELETE FROM {sessions} WHERE timestamp < %d", time() - $lifetime);

144
  return TRUE;
145
}
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

/**
 * Determine whether to save session data of the current request.
 *
 * This function allows the caller to temporarily disable writing of session data,
 * should the request end while performing potentially dangerous operations, such as
 * manipulating the global $user object.
 *
 * @param $status
 *   Disables writing of session data when FALSE, (re-)enables writing when TRUE.
 * @return
 *   FALSE if writing session data has been disabled. Otherwise, TRUE.
 */
function session_save_session($status = NULL) {
  static $save_session = TRUE;
  if (isset($status)) {
    $save_session = $status;
  }
  return ($save_session);
}