2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
|
|
|
|
2007-05-08 21:01:51 +00:00
|
|
|
class UserBlockBuildingEvent extends Event {
|
|
|
|
var $parts = array();
|
|
|
|
var $user = null;
|
|
|
|
|
|
|
|
public function UserBlockBuildingEvent($user) {
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add_link($name, $link, $position=50) {
|
|
|
|
while(isset($this->parts[$position])) $position++;
|
|
|
|
$this->parts[$position] = "<a href='$link'>$name</a>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-16 19:38:12 +00:00
|
|
|
class UserPageBuildingEvent extends Event {
|
|
|
|
var $page = null;
|
|
|
|
var $user = null;
|
|
|
|
|
|
|
|
public function UserPageBuildingEvent($page, $user) {
|
|
|
|
$this->page = $page;
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
class UserPage extends Extension {
|
2007-07-16 19:47:25 +00:00
|
|
|
var $theme;
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
// event handling {{{
|
|
|
|
public function receive_event($event) {
|
2007-07-16 19:47:25 +00:00
|
|
|
if(is_null($this->theme)) $this->theme = get_theme_object("user", "UserPageTheme");
|
|
|
|
|
2007-07-16 13:15:56 +00:00
|
|
|
if(is_a($event, 'InitExtEvent')) {
|
|
|
|
global $config;
|
|
|
|
$config->set_default_bool("login_signup_enabled", true);
|
|
|
|
$config->set_default_int("login_memory", 365);
|
|
|
|
}
|
|
|
|
|
2007-07-17 07:45:35 +00:00
|
|
|
if(is_a($event, 'PageRequestEvent') && ($event->page_name == "user")) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $page;
|
|
|
|
global $user;
|
|
|
|
global $database;
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
if($event->get_arg(0) == "login") {
|
|
|
|
if(isset($_POST['user']) && isset($_POST['pass'])) {
|
2007-07-22 11:07:07 +00:00
|
|
|
$this->login($event->page);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-07-16 19:47:25 +00:00
|
|
|
$this->theme->display_login_page($event->page);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if($event->get_arg(0) == "logout") {
|
2007-07-16 13:15:56 +00:00
|
|
|
setcookie("shm_session", "", time()+60*60*24*$config->get_int('login_memory'), "/");
|
2007-04-16 11:58:25 +00:00
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("index"));
|
|
|
|
}
|
|
|
|
else if($event->get_arg(0) == "changepass") {
|
2007-07-22 11:07:07 +00:00
|
|
|
$this->change_password_wrapper($event->page);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else if($event->get_arg(0) == "create") {
|
2007-07-22 11:07:07 +00:00
|
|
|
$this->create_user_wrapper($event->page);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else if($event->get_arg(0) == "set_more") {
|
2007-07-22 11:07:07 +00:00
|
|
|
$this->set_more_wrapper($event->page);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else { // view
|
|
|
|
$duser = ($event->count_args() == 0) ? $user : $database->get_user_by_name($event->get_arg(0));
|
2007-07-26 11:49:52 +00:00
|
|
|
if(!is_null($duser)) {
|
|
|
|
send_event(new UserPageBuildingEvent($event->page, $duser));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->theme->display_error($page, "No Such User",
|
|
|
|
"If you typed the ID by hand, try again; if you came from a link on this ".
|
|
|
|
"site, it might be bug report time...");
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-16 19:38:12 +00:00
|
|
|
|
|
|
|
if(is_a($event, 'UserPageBuildingEvent')) {
|
2007-07-19 14:32:25 +00:00
|
|
|
global $user;
|
|
|
|
$this->theme->display_user_page($event->page, $event->user, $user);
|
2007-07-23 13:48:52 +00:00
|
|
|
if($user->is_admin() || $user->id == $event->user->id) {
|
|
|
|
$this->theme->display_ip_list($event->page, $this->count_upload_ips($event->user), $this->count_comment_ips($event->user));
|
|
|
|
}
|
2007-07-16 19:38:12 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
|
|
|
// user info is shown on all pages
|
|
|
|
if(is_a($event, 'PageRequestEvent')) {
|
|
|
|
global $user;
|
|
|
|
global $page;
|
|
|
|
|
|
|
|
if($user->is_anonymous()) {
|
2007-07-17 07:45:35 +00:00
|
|
|
$this->theme->display_login_block($event->page);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-07-19 14:32:25 +00:00
|
|
|
$ubbe = new UserBlockBuildingEvent($user);
|
|
|
|
send_event($ubbe);
|
|
|
|
ksort($ubbe->parts);
|
|
|
|
$this->theme->display_user_block($page, $user, $ubbe->parts);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_a($event, 'SetupBuildingEvent')) {
|
|
|
|
$sb = new SetupBlock("User Options");
|
2007-05-23 23:19:35 +00:00
|
|
|
$sb->add_bool_option("login_signup_enabled", "Allow new signups: ");
|
2007-05-08 20:36:02 +00:00
|
|
|
$sb->add_longtext_option("login_tac", "<br>Terms & Conditions:<br>");
|
2007-06-30 01:19:11 +00:00
|
|
|
$event->panel->add_block($sb);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2007-05-08 21:01:51 +00:00
|
|
|
|
|
|
|
if(is_a($event, 'UserBlockBuildingEvent')) {
|
|
|
|
$event->add_link("User Config", make_link("user"));
|
2007-06-30 01:19:11 +00:00
|
|
|
$event->add_link("Log Out", make_link("user/logout"), 99);
|
2007-05-08 21:01:51 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
// Things done *with* the user {{{
|
2007-07-22 11:07:07 +00:00
|
|
|
private function login($page) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $database;
|
|
|
|
global $config;
|
|
|
|
global $user;
|
|
|
|
|
|
|
|
$name = $_POST['user'];
|
|
|
|
$pass = $_POST['pass'];
|
|
|
|
$addr = $_SERVER['REMOTE_ADDR'];
|
|
|
|
$hash = md5( strtolower($name) . $pass );
|
|
|
|
|
2007-07-12 07:37:32 +00:00
|
|
|
$duser = $database->get_user_by_name_and_hash($name, $hash);
|
2007-04-16 11:58:25 +00:00
|
|
|
if(!is_null($duser)) {
|
|
|
|
$user = $duser;
|
|
|
|
|
|
|
|
setcookie(
|
|
|
|
"shm_user", $name,
|
|
|
|
time()+60*60*24*365, "/"
|
|
|
|
);
|
|
|
|
setcookie(
|
|
|
|
"shm_session", md5($hash.$addr),
|
2007-07-16 13:15:56 +00:00
|
|
|
time()+60*60*24*$config->get_int('login_memory'), "/"
|
2007-04-16 11:58:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("user"));
|
|
|
|
}
|
|
|
|
else {
|
2007-07-22 11:07:07 +00:00
|
|
|
$this->theme->display_error($page, "Error", "No user with those details was found");
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-22 11:07:07 +00:00
|
|
|
private function create_user_wrapper($page) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $database;
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
if(!$config->get_bool("login_signup_enabled")) {
|
|
|
|
$page->set_title("Signups Disabled");
|
|
|
|
$page->set_heading("Signups Disabled");
|
2007-06-30 01:19:11 +00:00
|
|
|
$page->add_block(new NavBlock());
|
|
|
|
$page->add_block(new Block("Signups Disabled",
|
2007-04-16 11:58:25 +00:00
|
|
|
"The board admin has disabled the ability to create new accounts~"));
|
|
|
|
}
|
|
|
|
else if(isset($_POST['name']) && isset($_POST['pass1']) && isset($_POST['pass2'])) {
|
|
|
|
$name = trim($_POST['name']);
|
|
|
|
$pass1 = $_POST['pass1'];
|
|
|
|
$pass2 = $_POST['pass2'];
|
|
|
|
|
|
|
|
$page->set_title("Error");
|
|
|
|
$page->set_heading("Error");
|
2007-06-30 01:19:11 +00:00
|
|
|
$page->add_block(new NavBlock());
|
2007-04-16 11:58:25 +00:00
|
|
|
if(strlen($name) < 1) {
|
2007-06-30 01:19:11 +00:00
|
|
|
$page->add_block(new Block("Error", "Username must be at least 1 character"));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else if($pass1 != $pass2) {
|
2007-06-30 01:19:11 +00:00
|
|
|
$page->add_block(new Block("Error", "Passwords don't match"));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else if($database->db->GetRow("SELECT * FROM users WHERE name = ?", array($name))) {
|
2007-06-30 01:19:11 +00:00
|
|
|
$page->add_block(new Block("Error", "That username is already taken"));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$addr = $_SERVER['REMOTE_ADDR'];
|
|
|
|
$hash = md5( strtolower($name) . $pass1 );
|
|
|
|
$email = isset($_POST['email']) ? $_POST['email'] : null;
|
|
|
|
|
|
|
|
// FIXME: send_event()
|
2007-05-23 22:19:12 +00:00
|
|
|
$database->Execute(
|
2007-04-16 11:58:25 +00:00
|
|
|
"INSERT INTO users (name, pass, joindate, email) VALUES (?, ?, now(), ?)",
|
|
|
|
array($name, $hash, $email));
|
|
|
|
|
|
|
|
setcookie("shm_user", $name,
|
|
|
|
time()+60*60*24*365, '/');
|
|
|
|
setcookie("shm_session", md5($hash.$addr),
|
2007-07-16 13:15:56 +00:00
|
|
|
time()+60*60*24*$config->get_int('login_memory'), '/');
|
2007-04-16 11:58:25 +00:00
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("user"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2007-07-16 19:47:25 +00:00
|
|
|
$this->theme->display_signup_page($page);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//}}}
|
2007-06-30 01:19:11 +00:00
|
|
|
// Things done *to* the user {{{
|
2007-07-22 11:07:07 +00:00
|
|
|
private function change_password_wrapper($page) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $user;
|
|
|
|
global $database;
|
|
|
|
|
|
|
|
$page->set_title("Error");
|
|
|
|
$page->set_heading("Error");
|
2007-06-30 01:19:11 +00:00
|
|
|
$page->add_block(new NavBlock());
|
2007-04-16 11:58:25 +00:00
|
|
|
if($user->is_anonymous()) {
|
2007-06-30 01:19:11 +00:00
|
|
|
$page->add_block(new Block("Error", "You aren't logged in"));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else if(isset($_POST['id']) && isset($_POST['name']) &&
|
|
|
|
isset($_POST['pass1']) && isset($_POST['pass2'])) {
|
|
|
|
$name = $_POST['name'];
|
|
|
|
$id = $_POST['id'];
|
|
|
|
$pass1 = $_POST['pass1'];
|
|
|
|
$pass2 = $_POST['pass2'];
|
|
|
|
|
|
|
|
if((!$user->is_admin()) && ($name != $user->name)) {
|
2007-06-30 01:19:11 +00:00
|
|
|
$page->add_block(new Block("Error",
|
2007-04-16 11:58:25 +00:00
|
|
|
"You need to be an admin to change other people's passwords"));
|
|
|
|
}
|
|
|
|
else if($pass1 != $pass2) {
|
2007-06-30 01:19:11 +00:00
|
|
|
$page->add_block(new Block("Error", "Passwords don't match"));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
global $config;
|
|
|
|
$addr = $_SERVER['REMOTE_ADDR'];
|
|
|
|
$hash = md5( strtolower($name) . $pass1 );
|
|
|
|
|
|
|
|
// FIXME: send_event()
|
|
|
|
// FIXME: $duser->set_pass();
|
2007-05-23 22:19:12 +00:00
|
|
|
$database->Execute(
|
2007-04-16 11:58:25 +00:00
|
|
|
"UPDATE users SET pass = ? WHERE id = ?",
|
|
|
|
array($hash, $id));
|
|
|
|
|
|
|
|
if($id == $user->id) {
|
|
|
|
setcookie("shm_user", $name,
|
|
|
|
time()+60*60*24*365, '/');
|
|
|
|
setcookie("shm_session", md5($hash.$addr),
|
2007-07-16 13:15:56 +00:00
|
|
|
time()+60*60*24*$config->get_int('login_memory'), '/');
|
2007-04-16 11:58:25 +00:00
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("user"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("user/{$user->name}"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-22 11:07:07 +00:00
|
|
|
private function set_more_wrapper($page) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $user;
|
|
|
|
global $database;
|
|
|
|
|
|
|
|
$page->set_title("Error");
|
|
|
|
$page->set_heading("Error");
|
2007-06-30 01:19:11 +00:00
|
|
|
$page->add_block(new NavBlock());
|
2007-04-16 11:58:25 +00:00
|
|
|
if(!$user->is_admin()) {
|
2007-06-30 01:19:11 +00:00
|
|
|
$page->add_block(new Block("Not Admin", "Only admins can edit accounts"));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else if(!isset($_POST['id']) || !is_numeric($_POST['id'])) {
|
2007-06-30 01:19:11 +00:00
|
|
|
$page->add_block(new Block("No ID Specified",
|
2007-04-16 11:58:25 +00:00
|
|
|
"You need to specify the account number to edit"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$admin = (isset($_POST['admin']) && ($_POST['admin'] == "on"));
|
|
|
|
$enabled = (isset($_POST['enabled']) && ($_POST['enabled'] == "on"));
|
|
|
|
|
|
|
|
$duser = $database->get_user_by_id($_POST['id']);
|
|
|
|
$duser->set_admin($admin);
|
|
|
|
$duser->set_enabled($enabled);
|
|
|
|
|
|
|
|
$page->set_mode("redirect");
|
|
|
|
if($duser->id == $user->id) {
|
|
|
|
$page->set_redirect(make_link("user"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$page->set_redirect(make_link("user/{$duser->name}"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
2007-07-19 14:32:25 +00:00
|
|
|
// ips {{{
|
|
|
|
private function count_upload_ips($duser) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $database;
|
2007-07-19 14:32:25 +00:00
|
|
|
$rows = $database->db->GetAssoc("
|
2007-06-03 10:40:00 +00:00
|
|
|
SELECT owner_ip, COUNT(images.id) AS count
|
|
|
|
FROM images
|
|
|
|
WHERE owner_id=?
|
|
|
|
GROUP BY owner_ip
|
2007-07-23 13:51:41 +00:00
|
|
|
ORDER BY MAX(posted) DESC", array($duser->id));
|
2007-07-19 14:32:25 +00:00
|
|
|
return $rows;
|
|
|
|
}
|
|
|
|
private function count_comment_ips($duser) {
|
|
|
|
global $database;
|
|
|
|
$rows = $database->db->GetAssoc("
|
2007-06-03 10:40:00 +00:00
|
|
|
SELECT owner_ip, COUNT(comments.id) AS count
|
|
|
|
FROM comments
|
|
|
|
WHERE owner_id=?
|
|
|
|
GROUP BY owner_ip
|
2007-07-23 13:51:41 +00:00
|
|
|
ORDER BY MAX(posted) DESC", array($duser->id));
|
2007-07-19 14:32:25 +00:00
|
|
|
return $rows;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
}
|
|
|
|
add_event_listener(new UserPage());
|
|
|
|
?>
|