2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2012-02-26 15:24:37 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class LogNet extends Extension
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
private int $count = 0;
|
2012-03-19 21:15:54 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onInitExt(InitExtEvent $event): void
|
2023-02-16 19:30:24 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$config->set_default_string("log_net_host", "127.0.0.1:35353");
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onLog(LogEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2012-02-26 15:24:37 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($event->priority > 10) {
|
|
|
|
$this->count++;
|
|
|
|
if ($this->count < 10) {
|
|
|
|
// TODO: colour based on event->priority
|
|
|
|
$username = ($user && $user->name) ? $user->name : "Anonymous";
|
2022-01-17 17:06:20 +00:00
|
|
|
$str = sprintf("%-15s %-10s: %s", get_real_ip(), $username, $event->message);
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->msg($str);
|
|
|
|
} elseif ($this->count == 10) {
|
|
|
|
$this->msg('suppressing flood, check the web log');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-22 18:19:13 +00:00
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
private function msg(string $data): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
2023-02-16 19:30:24 +00:00
|
|
|
$host = $config->get_string("log_net_host");
|
2012-06-22 18:19:13 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if (!$host) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-22 18:19:13 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
try {
|
|
|
|
$parts = explode(":", $host);
|
|
|
|
$host = $parts[0];
|
2020-01-26 13:19:35 +00:00
|
|
|
$port = (int)$parts[1];
|
2019-05-28 16:59:38 +00:00
|
|
|
$fp = fsockopen("udp://$host", $port, $errno, $errstr);
|
2023-11-11 21:49:12 +00:00
|
|
|
if (!$fp) {
|
2019-05-28 16:59:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
fwrite($fp, "$data\n");
|
|
|
|
fclose($fp);
|
2023-01-11 11:15:26 +00:00
|
|
|
} catch (\Exception $e) {
|
2019-05-28 16:59:38 +00:00
|
|
|
/* logging errors shouldn't break everything */
|
|
|
|
}
|
|
|
|
}
|
2012-02-26 15:24:37 +00:00
|
|
|
}
|