webhook2xmpp/classes/xmpp.php
2021-04-27 09:43:13 +02:00

91 lines
No EOL
2.3 KiB
PHP

<?php
namespace classes;
use Norgul\Xmpp\Options;
use Norgul\Xmpp\XmppClient;
class xmpp extends core{
private $xmpp_options;
private $xmpp_client;
public function __construct()
{
parent::__construct();
$this->xmpp_options = new Options();
}
public function start(): void
{
$this->xmpp_options->setHost($this->config->xmpp_server);
$this->xmpp_options->setUseTls($this->config->xmpp_tls);
$this->xmpp_options->setUsername($this->config->xmpp_username);
$this->xmpp_options->setPassword($this->config->xmpp_password);
$this->xmpp_client = new XmppClient($this->xmpp_options);
}
public function connect(): void
{
$this->xmpp_client->connect();
}
public function sendMessage(string $text,string $event_filter): void
{
foreach($this->config->xmpp_recipients as $receipient)
{
if($this->checkIfEventFilter($event_filter,$receipient['event_filter']))
{
continue;
}
$type = $this->getChatType($receipient['type']);
if($type === "groupchat") {
$this->xmpp_client->send("<presence from='".$this->xmpp_options->bareJid()."/".$this->xmpp_options->getResource()."' to='".$receipient["jid"] . "/" . $receipient["muc_nickname"]. "'><x xmlns='http://jabber.org/protocol/muc'/></presence>");
}
$this->xmpp_client->message->send($text,$receipient['jid'],$type);
}
}
private function checkIfEventFilter(string $event_filter,string $receipient_event_filter)
{
$check_filter = false;
if(!empty($event_filter)) {
if ($event_filter !== false && $event_filter !== "all") {
$check_filter = true;
}
if ($check_filter && $receipient_event_filter !== $event_filter) {
return true;
}
}
return false;
}
private function getChatType(int $type): string
{
if($type === 1)
{
$return_type = "chat";
}
elseif($type === 2)
{
$return_type = "groupchat";
}
else
{
$return_type = "chat";
}
return $return_type;
}
public function disconnect(): void
{
$this->xmpp_client->disconnect();
}
}
?>