Newer
Older
define('USERNAME_MAX_LENGTH', 60);
define('EMAIL_MAX_LENGTH', 64);
/**
* Invokes hook_user() in every module.
*
* We cannot use module_invoke() for this, because the arguments need to
function user_module_invoke($type, &$array, &$user, $category = NULL) {
if (function_exists($function)) {
$function($type, $array, $user, $category);
}

Dries Buytaert
committed
/**

Gábor Hojtsy
committed
* Implementation of hook_theme().

Dries Buytaert
committed
*/
function user_theme() {
return array(
'user_picture' => array(
'arguments' => array('account' => NULL),
'template' => 'user-picture',

Dries Buytaert
committed
),
'user_profile' => array(

Steven Wittens
committed
'arguments' => array('account' => NULL),
'template' => 'user-profile',
'file' => 'user.pages.inc',

Steven Wittens
committed
),
'user_profile_category' => array(
'arguments' => array('element' => NULL),
'template' => 'user-profile-category',
'file' => 'user.pages.inc',

Steven Wittens
committed
),
'user_profile_item' => array(
'arguments' => array('element' => NULL),
'template' => 'user-profile-item',
'file' => 'user.pages.inc',

Dries Buytaert
committed
),
'user_list' => array(
'arguments' => array('users' => NULL, 'title' => NULL),
),
'user_admin_perm' => array(
'arguments' => array('form' => NULL),
'file' => 'user.admin.inc',

Dries Buytaert
committed
),
'user_admin_new_role' => array(
'arguments' => array('form' => NULL),
'file' => 'user.admin.inc',

Dries Buytaert
committed
),
'user_admin_account' => array(
'arguments' => array('form' => NULL),
'file' => 'user.admin.inc',

Dries Buytaert
committed
),
'user_filter_form' => array(
'arguments' => array('form' => NULL),
'file' => 'user.admin.inc',

Dries Buytaert
committed
),
'user_filters' => array(
'arguments' => array('form' => NULL),
'file' => 'user.admin.inc',

Dries Buytaert
committed
),

Dries Buytaert
committed
'user_signature' => array(
'arguments' => array('signature' => NULL),
),

Dries Buytaert
committed
);
}
$result = db_query("SELECT uid FROM {authmap} WHERE authname = '%s'", $authname);
if ($user = db_fetch_array($result)) {

Dries Buytaert
committed
/**

Gábor Hojtsy
committed
* Perform standard Drupal login operations for a user object.
*
* The user object must already be authenticated. This function verifies
* that the user account is not blocked/denied and then performs the login,
* updates the login timestamp in the database, invokes hook_user('login'),

Gábor Hojtsy
committed
* and regenerates the session.
*
* @param $account
* An authenticated user object to be set as the currently logged
* in user.
* @param $edit
* The array of form values submitted by the user, if any.

Gábor Hojtsy
committed
* This array is passed to hook_user op login.
* @return boolean
* TRUE if the login succeeds, FALSE otherwise.
*/
function user_external_login($account, $edit = array()) {

Dries Buytaert
committed
$form = drupal_get_form('user_login');
$state['values'] = $edit;
if (empty($state['values']['name'])) {
$state['values']['name'] = $account->name;
}

Gábor Hojtsy
committed
// Check if user is blocked or denied by access rules.

Dries Buytaert
committed
user_login_name_validate($form, $state, (array)$account);
if (form_get_errors()) {

Gábor Hojtsy
committed
// Invalid login.

Dries Buytaert
committed
return FALSE;
}

Gábor Hojtsy
committed
// Valid login.

Dries Buytaert
committed
global $user;
$user = $account;

Gábor Hojtsy
committed
user_authenticate_finalize($state['values']);

Dries Buytaert
committed
return TRUE;
}
/**
* Fetch a user object.
*
* @param $array
* An associative array of attributes to search for in selecting the
* user, such as user name or e-mail address.

Gábor Hojtsy
committed
* A fully-loaded $user object upon successful user load or FALSE if user
* cannot be loaded.
// Dynamically compose a SQL query:
$query = array();
$params = array();

Dries Buytaert
committed
if (is_numeric($array)) {
$array = array('uid' => $array);
}

Steven Wittens
committed
elseif (!is_array($array)) {
return FALSE;
}

Dries Buytaert
committed
if ($key == 'uid' || $key == 'status') {
$query[] = "$key = %d";
$params[] = $value;

Dries Buytaert
committed
}
else if ($key == 'pass') {
$query[] = "pass = '%s'";
$params[] = md5($value);
}
$query[]= "LOWER($key) = LOWER('%s')";

Steven Wittens
committed
$params[] = $value;
$result = db_query('SELECT * FROM {users} u WHERE '. implode(' AND ', $query), $params);

Dries Buytaert
committed
if ($user = db_fetch_object($result)) {

Steven Wittens
committed
$user = drupal_unpack($user);
$user->roles = array();

Dries Buytaert
committed
if ($user->uid) {
$user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
}
else {
$user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
}

Steven Wittens
committed
$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;
}

Dries Buytaert
committed
user_module_invoke('load', $array, $user);

Steven Wittens
committed
}
else {
$user = FALSE;
* Save changes to a user account or add a new user.
*
* @param $account
* The $user object for the user to modify or add. If $user->uid is
* omitted, a new user will be added.
*
* @param $array

Gábor Hojtsy
committed
* (optional) An array of fields and values to save. For example,
* array('name' => 'My name'); Setting a field to NULL deletes it from
* the data column.
*
* @param $category
* (optional) The category for storing profile information in.
*
* @return
* A fully-loaded $user object upon successful save or FALSE if the save failed.
function user_save($account, $array = array(), $category = 'account') {
// Dynamically compose a SQL query:

Steven Wittens
committed
if (is_object($account) && $account->uid) {
user_module_invoke('update', $array, $account, $category);

Dries Buytaert
committed
$query = '';
$data = unserialize(db_result(db_query('SELECT data FROM {users} WHERE uid = %d', $account->uid)));

Gábor Hojtsy
committed
// Consider users edited by an administrator as logged in, if they haven't
// already, so anonymous users can view the profile (if allowed).
if (empty($array['access']) && empty($account->access) && user_access('administer users')) {
$array['access'] = time();
}
if ($key == 'pass' && !empty($value)) {
else if ((substr($key, 0, 4) !== 'auth') && ($key != 'pass')) {

Gábor Hojtsy
committed
// Save standard fields.

Dries Buytaert
committed
// Roles is a special case: it used below.
if ($value === NULL) {
unset($data[$key]);
}
else {
$data[$key] = $value;
}
$query .= "data = '%s' ";
$success = db_query("UPDATE {users} SET $query WHERE uid = %d", array_merge($v, array($account->uid)));
if (!$success) {
// The query failed - better to abort the save than risk further data loss.
return FALSE;
}

Gábor Hojtsy
committed
// Reload user roles if provided.

Dries Buytaert
committed
if (isset($array['roles']) && is_array($array['roles'])) {
db_query('DELETE FROM {users_roles} WHERE uid = %d', $account->uid);

Dries Buytaert
committed
foreach (array_keys($array['roles']) as $rid) {

Dries Buytaert
committed
if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $account->uid, $rid);
}
// Delete a blocked user's sessions to kick them if they are online.
if (isset($array['status']) && $array['status'] == 0) {

Dries Buytaert
committed
sess_destroy_uid($account->uid);

Dries Buytaert
committed
// If the password changed, delete all open sessions and recreate
// the current one.

Gábor Hojtsy
committed
if (!empty($array['pass'])) {

Dries Buytaert
committed
sess_destroy_uid($account->uid);

Gábor Hojtsy
committed
if ($account->uid == $GLOBALS['user']->uid) {
sess_regenerate();
}

Dries Buytaert
committed
}

Gábor Hojtsy
committed
// Refresh user object.

Gábor Hojtsy
committed
// Send emails after we have the new user object.
if (isset($array['status']) && $array['status'] != $account->status) {

Gábor Hojtsy
committed
// The user's status is changing; conditionally send notification email.

Gábor Hojtsy
committed
$op = $array['status'] == 1 ? 'status_activated' : 'status_blocked';
_user_mail_notify($op, $user);
}
user_module_invoke('after_update', $array, $user, $category);

Gábor Hojtsy
committed
// Allow 'created' to be set by the caller.
if (!isset($array['created'])) {
$array['created'] = time();
}

Gábor Hojtsy
committed
// Consider users created by an administrator as already logged in, so
// anonymous users can view the profile (if allowed).
if (empty($array['access']) && user_access('administer users')) {
$array['access'] = time();
}

Gábor Hojtsy
committed
// Note: we wait to save the data column to prevent module-handled
// fields from being saved there. We cannot invoke hook_user('insert') here
// because we don't have a fully initialized user object yet.

Gerhard Killesreiter
committed
case 'pass':
$fields[] = $key;
$values[] = md5($value);

Gábor Hojtsy
committed
case 'mode': case 'sort': case 'timezone':

Gerhard Killesreiter
committed
case 'threshold': case 'created': case 'access':
case 'login': case 'status':
$fields[] = $key;
$values[] = $value;
$s[] = "%d";
break;
default:
if (substr($key, 0, 4) !== 'auth' && in_array($key, $user_fields)) {
$fields[] = $key;
$values[] = $value;
$s[] = "'%s'";
}
break;
$success = db_query('INSERT INTO {users} ('. implode(', ', $fields) .') VALUES ('. implode(', ', $s) .')', $values);
if (!$success) {

Gábor Hojtsy
committed
// On a failed INSERT some other existing user's uid may be returned.
// We must abort to avoid overwriting their account.
return FALSE;
}

Gábor Hojtsy
committed
// Build the initial user object.
$array['uid'] = db_last_insert_id('users', 'uid');
$user = user_load(array('uid' => $array['uid']));
user_module_invoke('insert', $array, $user, $category);

Gábor Hojtsy
committed
// Build and save the serialized data field now.
$data = array();
foreach ($array as $key => $value) {
if ((substr($key, 0, 4) !== 'auth') && ($key != 'roles') && (!in_array($key, $user_fields)) && ($value !== NULL)) {
$data[$key] = $value;
}
}
db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", serialize($data), $user->uid);

Dries Buytaert
committed
// Save user roles (delete just to be safe).
if (isset($array['roles']) && is_array($array['roles'])) {
db_query('DELETE FROM {users_roles} WHERE uid = %d', $array['uid']);
foreach (array_keys($array['roles']) as $rid) {
if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $array['uid'], $rid);
}

Dries Buytaert
committed
}
}
// Build the finished user object.
$user = user_load(array('uid' => $array['uid']));

Gábor Hojtsy
committed
// Save distributed authentication mappings.
if (sizeof($authmaps) > 0) {
/**
* Verify the syntax of the given name.
*/

Gábor Hojtsy
committed
if (!strlen($name)) {
return t('You must enter a username.');
}
if (substr($name, 0, 1) == ' ') {
return t('The username cannot begin with a space.');
}
if (substr($name, -1) == ' ') {
return t('The username cannot end with a space.');
}
if (strpos($name, ' ') !== FALSE) {
return t('The username cannot contain multiple spaces in a row.');
}
if (preg_match('/[^\x{80}-\x{F7} a-z0-9@_.\'-]/i', $name)) {
return t('The username contains an illegal character.');
}
if (preg_match('/[\x{80}-\x{A0}'. // Non-printable ISO-8859-1 + NBSP
'\x{AD}'. // Soft-hyphen
'\x{2000}-\x{200F}'. // Various space characters
'\x{2028}-\x{202F}'. // Bidirectional text overrides
'\x{205F}-\x{206F}'. // Various text hinting characters
'\x{FEFF}'. // Byte order mark
'\x{FF01}-\x{FF60}'. // Full-width latin
'\x{FFF9}-\x{FFFD}'. // Replacement characters

Gábor Hojtsy
committed
'\x{0}-\x{1F}]/u', // NULL byte and control characters
$name)) {
return t('The username contains an illegal character.');
}

Gábor Hojtsy
committed
if (drupal_strlen($name) > USERNAME_MAX_LENGTH) {
return t('The username %name is too long: it must be %max characters or less.', array('%name' => $name, '%max' => USERNAME_MAX_LENGTH));
}
if (!$mail) return t('You must enter an e-mail address.');
return t('The e-mail address %mail is not valid.', array('%mail' => $mail));

Dries Buytaert
committed
function user_validate_picture(&$form, &$form_state) {

Dries Buytaert
committed
// If required, validate the uploaded picture.
$validators = array(
'file_validate_is_image' => array(),
'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
);
if ($file = file_save_upload('picture_upload', $validators)) {

Gábor Hojtsy
committed
// Remove the old picture.
if (isset($form_state['values']['_account']->picture) && file_exists($form_state['values']['_account']->picture)) {
file_delete($form_state['values']['_account']->picture);
}
// The image was saved using file_save_upload() and was added to the
// files table as a temporary file. We'll make a copy and let the garbage
// collector delete the original upload.

Dries Buytaert
committed
$info = image_get_info($file->filepath);
$destination = variable_get('user_picture_path', 'pictures') .'/picture-'. $form['#uid'] .'.'. $info['extension'];
if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {

Dries Buytaert
committed
$form_state['values']['picture'] = $file->filepath;

Dries Buytaert
committed
}
else {
form_set_error('picture_upload', t("Failed to upload the picture image; the %directory directory doesn't exist or is not writable.", array('%directory' => variable_get('user_picture_path', 'pictures'))));
/**
* Generate a random alphanumeric password.
*/
function user_password($length = 10) {
// This variable contains the list of allowable characters for the
// password. Note that the number 0 and the letter 'O' have been
// removed to avoid confusion between the two. The same is true

Gábor Hojtsy
committed
// of 'I', 1, and 'l'.
$allowable_characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
// Zero-based count of characters in the allowable list:
$len = strlen($allowable_characters) - 1;
// Declare the password as a blank string.
$pass = '';
// Loop the number of times specified by $length.
for ($i = 0; $i < $length; $i++) {
// Each iteration, pick a random character from the
// allowable string and append it to the password:
$pass .= $allowable_characters[mt_rand(0, $len)];
/**
* Determine whether the user has a given privilege.
*
* @param $string
* The permission, such as "administer nodes", being checked for.
* @param $account
* (optional) The account to check, if not given use currently logged in user.

Gábor Hojtsy
committed
* @param $reset
* (optional) Resets the user's permissions cache, which will result in a
* recalculation of the user's permissions. This is necessary to support
* dynamically added user roles.

Gábor Hojtsy
committed
* Boolean TRUE if the current user has the requested permission.
*
* All permission checks in Drupal should go through this function. This
* way, we guarantee consistent behavior, and ensure that the superuser
* can perform all actions.
*/

Gábor Hojtsy
committed
function user_access($string, $account = NULL, $reset = FALSE) {

Gábor Hojtsy
committed
if ($reset) {
$perm = array();

Gábor Hojtsy
committed
}
if (!isset($account)) {
$account = $user;
}

Dries Buytaert
committed
// User #1 has all privileges:

Dries Buytaert
committed
if ($account->uid == 1) {

Dries Buytaert
committed
return TRUE;
// To reduce the number of SQL queries, we cache the user's permissions
// in a static variable.

Dries Buytaert
committed
if (!isset($perm[$account->uid])) {

Dries Buytaert
committed
$result = db_query("SELECT p.perm FROM {role} r INNER JOIN {permission} p ON p.rid = r.rid WHERE r.rid IN (". db_placeholders($account->roles) .")", array_keys($account->roles));

Dries Buytaert
committed
$perms = array();

Dries Buytaert
committed
$perms += array_flip(explode(', ', $row->perm));

Dries Buytaert
committed
$perm[$account->uid] = $perms;

Dries Buytaert
committed

Dries Buytaert
committed
return isset($perm[$account->uid][$string]);

Dries Buytaert
committed
/**

Gábor Hojtsy
committed
* Checks for usernames blocked by user administration.

Dries Buytaert
committed
*

Gábor Hojtsy
committed
* @return boolean TRUE for blocked users, FALSE for active.

Dries Buytaert
committed
*/
function user_is_blocked($name) {
$deny = db_fetch_object(db_query("SELECT name FROM {users} WHERE status = 0 AND name = LOWER('%s')", $name));

Dries Buytaert
committed

Dries Buytaert
committed
return $deny;

Dries Buytaert
committed
}
$result = db_query('SELECT * FROM {users} WHERE uid = 1');

Dries Buytaert
committed
if ($field = db_fetch_array($result)) {
$fields = array_keys($field);

Gábor Hojtsy
committed
// Make sure we return the default fields at least.
$fields = array('uid', 'name', 'pass', 'mail', 'picture', 'mode', 'sort', 'threshold', 'theme', 'signature', 'signature_format', 'created', 'access', 'login', 'status', 'timezone', 'language', 'init', 'data');
/**
* Implementation of hook_perm().
*/
return array('administer permissions', 'administer users', 'access user profiles', 'change own username');
/**
* Implementation of hook_file_download().
*
* Ensure that user pictures (avatars) are always downloadable.
*/
if (strpos($file, variable_get('user_picture_path', 'pictures') .'/picture-') === 0) {

Dries Buytaert
committed
$info = image_get_info(file_create_path($file));
return array('Content-type: '. $info['mime_type']);
/**
* Implementation of hook_search().
*/
function user_search($op = 'search', $keys = NULL, $skip_access_check = FALSE) {
switch ($op) {
case 'name':
if ($skip_access_check || user_access('access user profiles')) {
return t('Users');
case 'search':
if (user_access('access user profiles')) {
$find = array();
// Replace wildcards with MySQL/PostgreSQL wildcards.
$keys = preg_replace('!\*+!', '%', $keys);

Gábor Hojtsy
committed
if (user_access('administer users')) {
// Administrators can also search in the otherwise private email field.
$result = pager_query("SELECT name, uid, mail FROM {users} WHERE LOWER(name) LIKE LOWER('%%%s%%') OR LOWER(mail) LIKE LOWER('%%%s%%')", 15, 0, NULL, $keys, $keys);
while ($account = db_fetch_object($result)) {

Dries Buytaert
committed
$find[] = array('title' => $account->name .' ('. $account->mail .')', 'link' => url('user/'. $account->uid, array('absolute' => TRUE)));

Gábor Hojtsy
committed
}
}
else {

Dries Buytaert
committed
$result = pager_query("SELECT name, uid FROM {users} WHERE LOWER(name) LIKE LOWER('%%%s%%')", 15, 0, NULL, $keys);

Gábor Hojtsy
committed
while ($account = db_fetch_object($result)) {

Dries Buytaert
committed
$find[] = array('title' => $account->name, 'link' => url('user/'. $account->uid, array('absolute' => TRUE)));

Gábor Hojtsy
committed
}
}
return $find;
/**
* Implementation of hook_elements().
*/
function user_elements() {
return array(
'user_profile_category' => array(),
'user_profile_item' => array(),
);
}
/**
* Implementation of hook_user().
*/

Steven Wittens
committed
function user_user($type, &$edit, &$account, $category = NULL) {

Steven Wittens
committed
$account->content['user_picture'] = array(
'#value' => theme('user_picture', $account),
'#weight' => -10,
);
if (!isset($account->content['summary'])) {
$account->content['summary'] = array();
}
$account->content['summary'] += array(
'#type' => 'user_profile_category',
'#attributes' => array('class' => 'user-member'),

Gábor Hojtsy
committed
'#weight' => 5,

Steven Wittens
committed
'#title' => t('History'),
);
$account->content['summary']['member_for'] = array(

Steven Wittens
committed
'#type' => 'user_profile_item',
'#title' => t('Member for'),
'#value' => format_interval(time() - $account->created),

Dries Buytaert
committed
);
if ($type == 'form' && $category == 'account') {

Dries Buytaert
committed
$form_state = array();

Gábor Hojtsy
committed
return user_edit_form($form_state, (isset($account->uid) ? $account->uid : FALSE), $edit);
}
if ($type == 'validate' && $category == 'account') {

Gábor Hojtsy
committed
return _user_edit_validate((isset($account->uid) ? $account->uid : FALSE), $edit);
if ($type == 'submit' && $category == 'account') {

Gábor Hojtsy
committed
return _user_edit_submit((isset($account->uid) ? $account->uid : FALSE), $edit);
}
if ($type == 'categories') {
return array(array('name' => 'account', 'title' => t('Account settings'), 'weight' => 1));

Dries Buytaert
committed
function user_login_block() {
$form = array(
'#action' => url($_GET['q'], array('query' => drupal_get_destination())),

Dries Buytaert
committed
'#id' => 'user-login-form',

Dries Buytaert
committed
'#validate' => user_login_default_validators(),

Dries Buytaert
committed
'#submit' => array('user_login_submit'),

Dries Buytaert
committed
);
$form['name'] = array('#type' => 'textfield',
'#title' => t('Username'),
'#maxlength' => USERNAME_MAX_LENGTH,

Dries Buytaert
committed
'#size' => 15,
'#required' => TRUE,
);
$form['pass'] = array('#type' => 'password',
'#title' => t('Password'),
'#maxlength' => 60,

Dries Buytaert
committed
'#size' => 15,
'#required' => TRUE,
);
$form['submit'] = array('#type' => 'submit',
'#value' => t('Log in'),
);
$items = array();
if (variable_get('user_register', 1)) {

Gábor Hojtsy
committed
$items[] = l(t('Create new account'), 'user/register', array('attributes' => array('title' => t('Create a new user account.'))));

Dries Buytaert
committed
}

Gábor Hojtsy
committed
$items[] = l(t('Request new password'), 'user/password', array('attributes' => array('title' => t('Request new password via e-mail.'))));

Dries Buytaert
committed
$form['links'] = array('#value' => theme('item_list', $items));
return $form;
}
/**
* Implementation of hook_block().
*/
function user_block($op = 'list', $delta = 0, $edit = array()) {

Gábor Hojtsy
committed
$blocks[0]['info'] = t('User login');
// Not worth caching.
$blocks[0]['cache'] = BLOCK_NO_CACHE;

Gábor Hojtsy
committed
$blocks[1]['info'] = t('Navigation');
// Menu blocks can't be cached because each menu item can have
// a custom access callback. menu.inc manages its own caching.
$blocks[1]['cache'] = BLOCK_NO_CACHE;

Gábor Hojtsy
committed
$blocks[2]['info'] = t('Who\'s new');

Gábor Hojtsy
committed
// Too dynamic to cache.
$blocks[3]['info'] = t('Who\'s online');
$blocks[3]['cache'] = BLOCK_NO_CACHE;
return $blocks;

Neil Drumm
committed
else if ($op == 'configure' && $delta == 2) {
$form['user_block_whois_new_count'] = array(
'#type' => 'select',
'#title' => t('Number of users to display'),
'#default_value' => variable_get('user_block_whois_new_count', 5),
'#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
);
return $form;
}
else if ($op == 'configure' && $delta == 3) {
$period = drupal_map_assoc(array(30, 60, 120, 180, 300, 600, 900, 1800, 2700, 3600, 5400, 7200, 10800, 21600, 43200, 86400), 'format_interval');

Dries Buytaert
committed
$form['user_block_seconds_online'] = array('#type' => 'select', '#title' => t('User activity'), '#default_value' => variable_get('user_block_seconds_online', 900), '#options' => $period, '#description' => t('A user is considered online for this long after they have last viewed a page.'));
$form['user_block_max_list_count'] = array('#type' => 'select', '#title' => t('User list length'), '#default_value' => variable_get('user_block_max_list_count', 10), '#options' => drupal_map_assoc(array(0, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100)), '#description' => t('Maximum number of currently online users to display.'));
}

Neil Drumm
committed
else if ($op == 'save' && $delta == 2) {
variable_set('user_block_whois_new_count', $edit['user_block_whois_new_count']);
}
else if ($op == 'save' && $delta == 3) {
variable_set('user_block_seconds_online', $edit['user_block_seconds_online']);
variable_set('user_block_max_list_count', $edit['user_block_max_list_count']);
}
else if ($op == 'view') {
// For usability's sake, avoid showing two login forms on one page.
if (!$user->uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {
$block['subject'] = t('User login');

Dries Buytaert
committed
$block['content'] = drupal_get_form('user_login_block');

Dries Buytaert
committed
if ($menu = menu_tree()) {

Gábor Hojtsy
committed
$block['subject'] = $user->uid ? check_plain($user->name) : t('Navigation');
$block['content'] = $menu;

Dries Buytaert
committed
if (user_access('access content')) {
// Retrieve a list of new users who have subsequently accessed the site successfully.

Neil Drumm
committed
$result = db_query_range('SELECT uid, name FROM {users} WHERE status != 0 AND access != 0 ORDER BY created DESC', 0, variable_get('user_block_whois_new_count', 5));
while ($account = db_fetch_object($result)) {
$items[] = $account;
}
$output = theme('user_list', $items);
$block['subject'] = t('Who\'s new');
$block['content'] = $output;
}

Dries Buytaert
committed
if (user_access('access content')) {
// Count users active within the defined period.
$interval = time() - variable_get('user_block_seconds_online', 900);
// Perform database queries to gather online user lists. We use s.timestamp
// rather than u.access because it is much faster.
$anonymous_count = sess_count($interval);
$authenticated_users = db_query('SELECT DISTINCT u.uid, u.name, s.timestamp FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.timestamp >= %d AND s.uid > 0 ORDER BY s.timestamp DESC', $interval);

Dries Buytaert
committed
$authenticated_count = 0;
$max_users = variable_get('user_block_max_list_count', 10);
$items = array();
while ($account = db_fetch_object($authenticated_users)) {
if ($max_users > 0) {
$items[] = $account;
$max_users--;
}
$authenticated_count++;
}
// Format the output with proper grammar.
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')));
$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')));

Dries Buytaert
committed
// Display a list of currently online users.
$max_users = variable_get('user_block_max_list_count', 10);
if ($authenticated_count && $max_users) {

Gerhard Killesreiter
committed
$output .= theme('user_list', $items, t('Online users'));
}

Dries Buytaert
committed
$block['subject'] = t('Who\'s online');
$block['content'] = $output;
/**
* Process variables for user-picture.tpl.php.
*
* The $variables array contains the following arguments:
* - $account
*
* @see user-picture.tpl.php
*/
function template_preprocess_user_picture(&$variables) {
$variables['picture'] = '';
$account = $variables['account'];

Dries Buytaert
committed
if (!empty($account->picture) && file_exists($account->picture)) {
$picture = file_create_url($account->picture);
}
else if (variable_get('user_picture_default', '')) {
$picture = variable_get('user_picture_default', '');
}
if (isset($picture)) {
$alt = t("@user's picture", array('@user' => $account->name ? $account->name : variable_get('anonymous', t('Anonymous'))));
$variables['picture'] = theme('image', $picture, $alt, $alt, '', FALSE);

Gerhard Killesreiter
committed
if (!empty($account->uid) && user_access('access user profiles')) {
$attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE);
$variables['picture'] = l($variables['picture'], "user/$account->uid", $attributes);
/**
* Make a list of users.
*
* @param $users
* An array with user objects. Should contain at least the name and uid.
* @param $title
* (optional) Title to pass on to theme_item_list().
*
* @ingroup themeable
*/
function theme_user_list($users, $title = NULL) {
if (!empty($users)) {
foreach ($users as $user) {
$items[] = theme('username', $user);
}
return theme('item_list', $items, $title);

Dries Buytaert
committed
function user_is_anonymous() {

Dries Buytaert
committed
// Menu administrators can see items for anonymous when administering.
return !$GLOBALS['user']->uid || !empty($GLOBALS['menu_admin']);

Dries Buytaert
committed
}
function user_is_logged_in() {
return (bool)$GLOBALS['user']->uid;
}
function user_register_access() {
return user_is_anonymous() && variable_get('user_register', 1);

Dries Buytaert
committed
}
function user_view_access($account) {
return $account && $account->uid &&
(
// Always let users view their own profile.
($GLOBALS['user']->uid == $account->uid) ||
// Administrators can view all accounts.
user_access('administer users') ||
// The user is not blocked and logged in at least once.
($account->access && $account->status && user_access('access user profiles'))
);
}

Dries Buytaert
committed
function user_edit_access($account) {
return (($GLOBALS['user']->uid == $account->uid) || user_access('administer users')) && $account->uid > 0;

Dries Buytaert
committed
}
function user_load_self($arg) {
$arg[1] = user_load($GLOBALS['user']->uid);
return $arg;
}

Dries Buytaert
committed
function user_menu() {
$items['user/autocomplete'] = array(
'title' => 'User autocomplete',

Dries Buytaert
committed
'page callback' => 'user_autocomplete',
'access callback' => 'user_access',

Dries Buytaert
committed
'access arguments' => array('access user profiles'),
'type' => MENU_CALLBACK,
'file' => 'user.pages.inc',

Dries Buytaert
committed
);

Dries Buytaert
committed
// Registration and login pages.
$items['user'] = array(

Gábor Hojtsy
committed
'title' => 'User account',

Gábor Hojtsy
committed
'page callback' => 'user_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,

Gábor Hojtsy
committed
'file' => 'user.pages.inc',
);
$items['user/login'] = array(
'title' => 'Log in',
'access callback' => 'user_is_anonymous',

Dries Buytaert
committed
'type' => MENU_DEFAULT_LOCAL_TASK,
);

Dries Buytaert
committed
$items['user/register'] = array(
'title' => 'Create new account',

Dries Buytaert
committed
'page callback' => 'drupal_get_form',
'page arguments' => array('user_register'),
'access callback' => 'user_register_access',
'type' => MENU_LOCAL_TASK,
'file' => 'user.pages.inc',

Dries Buytaert
committed
);
$items['user/password'] = array(
'title' => 'Request new password',

Dries Buytaert
committed
'page callback' => 'drupal_get_form',
'page arguments' => array('user_pass'),
'access callback' => 'user_is_anonymous',

Dries Buytaert
committed
'type' => MENU_LOCAL_TASK,
'file' => 'user.pages.inc',

Dries Buytaert
committed
);
$items['user/reset/%/%/%'] = array(
'title' => 'Reset password',

Dries Buytaert
committed
'page callback' => 'drupal_get_form',
'page arguments' => array('user_pass_reset', 2, 3, 4),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'user.pages.inc',

Dries Buytaert
committed
);

Gábor Hojtsy
committed
// Admin user pages.

Dries Buytaert
committed
$items['admin/user'] = array(
'title' => 'User management',
'description' => "Manage your site's users, groups and access to site features.",

Dries Buytaert
committed
'position' => 'left',
'page callback' => 'system_admin_menu_block_page',

Dries Buytaert
committed
'access arguments' => array('access administration pages'),

Dries Buytaert
committed
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),

Dries Buytaert
committed
);
$items['admin/user/user'] = array(
'title' => 'Users',
'description' => 'List, add, and edit users.',

Dries Buytaert
committed
'page callback' => 'user_admin',
'page arguments' => array('list'),
'access arguments' => array('administer users'),
'file' => 'user.admin.inc',
);

Dries Buytaert
committed
$items['admin/user/user/list'] = array(
'title' => 'List',

Dries Buytaert
committed
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/user/user/create'] = array(
'title' => 'Add user',

Dries Buytaert
committed
'page arguments' => array('create'),

Dries Buytaert
committed
'type' => MENU_LOCAL_TASK,
'file' => 'user.admin.inc',

Dries Buytaert
committed
);
$items['admin/user/settings'] = array(
'title' => 'User settings',
'description' => 'Configure default behavior of users, including registration requirements, e-mails, and user pictures.',

Dries Buytaert
committed
'page callback' => 'drupal_get_form',
'page arguments' => array('user_admin_settings'),

Dries Buytaert
committed
'access arguments' => array('administer users'),
'file' => 'user.admin.inc',

Dries Buytaert
committed
);

Gábor Hojtsy
committed
// Admin access pages.
$items['admin/user/permissions'] = array(
'title' => 'Permissions',
'description' => 'Determine access to features by selecting permissions for roles.',

Dries Buytaert
committed
'page callback' => 'drupal_get_form',
'page arguments' => array('user_admin_perm'),
'access arguments' => array('administer permissions'),
'file' => 'user.admin.inc',

Dries Buytaert
committed
);
$items['admin/user/roles'] = array(
'title' => 'Roles',
'description' => 'List, edit, or add user roles.',

Dries Buytaert
committed
'page callback' => 'drupal_get_form',