page = $page; } } class AdminActionEvent extends Event { public string $action; public bool $redirect = true; public function __construct(string $action) { parent::__construct(); $this->action = $action; } } class AdminPage extends Extension { /** @var AdminPageTheme */ protected Themelet $theme; public function onPageRequest(PageRequestEvent $event) { global $database, $page, $user; if ($event->page_matches("admin")) { 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"); shm_set_timeout(null); $database->set_timeout(null); send_event($aae); } if ($aae->redirect) { $page->set_mode(PageMode::REDIRECT); $page->set_redirect(make_link("admin")); } } } } } public function onCliGen(CliGenEvent $event) { $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'); $_SERVER['REQUEST_URI'] = $query; if (!is_null($args)) { parse_str($args, $_GET); $_SERVER['REQUEST_URI'] .= "?" . $args; } send_event(new PageRequestEvent("GET", $query)); $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); } send_event(new PageRequestEvent("POST", $query)); $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('regen-thumb') ->addArgument('id_or_hash', InputArgument::REQUIRED) ->setDescription("Regenerate a post's thumbnail") ->setCode(function (InputInterface $input, OutputInterface $output): int { $uid = $input->getArgument('id_or_hash'); $image = Image::by_id_or_hash($uid); if ($image) { send_event(new ThumbnailGenerationEvent($image, true)); } else { $output->writeln("No post with ID '$uid'\n"); } 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; }); } public function onAdminBuilding(AdminBuildingEvent $event) { $this->theme->display_page(); } public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; 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; if ($user->can(Permissions::MANAGE_ADMINTOOLS)) { $event->add_link("Board Admin", make_link("admin")); } } }