modify filters

This commit is contained in:
Mateusz Pieła 2021-05-15 11:26:20 +02:00
parent 096f91cd74
commit e4c80782f8
4 changed files with 114 additions and 12 deletions

View file

@ -2,18 +2,19 @@
namespace classes;
use classes\filter\gitea;
use classes\webhook_comp\discord;
class webhook extends core
{
private $json;
private $filter;
private $comp;
public function __construct(string $json,string $filter)
public function __construct(string $json,string $comp)
{
parent::__construct();
if($json)
{
$this->filter = $filter;
$this->comp = $comp;
$this->json = json_decode($json, false);
}
else
@ -24,13 +25,33 @@ class webhook extends core
public function getMessage()
{
if($this->filter === "gitea")
if($this->comp === "gitea")
{
$filter = new gitea();
return($filter->formatMessageToReadable($this->json->text));
$comp = new discord($this->json);
return($comp->readableMessage());
}
return $this->json->text;
if($this->comp === "grafana")
{
$comp = new grafana($this->json);
return($comp->readableMessage());
}
if(isset($this->json->text))
{
return $this->json->text;
}
if(isset($this->json->message) && isset($this->json->imageUrl))
{
return $this->json->message . PHP_EOL . $this->json->imageUrl;
}
if(isset($this->json->message))
{
return $this->json->message;
}
throw new \Exception("Error not compatible query!");
}
public function getEventFilter()
@ -48,4 +69,4 @@ class webhook extends core
}
}
?>
?>

View file

@ -0,0 +1,37 @@
<?php
namespace classes\webhook_comp;
class discord
{
private $json;
private $readable = "";
public function __construct($json)
{
$this->json = $json;
}
public function readableMessage()
{
if(!empty($this->json->content))
{
return $this->json->content;
}
if(!empty($this->json->embeds->title))
{
$this->readable .= $this->json->embeds->title . PHP_EOL;
}
if(!empty($this->json->embeds->description))
{
$this->readable .= preg_replace("/(?:__|[*#])|\[(.*?)\]\(.*?\)/"," ",$this->json->embeds->description) . PHP_EOL;
$this->readable .= $this->json->embeds->url;
}
return $this->readable;
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace classes\webhook_comp;
class grafana
{
private $json;
private $readable = "";
public function __construct($json)
{
$this->json = $json;
}
public function readableMessage()
{
if(!empty($this->json->title))
{
$this->readable .= $this->json->title;
}
if(!empty($this->json->message))
{
$this->readable .= $this->json->message;
}
if(!empty($this->json->state))
{
$this->readable .= $this->json->state;
}
return $this->readable;
}
}

View file

@ -77,8 +77,8 @@ class app
private function handleIncomingData(): void
{
$post = file_get_contents("php://input");
$filter = $this->checkIfFilterNeeded();
$wh = new Webhook($post,$filter);
$comp = $this->checkIfCompatibilityClassNeeded();
$wh = new Webhook($post,$comp);
$xmpp = new Xmpp();
$xmpp->start();
@ -87,13 +87,19 @@ class app
$xmpp->disconnect();
}
private function checkIfFilterNeeded()
private function checkIfCompatibilityClassNeeded()
{
if(array_key_exists("HTTP_X_GITEA_DELIVERY",$_SERVER))
{
return 'gitea';
}
if(isset($_SERVER['User-Agent']) && $_SERVER['User-Agent'] === "Grafana")
{
return 'grafana';
}
return false;
}
@ -115,4 +121,4 @@ class app
return $ip;
}
}
}