From b60208bea7fca23c82f5750d8ea3f93453347426 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Wed, 4 Sep 2024 12:53:02 +0300 Subject: [PATCH] Moves the platforms config from flat file to SQLite DB --- CHANGELOG.md | 3 + app/classes/config.php | 13 +-- app/classes/database.php | 2 - app/classes/platform.php | 97 +++++++++++++++++++ app/config/jilo-web.conf.php | 21 ---- app/helpers/config.php | 25 ----- app/helpers/database.php | 5 +- app/helpers/render.php | 38 +------- app/helpers/render.php-- | 69 +++++++++++++ app/pages/components.php | 4 +- app/pages/conferences.php | 4 +- app/pages/config.php | 62 +++--------- app/pages/front.php | 4 +- app/pages/login.php | 6 +- app/pages/participants.php | 4 +- app/pages/register.php | 6 +- app/templates/config-delete-platform.php | 13 ++- app/templates/config-edit-platform.php | 17 ++-- app/templates/config-list-configjs.php | 4 +- .../config-list-interfaceconfigjs.php | 4 +- app/templates/config-list.php | 42 +++++++- app/templates/page-menu.php | 4 +- doc/jilo-web.schema | 18 ++++ public_html/index.php | 16 ++- 24 files changed, 296 insertions(+), 185 deletions(-) create mode 100644 app/classes/platform.php delete mode 100644 app/helpers/config.php create mode 100644 app/helpers/render.php-- diff --git a/CHANGELOG.md b/CHANGELOG.md index 00a761d..b8124b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ All notable changes to this project will be documented in this file. ## Unreleased +### Changed +- Jitsi platforms config moved from file to SQLite database + #### Links - upstream: https://code.lindeas.com/lindeas/jilo-web/compare/v0.2...HEAD - codeberg: https://codeberg.org/lindeas/jilo-web/compare/v0.2...HEAD diff --git a/app/classes/config.php b/app/classes/config.php index 0d00fb3..14c8e87 100644 --- a/app/classes/config.php +++ b/app/classes/config.php @@ -2,15 +2,10 @@ class Config { - public function getPlatformDetails($config, $platform_id) { - $platformDetails = $config['platforms'][$platform_id]; - return $platformDetails; - } - // loading the config.js - public function getPlatformConfigjs($platformDetails, $raw = false) { + public function getPlatformConfigjs($jitsiUrl, $raw = false) { // constructing the URL - $configjsFile = $platformDetails['jitsi_url'] . '/config.js'; + $configjsFile = $jitsiUrl . '/config.js'; // default content, if we can't get the file contents $platformConfigjs = "The file $configjsFile can't be loaded."; @@ -50,9 +45,9 @@ class Config { // loading the interface_config.js - public function getPlatformInterfaceConfigjs($platformDetails, $raw = false) { + public function getPlatformInterfaceConfigjs($jitsiUrl, $raw = false) { // constructing the URL - $interfaceConfigjsFile = $platformDetails['jitsi_url'] . '/interface_config.js'; + $interfaceConfigjsFile = $jitsiUrl . '/interface_config.js'; // default content, if we can't get the file contents $platformInterfaceConfigjs = "The file $interfaceConfigjsFile can't be loaded."; diff --git a/app/classes/database.php b/app/classes/database.php index 01f14a9..ca90434 100644 --- a/app/classes/database.php +++ b/app/classes/database.php @@ -1,7 +1,5 @@ db = $database->getConnection(); + } + + // get details of a specified platform ID (or all) + public function getPlatformDetails($platform_id = '') { + $sql = 'SELECT * FROM platforms'; + if ($platform_id !== '') { + $sql .= ' WHERE id = :platform_id'; + $query = $this->db->prepare($sql); + $query->bindParam(':platform_id', $platform_id); + } else { + $query = $this->db->prepare($sql); + } + + $query->execute(); + + return $query->fetchAll(PDO::FETCH_ASSOC); + } + + // add new platform + public function addPlatform($newPlatform) { + try { + $sql = 'INSERT INTO platforms + (name, jitsi_url, jilo_database) + VALUES + (:name, :jitsi_url, :jilo_database)'; + + $query = $this->db->prepare($sql); + $query->execute([ + ':name' => $newPlatform['name'], + ':jitsi_url' => $newPlatform['jitsi_url'], + ':jilo_database' => $newPlatform['jilo_database'], + ]); + + $query->execute(); + return true; + + } catch (Exception $e) { + return $e->getMessage(); + } + } + + // edit an existing platform + public function editPlatform($platform_id, $updatedPlatform) { + try { + $sql = 'UPDATE platforms SET + name = :name, + jitsi_url = :jitsi_url, + jilo_database = :jilo_database + WHERE + id = :platform_id'; + + $query = $this->db->prepare($sql); + $query->execute([ + ':name' => $updatedPlatform['name'], + ':jitsi_url' => $updatedPlatform['jitsi_url'], + ':jilo_database' => $updatedPlatform['jilo_database'], + ':platform_id' => $platform_id, + ]); + + $query->execute(); + return true; + + } catch (Exception $e) { + return $e->getMessage(); + } + } + + + // delete a platform + public function deletePlatform($platform_id) { + try { + $sql = 'DELETE FROM platforms + WHERE + id = :platform_id'; + + $query = $this->db->prepare($sql); + $query->bindParam(':platform_id', $platform_id); + + $query->execute(); + return true; + + } catch (Exception $e) { + return $e->getMessage(); + } + } + + +} + +?> diff --git a/app/config/jilo-web.conf.php b/app/config/jilo-web.conf.php index 00848b8..a40286c 100644 --- a/app/config/jilo-web.conf.php +++ b/app/config/jilo-web.conf.php @@ -31,27 +31,6 @@ return [ // development has verbose error messages, production has not 'environment' => 'development', - // ************************************* - // Maintained by the app, edit with care - // ************************************* - - 'platforms' => [ - '0' => [ - 'name' => 'lindeas', - 'jitsi_url' => 'https://meet.lindeas.com', - 'jilo_database' => '../../jilo/jilo-meet.lindeas.db', - ], - '1' => [ - 'name' => 'meet.example.com', - 'jitsi_url' => 'https://meet.example.com', - 'jilo_database' => '../../jilo/jilo.db', - ], - '2' => [ - 'name' => 'test3', - 'jitsi_url' => 'https://test3.example.com', - 'jilo_database' => '../../jilo/jilo2.db', - ], - ], ]; ?> diff --git a/app/helpers/config.php b/app/helpers/config.php deleted file mode 100644 index a1fab9c..0000000 --- a/app/helpers/config.php +++ /dev/null @@ -1,25 +0,0 @@ - $value) { - $output .= $indent . "'" . $key . "'" . ' => '; - - if (is_array($value)) { - $output .= formatArray($value, $indentLevel + 1); - } else { - $output .= var_export($value, true); - } - - $output .= ",\n"; - } - - $output .= str_repeat(' ', $indentLevel - 1) . ']'; - - return $output; -} - -?> diff --git a/app/helpers/database.php b/app/helpers/database.php index c32b0ad..db56adc 100644 --- a/app/helpers/database.php +++ b/app/helpers/database.php @@ -1,14 +1,13 @@ 'sqlite', diff --git a/app/helpers/render.php b/app/helpers/render.php index c25a8ba..b55a8da 100644 --- a/app/helpers/render.php +++ b/app/helpers/render.php @@ -4,40 +4,14 @@ function renderConfig($configPart, $indent, $platform=false, $parent='') { global $app_root; global $config; - if ($parent === 'platforms') { -?> -
- add -
-
-
- $config_value) { - if ($parent === 'platforms') { - $indent = 0; - } -?> + $config_value) { ?>
:
-
- edit - - delete - - delete - -
-
-
- +
+ +
'; } - ?> diff --git a/app/helpers/render.php-- b/app/helpers/render.php-- new file mode 100644 index 0000000..c25a8ba --- /dev/null +++ b/app/helpers/render.php-- @@ -0,0 +1,69 @@ + +
+ add +
+
+ +
+ $config_value) { + if ($parent === 'platforms') { + $indent = 0; + } +?> +
+
+ : +
+ +
+ edit + + delete + + delete + +
+ +
+ + +
+ +
+'; +} + +?> diff --git a/app/pages/components.php b/app/pages/components.php index 3e5f0b9..40b1369 100644 --- a/app/pages/components.php +++ b/app/pages/components.php @@ -1,11 +1,9 @@ $_POST['jitsi_url'], 'jilo_database' => $_POST['jilo_database'], ]; - - // Determine the next available index for the new platform - $nextIndex = count($config['platforms']); - - // Add the new platform to the platforms array - $config['platforms'][$nextIndex] = $newPlatform; - - // Rebuild the PHP array syntax for the platforms - $platformsArray = formatArray($config['platforms']); - - // Replace the platforms section in the config file - $updatedContent = preg_replace( - '/\'platforms\'\s*=>\s*\[[\s\S]+?\],/s', - "'platforms' => {$platformsArray}", - $content - ); - $updatedContent = preg_replace('/\s*\]\n/s', "\n", $updatedContent); + $platformObject->addPlatform($newPlatform); // deleting a platform } elseif (isset($_POST['delete']) && $_POST['delete'] === 'true') { $platform = $_POST['platform']; - - $config['platforms'][$platform]['name'] = $_POST['name']; - $config['platforms'][$platform]['jitsi_url'] = $_POST['jitsi_url']; - $config['platforms'][$platform]['jilo_database'] = $_POST['jilo_database']; - - $platformsArray = formatArray($config['platforms'][$platform], 3); - - $updatedContent = preg_replace( - "/\s*'$platform'\s*=>\s*\[\s*'name'\s*=>\s*'[^']*',\s*'jitsi_url'\s*=>\s*'[^']*,\s*'jilo_database'\s*=>\s*'[^']*',\s*\],/s", - "", - $content - ); - + $platformObject->deletePlatform($platform); // an update to an existing platform } else { - $platform = $_POST['platform']; - - $config['platforms'][$platform]['name'] = $_POST['name']; - $config['platforms'][$platform]['jitsi_url'] = $_POST['jitsi_url']; - $config['platforms'][$platform]['jilo_database'] = $_POST['jilo_database']; - - $platformsArray = formatArray($config['platforms'][$platform], 3); - - $updatedContent = preg_replace( - "/\s*'$platform'\s*=>\s*\[\s*'name'\s*=>\s*'[^']*',\s*'jitsi_url'\s*=>\s*'[^']*',\s*'jilo_database'\s*=>\s*'[^']*',\s*\],/s", - "\n '{$platform}' => {$platformsArray},", - $content - ); + $updatedPlatform = [ + 'name' => $_POST['name'], + 'jitsi_url' => $_POST['jitsi_url'], + 'jilo_database' => $_POST['jilo_database'], + ]; + $platformObject->editPlatform($platform, $updatedPlatform); } - // check if file is writable if (!is_writable($config_file)) { $_SESSION['error'] = getError('Configuration file is not writable.'); @@ -107,15 +69,13 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { case 'configjs': $mode = $_REQUEST['mode'] ?? ''; $raw = ($mode === 'raw'); - $platformDetails = $configure->getPlatformDetails($config, $platform_id); - $platformConfigjs = $configure->getPlatformConfigjs($platformDetails, $raw); + $platformConfigjs = $configure->getPlatformConfigjs($platformDetails[0]['jitsi_url'], $raw); include('../app/templates/config-list-configjs.php'); break; case 'interfaceconfigjs': $mode = $_REQUEST['mode'] ?? ''; $raw = ($mode === 'raw'); - $platformDetails = $configure->getPlatformDetails($config, $platform_id); - $platformInterfaceConfigjs = $configure->getPlatformInterfaceConfigjs($platformDetails, $raw); + $platformInterfaceConfigjs = $configure->getPlatformInterfaceConfigjs($platformDetails[0]['jitsi_url'], $raw); include('../app/templates/config-list-interfaceconfigjs.php'); break; diff --git a/app/pages/front.php b/app/pages/front.php index b2f4143..5a4ed8f 100644 --- a/app/pages/front.php +++ b/app/pages/front.php @@ -1,12 +1,10 @@
-

Jilo web configuration for Jitsi platform ""

+

Jilo web configuration for Jitsi platform ""

delete a platform:

- $config_value) { ?> + $value) { + if ($key === 'id') continue; +?>
- +
-
- +
+
diff --git a/app/templates/config-edit-platform.php b/app/templates/config-edit-platform.php index 7330b15..b080d5d 100644 --- a/app/templates/config-edit-platform.php +++ b/app/templates/config-edit-platform.php @@ -1,23 +1,26 @@
-

Jilo web configuration for Jitsi platform ""

+

Jilo web configuration for Jitsi platform ""

edit the platform details:

- $config_value) { ?> + $value) { + if ($key === 'id') continue; +?>
- + *
- - + +

descriptive name for the platform

- +

URL of the Jitsi Meet (used for checks and for loading config.js)

- +

path to the database file (relative to the app root)

diff --git a/app/templates/config-list-configjs.php b/app/templates/config-list-configjs.php index 49da179..842cf90 100644 --- a/app/templates/config-list-configjs.php +++ b/app/templates/config-list-configjs.php @@ -1,10 +1,10 @@
-

Configuration of the Jitsi platform

+

Configuration of the Jitsi platform

- URL: + URL: FILE: config.js view only active lines diff --git a/app/templates/config-list-interfaceconfigjs.php b/app/templates/config-list-interfaceconfigjs.php index 7d7c1f5..acb40dc 100644 --- a/app/templates/config-list-interfaceconfigjs.php +++ b/app/templates/config-list-interfaceconfigjs.php @@ -1,10 +1,10 @@

-

Configuration of the Jitsi platform

+

Configuration of the Jitsi platform

- URL: + URL: FILE: interface_config.js view only active lines diff --git a/app/templates/config-list.php b/app/templates/config-list.php index ac1fb7b..78cedb2 100644 --- a/app/templates/config-list.php +++ b/app/templates/config-list.php @@ -3,12 +3,52 @@

Jilo web configuration

-

platform variables

+

main variables

+ +
+

platforms configuration  add

+ + + +
+
+
+
+ : +
+
+ edit + + delete + + delete + +
+
+ $value) { + if ($key === 'id') continue; +?> +
+
+ : +
+
+ +
+
+ +
+
+
+
+ + +
diff --git a/app/templates/page-menu.php b/app/templates/page-menu.php index 421e8ad..0b8b18f 100644 --- a/app/templates/page-menu.php +++ b/app/templates/page-menu.php @@ -12,9 +12,9 @@ - $platform) { ?> +
  • - +
  • diff --git a/doc/jilo-web.schema b/doc/jilo-web.schema index d932839..01bd1d8 100644 --- a/doc/jilo-web.schema +++ b/doc/jilo-web.schema @@ -3,3 +3,21 @@ CREATE TABLE users ( username TEXT NOT NULL UNIQUE, password TEXT NOT NULL ); +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 jilo_agents ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + platform_id INTEGER NOT NULL, + type_id INTEGER NOT NULL, + url TEXT NOT NULL, + secret_key TEXT +); +CREATE TABLE jilo_agent_types ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + description TEXT, + endponts TEXT +); diff --git a/public_html/index.php b/public_html/index.php index 14efc8a..2be8a07 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -15,6 +15,8 @@ // flush it later only when there is no redirect ob_start(); +require '../app/helpers/errors.php'; + // error reporting, comment out in production ini_set('display_errors', 1); ini_set('display_startup_errors', 1); @@ -96,8 +98,20 @@ if (isset($_SESSION['error'])) { $error = $_SESSION['error']; } +// connect to db of Jilo Web +require '../app/classes/database.php'; +require '../app/helpers/database.php'; +$dbWeb = connectDB($config); + +// get platforms details +require '../app/classes/platform.php'; +$platformObject = new Platform($dbWeb); +$platformsAll = $platformObject->getPlatformDetails(); + // by default we connect ot the first configured platform -$platform_id = $_REQUEST['platform'] ?? '0'; +$firstPlatform = $platformsAll[0]['id']; +$platform_id = $_REQUEST['platform'] ?? $firstPlatform; +$platformDetails = $platformObject->getPlatformDetails($platform_id); // page building if (in_array($page, $allowed_urls)) {