From 5411c00ee94cdd7a1c1a9d96a63fc10e8b7b73df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Pie=C5=82a?= Date: Mon, 26 Apr 2021 23:01:23 +0200 Subject: [PATCH] Working version of project --- .gitignore | 3 ++ classes/core.php | 26 ++++++++++++++ classes/webhook.php | 27 ++++++++++++++ classes/webhook/core.php | 11 ------ classes/xmpp.php | 70 ++++++++++++++++++++++++++++++++++++ classes/xmpp/core.php | 29 --------------- config/config.php.dist | 73 ++++++++++++++++++++++++++++++++++++++ includes/app.php | 65 ++++++++++++++++++++++++++++++--- includes/core_autoload.php | 1 - 9 files changed, 259 insertions(+), 46 deletions(-) create mode 100644 .gitignore create mode 100644 classes/core.php create mode 100644 classes/webhook.php delete mode 100644 classes/webhook/core.php create mode 100644 classes/xmpp.php delete mode 100644 classes/xmpp/core.php create mode 100644 config/config.php.dist diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..df79b48 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/vendor/ +/.idea/ +/config/config.php diff --git a/classes/core.php b/classes/core.php new file mode 100644 index 0000000..201aab9 --- /dev/null +++ b/classes/core.php @@ -0,0 +1,26 @@ +config = new Config(); + } +} \ No newline at end of file diff --git a/classes/webhook.php b/classes/webhook.php new file mode 100644 index 0000000..cf702be --- /dev/null +++ b/classes/webhook.php @@ -0,0 +1,27 @@ +json = json_decode($json, false); + } + else + { + throw new \Exception("JSON is required to use this class"); + } + } + + public function getMessage() + { + return $this->json->text; + } +} + +?> \ No newline at end of file diff --git a/classes/webhook/core.php b/classes/webhook/core.php deleted file mode 100644 index d099d75..0000000 --- a/classes/webhook/core.php +++ /dev/null @@ -1,11 +0,0 @@ - \ No newline at end of file diff --git a/classes/xmpp.php b/classes/xmpp.php new file mode 100644 index 0000000..42a65b9 --- /dev/null +++ b/classes/xmpp.php @@ -0,0 +1,70 @@ +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(""); + } + + $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(); + } +} + + +?> \ No newline at end of file diff --git a/classes/xmpp/core.php b/classes/xmpp/core.php deleted file mode 100644 index 51d478d..0000000 --- a/classes/xmpp/core.php +++ /dev/null @@ -1,29 +0,0 @@ - \ No newline at end of file diff --git a/config/config.php.dist b/config/config.php.dist new file mode 100644 index 0000000..92dd6b0 --- /dev/null +++ b/config/config.php.dist @@ -0,0 +1,73 @@ + "", "type" => 1, "muc_nickname" => ""), + ); +} \ No newline at end of file diff --git a/includes/app.php b/includes/app.php index e815b80..cc5a2ca 100644 --- a/includes/app.php +++ b/includes/app.php @@ -1,18 +1,73 @@ config = new Config(); } + /** + * Main application loop + */ + public function start(){ + if($this->checkIfRequestMethodIsPost() && $this->checkAuthorization()) { + $this->handleIncomingData(); + } + } + + /** + * Check if client is authorized to post via this gate + * @return bool + */ + private function checkAuthorization() + { + if(isset($_GET['apikey']) && $_GET['apikey'] === $this->config->api_key) + { + return true; + } + else + { + die("ERROR: Client not authenticated to use this application"); + } + return false; + } + + /** + * Check if request method is post + * @return bool + */ + private function checkIfRequestMethodIsPost() + { + if($_SERVER["REQUEST_METHOD"]) + { + return true; + } + + return false; + } + + + private function handleIncomingData() + { + $post = file_get_contents("php://input"); + $wh = new Webhook($post); + $xmpp = new Xmpp(); + + $xmpp->start(); + $xmpp->connect(); + $xmpp->sendMessage($wh->getMessage()); + $xmpp->disconnect(); + } } \ No newline at end of file diff --git a/includes/core_autoload.php b/includes/core_autoload.php index 83b3597..94d9068 100644 --- a/includes/core_autoload.php +++ b/includes/core_autoload.php @@ -3,7 +3,6 @@ require ROOT_DIR . "/vendor/autoload.php"; function autoLoader($classname) { $file = ROOT_DIR . '\\' . $classname . '.php'; - print_r($file); $file = str_replace('\\', DIRECTORY_SEPARATOR, $file); include_once $file; }