microcrud for notatag

This commit is contained in:
Shish 2019-11-29 02:06:22 +00:00
parent 2deaeca133
commit 7d30aaf1ea
4 changed files with 52 additions and 89 deletions

View file

@ -7,13 +7,11 @@ use MicroCRUD\Table;
class HashBanTable extends Table class HashBanTable extends Table
{ {
public function __construct(\FFSPHP\PDO $db, $token=null) public function __construct(\FFSPHP\PDO $db)
{ {
parent::__construct($db, $token); parent::__construct($db);
$this->table = "image_bans"; $this->table = "image_bans";
$this->base_query = "SELECT * FROM image_bans"; $this->base_query = "SELECT * FROM image_bans";
$this->size = 100; $this->size = 100;
$this->limit = 1000000; $this->limit = 1000000;
$this->columns = [ $this->columns = [
@ -24,7 +22,6 @@ class HashBanTable extends Table
$this->order_by = ["date DESC", "id"]; $this->order_by = ["date DESC", "id"];
$this->create_url = make_link("image_hash_ban/add"); $this->create_url = make_link("image_hash_ban/add");
$this->delete_url = make_link("image_hash_ban/remove"); $this->delete_url = make_link("image_hash_ban/remove");
$this->table_attrs = ["class" => "zebra"]; $this->table_attrs = ["class" => "zebra"];
} }
} }
@ -104,12 +101,8 @@ class ImageBan extends Extension
} }
} elseif ($event->get_arg(0) == "remove") { } elseif ($event->get_arg(0) == "remove") {
$user->ensure_authed(); $user->ensure_authed();
$input = validate_input(["d_id"=>"int"]); $input = validate_input(["d_hash"=>"string"]);
$hash = $database->get_one( send_event(new RemoveImageHashBanEvent($input['hash']));
"SELECT hash FROM image_hash_bans WHERE id=:id",
["id"=>$input['d_id']]
);
send_event(new RemoveImageHashBanEvent($hash));
flash_message("Image ban removed"); flash_message("Image ban removed");
$page->set_mode(PageMode::REDIRECT); $page->set_mode(PageMode::REDIRECT);
$page->set_redirect($_SERVER['HTTP_REFERER']); $page->set_redirect($_SERVER['HTTP_REFERER']);

View file

@ -9,10 +9,9 @@ use MicroCRUD\Table;
class IPBanTable extends Table class IPBanTable extends Table
{ {
public function __construct(\FFSPHP\PDO $db, $token=null) public function __construct(\FFSPHP\PDO $db)
{ {
parent::__construct($db, $token); parent::__construct($db);
$this->table = "bans"; $this->table = "bans";
$this->base_query = " $this->base_query = "
SELECT * FROM ( SELECT * FROM (
@ -20,8 +19,8 @@ class IPBanTable extends Table
FROM bans JOIN users ON banner_id=users.id FROM bans JOIN users ON banner_id=users.id
) AS tbl1 ) AS tbl1
"; ";
$this->size = 100;
$this->size = 10; $this->limit = 1000000;
$this->columns = [ $this->columns = [
new InetColumn("ip", "IP"), new InetColumn("ip", "IP"),
new EnumColumn("mode", "Mode", ["Block"=>"block", "Firewall"=>"firewall", "Ghost"=>"ghost"]), new EnumColumn("mode", "Mode", ["Block"=>"block", "Firewall"=>"firewall", "Ghost"=>"ghost"]),
@ -36,8 +35,7 @@ class IPBanTable extends Table
]; ];
$this->create_url = make_link("ip_ban/create"); $this->create_url = make_link("ip_ban/create");
$this->delete_url = make_link("ip_ban/delete"); $this->delete_url = make_link("ip_ban/delete");
$this->table_attrs = ["class" => "zebra"];
$this->table_attrs = ["class" => "sortable zebra"];
} }
} }

View file

@ -1,5 +1,28 @@
<?php <?php
use MicroCRUD\TextColumn;
use MicroCRUD\Table;
class NotATagTable extends Table
{
public function __construct(\FFSPHP\PDO $db)
{
parent::__construct($db);
$this->table = "untags";
$this->base_query = "SELECT * FROM untags";
$this->size = 100;
$this->limit = 1000000;
$this->columns = [
new TextColumn("tag", "Tag"),
new TextColumn("redirect", "Redirect"),
];
$this->order_by = ["tag", "redirect"];
$this->create_url = make_link("untag/add");
$this->delete_url = make_link("untag/remove");
$this->table_attrs = ["class" => "zebra"];
}
}
class NotATag extends Extension class NotATag extends Extension
{ {
public function get_priority(): int public function get_priority(): int
@ -77,32 +100,29 @@ class NotATag extends Extension
if ($event->page_matches("untag")) { if ($event->page_matches("untag")) {
if ($user->can(Permissions::BAN_IMAGE)) { if ($user->can(Permissions::BAN_IMAGE)) {
if ($event->get_arg(0) == "add") { if ($event->get_arg(0) == "add") {
$tag = $_POST["tag"]; $user->ensure_authed();
$redirect = isset($_POST['redirect']) ? $_POST['redirect'] : "DNP"; $input = validate_input(["c_tag"=>"string", "c_redirect"=>"string"]);
$database->execute(
$database->Execute(
"INSERT INTO untags(tag, redirect) VALUES (:tag, :redirect)", "INSERT INTO untags(tag, redirect) VALUES (:tag, :redirect)",
["tag"=>$tag, "redirect"=>$redirect] ["tag"=>$input['c_tag'], "redirect"=>$input['c_redirect']]
); );
$page->set_mode(PageMode::REDIRECT); $page->set_mode(PageMode::REDIRECT);
$page->set_redirect($_SERVER['HTTP_REFERER']); $page->set_redirect($_SERVER['HTTP_REFERER']);
} elseif ($event->get_arg(0) == "remove") { } elseif ($event->get_arg(0) == "remove") {
if (isset($_POST['tag'])) { $user->ensure_authed();
$database->Execute($database->scoreql_to_sql("DELETE FROM untags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"), ["tag"=>$_POST['tag']]); $input = validate_input(["d_tag"=>"string"]);
$database->execute($database->scoreql_to_sql(
flash_message("Image ban removed"); "DELETE FROM untags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"),
$page->set_mode(PageMode::REDIRECT); ["tag"=>$input['d_tag']]
$page->set_redirect($_SERVER['HTTP_REFERER']); );
} flash_message("Image ban removed");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect($_SERVER['HTTP_REFERER']);
} elseif ($event->get_arg(0) == "list") { } elseif ($event->get_arg(0) == "list") {
$page_num = 0; $t = new NotATagTable($database->raw_db());
if ($event->count_args() == 2) { $t->token = $user->get_auth_token();
$page_num = int_escape($event->get_arg(1)); $t->inputs = $_GET;
} $this->theme->display_bans($page, $t->table($t->query()), $t->paginator());
$page_size = 100;
$page_count = ceil($database->get_one("SELECT COUNT(tag) FROM untags")/$page_size);
$this->theme->display_untags($page, $page_num, $page_count, $this->get_untags($page_num, $page_size));
} }
} }
} }

View file

@ -1,59 +1,11 @@
<?php <?php
class NotATagTheme extends Themelet class NotATagTheme extends Themelet
{ {
public function display_untags(Page $page, $page_number, $page_count, $bans) public function display_untags(Page $page, $table, $paginator)
{ {
$h_bans = "";
foreach ($bans as $ban) {
$h_bans .= "
<tr>
".make_form(make_link("untag/remove"))."
<td width='30%'>{$ban['tag']}</td>
<td>{$ban['redirect']}</td>
<td width='10%'>
<input type='hidden' name='tag' value='{$ban['tag']}'>
<input type='submit' value='Remove'>
</td>
</form>
</tr>
";
}
$html = "
<table id='image_bans' class='zebra sortable'>
<thead>
<th>Tag</th><th>Redirect</th><th>Action</th>
<tr>
<form action='".make_link("untag/list/1")."' method='GET'>
<td><input type='text' name='tag' class='autocomplete_tags' autocomplete='off'></td>
<td><input type='text' name='redirect'></td>
<td><input type='submit' value='Search'></td>
</form>
</tr>
</thead>
$h_bans
<tfoot><tr>
".make_form(make_link("untag/add"))."
<td><input type='text' name='tag' class='autocomplete_tags' autocomplete='off'></td>
<td><input type='text' name='redirect'></td>
<td><input type='submit' value='Ban'></td>
</form>
</tr></tfoot>
</table>
";
$prev = $page_number - 1;
$next = $page_number + 1;
$h_prev = ($page_number <= 1) ? "Prev" : "<a href='".make_link("untag/list/$prev")."'>Prev</a>";
$h_index = "<a href='".make_link()."'>Index</a>";
$h_next = ($page_number >= $page_count) ? "Next" : "<a href='".make_link("untag/list/$next")."'>Next</a>";
$nav = "$h_prev | $h_index | $h_next";
$page->set_title("UnTags"); $page->set_title("UnTags");
$page->set_heading("UnTags"); $page->set_heading("UnTags");
$page->add_block(new Block("Edit UnTags", $html)); $page->add_block(new NavBlock());
$page->add_block(new Block("Navigation", $nav, "left", 0)); $page->add_block(new Block("Edit UnTags", $table . $paginator));
$this->display_paginator($page, "untag/list", null, $page_number, $page_count);
} }
} }