include "includes/theme.inc";
function account_get_user($uname) {
$result = db_query("SELECT * FROM users WHERE userid = '$uname'");
return db_fetch_object($result);
}
function account_login() {
$output .= "
\n";
$output .= "You don't have an account yet? Register as new user.\n";
return $output;
}
function account_session_start($userid, $passwd) {
global $user;
$user = new User($userid, $passwd);
if ($user->id) {
session_start();
session_register("user");
watchdog("message", "session opened for user `$user->userid'");
}
else {
watchdog("warning", "failed login for user `$userid'");
}
}
function account_session_close() {
global $user;
watchdog("message", "session closed for user `$user->userid'");
session_unset();
session_destroy();
unset($user);
}
function account_user_edit() {
global $theme, $user;
if ($user->id) {
### Generate output/content:
$output .= "\n";
### Display output/content:
$theme->header();
$theme->box("Edit your information", $output);
$theme->footer();
}
else {
$theme->header();
$theme->box("Login", account_login());
$theme->footer();
}
}
function account_user_save($edit) {
global $user;
if ($user->id) {
$data[name] = $edit[name];
$data[fake_email] = $edit[fake_email];
$data[url] = $edit[url];
$data[bio] = $edit[bio];
$data[signature] = $edit[signature];
if ($edit[pass1] && $edit[pass1] == $edit[pass2]) $data[passwd] = $edit[pass1];
user_save($data, $user->id);
}
}
function account_page_edit() {
global $theme, $themes, $user;
if ($user->id) {
$output .= "\n";
$theme->header();
$theme->box("Customize your page", $output);
$theme->footer();
}
else {
$theme->header();
$theme->box("Login", account_login());
$theme->footer();
}
}
function account_page_save($edit) {
global $user;
if ($user->id) {
$data[theme] = $edit[theme];
$data[timezone] = $edit[timezone];
$data[stories] = $edit[stories];
$data[mode] = $edit[mode];
$data[sort] = $edit[sort];
$data[threshold] = $edit[threshold];
user_save($data, $user->id);
}
}
function account_user($uname) {
global $user, $theme;
if ($user->id && $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";
$result = db_query("SELECT c.cid, c.pid, c.sid, c.subject, c.timestamp, s.subject AS story FROM comments c LEFT JOIN users u ON u.id = c.author LEFT JOIN stories s ON s.id = c.sid WHERE u.userid = '$uname' AND s.status = 2 AND s.timestamp > ". (time() - 1209600) ." ORDER BY cid DESC LIMIT 10");
while ($comment = db_fetch_object($result)) {
$box2 .= "
\n";
$comments++;
}
$result = db_query("SELECT d.* FROM diaries d LEFT JOIN users u ON u.id = d.author WHERE u.userid = '$uname' AND d.timestamp > ". (time() - 1209600) ." ORDER BY id DESC LIMIT 2");
while ($diary = db_fetch_object($result)) {
$box3 .= "
\n";
$diaries++;
}
### Display account information:
$theme->header();
if ($box1) $theme->box("User information for $uname", $box1);
if ($box2) $theme->box("$uname has posted ". format_plural($comments, "comment", "comments") ." recently", $box2);
if ($box3) $theme->box("$uname has posted ". format_plural($diaries, "diary entry", "diary entries") ." recently", $box3);
$theme->footer();
}
else {
### Display login form:
$theme->header();
$theme->box("Login", account_login());
$theme->footer();
}
}
function account_validate($user) {
include "includes/ban.inc";
### Verify username and e-mail address:
$user[userid] = trim($user[userid]);
if (empty($user[real_email]) || (!eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $user[real_email]))) $error .= "
the specified e-mail address is not valid.
\n";
if (empty($user[userid]) || (ereg("[^a-zA-Z0-9_-]", $user[userid]))) $error .= "
the specified username is not valid.
\n";
if (strlen($user[userid]) > 15) $error .= "
the specified username is too long: it must be less than 15 characters.
\n";
### Check to see whether the username or e-mail address are banned:
if ($ban = ban_match($user[userid], $type2index[usernames])) $error .= "
the specified username is banned for the following reason: $ban->reason.
\n";
if ($ban = ban_match($user[real_email], $type2index[addresses])) $error .= "
the specified e-mail address is banned for the following reason: $ban->reason.
\n";
### 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) $error .= "
the specified username is already taken.
\n";
if (db_num_rows(db_query("SELECT real_email FROM users WHERE LOWER(real_email)=LOWER('$user[real_email]')")) > 0) $error .= "
the specified e-mail address is already registered.
\n";
return $error;
}
function account_register_enter($user = "", $error = "") {
global $theme;
if ($error) $output .= "Failed to register.$error\n";
else $output .= "
Registering allows you to comment on stories, to moderate comments and pending stories, to maintain an online diary, to customize the look and feel of the site and generally helps you interact with the site more efficiently.
To create an account, simply fill out this form an click the `Register' button below. An e-mail will then be sent to you with instructions on how to validate your account.
\n";
$output .= "\n";
$theme->header();
$theme->box("Register as new user", $output);
$theme->footer();
}
function account_register_submit($new) {
global $theme, $mail, $site_name, $site_url;
if ($rval = account_validate($new)) {
account_register_enter($new, "$rval");
}
else {
$new[passwd] = account_password();
$new[status] = 1;
$new[hash] = substr(md5("$new[userid]. ". time() .""), 0, 12);
user_save($new);
$link = $site_url ."account.php?op=confirm&name=$new[userid]&hash=$new[hash]";
$message = "$new[userid],\n\n\nsomeone signed up for a user account on $site_name and supplied this email address as their contact. If it wasn't you, don't get your panties in a knot and simply ignore this mail.\n\nIf this was you, you have to activate your account first before you can login. You can do so simply by visiting the URL below:\n\n $link\n\nVisiting this URL will automatically activate your account. Once activated you can login using the following information:\n\n username: $new[userid]\n password: $new[passwd]\n\n\n-- $site_name crew\n";
mail($new[real_email], "Account details for $site_name", $message, "From: noreply@$site_url");
watchdog("message", "new user `$new[userid]' <$new[real_email]>");
$theme->header();
$theme->box("Account details", "Congratulations! Your member account has been sucessfully created and further instructions on how to activate your account have been sent to your e-mail address.");
$theme->footer();
}
}
function account_register_confirm($name, $hash) {
global $theme;
$result = db_query("SELECT userid, hash, status FROM users WHERE userid = '$name'");
if ($account = db_fetch_object($result)) {
if ($account->status == 1) {
if ($account->hash == $hash) {
db_query("UPDATE users SET status = 2, hash = '' WHERE userid = '$name'");
$output .= "Your account has been sucessfully confirmed. You can click here to login.\n";
watchdog("message", "$name: account confirmation sucessful");
}
else {
$output .= "Confirmation failed: invalid confirmation hash.\n";
watchdog("error", "$name: invalid confirmation hash");
}
}
else {
$output .= "Confirmation failed: your account has already been confirmed. You can click here to login.\n";
watchdog("error", "$name: attempt to re-confirm account");
}
}
else {
$output .= "Confirmation failed: no such account found. ";
watchdog("error", "$name: attempt to confirm non-existing account");
}
$theme->header();
$theme->box("Account confirmation", $output);
$theme->footer();
}
function account_password($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_comments() {
global $theme, $user;
$info = "
This page might be helpful in case you want to keep track of your recent comments in any of the current discussions. You are presented an overview of your comments in each of the stories you participated in along with the number of replies each comment got.\n
\n";
$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 .= "