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/contrib/log_db/main.php

135 lines
3.9 KiB
PHP
Raw Normal View History

2009-12-30 07:59:40 +00:00
<?php
/*
* Name: Logging (Database)
* Author: Shish <webmaster@shishnet.org>
* Link: http://code.shishnet.org/shimmie2/
* Description: Keep a record of SCore events (in the database).
* Visibility: admin
2009-12-30 07:59:40 +00:00
*/
class LogDatabase extends Extension {
public function onInitExt(InitExtEvent $event) {
2009-12-30 07:59:40 +00:00
global $database;
global $config;
if($config->get_int("ext_log_database_version") < 1) {
$database->create_table("score_log", "
id SCORE_AIPK,
2010-02-01 16:18:16 +00:00
date_sent SCORE_DATETIME 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
");
2010-02-01 16:18:16 +00:00
//INDEX(section)
2009-12-30 07:59:40 +00:00
$config->set_int("ext_log_database_version", 1);
}
$config->set_default_int("log_db_priority", SCORE_LOG_INFO);
}
public function onSetupBuilding(SetupBuildingEvent $event) {
2009-12-30 07:59:40 +00:00
$sb = new SetupBlock("Logging (Database)");
$sb->add_choice_option("log_db_priority", array(
"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);
}
public function onPageRequest(PageRequestEvent $event) {
2009-12-30 07:59:40 +00:00
global $database, $user;
if($event->page_matches("log/view")) {
if($user->is_admin()) {
2010-02-03 13:57:39 +00:00
$wheres = array();
$args = array();
2010-02-12 17:22:19 +00:00
$page_num = int_escape($event->get_arg(0));
if($page_num <= 0) $page_num = 1;
2010-02-03 13:57:39 +00:00
if(!empty($_GET["time"])) {
2011-01-01 16:28:04 +00:00
$wheres[] = "date_sent LIKE :time";
$args["time"] = $_GET["time"]."%";
2010-02-03 13:57:39 +00:00
}
if(!empty($_GET["module"])) {
2011-01-01 16:28:04 +00:00
$wheres[] = "section = :module";
$args["module"] = $_GET["module"];
2010-02-03 13:57:39 +00:00
}
if(!empty($_GET["user"])) {
if($database->engine->name == "pgsql") {
if(preg_match("#\d+\.\d+\.\d+\.\d+(/\d+)?#", $_GET["user"])) {
2011-01-01 16:28:04 +00:00
$wheres[] = "(username = :user OR address << :user)";
}
else {
2011-01-01 16:28:04 +00:00
$wheres[] = "lower(username) = lower(:user)";
}
}
else {
2011-01-01 16:28:04 +00:00
$wheres[] = "(username = :user OR address = :user)";
}
2011-01-01 16:28:04 +00:00
$args["user"] = $_GET["user"];
2010-02-03 13:57:39 +00:00
}
if(!empty($_GET["priority"])) {
2011-01-01 16:28:04 +00:00
$wheres[] = "priority >= :priority";
$args["priority"] = int_escape($_GET["priority"]);
2010-02-03 13:57:39 +00:00
}
2010-02-12 17:22:19 +00:00
else {
2011-01-01 16:28:04 +00:00
$wheres[] = "priority >= :priority";
$args["priority"] = 20;
2010-02-12 17:22:19 +00:00
}
2010-02-03 13:57:39 +00:00
$where = "";
if(count($wheres) > 0) {
$where = "WHERE ";
$where .= join(" AND ", $wheres);
}
2010-02-12 17:22:19 +00:00
$limit = 50;
$offset = ($page_num-1) * $limit;
$page_total = $database->cache->get("event_log_length");
if(!$page_total) {
2011-01-01 16:28:04 +00:00
$page_total = $database->get_one("SELECT count(*) FROM score_log $where", $args);
2010-02-12 17:22:19 +00:00
// don't cache a length of zero when the extension is first installed
if($page_total > 10) {
$database->cache->set("event_log_length", 600);
}
}
2011-01-01 16:28:04 +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
$this->theme->display_events($events, $page_num, 100);
2009-12-30 07:59:40 +00:00
}
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
2010-01-05 10:52:23 +00:00
global $user;
if($user->is_admin()) {
$event->add_link("Event Log", make_link("log/view"));
}
2009-12-30 07:59:40 +00:00
}
public function onLog(LogEvent $event) {
2009-12-30 07:59:40 +00:00
global $config, $database, $user;
2011-01-26 14:55:44 +00:00
$username = ($user && $user->name) ? $user->name : "null";
// not installed yet...
if($config->get_int("ext_log_database_version") < 1) return;
2009-12-30 07:59:40 +00:00
if($event->priority >= $config->get_int("log_db_priority")) {
$database->execute("
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)
", array(
2011-01-26 14:55:44 +00:00
"section"=>$event->section, "priority"=>$event->priority, "username"=>$username,
2011-01-01 16:28:04 +00:00
"address"=>$_SERVER['REMOTE_ADDR'], "message"=>$event->message
));
2009-12-30 07:59:40 +00:00
}
}
}
?>