PSR-2. I'm not a huge fan, but ugly consistency beats no consistency...

This commit is contained in:
Shish
2019-05-28 17:59:38 +01:00
parent 5ec3e89884
commit 34b05cca7c
295 changed files with 27094 additions and 24632 deletions

View File

@@ -7,13 +7,15 @@
* Visibility: admin
*/
class LogDatabase extends Extension {
public function onInitExt(InitExtEvent $event) {
global $database;
global $config;
class LogDatabase extends Extension
{
public function onInitExt(InitExtEvent $event)
{
global $database;
global $config;
if($config->get_int("ext_log_database_version") < 1) {
$database->create_table("score_log", "
if ($config->get_int("ext_log_database_version") < 1) {
$database->create_table("score_log", "
id SCORE_AIPK,
date_sent SCORE_DATETIME NOT NULL,
section VARCHAR(32) NOT NULL,
@@ -22,125 +24,129 @@ class LogDatabase extends Extension {
priority INT NOT NULL,
message TEXT NOT NULL
");
//INDEX(section)
$config->set_int("ext_log_database_version", 1);
}
//INDEX(section)
$config->set_int("ext_log_database_version", 1);
}
$config->set_default_int("log_db_priority", SCORE_LOG_INFO);
}
$config->set_default_int("log_db_priority", SCORE_LOG_INFO);
}
public function onSetupBuilding(SetupBuildingEvent $event) {
$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 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);
}
public function onPageRequest(PageRequestEvent $event) {
global $database, $user;
if($event->page_matches("log/view")) {
if($user->can("view_eventlog")) {
$wheres = array();
$args = array();
$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"])) {
$wheres[] = "section = :module";
$args["module"] = $_GET["module"];
}
if(!empty($_GET["user"])) {
if($database->get_driver_name() == "pgsql") {
if(preg_match("#\d+\.\d+\.\d+\.\d+(/\d+)?#", $_GET["user"])) {
$wheres[] = "(username = :user1 OR text(address) = :user2)";
$args["user1"] = $_GET["user"];
$args["user2"] = $_GET["user"] . "/32";
}
else {
$wheres[] = "lower(username) = lower(:user)";
$args["user"] = $_GET["user"];
}
}
else {
$wheres[] = "(username = :user1 OR address = :user2)";
$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);
}
public function onPageRequest(PageRequestEvent $event)
{
global $database, $user;
if ($event->page_matches("log/view")) {
if ($user->can("view_eventlog")) {
$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"])) {
$wheres[] = "section = :module";
$args["module"] = $_GET["module"];
}
if (!empty($_GET["user"])) {
if ($database->get_driver_name() == "pgsql") {
if (preg_match("#\d+\.\d+\.\d+\.\d+(/\d+)?#", $_GET["user"])) {
$wheres[] = "(username = :user1 OR text(address) = :user2)";
$args["user1"] = $_GET["user"];
$args["user2"] = $_GET["user"] . "/32";
} else {
$wheres[] = "lower(username) = lower(:user)";
$args["user"] = $_GET["user"];
}
} else {
$wheres[] = "(username = :user1 OR address = :user2)";
$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);
}
$limit = 50;
$offset = ($page_num-1) * $limit;
$page_total = $database->cache->get("event_log_length");
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) {
$database->cache->set("event_log_length", $page_total, 600);
}
}
$limit = 50;
$offset = ($page_num-1) * $limit;
$page_total = $database->cache->get("event_log_length");
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) {
$database->cache->set("event_log_length", $page_total, 600);
}
}
$args["limit"] = $limit;
$args["offset"] = $offset;
$events = $database->get_all("SELECT * FROM score_log $where ORDER BY id DESC LIMIT :limit OFFSET :offset", $args);
$args["limit"] = $limit;
$args["offset"] = $offset;
$events = $database->get_all("SELECT * FROM score_log $where ORDER BY id DESC LIMIT :limit OFFSET :offset", $args);
$this->theme->display_events($events, $page_num, 100);
}
}
}
$this->theme->display_events($events, $page_num, 100);
}
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
global $user;
if($user->can("view_eventlog")) {
$event->add_link("Event Log", make_link("log/view"));
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event)
{
global $user;
if ($user->can("view_eventlog")) {
$event->add_link("Event Log", make_link("log/view"));
}
}
public function onLog(LogEvent $event) {
global $config, $database, $user;
public function onLog(LogEvent $event)
{
global $config, $database, $user;
$username = ($user && $user->name) ? $user->name : "null";
$username = ($user && $user->name) ? $user->name : "null";
// not installed yet...
if($config->get_int("ext_log_database_version") < 1) return;
// not installed yet...
if ($config->get_int("ext_log_database_version") < 1) {
return;
}
if($event->priority >= $config->get_int("log_db_priority")) {
$database->execute("
if ($event->priority >= $config->get_int("log_db_priority")) {
$database->execute("
INSERT INTO score_log(date_sent, section, priority, username, address, message)
VALUES(now(), :section, :priority, :username, :address, :message)
", array(
"section"=>$event->section, "priority"=>$event->priority, "username"=>$username,
"address"=>$_SERVER['REMOTE_ADDR'], "message"=>$event->message
));
}
}
", [
"section"=>$event->section, "priority"=>$event->priority, "username"=>$username,
"address"=>$_SERVER['REMOTE_ADDR'], "message"=>$event->message
]);
}
}
}

View File

@@ -1,11 +1,13 @@
<?php
class LogDatabaseTest extends ShimmiePHPUnitTestCase {
public function testLog() {
$this->log_in_as_admin();
$this->get_page("log/view");
$this->get_page("log/view?module=core-image");
$this->get_page("log/view?time=2012-03-01");
$this->get_page("log/view?user=demo");
$this->get_page("log/view?priority=10");
}
class LogDatabaseTest extends ShimmiePHPUnitTestCase
{
public function testLog()
{
$this->log_in_as_admin();
$this->get_page("log/view");
$this->get_page("log/view?module=core-image");
$this->get_page("log/view?time=2012-03-01");
$this->get_page("log/view?user=demo");
$this->get_page("log/view?priority=10");
}
}

View File

@@ -1,18 +1,28 @@
<?php
class LogDatabaseTheme extends Themelet {
protected function heie($var) {
if(isset($_GET[$var])) return html_escape($_GET[$var]);
else return "";
}
class LogDatabaseTheme extends Themelet
{
protected function heie($var)
{
if (isset($_GET[$var])) {
return html_escape($_GET[$var]);
} else {
return "";
}
}
protected function ueie($var) {
if(isset($_GET[$var])) return $var."=".url_escape($_GET[$var]);
else return "";
}
protected function ueie($var)
{
if (isset($_GET[$var])) {
return $var."=".url_escape($_GET[$var]);
} else {
return "";
}
}
public function display_events($events, $page_num, $page_total) {
$table = "
public function display_events($events, $page_num, $page_total)
{
$table = "
<style>
.sizedinputs TD INPUT {
width: 100%;
@@ -42,74 +52,83 @@ class LogDatabaseTheme extends Themelet {
</form>
</thead>
<tbody>\n";
reset($events); // rewind to first element in array.
foreach($events as $event) {
$c = $this->pri_to_col($event['priority']);
$table .= "<tr style='color: $c'>";
$table .= "<td>".str_replace(" ", "&nbsp;", substr($event['date_sent'], 0, 19))."</td>";
$table .= "<td>".$event['section']."</td>";
if($event['username'] == "Anonymous") {
$table .= "<td>".$event['address']."</td>";
}
else {
$table .= "<td><span title='".$event['address']."'>".
"<a href='".make_link("user/".url_escape($event['username']))."'>".html_escape($event['username'])."</a>".
"</span></td>";
}
$table .= "<td colspan='3'>".$this->scan_entities(html_escape($event['message']))."</td>";
$table .= "</tr>\n";
}
$table .= "</tbody></table>";
reset($events); // rewind to first element in array.
foreach ($events as $event) {
$c = $this->pri_to_col($event['priority']);
$table .= "<tr style='color: $c'>";
$table .= "<td>".str_replace(" ", "&nbsp;", substr($event['date_sent'], 0, 19))."</td>";
$table .= "<td>".$event['section']."</td>";
if ($event['username'] == "Anonymous") {
$table .= "<td>".$event['address']."</td>";
} else {
$table .= "<td><span title='".$event['address']."'>".
"<a href='".make_link("user/".url_escape($event['username']))."'>".html_escape($event['username'])."</a>".
"</span></td>";
}
$table .= "<td colspan='3'>".$this->scan_entities(html_escape($event['message']))."</td>";
$table .= "</tr>\n";
}
$table .= "</tbody></table>";
global $page;
$page->set_title("Event Log");
$page->set_heading("Event Log");
$page->add_block(new NavBlock());
$page->add_block(new Block("Events", $table));
$this->display_paginator($page, "log/view", $this->get_args(), $page_num, $page_total);
}
global $page;
$page->set_title("Event Log");
$page->set_heading("Event Log");
$page->add_block(new NavBlock());
$page->add_block(new Block("Events", $table));
$this->display_paginator($page, "log/view", $this->get_args(), $page_num, $page_total);
}
protected function get_args() {
$args = "";
// Check if each arg is actually empty and skip it if so
if(strlen($this->ueie("time-start")))
$args .= $this->ueie("time-start")."&";
if(strlen($this->ueie("time-end")))
$args .= $this->ueie("time-end")."&";
if(strlen($this->ueie("module")))
$args .= $this->ueie("module")."&";
if(strlen($this->ueie("user")))
$args .= $this->ueie("user")."&";
if(strlen($this->ueie("message")))
$args .= $this->ueie("message")."&";
if(strlen($this->ueie("priority")))
$args .= $this->ueie("priority");
// If there are no args at all, set $args to null to prevent an unnecessary ? at the end of the paginator url
if(strlen($args) == 0)
$args = null;
return $args;
}
protected function get_args()
{
$args = "";
// Check if each arg is actually empty and skip it if so
if (strlen($this->ueie("time-start"))) {
$args .= $this->ueie("time-start")."&";
}
if (strlen($this->ueie("time-end"))) {
$args .= $this->ueie("time-end")."&";
}
if (strlen($this->ueie("module"))) {
$args .= $this->ueie("module")."&";
}
if (strlen($this->ueie("user"))) {
$args .= $this->ueie("user")."&";
}
if (strlen($this->ueie("message"))) {
$args .= $this->ueie("message")."&";
}
if (strlen($this->ueie("priority"))) {
$args .= $this->ueie("priority");
}
// If there are no args at all, set $args to null to prevent an unnecessary ? at the end of the paginator url
if (strlen($args) == 0) {
$args = null;
}
return $args;
}
protected function pri_to_col($pri) {
switch($pri) {
case SCORE_LOG_DEBUG: return "#999";
case SCORE_LOG_INFO: return "#000";
case SCORE_LOG_WARNING: return "#800";
case SCORE_LOG_ERROR: return "#C00";
case SCORE_LOG_CRITICAL: return "#F00";
default: return "";
}
}
protected function pri_to_col($pri)
{
switch ($pri) {
case SCORE_LOG_DEBUG: return "#999";
case SCORE_LOG_INFO: return "#000";
case SCORE_LOG_WARNING: return "#800";
case SCORE_LOG_ERROR: return "#C00";
case SCORE_LOG_CRITICAL: return "#F00";
default: return "";
}
}
protected function scan_entities($line) {
$line = preg_replace_callback("/Image #(\d+)/s", array($this, "link_image"), $line);
return $line;
}
protected function scan_entities($line)
{
$line = preg_replace_callback("/Image #(\d+)/s", [$this, "link_image"], $line);
return $line;
}
protected function link_image($id) {
$iid = int_escape($id[1]);
return "<a href='".make_link("post/view/$iid")."'>Image #$iid</a>";
}
protected function link_image($id)
{
$iid = int_escape($id[1]);
return "<a href='".make_link("post/view/$iid")."'>Image #$iid</a>";
}
}