From 84901a77e0ba5c86ccdd1ee24f1e3d75a2c85f3c Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Wed, 30 Oct 2024 19:11:23 +0200 Subject: [PATCH] Fixes add host pages --- app/classes/host.php | 118 ++++++++++++++++++++++++++++++ app/helpers/sanitize.php | 11 +++ app/pages/config.php | 20 ++++- app/templates/config-host-add.php | 49 +++++++++++++ app/templates/config-host.php | 36 +++++++++ doc/jilo-web.schema | 29 ++++++-- 6 files changed, 254 insertions(+), 9 deletions(-) create mode 100644 app/classes/host.php create mode 100644 app/templates/config-host-add.php create mode 100644 app/templates/config-host.php diff --git a/app/classes/host.php b/app/classes/host.php new file mode 100644 index 0000000..8b394fa --- /dev/null +++ b/app/classes/host.php @@ -0,0 +1,118 @@ +db = $database->getConnection(); + } + + // get details of a specified host ID (or all) in a specified platform ID + public function getHostDetails($platform_id = '', $host_id = '') { + $sql = 'SELECT + id, + address, + port, + platform_id, + name + FROM + hosts'; + + if ($platform_id !== '' && $host_id !== '') { + $sql .= ' WHERE platform_id = :platform_id AND id = :host_id'; + } elseif ($platform_id !== '') { + $sql .= ' WHERE platform_id = :platform_id'; + } elseif ($host_id !== '') { + $sql .= ' WHERE id = :host_id'; + } + + $query = $this->db->prepare($sql); + + if ($platform_id !== '') { + $query->bindParam(':platform_id', $platform_id); + } + if ($host_id !== '') { + $query->bindParam(':host_id', $host_id); + } + + $query->execute(); + + return $query->fetchAll(PDO::FETCH_ASSOC); + } + + + // add new host + public function addHost($newHost) { + try { + $sql = 'INSERT INTO hosts + (address, port, platform_id, name) + VALUES + (:address, :port, :platform_id, :name)'; + + $query = $this->db->prepare($sql); + $query->execute([ + ':address' => $newHost['address'], + ':port' => $newHost['port'], + ':platform_id' => $newHost['platform_id'], + ':name' => $newHost['name'], + ]); + + return true; + + } catch (Exception $e) { + return $e->getMessage(); + } + } + + // edit an existing host + public function editAgent($platform_id, $updatedAgent) { + try { + $sql = 'UPDATE jilo_agents SET + agent_type_id = :agent_type_id, + url = :url, + secret_key = :secret_key, + check_period = :check_period + WHERE + id = :agent_id + AND + platform_id = :platform_id'; + + $query = $this->db->prepare($sql); + $query->execute([ + ':agent_type_id' => $updatedAgent['agent_type_id'], + ':url' => $updatedAgent['url'], + ':secret_key' => $updatedAgent['secret_key'], + ':check_period' => $updatedAgent['check_period'], + ':agent_id' => $updatedAgent['id'], + ':platform_id' => $platform_id, + ]); + + return true; + + } catch (Exception $e) { + return $e->getMessage(); + } + } + + + // delete a host + public function deleteAgent($agent_id) { + try { + $sql = 'DELETE FROM jilo_agents + WHERE + id = :agent_id'; + + $query = $this->db->prepare($sql); + $query->bindParam(':agent_id', $agent_id); + + $query->execute(); + return true; + + } catch (Exception $e) { + return $e->getMessage(); + } + } + +} + +?> diff --git a/app/helpers/sanitize.php b/app/helpers/sanitize.php index 7d213b1..8b6e8a7 100644 --- a/app/helpers/sanitize.php +++ b/app/helpers/sanitize.php @@ -28,6 +28,17 @@ if (isset($_SESSION['error'])) { $error = htmlspecialchars($_SESSION['error']); // 'error' for errors } +// hosts +if (isset($_POST['address'])) { + $address = htmlspecialchars($_POST['address']); +} +if (isset($_POST['port'])) { + $port = htmlspecialchars($_POST['port']); +} +if (isset($_POST['name'])) { + $name = htmlspecialchars($_POST['name']); +} + // agents if (isset($_POST['type'])) { $type = htmlspecialchars($_POST['type']); diff --git a/app/pages/config.php b/app/pages/config.php index ec9ef47..ece6447 100644 --- a/app/pages/config.php +++ b/app/pages/config.php @@ -4,9 +4,11 @@ $action = $_REQUEST['action'] ?? ''; $agent = $_REQUEST['agent'] ?? ''; require '../app/classes/config.php'; +require '../app/classes/host.php'; require '../app/classes/agent.php'; $configObject = new Config(); +$hostObject = new Host($dbWeb); $agentObject = new Agent($dbWeb); // if a form is submitted, it's from the edit page @@ -17,8 +19,23 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { // $content = file_get_contents($config_file); // $updatedContent = $content; + // new host adding + if (isset($_POST['new']) && isset($_POST['item']) && $_POST['new'] === 'true' && $_POST['item'] === 'host') { + $newHost = [ + 'address' => $address, + 'port' => $port, + 'platform_id' => $platform_id, + 'name' => $name, + ]; + $result = $hostObject->addHost($newHost); + if ($result === true) { + $_SESSION['notice'] = "New Jilo host added."; + } else { + $_SESSION['error'] = "Adding the host failed. Error: $result"; + } + // new agent adding - if (isset($_POST['new']) && isset($_POST['item']) && $_POST['new'] === 'true' && $_POST['item'] === 'agent') { + } elseif (isset($_POST['new']) && isset($_POST['item']) && $_POST['new'] === 'true' && $_POST['item'] === 'agent') { $newAgent = [ 'type_id' => $type, 'url' => $url, @@ -136,6 +153,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { include '../app/templates/config-host-delete.php'; } else { if ($userObject->hasRight($user_id, 'view config file')) { + $hostDetails = $hostObject->getHostDetails(); include '../app/templates/config-host.php'; } else { include '../app/templates/error-unauthorized.php'; diff --git a/app/templates/config-host-add.php b/app/templates/config-host-add.php new file mode 100644 index 0000000..1c5d8b6 --- /dev/null +++ b/app/templates/config-host-add.php @@ -0,0 +1,49 @@ + + +
+

Add new host in Jitsi platform

+
+
+ +
+
+ + * +
+
+ +

DNS name or IP address of the machine

+
+
+ +
+
+ + * +
+
+ +

port on which the Jilo Agent is listening

+
+
+ +
+
+ +
+
+ +

description or name of the host (optional)

+
+
+ + + + +
+ Cancel + +
+
+
+ diff --git a/app/templates/config-host.php b/app/templates/config-host.php new file mode 100644 index 0000000..459c568 --- /dev/null +++ b/app/templates/config-host.php @@ -0,0 +1,36 @@ + + +
+

Jilo configuration :: Jitsi Meet hosts

+
+

Jitsi hosts configuration +

+getHostDetails($platform_array['id']); +?> + +
+

+ platform +

+ + + +

+ : +   + edit host + delete host +

+ +

+ total   +   + add new +

+ +
+ +
+
+ diff --git a/doc/jilo-web.schema b/doc/jilo-web.schema index 4b302d3..40dda4a 100644 --- a/doc/jilo-web.schema +++ b/doc/jilo-web.schema @@ -14,6 +14,7 @@ CREATE TABLE users_meta ( bio TEXT, FOREIGN KEY (user_id) REFERENCES users(id) ); + CREATE TABLE users_rights ( user_id INTEGER, right_id INTEGER, @@ -25,12 +26,32 @@ CREATE TABLE rights ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE ); + +CREATE TABLE logs ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGET NOT NULL, + time TEXT DEFAULT (DATETIME('now')), + scope TEXT NOT NULL, + message TEXT NOT NULL, + FOREIGN KEY (user_id) REFERENCES users(id) +); + CREATE TABLE platforms ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE, jitsi_url TEXT NOT NULL, jilo_database TEXT NOT NULL ); + +CREATE TABLE hosts ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + address TEXT NOT NULL, + port INTEGER NOT NULL, + platform_id INTEGER NOT NULL, + name TEXT, + FOREIGN KEY(platform_id) REFERENCES platforms(id) +); + CREATE TABLE jilo_agents ( id INTEGER PRIMARY KEY AUTOINCREMENT, platform_id INTEGER NOT NULL, @@ -55,11 +76,3 @@ CREATE TABLE jilo_agent_checks ( response_content TEXT, FOREIGN KEY (agent_id) REFERENCES jilo_agents(id) ); -CREATE TABLE logs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - user_id INTEGET NOT NULL, - time TEXT DEFAULT (DATETIME('now')), - scope TEXT NOT NULL, - message TEXT NOT NULL, - FOREIGN KEY (user_id) REFERENCES users(id) -);