This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/log_db/main.php

164 lines
4.9 KiB
PHP
Raw Normal View History

2009-12-30 07:59:40 +00:00
<?php
2019-12-01 18:58:13 +00:00
use function MicroHTML\{A,SPAN};
use MicroCRUD\Column;
use MicroCRUD\DateTimeColumn;
use MicroCRUD\TextColumn;
use MicroCRUD\Table;
class ActorColumn extends Column {
public function __construct($name, $title)
{
parent::__construct($name, $title, "((username=:$name) OR (address=:$name))");
}
public function display($row)
{
if ($row['username'] == "Anonymous") {
return $row["address"];
} else {
return A(["href"=>make_link("user/{$row['username']}"), "title"=>$row['address']], $row['username']);
}
}
}
class MessageColumn extends TextColumn {
public function display($row)
{
$c = "#000";
switch ($row['priority']) {
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;
}
return SPAN(["style"=>"color: $c"], $this->scan_entities($row[$this->name]));
}
protected function scan_entities($line)
{
return preg_replace_callback("/Image #(\d+)/s", [$this, "link_image"], $line);
}
protected function link_image($id)
{
$iid = int_escape($id[1]);
return "<a href='".make_link("post/view/$iid")."'>Image #$iid</a>";
}
}
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;
$this->limit = 1000000;
$this->columns = [
new DateTimeColumn("date_sent", "Time"),
new TextColumn("section", "Module"),
new ActorColumn("username_or_address", "User"),
new MessageColumn("message", "Message")
];
$this->order_by = ["date_sent DESC"];
$this->table_attrs = ["class" => "zebra"];
}
}
class LogDatabase extends Extension
{
public function onInitExt(InitExtEvent $event)
{
global $config;
$config->set_default_int("log_db_priority", SCORE_LOG_INFO);
}
2019-11-03 18:28:38 +00:00
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event)
{
global $config, $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) {
$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
");
//INDEX(section)
2019-11-03 19:49:52 +00:00
$this->set_version("ext_log_database_version", 1);
}
}
2009-12-30 07:59:40 +00:00
public function onSetupBuilding(SetupBuildingEvent $event)
{
$sb = new SetupBlock("Logging (Database)");
$sb->add_choice_option("log_db_priority", [
"Debug" => SCORE_LOG_DEBUG,
"Info" => SCORE_LOG_INFO,
"Warning" => SCORE_LOG_WARNING,
"Error" => SCORE_LOG_ERROR,
"Critical" => SCORE_LOG_CRITICAL,
], "Debug Level: ");
$event->panel->add_block($sb);
}
2009-12-30 07:59:40 +00:00
public function onPageRequest(PageRequestEvent $event)
{
global $cache, $database, $user;
if ($event->page_matches("log/view")) {
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::VIEW_EVENTLOG)) {
2019-12-07 23:00:52 +00:00
$t = new LogTable($database->raw_db());
$t->inputs = $_GET;
$this->theme->display_events($t->table($t->query()), $t->paginator());
}
}
}
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
{
global $user;
2019-09-29 13:30:55 +00:00
if ($event->parent==="system") {
if ($user->can(Permissions::VIEW_EVENTLOG)) {
$event->add_nav_link("event_log", new Link('log/view'), "Event Log");
}
}
}
2009-12-30 07:59:40 +00:00
public function onUserBlockBuilding(UserBlockBuildingEvent $event)
{
global $user;
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::VIEW_EVENTLOG)) {
$event->add_link("Event Log", make_link("log/view"));
}
}
2009-12-30 07:59:40 +00:00
public function onLog(LogEvent $event)
{
global $config, $database, $user;
$username = ($user && $user->name) ? $user->name : "null";
2011-01-26 14:55:44 +00:00
// not installed yet...
2019-11-03 19:49:52 +00:00
if ($this->get_version("ext_log_database_version") < 1) {
return;
}
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)
", [
"section"=>$event->section, "priority"=>$event->priority, "username"=>$username,
"address"=>$_SERVER['REMOTE_ADDR'], "message"=>$event->message
]);
}
}
2009-12-30 07:59:40 +00:00
}