session.inc 2.94 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
  // retrieve data for a $user object
  $result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key);
22
  if (!db_num_rows($result)) {
Dries's avatar
   
Dries committed
23
    $result = db_query("SELECT u.* FROM {users} u WHERE u.uid = 0");
24
  }
25
26
27
  else {
    $result = db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = '%s'", $key);
  }
28

29
  // Build $user object:
Dries's avatar
 
Dries committed
30
  $user = db_fetch_object($result);
Dries's avatar
   
Dries committed
31
  $user = drupal_unpack($user);
Dries's avatar
   
Dries committed
32

33
34
  // Add roles element to $user:
  $user->roles = array();
35
36
  if ($user->uid) {
    $user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
37
38
39
40
41

    $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;
    }
42
43
44
45
  }
  else {
    $user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
  }
Dries's avatar
 
Dries committed
46
47
48
49
50
51
52

  return !empty($user->session) ? $user->session : '';
}

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

53
54
55
  $result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key);

  if (!db_num_rows($result)) {
56
57
58
59
60
61
    // Only save session data when when the browser sends a cookie.  This keeps 
    // crawlers out of session table. This improves speed up queries, reduces 
    // memory, and gives more useful statistics. We can't eliminate anonymous
    // session table rows without breaking throttle modulee and "Who's Online"
    // block.
    if ($user->uid || $value || count($_COOKIE)) {
62
63
64
65
66
67
68
69
70
71
      db_query("INSERT INTO {sessions} (sid, uid, cache, hostname, session, timestamp) VALUES ('%s', %d, %d, '%s', '%s', %d)", $key, $user->uid, $user->cache, $_SERVER["REMOTE_ADDR"], $value, time());
    }
  }
  else {
    db_query("UPDATE {sessions} SET uid = %d, cache = %d, hostname = '%s', session = '%s', timestamp = %d WHERE sid = '%s'", $user->uid, $user->cache, $_SERVER["REMOTE_ADDR"], $value, time(), $key);

    // TODO: this can be an expensive query. Perhaps only execute it every x minutes. Requires investigation into cache expiration.
    if ($user->uid) {
      db_query("UPDATE {users} SET access = %d WHERE uid = %d", time(), $user->uid);
    }
72
  }
73
74

  return TRUE;
Dries's avatar
 
Dries committed
75
76
77
}

function sess_destroy($key) {
78
  db_query("DELETE FROM {sessions} WHERE sid = '%s'", $key);
Dries's avatar
 
Dries committed
79
80
81
}

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

89
  return TRUE;
Dries's avatar
 
Dries committed
90
91
}