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/admin/main.php

154 lines
4.6 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
2010-01-05 10:11:53 +00:00
namespace Shimmie2;
2009-07-21 03:18:40 +00:00
/**
* Sent when the admin page is ready to be added to
*/
class AdminBuildingEvent extends Event
{
public Page $page;
public function __construct(Page $page)
{
2020-01-26 13:19:35 +00:00
parent::__construct();
$this->page = $page;
}
}
class AdminActionEvent extends Event
{
public string $action;
public bool $redirect = true;
2014-04-27 23:29:36 +00:00
public function __construct(string $action)
{
2020-01-26 13:19:35 +00:00
parent::__construct();
$this->action = $action;
}
2012-03-10 12:57:13 +00:00
}
class AdminPage extends Extension
{
2020-01-26 13:19:35 +00:00
/** @var AdminPageTheme */
2023-06-27 14:56:49 +00:00
protected Themelet $theme;
2020-01-26 13:19:35 +00:00
public function onPageRequest(PageRequestEvent $event)
{
2023-06-25 13:19:02 +00:00
global $database, $page, $user;
if ($event->page_matches("admin")) {
2019-07-09 14:10:21 +00:00
if (!$user->can(Permissions::MANAGE_ADMINTOOLS)) {
$this->theme->display_permission_denied();
} else {
if ($event->count_args() == 0) {
send_event(new AdminBuildingEvent($page));
} else {
$action = $event->get_arg(0);
$aae = new AdminActionEvent($action);
if ($user->check_auth_token()) {
log_info("admin", "Util: $action");
2023-06-25 13:19:02 +00:00
shm_set_timeout(null);
$database->set_timeout(null);
send_event($aae);
}
if ($aae->redirect) {
2019-06-19 01:58:28 +00:00
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("admin"));
}
}
}
}
}
public function onCommand(CommandEvent $event)
{
if ($event->cmd == "help") {
2019-10-04 19:48:21 +00:00
print "\tget-page <query string>\n";
print "\t\teg 'get-page post/list'\n\n";
2019-11-27 16:10:12 +00:00
print "\tpost-page <query string> <urlencoded params>\n";
print "\t\teg 'post-page ip_ban/delete id=1'\n\n";
print "\tget-token\n";
print "\t\tget a CSRF auth token\n\n";
2019-10-04 19:48:21 +00:00
print "\tregen-thumb <id / hash>\n";
print "\t\tregenerate a thumbnail\n\n";
2020-01-30 21:05:59 +00:00
print "\tcache [get|set|del] [key] <value>\n";
print "\t\teg 'cache get config'\n\n";
}
if ($event->cmd == "get-page") {
global $page;
2020-10-26 17:33:40 +00:00
$_SERVER['REQUEST_URI'] = $event->args[0];
2019-12-15 15:31:44 +00:00
if (isset($event->args[1])) {
parse_str($event->args[1], $_GET);
2020-10-26 17:33:40 +00:00
$_SERVER['REQUEST_URI'] .= "?" . $event->args[1];
2019-12-15 15:31:44 +00:00
}
send_event(new PageRequestEvent("GET", $event->args[0]));
$page->display();
}
2019-11-27 16:10:12 +00:00
if ($event->cmd == "post-page") {
global $page;
2019-12-15 15:31:44 +00:00
if (isset($event->args[1])) {
parse_str($event->args[1], $_POST);
}
send_event(new PageRequestEvent("POST", $event->args[0]));
2019-11-27 16:10:12 +00:00
$page->display();
}
if ($event->cmd == "get-token") {
global $user;
print($user->get_auth_token());
}
if ($event->cmd == "regen-thumb") {
2019-10-04 19:48:21 +00:00
$uid = $event->args[0];
$image = Image::by_id_or_hash($uid);
if ($image) {
2020-06-14 16:05:55 +00:00
send_event(new ThumbnailGenerationEvent($image->hash, $image->get_mime(), true));
} else {
2019-10-04 19:48:21 +00:00
print("No post with ID '$uid'\n");
}
}
2020-01-30 21:05:59 +00:00
if ($event->cmd == "cache") {
global $cache;
$cmd = $event->args[0];
$key = $event->args[1];
switch ($cmd) {
case "get":
2023-02-08 00:54:13 +00:00
var_export($cache->get($key));
2020-01-30 21:05:59 +00:00
break;
case "set":
$cache->set($key, $event->args[2], 60);
break;
case "del":
$cache->delete($key);
break;
}
}
}
public function onAdminBuilding(AdminBuildingEvent $event)
{
$this->theme->display_page();
}
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
{
global $user;
2023-11-11 21:49:12 +00:00
if ($event->parent === "system") {
if ($user->can(Permissions::MANAGE_ADMINTOOLS)) {
$event->add_nav_link("admin", new Link('admin'), "Board Admin");
}
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event)
{
global $user;
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::MANAGE_ADMINTOOLS)) {
$event->add_link("Board Admin", make_link("admin"));
}
}
}