2024-08-12 11:12:24 +00:00
|
|
|
<?php
|
|
|
|
|
2024-08-18 19:12:45 +00:00
|
|
|
$action = $_REQUEST['action'] ?? '';
|
2024-08-19 10:42:16 +00:00
|
|
|
require '../app/helpers/errors.php';
|
2024-08-26 18:19:21 +00:00
|
|
|
require '../app/helpers/config.php';
|
2024-08-18 19:12:45 +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-08-19 10:25:09 +00:00
|
|
|
// 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-08-26 18:19:21 +00:00
|
|
|
// new platform adding
|
|
|
|
if (isset($_POST['new']) && $_POST['new'] === 'true') {
|
|
|
|
$newPlatform = [
|
|
|
|
'name' => $_POST['name'],
|
|
|
|
'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);
|
|
|
|
|
|
|
|
// deleting a platform
|
|
|
|
} elseif (isset($_POST['delete']) && $_POST['delete'] === 'true') {
|
|
|
|
$platform = $_POST['platform'];
|
|
|
|
|
|
|
|
$config['platforms'][$platform]['name'] = $_POST['name'];
|
|
|
|
$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*'jilo_database'\s*=>\s*'[^']*',\s*\],/s",
|
|
|
|
"",
|
|
|
|
$content
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// an update to an existing platform
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$platform = $_POST['platform'];
|
|
|
|
|
|
|
|
$config['platforms'][$platform]['name'] = $_POST['name'];
|
|
|
|
$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*'jilo_database'\s*=>\s*'[^']*',\s*\],/s",
|
|
|
|
"\n '{$platform}' => {$platformsArray},",
|
|
|
|
$content
|
2024-08-20 09:58:55 +00:00
|
|
|
);
|
2024-08-26 18:19:21 +00:00
|
|
|
|
2024-08-19 10:25:09 +00:00
|
|
|
}
|
|
|
|
|
2024-08-26 18:19:21 +00:00
|
|
|
|
|
|
|
|
2024-08-19 10:42:16 +00:00
|
|
|
// 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
|
2024-08-19 10:25:09 +00:00
|
|
|
if (file_put_contents($config_file, $updatedContent) !== false) {
|
|
|
|
// update successful
|
|
|
|
$_SESSION['notice'] = "Configuration for {$_POST['name']} is updated.";
|
|
|
|
} else {
|
|
|
|
// unsuccessful
|
2024-08-19 10:42:16 +00:00
|
|
|
$error = error_get_last();
|
|
|
|
$_SESSION['error'] = getError('Error updating the config: ' . ($error['message'] ?? 'unknown error'));
|
2024-08-19 10:25:09 +00:00
|
|
|
}
|
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 {
|
|
|
|
switch ($action) {
|
2024-08-26 18:19:21 +00:00
|
|
|
case 'add':
|
|
|
|
include('../app/templates/config-add-platform.php');
|
|
|
|
break;
|
2024-08-19 10:25:09 +00:00
|
|
|
case 'edit':
|
|
|
|
include('../app/templates/config-edit-platform.php');
|
|
|
|
break;
|
2024-08-26 18:19:21 +00:00
|
|
|
case 'delete':
|
|
|
|
include('../app/templates/config-delete-platform.php');
|
|
|
|
break;
|
2024-08-19 10:25:09 +00:00
|
|
|
default:
|
|
|
|
include('../app/templates/config-list.php');
|
|
|
|
}
|
2024-08-18 19:12:45 +00:00
|
|
|
}
|
2024-08-12 11:12:24 +00:00
|
|
|
|
|
|
|
?>
|