Commit cdd120ed authored by Dries's avatar Dries

- Patch #77936 by moshe and rdouglass: pluggable session handling.

parent c29daaaa
......@@ -46,8 +46,9 @@ Drupal x.x.x, xxxx-xx-xx (development version)
- performance:
* improved session handling: reduces database overhead.
* improved access checking: reduces database overhead.
* made it possible to do memcached based session management.
* omit sidebars when serving a '404 - Page not found': saves CPU cycles and bandwidth.
* added an 'aggressive' caching policy.
* added an 'aggressive' caching policy.
- removed the archive module.
- upgrade system:
* created space for update branches.
......
......@@ -128,6 +128,30 @@ function module_rebuild_cache() {
return $files;
}
/**
* Parse Drupal meta file format.
* Uses ini parser provided by php's parse_ini_file().
* You should not use the meta files to store module specific settings:
* user <code>variable_get()</code> and <code>variable_set()</code> for those.
*
* Files should use the ini format to specify values:
* key = "value"
* key2 = value2
*
* @param $filename
* The file we are parsing. Accepts file with relative or absolute path.
* @return
* The meta data array.
*/
function module_parse_meta_file($filename) {
$metadata = array();
if (file_exists($filename)) {
$metadata = parse_ini_file($filename);
}
return $metadata;
}
/**
* Determine whether a given module exists.
*
......
......@@ -79,8 +79,43 @@ function sess_write($key, $value) {
return TRUE;
}
function sess_destroy($key) {
db_query("DELETE FROM {sessions} WHERE sid = '%s'", $key);
/**
* Called when an anonymous user becomes authenticated or vice-versa.
*/
function sess_regenerate() {
$old_session_id = session_id();
session_regenerate_id();
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.
* Can also be called directly, either with the PHP session ID or another identifier
* such as uid to end a specific user's session.
*
* @param string $uid
* the user id
*/
function sess_destroy($uid) {
db_query('DELETE FROM {sessions} WHERE uid = %d', $uid);
}
function sess_gc($lifetime) {
......
......@@ -63,13 +63,13 @@ function throttle_exit() {
$throttle = module_invoke('throttle', 'status');
if ($max_guests = variable_get('throttle_anonymous', 0)) {
$guests = db_result(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period));
$guests = sess_count(time() - $time_period, TRUE);
}
else {
$guests = 0;
}
if ($max_users = variable_get('throttle_user', 0)) {
$users = db_result(db_query('SELECT COUNT(DISTINCT(uid)) AS count FROM {sessions} WHERE timestamp >= %d AND uid != 0', time() - $time_period));
$users = sess_count(time() - $time_period, FALSE);
}
else {
$users = 0;
......
......@@ -148,7 +148,7 @@ function user_save($account, $array = array(), $category = 'account') {
// Delete a blocked user's sessions to kick them if they are online.
if (isset($array['status']) && $array['status'] == 0) {
db_query('DELETE FROM {sessions} WHERE uid = %d', $account->uid);
sess_destroy($account->uid);
}
// Refresh user object
......@@ -560,24 +560,25 @@ function user_block($op = 'list', $delta = 0, $edit = array()) {
case 3:
if (user_access('access content')) {
// Count users with activity in the past defined period.
$time_period = variable_get('user_block_seconds_online', 900);
$time_period = time() - variable_get('user_block_seconds_online', 900);
// Perform database queries to gather online user lists.
$guests = db_fetch_object(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period));
$users = db_query('SELECT uid, name, access FROM {users} WHERE access >= %d AND uid != 0 ORDER BY access DESC', time() - $time_period);
$total_users = db_num_rows($users);
$anonymous_count = sess_count($time_period);
$authenticated_count = sess_count($time_period, false);
$authenticated_users = db_query('SELECT uid, name, access FROM {users} WHERE access >= %d AND uid != 0 ORDER BY access DESC', time() - $time_period);
// Format the output with proper grammar.
if ($total_users == 1 && $guests->count == 1) {
$output = t('There is currently %members and %visitors online.', array('%members' => format_plural($total_users, '1 user', '@count users'), '%visitors' => format_plural($guests->count, '1 guest', '@count guests')));
if ($anonymous_count == 1 && $authenticated_count == 1) {
$output = t('There is currently %members and %visitors online.', array('%members' => format_plural($authenticated_count, '1 user', '@count users'), '%visitors' => format_plural($anonymous_count, '1 guest', '@count guests')));
}
else {
$output = t('There are currently %members and %visitors online.', array('%members' => format_plural($total_users, '1 user', '@count users'), '%visitors' => format_plural($guests->count, '1 guest', '@count guests')));
$output = t('There are currently %members and %visitors online.', array('%members' => format_plural($authenticated_count, '1 user', '@count users'), '%visitors' => format_plural($anonymous_count, '1 guest', '@count guests')));
}
// Display a list of currently online users.
$max_users = variable_get('user_block_max_list_count', 10);
if ($total_users && $max_users) {
if ($authenticated_count && $max_users) {
$items = array();
while ($max_users-- && $account = db_fetch_object($users)) {
......@@ -929,10 +930,7 @@ function user_login_submit($form_id, $form_values) {
user_module_invoke('login', $form_values, $user);
$old_session_id = session_id();
session_regenerate_id();
db_query("UPDATE {sessions} SET sid = '%s' WHERE sid = '%s'", session_id(), $old_session_id);
sess_regenerate();
}
}
......@@ -993,7 +991,7 @@ function user_logout() {
watchdog('user', t('Session closed for %name.', array('%name' => $user->name)));
// Destroy the current session:
session_destroy();
sess_destroy($user->uid);
module_invoke_all('user', 'logout', NULL, $user);
// We have to use $GLOBALS to unset a global variable:
......@@ -1433,8 +1431,8 @@ function user_confirm_delete($name, $uid) {
*/
function user_delete($edit, $uid) {
$account = user_load(array('uid' => $uid));
sess_destroy($uid);
db_query('DELETE FROM {users} WHERE uid = %d', $uid);
db_query('DELETE FROM {sessions} WHERE uid = %d', $uid);
db_query('DELETE FROM {users_roles} WHERE uid = %d', $uid);
db_query('DELETE FROM {authmap} WHERE uid = %d', $uid);
$array = array('%name' => $account->name, '%email' => '<'. $account->mail .'>');
......
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