Commit 3ace12ca authored by Dries's avatar Dries
Browse files

- Patch #node/76931 by Robert: improved performance of Drupal's session handling.

parent 9f8abbcc
......@@ -610,6 +610,20 @@ function drupal_is_denied($type, $mask) {
return $deny && !$allow;
}
/**
* Generates a default annonymous $user object.
*
* @return Object - the user object.
*/
function drupal_anonymous_user() {
$user = new stdClass();
$user->uid = 0;
$user->hostname = $_SERVER['REMOTE_ADDR'];
$user->roles = array();
$user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
return $user;
}
/**
* A string describing a phase of Drupal to load. Each phase adds to the
* previous one, so invoking a later phase automatically runs the earlier
......
......@@ -17,39 +17,44 @@ function sess_close() {
function sess_read($key) {
global $user;
// retrieve data for a $user object
$result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key);
if (!db_num_rows($result)) {
$result = db_query("SELECT u.* FROM {users} u WHERE u.uid = 0");
}
else {
$result = db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = '%s'", $key);
// 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 '';
}
// Build $user object:
$user = db_fetch_object($result);
$user = drupal_unpack($user);
// 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));
// Add roles element to $user:
$user->roles = array();
if ($user->uid) {
$user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
// We found the client's session record and they are an authenticated user
if ($user->uid > 0) {
// This is done to unserialize the data member of $user
$user = drupal_unpack($user);
// Add roles element to $user
$user->roles = array();
$user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
$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;
}
}
else {
$user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
// We didn't find the client's record (session has expired), or they are an anonymous user.
else {
$user = drupal_anonymous_user();
}
return !empty($user->session) ? $user->session : '';
return $user->session;
}
function sess_write($key, $value) {
global $user;
// 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;
}
$result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key);
if (!db_num_rows($result)) {
......@@ -87,5 +92,4 @@ function sess_gc($lifetime) {
db_query("DELETE FROM {sessions} WHERE timestamp < %d", time() - $lifetime);
return TRUE;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment