From a175405210a2cb177aab004a56d27f389983ef46 Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 24 Nov 2019 15:59:14 +0000 Subject: [PATCH] replace ipban page with microcrud --- composer.json | 1 + composer.lock | 125 ++++++++++++++++++++++++++++++++++++++------ core/database.php | 4 ++ ext/ipban/main.php | 38 ++++++++++++-- ext/ipban/theme.php | 67 ++---------------------- 5 files changed, 153 insertions(+), 82 deletions(-) diff --git a/composer.json b/composer.json index 743e91c3..5152f71d 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,7 @@ "google/recaptcha" : "~1.1", "dapphp/securimage" : "3.6.*", "shish/eventtracer-php" : "dev-master", + "shish/microcrud" : "dev-master", "enshrined/svg-sanitize" : "0.8.*", "bower-asset/jquery" : "1.12.*", diff --git a/composer.lock b/composer.lock index d4d27f2e..a1eda694 100644 --- a/composer.lock +++ b/composer.lock @@ -4,14 +4,14 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "771e83de94b5aef4631762f45a430fa8", + "content-hash": "1a344925ec0ffdb23fb554973ae897bd", "packages": [ { "name": "bower-asset/jquery", "version": "1.12.4", "source": { "type": "git", - "url": "https://github.com/jquery/jquery-dist.git", + "url": "git@github.com:jquery/jquery-dist.git", "reference": "5e89585e0121e72ff47de177c5ef604f3089a53d" }, "dist": { @@ -339,6 +339,100 @@ "description": "An API to write JSON traces as used by the Chrome Trace Viewer", "homepage": "https://github.com/shish/eventtracer-php", "time": "2019-11-21T13:07:27+00:00" + }, + { + "name": "shish/microcrud", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/shish/microcrud.git", + "reference": "6e297d376904e34986320896f6a1c83b377a54cc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/shish/microcrud/zipball/6e297d376904e34986320896f6a1c83b377a54cc", + "reference": "6e297d376904e34986320896f6a1c83b377a54cc", + "shasum": "" + }, + "require": { + "ext-pdo": "*", + "php": ">=7.2", + "shish/microhtml": "dev-master" + }, + "require-dev": { + "phpunit/phpunit": "8.*" + }, + "type": "library", + "autoload": { + "psr-4": { + "MicroCRUD\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Shish", + "email": "webmaster@shishnet.org", + "homepage": "http://shishnet.org", + "role": "Developer" + } + ], + "description": "A minimal CRUD generating library", + "homepage": "https://github.com/shish/microcrud", + "keywords": [ + "crud", + "generator" + ], + "time": "2019-11-24T13:37:36+00:00" + }, + { + "name": "shish/microhtml", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/shish/microhtml.git", + "reference": "ac1cd313555d81b7b45e162291eed8d0a8165511" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/shish/microhtml/zipball/ac1cd313555d81b7b45e162291eed8d0a8165511", + "reference": "ac1cd313555d81b7b45e162291eed8d0a8165511", + "shasum": "" + }, + "require": { + "php": ">=7.2" + }, + "require-dev": { + "phpunit/phpunit": "8.*" + }, + "type": "library", + "autoload": { + "files": [ + "src/microhtml.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Shish", + "email": "webmaster@shishnet.org", + "homepage": "http://shishnet.org", + "role": "Developer" + } + ], + "description": "A minimal HTML generating library", + "homepage": "https://github.com/shish/microhtml", + "keywords": [ + "generator", + "html" + ], + "time": "2019-11-24T02:10:21+00:00" } ], "packages-dev": [ @@ -1018,12 +1112,12 @@ "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "f2eeaa3cdab29a70ba900d5f1c67f841adf028ea" + "reference": "cc8ddbe513adf9c360c203bf769fc9700289c5ae" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f2eeaa3cdab29a70ba900d5f1c67f841adf028ea", - "reference": "f2eeaa3cdab29a70ba900d5f1c67f841adf028ea", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/cc8ddbe513adf9c360c203bf769fc9700289c5ae", + "reference": "cc8ddbe513adf9c360c203bf769fc9700289c5ae", "shasum": "" }, "require": { @@ -1093,7 +1187,7 @@ "testing", "xunit" ], - "time": "2019-11-21T09:37:31+00:00" + "time": "2019-11-24T09:29:40+00:00" }, { "name": "sebastian/code-unit-reverse-lookup", @@ -1810,31 +1904,29 @@ }, { "name": "webmozart/assert", - "version": "1.5.0", + "version": "1.6.0", "source": { "type": "git", "url": "https://github.com/webmozart/assert.git", - "reference": "88e6d84706d09a236046d686bbea96f07b3a34f4" + "reference": "573381c0a64f155a0d9a23f4b0c797194805b925" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/webmozart/assert/zipball/88e6d84706d09a236046d686bbea96f07b3a34f4", - "reference": "88e6d84706d09a236046d686bbea96f07b3a34f4", + "url": "https://api.github.com/repos/webmozart/assert/zipball/573381c0a64f155a0d9a23f4b0c797194805b925", + "reference": "573381c0a64f155a0d9a23f4b0c797194805b925", "shasum": "" }, "require": { "php": "^5.3.3 || ^7.0", "symfony/polyfill-ctype": "^1.8" }, + "conflict": { + "vimeo/psalm": "<3.6.0" + }, "require-dev": { "phpunit/phpunit": "^4.8.36 || ^7.5.13" }, "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.3-dev" - } - }, "autoload": { "psr-4": { "Webmozart\\Assert\\": "src/" @@ -1856,13 +1948,14 @@ "check", "validate" ], - "time": "2019-08-24T08:43:50+00:00" + "time": "2019-11-24T13:36:37+00:00" } ], "aliases": [], "minimum-stability": "dev", "stability-flags": { "shish/eventtracer-php": 20, + "shish/microcrud": 20, "bower-asset/tablesorter": 20 }, "prefer-stable": false, diff --git a/core/database.php b/core/database.php index 3576d3a7..6f5d352f 100644 --- a/core/database.php +++ b/core/database.php @@ -378,6 +378,10 @@ class Database throw new SCoreException("Can't count tables for database type {$this->engine->name}"); } } + + public function raw_db(): PDO { + return $this->db; + } } class MockDatabase extends Database diff --git a/ext/ipban/main.php b/ext/ipban/main.php index 72040316..8733175a 100644 --- a/ext/ipban/main.php +++ b/ext/ipban/main.php @@ -1,5 +1,35 @@ table = "bans"; + $this->base_query = " + SELECT *, users.name AS banner + FROM bans JOIN users ON banner_id=users.id + "; + + $this->size = 10; + $this->columns = [ + new StringColumn("ip", "IP"), + new EnumColumn("mode", "Mode", ["Block"=>"block", "Firewall"=>"firewall"]), + new TextColumn("reason", "Reason"), + new StringColumn("banner", "Banner"), + new DateColumn("added", "Added"), + new DateColumn("expires", "Expires"), + ]; + $this->order_by = ["expires", "id"]; + $this->flags = [ + "all" => ["((expires > CURRENT_TIMESTAMP) OR (expires IS NULL))", null], + ]; + $this->create_url = "/ip_ban/create"; + $this->delete_url = "/ip_ban/remove"; + } +} + class RemoveIPBanEvent extends Event { public $id; @@ -47,7 +77,7 @@ class IPBan extends Extension public function onPageRequest(PageRequestEvent $event) { if ($event->page_matches("ip_ban")) { - global $page, $user; + global $database, $page, $user; if ($user->can(Permissions::BAN_IP)) { if ($event->get_arg(0) == "create" && $user->check_auth_token()) { if (isset($_POST['c_ip']) && isset($_POST['c_reason']) && isset($_POST['c_expires'])) { @@ -71,9 +101,9 @@ class IPBan extends Extension $page->set_redirect(make_link("ip_ban/list")); } } elseif ($event->get_arg(0) == "list") { - $pageNum = isset($_GET["r__page"]) ? int_escape($_GET["r__page"]) : 1; - $bans = $this->get_bans(isset($_GET["r_all"]), $pageNum); - $this->theme->display_bans($page, $bans); + $t = new IPBanTable($database->raw_db(), $user->get_auth_token()); + $table = $t->table($t->query()); + $this->theme->display_bans($page, $table, $t->paginator()); } } else { $this->theme->display_permission_denied(); diff --git a/ext/ipban/theme.php b/ext/ipban/theme.php index 3eb8f4df..c94a0032 100644 --- a/ext/ipban/theme.php +++ b/ext/ipban/theme.php @@ -2,73 +2,16 @@ class IPBanTheme extends Themelet { - /* - * Show all the bans - * - * $bans = an array of ( - * 'ip' => the banned IP - * 'reason' => why the IP was banned - * 'added' => when the ban started - * 'expires' => when the ban will end - * ) - */ - public function display_bans(Page $page, $bans) + public function display_bans(Page $page, $table, $paginator) { - global $database, $user; - $h_bans = ""; - $prefix = ($database->get_driver_name() == DatabaseDriver::SQLITE ? "bans." : ""); - foreach ($bans as $ban) { - $h_bans .= " - - {$ban[$prefix.'ip']} - {$ban[$prefix.'reason']} - {$ban['banner']} - ".substr($ban[$prefix.'added'], 0, 10)." - ".substr($ban[$prefix.'expires'], 0, 10)." - {$ban['mode']} - ".make_form(make_link("ip_ban/delete"))." - - - - - - - "; - } $today = date('Y-m-d'); $html = " Show All Active / Show EVERYTHING -

- - - - ".make_form(make_link("ip_ban/list"), "GET")." - - - - - - - - - - - $h_bans - - - ".make_form(make_link("ip_ban/create"))." - - - - - - - - - - -
IPReasonByFromUntilTypeAction
{$user->name}{$today}block
+ + $table + + $paginator "; $page->set_title("IP Bans"); $page->set_heading("IP Bans");