This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/user/main.php

532 lines
16 KiB
PHP
Raw Normal View History

<?php
2010-01-05 10:11:53 +00:00
/*
* Name: User Management
* Author: Shish
* Description: Allows people to sign up to the website
*/
class UserBlockBuildingEvent extends Event {
var $parts = array();
public function add_link($name, $link, $position=50) {
while(isset($this->parts[$position])) $position++;
2009-08-03 09:46:40 +00:00
$this->parts[$position] = array("name" => $name, "link" => $link);
}
}
class UserPageBuildingEvent extends Event {
2009-01-04 18:31:54 +00:00
var $display_user;
var $stats = array();
public function __construct(User $display_user) {
2009-01-04 18:31:54 +00:00
$this->display_user = $display_user;
}
public function add_stats($html, $position=50) {
while(isset($this->stats[$position])) $position++;
$this->stats[$position] = $html;
}
}
class UserCreationEvent extends Event {
var $username;
var $password;
var $email;
public function __construct($name, $pass, $email) {
$this->username = $name;
$this->password = $pass;
$this->email = $email;
}
}
class UserCreationException extends SCoreException {}
class UserPage extends Extension {
2012-02-02 08:07:57 +00:00
public function onInitExt(InitExtEvent $event) {
2009-05-30 13:47:57 +00:00
global $config;
$config->set_default_bool("login_signup_enabled", true);
$config->set_default_int("login_memory", 365);
2010-04-21 16:56:01 +00:00
$config->set_default_string("avatar_host", "none");
$config->set_default_int("avatar_gravatar_size", 80);
$config->set_default_string("avatar_gravatar_default", "");
$config->set_default_string("avatar_gravatar_rating", "g");
2010-01-05 19:06:04 +00:00
$config->set_default_bool("login_tac_bbcode", true);
2009-05-30 13:47:57 +00:00
}
2012-02-02 08:07:57 +00:00
public function onPageRequest(PageRequestEvent $event) {
2009-05-30 13:47:57 +00:00
global $config, $database, $page, $user;
2009-01-04 19:18:37 +00:00
2009-05-30 13:47:57 +00:00
// user info is shown on all pages
if($user->is_anonymous()) {
$this->theme->display_login_block($page);
}
else {
$ubbe = new UserBlockBuildingEvent();
send_event($ubbe);
ksort($ubbe->parts);
$this->theme->display_user_block($page, $user, $ubbe->parts);
}
2009-05-30 13:47:57 +00:00
if($event->page_matches("user_admin")) {
if($event->get_arg(0) == "login") {
if(isset($_POST['user']) && isset($_POST['pass'])) {
2009-01-04 19:18:37 +00:00
$this->login($page);
}
else {
2009-01-04 19:18:37 +00:00
$this->theme->display_login_page($page);
}
}
else if($event->get_arg(0) == "logout") {
2009-10-08 12:59:12 +00:00
set_prefixed_cookie("session", "", time()+60*60*24*$config->get_int('login_memory'), "/");
2012-01-26 16:51:24 +00:00
if(CACHE_HTTP || SPEED_HAX) {
# to keep as few versions of content as possible,
# make cookies all-or-nothing
set_prefixed_cookie("user", "", time()+60*60*24*$config->get_int('login_memory'), "/");
}
2009-12-30 08:17:33 +00:00
log_info("user", "Logged out");
2009-01-04 19:18:37 +00:00
$page->set_mode("redirect");
$page->set_redirect(make_link());
}
else if($event->get_arg(0) == "change_pass") {
if(isset($_POST['id']) && isset($_POST['pass1']) && isset($_POST['pass2'])) {
$duser = User::by_id($_POST['id']);
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
$this->change_password_wrapper($duser, $pass1, $pass2);
}
}
2009-08-11 16:07:03 +00:00
else if($event->get_arg(0) == "change_email") {
if(isset($_POST['id']) && isset($_POST['address'])) {
$duser = User::by_id($_POST['id']);
$address = $_POST['address'];
$this->change_email_wrapper($duser, $address);
}
}
else if($event->get_arg(0) == "change_class") {
global $_user_classes;
if(isset($_POST['id']) && isset($_POST['class'])) {
$duser = User::by_id($_POST['id']);
$class = $_POST['class'];
if(!array_key_exists($class, $_user_classes)) {
throw Exception("Invalid user class: ".html_escape($class));
}
$this->change_class_wrapper($duser, $class);
}
2009-08-11 16:07:03 +00:00
}
2009-07-11 12:38:48 +00:00
else if($event->get_arg(0) == "recover") {
$user = User::by_name($_POST['username']);
if(is_null($user)) {
$this->theme->display_error(404, "Error", "There's no user with that name");
2009-07-11 12:38:48 +00:00
}
if(is_null($user->email)) {
//
}
}
else if($event->get_arg(0) == "create") {
if(!$config->get_bool("login_signup_enabled")) {
$this->theme->display_signups_disabled($page);
}
else if(!isset($_POST['name'])) {
2009-01-04 19:18:37 +00:00
$this->theme->display_signup_page($page);
}
else if($_POST['pass1'] != $_POST['pass2']) {
$this->theme->display_error(400, "Password Mismatch", "Passwords don't match");
}
else {
try {
if(!captcha_check()) {
throw new UserCreationException("Error in captcha");
2009-11-10 03:21:02 +00:00
}
$uce = new UserCreationEvent($_POST['name'], $_POST['pass1'], $_POST['email']);
send_event($uce);
$this->set_login_cookie($uce->username, $uce->password);
2009-01-04 19:18:37 +00:00
$page->set_mode("redirect");
$page->set_redirect(make_link("user"));
}
catch(UserCreationException $ex) {
$this->theme->display_error(400, "User Creation Error", $ex->getMessage());
}
}
}
2009-05-30 13:47:57 +00:00
else if($event->get_arg(0) == "list") {
// select users.id,name,joindate,admin,
// (select count(*) from images where images.owner_id=users.id) as images,
// (select count(*) from comments where comments.owner_id=users.id) as comments from users;
// select users.id,name,joindate,admin,image_count,comment_count
// from users
// join (select owner_id,count(*) as image_count from images group by owner_id) as _images on _images.owner_id=users.id
// join (select owner_id,count(*) as comment_count from comments group by owner_id) as _comments on _comments.owner_id=users.id;
$this->theme->display_user_list($page, User::by_list(0), $user);
}
else if($event->get_arg(0) == "delete_user") {
$this->delete_user($page);
}
else if($event->get_arg(0) == "delete_user_with_images") {
$this->delete_user_with_images($page);
}
}
2009-05-30 13:47:57 +00:00
2012-02-02 08:07:57 +00:00
if($event->page_matches("user")) {
$display_user = ($event->count_args() == 0) ? $user : User::by_name($event->get_arg(0));
2009-07-20 05:42:09 +00:00
if($event->count_args() == 0 && $user->is_anonymous()) {
$this->theme->display_error(401, "Not Logged In",
2009-07-20 05:42:09 +00:00
"You aren't logged in. First do that, then you can see your stats.");
}
else if(!is_null($display_user) && ($display_user->id != $config->get_int("anon_id"))) {
send_event(new UserPageBuildingEvent($display_user));
}
else {
$this->theme->display_error(404, "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...");
}
}
2009-05-30 13:47:57 +00:00
}
2009-01-04 19:18:37 +00:00
2012-02-02 08:07:57 +00:00
public function onUserPageBuilding(UserPageBuildingEvent $event) {
2009-06-06 19:04:33 +00:00
global $page, $user, $config;
$h_join_date = autodate($event->display_user->join_date);
$event->add_stats("Joined: $h_join_date", 10);
2009-10-08 11:32:01 +00:00
$av = $event->display_user->get_avatar_html();
if($av) $event->add_stats($av, 0);
ksort($event->stats);
$this->theme->display_user_page($event->display_user, $event->stats);
2009-05-30 13:47:57 +00:00
if($user->id == $event->display_user->id) {
$ubbe = new UserBlockBuildingEvent();
send_event($ubbe);
ksort($ubbe->parts);
$this->theme->display_user_links($page, $user, $ubbe->parts);
}
if(
2012-02-07 15:15:18 +00:00
($user->can("view_ip") || ($user->is_logged_in() && $user->id == $event->display_user->id)) && # admin or self-user
($event->display_user->id != $config->get_int('anon_id')) # don't show anon's IP list, it is le huge
) {
$this->theme->display_ip_list(
$page,
$this->count_upload_ips($event->display_user),
$this->count_comment_ips($event->display_user));
}
2009-05-30 13:47:57 +00:00
}
2009-01-04 19:18:37 +00:00
2012-02-02 08:07:57 +00:00
public function onSetupBuilding(SetupBuildingEvent $event) {
2010-04-21 16:56:01 +00:00
global $config;
2010-04-21 16:32:32 +00:00
$hosts = array(
"None" => "none",
"Gravatar" => "gravatar"
);
2009-05-30 13:47:57 +00:00
$sb = new SetupBlock("User Options");
$sb->add_bool_option("login_signup_enabled", "Allow new signups: ");
$sb->add_longtext_option("login_tac", "<br>Terms &amp; Conditions:<br>");
2010-04-21 16:56:01 +00:00
$sb->add_choice_option("avatar_host", $hosts, "<br>Avatars: ");
if($config->get_string("avatar_host") == "gravatar") {
$sb->add_label("<br>&nbsp;<br><b>Gravatar Options</b>");
$sb->add_choice_option("avatar_gravatar_type",
array(
'Default'=>'default',
'Wavatar'=>'wavatar',
'Monster ID'=>'monsterid',
'Identicon'=>'identicon'
),
"<br>Type: ");
$sb->add_choice_option("avatar_gravatar_rating",
array('G'=>'g', 'PG'=>'pg', 'R'=>'r', 'X'=>'x'),
"<br>Rating: ");
}
2009-05-30 13:47:57 +00:00
$event->panel->add_block($sb);
}
2012-02-02 08:07:57 +00:00
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
2009-05-30 13:47:57 +00:00
$event->add_link("My Profile", make_link("user"));
$event->add_link("Log Out", make_link("user_admin/logout"), 99);
}
2012-02-02 08:07:57 +00:00
public function onUserCreation(UserCreationEvent $event) {
2009-05-30 13:47:57 +00:00
$this->check_user_creation($event);
$this->create_user($event);
}
2012-02-02 08:07:57 +00:00
public function onSearchTermParse(SearchTermParseEvent $event) {
2011-12-25 12:31:28 +00:00
global $user;
2009-05-30 13:47:57 +00:00
$matches = array();
if(preg_match("/^(poster|user)=(.*)$/i", $event->term, $matches)) {
$user = User::by_name($matches[2]);
if(!is_null($user)) {
$user_id = $user->id;
}
2009-05-30 13:47:57 +00:00
else {
$user_id = -1;
}
2009-05-30 13:47:57 +00:00
$event->add_querylet(new Querylet("images.owner_id = $user_id"));
}
else if(preg_match("/^(poster|user)_id=([0-9]+)$/i", $event->term, $matches)) {
$user_id = int_escape($matches[2]);
$event->add_querylet(new Querylet("images.owner_id = $user_id"));
}
2012-02-07 15:15:18 +00:00
else if($user->can("view_ip") && preg_match("/^(poster|user)_ip=([0-9\.]+)$/i", $event->term, $matches)) {
2011-12-25 12:38:07 +00:00
$user_ip = $matches[2]; // FIXME: ip_escape?
2011-12-25 12:31:28 +00:00
$event->add_querylet(new Querylet("images.owner_ip = '$user_ip'"));
}
}
// }}}
// Things done *with* the user {{{
2012-02-02 14:14:33 +00:00
private function login(Page $page) {
global $user;
$name = $_POST['user'];
$pass = $_POST['pass'];
$hash = md5(strtolower($name) . $pass);
$duser = User::by_name_and_hash($name, $hash);
if(!is_null($duser)) {
$user = $duser;
$this->set_login_cookie($name, $pass);
2010-01-05 10:52:23 +00:00
if($user->is_admin()) {
log_warning("user", "Admin logged in");
}
else {
log_info("user", "User logged in");
}
$page->set_mode("redirect");
$page->set_redirect(make_link("user"));
}
else {
2011-01-22 16:10:07 +00:00
log_warning("user", "Failed to log in as ".html_escape($name)." [$hash]");
$this->theme->display_error(401, "Error", "No user with those details was found");
}
}
2012-02-02 14:14:33 +00:00
private function check_user_creation($event) { // FIXME type
$name = $event->username;
$pass = $event->password;
$email = $event->email;
global $database;
if(strlen($name) < 1) {
throw new UserCreationException("Username must be at least 1 character");
}
else if(!preg_match('/^[a-zA-Z0-9-_]+$/', $name)) {
throw new UserCreationException(
"Username contains invalid characters. Allowed characters are ".
"letters, numbers, dash, and underscore");
}
2011-02-21 11:57:50 +00:00
else if($database->get_row("SELECT * FROM users WHERE name = :name", array("name"=>$name))) {
throw new UserCreationException("That username is already taken");
}
}
2012-02-02 14:14:33 +00:00
private function create_user($event) { // FIXME type
global $database;
$hash = md5(strtolower($event->username) . $event->password);
$email = (!empty($event->email)) ? $event->email : null;
2009-01-22 09:39:44 +00:00
// if there are currently no admins, the new user should be one
2012-02-21 18:37:36 +00:00
$need_admin = ($database->get_one("SELECT COUNT(*) FROM users WHERE class='admin'") == 0);
2012-02-22 13:58:55 +00:00
$class = $need_admin ? 'admin' : 'user';
2009-01-22 09:39:44 +00:00
$database->Execute(
2012-02-21 18:37:36 +00:00
"INSERT INTO users (name, pass, joindate, email, class) VALUES (:username, :hash, now(), :email, :class)",
array("username"=>$event->username, "hash"=>$hash, "email"=>$email, "class"=>$class));
2011-01-03 15:18:24 +00:00
$uid = $database->get_last_insert_id();
2009-05-08 10:52:29 +00:00
log_info("user", "Created User #$uid ({$event->username})");
}
2009-01-04 19:18:37 +00:00
2012-02-02 14:14:33 +00:00
private function set_login_cookie(/*string*/ $name, /*string*/ $pass) {
global $config;
$addr = get_session_ip($config);
$hash = md5(strtolower($name) . $pass);
2009-10-08 12:59:12 +00:00
set_prefixed_cookie("user", $name,
time()+60*60*24*365, '/');
2009-10-08 12:59:12 +00:00
set_prefixed_cookie("session", md5($hash.$addr),
time()+60*60*24*$config->get_int('login_memory'), '/');
}
2009-01-04 19:18:37 +00:00
//}}}
// Things done *to* the user {{{
private function user_can_edit_user(User $a, User $b) {
if($a->is_anonymous()) {
$this->theme->display_error(401, "Error", "You aren't logged in");
}
if($a->name == $b->name) {
return true;
}
if($b->can("protected")) {
if($a->class->name == "admin") {
return true;
}
else {
$this->theme->display_error(401, "Error", "You need to be an admin to change other people's details");
}
}
else {
if($a->can("edit_user_info")) {
return true;
}
else {
$this->theme->display_error(401, "Error", "You need to be an admin to change other people's details");
2009-08-11 16:07:03 +00:00
}
}
return false;
2009-08-11 16:07:03 +00:00
}
private function redirect_to_user(User $duser) {
global $page, $user;
2009-08-11 16:07:03 +00:00
if($user->id == $duser->id) {
$page->set_mode("redirect");
$page->set_redirect(make_link("user"));
}
else {
$page->set_mode("redirect");
$page->set_redirect(make_link("user/{$duser->name}"));
2009-08-11 16:07:03 +00:00
}
}
2009-08-11 16:07:03 +00:00
private function change_password_wrapper(User $duser, $pass1, $pass2) {
global $user;
2009-08-11 16:07:03 +00:00
if($this->user_can_edit_user($user, $duser)) {
if($pass1 != $pass2) {
$this->theme->display_error(400, "Error", "Passwords don't match");
2009-08-11 16:07:03 +00:00
}
else {
// FIXME: send_event()
$duser->set_password($pass1);
2009-08-11 16:07:03 +00:00
if($id == $user->id) {
$this->set_login_cookie($duser->name, $pass1);
}
$this->redirect_to_user($duser);
}
}
}
private function change_email_wrapper(User $duser, /*string(email)*/ $address) {
global $user;
2009-01-04 19:18:37 +00:00
if($this->user_can_edit_user($user, $duser)) {
$duser->set_email($address);
$this->redirect_to_user($duser);
}
}
2009-01-04 19:18:37 +00:00
private function change_class_wrapper(User $duser, /*string(class)*/ $class) {
global $user;
if($user->class->name == "admin") {
$duser = User::by_id($_POST['id']);
2012-02-14 20:38:19 +00:00
$duser->set_class($class);
$this->redirect_to_user($duser);
}
}
// }}}
// ips {{{
2012-02-02 14:14:33 +00:00
private function count_upload_ips(User $duser) {
global $database;
2011-01-01 16:28:04 +00:00
$rows = $database->get_pairs("
SELECT
owner_ip,
COUNT(images.id) AS count,
MAX(posted) AS most_recent
FROM images
2011-01-01 16:28:04 +00:00
WHERE owner_id=:id
GROUP BY owner_ip
2011-01-01 16:28:04 +00:00
ORDER BY most_recent DESC", array("id"=>$duser->id));
return $rows;
}
2012-02-02 14:14:33 +00:00
private function count_comment_ips(User $duser) {
global $database;
2011-01-01 16:28:04 +00:00
$rows = $database->get_pairs("
SELECT
owner_ip,
COUNT(comments.id) AS count,
MAX(posted) AS most_recent
FROM comments
2011-01-01 16:28:04 +00:00
WHERE owner_id=:id
GROUP BY owner_ip
2011-01-01 16:28:04 +00:00
ORDER BY most_recent DESC", array("id"=>$duser->id));
return $rows;
}
2012-02-02 14:14:33 +00:00
private function delete_user(Page $page) {
global $user;
global $config;
global $database;
$page->set_title("Error");
$page->set_heading("Error");
$page->add_block(new NavBlock());
2012-02-07 15:15:18 +00:00
if (!$user->can("delete_user")) {
$page->add_block(new Block("Not Admin", "Only admins can delete accounts"));
}
else if(!isset($_POST['id']) || !is_numeric($_POST['id'])) {
$page->add_block(new Block("No ID Specified",
"You need to specify the account number to edit"));
}
else{
$database->Execute(
"UPDATE images SET owner_id = :new_owner_id WHERE owner_id = :old_owner_id",
array("new_owner_id" => $config->get_int('anon_id'), "old_owner_id" => $_POST['id'])
);
$database->execute(
"DELETE FROM users WHERE id = :id",
array("id" => $_POST['id'])
);
$page->set_mode("redirect");
$page->set_redirect(make_link("post/list"));
}
}
2012-02-02 14:14:33 +00:00
private function delete_user_with_images(Page $page) {
global $user;
global $config;
global $database;
$page->set_title("Error");
$page->set_heading("Error");
$page->add_block(new NavBlock());
2012-02-07 15:15:18 +00:00
if (!$user->can("delete_user") || !$user->can("delete_image")) {
$page->add_block(new Block("Not Admin", "Only admins can delete accounts"));
}
else if(!isset($_POST['id']) || !is_numeric($_POST['id'])) {
$page->add_block(new Block("No ID Specified",
"You need to specify the account number to edit"));
}
else{
$rows = $database->get_all("SELECT * FROM images WHERE owner_id = :owner_id", array("owner_id" => $_POST['id']));
foreach ($rows as $key => $value)
{
$image = Image::by_id($value['id']);
if($image) {
send_event(new ImageDeletionEvent($image));
}
}
$database->execute("DELETE FROM users
WHERE id = :id"
, array("id"=>$_POST['id']));
$page->set_mode("redirect");
$page->set_redirect(make_link("post/list"));
}
}
// }}}
}
?>