70 lines
No EOL
1.7 KiB
PHP
70 lines
No EOL
1.7 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()
|
|
{
|
|
$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()
|
|
{
|
|
$this->xmpp_client->connect();
|
|
}
|
|
|
|
public function sendMessage(string $text)
|
|
{
|
|
foreach($this->config->xmpp_recipients as $receipient)
|
|
{
|
|
$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 getChatType(int $type)
|
|
{
|
|
if($type === 1)
|
|
{
|
|
$return_type = "chat";
|
|
}
|
|
elseif($type === 2)
|
|
{
|
|
$return_type = "groupchat";
|
|
}
|
|
else
|
|
{
|
|
$return_type = "chat";
|
|
}
|
|
|
|
return $return_type;
|
|
}
|
|
|
|
public function disconnect()
|
|
{
|
|
$this->xmpp_client->disconnect();
|
|
}
|
|
}
|
|
|
|
|
|
?>
|