jilo-web/app/pages/config.php

207 lines
7.7 KiB
PHP
Raw Normal View History

2024-08-12 11:12:24 +00:00
<?php
2024-08-18 19:12:45 +00:00
$action = $_REQUEST['action'] ?? '';
2024-09-20 09:13:18 +00:00
$agent = $_REQUEST['agent'] ?? '';
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 a form is submitted, it's from the edit page
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
2024-08-18 19:12:45 +00:00
2024-09-04 22:06:38 +00:00
// 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;
2024-08-18 19:12:45 +00:00
2024-10-30 17:11:23 +00:00
// 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";
}
2024-09-20 09:13:18 +00:00
// new agent adding
2024-10-30 17:11:23 +00:00
} elseif (isset($_POST['new']) && isset($_POST['item']) && $_POST['new'] === 'true' && $_POST['item'] === 'agent') {
2024-09-20 09:13:18 +00:00
$newAgent = [
2024-10-04 08:36:45 +00:00
'type_id' => $type,
'url' => $url,
'secret_key' => $secret_key,
'check_period' => $check_period,
2024-09-20 09:13:18 +00:00
];
$result = $agentObject->addAgent($platform_id, $newAgent);
if ($result === true) {
$_SESSION['notice'] = "New Jilo Agent added.";
} else {
$_SESSION['error'] = "Adding the agent failed. Error: $result";
}
// new platform adding
2024-09-20 09:13:18 +00:00
} elseif (isset($_POST['new']) && $_POST['new'] === 'true') {
$newPlatform = [
2024-10-04 08:36:45 +00:00
'name' => $name,
'jitsi_url' => $_POST['jitsi_url'],
'jilo_database' => $_POST['jilo_database'],
];
$platformObject->addPlatform($newPlatform);
2024-09-20 09:13:18 +00:00
// deleting an agent
} elseif (isset($_POST['delete']) && isset($_POST['agent']) && $_POST['delete'] === 'true') {
2024-09-20 09:13:18 +00:00
$result = $agentObject->deleteAgent($agent);
if ($result === true) {
$_SESSION['notice'] = "Agent id \"{$_REQUEST['agent']}\" deleted.";
} else {
$_SESSION['error'] = "Deleting the agent failed. Error: $result";
}
// deleting a platform
} elseif (isset($_POST['delete']) && $_POST['delete'] === 'true') {
$platform = $_POST['platform'];
$platformObject->deletePlatform($platform);
2024-09-22 09:26:19 +00:00
// an update to an existing agent
} elseif (isset($_POST['agent'])) {
$updatedAgent = [
'id' => $agent,
2024-10-04 08:36:45 +00:00
'agent_type_id' => $type,
'url' => $url,
'secret_key' => $secret_key,
2024-10-24 09:11:35 +00:00
'check_period' => $check_period,
2024-09-22 09:26:19 +00:00
];
$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";
}
// an update to an existing platform
} else {
$platform = $_POST['platform'];
$updatedPlatform = [
2024-10-04 08:36:45 +00:00
'name' => $name,
'jitsi_url' => $_POST['jitsi_url'],
'jilo_database' => $_POST['jilo_database'],
];
$platformObject->editPlatform($platform, $updatedPlatform);
2024-08-19 10:25:09 +00:00
}
2024-09-06 16:03:08 +00:00
// FIXME - if this is not needed for editing the flat file, remove it
// // check if file is writable
// if (!is_writable($config_file)) {
// $_SESSION['error'] = getError('Configuration file is not writable.');
// header("Location: $app_root?platform=$platform_id&page=config");
// exit();
// }
//
// // try to update the config file
// if (file_put_contents($config_file, $updatedContent) !== false) {
// // update successful
// $_SESSION['notice'] = "Configuration for {$_POST['name']} is updated.";
// } else {
// // unsuccessful
// $error = error_get_last();
// $_SESSION['error'] = getError('Error updating the config: ' . ($error['message'] ?? 'unknown error'));
// }
2024-08-19 10:42:16 +00:00
2024-08-19 10:25:09 +00:00
// FIXME the new file is not loaded on first page load
unset($config);
header("Location: $app_root?platform=$platform_id&page=config");
exit();
// no form submitted, show the templates
} else {
2024-08-28 09:59:13 +00:00
switch ($item) {
2024-10-30 15:21:36 +00:00
2024-10-30 14:54:09 +00:00
case 'platform':
2024-10-30 15:10:35 +00:00
if (isset($action) && $action === 'add') {
include '../app/templates/config-platform-add.php';
} elseif (isset($action) && $action === 'edit') {
2024-10-30 14:54:09 +00:00
include '../app/templates/config-platform-edit.php';
2024-10-30 15:03:42 +00:00
} elseif (isset($action) && $action === 'delete') {
include '../app/templates/config-platform-delete.php';
2024-10-30 14:54:09 +00:00
} else {
if ($userObject->hasRight($user_id, 'view config file')) {
include '../app/templates/config-platform.php';
} else {
include '../app/templates/error-unauthorized.php';
}
}
2024-08-19 10:25:09 +00:00
break;
2024-10-30 15:21:36 +00:00
2024-10-30 14:54:09 +00:00
case 'host':
2024-10-30 15:21:36 +00:00
if (isset($action) && $action === 'add') {
include '../app/templates/config-host-add.php';
} elseif (isset($action) && $action === 'edit') {
include '../app/templates/config-host-edit.php';
} elseif (isset($action) && $action === 'delete') {
include '../app/templates/config-host-delete.php';
} else {
if ($userObject->hasRight($user_id, 'view config file')) {
2024-10-30 17:11:23 +00:00
$hostDetails = $hostObject->getHostDetails();
2024-10-30 15:21:36 +00:00
include '../app/templates/config-host.php';
} else {
include '../app/templates/error-unauthorized.php';
}
}
break;
2024-10-30 15:21:36 +00:00
2024-10-30 14:54:09 +00:00
case 'endpoint':
2024-10-30 15:21:36 +00:00
// TODO
2024-10-29 11:15:14 +00:00
break;
2024-10-30 15:21:36 +00:00
2024-10-29 11:15:14 +00:00
case 'config_file':
2024-10-29 11:38:22 +00:00
if (isset($action) && $action === 'edit') {
include '../app/templates/config-configfile-edit.php';
2024-10-29 11:15:14 +00:00
} else {
2024-10-29 11:38:22 +00:00
if ($userObject->hasRight($user_id, 'view config file')) {
include '../app/templates/config-configfile.php';
} else {
include '../app/templates/error-unauthorized.php';
}
2024-10-29 11:15:14 +00:00
}
break;
2024-08-28 09:59:13 +00:00
2024-09-21 09:21:46 +00:00
// if there is no $item, we work on the local config DB
2024-08-19 10:25:09 +00:00
default:
2024-08-28 09:59:13 +00:00
switch ($action) {
2024-09-20 09:13:18 +00:00
case 'add-agent':
$jilo_agent_types = $agentObject->getAgentTypes();
$jilo_agents_in_platform = $agentObject->getPlatformAgentTypes($platform_id);
$jilo_agent_types_in_platform = array_column($jilo_agents_in_platform, 'agent_type_id');
2024-09-20 09:13:18 +00:00
include '../app/templates/config-add-agent.php';
break;
2024-08-28 09:59:13 +00:00
case 'edit':
2024-09-20 09:13:18 +00:00
if (isset($_GET['agent'])) {
$agentDetails = $agentObject->getAgentDetails($platform_id, $agent);
$jilo_agent_types = $agentObject->getAgentTypes();
2024-09-20 09:13:18 +00:00
include '../app/templates/config-edit-agent.php';
}
2024-08-28 09:59:13 +00:00
break;
case 'delete':
2024-09-21 09:21:46 +00:00
if (isset($_GET['agent'])) {
$agentDetails = $agentObject->getAgentDetails($platform_id, $agent);
include '../app/templates/config-delete-agent.php';
}
2024-08-28 09:59:13 +00:00
break;
}
2024-08-19 10:25:09 +00:00
}
2024-08-18 19:12:45 +00:00
}
2024-08-12 11:12:24 +00:00
?>