2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2010-01-05 10:11:53 +00:00
|
|
|
|
2019-08-07 19:53:59 +00:00
|
|
|
require_once "events.php";
|
2007-11-04 03:51:41 +00:00
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
use MicroHTML\HTMLElement;
|
2019-12-26 00:36:32 +00:00
|
|
|
use MicroCRUD\ActionColumn;
|
2019-12-01 00:46:54 +00:00
|
|
|
use MicroCRUD\EnumColumn;
|
2020-03-09 23:54:00 +00:00
|
|
|
use MicroCRUD\IntegerColumn;
|
2019-12-01 00:46:54 +00:00
|
|
|
use MicroCRUD\TextColumn;
|
2019-12-16 09:00:41 +00:00
|
|
|
use MicroCRUD\DateColumn;
|
2019-12-01 00:46:54 +00:00
|
|
|
use MicroCRUD\Table;
|
2022-10-27 16:21:46 +00:00
|
|
|
|
2021-12-14 18:32:47 +00:00
|
|
|
use function MicroHTML\A;
|
2019-12-01 00:46:54 +00:00
|
|
|
|
2019-12-15 15:31:44 +00:00
|
|
|
class UserNameColumn extends TextColumn
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
public function display(array $row): HTMLElement
|
2019-12-15 15:31:44 +00:00
|
|
|
{
|
2019-12-01 00:46:54 +00:00
|
|
|
return A(["href"=>make_link("user/{$row[$this->name]}")], $row[$this->name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 00:36:32 +00:00
|
|
|
class UserActionColumn extends ActionColumn
|
2019-12-15 15:31:44 +00:00
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
parent::__construct("id");
|
2019-12-01 00:46:54 +00:00
|
|
|
$this->sortable = false;
|
|
|
|
}
|
2019-12-26 00:36:32 +00:00
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
public function display(array $row): HTMLElement
|
2019-12-15 15:31:44 +00:00
|
|
|
{
|
2019-12-15 15:40:15 +00:00
|
|
|
return A(["href"=>make_link("post/list/user={$row['name']}/1")], "Posts");
|
2019-12-01 00:46:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UserTable extends Table
|
|
|
|
{
|
|
|
|
public function __construct(\FFSPHP\PDO $db)
|
|
|
|
{
|
|
|
|
global $_shm_user_classes;
|
|
|
|
$classes = [];
|
2019-12-15 15:31:44 +00:00
|
|
|
foreach ($_shm_user_classes as $cls) {
|
2019-12-01 00:46:54 +00:00
|
|
|
$classes[$cls->name] = $cls->name;
|
|
|
|
}
|
|
|
|
ksort($classes);
|
|
|
|
parent::__construct($db);
|
|
|
|
$this->table = "users";
|
|
|
|
$this->base_query = "SELECT * FROM users";
|
|
|
|
$this->size = 100;
|
|
|
|
$this->limit = 1000000;
|
2019-12-26 00:36:32 +00:00
|
|
|
$this->set_columns([
|
2020-03-09 23:54:00 +00:00
|
|
|
new IntegerColumn("id", "ID"),
|
2019-12-01 00:46:54 +00:00
|
|
|
new UserNameColumn("name", "Name"),
|
|
|
|
new EnumColumn("class", "Class", $classes),
|
|
|
|
// Added later, for admins only
|
|
|
|
// new TextColumn("email", "Email"),
|
2019-12-16 09:00:41 +00:00
|
|
|
new DateColumn("joindate", "Join Date"),
|
2019-12-26 00:36:32 +00:00
|
|
|
new UserActionColumn(),
|
|
|
|
]);
|
2020-03-09 23:51:01 +00:00
|
|
|
$this->order_by = ["id DESC"];
|
2019-12-01 00:46:54 +00:00
|
|
|
$this->table_attrs = ["class" => "zebra"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class UserCreationException extends SCoreException
|
|
|
|
{
|
|
|
|
}
|
2012-03-25 01:41:33 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class NullUserException extends SCoreException
|
|
|
|
{
|
2012-03-25 01:41:33 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class UserPage extends Extension
|
|
|
|
{
|
|
|
|
/** @var UserPageTheme $theme */
|
2021-03-14 23:43:50 +00:00
|
|
|
public ?Themelet $theme;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
public function onInitExt(InitExtEvent $event)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$config->set_default_bool("login_signup_enabled", true);
|
|
|
|
$config->set_default_int("login_memory", 365);
|
|
|
|
$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");
|
|
|
|
$config->set_default_bool("login_tac_bbcode", true);
|
|
|
|
}
|
|
|
|
|
2019-07-04 17:48:33 +00:00
|
|
|
public function onUserLogin(UserLoginEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
$user = $event->user;
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
|
|
|
global $config, $database, $page, $user;
|
|
|
|
|
|
|
|
$this->show_user_info();
|
|
|
|
|
|
|
|
if ($event->page_matches("user_admin")) {
|
|
|
|
if ($event->get_arg(0) == "login") {
|
|
|
|
if (isset($_POST['user']) && isset($_POST['pass'])) {
|
|
|
|
$this->page_login($_POST['user'], $_POST['pass']);
|
|
|
|
} else {
|
|
|
|
$this->theme->display_login_page($page);
|
|
|
|
}
|
|
|
|
} elseif ($event->get_arg(0) == "recover") {
|
|
|
|
$this->page_recover($_POST['username']);
|
|
|
|
} elseif ($event->get_arg(0) == "create") {
|
|
|
|
$this->page_create();
|
2020-05-19 18:33:51 +00:00
|
|
|
} elseif ($event->get_arg(0) == "create_other") {
|
|
|
|
$uce = new UserCreationEvent($_POST['name'], $_POST['pass1'], $_POST['email'], false);
|
|
|
|
send_event($uce);
|
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(make_link("admin"));
|
|
|
|
$page->flash("Created new user");
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif ($event->get_arg(0) == "list") {
|
2019-12-01 00:46:54 +00:00
|
|
|
$t = new UserTable($database->raw_db());
|
|
|
|
$t->token = $user->get_auth_token();
|
|
|
|
$t->inputs = $_GET;
|
|
|
|
if ($user->can(Permissions::DELETE_USER)) {
|
|
|
|
$col = new TextColumn("email", "Email");
|
|
|
|
// $t->columns[] = $col;
|
|
|
|
array_splice($t->columns, 2, 0, [$col]);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-12-01 00:46:54 +00:00
|
|
|
$this->theme->display_user_list($page, $t->table($t->query()), $t->paginator());
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif ($event->get_arg(0) == "logout") {
|
|
|
|
$this->page_logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$user->check_auth_token()) {
|
|
|
|
return;
|
|
|
|
} elseif ($event->get_arg(0) == "change_name") {
|
|
|
|
$input = validate_input([
|
|
|
|
'id' => 'user_id,exists',
|
|
|
|
'name' => 'user_name',
|
|
|
|
]);
|
|
|
|
$duser = User::by_id($input['id']);
|
|
|
|
$this->change_name_wrapper($duser, $input['name']);
|
|
|
|
} elseif ($event->get_arg(0) == "change_pass") {
|
|
|
|
$input = validate_input([
|
|
|
|
'id' => 'user_id,exists',
|
|
|
|
'pass1' => 'password',
|
|
|
|
'pass2' => 'password',
|
|
|
|
]);
|
|
|
|
$duser = User::by_id($input['id']);
|
|
|
|
$this->change_password_wrapper($duser, $input['pass1'], $input['pass2']);
|
|
|
|
} elseif ($event->get_arg(0) == "change_email") {
|
|
|
|
$input = validate_input([
|
|
|
|
'id' => 'user_id,exists',
|
|
|
|
'address' => 'email',
|
|
|
|
]);
|
|
|
|
$duser = User::by_id($input['id']);
|
|
|
|
$this->change_email_wrapper($duser, $input['address']);
|
|
|
|
} elseif ($event->get_arg(0) == "change_class") {
|
|
|
|
$input = validate_input([
|
|
|
|
'id' => 'user_id,exists',
|
|
|
|
'class' => 'user_class',
|
|
|
|
]);
|
|
|
|
$duser = User::by_id($input['id']);
|
|
|
|
$this->change_class_wrapper($duser, $input['class']);
|
|
|
|
} elseif ($event->get_arg(0) == "delete_user") {
|
|
|
|
$this->delete_user($page, isset($_POST["with_images"]), isset($_POST["with_comments"]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($event->page_matches("user")) {
|
|
|
|
$display_user = ($event->count_args() == 0) ? $user : User::by_name($event->get_arg(0));
|
|
|
|
if ($event->count_args() == 0 && $user->is_anonymous()) {
|
|
|
|
$this->theme->display_error(
|
|
|
|
401,
|
|
|
|
"Not Logged In",
|
|
|
|
"You aren't logged in. First do that, then you can see your stats."
|
|
|
|
);
|
|
|
|
} elseif (!is_null($display_user) && ($display_user->id != $config->get_int("anon_id"))) {
|
|
|
|
$e = new UserPageBuildingEvent($display_user);
|
|
|
|
send_event($e);
|
|
|
|
$this->display_stats($e);
|
|
|
|
} 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..."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onUserPageBuilding(UserPageBuildingEvent $event)
|
|
|
|
{
|
|
|
|
global $user, $config;
|
|
|
|
|
|
|
|
$h_join_date = autodate($event->display_user->join_date);
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($event->display_user->can(Permissions::HELLBANNED)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$h_class = $event->display_user->class->parent->name;
|
|
|
|
} else {
|
|
|
|
$h_class = $event->display_user->class->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
$event->add_stats("Joined: $h_join_date", 10);
|
2019-12-15 15:31:44 +00:00
|
|
|
if ($user->name == $event->display_user->name) {
|
2022-01-17 17:06:20 +00:00
|
|
|
$event->add_stats("Current IP: " . get_real_ip(), 80);
|
2019-11-30 05:07:31 +00:00
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
$event->add_stats("Class: $h_class", 90);
|
|
|
|
|
|
|
|
$av = $event->display_user->get_avatar_html();
|
|
|
|
if ($av) {
|
|
|
|
$event->add_stats($av, 0);
|
|
|
|
} elseif ((
|
|
|
|
$config->get_string("avatar_host") == "gravatar"
|
|
|
|
) &&
|
|
|
|
($user->id == $event->display_user->id)
|
|
|
|
) {
|
|
|
|
$event->add_stats(
|
|
|
|
"No avatar? This gallery uses <a href='https://gravatar.com'>Gravatar</a> for avatar hosting, use the".
|
|
|
|
"<br>same email address here and there to have your avatar synced<br>",
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:54:48 +00:00
|
|
|
public function onPageNavBuilding(PageNavBuildingEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
if ($user->is_anonymous()) {
|
|
|
|
$event->add_nav_link("user", new Link('user_admin/login'), "Account", null, 10);
|
|
|
|
} else {
|
|
|
|
$event->add_nav_link("user", new Link('user'), "Account", null, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function display_stats(UserPageBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $user, $page, $config;
|
|
|
|
|
|
|
|
ksort($event->stats);
|
|
|
|
$this->theme->display_user_page($event->display_user, $event->stats);
|
2019-06-27 13:12:15 +00:00
|
|
|
|
|
|
|
if (!$user->is_anonymous()) {
|
|
|
|
if ($user->id == $event->display_user->id || $user->can("edit_user_info")) {
|
2020-10-26 15:13:28 +00:00
|
|
|
$user_config = UserConfig::get_for_user($event->display_user->id);
|
2019-06-27 13:12:15 +00:00
|
|
|
|
2020-10-26 15:13:28 +00:00
|
|
|
$uobe = new UserOperationsBuildingEvent($event->display_user, $user_config);
|
|
|
|
send_event($uobe);
|
|
|
|
$page->add_block(new Block("Operations", $this->theme->build_operations($event->display_user, $uobe), "main", 60));
|
2019-06-27 13:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +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 (
|
2019-07-09 14:10:21 +00:00
|
|
|
($user->can(Permissions::VIEW_IP) || ($user->is_logged_in() && $user->id == $event->display_user->id)) && # admin or self-user
|
2019-05-28 16:59:38 +00:00
|
|
|
($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),
|
|
|
|
$this->count_log_ips($event->display_user)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$hosts = [
|
|
|
|
"None" => "none",
|
|
|
|
"Gravatar" => "gravatar"
|
|
|
|
];
|
|
|
|
|
2020-10-26 15:13:28 +00:00
|
|
|
$sb = $event->panel->create_new_block("User Options");
|
2020-06-17 00:03:03 +00:00
|
|
|
$sb->start_table();
|
|
|
|
$sb->add_bool_option(UserConfig::ENABLE_API_KEYS, "Enable user API keys", true);
|
|
|
|
$sb->add_bool_option("login_signup_enabled", "Allow new signups", true);
|
|
|
|
$sb->add_longtext_option("login_tac", "Terms & Conditions", true);
|
|
|
|
$sb->add_choice_option(
|
|
|
|
"user_loginshowprofile",
|
|
|
|
[
|
|
|
|
"return to previous page" => 0, // 0 is default
|
|
|
|
"send to user profile" => 1],
|
|
|
|
"On log in/out",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$sb->add_choice_option("avatar_host", $hosts, "Avatars", true);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
if ($config->get_string("avatar_host") == "gravatar") {
|
2020-06-17 00:03:03 +00:00
|
|
|
$sb->start_table_row();
|
|
|
|
$sb->start_table_cell(2);
|
2021-03-14 23:43:50 +00:00
|
|
|
$sb->add_label("<div style='text-align: center'><b>Gravatar Options</b></div>");
|
2020-06-17 00:03:03 +00:00
|
|
|
$sb->end_table_cell();
|
|
|
|
$sb->end_table_row();
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_choice_option(
|
|
|
|
"avatar_gravatar_type",
|
|
|
|
[
|
|
|
|
'Default'=>'default',
|
|
|
|
'Wavatar'=>'wavatar',
|
|
|
|
'Monster ID'=>'monsterid',
|
|
|
|
'Identicon'=>'identicon'
|
|
|
|
],
|
2020-06-17 00:03:03 +00:00
|
|
|
"Type",
|
|
|
|
true
|
2019-05-28 16:59:38 +00:00
|
|
|
);
|
|
|
|
$sb->add_choice_option(
|
|
|
|
"avatar_gravatar_rating",
|
|
|
|
['G'=>'g', 'PG'=>'pg', 'R'=>'r', 'X'=>'x'],
|
2020-06-17 00:03:03 +00:00
|
|
|
"Rating",
|
|
|
|
true
|
2019-05-28 16:59:38 +00:00
|
|
|
);
|
|
|
|
}
|
2020-07-06 16:56:55 +00:00
|
|
|
$sb->end_table();
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 19:54:48 +00:00
|
|
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
2019-09-29 13:30:55 +00:00
|
|
|
if ($event->parent==="system") {
|
2020-03-13 10:08:23 +00:00
|
|
|
if ($user->can(Permissions::EDIT_USER_PASSWORD)) {
|
2019-08-02 19:54:48 +00:00
|
|
|
$event->add_nav_link("user_admin", new Link('user_admin/list'), "User List", NavLink::is_active(["user_admin"]));
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 15:36:10 +00:00
|
|
|
|
|
|
|
if ($event->parent==="user" && !$user->is_anonymous()) {
|
|
|
|
$event->add_nav_link("logout", new Link('user_admin/logout'), "Log Out", false, 90);
|
|
|
|
}
|
2019-08-02 19:54:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onUserBlockBuilding(UserBlockBuildingEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
$event->add_link("My Profile", make_link("user"));
|
2020-03-13 10:08:23 +00:00
|
|
|
if ($user->can(Permissions::EDIT_USER_PASSWORD)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$event->add_link("User List", make_link("user_admin/list"), 98);
|
|
|
|
}
|
|
|
|
$event->add_link("Log Out", make_link("user_admin/logout"), 99);
|
|
|
|
}
|
|
|
|
|
2020-05-19 18:33:51 +00:00
|
|
|
public function onAdminBuilding(AdminBuildingEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
if ($user->can(Permissions::CREATE_OTHER_USER)) {
|
|
|
|
$this->theme->display_user_creator();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onUserCreation(UserCreationEvent $event)
|
|
|
|
{
|
|
|
|
$this->check_user_creation($event);
|
2021-06-11 22:47:59 +00:00
|
|
|
$new_user = $this->create_user($event);
|
2020-05-19 18:33:51 +00:00
|
|
|
if ($event->login) {
|
2021-06-11 22:47:59 +00:00
|
|
|
send_event(new UserLoginEvent($new_user));
|
2020-05-19 18:33:51 +00:00
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 15:13:28 +00:00
|
|
|
public const USER_SEARCH_REGEX = "/^(?:poster|user)(!?)[=|:](.*)$/i";
|
|
|
|
public const USER_ID_SEARCH_REGEX = "/^(?:poster|user)_id(!?)[=|:]([0-9]+)$/i";
|
2020-06-02 23:08:24 +00:00
|
|
|
|
|
|
|
public static function has_user_query(array $context): bool
|
|
|
|
{
|
|
|
|
foreach ($context as $term) {
|
|
|
|
if (preg_match(self::USER_SEARCH_REGEX, $term)||
|
|
|
|
preg_match(self::USER_ID_SEARCH_REGEX, $term)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onSearchTermParse(SearchTermParseEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
|
2020-01-26 16:38:26 +00:00
|
|
|
if (is_null($event->term)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-26 13:19:35 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$matches = [];
|
2020-06-02 23:08:24 +00:00
|
|
|
if (preg_match(self::USER_SEARCH_REGEX, $event->term, $matches)) {
|
2021-11-10 19:46:12 +00:00
|
|
|
$duser = User::by_name($matches[2]);
|
2021-11-10 19:42:01 +00:00
|
|
|
if (is_null($duser)) {
|
|
|
|
throw new SearchTermParseException(
|
2021-11-10 19:46:12 +00:00
|
|
|
"Can't find the user named ".html_escape($matches[2])
|
2021-11-10 19:42:01 +00:00
|
|
|
);
|
2021-11-10 19:33:51 +00:00
|
|
|
}
|
2021-11-10 19:42:01 +00:00
|
|
|
$event->add_querylet(new Querylet("images.owner_id ${matches[1]}= {$duser->id}"));
|
2020-06-02 23:08:24 +00:00
|
|
|
} elseif (preg_match(self::USER_ID_SEARCH_REGEX, $event->term, $matches)) {
|
2020-10-26 15:13:28 +00:00
|
|
|
$user_id = int_escape($matches[2]);
|
|
|
|
$event->add_querylet(new Querylet("images.owner_id ${matches[1]}= $user_id"));
|
2019-07-09 14:10:21 +00:00
|
|
|
} elseif ($user->can(Permissions::VIEW_IP) && preg_match("/^(?:poster|user)_ip[=|:]([0-9\.]+)$/i", $event->term, $matches)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$user_ip = $matches[1]; // FIXME: ip_escape?
|
|
|
|
$event->add_querylet(new Querylet("images.owner_ip = '$user_ip'"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 20:05:49 +00:00
|
|
|
public function onHelpPageBuilding(HelpPageBuildingEvent $event)
|
|
|
|
{
|
2019-09-29 13:30:55 +00:00
|
|
|
if ($event->key===HelpPages::SEARCH) {
|
2019-08-02 20:05:49 +00:00
|
|
|
$block = new Block();
|
|
|
|
$block->header = "Users";
|
2021-03-14 23:43:50 +00:00
|
|
|
$block->body = (string)$this->theme->get_help_html();
|
2019-08-02 20:05:49 +00:00
|
|
|
$event->add_block($block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function show_user_info(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $user, $page;
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
2019-11-03 16:22:59 +00:00
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function page_login(string $name, string $pass): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-10-02 10:23:57 +00:00
|
|
|
global $config, $page;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
if (empty($name) || empty($pass)) {
|
|
|
|
$this->theme->display_error(400, "Error", "Username or password left blank");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$duser = User::by_name_and_pass($name, $pass);
|
|
|
|
if (!is_null($duser)) {
|
2019-07-04 17:48:33 +00:00
|
|
|
send_event(new UserLoginEvent($duser));
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->set_login_cookie($duser->name, $pass);
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
// Try returning to previous page
|
2020-03-27 20:24:26 +00:00
|
|
|
if ($config->get_int("user_loginshowprofile", 0)) {
|
|
|
|
$page->set_redirect(referer_or(make_link(), ["user/"]));
|
2019-05-28 16:59:38 +00:00
|
|
|
} else {
|
|
|
|
$page->set_redirect(make_link("user"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->theme->display_error(401, "Error", "No user with those details was found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function page_logout(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $page, $config;
|
|
|
|
$page->add_cookie("session", "", time() + 60 * 60 * 24 * $config->get_int('login_memory'), "/");
|
2020-02-09 16:25:17 +00:00
|
|
|
if (SPEED_HAX) {
|
2019-05-28 16:59:38 +00:00
|
|
|
# to keep as few versions of content as possible,
|
|
|
|
# make cookies all-or-nothing
|
|
|
|
$page->add_cookie("user", "", time() + 60 * 60 * 24 * $config->get_int('login_memory'), "/");
|
|
|
|
}
|
|
|
|
log_info("user", "Logged out");
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
// Try forwarding to same page on logout unless user comes from registration page
|
2020-03-27 20:24:26 +00:00
|
|
|
if ($config->get_int("user_loginshowprofile", 0)) {
|
|
|
|
$page->set_redirect(referer_or(make_link(), ["post/"]));
|
2019-05-28 16:59:38 +00:00
|
|
|
} else {
|
|
|
|
$page->set_redirect(make_link());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function page_recover(string $username): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-11-11 16:24:13 +00:00
|
|
|
$my_user = User::by_name($username);
|
|
|
|
if (is_null($my_user)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->theme->display_error(404, "Error", "There's no user with that name");
|
2019-11-11 16:24:13 +00:00
|
|
|
} elseif (is_null($my_user->email)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->theme->display_error(400, "Error", "That user has no registered email address");
|
|
|
|
} else {
|
2020-01-26 16:38:13 +00:00
|
|
|
throw new SCoreException("Email sending not implemented");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function page_create(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-11-28 21:32:18 +00:00
|
|
|
global $config, $page, $user;
|
2019-12-26 17:08:29 +00:00
|
|
|
if (!$user->can(Permissions::CREATE_USER)) {
|
2019-11-28 21:32:18 +00:00
|
|
|
$this->theme->display_error(403, "Account creation blocked", "Account creation is currently disabled");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if (!$config->get_bool("login_signup_enabled")) {
|
|
|
|
$this->theme->display_signups_disabled($page);
|
|
|
|
} elseif (!isset($_POST['name'])) {
|
|
|
|
$this->theme->display_signup_page($page);
|
|
|
|
} elseif ($_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");
|
|
|
|
}
|
|
|
|
|
2020-05-19 18:33:51 +00:00
|
|
|
$uce = new UserCreationEvent($_POST['name'], $_POST['pass1'], $_POST['email'], true);
|
2019-05-28 16:59:38 +00:00
|
|
|
send_event($uce);
|
|
|
|
$this->set_login_cookie($uce->username, $uce->password);
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_redirect(make_link("user"));
|
|
|
|
} catch (UserCreationException $ex) {
|
|
|
|
$this->theme->display_error(400, "User Creation Error", $ex->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function check_user_creation(UserCreationEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$name = $event->username;
|
|
|
|
//$pass = $event->password;
|
|
|
|
//$email = $event->email;
|
|
|
|
|
|
|
|
if (strlen($name) < 1) {
|
|
|
|
throw new UserCreationException("Username must be at least 1 character");
|
|
|
|
} elseif (!preg_match('/^[a-zA-Z0-9-_]+$/', $name)) {
|
|
|
|
throw new UserCreationException(
|
|
|
|
"Username contains invalid characters. Allowed characters are ".
|
|
|
|
"letters, numbers, dash, and underscore"
|
|
|
|
);
|
|
|
|
} elseif (User::by_name($name)) {
|
|
|
|
throw new UserCreationException("That username is already taken");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 18:33:51 +00:00
|
|
|
private function create_user(UserCreationEvent $event): User
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2021-06-11 22:47:59 +00:00
|
|
|
global $database;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$email = (!empty($event->email)) ? $event->email : null;
|
|
|
|
|
|
|
|
// if there are currently no admins, the new user should be one
|
|
|
|
$need_admin = ($database->get_one("SELECT COUNT(*) FROM users WHERE class='admin'") == 0);
|
|
|
|
$class = $need_admin ? 'admin' : 'user';
|
|
|
|
|
2020-10-25 21:34:52 +00:00
|
|
|
$database->execute(
|
2019-05-28 16:59:38 +00:00
|
|
|
"INSERT INTO users (name, pass, joindate, email, class) VALUES (:username, :hash, now(), :email, :class)",
|
|
|
|
["username"=>$event->username, "hash"=>'', "email"=>$email, "class"=>$class]
|
|
|
|
);
|
|
|
|
$uid = $database->get_last_insert_id('users_id_seq');
|
2021-06-11 22:47:59 +00:00
|
|
|
$new_user = User::by_name($event->username);
|
|
|
|
$new_user->set_password($event->password);
|
2019-07-04 17:48:33 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
log_info("user", "Created User #$uid ({$event->username})");
|
2020-05-19 18:33:51 +00:00
|
|
|
|
2021-06-11 22:47:59 +00:00
|
|
|
return $new_user;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function set_login_cookie(string $name, string $pass): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config, $page;
|
|
|
|
|
|
|
|
$addr = get_session_ip($config);
|
|
|
|
$hash = User::by_name($name)->passhash;
|
|
|
|
|
|
|
|
$page->add_cookie(
|
|
|
|
"user",
|
|
|
|
$name,
|
|
|
|
time()+60*60*24*365,
|
|
|
|
'/'
|
|
|
|
);
|
|
|
|
$page->add_cookie(
|
|
|
|
"session",
|
|
|
|
md5($hash.$addr),
|
|
|
|
time()+60*60*24*$config->get_int('login_memory'),
|
|
|
|
'/'
|
|
|
|
);
|
|
|
|
}
|
2019-11-03 16:22:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
private function user_can_edit_user(User $a, User $b): bool
|
|
|
|
{
|
|
|
|
if ($a->is_anonymous()) {
|
|
|
|
$this->theme->display_error(401, "Error", "You aren't logged in");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
($a->name == $b->name) ||
|
2019-07-09 14:10:21 +00:00
|
|
|
($b->can(Permissions::PROTECTED) && $a->class->name == "admin") ||
|
|
|
|
(!$b->can(Permissions::PROTECTED) && $a->can(Permissions::EDIT_USER_INFO))
|
2019-05-28 16:59:38 +00:00
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
$this->theme->display_error(401, "Error", "You need to be an admin to change other people's details");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function redirect_to_user(User $duser): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $page, $user;
|
|
|
|
|
|
|
|
if ($user->id == $duser->id) {
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_redirect(make_link("user"));
|
|
|
|
} else {
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_redirect(make_link("user/{$duser->name}"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function change_name_wrapper(User $duser, string $name): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-12-15 19:47:18 +00:00
|
|
|
global $page, $user;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::EDIT_USER_NAME) && $this->user_can_edit_user($user, $duser)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$duser->set_name($name);
|
2019-12-15 19:47:18 +00:00
|
|
|
$page->flash("Username changed");
|
2019-05-28 16:59:38 +00:00
|
|
|
// TODO: set login cookie if user changed themselves
|
|
|
|
$this->redirect_to_user($duser);
|
|
|
|
} else {
|
|
|
|
$this->theme->display_error(400, "Error", "Permission denied");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function change_password_wrapper(User $duser, string $pass1, string $pass2): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-12-15 19:47:18 +00:00
|
|
|
global $page, $user;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
if ($this->user_can_edit_user($user, $duser)) {
|
|
|
|
if ($pass1 != $pass2) {
|
|
|
|
$this->theme->display_error(400, "Error", "Passwords don't match");
|
|
|
|
} else {
|
|
|
|
// FIXME: send_event()
|
|
|
|
$duser->set_password($pass1);
|
|
|
|
|
|
|
|
if ($duser->id == $user->id) {
|
|
|
|
$this->set_login_cookie($duser->name, $pass1);
|
|
|
|
}
|
|
|
|
|
2019-12-15 19:47:18 +00:00
|
|
|
$page->flash("Password changed");
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->redirect_to_user($duser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function change_email_wrapper(User $duser, string $address): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-12-15 19:47:18 +00:00
|
|
|
global $page, $user;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
if ($this->user_can_edit_user($user, $duser)) {
|
|
|
|
$duser->set_email($address);
|
|
|
|
|
2019-12-15 19:47:18 +00:00
|
|
|
$page->flash("Email changed");
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->redirect_to_user($duser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function change_class_wrapper(User $duser, string $class): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-12-15 19:47:18 +00:00
|
|
|
global $page, $user;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
if ($user->class->name == "admin") {
|
|
|
|
$duser->set_class($class);
|
2019-12-15 19:47:18 +00:00
|
|
|
$page->flash("Class changed");
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->redirect_to_user($duser);
|
|
|
|
}
|
|
|
|
}
|
2019-11-03 16:22:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
private function count_upload_ips(User $duser): array
|
|
|
|
{
|
|
|
|
global $database;
|
2020-01-26 13:19:35 +00:00
|
|
|
return $database->get_pairs("
|
2007-07-27 02:31:15 +00:00
|
|
|
SELECT
|
|
|
|
owner_ip,
|
2019-07-07 21:59:22 +00:00
|
|
|
COUNT(images.id) AS count
|
2007-06-03 10:40:00 +00:00
|
|
|
FROM images
|
2011-01-01 16:28:04 +00:00
|
|
|
WHERE owner_id=:id
|
2007-06-03 10:40:00 +00:00
|
|
|
GROUP BY owner_ip
|
2019-07-07 21:59:22 +00:00
|
|
|
ORDER BY max(posted) DESC", ["id"=>$duser->id]);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function count_comment_ips(User $duser): array
|
|
|
|
{
|
|
|
|
global $database;
|
2020-01-26 13:19:35 +00:00
|
|
|
return $database->get_pairs("
|
2007-07-27 02:31:15 +00:00
|
|
|
SELECT
|
|
|
|
owner_ip,
|
2019-07-07 21:59:22 +00:00
|
|
|
COUNT(comments.id) AS count
|
2007-06-03 10:40:00 +00:00
|
|
|
FROM comments
|
2011-01-01 16:28:04 +00:00
|
|
|
WHERE owner_id=:id
|
2007-06-03 10:40:00 +00:00
|
|
|
GROUP BY owner_ip
|
2019-07-07 21:59:22 +00:00
|
|
|
ORDER BY max(posted) DESC", ["id"=>$duser->id]);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function count_log_ips(User $duser): array
|
|
|
|
{
|
|
|
|
if (!class_exists('LogDatabase')) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
global $database;
|
2020-01-26 13:19:35 +00:00
|
|
|
return $database->get_pairs("
|
2019-02-22 21:04:09 +00:00
|
|
|
SELECT
|
|
|
|
address,
|
2019-07-07 15:02:33 +00:00
|
|
|
COUNT(id) AS count
|
2019-02-22 21:04:09 +00:00
|
|
|
FROM score_log
|
|
|
|
WHERE username=:username
|
|
|
|
GROUP BY address
|
2019-07-07 15:02:33 +00:00
|
|
|
ORDER BY MAX(date_sent) DESC", ["username"=>$duser->name]);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function delete_user(Page $page, bool $with_images=false, bool $with_comments=false): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $user, $config, $database;
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_title("Error");
|
|
|
|
$page->set_heading("Error");
|
|
|
|
$page->add_block(new NavBlock());
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-07-09 14:10:21 +00:00
|
|
|
if (!$user->can(Permissions::DELETE_USER)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->add_block(new Block("Not Admin", "Only admins can delete accounts"));
|
|
|
|
} elseif (!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 {
|
2021-02-12 20:06:41 +00:00
|
|
|
$uid = int_escape($_POST['id']);
|
|
|
|
$duser = User::by_id($uid);
|
|
|
|
log_warning("user", "Deleting user #{$uid} (@{$duser->name})");
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
if ($with_images) {
|
2021-02-12 20:06:41 +00:00
|
|
|
log_warning("user", "Deleting user #{$_POST['id']} (@{$duser->name})'s uploads");
|
2021-12-13 01:27:49 +00:00
|
|
|
$image_ids = $database->get_col("SELECT id FROM images WHERE owner_id = :owner_id", ["owner_id" => $_POST['id']]);
|
|
|
|
foreach ($image_ids as $image_id) {
|
|
|
|
$image = Image::by_id((int)$image_id);
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($image) {
|
|
|
|
send_event(new ImageDeletionEvent($image));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-10-25 21:34:52 +00:00
|
|
|
$database->execute(
|
2019-05-28 16:59:38 +00:00
|
|
|
"UPDATE images SET owner_id = :new_owner_id WHERE owner_id = :old_owner_id",
|
|
|
|
["new_owner_id" => $config->get_int('anon_id'), "old_owner_id" => $_POST['id']]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($with_comments) {
|
2021-02-12 20:06:41 +00:00
|
|
|
log_warning("user", "Deleting user #{$_POST['id']} (@{$duser->name})'s comments");
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("DELETE FROM comments WHERE owner_id = :owner_id", ["owner_id" => $_POST['id']]);
|
|
|
|
} else {
|
2020-10-25 21:34:52 +00:00
|
|
|
$database->execute(
|
2019-05-28 16:59:38 +00:00
|
|
|
"UPDATE comments SET owner_id = :new_owner_id WHERE owner_id = :old_owner_id",
|
|
|
|
["new_owner_id" => $config->get_int('anon_id'), "old_owner_id" => $_POST['id']]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-12 20:06:41 +00:00
|
|
|
send_event(new UserDeletionEvent($uid));
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$database->execute(
|
|
|
|
"DELETE FROM users WHERE id = :id",
|
|
|
|
["id" => $_POST['id']]
|
|
|
|
);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_redirect(make_link("post/list"));
|
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|