diff --git a/app/classes/config.php b/app/classes/config.php index 5eaa3c5..f1152fc 100644 --- a/app/classes/config.php +++ b/app/classes/config.php @@ -3,15 +3,15 @@ /** * class Config * - * Handles editing and fetching configuration files. + * Handles editing and fetching ot the config files. */ class Config { /** - * Edits a configuration file by updating specified options. + * Edits a config file by updating specified options. * - * @param array $updatedConfig Key-value pairs of configuration options to update. - * @param string $config_file Path to the configuration file. + * @param array $updatedConfig Key-value pairs of config options to update. + * @param string $config_file Path to the config file. * * @return mixed Returns true on success, or an error message on failure. */ @@ -50,104 +50,6 @@ class Config { return true; } - - /** - * Loads the config.js file from the Jitsi server. - * - * @param string $jitsiUrl The base URL of the Jitsi server. - * @param bool $raw Whether to return the full file (true) or only uncommented values (false). - * - * @return string The content of the config.js file or an error message. - */ - public function getPlatformConfigjs($jitsiUrl, $raw = false) { - // constructing the URL - $configjsFile = $jitsiUrl . '/config.js'; - - // default content, if we can't get the file contents - $platformConfigjs = "The file $configjsFile can't be loaded."; - - // ssl options - $contextOptions = [ - 'ssl' => [ - 'verify_peer' => true, - 'verify_peer_name' => true, - ], - ]; - $context = stream_context_create($contextOptions); - - // get the file - $fileContent = @file_get_contents($configjsFile, false, $context); - - if ($fileContent !== false) { - - // when we need only uncommented values - if ($raw === false) { - // remove block comments - $platformConfigjs = preg_replace('!/\*.*?\*/!s', '', $fileContent); - // remove single-line comments - $platformConfigjs = preg_replace('/\/\/[^\n]*/', '', $platformConfigjs); - // remove empty lines - $platformConfigjs = preg_replace('/^\s*[\r\n]/m', '', $platformConfigjs); - - // when we need the full file as it is - } else { - $platformConfigjs = $fileContent; - } - } - - return $platformConfigjs; - - } - - - /** - * Loads the interface_config.js file from the Jitsi server. - * - * @param string $jitsiUrl The base URL of the Jitsi server. - * @param bool $raw Whether to return the full file (true) or only uncommented values (false). - * - * @return string The content of the interface_config.js file or an error message. - */ - public function getPlatformInterfaceConfigjs($jitsiUrl, $raw = false) { - // constructing the URL - $interfaceConfigjsFile = $jitsiUrl . '/interface_config.js'; - - // default content, if we can't get the file contents - $platformInterfaceConfigjs = "The file $interfaceConfigjsFile can't be loaded."; - - // ssl options - $contextOptions = [ - 'ssl' => [ - 'verify_peer' => true, - 'verify_peer_name' => true, - ], - ]; - $context = stream_context_create($contextOptions); - - // get the file - $fileContent = @file_get_contents($interfaceConfigjsFile, false, $context); - - if ($fileContent !== false) { - - // when we need only uncommented values - if ($raw === false) { - // remove block comments - $platformInterfaceConfigjs = preg_replace('!/\*.*?\*/!s', '', $fileContent); - // remove single-line comments - $platformInterfaceConfigjs = preg_replace('/\/\/[^\n]*/', '', $platformInterfaceConfigjs); - // remove empty lines - $platformInterfaceConfigjs = preg_replace('/^\s*[\r\n]/m', '', $platformInterfaceConfigjs); - - // when we need the full file as it is - } else { - $platformInterfaceConfigjs = $fileContent; - } - } - - return $platformInterfaceConfigjs; - - } - } ?> diff --git a/app/pages/config.php b/app/pages/config.php index 9e2d8d3..50403a5 100644 --- a/app/pages/config.php +++ b/app/pages/config.php @@ -1,205 +1,92 @@ false, + 'message' => 'Invalid JSON data received' + ]); + exit; + } + + // Check if file is writable if (!is_writable($config_file)) { - $_SESSION['error'] = "Configuration file is not writable."; + Messages::flash('ERROR', 'DEFAULT', 'Config file is not writable', true); + echo json_encode([ + 'success' => false, + 'message' => 'Config file is not writable' + ]); + exit; + } + + // Try to update config file + $result = $configObject->editConfigFile($postData, $config_file); + if ($result === true) { + $messageData = Messages::getMessageData('NOTICE', 'DEFAULT', 'Config file updated successfully', true); + echo json_encode([ + 'success' => true, + 'message' => 'Config file updated successfully', + 'messageData' => $messageData + ]); } else { - $result = $configObject->editConfigFile($_POST, $config_file); - if ($result === true) { - $_SESSION['notice'] = "The config file is edited."; - } else { - $_SESSION['error'] = "Editing the config file failed. Error: $result"; - } + $messageData = Messages::getMessageData('ERROR', 'DEFAULT', "Error updating config file: $result", true); + echo json_encode([ + 'success' => false, + 'message' => "Error updating config file: $result", + 'messageData' => $messageData + ]); } - header('Location: ' . $redirectUrl); - exit; - - // host operations - } elseif (isset($_POST['item']) && $_POST['item'] === 'host') { - if (isset($_POST['delete']) && $_POST['delete'] === 'true') { // This is a host delete - $host_id = $_POST['host']; - $result = $hostObject->deleteHost($host_id); - if ($result === true) { - $_SESSION['notice'] = "Host deleted successfully."; - } else { - $_SESSION['error'] = "Deleting the host failed. Error: $result"; - } - } else if (!isset($_POST['host'])) { // This is a new host - $newHost = [ - 'address' => $_POST['address'], - 'platform_id' => $_POST['platform'], - 'name' => empty($_POST['name']) ? $_POST['address'] : $_POST['name'], - ]; - $result = $hostObject->addHost($newHost); - if ($result === true) { - $_SESSION['notice'] = "New Jilo host added."; - } else { - $_SESSION['error'] = "Adding the host failed. Error: $result"; - } - } else { // This is an edit of existing host - $host_id = $_POST['host']; - $platform_id = $_POST['platform']; - $updatedHost = [ - 'id' => $host_id, - 'address' => $_POST['address'], - 'name' => empty($_POST['name']) ? $_POST['address'] : $_POST['name'], - ]; - $result = $hostObject->editHost($platform_id, $updatedHost); - if ($result === true) { - $_SESSION['notice'] = "Host edited."; - } else { - $_SESSION['error'] = "Editing the host failed. Error: $result"; - } - } - header('Location: ' . $redirectUrl); - exit; - - // agent operations - } elseif (isset($_POST['item']) && $_POST['item'] === 'agent') { - if (isset($_POST['delete']) && $_POST['delete'] === 'true') { // This is an agent delete - $agent_id = $_POST['agent']; - $result = $agentObject->deleteAgent($agent_id); - if ($result === true) { - $_SESSION['notice'] = "Agent deleted successfully."; - } else { - $_SESSION['error'] = "Deleting the agent failed. Error: $result"; - } - } else if (isset($_POST['new']) && $_POST['new'] === 'true') { // This is a new agent - $newAgent = [ - 'type_id' => $_POST['type'], - 'url' => $_POST['url'], - 'secret_key' => empty($_POST['secret_key']) ? null : $_POST['secret_key'], - 'check_period' => empty($_POST['check_period']) ? 0 : $_POST['check_period'], - ]; - $result = $agentObject->addAgent($_POST['host'], $newAgent); - if ($result === true) { - $_SESSION['notice'] = "New Jilo agent added."; - } else { - $_SESSION['error'] = "Adding the agent failed. Error: $result"; - } - } else { // This is an edit of existing agent - $agent_id = $_POST['agent']; - $updatedAgent = [ - 'agent_type_id' => $_POST['agent_type_id'], - 'url' => $_POST['url'], - 'secret_key' => empty($_POST['secret_key']) ? null : $_POST['secret_key'], - 'check_period' => empty($_POST['check_period']) ? 0 : $_POST['check_period'], - ]; - $result = $agentObject->editAgent($agent_id, $updatedAgent); - if ($result === true) { - $_SESSION['notice'] = "Agent edited."; - } else { - $_SESSION['error'] = "Editing the agent failed. Error: $result"; - } - } - header('Location: ' . $redirectUrl); - exit; - - // platform operations - } elseif (isset($_POST['item']) && $_POST['item'] === 'platform') { - if (isset($_POST['delete']) && $_POST['delete'] === 'true') { // This is a platform delete - $platform_id = $_POST['platform']; - $result = $platformObject->deletePlatform($platform_id); - if ($result === true) { - $_SESSION['notice'] = "Platform deleted successfully."; - } else { - $_SESSION['error'] = "Deleting the platform failed. Error: $result"; - } - } else if (!isset($_POST['platform'])) { // This is a new platform - $newPlatform = [ - 'name' => $_POST['name'], - 'jitsi_url' => $_POST['jitsi_url'], - 'jilo_database' => $_POST['jilo_database'], - ]; - $result = $platformObject->addPlatform($newPlatform); - if ($result === true) { - $_SESSION['notice'] = "New Jitsi platform added."; - } else { - $_SESSION['error'] = "Adding the platform failed. Error: $result"; - } - } else { // This is an edit of existing platform - $platform_id = $_POST['platform']; - $updatedPlatform = [ - 'id' => $platform_id, - 'name' => $_POST['name'], - 'jitsi_url' => $_POST['jitsi_url'], - 'jilo_database' => $_POST['jilo_database'], - ]; - $result = $platformObject->editPlatform($updatedPlatform); - if ($result === true) { - $_SESSION['notice'] = "Platform edited."; - } else { - $_SESSION['error'] = "Editing the platform failed. Error: $result"; - } - } - header('Location: ' . $redirectUrl); exit; } -} else { - /** - * Handles GET requests to display templates. - */ - - switch ($item) { - - case 'config_file': - if (isset($action) && $action === 'edit') { - include '../app/templates/config-configfile-edit.php'; - } else { - if ($userObject->hasRight($user_id, 'view config file')) { - include '../app/templates/config-configfile.php'; - } else { - include '../app/templates/error-unauthorized.php'; - } - } - break; - - default: - if ($userObject->hasRight($user_id, 'view config file')) { - $jilo_agent_types = $agentObject->getAgentTypes(); - include '../app/templates/config-jilo.php'; - } else { - include '../app/templates/error-unauthorized.php'; - } + // Handle non-AJAX POST + if (!is_writable($config_file)) { + Messages::flash('ERROR', 'DEFAULT', 'Config file is not writable', true); + } else { + $result = $configObject->editConfigFile($_POST, $config_file); + if ($result === true) { + Messages::flash('NOTICE', 'DEFAULT', 'Config file updated successfully', true); + } else { + Messages::flash('ERROR', 'DEFAULT', "Error updating config file: $result", true); + } } + + header('Location: ' . htmlspecialchars($app_root) . '?page=config'); + exit; } +// Only include template for non-AJAX requests +if (!$isAjax) { + include '../app/templates/config.php'; +} ?> diff --git a/app/templates/config-configfile-edit.php b/app/templates/config-configfile-edit.php deleted file mode 100644 index 97cd390..0000000 --- a/app/templates/config-configfile-edit.php +++ /dev/null @@ -1,27 +0,0 @@ - - -
-

Jilo configuration file :: edit

-
-
-

this may break everything, use with extreme caution

-
-
- - - -

this may break everything, use with extreme caution

-
- - Cancel -    - -
- -
-
- diff --git a/app/templates/config-configfile.php b/app/templates/config-configfile.php deleted file mode 100644 index 19f1292..0000000 --- a/app/templates/config-configfile.php +++ /dev/null @@ -1,17 +0,0 @@ - - -
-

Jilo configuration file

-
- - - -
- Edit -
-
- diff --git a/app/templates/config-jilo.php b/app/templates/config-jilo.php deleted file mode 100644 index c13c14b..0000000 --- a/app/templates/config-jilo.php +++ /dev/null @@ -1,1195 +0,0 @@ - - -
-
-
-

Jitsi Meet platforms configuration

-
-
- -
-
- - - -
- $platform): ?> - getHostDetails($platform['id']); - $agents = $agentObject->getAgentDetails($platform['id']); - ?> -
- -
-
- - - Platform # - -
-
- - - - hasRight($user_id, 'delete platform')): ?> - - -
-
- -
- - - $value): ?> - - - - - - - -
-
- - - - - - - - -
- -
-
- - -
-
-
- - - - for platform "" - -
- -
- - - - getAgentDetails($host['id']); - ?> -
-
-
-
- -
Host "" (#) in platform ""
-
-
- -
-
-
Host description
-
-
-
-
DNS name or IP
-
-
-
-
- -
-
-
- - - - -
-
- -
- -
-
- - - getAgentDetails($host['id']); - echo count($hostAgents) . ' ' . (count($hostAgents) === 1 ? 'agent' : 'agents') . ' for host "' . htmlspecialchars($host['name']) . '"'; - ?> - -
- -
- - -
- - - - - - - - - - - - - - - - - - - - - -
Agent typeEndpoint URLSecret keyCheck periodActions
-
- - - - - -
-
- - - - - - - - - - - - 0 ? - htmlspecialchars($agent['check_period']) . ' ' . - ($agent['check_period'] == 1 ? 'minute' : 'minutes') : - 'Not monitored' ?> - - - -
- - - - -
-
-
- -
- - No agents configured for this host. -
- -
-
- - -
- - No hosts configured for platform . -
- -
-
- -
- -
- - No platforms available. Use the button above to add your first platform. -
- -
-
-
- - - - - - - - - - - - - - - - - - - - - - diff --git a/app/templates/config.php b/app/templates/config.php new file mode 100644 index 0000000..d6bf531 --- /dev/null +++ b/app/templates/config.php @@ -0,0 +1,240 @@ + +
+
+ +
+
+

Configuration

+
+
+ +
+
+
+ + Jilo Web app configuration +
+ hasRight($user_id, 'edit config file')): ?> +
+ +
+ + +
+
+ +
+
+
+ '; + echo '
' . htmlspecialchars($displayName) . '
'; + echo '
'; + foreach ($value as $subKey => $subValue) { + renderConfigItem($subKey, $subValue, $fullPath); + } + echo '
'; + echo '
'; + } else { + ?> +
+
+ +
+
+
+ + + + + + + + + + + blank + + + + + + +
+
+ +
+ > +
+ + + + + +
+
+
+ $value) { + renderConfigItem($key, $value); + } + ?> + +
+
+ + + +