exec(" CREATE TABLE IF NOT EXISTS `system_settings` ( `setting_key` VARCHAR(64) PRIMARY KEY, `setting_value` TEXT NULL, `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; "); $ensured = true; } catch (Throwable $e) { error_log("ensure_system_settings_table error: " . $e->getMessage()); } } /** * Haalt alle systeeminstellingen in 1 geoptimaliseerde query op en bewaart deze in het static runtime geheugen */ function get_all_system_settings(bool $forceRefresh = false): array { static $settingsCache = null; if ($settingsCache !== null && !$forceRefresh) { return $settingsCache; } $settingsCache = []; try { $db = get_db_connection(); ensure_system_settings_table($db); $stmt = $db->query("SELECT setting_key, setting_value FROM system_settings"); while ($row = $stmt->fetch()) { $val = $row['setting_value']; $decoded = json_decode($val, true); $settingsCache[$row['setting_key']] = (json_last_error() === JSON_ERROR_NONE) ? $decoded : $val; } } catch (Throwable $e) { error_log("get_all_system_settings error: " . $e->getMessage()); } return $settingsCache; } /** * Haalt een enkele instelling op uit het geheugen (of fallback default) */ function get_system_setting(string $key, $default = null) { $all = get_all_system_settings(); return array_key_exists($key, $all) ? $all[$key] : $default; } /** * Slaat een instelling op in MariaDB en ververst direct het cachegeheugen */ function set_system_setting(string $key, $value): bool { try { $db = get_db_connection(); ensure_system_settings_table($db); $valToStore = is_array($value) || is_object($value) ? json_encode($value) : (string)$value; $stmt = $db->prepare(" INSERT INTO system_settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value), updated_at = NOW() "); $ok = $stmt->execute([$key, $valToStore]); if ($ok) { get_all_system_settings(true); } return $ok; } catch (Throwable $e) { error_log("set_system_setting error for {$key}: " . $e->getMessage()); return false; } } /** * Haalt alle organisatie werkdag standaarden op (Release v1.4) */ function get_workday_settings(): array { return [ 'workday_start' => get_system_setting('workday_start', '08:30'), 'workday_end' => get_system_setting('workday_end', '17:30'), 'lunch_start' => get_system_setting('lunch_start', '12:30'), 'lunch_end' => get_system_setting('lunch_end', '13:00'), 'pomodoro_focus' => (int)get_system_setting('pomodoro_focus', 25), 'pomodoro_deep' => (int)get_system_setting('pomodoro_deep', 50), 'pomodoro_break' => (int)get_system_setting('pomodoro_break', 5), 'default_task_duration' => (int)get_system_setting('default_task_duration', 30), ]; } /** * Haalt AI configuratie-instellingen op (Release v1.5) */ function get_ai_settings(): array { $envKey = fociz_env('OPENAI_API_KEY', ''); $savedKeyRaw = get_system_setting('ai_api_key', ''); $savedKey = ''; if (!empty($savedKeyRaw)) { if (!function_exists('fociz_decrypt')) { require_once __DIR__ . '/auth.php'; } $savedKey = fociz_decrypt($savedKeyRaw); } $apiKey = !empty($savedKey) ? $savedKey : $envKey; return [ 'ai_enabled' => (bool)get_system_setting('ai_enabled', !empty($apiKey)), 'ai_provider' => get_system_setting('ai_provider', 'openai'), 'ai_api_key' => $apiKey, 'ai_model' => get_system_setting('ai_model', 'gpt-4o-mini'), 'has_key' => !empty($apiKey), ]; } /** * Haalt geconfigureerde sessie idle timeout in seconden op conform NIST SP 800-63B / ISO 27001 * Toegestane waarden: 900 (15m), 1800 (30m - default), 3600 (60m), 7200 (120m) */ function get_session_idle_timeout(): int { $timeout = (int)get_system_setting('session_idle_timeout', 1800); $validTimeouts = [900, 1800, 3600, 7200]; return in_array($timeout, $validTimeouts, true) ? $timeout : 1800; }