jilo-web/app/pages/config.php

102 lines
3.1 KiB
PHP
Raw Normal View History

2024-08-12 11:12:24 +00:00
<?php
2024-12-04 10:13:33 +00:00
/**
* Config management.
2024-12-04 10:13:33 +00:00
*
* This page handles the config file.
2024-12-04 10:13:33 +00:00
*/
2025-02-17 08:24:50 +00:00
// Get any new feedback messages
include '../app/includes/feedback-get.php';
include '../app/includes/feedback-show.php';
require '../app/classes/config.php';
2024-09-06 16:34:03 +00:00
$configObject = new Config();
2024-08-18 19:12:45 +00:00
2025-02-17 13:15:05 +00:00
require '../app/includes/rate_limit_middleware.php';
// For AJAX requests
$isAjax = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
// Check if file is writable
$isWritable = is_writable($config_file);
$configMessage = '';
if (!$isWritable) {
2025-02-16 08:18:26 +00:00
$configMessage = Feedback::render('ERROR', 'DEFAULT', 'Config file is not writable', false);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
2025-02-17 13:15:05 +00:00
// Apply rate limiting
checkRateLimit($dbWeb, 'config', $user_id);
// Ensure no output before this point
ob_clean();
// For AJAX requests, get JSON data
if ($isAjax) {
header('Content-Type: application/json');
// Get raw input
$jsonData = file_get_contents('php://input');
2025-01-26 15:32:37 +00:00
$postData = json_decode($jsonData, true);
if (json_last_error() !== JSON_ERROR_NONE) {
2025-01-26 15:32:37 +00:00
$error = json_last_error_msg();
2025-02-16 08:18:26 +00:00
Feedback::flash('ERROR', 'DEFAULT', 'Invalid JSON data received: ' . $error, true);
echo json_encode([
'success' => false,
2025-01-26 15:32:37 +00:00
'message' => 'Invalid JSON data received: ' . $error
]);
exit;
2024-11-01 16:23:40 +00:00
}
// Try to update config file
$result = $configObject->editConfigFile($postData, $config_file);
if ($result === true) {
2025-02-16 08:18:26 +00:00
$messageData = Feedback::getMessageData('NOTICE', 'DEFAULT', 'Config file updated successfully', true);
echo json_encode([
'success' => true,
'message' => 'Config file updated successfully',
'messageData' => $messageData
]);
} else {
2025-02-16 08:18:26 +00:00
$messageData = Feedback::getMessageData('ERROR', 'DEFAULT', "Error updating config file: $result", true);
echo json_encode([
'success' => false,
'message' => "Error updating config file: $result",
'messageData' => $messageData
]);
2024-09-22 09:26:19 +00:00
}
2025-01-22 20:52:50 +00:00
exit;
}
2024-09-22 09:26:19 +00:00
// Handle non-AJAX POST
$result = $configObject->editConfigFile($_POST, $config_file);
if ($result === true) {
2025-02-16 08:18:26 +00:00
Feedback::flash('NOTICE', 'DEFAULT', 'Config file updated successfully', true);
} else {
2025-02-16 08:18:26 +00:00
Feedback::flash('ERROR', 'DEFAULT', "Error updating config file: $result", true);
2024-08-19 10:25:09 +00:00
}
header('Location: ' . htmlspecialchars($app_root) . '?page=config');
exit;
2024-08-18 19:12:45 +00:00
}
2024-08-12 11:12:24 +00:00
// Only include template for non-AJAX requests
if (!$isAjax) {
2025-01-26 17:07:07 +00:00
/**
* Handles GET requests to display templates.
*/
if ($userObject->hasRight($user_id, 'view config file')) {
include '../app/templates/config.php';
} else {
$logObject->insertLog($user_id, "Unauthorized: User \"$currentUser\" tried to access \"config\" page. IP: $user_IP", 'system');
include '../app/templates/error-unauthorized.php';
}
}
2024-08-12 11:12:24 +00:00
?>