This commit is contained in:
2026-09-12 11:12:31 +02:00
parent 09ab527a93
commit bb0bf658ed
6 changed files with 208 additions and 107 deletions
+37 -13
View File
@@ -45,8 +45,17 @@ function get_all_system_settings(bool $forceRefresh = false): array {
$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;
if ($val === null) {
$settingsCache[$row['setting_key']] = null;
continue;
}
$trimmed = trim((string)$val);
if (str_starts_with($trimmed, '{') || str_starts_with($trimmed, '[')) {
$decoded = json_decode($val, true);
$settingsCache[$row['setting_key']] = (json_last_error() === JSON_ERROR_NONE) ? $decoded : $val;
} else {
$settingsCache[$row['setting_key']] = $val;
}
}
} catch (Throwable $e) {
error_log("get_all_system_settings error: " . $e->getMessage());
@@ -60,7 +69,10 @@ function get_all_system_settings(bool $forceRefresh = false): array {
*/
function get_system_setting(string $key, $default = null) {
$all = get_all_system_settings();
return array_key_exists($key, $all) ? $all[$key] : $default;
if (array_key_exists($key, $all) && $all[$key] !== null && $all[$key] !== '') {
return $all[$key];
}
return $default;
}
/**
@@ -123,6 +135,8 @@ function get_ai_settings(?int $userId = null): array {
$apiKey = '';
$isPersonal = false;
$personalProvider = null;
$personalModel = null;
// 1. Controleer persoonlijke sleutel van de ingelogde gebruiker
if ($userId !== null && $userId > 0) {
@@ -134,12 +148,12 @@ function get_ai_settings(?int $userId = null): array {
if ($row) {
// Sleutel kan in access_token staan of in settings['api_key']
if (!empty($row['access_token'])) {
$apiKey = fociz_decrypt($row['access_token']);
$apiKey = fociz_decrypt((string)$row['access_token']);
$isPersonal = true;
} elseif (!empty($row['settings'])) {
$userSettings = json_decode($row['settings'], true) ?: [];
if (!empty($userSettings['api_key'])) {
$apiKey = fociz_decrypt($userSettings['api_key']);
$apiKey = fociz_decrypt((string)$userSettings['api_key']);
$isPersonal = true;
}
}
@@ -147,10 +161,10 @@ function get_ai_settings(?int $userId = null): array {
if (!empty($row['settings'])) {
$userSettings = json_decode($row['settings'], true) ?: [];
if (!empty($userSettings['provider'])) {
$personalProvider = $userSettings['provider'];
$personalProvider = (string)$userSettings['provider'];
}
if (!empty($userSettings['model'])) {
$personalModel = $userSettings['model'];
$personalModel = (string)$userSettings['model'];
}
}
}
@@ -163,24 +177,34 @@ function get_ai_settings(?int $userId = null): array {
if (empty($apiKey)) {
$savedKeyRaw = get_system_setting('ai_api_key', '');
if (!empty($savedKeyRaw)) {
$apiKey = fociz_decrypt($savedKeyRaw);
$apiKey = fociz_decrypt((string)$savedKeyRaw);
}
}
// 3. Fallback naar .env variabelen (GEMINI_API_KEY of OPENAI_API_KEY)
if (empty($apiKey)) {
$apiKey = fociz_env('GEMINI_API_KEY', '') ?: fociz_env('OPENAI_API_KEY', '');
$apiKey = (string)(fociz_env('GEMINI_API_KEY', '') ?: fociz_env('OPENAI_API_KEY', ''));
}
$provider = !empty($personalProvider) ? $personalProvider : get_system_setting('ai_provider', 'gemini');
if (empty($provider)) {
$provider = 'gemini';
}
$model = !empty($personalModel) ? $personalModel : get_system_setting('ai_model', 'gemini-1.5-pro');
if (empty($model)) {
$model = (strpos((string)$provider, 'openai') !== false) ? 'gpt-4o-mini' : 'gemini-1.5-pro';
}
$apiKey = trim((string)$apiKey);
$hasKey = ($apiKey !== '');
return [
'ai_enabled' => (bool)get_system_setting('ai_enabled', !empty($apiKey)),
'ai_provider' => $provider,
'ai_enabled' => (bool)get_system_setting('ai_enabled', $hasKey),
'ai_provider' => (string)$provider,
'ai_api_key' => $apiKey,
'ai_model' => $model,
'has_key' => !empty($apiKey),
'ai_model' => (string)$model,
'has_key' => $hasKey,
'is_personal_key'=> $isPersonal,
];
}