2009-12-30 07:59:40 +00:00
|
|
|
<?php
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class LogDatabase extends Extension
|
|
|
|
{
|
|
|
|
public function onInitExt(InitExtEvent $event)
|
|
|
|
{
|
|
|
|
global $config;
|
2019-11-03 17:19:37 +00:00
|
|
|
$config->set_default_int("log_db_priority", SCORE_LOG_INFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event) {
|
|
|
|
global $config, $database;
|
2009-12-30 07:59:40 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($config->get_int("ext_log_database_version") < 1) {
|
|
|
|
$database->create_table("score_log", "
|
2009-12-30 07:59:40 +00:00
|
|
|
id SCORE_AIPK,
|
2019-11-03 18:28:05 +00:00
|
|
|
date_sent TIMESTAMP NOT NULL,
|
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)
|
|
|
|
$config->set_int("ext_log_database_version", 1);
|
|
|
|
}
|
|
|
|
}
|
2009-12-30 07:59:40 +00:00
|
|
|
|
2019-05-28 16:59:38 +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
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
2019-10-02 09:49:32 +00:00
|
|
|
global $cache, $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-05-28 16:59:38 +00:00
|
|
|
$wheres = [];
|
|
|
|
$args = [];
|
|
|
|
$page_num = int_escape($event->get_arg(0));
|
|
|
|
if ($page_num <= 0) {
|
|
|
|
$page_num = 1;
|
|
|
|
}
|
|
|
|
if (!empty($_GET["time-start"])) {
|
|
|
|
$wheres[] = "date_sent > :time_start";
|
|
|
|
$args["time_start"] = $_GET["time-start"];
|
|
|
|
}
|
|
|
|
if (!empty($_GET["time-end"])) {
|
|
|
|
$wheres[] = "date_sent < :time_end";
|
|
|
|
$args["time_end"] = $_GET["time-end"];
|
|
|
|
}
|
|
|
|
if (!empty($_GET["module"])) {
|
2019-10-15 18:46:08 +00:00
|
|
|
$wheres[] = $database->scoreql_to_sql("SCORE_STRNORM(section) = SCORE_STRNORM(:module)");
|
2019-05-28 16:59:38 +00:00
|
|
|
$args["module"] = $_GET["module"];
|
|
|
|
}
|
|
|
|
if (!empty($_GET["user"])) {
|
2019-06-20 15:42:32 +00:00
|
|
|
if ($database->get_driver_name() == DatabaseDriver::PGSQL) {
|
2019-05-28 16:59:38 +00:00
|
|
|
if (preg_match("#\d+\.\d+\.\d+\.\d+(/\d+)?#", $_GET["user"])) {
|
2019-10-15 18:46:08 +00:00
|
|
|
$wheres[] = $database->scoreql_to_sql("(SCORE_STRNORM(username) = SCORE_STRNORM(:user1) OR SCORE_STRNORM(text(address)) = SCORE_STRNORM(:user2))");
|
2019-05-28 16:59:38 +00:00
|
|
|
$args["user1"] = $_GET["user"];
|
|
|
|
$args["user2"] = $_GET["user"] . "/32";
|
|
|
|
} else {
|
2019-10-15 18:46:08 +00:00
|
|
|
$wheres[] = $database->scoreql_to_sql("SCORE_STRNORM(username) = SCORE_STRNORM(:user)");
|
2019-05-28 16:59:38 +00:00
|
|
|
$args["user"] = $_GET["user"];
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-15 18:46:08 +00:00
|
|
|
$wheres[] = $database->scoreql_to_sql("(SCORE_STRNORM(username) = SCORE_STRNORM(:user1) OR SCORE_STRNORM(address) = SCORE_STRNORM(:user2))");
|
2019-05-28 16:59:38 +00:00
|
|
|
$args["user1"] = $_GET["user"];
|
|
|
|
$args["user2"] = $_GET["user"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($_GET["priority"])) {
|
|
|
|
$wheres[] = "priority >= :priority";
|
|
|
|
$args["priority"] = int_escape($_GET["priority"]);
|
|
|
|
} else {
|
|
|
|
$wheres[] = "priority >= :priority";
|
|
|
|
$args["priority"] = 20;
|
|
|
|
}
|
|
|
|
if (!empty($_GET["message"])) {
|
|
|
|
$wheres[] = $database->scoreql_to_sql("SCORE_STRNORM(message) LIKE SCORE_STRNORM(:message)");
|
|
|
|
$args["message"] = "%" . $_GET["message"] . "%";
|
|
|
|
}
|
|
|
|
$where = "";
|
|
|
|
if (count($wheres) > 0) {
|
|
|
|
$where = "WHERE ";
|
|
|
|
$where .= join(" AND ", $wheres);
|
|
|
|
}
|
2010-02-12 17:22:19 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$limit = 50;
|
|
|
|
$offset = ($page_num-1) * $limit;
|
2019-10-02 09:49:32 +00:00
|
|
|
$page_total = $cache->get("event_log_length");
|
2019-05-28 16:59:38 +00:00
|
|
|
if (!$page_total) {
|
|
|
|
$page_total = $database->get_one("SELECT count(*) FROM score_log $where", $args);
|
|
|
|
// don't cache a length of zero when the extension is first installed
|
|
|
|
if ($page_total > 10) {
|
2019-10-02 09:49:32 +00:00
|
|
|
$cache->set("event_log_length", $page_total, 600);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-12 17:22:19 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$args["limit"] = $limit;
|
|
|
|
$args["offset"] = $offset;
|
|
|
|
$events = $database->get_all("SELECT * FROM score_log $where ORDER BY id DESC LIMIT :limit OFFSET :offset", $args);
|
2010-02-12 17:22:19 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->theme->display_events($events, $page_num, 100);
|
|
|
|
}
|
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::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
|
|
|
|
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::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
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onLog(LogEvent $event)
|
|
|
|
{
|
|
|
|
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...
|
|
|
|
if ($config->get_int("ext_log_database_version") < 1) {
|
|
|
|
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
|
|
|
", [
|
|
|
|
"section"=>$event->section, "priority"=>$event->priority, "username"=>$username,
|
|
|
|
"address"=>$_SERVER['REMOTE_ADDR'], "message"=>$event->message
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2009-12-30 07:59:40 +00:00
|
|
|
}
|