jilo-web/app/pages/config.php

193 lines
7.3 KiB
PHP
Raw Normal View History

2024-08-12 11:12:24 +00:00
<?php
2024-12-04 10:13:33 +00:00
/**
* Configuration management.
*
* This page ("config") handles configuration by adding, editing, and deleting platforms,
* hosts, agents, and the configuration file itself.
*/
// Get any new messages
include '../app/includes/messages.php';
include '../app/includes/messages-show.php';
2024-08-18 19:12:45 +00:00
$action = $_REQUEST['action'] ?? '';
2024-09-20 09:13:18 +00:00
$agent = $_REQUEST['agent'] ?? '';
2024-10-30 20:14:54 +00:00
$host = $_REQUEST['host'] ?? '';
2024-09-20 09:13:18 +00:00
require '../app/classes/config.php';
2024-10-30 17:11:23 +00:00
require '../app/classes/host.php';
2024-09-20 09:13:18 +00:00
require '../app/classes/agent.php';
2024-08-18 19:12:45 +00:00
2024-09-06 16:34:03 +00:00
$configObject = new Config();
2024-10-30 17:11:23 +00:00
$hostObject = new Host($dbWeb);
2024-09-20 09:13:18 +00:00
$agentObject = new Agent($dbWeb);
2024-08-28 09:59:13 +00:00
2024-08-19 10:25:09 +00:00
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
2024-12-04 10:13:33 +00:00
/**
* Handles form submissions from editing
2024-12-04 10:13:33 +00:00
*/
2024-08-18 19:12:45 +00:00
2024-11-01 16:23:40 +00:00
// editing the config file
if (isset($_POST['item']) && $_POST['item'] === 'config_file') {
2024-11-06 16:39:51 +00:00
// check if file is writable
if (!is_writable($config_file)) {
$_SESSION['error'] = "Configuration file is not writable.";
2024-11-01 16:23:40 +00:00
} else {
2024-11-06 16:39:51 +00:00
$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";
}
2024-11-01 16:23:40 +00:00
}
// host operations
2025-01-20 19:17:54 +00:00
} 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);
2025-01-20 19:17:54 +00:00
if ($result === true) {
$_SESSION['notice'] = "Host deleted successfully.";
2025-01-20 19:17:54 +00:00
} else {
$_SESSION['error'] = "Deleting the host failed. Error: $result";
2025-01-20 19:17:54 +00:00
}
} else if (!isset($_POST['host'])) { // This is a new host
$newHost = [
'address' => $_POST['address'],
'platform_id' => $_POST['platform'],
'name' => $_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' => $_POST['name'],
];
$result = $hostObject->editHost($platform_id, $updatedHost);
2025-01-20 19:17:54 +00:00
if ($result === true) {
$_SESSION['notice'] = "Host edited.";
} else {
$_SESSION['error'] = "Editing the host failed. Error: $result";
}
2024-10-31 09:25:37 +00:00
}
// 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' => $_POST['secret_key'],
'check_period' => $_POST['check_period'],
];
2025-01-22 15:46:08 +00:00
$result = $agentObject->addAgent($_POST['host'], $newAgent);
if ($result === true) {
2025-01-22 15:46:08 +00:00
$_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 = [
'url' => $_POST['url'],
'secret_key' => $_POST['secret_key'],
2025-01-22 15:46:08 +00:00
'check_period' => $_POST['check_period'],
];
2025-01-22 15:46:08 +00:00
$result = $agentObject->editAgent($agent_id, $updatedAgent);
if ($result === true) {
$_SESSION['notice'] = "Agent edited.";
} else {
$_SESSION['error'] = "Editing the agent failed. Error: $result";
}
2024-09-22 09:26:19 +00:00
}
// 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);
2025-01-20 19:17:54 +00:00
if ($result === true) {
$_SESSION['notice'] = "Platform deleted successfully.";
2025-01-20 19:17:54 +00:00
} else {
$_SESSION['error'] = "Deleting the platform failed. Error: $result";
2025-01-20 19:17:54 +00:00
}
} 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);
2025-01-20 19:17:54 +00:00
if ($result === true) {
$_SESSION['notice'] = "Platform edited.";
} else {
$_SESSION['error'] = "Editing the platform failed. Error: $result";
}
2024-10-31 09:39:03 +00:00
}
2024-08-19 10:25:09 +00:00
}
// After any POST operation, redirect back to the main config page
header("Location: $app_root?page=config");
2024-08-19 10:25:09 +00:00
exit();
} else {
2024-12-04 10:13:33 +00:00
/**
* Handles GET requests to display templates.
*/
2024-08-28 09:59:13 +00:00
switch ($item) {
2024-10-30 15:21:36 +00:00
2024-11-05 16:35:01 +00:00
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')) {
include '../app/templates/config-jilo.php';
} else {
include '../app/templates/error-unauthorized.php';
}
2024-08-19 10:25:09 +00:00
}
2024-08-18 19:12:45 +00:00
}
2024-08-12 11:12:24 +00:00
?>