From de7133be3d3e84afc308f31bffd5b8cd717c0d64 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Thu, 5 Sep 2024 01:06:38 +0300 Subject: [PATCH] Adds support for managing Jilo Agents --- CHANGELOG.md | 3 + app/classes/agent.php | 105 +++++++++++++++++++++++++++++++++ app/classes/platform.php | 3 - app/pages/agents.php | 76 ++++++++++++++++++++++++ app/pages/config.php | 7 ++- app/templates/agent-add.php | 50 ++++++++++++++++ app/templates/agent-delete.php | 32 ++++++++++ app/templates/agent-edit.php | 50 ++++++++++++++++ app/templates/agent-list.php | 42 +++++++++++++ app/templates/config-list.php | 11 +++- app/templates/page-sidebar.php | 9 ++- public_html/index.php | 1 + 12 files changed, 380 insertions(+), 9 deletions(-) create mode 100644 app/classes/agent.php create mode 100644 app/pages/agents.php create mode 100644 app/templates/agent-add.php create mode 100644 app/templates/agent-delete.php create mode 100644 app/templates/agent-edit.php create mode 100644 app/templates/agent-list.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b8124b4..0910e00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ All notable changes to this project will be documented in this file. ## Unreleased +### Added +- Added support for managing Jilo Agents + ### Changed - Jitsi platforms config moved from file to SQLite database diff --git a/app/classes/agent.php b/app/classes/agent.php new file mode 100644 index 0000000..c087039 --- /dev/null +++ b/app/classes/agent.php @@ -0,0 +1,105 @@ +db = $database->getConnection(); + } + + // get details of a specified agent ID (or all) in a specified platform ID + public function getAgentDetails($platform_id, $agent_id = '') { + $sql = 'SELECT * FROM jilo_agents + WHERE + platform_id = :platform_id'; + if ($agent_id !== '') { + $sql .= ' AND id = :agent_id'; + $query = $this->db->prepare($sql); + $query->execute([ + ':platform_id' => $platform_id, + ':agent_id' => $agent_id, + ]); + } else { + $query = $this->db->prepare($sql); + $query->bindParam(':platform_id', $platform_id); + } + + $query->execute(); + + return $query->fetchAll(PDO::FETCH_ASSOC); + } + + // add new agent + public function addAgent($platform_id, $newAgent) { + try { + $sql = 'INSERT INTO jilo_agents + (platform_id, type_id, url, secret_key) + VALUES + (:platform_id, :type_id, :url, :secret_key)'; + + $query = $this->db->prepare($sql); + $query->execute([ + ':platform_id' => $platform_id, + ':type_id' => $newAgent['type_id'], + ':url' => $newAgent['url'], + ':secret_key' => $newAgent['secret_key'], + ]); + + return true; + + } catch (Exception $e) { + return $e->getMessage(); + } + } + + // edit an existing agent + public function editAgent($platform_id, $updatedAgent) { + try { + $sql = 'UPDATE jilo_agents SET + type_id = :type_id, + url = :url, + secret_key = :secret_key + WHERE + id = :agent_id + AND + platform_id = :platform_id'; + + $query = $this->db->prepare($sql); + $query->execute([ + ':type_id' => $updatedAgent['type_id'], + ':url' => $updatedAgent['url'], + ':secret_key' => $updatedAgent['secret_key'], + ':agent_id' => $updatedAgent['id'], + ':platform_id' => $platform_id, + ]); + + return true; + + } catch (Exception $e) { + return $e->getMessage(); + } + } + + + // delete an agent + 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/classes/platform.php b/app/classes/platform.php index 49aa50c..44a28ca 100644 --- a/app/classes/platform.php +++ b/app/classes/platform.php @@ -38,7 +38,6 @@ class Platform { ':jilo_database' => $newPlatform['jilo_database'], ]); - $query->execute(); return true; } catch (Exception $e) { @@ -64,7 +63,6 @@ class Platform { ':platform_id' => $platform_id, ]); - $query->execute(); return true; } catch (Exception $e) { @@ -72,7 +70,6 @@ class Platform { } } - // delete a platform public function deletePlatform($platform_id) { try { diff --git a/app/pages/agents.php b/app/pages/agents.php new file mode 100644 index 0000000..a9706e7 --- /dev/null +++ b/app/pages/agents.php @@ -0,0 +1,76 @@ + 1, + 'url' => $_POST['url'], + 'secret_key' => $_POST['secret_key'], + ]; + $result = $agentObject->addAgent($platform_id, $newAgent); + if ($result === true) { + $_SESSION['notice'] = "New Jilo Agent added."; + } else { + $_SESSION['error'] = "Adding the agent failed. Error: $result"; + } + + // deleting an agent + } elseif (isset($_POST['delete']) && $_POST['delete'] === 'true') { + $result = $agentObject->deleteAgent($agent); + if ($result === true) { + $_SESSION['notice'] = "Agent id \"{$_REQUEST['agent']}\" deleted."; + } else { + $_SESSION['error'] = "Deleting the agent failed. Error: $result"; + } + + // an update to an existing agent + } else { + $updatedAgent = [ + 'id' => $agent, + 'type_id' => 1, + 'url' => $_POST['url'], + 'secret_key' => $_POST['secret_key'], + ]; + $result = $agentObject->editAgent($platform_id, $updatedAgent); + if ($result === true) { + $_SESSION['notice'] = "Agent id \"{$_REQUEST['agent']}\" edited."; + } else { + $_SESSION['error'] = "Editing the agent failed. Error: $result"; + } + + } + + header("Location: $app_root?platform=$platform_id&page=agents"); + exit(); + +// no form submitted, show the templates +} else { + + switch ($action) { + case 'add': + include('../app/templates/agent-add.php'); + break; + case 'edit': + $agentDetails = $agentObject->getAgentDetails($platform_id, $agent); + include('../app/templates/agent-edit.php'); + break; + case 'delete': + $agentDetails = $agentObject->getAgentDetails($platform_id, $agent); + include('../app/templates/agent-delete.php'); + break; + default: + $agentDetails = $agentObject->getAgentDetails($platform_id); + include('../app/templates/agent-list.php'); + } +} + +?> diff --git a/app/pages/config.php b/app/pages/config.php index 066606a..8998b3a 100644 --- a/app/pages/config.php +++ b/app/pages/config.php @@ -8,9 +8,10 @@ $configure = new Config(); // if a form is submitted, it's from the edit page if ($_SERVER['REQUEST_METHOD'] == 'POST') { - // load the config file and initialize a copy - $content = file_get_contents($config_file); - $updatedContent = $content; +// FIXME - if editing the flat file is no more needed, remove this +// // load the config file and initialize a copy +// $content = file_get_contents($config_file); +// $updatedContent = $content; // new platform adding if (isset($_POST['new']) && $_POST['new'] === 'true') { diff --git a/app/templates/agent-add.php b/app/templates/agent-add.php new file mode 100644 index 0000000..0f8a1fe --- /dev/null +++ b/app/templates/agent-add.php @@ -0,0 +1,50 @@ + + +
+

Add new Jilo Agent to Jitsi platform ""

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

type of agent (meet, jvb, jibri, all)

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

URL of the Jilo Agent API (https://example.com:8081)

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

secret key for generating the access JWT token

+
+
+ + + +
+ Cancel + +
+
+
+ diff --git a/app/templates/agent-delete.php b/app/templates/agent-delete.php new file mode 100644 index 0000000..13aee9b --- /dev/null +++ b/app/templates/agent-delete.php @@ -0,0 +1,32 @@ + + +
+

Jilo Agent configuration for Jitsi platform ""

+
+

delete an agent:

+
+ $value) { +// if ($key === 'id') continue; +?> +
+
+ +
+
+
+ +
+
+ +
+ + +

Are you sure you want to delete this agent?

+
+ Cancel + +
+
+
+ diff --git a/app/templates/agent-edit.php b/app/templates/agent-edit.php new file mode 100644 index 0000000..db60f23 --- /dev/null +++ b/app/templates/agent-edit.php @@ -0,0 +1,50 @@ + + +
+

Jilo Agent configuration for Jitsi platform ""

+
+

edit the agent details:

+
+ +
+
+ + * +
+
+ +

type of agent (meet, jvb, jibri, all)

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

URL of the Jilo Agent API (https://example.com:8081)

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

secret key for generating the access JWT token

+
+
+ + +
+ + Cancel + +
+
+
+ diff --git a/app/templates/agent-list.php b/app/templates/agent-list.php new file mode 100644 index 0000000..be1950a --- /dev/null +++ b/app/templates/agent-list.php @@ -0,0 +1,42 @@ + + +
+

Jilo Agents on platform ""

+
+

agents configuration  add

+ + + +
+
+
+
+ agent id : +
+
+ edit + delete +
+
+ $value) { + if ($key === 'id') continue; +?> +
+
+ : +
+
+ +
+
+ +
+
+
+
+ + + +
+
+ diff --git a/app/templates/config-list.php b/app/templates/config-list.php index f2f1649..44a3615 100644 --- a/app/templates/config-list.php +++ b/app/templates/config-list.php @@ -19,7 +19,7 @@ echo "\n";
- : + platform id :
edit @@ -43,6 +43,15 @@ echo "\n";
+ +
+ configured jilo agents: +
+
+ 0 + configure +
+
diff --git a/app/templates/page-sidebar.php b/app/templates/page-sidebar.php index db0aa1e..3b6e9c4 100644 --- a/app/templates/page-sidebar.php +++ b/app/templates/page-sidebar.php @@ -46,12 +46,17 @@
  • - config.js + config.js
  • - interface_config.js + interface_config.js +
  • +
    + +
  • + jilo agents
  • diff --git a/public_html/index.php b/public_html/index.php index 2be8a07..19270e2 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -31,6 +31,7 @@ $allowed_urls = [ 'register', 'profile', 'config', + 'agents', 'conferences', 'participants', 'components',