Working version of project

This commit is contained in:
Mateusz Pieła 2021-04-26 23:01:23 +02:00
parent 0b21896cd5
commit 5411c00ee9
9 changed files with 259 additions and 46 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/vendor/
/.idea/
/config/config.php

26
classes/core.php Normal file
View file

@ -0,0 +1,26 @@
<?php
/*
* The MIT License (MIT)
*
* Copyright (c) 2021 Mateusz Pieła
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Classes;
use Config\Config;
class core
{
protected $config;
public function __construct(){
$this->config = new Config();
}
}

27
classes/webhook.php Normal file
View file

@ -0,0 +1,27 @@
<?php
namespace Classes;
class Webhook extends Core
{
private $json;
public function __construct(string $json)
{
parent::__construct();
if($json)
{
$this->json = json_decode($json, false);
}
else
{
throw new \Exception("JSON is required to use this class");
}
}
public function getMessage()
{
return $this->json->text;
}
}
?>

View file

@ -1,11 +0,0 @@
<?php
namespace Classes\Webhook;
class Core
{
public function parseJson(){
}
}
?>

70
classes/xmpp.php Normal file
View file

@ -0,0 +1,70 @@
<?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();
}
}
?>

View file

@ -1,29 +0,0 @@
<?php
namespace Classes\Xmpp;
class Core {
public function start()
{
}
public function connect()
{
}
public function isConnected()
{
}
public function sendMessage()
{
}
}
?>

73
config/config.php.dist Normal file
View file

@ -0,0 +1,73 @@
<?php
/*
* The MIT License (MIT)
*
* Copyright (c) 2021 Mateusz Pieła
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Config;
class config
{
/**
* Api key authorization
* @var string
*/
public $api_key = "";
/**
* Toogle IP Whitelist
* @var bool
*/
public $ip_whitelist = false;
/**
* List of ip whitelisted to access this webhook2xmpp
* @var array
*/
public $ips_whitelist_list = array(
"127.0.0.1",
);
/**
* XMPP Server
*/
public $xmpp_server = "";
/**
* XMPP Use TLS
*/
public $xmpp_tls = true;
/**
* XMPP JID or Username
*/
public $xmpp_username = "";
/**
* XMPP Password
*/
public $xmpp_password = "";
/**
* XMPP Recipients
* Format JID,Type,MUC Nickname
*
* JID
*
* Type
* 1 = Normal chat
* 2 = Group chat
*
*/
public $xmpp_recipients = array(
array("jid" => "", "type" => 1, "muc_nickname" => ""),
);
}

View file

@ -1,18 +1,73 @@
<?php
namespace Includes;
use Classes\Xmpp;
use Classes\Webhook;
use Config\Config;
/**
* Main Application Class
* @package Includes
*/
class App
{
protected $config;
/**
* Start application
*/
public function start(){
print("SERVING WEBHOOK API");
public function __construct()
{
$this->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();
}
}

View file

@ -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;
}