1888 lines
82 KiB
Plaintext
1888 lines
82 KiB
Plaintext
<?php
|
|
// $Id$
|
|
|
|
session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc");
|
|
session_start();
|
|
|
|
function user_system($field){
|
|
$system["description"] = t("Enables the user registration and login system.");
|
|
return $system[$field];
|
|
}
|
|
|
|
/*** Session functions *****************************************************/
|
|
|
|
function sess_open($save_path, $session_name) {
|
|
return 1;
|
|
}
|
|
|
|
function sess_close() {
|
|
return 1;
|
|
}
|
|
|
|
function sess_read($key) {
|
|
global $user;
|
|
$user = user_load(array("sid" => $key, "status" => 1));
|
|
|
|
return !empty($user->session) ? $user->session : '';
|
|
}
|
|
|
|
function sess_write($key, $value) {
|
|
global $HTTP_SERVER_VARS;
|
|
|
|
db_query("UPDATE users SET hostname = '%s', session = '%s', timestamp = '%s' WHERE sid = '$key'", $HTTP_SERVER_VARS["REMOTE_ADDR"], $value, time());
|
|
|
|
return '';
|
|
}
|
|
|
|
function sess_destroy($key) {
|
|
global $HTTP_SERVER_VARS;
|
|
|
|
db_query("UPDATE users SET hostname = '%s', timestamp = '%s', sid = '' WHERE sid = '$key'", $HTTP_SERVER_VARS["REMOTE_ADDR"], time());
|
|
}
|
|
|
|
function sess_gc($lifetime) {
|
|
return 1;
|
|
}
|
|
|
|
/*** Common functions ******************************************************/
|
|
|
|
function user_external_load($authname) {
|
|
$arr_uid = db_query("SELECT uid FROM authmap WHERE authname = '%s'", $authname);
|
|
|
|
if (db_fetch_object($arr_uid)) {
|
|
$uid = db_result($arr_uid);
|
|
return user_load(array("uid" => $uid));
|
|
}
|
|
else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
function user_load($array = array()) {
|
|
|
|
/*
|
|
** Dynamically compose a SQL query:
|
|
*/
|
|
|
|
$query = "";
|
|
|
|
foreach ($array as $key => $value) {
|
|
if ($key == "pass") {
|
|
$query .= "u.$key = '". md5($value) ."' AND ";
|
|
}
|
|
else {
|
|
$query .= "u.$key = '". check_query($value) ."' AND ";
|
|
}
|
|
}
|
|
$result = db_query("SELECT u.*, r.name AS role FROM users u LEFT JOIN role r ON u.rid = r.rid WHERE $query u.status < 3 LIMIT 1");
|
|
|
|
$user = db_fetch_object($result);
|
|
if ($user->data && $data = unserialize($user->data)) {
|
|
foreach ($data as $key => $value) {
|
|
if (!isset($user->$key)) {
|
|
$user->$key = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
|
|
function user_save($account, $array = array()) {
|
|
/*
|
|
** Dynamically compose a SQL query:
|
|
*/
|
|
|
|
$user_fields = user_fields();
|
|
if ($account->uid) {
|
|
$data = unserialize(db_result(db_query("SELECT data FROM users WHERE uid = '$account->uid'")));
|
|
foreach ($array as $key => $value) {
|
|
if ($key == "pass") {
|
|
$query .= "$key = '". md5($value) ."', ";
|
|
}
|
|
else if (substr($key, 0, 4) !== "auth") {
|
|
if (in_array($key, $user_fields)) {
|
|
$query .= "$key = '". check_query($value) ."', ";
|
|
}
|
|
else {
|
|
$data[$key] = $value;
|
|
}
|
|
}
|
|
}
|
|
$query .= "data = '". check_query(serialize($data)) ."', ";
|
|
|
|
db_query("UPDATE users SET $query timestamp = '%s' WHERE uid = '$account->uid'", time());
|
|
|
|
$user = user_load(array("uid" => $account->uid));
|
|
}
|
|
else {
|
|
$array["timestamp"] = time();
|
|
|
|
foreach ($array as $key => $value) {
|
|
if ($key == "pass") {
|
|
$fields[] = check_query($key);
|
|
$values[] = "'". md5($value) ."'";
|
|
}
|
|
else if (substr($key, 0, 4) !== "auth") {
|
|
if (in_array($key, $user_fields)) {
|
|
$fields[] = check_query($key);
|
|
$values[] = "'". check_query($value) ."'";
|
|
}
|
|
else {
|
|
$data[$key] = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
$fields[] = "data";
|
|
$values[] = "'". check_query(serialize($data)) ."'";
|
|
|
|
db_query("INSERT INTO users (". implode(", ", $fields) .") VALUES (". implode(", ", $values) .")");
|
|
|
|
$user = user_load(array("name" => $array["name"]));
|
|
}
|
|
|
|
foreach ($array as $key => $value) {
|
|
if (substr($key, 0, 4) == "auth") {
|
|
$authmaps[$key] = $value;
|
|
}
|
|
}
|
|
|
|
if ($authmaps) {
|
|
$result = user_set_authmaps($user, $authmaps);
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
|
|
function user_set($account, $key, $value) {
|
|
$account->data[$key] = $value;
|
|
return $account;
|
|
}
|
|
|
|
function user_get($account, $key) {
|
|
return $account->data[$key];
|
|
}
|
|
|
|
function user_validate_name($name) {
|
|
|
|
/*
|
|
** Verify the syntax of the given name:
|
|
*/
|
|
|
|
if (!$name) return t("You must enter a username.");
|
|
if (ereg("^ ", $name)) return t("The username cannot begin with a space.");
|
|
if (ereg(" \$", $name)) return t("The username cannot end with a space.");
|
|
if (ereg(" ", $name)) return t("The username cannot contain multiple spaces in a row.");
|
|
// if (ereg("[^a-zA-Z0-9@-@]", $name)) return t("The username contains an illegal character.");
|
|
if (ereg('@', $name) && !eregi('@([0-9a-z](-?[0-9a-z])*\.)+[a-z]{2}([zmuvtg]|fo|me)?$', $name)) return t("The username is not a valid authentication ID.");
|
|
if (!eregi('^[[:print:]]+', $name)) return t("The name contains an illegal character.");
|
|
if (strlen($name) > 56) return t("The username '$name' is too long: it must be less than 56 characters.");
|
|
}
|
|
|
|
function user_validate_mail($mail) {
|
|
|
|
/*
|
|
** Verify the syntax of the given e-mail address. Empty e-mail addresses
|
|
** allowed.
|
|
*/
|
|
|
|
if ($mail && !eregi("^[0-9a-z_\.-]+@(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2,})$", $mail)) {
|
|
return t("The e-mail address '%mail' is not valid.", array("%mail" => $mail));
|
|
}
|
|
}
|
|
|
|
function user_validate_authmap($account, $authname, $module) {
|
|
$result = db_query("SELECT COUNT(*) from authmap WHERE uid != '$account->uid' && authname = '%s'", $authname);
|
|
if (db_result($result) > 0) {
|
|
$name = module_invoke($module, "info", "name");
|
|
return t("The %u ID %s is already taken.", array("%u" => ucfirst($name), "%s" => "<i>$authname</i>"));
|
|
}
|
|
}
|
|
|
|
function user_password($min_length = 6) {
|
|
|
|
/*
|
|
** Generate a human-readable password:
|
|
*/
|
|
|
|
mt_srand((double)microtime() * 1000000);
|
|
$words = explode(",", variable_get("user_password", "foo,bar,guy,neo,tux,moo,sun,asm,dot,god,axe,geek,nerd,fish,hack,star,mice,warp,moon,hero,cola,girl,fish,java,perl,boss,dark,sith,jedi,drop,mojo"));
|
|
while (strlen($password) < $min_length) $password .= trim($words[mt_rand(0, count($words))]);
|
|
return $password;
|
|
}
|
|
|
|
function user_access($string) {
|
|
|
|
global $user;
|
|
static $perm;
|
|
|
|
/*
|
|
** To reduce the number of SQL queries, we cache the user's permissions
|
|
** in a static variable.
|
|
*/
|
|
|
|
if (!$perm) {
|
|
if ($user->uid) {
|
|
$perm = db_result(db_query("SELECT p.perm FROM role r, permission p WHERE r.rid = p.rid AND name = '$user->role'"), 0);
|
|
}
|
|
else {
|
|
$perm = db_result(db_query("SELECT p.perm FROM role r, permission p WHERE r.rid = p.rid AND name = 'anonymous user'"), 0);
|
|
}
|
|
}
|
|
|
|
if ($user->uid == 1) {
|
|
return 1;
|
|
}
|
|
else {
|
|
return strstr($perm, $string);
|
|
}
|
|
|
|
}
|
|
|
|
function user_mail($mail, $subject, $message, $header) {
|
|
if (variable_get("smtp_library", "") && file_exists(variable_get("smtp_library", ""))) {
|
|
include_once variable_get("smtp_library", "");
|
|
return user_mail_wrapper($mail, $subject, $message, $header);
|
|
}
|
|
else {
|
|
/*
|
|
** Note: if you are having problems with sending mail, or mails look wrong
|
|
** when they are recieved you may have to modify the str_replace to suit
|
|
** your systems.
|
|
** - \r\n will work under dos and windows.
|
|
** - \n will work for linux, unix and BSDs.
|
|
** - \r will work for macs.
|
|
*/
|
|
return mail($mail, $subject, str_replace("\r", "", $message), $header);
|
|
}
|
|
}
|
|
|
|
function user_deny($type, $mask) {
|
|
|
|
$allow = db_fetch_object(db_query("SELECT * FROM access WHERE status = '1' AND type = '%s' AND LOWER('%s') LIKE LOWER(mask)", $type, $mask));
|
|
|
|
$deny = db_fetch_object(db_query("SELECT * FROM access WHERE status = '0' AND type = '%s' AND LOWER('%s') LIKE LOWER(mask)", $type, $mask));
|
|
|
|
if ($deny && !$allow) {
|
|
return 1;
|
|
}
|
|
else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
function user_fields() {
|
|
static $fields;
|
|
|
|
if (!$fields) {
|
|
$result = db_query("SELECT * FROM users WHERE uid = 1");
|
|
if (db_num_rows($result)) {
|
|
$fields = array_keys(db_fetch_array($result));
|
|
}
|
|
}
|
|
|
|
// Make sure we return the default fields at least
|
|
return is_array($fields) ? $fields: array("uid", "name", "pass", "mail", "homepage", "mode", "sort", "threshold", "theme", "signature", "timestamp", "hostname", "status", "timezone", "rating", "language", "sid", "init", "session", "data", "rid");
|
|
}
|
|
|
|
/*** Module hooks **********************************************************/
|
|
|
|
function user_perm() {
|
|
return array("administer users");
|
|
}
|
|
|
|
function user_search($keys) {
|
|
global $PHP_SELF;
|
|
$result = db_query("SELECT * FROM users WHERE name LIKE '%$keys%' LIMIT 20");
|
|
while ($account = db_fetch_object($result)) {
|
|
$find[$i++] = array("title" => $account->name, "link" => (strstr($PHP_SELF, "admin.php") ? drupal_url(array("mod" => "user", "op" => "edit", "id" => $account->uid), "admin") : drupal_url(array("mod" => "user", "op" => "view", "id" => $account->uid), "module")), "user" => $account->name);
|
|
}
|
|
return $find;
|
|
}
|
|
|
|
function user_block($op = "list", $delta = 0) {
|
|
global $user, $edit;
|
|
if ($op == "list") {
|
|
$blocks[0]["info"] = t("Log in");
|
|
$blocks[1]["info"] = t("User information");
|
|
$blocks[2]["info"] = t("Who's new");
|
|
|
|
return $blocks;
|
|
}
|
|
else {
|
|
switch ($delta) {
|
|
case 0:
|
|
if (!$user->uid) {
|
|
$output = "<div align=\"center\">\n";
|
|
$output .= "<form action=\"". drupal_url(array("mod" => "user", "op" => "login"), "module") ."\" method=\"post\">\n";
|
|
// Save the referer. We record where the user came from such that we
|
|
// can redirect him after having completed the login form.
|
|
if (!$edit["destination"]) $edit["destination"] = request_uri();
|
|
$output .= "<input name=\"edit[destination]\" type=\"hidden\" value=\"" . $edit["destination"] . "\" />";
|
|
$output .= "<b>". t("Username") .":</b><br /><input name=\"edit[name]\" size=\"15\" /><br />\n";
|
|
$output .= "<b>". t("Password") .":</b><br /><input name=\"edit[pass]\" size=\"15\" type=\"password\" /><br />\n";
|
|
|
|
if (variable_get("user_remember", 0) == 0) {
|
|
$output .= "<input name=\"edit[remember_me]\" type=\"checkbox\" />". t("Remember me");
|
|
}
|
|
elseif (variable_get("user_remember", 1) == 1) {
|
|
$output .= form_hidden("remember_me", 1);
|
|
}
|
|
|
|
$output .= "<br />\n<input name=\"edit[op]\" type=\"submit\" value=\"". t("Log in") ."\" /><br />\n";
|
|
$output .= "</form></div>\n";
|
|
|
|
if (variable_get("user_register", 1)) {
|
|
$items[] = lm(t("Create new account"), array("mod" => "user", "op" => "register"), "", array("title" => t("Create a new user account.")));
|
|
}
|
|
$items[] = lm(t("Request new password"), array("mod" => "user", "op" => "password"), "", array("title" => t("Request new password via e-mail.")));
|
|
|
|
$output .= theme_invoke("theme_item_list", $items);
|
|
|
|
$block["subject"] = t("Log in");
|
|
$block["content"] = "<div style=\"{ width: 155; }\">$output</div>";
|
|
return $block;
|
|
}
|
|
break;
|
|
case 1:
|
|
if ($user->uid) {
|
|
$content[] = theme_invoke("theme_item_list", module_invoke_all("link", "menu.create"));
|
|
$content[] = theme_invoke("theme_item_list", module_invoke_all("link", "menu.view"));
|
|
$content[] = theme_invoke("theme_item_list", module_invoke_all("link", "menu.settings"));
|
|
$content[] = theme_invoke("theme_item_list", module_invoke_all("link", "menu.misc"));
|
|
|
|
$output = implode($content, "<br />");
|
|
|
|
$block["subject"] = $user->name;
|
|
$block["content"] = "<div style=\"{ width: 155; }\">$output</div>";
|
|
return $block;
|
|
}
|
|
break;
|
|
case 2:
|
|
|
|
$result = db_query("SELECT uid, name FROM users WHERE status != '0' ORDER BY uid DESC LIMIT 5");
|
|
while ($account = db_fetch_object($result)) {
|
|
$items[] = lm((strlen($account->name) > 15 ? substr($account->name, 0, 15) . '...' : $account->name), array("mod" =>user, "op" => "view", "id" => $account->uid));
|
|
}
|
|
|
|
$output = theme_invoke("theme_item_list", $items);
|
|
|
|
$block["subject"] = t("Who's new");
|
|
$block["content"] = $output;
|
|
return $block;
|
|
}
|
|
}
|
|
}
|
|
|
|
function user_link($type) {
|
|
if ($type == "page") {
|
|
$links[] = lm(t("user account"), array("mod" => "user"), "", array("title" => t("Create a user account, request a new password or edit your account settings.")));
|
|
}
|
|
|
|
if ($type == "menu.settings") {
|
|
$links[] = lm(t("edit account"), array("mod" => "user", "op" => "edit"), "", array("title" => t("View and edit your account information.")));
|
|
}
|
|
|
|
if ($type == "menu.misc") {
|
|
if (user_access("access administration pages")) {
|
|
$links[] = la(t("administer %a", array("%a" => variable_get("site_name", "drupal"))), array(), "", array("title" => t("Access administration pages.")));
|
|
}
|
|
|
|
$links[] = lm(t("logout"), array("mod" => "user", "op" => "logout"), "", array("title" => t("Logout.")));
|
|
}
|
|
|
|
if ($type == "admin" && user_access("administer users")) {
|
|
$help["user"] = "Drupal allows users to register, login, logout, maintain user profiles, etc. No participant can use his own name to post content until he signs up for a user account. There is several configuration pages that help administrators manage user accounts.";
|
|
$help["create"] = "If your site is completely private, and doesn't allow public registration, then you can add new users manually. This web page allows administrator to register a new users.";
|
|
$help["view"] = "This page allows you to review and edit any user's profile.";
|
|
$help["access"] = "Access rules enable administrators to filter out usernames and e-mail addresses which are not allowed in Drupal. An administrator creates a 'mask' against which each new registration is checked. Disallowed names and e-mail addresses are denied access to the site.";
|
|
$help["permission"] = "Each user role has certain things that its users are allowed to do, and some that are disallowed. For example, authenticated users may usually post a story but anonymous users may not. Each permission describes a fine-grained logical operation such as access administration pages or add and modify user accounts. You could say a permission represents access granted to a user to perform a set of operations.";
|
|
$help["role"] = "Roles allow you to fine tune the security and administration of drupal. A role defines a group of users which have certain privileges. Examples of roles include: anonymous user, authenticated user, moderator, administrator and so on. By default, Drupal comes with two user roles: <ul><li>Anonymous user: this role is used for users that don't have a user account or that are not authenticated.</li><li>Registered user: this role is assigned automatically to authenticated users. Most registered users will belong to this user role unless specified otherwise.</li></ul>";
|
|
$help["search"] = "On this page you can query any username. For example, one may search for 'br' and Drupal might return 'brian', 'brad', and 'brenda'.";
|
|
$help["setting"] = "Administrators may choose to restrict registration to their site. That restriction may be accomplished on this page. Also, the list of words which may be included in a system generated password is also listed on this page. Drupal generates passwords by joining small words from the password list until the new password is greater than 6 characters.";
|
|
|
|
menu_add("user management", "admin.php?mod=user", "User management", $help["user"]);
|
|
menu_add("create new account", "admin.php?mod=user&op=create", "Create a new user account.", $help["create"], "user management", 1);
|
|
menu_add("view user accounts", "admin.php?mod=user&op=account", "Display user account listings.", $help["view"], "user management", 2, 1);
|
|
menu_add("access rules", "admin.php?mod=user&op=access", "Configure access rules.", $help["access"], "user management", 3);
|
|
menu_add("e-mail rules", "admin.php?mod=user&op=access&type=mail", "Allow or deny certain e-mail addresses.", $help["access"], "access rules");
|
|
menu_add("username rules", "admin.php?mod=user&op=access&type=user", "Allow or deny certain usernames.", $help["access"], "access rules");
|
|
menu_add("user roles", "admin.php?mod=user&op=role", "Configure user roles.", $help["role"], "user management", 4);
|
|
menu_add("user permissions", "admin.php?mod=user&op=permission", "Configure user permissions.", $help["permission"], "user management", 5);
|
|
menu_add("search account", "admin.php?mod=user&op=search", "Search a user account.", $help["search"], "user management", 5);
|
|
menu_add("help", "admin.php?mod=user&op=help", "More information about user management.", NULL, "user management", 7);
|
|
|
|
menu_add("active users", "admin.php?mod=user&op=account&query=0", t("Active users."), $help["view"], "view user accounts", 1);
|
|
menu_add("new users", "admin.php?mod=user&op=account&query=1", t("New users."), $help["view"], "view user accounts", 2);
|
|
menu_add("blocked users", "admin.php?mod=user&op=account&query=2", t("Blocked users."), $help["view"], "view user accounts", 3);
|
|
|
|
$i = 3;
|
|
foreach (user_roles(1) as $key => $value) {
|
|
menu_add("users with role '$value'", "admin.php?mod=user&op=account&query=". $i++, NULL, $help["view"], "view user accounts", 4);
|
|
}
|
|
}
|
|
|
|
return $links ? $links : array();
|
|
}
|
|
|
|
/*** Authentication methods ************************************************/
|
|
|
|
function user_get_authname($account, $module) {
|
|
|
|
/*
|
|
** Called by authentication modules in order to edit/view their authmap information.
|
|
*/
|
|
|
|
$result = db_query("SELECT authname FROM authmap WHERE uid = '$account->uid' && module = '$module'");
|
|
return db_result($result);
|
|
}
|
|
|
|
|
|
function user_get_authmaps($authname = NULL) {
|
|
|
|
/*
|
|
** Accepts an user object, $account, or an DA name and returns an
|
|
** associtive array of modules and DA names. Called at external login.
|
|
*/
|
|
|
|
$result = db_query("SELECT authname, module FROM authmap WHERE authname = '%s'", $authname);
|
|
if (db_num_rows($result) > 0) {
|
|
while ($authmap = db_fetch_object($result)) {
|
|
$authmaps[$authmap->module] = $authmap->authname;
|
|
}
|
|
return $authmaps;
|
|
}
|
|
else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
function user_set_authmaps($account, $authmaps) {
|
|
foreach ($authmaps as $key => $value) {
|
|
$module = explode("_", $key, 2);
|
|
if ($value) {
|
|
$result = db_query("SELECT COUNT(*) from authmap WHERE uid = '$account->uid' && module = '$module[1]'");
|
|
if (db_result($result) == 0) {
|
|
$result = db_query("INSERT INTO authmap (authname, uid, module) VALUES ('%s', '%s', '%s')", $value, $account->uid, $module[1]);
|
|
}
|
|
else {
|
|
$result = db_query("UPDATE authmap SET authname = '%s' WHERE uid = '$account->uid' && module = '$module[1]'", $value);
|
|
}
|
|
}
|
|
else {
|
|
$result = db_query("DELETE FROM authmap WHERE uid = '$account->uid' && module = '$module[1]'");
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
function user_auth_help_links() {
|
|
$links = array();
|
|
foreach (module_list() as $module) {
|
|
if (module_hook($module, "auth_help")) {
|
|
$links[] = lm(module_invoke($module, "info", "name"), array("mod" => "user", "op" => "help"), $module);
|
|
}
|
|
}
|
|
return $links;
|
|
}
|
|
|
|
/*** User features *********************************************************/
|
|
|
|
function user_login($edit = array(), $msg = "") {
|
|
global $user, $referer;
|
|
|
|
/*
|
|
** If we are already logged on, go to the user page instead.
|
|
*/
|
|
|
|
if ($user->uid) {
|
|
drupal_goto(drupal_url(array("mod" => "user"), "module"));
|
|
}
|
|
|
|
if (user_deny("user", $edit["name"])) {
|
|
$error = t("The name '%s' has been denied access.", array("%s" => $edit["name"]));
|
|
}
|
|
else if ($edit["name"] && $edit["pass"]) {
|
|
|
|
/*
|
|
** Try to log in the user locally:
|
|
*/
|
|
|
|
if (!$user) {
|
|
$name = check_input($edit["name"]);
|
|
$pass = check_input($edit["pass"]);
|
|
$user = user_load(array("name" => $name, "pass" => $pass, "status" => 1));
|
|
}
|
|
|
|
/*
|
|
** Strip name and server from ID:
|
|
*/
|
|
|
|
if ($server = strrchr($edit["name"], "@")) {
|
|
$name = check_input(substr($edit["name"], 0, strlen($edit["name"]) - strlen($server)));
|
|
$server = check_input(substr($server, 1));
|
|
$pass = check_input($edit["pass"]);
|
|
}
|
|
|
|
/*
|
|
** When possible, determine corrosponding external auth source. Invoke source, and login user if successful:
|
|
*/
|
|
|
|
if (!$user && $server && $result = user_get_authmaps("$name@$server")) {
|
|
if (module_invoke(key($result), "auth", $name, $pass, $server)) {
|
|
$user = user_external_load("$name@$server");
|
|
watchdog("user", "external load: $name@$server, module: " . key($result));
|
|
}
|
|
else {
|
|
$error = t("Invalid password for %s.", array("%s" => "<i>$name@$server</i>"));
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Try each external authentication source in series. Register user if successful.
|
|
*/
|
|
|
|
else if (!$user && $server) {
|
|
foreach (module_list() as $module) {
|
|
if (module_hook($module, "auth")) {
|
|
if (module_invoke($module, "auth", $name, $pass, $server)) {
|
|
if (variable_get("user_register", 1) == 1 && !user_load(array("name" => "$name@$server"))) { //register this new user
|
|
watchdog("user", "new user: $name@$server ($module ID)");
|
|
$user = user_save("", array("name" => "$name@$server", "pass" => user_password(), "init" => "$name@$server", "status" => 1, "authname_$module" => "$name@$server", "rid" => _user_authenticated_id()));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($user->uid) {
|
|
watchdog("user", "session opened for '$user->name'");
|
|
|
|
/*
|
|
** Write session ID to database:
|
|
*/
|
|
|
|
user_save($user, array("sid" => session_id()));
|
|
|
|
/*
|
|
** If the user wants to be remembered, set the proper cookie such
|
|
** that the session won't expire.
|
|
*/
|
|
|
|
if ($edit["remember_me"]) {
|
|
setcookie(session_name(), session_id(), time() + 3600 * 24 * 365);
|
|
}
|
|
else {
|
|
setcookie(session_name(), session_id());
|
|
}
|
|
|
|
/*
|
|
** Redirect the user to the page he logged on from.
|
|
*/
|
|
|
|
drupal_goto($edit["destination"]);
|
|
}
|
|
else {
|
|
if (!$error) {
|
|
$error = t("Sorry. Unrecognized username or password.") ." ". lm(t("Have you forgotten your password?"), array("mod" => "user", "op" => "password"));
|
|
}
|
|
if ($server) {
|
|
watchdog("user", "failed login for '$name@$server': $error");
|
|
}
|
|
else {
|
|
watchdog("user", "failed login for '$name': $error");
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Display error message (if any):
|
|
*/
|
|
|
|
if ($error) {
|
|
$output .= theme_invoke("theme_error", check_output($error));
|
|
}
|
|
|
|
/*
|
|
** Save the referer. We record where the user came from such that we
|
|
** can redirect him after having completed the login form.
|
|
*/
|
|
|
|
if (!$edit["destination"]) {
|
|
$edit["destination"] = request_uri();
|
|
}
|
|
$output .= form_hidden("destination", $edit["destination"]);
|
|
|
|
/*
|
|
** Display login form:
|
|
*/
|
|
|
|
if ($msg) {
|
|
$output .= "<p>$msg</p>";
|
|
}
|
|
if (count(user_auth_help_links()) > 0) {
|
|
$output .= form_textfield(t("Username"), "name", $edit["name"], 20, 64, t("Enter your %s username, or an ID from one of our affiliates: %a.", array("%s" => variable_get("site_name", "local"), "%a" => implode(", ", user_auth_help_links()))));
|
|
}
|
|
else {
|
|
$output .= form_textfield(t("Username"), "name", $edit["name"], 20, 64, t("Enter your %s username.", array("%s" => variable_get("site_name", "local"))));
|
|
}
|
|
$output .= form_password(t("Password"), "pass", $pass, 20, 64, t("Enter the password that accompanies your username."));
|
|
$output .= form_checkbox(t("Remember me"), "remember_me", 1, 0, 0);
|
|
$output .= form_submit(t("Log in"));
|
|
$output .= "<p>» ". lm(t("Request new password"), array("mod" => "user", "op" => "password")). "<br />";
|
|
if (variable_get("user_register", 1)) {
|
|
$output .= "» ". lm(t("Create new account"), array("mod" => "user", "op" => "register"));
|
|
}
|
|
$output .= "</p>";
|
|
|
|
return form($output, "post", drupal_url(array ("mod" => "user"), "module"));
|
|
}
|
|
|
|
function _user_authenticated_id() {
|
|
return db_result(db_query("SELECT rid FROM role WHERE name = 'authenticated user'"));
|
|
}
|
|
|
|
function user_logout() {
|
|
global $user;
|
|
|
|
if ($user->uid) {
|
|
watchdog("user", "session closed for user '$user->name'");
|
|
|
|
/*
|
|
** Destroy the current session:
|
|
*/
|
|
|
|
session_destroy();
|
|
unset($user);
|
|
}
|
|
|
|
/*
|
|
** Redirect the user to his personal information page:
|
|
*/
|
|
|
|
drupal_goto("index.php");
|
|
|
|
}
|
|
|
|
function user_pass($edit = array()) {
|
|
|
|
if ($edit["name"]) {
|
|
$account = db_fetch_object(db_query("SELECT uid, name, mail FROM users WHERE name = '%s'", $edit["name"]));
|
|
if (!$account) $error = t("Sorry. The username <i>%s</i> is not recognized.", array("%s" => $edit["name"]));
|
|
}
|
|
else if ($edit["mail"]) {
|
|
$account = db_fetch_object(db_query("SELECT uid, name, mail FROM users WHERE mail = '%s'", $edit["mail"]));
|
|
if (!$account) $error = t("Sorry. The e-mail address <i>%s</i> is not recognized.", array("%s" => $edit["mail"]));
|
|
}
|
|
if ($account) {
|
|
|
|
$from = variable_get("site_mail", ini_get("sendmail_from"));
|
|
$pass = user_password();
|
|
|
|
/*
|
|
** Save new password:
|
|
*/
|
|
|
|
user_save($account, array("pass" => $pass));
|
|
|
|
/*
|
|
** Mail new password:
|
|
*/
|
|
|
|
$variables = array("%username" => $account->name, "%site" => variable_get("site_name", "drupal"), "%password" => $pass, "%uri" => path_uri(), "%uri_brief" => path_uri(1), "%mailto" => $account->mail, "%date" => format_date(time()));
|
|
$subject = strtr(variable_get("user_mail_pass_subject", t("Replacement login information for %username at %site")), $variables);
|
|
$body = strtr(variable_get("user_mail_pass_body", t("%username,\n\nHere is your new password for %site. You may now login to %uri". drupal_url(array("mod" => "user", "op" => "login"), "module") ." using the following username and password:\n\nusername: %username\npassword: %password\n\nAfter logging in, you may wish to change your password at %uri". drupal_url(array("mod" => "user", "op" => "edit"), "module") .".\n\nYour new %site membership also enables you to login to other Drupal powered websites (e.g. http://www.drop.org/) without registering. Just use the following Drupal ID and password:\n\nDrupal ID: %username@%uri_brief\npassword: %password\n\n\n-- %site team")), $variables);
|
|
$headers = "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from";
|
|
user_mail($account->mail, $subject, $body, $headers);
|
|
|
|
watchdog("user", "mail password: '". $account->name ."' <". $account->mail .">");
|
|
|
|
return t("Your password and further instructions have been sent to your e-mail address.");
|
|
}
|
|
else {
|
|
|
|
// Display error message if necessary.
|
|
if ($error) {
|
|
$output .= theme_invoke("theme_error", check_output($error));
|
|
}
|
|
|
|
/*
|
|
** Display form:
|
|
*/
|
|
|
|
$output .= "<p>". sprintf(t("Enter your username %sor%s your e-mail address."), "<b><i>", "</i></b>") ."</p>";
|
|
$output .= form_textfield(t("Username"), "name", $edit["name"], 30, 64);
|
|
$output .= form_textfield(t("E-mail address"), "mail", $edit["mail"], 30, 64);
|
|
$output .= form_submit(t("E-mail new password"));
|
|
$output .= "<p>» ". lm(t("Log in"), array("mod" =>user, "op" => "login")) ."<br />";
|
|
if (variable_get("user_register", 1)) {
|
|
$output .= "» ". lm(t("Create new account"), array("mod" => "user", "op" => "register"));
|
|
}
|
|
$output .= "</p>";
|
|
|
|
return form($output, "post", drupal_url(array ("mod" => "user"), "module"));
|
|
}
|
|
}
|
|
|
|
function user_register($edit = array()) {
|
|
global $user;
|
|
|
|
/*
|
|
** If we are already logged on, go to the user page instead.
|
|
*/
|
|
|
|
if ($user->uid) {
|
|
drupal_goto(drupal_url(array("mod" => "user", "op" => "edit"), "module"));
|
|
}
|
|
|
|
if ($edit["name"] && $edit["mail"]) {
|
|
if ($error = user_validate_name($edit["name"])) {
|
|
// do nothing
|
|
}
|
|
else if ($error = user_validate_mail($edit["mail"])) {
|
|
// do nothing
|
|
}
|
|
else if (user_deny("user", $edit["name"])) {
|
|
$error = t("The name '%s' has been denied access.", array("%s" => $edit["name"]));
|
|
}
|
|
else if (user_deny("mail", $edit["mail"])) {
|
|
$error = t("The e-mail address '%s' has been denied access.", array("%s" => $edit["mail"]));
|
|
}
|
|
else if (db_num_rows(db_query("SELECT name FROM users WHERE LOWER(name) = LOWER('%s')", $edit["name"])) > 0) {
|
|
$error = t("The name '%s' is already taken.", array("%s" => $edit["name"]));
|
|
}
|
|
else if (db_num_rows(db_query("SELECT mail FROM users WHERE LOWER(mail) = LOWER('%s')", $edit["mail"])) > 0) {
|
|
$error = t("The e-mail address '%s' is already taken.", array("%s" => $edit["mail"]));
|
|
}
|
|
else if (variable_get("user_register", 1) == 0) {
|
|
$error = t("Public registrations have been disabled by the site administrator.");
|
|
}
|
|
else {
|
|
foreach (module_list() as $module) {
|
|
if (module_hook($module, "user")) {
|
|
$result = module_invoke($module, "user", "register_validate", $edit, $user);
|
|
if (is_array($result)) {
|
|
$data = array_merge($data, $result);
|
|
}
|
|
elseif (is_string($result)) {
|
|
$error = $result;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (!$error) {
|
|
$success = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($success) {
|
|
|
|
$from = variable_get("site_mail", ini_get("sendmail_from"));
|
|
$pass = user_password();
|
|
|
|
// create new user account, noting whether administrator approval is required
|
|
user_role_init();
|
|
// TODO: is this necessary? Won't session_write replicate this?
|
|
unset($edit["session"]);
|
|
$account = user_save("", array_merge(array("name" => $edit["name"], "pass" => $pass, "init" => $edit["mail"], "mail" => $edit["mail"], "rid" => _user_authenticated_id(), "rating" => 0, "status" => (variable_get("user_register", 1) == 1 ? 1 : 0)), $data));
|
|
watchdog("user", "new user: '". $edit["name"] ."' <". $edit["mail"] .">");
|
|
|
|
$variables = array("%username" => $edit["name"], "%site" => variable_get("site_name", "drupal"), "%password" => $pass, "%uri" => path_uri(), "%uri_brief" => path_uri(1), "%mailto" => $edit["mail"], "%date" => format_date(time()));
|
|
|
|
//the first user may login immediately, and receives a customized welcome e-mail.
|
|
if ($account->uid == 1) {
|
|
user_mail($edit["mail"], t("drupal user account details for %s", array("%s" => $edit["name"])), strtr(t("%username,\n\nYou may now login to %uri using the following username and password:\n\n username: %username\n password: %password\n\nAfter logging in, you may wish to visit the following pages:\n\nAdministration: %uriadmin.php\nEdit user account: %uri". drupal_url(array("mod" => "user", "op" => "edit"), "module") ."\n\n--drupal"), $variables), "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
|
|
// This should not be t()'ed. No point as its only shown once in the sites lifetime, and it would be bad to store the password
|
|
$output .= "<p>Welcome to Drupal. You are user #1, which gives you full and immediate access. All future registrants will receive their passwords via e-mail, so please configure your e-mail settings using the Administration pages.</p><p> Your password is <b>$pass</b>. You may change your password on the next page.</p><p>Please login below.</p>";
|
|
$output .= form_hidden("name", $account->name);
|
|
$output .= form_hidden("pass", $pass);
|
|
$output .= form_submit(t("Log in"));
|
|
return form($output);
|
|
}
|
|
else {
|
|
if ($account->status) {
|
|
/*
|
|
** Create new user account, no administrator approval required:
|
|
*/
|
|
|
|
$subject = strtr(variable_get("user_mail_welcome_subject", t("User account details for %username at %site")), $variables);
|
|
$body = strtr(variable_get("user_mail_welcome_body", t("%username,\n\nThank you for registering at %site. You may now login to %uri". drupal_url(array("mod" => "user", "op" => "login"), "module") ." using the following username and password:\n\nusername: %username\npassword: %password\n\nAfter logging in, you may wish to change your password at %urimodule.php?mod=user&op=edit\n\nYour new %site membership also enables to you to login to other Drupal powered websites (e.g. http://www.drop.org/) without registering. Just use the following Drupal ID and password:\n\nDrupal ID: %username@%uri_brief\npassword: %password\n\n\n-- %site team")), $variables);
|
|
user_mail($edit["mail"], $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
|
|
return t("Your password and further instructions have been sent to your e-mail address.");
|
|
}
|
|
else {
|
|
/*
|
|
** Create new user account, administrator approval required:
|
|
*/
|
|
$subject = strtr(variable_get("user_mail_welcome_subject", t("User account details for %username at %site")), $variables);
|
|
$body = strtr(variable_get("user_mail_welcome_body", t("%username,\n\nThank you for registering at %site. Your account will have to be approved by the site administrator. You may now login to %uri". drupal_url(array("mod" => "user", "op" => "login"), "module") ." using the following username and password:\n\nusername: %username\npassword: %password\n\nAfter logging in, you may wish to change your password at %urimodule.php?mod=user&op=edit\n\nYour new %site membership also enables to you to login to other Drupal powered websites (e.g. http://www.drop.org/) without registering. Just use the following Drupal ID and password:\n\nDrupal ID: %username@%uri_brief\npassword: %password\n\n\n-- %site team")), $variables);
|
|
user_mail($edit["mail"], $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
|
|
user_mail(variable_get("site_mail", ini_get("sendmail_from")), $subject, t("%u has applied for an account.\n\n%uri", array("%u" => $account->name, "%uri" => path_uri() . drupal_url(array("mod" => "user", "op" => "edit", "id" => $account->uid), "admin"))), "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
|
|
return t("Your password and further instructions have been sent to your e-mail address.");
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
if ($error) {
|
|
$output .= theme_invoke("theme_error", check_output($error));
|
|
}
|
|
}
|
|
|
|
// display the registration form
|
|
$affiliates = user_auth_help_links();
|
|
if (count($affiliates) > 0) {
|
|
$affiliates = implode(", ", $affiliates);
|
|
$output .= "<p>" . t("Note: If you have an account with one of our affiliates (%s), you may ". lm("login now", array("mod" => "user", "op" => "login")) ." instead of registering.", array("%s" => $affiliates)) ."</p>";
|
|
}
|
|
$output .= form_textfield(t("Username"), "name", $edit["name"], 30, 64, t("Your full name or your preferred username: only letters, numbers and spaces are allowed."));
|
|
$output .= form_textfield(t("E-mail address"), "mail", $edit["mail"], 30, 64, t("A password and instructions will be sent to this e-mail address, so make sure it is accurate."));
|
|
foreach (module_list() as $module) {
|
|
if (module_hook($module, "user")) {
|
|
$output .= module_invoke($module, "user", "register_form", $edit, $user);
|
|
}
|
|
}
|
|
$output .= form_submit(t("Create new account"));
|
|
$output .= "<p>» ". lm(t("E-mail new password"), array("mod" => "user", "op" => "password")). "<br />";
|
|
$output .= "» " .lm(t("Log in"), array("mod" => "user", "op" => "login")). "</p>";
|
|
|
|
return form($output);
|
|
}
|
|
|
|
|
|
function user_delete() {
|
|
global $edit, $user;
|
|
|
|
if ($edit["confirm"]) {
|
|
watchdog(user,"$user->name deactivated her own account.");
|
|
db_query("UPDATE users SET mail = 'deleted', status='0' WHERE uid = '$user->uid'");
|
|
$output .= t("Your account has been deactivated.");
|
|
}
|
|
else {
|
|
$output .= form_item(t("Confirm Deletion"), t("You are about to deactivate your own user account. In addition, your e-mail address will be removed from the database."));
|
|
$output .= form_hidden("confirm", 1);
|
|
$output .= form_submit(t("Delete account"));
|
|
$output = form($output);
|
|
}
|
|
return $output;
|
|
}
|
|
|
|
function user_edit($edit = array()) {
|
|
global $user, $languages;
|
|
|
|
if ($user->uid) {
|
|
if ($edit["name"]) {
|
|
if ($error = user_validate_name($edit["name"])) {
|
|
// do nothing
|
|
}
|
|
else if ($error = user_validate_mail($edit["mail"])) {
|
|
// do nothing
|
|
}
|
|
else if (db_num_rows(db_query("SELECT uid FROM users WHERE uid != '$user->uid' AND LOWER(name) = LOWER('%s')", $edit["name"])) > 0) {
|
|
$error = t("The name '%s' is already taken.", array("%s" => $edit["name"]));
|
|
}
|
|
else if ($edit["mail"] && db_num_rows(db_query("SELECT uid FROM users WHERE uid != '$user->uid' AND LOWER(mail) = LOWER('%s')", $edit["mail"])) > 0) {
|
|
$error = t("The e-mail address '%s' is already taken.", array("%s" => $edit["mail"]));
|
|
}
|
|
else if ($user->uid) {
|
|
foreach (module_list() as $module) {
|
|
if (module_hook($module, "user")) {
|
|
$result = module_invoke($module, "user", "edit_validate", $edit, $user);
|
|
}
|
|
if (is_array($result)) {
|
|
$data = array_merge($data, $result);
|
|
}
|
|
elseif (is_string($result)) {
|
|
$error = $result;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/*
|
|
** If required, check that proposed passwords match. If so,
|
|
** add new password to $edit.
|
|
*/
|
|
|
|
if ($edit["pass1"]) {
|
|
if ($edit["pass1"] == $edit["pass2"]) {
|
|
$edit["pass"] = $edit["pass1"];
|
|
}
|
|
else {
|
|
$error = t("The specified passwords do not match.");
|
|
}
|
|
}
|
|
unset($edit["pass1"], $edit["pass2"]);
|
|
|
|
/*
|
|
** Validate input fields to make sure users don't submit
|
|
** invalid form data.
|
|
*/
|
|
|
|
if (!user_access("administer users")) {
|
|
if (array_intersect(array_keys($edit), array("rid", "init", "rating", "session"))) {
|
|
watchdog("warning", "detected malicious attempt to alter a protected database field");
|
|
}
|
|
|
|
$edit["rid"] = $user->rid;
|
|
$edit["init"] = $user->init;
|
|
$edit["rating"] = $user->rating;
|
|
$edit["session"] = $user->session;
|
|
}
|
|
|
|
if (!$error) {
|
|
/*
|
|
** Save user information:
|
|
*/
|
|
|
|
$user = user_save($user, array_merge($edit, $data));
|
|
|
|
$output .= t("Your user information changes have been saved.");
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($error) {
|
|
$output .= theme_invoke("theme_error", check_output($error));
|
|
}
|
|
|
|
if (!$edit) {
|
|
$edit = object2array($user);
|
|
}
|
|
|
|
$output .= form_textfield(t("Username"), "name", $edit["name"], 30, 55, t("Your full name or your preferred username: only letters, numbers and spaces are allowed."));
|
|
$output .= form_textfield(t("E-mail address"), "mail", $edit["mail"], 30, 55, t("Insert a valid e-mail address. All e-mails from the system will be sent to this address. The e-mail address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by e-mail."));
|
|
|
|
foreach (module_list() as $module) {
|
|
if (module_hook($module, "user")) {
|
|
$output .= module_invoke($module, "user", "edit_form", $edit, $user);
|
|
}
|
|
}
|
|
|
|
$options = "<option value=\"\"". (("" == $key) ? " selected=\"selected\"" : "") .">". t("Default theme") ."</option>\n";
|
|
foreach (theme_list() as $key => $value) {
|
|
$options .= "<option value=\"$key\"". (($edit["theme"] == $key) ? " selected=\"selected\"" : "") .">$key - $value->description</option>\n";
|
|
}
|
|
|
|
$output .= form_item(t("Theme"), "<select name=\"edit[theme]\">$options</select>", t("Selecting a different theme will change the look and feel of the site."));
|
|
for ($zone = -43200; $zone <= 46800; $zone += 3600) $zones[$zone] = date("l, F dS, Y - h:i A", time() - date("Z") + $zone) ." (GMT ". $zone / 3600 .")";
|
|
$output .= form_select(t("Time zone"), "timezone", $edit["timezone"], $zones, t("Select what time you currently have and your time zone settings will be set appropriate."));
|
|
$output .= form_select(t("Language"), "language", $edit["language"], $languages, t("Selecting a different language will change the language of the site."));
|
|
$output .= form_item(t("Password"), "<input type=\"password\" name=\"edit[pass1]\" size=\"12\" maxlength=\"24\" /> <input type=\"password\" name=\"edit[pass2]\" size=\"12\" maxlength=\"24\" />", t("Enter your new password twice if you want to change your current password or leave it blank if you are happy with your current password."));
|
|
$output .= form_submit(t("Save user information"));
|
|
|
|
$output = form($output, "post", 0, "enctype=\"multipart/form-data\"");
|
|
}
|
|
else {
|
|
$output = user_login();
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
function user_menu() {
|
|
$links[] = lm(t("view user information"), array("mod" => "user", "op" => "view"));
|
|
$links[] = lm(t("edit user information"), array("mod" => "user", "op" => "edit"));
|
|
$links[] = lm(t("delete account"), array("mod" => "user", "op" => "delete"));
|
|
|
|
return "<div align=\"center\">". implode(" · ", $links) ."</div>";
|
|
}
|
|
|
|
function user_view($uid = 0) {
|
|
global $theme, $user;
|
|
|
|
if (!$uid) {
|
|
$uid = $user->uid;
|
|
}
|
|
|
|
if ($user->uid && $user->uid == $uid) {
|
|
$output = form_item(t("Name"), check_output("$user->name ($user->init)"));
|
|
$output .= form_item(t("E-mail address"), check_output($user->mail));
|
|
|
|
foreach (module_list() as $module) {
|
|
if (module_hook($module, "user")) {
|
|
$output .= module_invoke($module, "user", "view_private", "", $user);
|
|
}
|
|
}
|
|
|
|
$theme->header();
|
|
$theme->box(t("User account"), user_menu());
|
|
$theme->box(t("View user information"), $output);
|
|
$theme->footer();
|
|
}
|
|
else if ($uid && $account = user_load(array("uid" => $uid, "status" => 1))) {
|
|
$output = form_item(t("Name"), check_output($account->name));
|
|
|
|
foreach (module_list() as $module) {
|
|
if (module_hook($module, "user")) {
|
|
$output .= module_invoke($module, "user", "view_public", "", $account);
|
|
}
|
|
}
|
|
|
|
$theme->header();
|
|
$theme->box(t("View user information"), $output);
|
|
$theme->footer();
|
|
}
|
|
else {
|
|
$theme->header();
|
|
$theme->box(t("Log in"), user_login());
|
|
if (variable_get("user_register", 1)) {
|
|
$theme->box(t("Create new user account"), user_register());
|
|
}
|
|
$theme->box(t("E-mail new password"), user_pass());
|
|
$theme->footer();
|
|
}
|
|
}
|
|
|
|
function user_page() {
|
|
global $edit, $op, $id, $theme;
|
|
|
|
switch ($op) {
|
|
case t("E-mail new password");
|
|
case "password":
|
|
$theme->header();
|
|
$theme->box(t("E-mail new password"), user_pass($edit));
|
|
$theme->footer();
|
|
break;
|
|
case t("Create new account"):
|
|
case "register":
|
|
$output = user_register($edit);
|
|
$theme->header();
|
|
if (variable_get("user_register", 1)) {
|
|
$theme->box(t("Create new account"), $output);
|
|
}
|
|
else {
|
|
print message_access();
|
|
}
|
|
$theme->footer();
|
|
break;
|
|
case t("Log in"):
|
|
case "login":
|
|
$output = user_login($edit);
|
|
$theme->header();
|
|
$theme->box(t("Log in"), $output);
|
|
$theme->footer();
|
|
break;
|
|
case t("Delete account"):
|
|
case t("delete");
|
|
$output = user_delete();
|
|
$theme->header();
|
|
$theme->box(t("User account"), user_menu());
|
|
$theme->box(t("Delete account"), $output);
|
|
$theme->footer();
|
|
break;
|
|
case t("Save user information"):
|
|
case "edit":
|
|
$output = user_edit($edit);
|
|
$theme = theme_init();
|
|
$theme->header();
|
|
$theme->box(t("User account"), user_menu());
|
|
$theme->box(t("Edit user information"), $output);
|
|
$theme->footer();
|
|
break;
|
|
case "view":
|
|
user_view($id);
|
|
break;
|
|
case t("Logout"):
|
|
case "logout":
|
|
print user_logout();
|
|
break;
|
|
case "help":
|
|
$theme->header();
|
|
$theme->box(t("Distributed authentication"), user_help_users_da());
|
|
$theme->footer();
|
|
break;
|
|
default:
|
|
print user_view();
|
|
}
|
|
|
|
}
|
|
|
|
/*** Administrative features ***********************************************/
|
|
|
|
function user_conf_options() {
|
|
$output .= form_select(t("Public registrations"), "user_register", variable_get("user_register", 1), array(t("Only site administrators can create new user accounts."), t("Visitors can create accounts and no administrator approval is required."), t("Visitors can create accounts but administrator approval is required.")));
|
|
$output .= form_textfield(t("Password words"), "user_password", variable_get("user_password", "foo,bar,guy,neo,tux,moo,sun,asm,dot,god,axe,geek,nerd,fish,hack,star,mice,warp,moon,hero,cola,girl,fish,java,perl,boss,dark,sith,jedi,drop,mojo"), 55, 256, t("A comma separated list of short words that can be concatenated to generate human-readable passwords."));
|
|
$output .= form_select(t("Remember authenticated users"), "user_remember", variable_get("user_remember", 0), array(t("Let the user decide whether he should be logged out when leaving the site."), t("Authenticated users are not logged out upon leaving the site."), t("Authenticated users are logged out upon leaving the site.")));
|
|
$output .= form_textfield(t("Subject of welcome e-mail"), "user_mail_welcome_subject", variable_get("user_mail_welcome_subject", "User account details for %username at %site"), 80, 180, t("Customize the subject of your welcome e-mail, which is sent to new members upon registering.") . " " . t("Available variables are:") . " " . "%username, %site, %password, %uri, %uri_brief, %mailto, %date");
|
|
$output .= form_textarea(t("Body of welcome e-mail"), "user_mail_welcome_body", variable_get("user_mail_welcome_body", t("%username,\n\nThank you for registering at %site. You may now login to %uri". drupal_url(array("mod" => "user", "op" => "login"), "module") ." using the following username and password:\n\nusername: %username\npassword: %password\n\nAfter logging in, you may wish to change your password at %uri". drupal_url(array("mod" => "user", "op" => "edit"), "module") ."\n\nYour new %site membership also enables to you to login to other Drupal powered websites (e.g. http://www.drop.org/) without registering. Just use the following Drupal ID and password:\n\nDrupal ID: %username@%uri_brief\npassword: %password\n\n\n-- %site team")), 70, 10, t("Customize the body of the welcome e-mail, which is sent to new members upon registering.") . " " . t("Available variables are:") . " " . "%username, %site, %password, %uri, %uri_brief, %mailto");
|
|
$output .= form_textfield(t("Subject of password recovery e-mail"), "user_mail_pass_subject", variable_get("user_mail_pass_subject", t("Replacement login information for %username at %site")), 80, 180, t("Customize the Subject of your forgotten password e-mail.") . " " . t("Available variables are:") . " " . "%username, %site, %password, %uri, %uri_brief, %mailto, %date");
|
|
$output .= form_textarea(t("Body of password recovery e-mail"), "user_mail_pass_body", variable_get("user_mail_pass_body", t("%username,\n\nHere is your new password for %site. You may now login to %uri". drupal_url(array("mod" => "user", "op" => "login"), "module") ." using the following username and password:\n\nusername: %username\npassword: %password\n\nAfter logging in, you may wish to change your password at %uri". drupal_url(array("mod" => "user", "op" => "edit"), "module") ."\n\nYour new %site membership also enables to you to login to other Drupal powered websites (e.g. http://www.drop.org/) without registering. Just use the following Drupal ID and password:\n\nDrupal ID: %username@%uri_brief\npassword: %password\n\n\n-- %site team")), 70, 10, t("Customize the body of the forgotten password e-mail.") . " " . t("Available variables are:") . " " . "%username, %site, %password, %uri, %uri_brief, %mailto");
|
|
|
|
return $output;
|
|
}
|
|
|
|
function user_admin_create($edit = array()) {
|
|
|
|
if ($edit["name"] || $edit["mail"]) {
|
|
if ($error = user_validate_name($edit["name"])) {
|
|
// do nothing
|
|
}
|
|
else if ($error = user_validate_mail($edit["mail"])) {
|
|
// do nothing
|
|
}
|
|
else if (db_num_rows(db_query("SELECT name FROM users WHERE LOWER(name) = LOWER('%s')", $edit["name"])) > 0) {
|
|
$error = t("The name '%s' is already taken.", array("%s" => $edit["name"]));
|
|
}
|
|
else if (db_num_rows(db_query("SELECT mail FROM users WHERE LOWER(mail) = LOWER('%s')", $edit["mail"])) > 0) {
|
|
$error = t("The e-mail address '%s' is already taken.", array("%s" => $edit["mail"]));
|
|
}
|
|
else {
|
|
$success = 1;
|
|
}
|
|
}
|
|
|
|
if ($success) {
|
|
|
|
watchdog("user", "new user: '". $edit["name"] ."' <". $edit["mail"] .">");
|
|
|
|
user_save("", array("name" => $edit["name"], "pass" => $edit["pass"], "init" => $edit["mail"], "mail" => $edit["mail"], "rid" => _user_authenticated_id(), "status" => 1));
|
|
|
|
return "Created a new user '". $edit["name"] ."'. No e-mail has been sent.";
|
|
}
|
|
else {
|
|
|
|
if ($error) {
|
|
$output .= theme_invoke("theme_error", check_output($error));
|
|
}
|
|
|
|
$output .= form_textfield(t("Username"), "name", $edit["name"], 30, 55, t("Provide the username of the new account."));
|
|
$output .= form_textfield(t("E-mail address"), "mail", $edit["mail"], 30, 55, t("Provide the e-mail address associated with the new account."));
|
|
$output .= form_textfield(t("Password"), "pass", $edit["pass"], 30, 55, t("Provide a password for the new account."));
|
|
$output .= form_submit(t("Create account"));
|
|
|
|
return form($output);
|
|
}
|
|
}
|
|
|
|
function user_admin_access($edit = array()) {
|
|
global $op, $id, $type;
|
|
|
|
if (empty($type)) {
|
|
return;
|
|
}
|
|
|
|
if ($type == "mail") {
|
|
$output .= "<h3>" . t("E-mail rules") . "</h3>";
|
|
}
|
|
|
|
if ($type == "user") {
|
|
$output .= "<h3>" . t("Username rules") . "</h3>";
|
|
}
|
|
|
|
if ($op == t("Add rule")) {
|
|
db_query("INSERT INTO access (mask, type, status) VALUES ('%s', '%s', '%s')", $edit["mask"], $type, $edit["status"]);
|
|
}
|
|
else if ($op == t("Check")) {
|
|
if (user_deny($type, $edit["test"])) {
|
|
$message = "<b>'". $edit["test"] ."' is not allowed.</b><p />";
|
|
}
|
|
else {
|
|
$message = "<b>'". $edit["test"] ."' is allowed.</b><p />";
|
|
}
|
|
}
|
|
else if ($id) {
|
|
db_query("DELETE FROM access WHERE aid = '%s'", $id);
|
|
}
|
|
|
|
$header = array(t("type"), t("mask"), t("operations"));
|
|
|
|
$result = db_query("SELECT * FROM access WHERE type = '%s' AND status = '1' ORDER BY mask", $type);
|
|
|
|
while ($rule = db_fetch_object($result)) {
|
|
$rows[] = array(t("Allow"), $rule->mask, array("data" => la(t("delete rule"), array("mod" => "user", "op" => "access", "type" => $type, "id" => $rule->aid)), "align" => "center"));
|
|
}
|
|
|
|
$result = db_query("SELECT * FROM access WHERE type = '%s' AND status = '0' ORDER BY mask", $type);
|
|
|
|
while ($rule = db_fetch_object($result)) {
|
|
$rows[] = array(t("Deny"), $rule->mask, la(t("delete rule"), array("mod" => "user", "op" => "access", "type" => $type, "id" => $rule->aid)));
|
|
}
|
|
|
|
$rows[] = array("<select name=\"edit[status]\"><option value=\"1\">". t("Allow") ."</option><option value=\"0\">". t("Deny") ."</option></select>", "<input size=\"32\" maxlength=\"64\" name=\"edit[mask]\" />", "<input type=\"submit\" name=\"op\" value=\"" . t("Add rule") . "\" />");
|
|
|
|
$output .= table($header, $rows);
|
|
|
|
$output .= "<p><small>%: " . t("Matches any number of characters, even zero characters") . ".<br />_: " . t("Matches exactly one character.") . "</small></p>";
|
|
|
|
if ($type != "user") {
|
|
$output .= "<h3>" . t("Check e-mail address") . "</h3>";
|
|
}
|
|
else {
|
|
$output .= "<h3>" . t("Check username") . "</h3>";
|
|
}
|
|
|
|
$output .= "$message<input size=\"32\" maxlength=\"64\" name=\"edit[test]\" value=\"". $edit["test"] ."\" /><input type=\"submit\" name=\"op\" value=\"" . t("Check") . "\" />";
|
|
|
|
return form($output);
|
|
}
|
|
|
|
function user_roles($membersonly = 0) {
|
|
$result = db_query("SELECT * FROM role ORDER BY name");
|
|
while ($role = db_fetch_object($result)) {
|
|
if (!$membersonly || ($membersonly && $role->name != "anonymous user")) {
|
|
$roles[$role->rid] = $role->name;
|
|
}
|
|
}
|
|
return $roles;
|
|
}
|
|
|
|
function user_admin_perm($edit = array()) {
|
|
global $tid;
|
|
|
|
if ($edit) {
|
|
|
|
/*
|
|
** Save permissions:
|
|
*/
|
|
|
|
$tid = check_input($edit["tid"]);
|
|
|
|
$result = db_query("SELECT * FROM role");
|
|
while ($role = db_fetch_object($result)) {
|
|
// delete, so if we clear every checkbox we reset that role;
|
|
// otherwise permissions are active and denied everywhere
|
|
db_query("DELETE FROM permission WHERE rid = '%s' AND tid = '%s'", $role->rid, $tid);
|
|
$perm = $edit[$role->rid] ? implode(", ", array_keys($edit[$role->rid])) : "";
|
|
if ($perm) {
|
|
db_query("INSERT INTO permission (rid, perm, tid) VALUES ('%s', '%s', %s'')", $role->rid, $perm, $tid);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Compile permission array:
|
|
*/
|
|
|
|
foreach (module_list() as $name) {
|
|
if (module_hook($name, "perm")) {
|
|
$perms = array_merge($perms, module_invoke($name, "perm"));
|
|
}
|
|
}
|
|
asort($perms);
|
|
|
|
/*
|
|
** Compile role array:
|
|
*/
|
|
|
|
$result = db_query("SELECT r.rid, p.perm FROM role r LEFT JOIN permission p ON r.rid = p.rid WHERE tid = '%s' ORDER BY name", $tid);
|
|
$roles = array();
|
|
while ($role = db_fetch_object($result)) {
|
|
$role_perms[$role->rid] = $role->perm;
|
|
}
|
|
|
|
$result = db_query("SELECT rid, name FROM role ORDER BY name");
|
|
$role_names = array();
|
|
while ($role = db_fetch_object($result)) {
|
|
$role_names[$role->rid] = $role->name;
|
|
}
|
|
|
|
/*
|
|
** Render roles / permission overview:
|
|
*/
|
|
|
|
$header = array_merge(array(" "), $role_names);
|
|
|
|
foreach ($perms as $perm) {
|
|
$row[] = t($perm);
|
|
foreach ($role_names as $rid => $name) {
|
|
$row[] = "<input type=\"checkbox\" name=\"edit[$rid][$perm]\"". (strstr($role_perms[$rid], $perm) ? " checked=\"checked\"" : "") ." />";
|
|
}
|
|
$rows[] = $row;
|
|
unset($row);
|
|
}
|
|
|
|
$output = table($header, $rows);
|
|
$output .= form_hidden("tid", $tid);
|
|
$output .= form_submit(t("Save permissions"));
|
|
|
|
return form($output);
|
|
}
|
|
|
|
function user_admin_role($edit = array()) {
|
|
global $op, $id;
|
|
|
|
if ($op == t("Save role")) {
|
|
db_query("UPDATE role SET name = '%s' WHERE rid = '%s'", $edit["name"], $id);
|
|
}
|
|
else if ($op == t("Delete role")) {
|
|
db_query("DELETE FROM role WHERE rid = '%s'", $id);
|
|
db_query("DELETE FROM permission WHERE rid = '%s'", $id);
|
|
}
|
|
else if ($op == t("Add role")) {
|
|
db_query("INSERT INTO role (name) VALUES ('%s')", $edit["name"]);
|
|
}
|
|
else if ($id) {
|
|
/*
|
|
** Display role form:
|
|
*/
|
|
|
|
$role = db_fetch_object(db_query("SELECT * FROM role WHERE rid = '%s'", $id));
|
|
|
|
$output .= form_textfield(t("Role name"), "name", $role->name, 32, 64, t("The name for this role. Example: 'moderator', 'editorial board', 'site architect'."));
|
|
$output .= form_submit(t("Save role"));
|
|
$output .= form_submit(t("Delete role"));
|
|
|
|
$output = form($output);
|
|
}
|
|
|
|
if (!$output) {
|
|
/*
|
|
** Render role overview:
|
|
*/
|
|
|
|
$result = db_query("SELECT * FROM role ORDER BY name");
|
|
|
|
$header = array(t("name"), t("operations"));
|
|
while ($role = db_fetch_object($result)) {
|
|
$rows[] = array($role->name, array("data" => la(t("edit role"), array("mod" => "user", "op" => "role", "id" => $role->rid)), "align" => "center"));
|
|
}
|
|
$rows[] = array("<input size=\"32\" maxlength=\"64\" name=\"edit[name]\" />", "<input type=\"submit\" name=\"op\" value=\"". t("Add role") ."\" />");
|
|
|
|
$output = table($header, $rows);
|
|
$output = form($output);
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
function user_admin_edit($edit = array()) {
|
|
global $op, $id;
|
|
|
|
if ($account = user_load(array("uid" => $id))) {
|
|
|
|
if ($op == t("Save account")) {
|
|
foreach (module_list() as $module) {
|
|
if (module_hook($module, "user")) {
|
|
$result = module_invoke($module, "user", "edit_validate", $edit, $account);
|
|
}
|
|
if (is_array($result)) {
|
|
$data = array_merge($data, $result);
|
|
}
|
|
elseif (is_string($result)) {
|
|
$error = $result;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// TODO: this display/edit/validate should be moved to a new profile.module implementing the _user hooks
|
|
if ($error = user_validate_name($edit["name"])) {
|
|
// do nothing
|
|
}
|
|
else if ($error = user_validate_mail($edit["mail"])) {
|
|
// do nothing
|
|
}
|
|
else if (db_num_rows(db_query("SELECT uid FROM users WHERE uid != '$account->uid' AND LOWER(name) = LOWER('%s')", $edit["name"])) > 0) {
|
|
$error = t("The name '%s' is already taken.", array("%s" => $edit["name"]));
|
|
}
|
|
else if ($edit["mail"] && db_num_rows(db_query("SELECT uid FROM users WHERE uid != '$account->uid' AND LOWER(mail) = LOWER('%s')", $edit["mail"])) > 0) {
|
|
$error = t("The e-mail address '%s' is already taken.", array("%s" => $edit["mail"]));
|
|
}
|
|
|
|
/*
|
|
** If required, check that proposed passwords match. If so,
|
|
** add new password to $edit.
|
|
*/
|
|
|
|
if ($edit["pass1"]) {
|
|
if ($edit["pass1"] == $edit["pass2"]) {
|
|
$edit["pass"] = $edit["pass1"];
|
|
}
|
|
else {
|
|
$error = t("The specified passwords do not match.");
|
|
}
|
|
}
|
|
|
|
unset($edit["pass1"], $edit["pass2"]);
|
|
if (!$error) {
|
|
$account = user_save($account, $edit);
|
|
$output .= status(t("your user information changes have been saved."));
|
|
}
|
|
else {
|
|
$output .= theme_invoke("theme_error", check_output($error));
|
|
}
|
|
}
|
|
else if ($op == t("Delete account")) {
|
|
if ($edit["status"] == 0) {
|
|
db_query("DELETE FROM users WHERE uid = '$account->uid'");
|
|
db_query("DELETE FROM authmap WHERE uid = '$account->uid'");
|
|
$output .= t("The account has been deleted.");
|
|
}
|
|
else {
|
|
$output .= t("Failed to delete account: the account has to be blocked first.");
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Display user form:
|
|
*/
|
|
|
|
$output .= form_item(t("User ID"), check_output($account->uid));
|
|
$output .= form_textfield(t("Username"), "name", $account->name, 30, 55, t("Your full name or your preferred username: only letters, numbers and spaces are allowed."));
|
|
$output .= form_textfield(t("E-mail address"), "mail", $account->mail, 30, 55, t("Insert a valid e-mail address. All e-mails from the system will be sent to this address. The e-mail address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by e-mail."));
|
|
|
|
foreach (module_list() as $module) {
|
|
if (module_hook($module, "user")) {
|
|
$output .= module_invoke($module, "user", "edit_form", $edit, $account);
|
|
}
|
|
}
|
|
|
|
$options = "<option value=\"\"". (("" == $key) ? " selected=\"selected\"" : "") .">". t("Default theme") ."</option>\n";
|
|
foreach (theme_list() as $key => $value) {
|
|
$options .= "<option value=\"$key\"". (($edit["theme"] == $key) ? " selected=\"selected\"" : "") .">$key - $value->description</option>\n";
|
|
}
|
|
$output .= form_item(t("Theme"), "<select name=\"edit[theme]\">$options</select>", t("Selecting a different theme will change the look and feel of the site."));
|
|
for ($zone = -43200; $zone <= 46800; $zone += 3600) $zones[$zone] = date("l, F dS, Y - h:i A", time() - date("Z") + $zone) ." (GMT ". $zone / 3600 .")";
|
|
$output .= form_select(t("Time zone"), "timezone", $account->timezone, $zones, t("Select what time you currently have and your time zone settings will be set appropriate."));
|
|
$output .= form_select(t("Language"), "language", $account->language, $languages, t("Selecting a different language will change the language of the site."));
|
|
$output .= form_item(t("Password"), "<input type=\"password\" name=\"edit[pass1]\" size=\"12\" maxlength=\"24\" /> <input type=\"password\" name=\"edit[pass2]\" size=\"12\" maxlength=\"24\" />", t("Enter a new password twice if you want to change the current password for this user or leave it blank if you are happy with the current password."));
|
|
$output .= form_select(t("Status"), "status", $account->status, array(t("Blocked"), t("Active")));
|
|
$output .= form_select(t("Role"), "rid", $account->rid, user_roles(1));
|
|
|
|
$output .= form_submit(t("Save account"));
|
|
$output .= form_submit(t("Delete account"));
|
|
|
|
$output = form($output);
|
|
|
|
}
|
|
else {
|
|
$output = t("No such user");
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
function user_admin_account() {
|
|
global $query;
|
|
|
|
$queries = array("ORDER BY timestamp DESC", "ORDER BY u.uid DESC", "WHERE status = 0 ORDER BY u.uid DESC");
|
|
foreach (user_roles(1) as $key => $value) {
|
|
$queries[] = "WHERE r.name = '$value' ORDER BY u.uid DESC";
|
|
}
|
|
|
|
$result = pager_query("SELECT u.uid, u.name, u.timestamp FROM users u LEFT JOIN role r ON u.rid = r.rid ". $queries[$query ? $query : 0], 50);
|
|
|
|
$header = array(t("username"), t("last access"), t("operations"));
|
|
while ($account = db_fetch_object($result)) {
|
|
$rows[] = array(format_name($account), format_date($account->timestamp, "small"), la(t("edit account"), array("mod" => "user", "op" => "edit", "id" => $account->uid)));
|
|
}
|
|
|
|
if ($pager = pager_display(NULL, 50, 0, "admin")) {
|
|
$rows[] = array(array("data" => $pager, "colspan" => 3));
|
|
}
|
|
|
|
return table($header, $rows);
|
|
}
|
|
|
|
function user_role_init() {
|
|
$role = db_fetch_object(db_query("SELECT * FROM role WHERE name = 'anonymous user'"));
|
|
if (!$role) {
|
|
db_query("INSERT INTO role (name) VALUES ('anonymous user')");
|
|
}
|
|
|
|
$role = db_fetch_object(db_query("SELECT * FROM role WHERE name = 'authenticated user'"));
|
|
if (!$role) {
|
|
db_query("INSERT INTO role (name) VALUES ('authenticated user')");
|
|
}
|
|
}
|
|
|
|
function user_admin() {
|
|
global $edit, $id, $op;
|
|
|
|
if (user_access("administer users")) {
|
|
/*
|
|
** Initialize all the roles and permissions:
|
|
*/
|
|
|
|
user_role_init();
|
|
|
|
switch ($op) {
|
|
case "help":
|
|
print user_help();
|
|
break;
|
|
case "search":
|
|
print search_type("user", drupal_url(array("mod" => "user", "op" => "search"), "admin"));
|
|
break;
|
|
case t("Add rule"):
|
|
case t("Check"):
|
|
case "access":
|
|
print user_admin_access($edit);
|
|
break;
|
|
case t("Save permissions"):
|
|
case "permission":
|
|
print user_admin_perm($edit);
|
|
break;
|
|
case t("Create account"):
|
|
case "create":
|
|
print user_admin_create($edit);
|
|
break;
|
|
case t("Add role"):
|
|
case t("Delete role"):
|
|
case t("Save role"):
|
|
case "role":
|
|
print user_admin_role($edit);
|
|
break;
|
|
case t("Delete account"):
|
|
case t("Save account"):
|
|
case "edit":
|
|
print user_admin_edit($edit);
|
|
break;
|
|
default:
|
|
print user_admin_account();
|
|
}
|
|
}
|
|
}
|
|
// this help is for end users
|
|
function user_help_users_da() {
|
|
$site = "<i>". variable_get("site_name", "this website"). "</i>";
|
|
|
|
$output = "
|
|
<h3>Distributed authentication<a name=\"da\"></a></h3>
|
|
<p>One of the more tedious moments in visiting a new website is filling out the
|
|
registration form. Here at %s, you do not have to fill out a registration form
|
|
if you are already a member of ";
|
|
|
|
$output .= implode(", ", user_auth_help_links());
|
|
$output .= ". This capability is called <i>Distributed
|
|
Authentication</i>, and is unique to <a href=\"http://www.drupal.org\">Drupal</a>,
|
|
the software which powers %s.</p>
|
|
<p>Distributed authentication enables a new user to input a username and password into the login box,
|
|
and immediately be recognized, even if that user never registered at %s. This
|
|
works because Drupal knows how to communicate with external registration databases.
|
|
For example, lets say that new user 'Joe' is already a registered member of
|
|
<a href=\"http://www.delphiforums.com\">Delphi Forums</a>. Drupal informs Joe
|
|
on registration and login screens that he may login with his Delphi ID instead
|
|
of registering with %s. Joe likes that idea, and logs in with a username
|
|
of joe@remote.delphiforums.com and his usual Delphi password. Drupal then contacts
|
|
the <i>remote.delphiforums.com</i> server behind the scenes (usually using <a href=\"http://www.xmlrpc.com\">XML-RPC</a>,
|
|
<a href=\"http://www.w3.org/Protocols/\">HTTP POST</a>, or <a href=\"http://www.soapware.org\">SOAP</a>)
|
|
and asks: \"Is the password for user Joe correct?\". If Delphi replies yes, then
|
|
we create a new $site account for Joe and log him into it. Joe may keep
|
|
on logging into %s in the same manner, and he will always be logged into the
|
|
same account.</p>";
|
|
|
|
$output = strtr($output, array("%s" => $site));
|
|
|
|
foreach (module_list() as $module) {
|
|
if (module_hook($module, "auth")) {
|
|
$output .= "<h4><a name=\"$module\"></a>" . module_invoke($module, "info", "name") . "</h4>";
|
|
$output .= module_invoke($module, "auth_help");
|
|
}
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
// the following functions comprise help for admins and developers
|
|
function user_help() {
|
|
user_help_admin();
|
|
user_help_admin_da();
|
|
user_help_devel_da();
|
|
user_help_devel_userhook();
|
|
}
|
|
|
|
function user_help_admin() {
|
|
?>
|
|
<h3>Introduction</h3>
|
|
<p>Drupal offers a powerful and open user system. This system allows users to
|
|
register, login, logout, maintain user profiles, etc. No participant can use
|
|
his own name to post comments until he signs up and submits his e-mail address.
|
|
Those who do not register may participate as anonymous users, but they will
|
|
suffer numerous disadvantages, for example their posts beginning at a lower
|
|
score. </p>
|
|
<p>In contrast, those with a user account can use their own name or handle and
|
|
are granted various privileges: the most important are probably the ability
|
|
to moderate new submissions, to rate comments, and to fine-tune the site to
|
|
their personal liking. Drupal themes make fine tuning quite a pleasure.</p>
|
|
<p>Registered users need to authenticate by supplying a username and password.
|
|
Users may authenticate locally or via an external authentication source like
|
|
<a href="http://www.jabber.org/">Jabber</a>, <a href="http://www.delphiforums.com/">Delphi</a>,
|
|
and other <a href="http://www.drupal.org/">Drupal</a> websites. See <a href="#da">distributed
|
|
authentication</a> for more information on this innovative feature. The username
|
|
and password are kept in your database, where the password is hashed so that
|
|
no one can read nor use it. When a username and password needs to be checked
|
|
the system goes down the list of registered users until it finds a matching
|
|
username, and then hashes the password that was supplied and compares it to
|
|
the listed value. If the hashes match, the username and password are correct.
|
|
Once a user authenticated session is started, and until that session is over,
|
|
the user won't have to re-authenticate. To keep track of the individual sessions,
|
|
Drupal relies on <a href="http://www.php.net/manual/en/ref.session.php">PHP's
|
|
session support</a>. A visitor accessing your website is assigned an unique
|
|
ID, the so-called session ID, which is stored in a cookie. For security's sake,
|
|
the cookie does not contain personal information but acts as a key to retrieve
|
|
the information stored on your server's side. When a visitor accesses your site,
|
|
Drupal will check whether a specific session ID has been sent with the request.
|
|
If this is the case, the prior saved environment is recreated.</p>
|
|
<p>Authenticated users can select entirely different appearances for the site,
|
|
utilizing their own preferences for how the pages are structured, how navigation
|
|
lists and other page components are presented and much more. <br />
|
|
</p>
|
|
<h3>User preferences and profiles</h3>
|
|
<p>Drupal comes with a set of user preferences and profile which a user may edit by
|
|
clicking on the user account link. Of course, a user must be logged into reach those pages.
|
|
There, users will find a page for changing their preferred time zone, language, username, e-mail address, password, theme, signature, homepage, and <a href="#da">distributed authentication</a> names.
|
|
Changes made here take effect immediately. Also, administrators may make profile and preferences changes in the Admin Center on behalf of their users.</p>
|
|
<p>Module developers are provided several hooks for adding custom fields to the user view/edit pages. These hooks are described in the Developer section of the <a href="http://www.drupal.org">Drupal Handbook</a>. For an example, see the <code>jabber_user()</code> function in <i>/modules/jabber.module</i>.
|
|
</p>
|
|
<?php
|
|
}
|
|
|
|
function user_help_admin_da() {
|
|
?>
|
|
|
|
<h3>Distributed authentication<a name="da"> </a></h3>
|
|
<p>One of the more tedious moments in visiting a new website is filling out the
|
|
registration form. The reg form provides helpful information to the website
|
|
owner, but not much value for the user. The value for the end user is usually
|
|
a the ability to post a messages or receive personalized news, etc. Distributed
|
|
authentication (DA) gives the user what he wants without having to fill out
|
|
the reg form. Removing this obstacle yields more registered and active users
|
|
for the website.</p>
|
|
<p>DA enables a new user to input a username and password into the login box and
|
|
immediately be recognized, even if that user never registered on your site.
|
|
This works because Drupal knows how to communicate with external registration
|
|
databases. For example, lets say that your new user 'Joe' is already a registered
|
|
member of Delphi Forums. If your Drupal has delphi.module installed, then Drupal
|
|
will inform Joe on the registration and login screens that he may login with
|
|
his Delphi ID instead of registering with your Drupal instance. Joe likes that
|
|
idea, and logs in with a username of joe@remote.delphiforums.com and his usual
|
|
Delphi password. Drupal then communicates with remote.delphiforums.com (usually using <a href="http://www.xmlrpc.com/">XML-RPC</a>,
|
|
<a href="http://www.w3.org/Protocols/">HTTP POST</a>, or <a href="http://www.soapware.org/">SOAP</a>) behind
|
|
the scenes and asks "is this password for username=joe? If Delphi replies
|
|
yes, then Drupal will create a new local account for joe and log joe into it.
|
|
Joe may keep on logging into your Drupal instance in the same manner, and he
|
|
will be logged into the same joe@remote.delphiforums.com account.</p>
|
|
<p>One key element of DA is the 'authmap' table, which maps a user's authname
|
|
(e.g. joe@remote.delphiforums.com) to his local UID (i.e. universal identification
|
|
number). This map is checked whenever a user successfully logs into an external
|
|
authentication source. Once Drupal knows that the current user is definately
|
|
joe@remote.delphiforums.com (because Delphi says so), he looks up Joe's UID
|
|
and logs Joe into that account.</p>
|
|
<p>To disable distributed authentication, simply disable or remove all DA modules. For a virgin
|
|
install, that means removing/disabling <i>jabber.module</i> and <i>drupal.module</i>
|
|
</p>
|
|
<p>Drupal is setup so that it is very easy to add support for any external authentication
|
|
source. You currently have the following authentication modules installed ...</p>
|
|
<?php
|
|
foreach (module_list() as $module) {
|
|
if (module_hook($module, "auth")) {
|
|
print "<h4>" . module_invoke($module, "info", "name") . "</h4>";
|
|
print module_invoke($module, "auth_help");
|
|
}
|
|
}
|
|
}
|
|
|
|
function user_help_devel_da() {
|
|
?>
|
|
<h3>Writing distributed authentication modules</h3>
|
|
<p>Drupal is specifically architected to enable easy authoring of new authentication
|
|
modules. I'll deconstruct the <a href="http://www.blogger.com">Blogger</a> authentication
|
|
module here, and hopefully provide all the details you'll need to write a new
|
|
auth module. If you want to download the full text of this module, visit the
|
|
<a href="http://cvs.drupal.org/viewcvs.cgi/contributions/modules/authentication/Blogger/?cvsroot=contrib">Drupal contributions CVS repository</a>.</p>
|
|
<h4>Code review</h4>
|
|
<pre><?php</pre>
|
|
<p>The first line of every authentication module is the same. It is the standard
|
|
processing instruction for any PHP file. Authentication modules are always written
|
|
in PHP, although they typically interact with systems written in many different
|
|
programming languages and operating systems languages.</p>
|
|
<pre>function blogger_info($field = NULL) {
|
|
$info["name"] = "Blogger";
|
|
$info["protocol"] = "XML-RPC";
|
|
$info["link"] = "<a href=\"module.php?mod=user&op=da_help#blogger\">Blogger</a>";
|
|
$info["maintainer"] = "Moshe Weitzman";
|
|
$info["maintaineremail"] = "weitzman at tejasa.com";
|
|
|
|
if ($field) {
|
|
return $info[$field];
|
|
}
|
|
else {
|
|
return $info;
|
|
}
|
|
}</pre>
|
|
<p>The <i>_info</i> function is always the first function defined in your module.
|
|
This function populates an array called <i>$info</i> with various pieces of
|
|
data. Some of this data is used by Drupal ("name", "link"),
|
|
and some of it just informs the users of your module. Your goal is to simply
|
|
copy the b<i>logger_info</i> function in your module - but wherever it says
|
|
<i>blogger</i> here, substitute your own module name.</p>
|
|
<pre>function blogger_auth($name, $pass, $server) {
|
|
if ($server !== "blogger.com") {
|
|
return 0; // user did not present a Blogger ID so don't bother trying.
|
|
}
|
|
|
|
$appkey = "6D4A2D6811A6E1F75148DC1155D33C0C958107BC"; //provided to Drupal by Ev@Blogger
|
|
$message = new xmlrpcmsg("blogger.getUsersBlogs", array(new xmlrpcval($appkey, "string"), new xmlrpcval($name, "string"), new xmlrpcval($pass, "string")));
|
|
$client = new xmlrpc_client("/api/RPC2", "plant.blogger.com");<br> $result = $client->send($message, 5);
|
|
|
|
// Since Blogger doesn't return a properly formed FaultCode, we just search for the string 'fault'.
|
|
if ($result && !stristr($result->serialize(), "fault")) {
|
|
// watchdog("user", "Success Blogger Auth. Response: " .
|
|
$result->serialize());
|
|
return 1;
|
|
}
|
|
else if ($result) {
|
|
// watchdog("user", "Blogger Auth failure. Response was " . $result->serialize());
|
|
return 0;
|
|
}
|
|
else {
|
|
// watchdog("user", "Blogger Auth failure. Could not connect.");
|
|
return 0;
|
|
}
|
|
}</pre>
|
|
<p>The <i>_auth</i> function is the heart of any authentication module. This function
|
|
is called whenever a user is attempting to login using your authentication module.
|
|
For successful authentications, this function returns TRUE. Otherwise, it returns
|
|
FALSE. This function always accepts 3 parameters, as shown above. These parameters
|
|
are passed by the user system (user.module). The user system parses the username
|
|
as typed by the user into 2 substrings - $name and $server. The parsing rules
|
|
are:</p>
|
|
<table width="80%" border="0" cellspacing="4" cellpadding="4" align="center">
|
|
<tr>
|
|
<th colspan="2" align="left">
|
|
_auth function parameters
|
|
</th>
|
|
</tr>
|
|
<tr>
|
|
<th>$name</th>
|
|
<td>The substring before the final <i>'@'</i> character in the username field</td>
|
|
</tr>
|
|
<tr>
|
|
<th>$pass</th>
|
|
<td>The whole string submitted by the user in the password field</td>
|
|
</tr>
|
|
<tr>
|
|
<th>$server</th>
|
|
<td>The substring after the final <i>'@'</i> symbol in the username field</td>
|
|
</tr>
|
|
</table>
|
|
<p>So now lets use that $name, $pass, and $server which has been passed to our
|
|
<i>_auth</i> function. Blogger authenticates users via <a href="http://www.xmlrpc.com">XML-RPC</a>.
|
|
Your module may authenticate using a different technique. Drupal doesn't reallly
|
|
care how your module communicates with its registration source. It just <b>trusts</b>
|
|
the module. </p>
|
|
<p>The lines above illustrate a typical <a href="http://www.xmlrpc.com/">XML-RPC</a>
|
|
method call. Here we build up a message and send it to Blogger, storing the
|
|
response in a variable called <i>$response</i>. The message we pass conforms
|
|
to the published <a href="http://plant.blogger.com/API">Blogger XML-RPC API</a>.
|
|
Your module will no doubt implement a different API. One peculiarity of this
|
|
module is that we don't actually use the $server parameter. Blogger only accepts
|
|
authentication at <i>plant.blogger.com</i>, so we hard-code that value into
|
|
the <i>xmlrpc_client()</i> function. A more typical example might be the jabber
|
|
module, which uses the <i>$server</i> parameter to determine where to send the
|
|
authentication request. Also of note is the '5' parameter in the <i>$client->send()</i>
|
|
call. This is a timeout value in seconds. All authentication modules should
|
|
implement a timeout on their external calls. This makes sure to return control
|
|
to the user.module if your registration database has become inoperable or unreachable.</p>
|
|
<pre>
|
|
if ($result && !stristr($result->serialize(), "fault")) {
|
|
// watchdog("user", "Success Blogger auth. Response was " . $result->serialize());
|
|
return 1;
|
|
}
|
|
else if ($result) {
|
|
// watchdog("user", "Blogger auth failure. Response was " . $result->serialize());
|
|
return 0;
|
|
}
|
|
else {
|
|
// watchdog("user", "Blogger auth failure. Could not connect.");<br>
|
|
return 0;
|
|
}
|
|
</pre>
|
|
<p>This second half of the <i>_auth</i> function examines the <i>$response</i>
|
|
from plant.blogger.com and returns a TRUE or FALSE as appropriate. This is a
|
|
critical decision, so do be sure you have good logic here, and perform sufficient
|
|
testing for all cases. In the case of Blogger, we search for the string 'fault'
|
|
in the response. If that string is present, or there is no repsonse, our function
|
|
returns FALSE. Otherwise, Blogger has returned valid data to our method request
|
|
and we return TRUE. There are several watchdog() calls here which are commented
|
|
out. These are useful for debugging, so I've left them in the code as comments.
|
|
</p>
|
|
<pre>function blogger_page() {
|
|
global $theme;
|
|
$theme->header();
|
|
$theme->box("Blogger", blogger_auth_help());
|
|
$theme->footer();
|
|
}</pre>
|
|
<p>The _page function is not currently used, but it might be in the future. For
|
|
now, just copy what you see here, substituting your module name for <i>blogger</i>.</p>
|
|
<pre>function blogger_auth_help() {
|
|
$site = variable_get("site_name", "this web site");
|
|
$output = "
|
|
<p>You may login to <i>%s</i> using a <b>Blogger ID</b>
|
|
and password. A Blogger ID consists of your Blogger username followed by <i>@blogger.com</i>.
|
|
So a valid blogger ID is mwlily@blogger.com. If you are a Blogger member, go
|
|
ahead and login now.</p><br>
|
|
<p>Blogger offers you instant communication power by letting you post
|
|
your thoughts to the web whenever the urge strikes.
|
|
Blogger will publish to your current web site or help you create one. <a
|
|
href=\"http://www.blogger.com/about.pyra\">Learn more about it</a>.";
|
|
|
|
return sprintf(t($output), $site);
|
|
}
|
|
</pre>
|
|
<p>The <i>_auth_help</i> function is prominently linked within Drupal, so you'll
|
|
want to write the best possible user help here. You'll want to tell users what
|
|
a proper username looks like for your authentication module. Also, you may advertise
|
|
a bit about your service at the end. Note that your help text is passed through
|
|
a t() function in the last line. This is Drupal's localization function. Translators
|
|
may localize your help text just like any other text in Drupal.</p>
|
|
<h4>Publishing your module</h4>
|
|
<p>Once you've written and tested your authentication module, you'll have to usually
|
|
want to share it with the world. The best way to do this is to add the module
|
|
to the <a href="http://cvs.drupal.org/viewcvs.cgi/contributions/modules/authentication/Blogger/?cvsroot=contrib">Drupal
|
|
contributions CVS repository</a>. You'll need to request priveleges in this repository - see <a href="http://cvs.drupal.org/viewcvs.cgi/contributions/README?rev=HEAD&cvsroot=contrib&content-type=text/vnd.viewcvs-markup">README</a>
|
|
for the details. Then you'll want to announce your contribution on the <a href="http://www.drop.org/node.php?title=mailing%2Blists">Drupal_devel
|
|
and Drupal_support mailing lists</a>. You might also want to post a story on
|
|
<a href="http://www.drop.org">Drop.org</a>.<br>
|
|
</p>
|
|
<?php
|
|
}
|
|
|
|
function user_help_devel_userhook() {
|
|
?>
|
|
<h3><a name="userhook">module_user()</a></h3>
|
|
<p>The <b>_user()</b> hook provides to module authors a mechanism for inserting text and form fields into the registration page, the user account view/edit pages, and the administer users page. This is useful if you want to add a custom field for your particular community. This is best illustrated by an example called <a href="http://cvs.drupal.org/viewcvs.cgi/contributions/modules/profile/?cvsroot=contrib">profile.module</a> in the contributions CVS repository. Profile.module is meant to be customized for your needs. Please download it and hack away until it does what you need.</p>
|
|
|
|
<p>Consider this simpler example from a fictional recipe community web site called Julia's Kitchen. Julia customizes her Drupal powered site by creating a new file called <i>julia.module</i>. That file does the following:
|
|
- new members must agree to Julia's Privacy Policy on the reg page.
|
|
- members may list their favorite ingredients on their public user profile page</p>
|
|
|
|
<p>Julia achieves this with the following code. The comments below should help you understand what is going on.</p>
|
|
|
|
<pre>
|
|
function julia_user($type, $edit, &$user) {
|
|
switch ($type) {
|
|
case "register_form":
|
|
$output .= form_item("Privacy Policy", "Julia would never sell your user information. She is just nice old French chef who lives near me in Cambridge, Massachussetts USA.");
|
|
$output .= form_checkbox("Accept <i>Julia's Kitchen</i> privacy policy.", julia_accept, 1, $edit["julia_accept"]);
|
|
return $output;
|
|
case "register_validate":
|
|
if ($edit["julia_accept"] == "1") {
|
|
// on success return the values you want to store
|
|
return array("julia_accept" => 1);
|
|
}
|
|
else {
|
|
// on error return an error message
|
|
return "You must accept the Julia's Kitchen privacy policy to register.";
|
|
}
|
|
case "view_public":
|
|
// when others look at user data
|
|
return form_item("Favorite Ingredient", $user->julia_favingredient);
|
|
case "view_private":
|
|
// when user tries to view his own user page.
|
|
return form_item("Favorite Ingredient", $user->julia_favingredient);
|
|
case "edit_form":
|
|
// when user tries to edit his own user page.
|
|
return form_textfield("Favorite Ingredient", "julia_favingredient", $user->julia_favingredient, 50, 65, "Tell everyone your secret spice");
|
|
case "edit_validate":
|
|
return user_save($user, array("julia_favingredient" => $edit["julia_favingredient"]));
|
|
}
|
|
}
|
|
</pre>
|
|
<?php
|
|
}
|
|
|
|
?>
|