include "function.inc";
include "config.inc";
include "theme.inc";
function account_getUser($uname) {
$result = db_query("SELECT * FROM users WHERE userid = '$uname'");
return db_fetch_object($result);
}
function showLogin($userid = "") {
$output .= "
\n";
return $output;
}
function showAccess() {
global $user, $access;
foreach ($access as $key=>$value) if ($user->access & $value) $result .= "$key ";
return $result;
}
function showUser($uname) {
global $user, $theme;
if ($user && $uname && $user->userid == $uname) {
$output .= "
Welcome $user->userid! This is your user info page. There are many more, but this one is yours. You are probably most interested in editing something, but if you need to kill some time, this place is as good as any other place.
\n";
### Display account information:
$theme->header();
$theme->box("User information", $output);
$theme->footer();
}
else {
### Display login form:
$theme->header();
$theme->box("Login", showLogin($userid));
$theme->footer();
}
}
function newUser($user = "", $error="") {
global $theme;
$output .= "\n";
$theme->header();
$theme->box("Register as new user", $output);
$theme->footer();
}
function validateUser($user) {
include "ban.inc";
### Verify username and e-mail address:
$user[userid] = trim($user[userid]);
if (empty($user[email]) || (!eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $user[email]))) $rval = "the specified e-mail address is not valid. ";
if (empty($user[userid]) || (ereg("[^a-zA-Z0-9_-]", $user[userid]))) $rval = "the specified username '$new[userid]' is not valid. ";
if (strlen($user[userid]) > 15) $rval = "the specified username is too long: it must be less than 15 characters.";
### Check to see whether the username or e-mail address are banned:
if ($ban = ban_match($user[userid], $type2index[usernames])) $rval = "the specified username is banned for the following reason: $ban->reason.";
if ($ban = ban_match($user[email], $type2index[addresses])) $rval = "the specified e-mail address is banned for the following reason: $ban->reason.";
### Verify whether username and e-mail address are unique:
if (db_num_rows(db_query("SELECT userid FROM users WHERE LOWER(userid)=LOWER('$user[userid]')")) > 0) $rval = "the specified username is already taken.";
if (db_num_rows(db_query("SELECT email FROM users WHERE LOWER(email)=LOWER('$user[email]')")) > 0) $rval = "the specified e-mail address is already registered.";
return($rval);
}
function account_makePassword($min_length=6) {
mt_srand((double)microtime() * 1000000);
$words = array("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 .= $words[mt_rand(0, count($words))];
return $password;
}
function account_track_comments() {
global $user;
$output .= "
This page might be helpful in case you want to keep track of your most recent comments in any of the discussions. You are given an overview of your comments in each of the stories you participates in along with the number of replies each comment got.\n
\n";
### Perform query:
$sresult = db_query("SELECT s.id, s.subject, COUNT(s.id) as count FROM comments c LEFT JOIN stories s ON c.sid = s.id WHERE c.author = $user->id GROUP BY s.id DESC LIMIT 5");
while ($story = db_fetch_object($sresult)) {
$output .= "
". format_plural($story->count, comment, comments) ." in story `id\">$story->subject`:
\n";
$output .= "
\n";
$cresult = db_query("SELECT * FROM comments WHERE author = $user->id AND sid = $story->id");
while ($comment = db_fetch_object($cresult)) {
$output .= "
\n";
}
return $output;
}
switch ($op) {
case "Login":
session_start();
$user = new User($userid, $passwd);
if ($user && user_valid()) {
session_register("user");
watchdog(1, "session opened for user `$user->userid'.");
}
else {
watchdog(2, "failed login for user `$userid'.");
}
showUser($user->userid);
break;
case "new":
newUser();
break;
case "view":
showUser($name);
break;
case "discussion":
$theme->header();
$theme->box("Track your comments", account_track_comments());
$theme->footer();
break;
case "logout":
watchdog(1, "session closed for user `$user->userid'.");
session_unset();
session_destroy();
unset($user);
showUser();
break;
case "Register":
if ($rval = validateUser($new)) { newUser($new, "Error: $rval"); }
else {
### Generate new password:
$new[passwd] = account_makePassword();
dbsave("users", $new);
if ($system == 1) {
### Display account information:
$theme->header();
$theme->box("Account details", "Your password is: $new[passwd] Login to change your personal settings.");
$theme->footer();
} else {
### Send e-mail with account details:
mail($new[email], "Account details for $sitename", "$user->name,\n\nyour $sitename member account has been created succesfully. To be able to use it, you must login using the information below. Please save this mail for further reference.\n\n username: $new[userid]\n e-mail: $new[email]\n password: $new[passwd]\n\nThis password is generated by a randomizer. It is recommended that you change this password immediately.\n\n$contact_signature", "From: $contact_email\nX-Mailer: PHP/" . phpversion());
### Display account information:
$theme->header();
$theme->box("Account details", "Your member account has been created and the details necessary to login have been sent to your e-mail account $new[email]. Once you received the account confirmation, hit this link to login.");
$theme->footer();
}
watchdog(1, "new user `$new[userid]' registered with e-mail address `$new[email]'");
}
break;
case "user":
if ($user->id && user_valid()) {
### Generate output/content:
$output .= "\n";
### Display output/content:
$theme->header();
$theme->box("Edit your information", $output);
$theme->footer();
}
else {
$theme->header();
$theme->box("Login", showLogin($userid));
$theme->footer();
}
break;
case "page":
if ($user && user_valid()) {
### Generate output/content:
$output .= "\n";
### Display output/content:
$theme->header();
$theme->box("Customize your page", $output);
$theme->footer();
}
else {
$theme->header();
$theme->box("Login", showLogin($userid));
$theme->footer();
}
break;
case "Save user information":
if ($user && user_valid()) {
$data[name] = $edit[name];
$data[email] = $edit[email];
$data[femail] = $edit[femail];
$data[url] = $edit[url];
$data[bio] = $edit[bio];
$data[ublock] = $edit[ublock];
$data[ublockon] = $edit[ublockon];
if ($edit[pass1] == $edit[pass2] && !empty($edit[pass1])) { $data[passwd] = $edit[pass1]; }
dbsave("users", $data, $user->id);
user_rehash();
}
showUser($user->userid);
break;
case "Save page settings":
if ($user && user_valid()) {
$data[theme] = $edit[theme];
$data[storynum] = $edit[storynum];
$data[umode] = $edit[umode];
$data[uorder] = $edit[uorder];
$data[thold] = $edit[thold];
$data[signature] = $edit[signature];
dbsave("users", $data, $user->id);
user_rehash();
}
showUser($user->userid);
break;
default:
showUser($user->userid);
}
?>