From 0daf2494d2aee27a71f6f42419d0fc22fc9cc62b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Pie=C5=82a?= Date: Tue, 27 Apr 2021 09:06:56 +0200 Subject: [PATCH] Prototype gitea filter --- classes/filter/gitea.php | 27 +++++++++++++++++++++++++++ classes/webhook.php | 12 +++++++++++- composer.json | 2 ++ includes/app.php | 12 +++++++++++- 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 classes/filter/gitea.php diff --git a/classes/filter/gitea.php b/classes/filter/gitea.php new file mode 100644 index 0000000..3c54474 --- /dev/null +++ b/classes/filter/gitea.php @@ -0,0 +1,27 @@ +\]])/', '',$message); + $message = preg_replace('/\|.*(?=http)/',' ',$message); + $message = preg_replace('/\|(\w+)/', '', $message); + return $message; + } +} \ No newline at end of file diff --git a/classes/webhook.php b/classes/webhook.php index c1afa9b..a22cb90 100644 --- a/classes/webhook.php +++ b/classes/webhook.php @@ -1,15 +1,19 @@ filter = $filter ?? NULL; $this->json = json_decode($json, false); } else @@ -20,6 +24,12 @@ class webhook extends core public function getMessage() { + if($this->filter === "gitea") + { + $filter = new gitea(); + return($filter->formatMessageToReadable($this->json->text)); + } + return $this->json->text; } } diff --git a/composer.json b/composer.json index 033827c..737685b 100644 --- a/composer.json +++ b/composer.json @@ -1,4 +1,6 @@ { + "name": "mateusz/webhook2xmpp", + "description": "", "require": { "norgul/xmpp-php": "^2.2" } diff --git a/includes/app.php b/includes/app.php index c371288..d948a98 100644 --- a/includes/app.php +++ b/includes/app.php @@ -77,7 +77,8 @@ class app private function handleIncomingData(): void { $post = file_get_contents("php://input"); - $wh = new Webhook($post); + $filter = $this->checkIfFilterNeeded(); + $wh = new Webhook($post,$filter); $xmpp = new Xmpp(); $xmpp->start(); @@ -86,6 +87,15 @@ class app $xmpp->disconnect(); } + private function checkIfFilterNeeded() + { + if(in_array("X-Gitea",$_SERVER)) + { + return 'gitea'; + } + return false; + } + private function getRealIPAddr() { if (!empty($_SERVER['HTTP_CLIENT_IP']))