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

76 lines
1.8 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
2012-06-22 18:13:57 +00:00
namespace Shimmie2;
class LiveFeed extends Extension
{
public function onSetupBuilding(SetupBuildingEvent $event)
{
$sb = $event->panel->create_new_block("Live Feed");
$sb->add_text_option("livefeed_host", "IP:port to send events to: ");
}
2012-06-22 18:13:57 +00:00
public function onUserCreation(UserCreationEvent $event)
{
$this->msg("New user created: {$event->username}");
}
2012-06-22 18:13:57 +00:00
public function onImageAddition(ImageAdditionEvent $event)
{
global $user;
$this->msg(
make_http(make_link("post/view/".$event->image->id))." - ".
"new post by ".$user->name
);
}
public function onTagSet(TagSetEvent $event)
{
$this->msg(
make_http(make_link("post/view/".$event->image->id))." - ".
"tags set to: ".Tag::implode($event->tags)
);
}
2012-06-22 18:13:57 +00:00
public function onCommentPosting(CommentPostingEvent $event)
{
global $user;
$this->msg(
make_http(make_link("post/view/".$event->image_id))." - ".
$user->name . ": " . str_replace("\n", " ", $event->comment)
);
}
2012-06-22 18:13:57 +00:00
public function get_priority(): int
{
return 99;
}
2012-06-22 18:13:57 +00:00
private function msg(string $data)
{
global $config;
$host = $config->get_string("livefeed_host", "127.0.0.1:25252");
2012-06-22 18:13:57 +00:00
if (!$host) {
return;
}
2012-06-22 18:13:57 +00:00
try {
$parts = explode(":", $host);
2012-06-22 18:13:57 +00:00
$host = $parts[0];
2020-01-26 13:19:35 +00:00
$port = (int)$parts[1];
2012-06-22 18:13:57 +00:00
$fp = fsockopen("udp://$host", $port, $errno, $errstr);
2023-11-11 21:49:12 +00:00
if (!$fp) {
return;
}
fwrite($fp, "$data\n");
2012-06-22 18:13:57 +00:00
fclose($fp);
2023-01-11 11:15:26 +00:00
} catch (\Exception $e) {
/* logging errors shouldn't break everything */
2012-06-22 18:13:57 +00:00
}
}
}