Fixes error handling

main
Yasen Pramatarov 2024-08-19 13:42:16 +03:00
parent 1cda74cd50
commit 2bbc4af068
2 changed files with 13 additions and 2 deletions

View File

@ -29,7 +29,7 @@ return [
// system info // system info
'version' => '0.1.1', 'version' => '0.1.1',
// development has verbose error messages, production has not // development has verbose error messages, production has not
'environment' => 'production1', 'environment' => 'production',
// ************************************* // *************************************
// Maintained by the app, edit with care // Maintained by the app, edit with care

View File

@ -1,6 +1,7 @@
<?php <?php
$action = $_REQUEST['action'] ?? ''; $action = $_REQUEST['action'] ?? '';
require '../app/helpers/errors.php';
// if a form is submitted, it's from the edit page // if a form is submitted, it's from the edit page
if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($_SERVER['REQUEST_METHOD'] == 'POST') {
@ -20,13 +21,23 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$updatedContent = preg_replace($oldValue, $newValue, $updatedContent); $updatedContent = preg_replace($oldValue, $newValue, $updatedContent);
} }
// 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) { if (file_put_contents($config_file, $updatedContent) !== false) {
// update successful // update successful
$_SESSION['notice'] = "Configuration for {$_POST['name']} is updated."; $_SESSION['notice'] = "Configuration for {$_POST['name']} is updated.";
} else { } else {
// unsuccessful // unsuccessful
$_SESSION['error'] = 'Error updating the config'; $error = error_get_last();
$_SESSION['error'] = getError('Error updating the config: ' . ($error['message'] ?? 'unknown error'));
} }
// FIXME the new file is not loaded on first page load // FIXME the new file is not loaded on first page load
unset($config); unset($config);
header("Location: $app_root?platform=$platform_id&page=config"); header("Location: $app_root?platform=$platform_id&page=config");