2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2010-01-05 10:11:53 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2024-01-11 00:55:05 +00:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\{InputInterface,InputArgument};
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
2009-07-21 03:18:40 +00:00
|
|
|
/**
|
2007-04-16 11:58:25 +00:00
|
|
|
* Sent when the admin page is ready to be added to
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
class AdminBuildingEvent extends Event
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
public Page $page;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
public function __construct(Page $page)
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
parent::__construct();
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->page = $page;
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2007-07-19 12:08:42 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class AdminActionEvent extends Event
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
public string $action;
|
|
|
|
public bool $redirect = true;
|
2024-02-10 00:00:49 +00:00
|
|
|
/** @var array<string, mixed> */
|
|
|
|
public array $params;
|
2014-04-27 23:29:36 +00:00
|
|
|
|
2024-02-10 00:00:49 +00:00
|
|
|
/**
|
|
|
|
* @param array<string, mixed> $params
|
|
|
|
*/
|
|
|
|
public function __construct(string $action, array $params)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
parent::__construct();
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->action = $action;
|
2024-02-10 00:00:49 +00:00
|
|
|
$this->params = $params;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2012-03-10 12:57:13 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +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
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2023-06-25 13:19:02 +00:00
|
|
|
global $database, $page, $user;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2024-02-11 11:34:09 +00:00
|
|
|
if ($event->page_matches("admin", method: "GET", permission: Permissions::MANAGE_ADMINTOOLS)) {
|
|
|
|
send_event(new AdminBuildingEvent($page));
|
|
|
|
}
|
|
|
|
if ($event->page_matches("admin/{action}", method: "POST", permission: Permissions::MANAGE_ADMINTOOLS)) {
|
|
|
|
$action = $event->get_arg('action');
|
|
|
|
$aae = new AdminActionEvent($action, $event->POST);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2024-02-11 11:34:09 +00:00
|
|
|
log_info("admin", "Util: $action");
|
|
|
|
shm_set_timeout(null);
|
|
|
|
$database->set_timeout(null);
|
|
|
|
send_event($aae);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2024-02-11 11:34:09 +00:00
|
|
|
if ($aae->redirect) {
|
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(make_link("admin"));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onCliGen(CliGenEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-01-11 00:55:05 +00:00
|
|
|
$event->app->register('page:get')
|
|
|
|
->addArgument('query', InputArgument::REQUIRED)
|
|
|
|
->addArgument('args', InputArgument::OPTIONAL)
|
|
|
|
->setDescription('Get a page, eg /post/list')
|
|
|
|
->setCode(function (InputInterface $input, OutputInterface $output): int {
|
|
|
|
global $page;
|
|
|
|
$query = $input->getArgument('query');
|
|
|
|
$args = $input->getArgument('args');
|
2024-02-09 16:36:57 +00:00
|
|
|
$_SERVER['REQUEST_URI'] = make_link($query);
|
2024-01-11 00:55:05 +00:00
|
|
|
if (!is_null($args)) {
|
|
|
|
parse_str($args, $_GET);
|
|
|
|
$_SERVER['REQUEST_URI'] .= "?" . $args;
|
|
|
|
}
|
2024-02-09 16:36:57 +00:00
|
|
|
send_event(new PageRequestEvent("GET", $query, $_GET, []));
|
2024-01-11 00:55:05 +00:00
|
|
|
$page->display();
|
|
|
|
return Command::SUCCESS;
|
|
|
|
});
|
|
|
|
$event->app->register('page:post')
|
|
|
|
->addArgument('query', InputArgument::REQUIRED)
|
|
|
|
->addArgument('args', InputArgument::OPTIONAL)
|
|
|
|
->setDescription('Post a page, eg ip_ban/delete id=1')
|
|
|
|
->setCode(function (InputInterface $input, OutputInterface $output): int {
|
|
|
|
global $page;
|
|
|
|
$query = $input->getArgument('query');
|
|
|
|
$args = $input->getArgument('args');
|
|
|
|
global $page;
|
|
|
|
if (!is_null($args)) {
|
|
|
|
parse_str($args, $_POST);
|
|
|
|
}
|
2024-02-09 16:36:57 +00:00
|
|
|
send_event(new PageRequestEvent("POST", $query, [], $_POST));
|
2024-01-11 00:55:05 +00:00
|
|
|
$page->display();
|
|
|
|
return Command::SUCCESS;
|
|
|
|
});
|
|
|
|
$event->app->register('get-token')
|
|
|
|
->setDescription('Get a CSRF token')
|
|
|
|
->setCode(function (InputInterface $input, OutputInterface $output): int {
|
|
|
|
global $user;
|
|
|
|
$output->writeln($user->get_auth_token());
|
|
|
|
return Command::SUCCESS;
|
|
|
|
});
|
|
|
|
$event->app->register('cache:get')
|
|
|
|
->addArgument('key', InputArgument::REQUIRED)
|
|
|
|
->setDescription("Get a cache value")
|
|
|
|
->setCode(function (InputInterface $input, OutputInterface $output): int {
|
|
|
|
global $cache;
|
|
|
|
$key = $input->getArgument('key');
|
|
|
|
$output->writeln(var_export($cache->get($key), true));
|
|
|
|
return Command::SUCCESS;
|
|
|
|
});
|
|
|
|
$event->app->register('cache:set')
|
|
|
|
->addArgument('key', InputArgument::REQUIRED)
|
|
|
|
->addArgument('value', InputArgument::REQUIRED)
|
|
|
|
->setDescription("Set a cache value")
|
|
|
|
->setCode(function (InputInterface $input, OutputInterface $output): int {
|
|
|
|
global $cache;
|
|
|
|
$key = $input->getArgument('key');
|
|
|
|
$value = $input->getArgument('value');
|
|
|
|
$cache->set($key, $value, 60);
|
|
|
|
return Command::SUCCESS;
|
|
|
|
});
|
|
|
|
$event->app->register('cache:del')
|
|
|
|
->addArgument('key', InputArgument::REQUIRED)
|
|
|
|
->setDescription("Delete a cache value")
|
|
|
|
->setCode(function (InputInterface $input, OutputInterface $output): int {
|
|
|
|
global $cache;
|
|
|
|
$key = $input->getArgument('key');
|
|
|
|
$cache->delete($key);
|
|
|
|
return Command::SUCCESS;
|
|
|
|
});
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 14:51:55 +00:00
|
|
|
public function onAdminAction(AdminActionEvent $event): void
|
|
|
|
{
|
|
|
|
global $page;
|
|
|
|
if ($event->action === "test") {
|
|
|
|
$page->set_mode(PageMode::DATA);
|
|
|
|
$page->set_data("test");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onAdminBuilding(AdminBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->theme->display_page();
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event): void
|
2019-08-02 19:54:48 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($event->parent === "system") {
|
2019-08-02 19:54:48 +00:00
|
|
|
if ($user->can(Permissions::MANAGE_ADMINTOOLS)) {
|
|
|
|
$event->add_nav_link("admin", new Link('admin'), "Board Admin");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onUserBlockBuilding(UserBlockBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::MANAGE_ADMINTOOLS)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$event->add_link("Board Admin", make_link("admin"));
|
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|