2020-03-13 09:23:54 +00:00
|
|
|
<?php /** @noinspection PhpUnusedPrivateMethodInspection */
|
|
|
|
declare(strict_types=1);
|
2010-01-05 10:11:53 +00:00
|
|
|
|
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
|
|
|
|
{
|
2019-05-28 19:27:23 +00:00
|
|
|
/** @var Page */
|
2019-05-28 16:59:38 +00:00
|
|
|
public $page;
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
/** @var string */
|
|
|
|
public $action;
|
|
|
|
/** @var bool */
|
|
|
|
public $redirect = true;
|
2014-04-27 23:29:36 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function __construct(string $action)
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
parent::__construct();
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->action = $action;
|
|
|
|
}
|
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 */
|
|
|
|
protected $theme;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
|
|
|
global $page, $user;
|
|
|
|
|
|
|
|
if ($event->page_matches("admin")) {
|
2019-07-09 14:10:21 +00:00
|
|
|
if (!$user->can(Permissions::MANAGE_ADMINTOOLS)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$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");
|
|
|
|
set_time_limit(0);
|
|
|
|
send_event($aae);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($aae->redirect) {
|
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("admin"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onCommand(CommandEvent $event)
|
|
|
|
{
|
|
|
|
if ($event->cmd == "help") {
|
2019-10-04 19:48:21 +00:00
|
|
|
print "\tget-page <query string>\n";
|
2019-05-28 16:59:38 +00:00
|
|
|
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";
|
2019-05-28 16:59:38 +00:00
|
|
|
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";
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
if ($event->cmd == "get-page") {
|
|
|
|
global $page;
|
2019-12-15 15:31:44 +00:00
|
|
|
if (isset($event->args[1])) {
|
|
|
|
parse_str($event->args[1], $_GET);
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
send_event(new PageRequestEvent($event->args[0]));
|
|
|
|
$page->display();
|
|
|
|
}
|
2019-11-27 16:10:12 +00:00
|
|
|
if ($event->cmd == "post-page") {
|
|
|
|
global $page;
|
|
|
|
$_SERVER['REQUEST_METHOD'] = "POST";
|
2019-12-15 15:31:44 +00:00
|
|
|
if (isset($event->args[1])) {
|
|
|
|
parse_str($event->args[1], $_POST);
|
|
|
|
}
|
2019-11-27 16:10:12 +00:00
|
|
|
send_event(new PageRequestEvent($event->args[0]));
|
|
|
|
$page->display();
|
|
|
|
}
|
|
|
|
if ($event->cmd == "get-token") {
|
|
|
|
global $user;
|
|
|
|
print($user->get_auth_token());
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($event->cmd == "regen-thumb") {
|
2019-10-04 19:48:21 +00:00
|
|
|
$uid = $event->args[0];
|
|
|
|
$image = Image::by_id_or_hash($uid);
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($image) {
|
|
|
|
send_event(new ThumbnailGenerationEvent($image->hash, $image->ext, true));
|
|
|
|
} else {
|
2019-10-04 19:48:21 +00:00
|
|
|
print("No post with ID '$uid'\n");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
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":
|
|
|
|
var_dump($cache->get($key));
|
|
|
|
break;
|
|
|
|
case "set":
|
|
|
|
$cache->set($key, $event->args[2], 60);
|
|
|
|
break;
|
|
|
|
case "del":
|
|
|
|
$cache->delete($key);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onAdminBuilding(AdminBuildingEvent $event)
|
|
|
|
{
|
|
|
|
$this->theme->display_page();
|
|
|
|
$this->theme->display_form();
|
|
|
|
}
|
|
|
|
|
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") {
|
2019-08-02 19:54:48 +00:00
|
|
|
if ($user->can(Permissions::MANAGE_ADMINTOOLS)) {
|
|
|
|
$event->add_nav_link("admin", new Link('admin'), "Board Admin");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onUserBlockBuilding(UserBlockBuildingEvent $event)
|
|
|
|
{
|
|
|
|
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"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onAdminAction(AdminActionEvent $event)
|
|
|
|
{
|
|
|
|
$action = $event->action;
|
|
|
|
if (method_exists($this, $action)) {
|
|
|
|
$event->redirect = $this->$action();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function set_tag_case()
|
|
|
|
{
|
|
|
|
global $database;
|
2020-02-01 22:44:50 +00:00
|
|
|
$database->execute(
|
|
|
|
"UPDATE tags SET tag=:tag1 WHERE LOWER(tag) = LOWER(:tag2)",
|
|
|
|
["tag1" => $_POST['tag'], "tag2" => $_POST['tag']]
|
|
|
|
);
|
2020-01-26 17:39:55 +00:00
|
|
|
log_info("admin", "Fixed the case of {$_POST['tag']}", "Fixed case");
|
2019-05-28 16:59:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function lowercase_all_tags()
|
|
|
|
{
|
|
|
|
global $database;
|
|
|
|
$database->execute("UPDATE tags SET tag=lower(tag)");
|
|
|
|
log_warning("admin", "Set all tags to lowercase", "Set all tags to lowercase");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function recount_tag_use()
|
|
|
|
{
|
|
|
|
global $database;
|
|
|
|
$database->Execute("
|
2009-07-28 00:19:40 +00:00
|
|
|
UPDATE tags
|
|
|
|
SET count = COALESCE(
|
2009-10-08 13:22:18 +00:00
|
|
|
(SELECT COUNT(image_id) FROM image_tags WHERE tag_id=tags.id GROUP BY tag_id),
|
2009-07-28 00:19:40 +00:00
|
|
|
0
|
2012-03-10 12:57:13 +00:00
|
|
|
)
|
|
|
|
");
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->Execute("DELETE FROM tags WHERE count=0");
|
|
|
|
log_warning("admin", "Re-counted tags", "Re-counted tags");
|
|
|
|
return true;
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|