- Patch #node/76931 by Robert: improved performance of Drupal's session handling.
parent
9f8abbcc59
commit
3ace12caf8
|
@ -610,6 +610,20 @@ function drupal_is_denied($type, $mask) {
|
||||||
return $deny && !$allow;
|
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
|
* 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
|
* previous one, so invoking a later phase automatically runs the earlier
|
||||||
|
|
|
@ -17,39 +17,44 @@ function sess_close() {
|
||||||
function sess_read($key) {
|
function sess_read($key) {
|
||||||
global $user;
|
global $user;
|
||||||
|
|
||||||
// retrieve data for a $user object
|
// Handle the case of first time visitors and clients that don't store cookies (eg. web crawlers).
|
||||||
$result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key);
|
if (!isset($_COOKIE[session_name()])) {
|
||||||
if (!db_num_rows($result)) {
|
$user = drupal_anonymous_user();
|
||||||
$result = db_query("SELECT u.* FROM {users} u WHERE u.uid = 0");
|
return '';
|
||||||
}
|
|
||||||
else {
|
|
||||||
$result = db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = '%s'", $key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build $user object:
|
// Otherwise, if the session is still active, we have a record of the client's session in the database.
|
||||||
$user = db_fetch_object($result);
|
$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));
|
||||||
$user = drupal_unpack($user);
|
|
||||||
|
|
||||||
// Add roles element to $user:
|
// We found the client's session record and they are an authenticated user
|
||||||
$user->roles = array();
|
if ($user->uid > 0) {
|
||||||
if ($user->uid) {
|
// 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';
|
$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);
|
$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)) {
|
while ($role = db_fetch_object($result)) {
|
||||||
$user->roles[$role->rid] = $role->name;
|
$user->roles[$role->rid] = $role->name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
// We didn't find the client's record (session has expired), or they are an anonymous user.
|
||||||
$user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
|
else {
|
||||||
|
$user = drupal_anonymous_user();
|
||||||
}
|
}
|
||||||
|
|
||||||
return !empty($user->session) ? $user->session : '';
|
return $user->session;
|
||||||
}
|
}
|
||||||
|
|
||||||
function sess_write($key, $value) {
|
function sess_write($key, $value) {
|
||||||
global $user;
|
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);
|
$result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key);
|
||||||
|
|
||||||
if (!db_num_rows($result)) {
|
if (!db_num_rows($result)) {
|
||||||
|
@ -88,4 +93,3 @@ function sess_gc($lifetime) {
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue