2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
2009-12-30 07:59:40 +00:00
|
|
|
|
2021-12-14 18:32:47 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2021-12-14 18:32:47 +00:00
|
|
|
use MicroHTML\HTMLElement;
|
|
|
|
use MicroCRUD\ActionColumn;
|
|
|
|
use MicroCRUD\Column;
|
|
|
|
use MicroCRUD\DateTimeColumn;
|
|
|
|
use MicroCRUD\TextColumn;
|
|
|
|
use MicroCRUD\Table;
|
2022-10-27 16:21:46 +00:00
|
|
|
|
2019-12-15 15:31:44 +00:00
|
|
|
use function MicroHTML\A;
|
|
|
|
use function MicroHTML\SPAN;
|
|
|
|
use function MicroHTML\emptyHTML;
|
|
|
|
use function MicroHTML\INPUT;
|
|
|
|
use function MicroHTML\BR;
|
|
|
|
use function MicroHTML\SELECT;
|
|
|
|
use function MicroHTML\OPTION;
|
|
|
|
use function MicroHTML\rawHTML;
|
2019-12-01 18:58:13 +00:00
|
|
|
|
2019-12-15 15:31:44 +00:00
|
|
|
class ShortDateTimeColumn extends DateTimeColumn
|
|
|
|
{
|
2024-01-15 12:14:41 +00:00
|
|
|
public function read_input(array $inputs): HTMLElement
|
2019-12-08 10:52:00 +00:00
|
|
|
{
|
|
|
|
return emptyHTML(
|
|
|
|
INPUT([
|
2023-11-11 21:49:12 +00:00
|
|
|
"type" => "date",
|
|
|
|
"name" => "r_{$this->name}[]",
|
|
|
|
"value" => @$inputs["r_{$this->name}"][0]
|
2019-12-08 10:52:00 +00:00
|
|
|
]),
|
|
|
|
BR(),
|
|
|
|
INPUT([
|
2023-11-11 21:49:12 +00:00
|
|
|
"type" => "date",
|
|
|
|
"name" => "r_{$this->name}[]",
|
|
|
|
"value" => @$inputs["r_{$this->name}"][1]
|
2019-12-08 10:52:00 +00:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 15:31:44 +00:00
|
|
|
class ActorColumn extends Column
|
|
|
|
{
|
2019-12-01 18:58:13 +00:00
|
|
|
public function __construct($name, $title)
|
|
|
|
{
|
2019-12-26 00:36:32 +00:00
|
|
|
parent::__construct($name, $title);
|
|
|
|
$this->sortable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_sql_filter(): string
|
|
|
|
{
|
2023-01-11 11:15:26 +00:00
|
|
|
$driver = $this->table->db->getAttribute(\PDO::ATTR_DRIVER_NAME);
|
2019-12-26 00:36:32 +00:00
|
|
|
switch ($driver) {
|
|
|
|
case "pgsql":
|
2020-03-18 17:35:38 +00:00
|
|
|
return "((LOWER(username) = LOWER(:{$this->name}_0)) OR (address && cast(:{$this->name}_1 as inet)))";
|
2019-12-26 00:36:32 +00:00
|
|
|
default:
|
|
|
|
return "((username = :{$this->name}_0) OR (address = :{$this->name}_1))";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 12:14:41 +00:00
|
|
|
public function read_input(array $inputs): HTMLElement
|
2019-12-26 00:36:32 +00:00
|
|
|
{
|
|
|
|
return emptyHTML(
|
|
|
|
INPUT([
|
|
|
|
"type" => "text",
|
|
|
|
"name" => "r_{$this->name}[]",
|
|
|
|
"placeholder" => "Username",
|
|
|
|
"value" => @$inputs["r_{$this->name}"][0]
|
|
|
|
]),
|
|
|
|
BR(),
|
|
|
|
INPUT([
|
|
|
|
"type" => "text",
|
|
|
|
"name" => "r_{$this->name}[]",
|
|
|
|
"placeholder" => "IP Address",
|
|
|
|
"value" => @$inputs["r_{$this->name}"][1]
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-15 12:14:41 +00:00
|
|
|
public function modify_input_for_read(string|array $input): array
|
2019-12-26 00:36:32 +00:00
|
|
|
{
|
|
|
|
list($un, $ip) = $input;
|
|
|
|
if (empty($un)) {
|
|
|
|
$un = null;
|
|
|
|
}
|
|
|
|
if (empty($ip)) {
|
2019-12-26 16:52:59 +00:00
|
|
|
$ip = null;
|
2019-12-26 00:36:32 +00:00
|
|
|
}
|
|
|
|
return [$un, $ip];
|
2019-12-01 18:58:13 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
public function display($row): HTMLElement
|
2019-12-01 18:58:13 +00:00
|
|
|
{
|
2019-12-15 15:31:44 +00:00
|
|
|
$ret = emptyHTML();
|
2019-12-08 10:52:00 +00:00
|
|
|
if ($row['username'] != "Anonymous") {
|
2023-11-11 21:49:12 +00:00
|
|
|
$ret->appendChild(A(["href" => make_link("user/{$row['username']}"), "title" => $row['address']], $row['username']));
|
2019-12-15 15:31:44 +00:00
|
|
|
$ret->appendChild(BR());
|
2019-12-01 18:58:13 +00:00
|
|
|
}
|
2019-12-15 15:31:44 +00:00
|
|
|
$ret->appendChild($row['address']);
|
2019-12-08 10:52:00 +00:00
|
|
|
return $ret;
|
2019-12-01 18:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 15:31:44 +00:00
|
|
|
class MessageColumn extends Column
|
|
|
|
{
|
2020-01-26 17:39:55 +00:00
|
|
|
public function __construct(string $name, string $title)
|
2019-12-08 10:52:00 +00:00
|
|
|
{
|
2019-12-26 00:36:32 +00:00
|
|
|
parent::__construct($name, $title);
|
|
|
|
$this->sortable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_sql_filter(): string
|
|
|
|
{
|
2023-06-28 13:02:20 +00:00
|
|
|
$driver = $this->table->db->getAttribute(\PDO::ATTR_DRIVER_NAME);
|
|
|
|
switch ($driver) {
|
|
|
|
case "pgsql":
|
|
|
|
return "(LOWER({$this->name}) LIKE LOWER(:{$this->name}_0) AND priority >= :{$this->name}_1)";
|
|
|
|
default:
|
|
|
|
return "({$this->name} LIKE :{$this->name}_0 AND priority >= :{$this->name}_1)";
|
|
|
|
}
|
2019-12-08 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 12:14:41 +00:00
|
|
|
public function read_input(array $inputs): HTMLElement
|
2019-12-08 10:52:00 +00:00
|
|
|
{
|
|
|
|
$ret = emptyHTML(
|
2019-12-15 15:31:44 +00:00
|
|
|
INPUT([
|
2023-11-11 21:49:12 +00:00
|
|
|
"type" => "text",
|
|
|
|
"name" => "r_{$this->name}[]",
|
|
|
|
"placeholder" => $this->title,
|
|
|
|
"value" => @$inputs["r_{$this->name}"][0]
|
2019-12-15 15:31:44 +00:00
|
|
|
])
|
|
|
|
);
|
|
|
|
|
|
|
|
$options = [
|
|
|
|
"Debug" => SCORE_LOG_DEBUG,
|
|
|
|
"Info" => SCORE_LOG_INFO,
|
|
|
|
"Warning" => SCORE_LOG_WARNING,
|
|
|
|
"Error" => SCORE_LOG_ERROR,
|
|
|
|
"Critical" => SCORE_LOG_CRITICAL,
|
|
|
|
];
|
2023-11-11 21:49:12 +00:00
|
|
|
$s = SELECT(["name" => "r_{$this->name}[]"]);
|
|
|
|
$s->appendChild(OPTION(["value" => ""], '-'));
|
2019-12-08 10:52:00 +00:00
|
|
|
foreach ($options as $k => $v) {
|
2023-11-11 21:49:12 +00:00
|
|
|
$attrs = ["value" => $v];
|
2019-12-08 10:52:00 +00:00
|
|
|
if ($v == @$inputs["r_{$this->name}"][1]) {
|
|
|
|
$attrs["selected"] = true;
|
|
|
|
}
|
|
|
|
$s->appendChild(OPTION($attrs, $k));
|
|
|
|
}
|
2019-12-15 15:31:44 +00:00
|
|
|
$ret->appendChild($s);
|
2019-12-08 10:52:00 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2024-01-15 12:14:41 +00:00
|
|
|
public function modify_input_for_read(array|string $input): mixed
|
2019-12-26 00:36:32 +00:00
|
|
|
{
|
|
|
|
list($m, $l) = $input;
|
|
|
|
if (empty($m)) {
|
|
|
|
$m = "%";
|
|
|
|
} else {
|
|
|
|
$m = "%$m%";
|
|
|
|
}
|
|
|
|
if (empty($l)) {
|
|
|
|
$l = 0;
|
|
|
|
}
|
|
|
|
return [$m, $l];
|
|
|
|
}
|
|
|
|
|
2024-01-15 12:14:41 +00:00
|
|
|
public function display(array $row): HTMLElement
|
2019-12-01 18:58:13 +00:00
|
|
|
{
|
|
|
|
$c = "#000";
|
|
|
|
switch ($row['priority']) {
|
2022-10-27 16:29:24 +00:00
|
|
|
case SCORE_LOG_DEBUG:
|
|
|
|
$c = "#999";
|
|
|
|
break;
|
|
|
|
case SCORE_LOG_INFO:
|
|
|
|
$c = "#000";
|
|
|
|
break;
|
|
|
|
case SCORE_LOG_WARNING:
|
|
|
|
$c = "#800";
|
|
|
|
break;
|
|
|
|
case SCORE_LOG_ERROR:
|
|
|
|
$c = "#C00";
|
|
|
|
break;
|
|
|
|
case SCORE_LOG_CRITICAL:
|
|
|
|
$c = "#F00";
|
|
|
|
break;
|
2019-12-01 18:58:13 +00:00
|
|
|
}
|
2023-11-11 21:49:12 +00:00
|
|
|
return SPAN(["style" => "color: $c"], rawHTML($this->scan_entities($row[$this->name])));
|
2019-12-01 18:58:13 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 17:39:55 +00:00
|
|
|
protected function scan_entities(string $line)
|
2019-12-01 18:58:13 +00:00
|
|
|
{
|
2020-10-24 12:46:49 +00:00
|
|
|
$line = preg_replace_callback("/Image #(\d+)/s", [$this, "link_image"], $line);
|
2020-10-26 15:14:36 +00:00
|
|
|
$line = preg_replace_callback("/Post #(\d+)/s", [$this, "link_image"], $line);
|
2020-10-24 12:46:49 +00:00
|
|
|
$line = preg_replace_callback("/>>(\d+)/s", [$this, "link_image"], $line);
|
|
|
|
return $line;
|
2019-12-01 18:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function link_image($id)
|
|
|
|
{
|
|
|
|
$iid = int_escape($id[1]);
|
2020-10-09 12:45:23 +00:00
|
|
|
return "<a href='".make_link("post/view/$iid")."'>>>$iid</a>";
|
2019-12-01 18:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class LogTable extends Table
|
|
|
|
{
|
|
|
|
public function __construct(\FFSPHP\PDO $db)
|
|
|
|
{
|
|
|
|
parent::__construct($db);
|
|
|
|
$this->table = "score_log";
|
|
|
|
$this->base_query = "SELECT * FROM score_log";
|
|
|
|
$this->size = 100;
|
2020-10-25 10:44:30 +00:00
|
|
|
$this->limit = 100000;
|
2019-12-26 00:36:32 +00:00
|
|
|
$this->set_columns([
|
2019-12-08 10:52:00 +00:00
|
|
|
new ShortDateTimeColumn("date_sent", "Time"),
|
2019-12-01 18:58:13 +00:00
|
|
|
new TextColumn("section", "Module"),
|
|
|
|
new ActorColumn("username_or_address", "User"),
|
2019-12-26 00:36:32 +00:00
|
|
|
new MessageColumn("message", "Message"),
|
|
|
|
new ActionColumn("id"),
|
|
|
|
]);
|
2019-12-01 18:58:13 +00:00
|
|
|
$this->order_by = ["date_sent DESC"];
|
2024-01-08 21:03:04 +00:00
|
|
|
$this->table_attrs = ["class" => "zebra form"];
|
2019-12-01 18:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class LogDatabase extends Extension
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
/** @var LogDatabaseTheme */
|
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 onInitExt(InitExtEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
2019-11-03 17:19:37 +00:00
|
|
|
$config->set_default_int("log_db_priority", SCORE_LOG_INFO);
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
|
2019-11-03 18:28:38 +00:00
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
global $database;
|
2009-12-30 07:59:40 +00:00
|
|
|
|
2019-11-03 19:49:52 +00:00
|
|
|
if ($this->get_version("ext_log_database_version") < 1) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->create_table("score_log", "
|
2009-12-30 07:59:40 +00:00
|
|
|
id SCORE_AIPK,
|
2019-11-03 19:25:51 +00:00
|
|
|
date_sent TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
2009-12-30 07:59:40 +00:00
|
|
|
section VARCHAR(32) NOT NULL,
|
|
|
|
username VARCHAR(32) NOT NULL,
|
|
|
|
address SCORE_INET NOT NULL,
|
|
|
|
priority INT NOT NULL,
|
2010-02-01 16:18:16 +00:00
|
|
|
message TEXT NOT NULL
|
2009-12-30 07:59:40 +00:00
|
|
|
");
|
2019-05-28 16:59:38 +00:00
|
|
|
//INDEX(section)
|
2019-11-03 19:49:52 +00:00
|
|
|
$this->set_version("ext_log_database_version", 1);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-30 07:59:40 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-10-26 15:13:28 +00:00
|
|
|
$sb = $event->panel->create_new_block("Logging (Database)");
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_choice_option("log_db_priority", [
|
2020-06-02 22:56:28 +00:00
|
|
|
LOGGING_LEVEL_NAMES[SCORE_LOG_DEBUG] => SCORE_LOG_DEBUG,
|
|
|
|
LOGGING_LEVEL_NAMES[SCORE_LOG_INFO] => SCORE_LOG_INFO,
|
|
|
|
LOGGING_LEVEL_NAMES[SCORE_LOG_WARNING] => SCORE_LOG_WARNING,
|
|
|
|
LOGGING_LEVEL_NAMES[SCORE_LOG_ERROR] => SCORE_LOG_ERROR,
|
|
|
|
LOGGING_LEVEL_NAMES[SCORE_LOG_CRITICAL] => SCORE_LOG_CRITICAL,
|
2019-05-28 16:59:38 +00:00
|
|
|
], "Debug Level: ");
|
|
|
|
}
|
2009-12-30 07:59:40 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
global $database, $user;
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($event->page_matches("log/view")) {
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::VIEW_EVENTLOG)) {
|
2019-12-15 15:31:44 +00:00
|
|
|
$t = new LogTable($database->raw_db());
|
|
|
|
$t->inputs = $_GET;
|
2019-12-07 23:00:52 +00:00
|
|
|
$this->theme->display_events($t->table($t->query()), $t->paginator());
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-08-02 19:54:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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::VIEW_EVENTLOG)) {
|
|
|
|
$event->add_nav_link("event_log", new Link('log/view'), "Event Log");
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-30 07:59:40 +00:00
|
|
|
|
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::VIEW_EVENTLOG)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$event->add_link("Event Log", make_link("log/view"));
|
|
|
|
}
|
|
|
|
}
|
2009-12-30 07:59:40 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onLog(LogEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config, $database, $user;
|
2010-03-08 21:56:31 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$username = ($user && $user->name) ? $user->name : "null";
|
2011-01-26 14:55:44 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// not installed yet...
|
2019-11-03 19:49:52 +00:00
|
|
|
if ($this->get_version("ext_log_database_version") < 1) {
|
2019-05-28 16:59:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-03-08 21:56:31 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($event->priority >= $config->get_int("log_db_priority")) {
|
|
|
|
$database->execute("
|
2009-12-30 07:59:40 +00:00
|
|
|
INSERT INTO score_log(date_sent, section, priority, username, address, message)
|
2011-01-01 16:28:04 +00:00
|
|
|
VALUES(now(), :section, :priority, :username, :address, :message)
|
2019-05-28 16:59:38 +00:00
|
|
|
", [
|
2023-11-11 21:49:12 +00:00
|
|
|
"section" => $event->section, "priority" => $event->priority, "username" => $username,
|
|
|
|
"address" => get_real_ip(), "message" => $event->message
|
2019-05-28 16:59:38 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2009-12-30 07:59:40 +00:00
|
|
|
}
|