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

58 lines
1.4 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
2012-02-26 15:24:37 +00:00
namespace Shimmie2;
class LogNet extends Extension
{
private int $count = 0;
2012-03-19 21:15:54 +00:00
public function onInitExt(InitExtEvent $event): void
{
global $config;
$config->set_default_string("log_net_host", "127.0.0.1:35353");
}
public function onLog(LogEvent $event): void
{
global $user;
2012-02-26 15:24:37 +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";
$str = sprintf("%-15s %-10s: %s", get_real_ip(), $username, $event->message);
$this->msg($str);
} elseif ($this->count == 10) {
$this->msg('suppressing flood, check the web log');
}
}
}
private function msg(string $data): void
{
global $config;
$host = $config->get_string("log_net_host");
if (!$host) {
return;
}
try {
$parts = explode(":", $host);
$host = $parts[0];
2020-01-26 13:19:35 +00:00
$port = (int)$parts[1];
$fp = fsockopen("udp://$host", $port, $errno, $errstr);
2023-11-11 21:49:12 +00:00
if (!$fp) {
return;
}
fwrite($fp, "$data\n");
fclose($fp);
2023-01-11 11:15:26 +00:00
} catch (\Exception $e) {
/* logging errors shouldn't break everything */
}
}
2012-02-26 15:24:37 +00:00
}