jilo-web/app/pages/config.php

125 lines
3.8 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
2025-02-17 14:50:57 +00:00
include '../app/helpers/feedback.php';
require '../app/classes/config.php';
2025-04-11 13:55:08 +00:00
require '../app/classes/api_response.php';
2024-08-18 19:12:45 +00:00
2025-04-11 13:55:08 +00:00
// Initialize required objects
$userObject = new User($dbWeb);
$logObject = new Log($dbWeb);
$configObject = new Config();
2025-02-17 13:15:05 +00:00
// For AJAX requests
2025-04-11 15:29:47 +00:00
$isAjax = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
2025-04-11 15:29:47 +00:00
// Set JSON content type for AJAX requests
if ($isAjax) {
header('Content-Type: application/json');
}
2025-04-11 13:55:08 +00:00
// Ensure config file path is set
if (!isset($config_file) || empty($config_file)) {
if ($isAjax) {
ApiResponse::error('Config file path not set');
2025-04-11 15:29:47 +00:00
exit;
2025-04-11 13:55:08 +00:00
} else {
Feedback::flash('ERROR', 'DEFAULT', 'Config file path not set');
header('Location: ' . htmlspecialchars($app_root));
2025-04-11 15:29:47 +00:00
exit;
2025-04-11 13:55:08 +00:00
}
}
// 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);
2025-04-11 15:29:47 +00:00
if ($isAjax) {
ApiResponse::error('Config file is not writable', null, 403);
exit;
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
2025-04-11 13:55:08 +00:00
// Check if user has permission to edit config
if (!$userObject->hasRight($user_id, 'edit config file')) {
$logObject->insertLog($user_id, "Unauthorized: User \"$currentUser\" tried to edit config file. IP: $user_IP", 'system');
if ($isAjax) {
ApiResponse::error('Forbidden: You do not have permission to edit the config file', null, 403);
2025-04-11 15:29:47 +00:00
exit;
2025-04-11 13:55:08 +00:00
} else {
include '../app/templates/error-unauthorized.php';
2025-04-11 15:29:47 +00:00
exit;
2025-04-11 13:55:08 +00:00
}
}
2025-02-17 13:15:05 +00:00
// Apply rate limiting
2025-04-11 13:55:08 +00:00
require '../app/includes/rate_limit_middleware.php';
2025-02-17 13:15:05 +00:00
checkRateLimit($dbWeb, 'config', $user_id);
// Ensure no output before this point
ob_clean();
// For AJAX requests, get JSON data
if ($isAjax) {
// Get raw input
$jsonData = file_get_contents('php://input');
2025-04-11 13:55:08 +00:00
if ($jsonData === false) {
$logObject->insertLog($user_id, "Failed to read request data for config update", 'system');
ApiResponse::error('Failed to read request data');
2025-04-11 15:29:47 +00:00
exit;
2025-04-11 13:55:08 +00:00
}
2025-01-26 15:32:37 +00:00
2025-04-11 13:55:08 +00:00
// Try to parse JSON
$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-04-11 13:55:08 +00:00
ApiResponse::error('Invalid JSON data received: ' . $error);
2025-04-11 15:29:47 +00:00
exit;
2024-11-01 16:23:40 +00:00
}
// Try to update config file
$result = $configObject->editConfigFile($postData, $config_file);
2025-04-11 13:55:08 +00:00
if ($result['success']) {
ApiResponse::success($result['updated'], 'Config file updated successfully');
} else {
2025-04-11 13:55:08 +00:00
ApiResponse::error($result['error']);
2024-09-22 09:26:19 +00:00
}
2025-04-11 15:29:47 +00:00
exit;
} else {
2025-04-11 15:29:47 +00:00
// Handle non-AJAX POST
$result = $configObject->editConfigFile($_POST, $config_file);
if ($result['success']) {
Feedback::flash('NOTICE', 'DEFAULT', 'Config file updated successfully', true);
} else {
Feedback::flash('ERROR', 'DEFAULT', "Error updating config file: " . $result['error'], true);
}
2024-08-19 10:25:09 +00:00
2025-04-11 15:29:47 +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';
}
}