Fix
This commit is contained in:
+42
-9
@@ -82,12 +82,19 @@ function fociz_ai_enforce_privacy_firewall(array &$messages): array {
|
||||
* @return array
|
||||
*/
|
||||
function fociz_ai_chat_gemini(array $messages, array $options, array $aiSettings): array {
|
||||
$apiKey = $aiSettings['ai_api_key'];
|
||||
$model = !empty($aiSettings['ai_model']) ? $aiSettings['ai_model'] : 'gemini-1.5-pro';
|
||||
$apiKey = trim((string)($aiSettings['ai_api_key'] ?? ''));
|
||||
$model = !empty($aiSettings['ai_model']) ? trim((string)$aiSettings['ai_model']) : 'gemini-1.5-pro';
|
||||
$temperature = $options['temperature'] ?? 0.3;
|
||||
$maxTokens = $options['max_tokens'] ?? 2048;
|
||||
$jsonMode = !empty($options['json_mode']);
|
||||
|
||||
if (empty($apiKey)) {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => 'Geen Google Gemini API-sleutel ingevuld of geconfigureerd.',
|
||||
];
|
||||
}
|
||||
|
||||
// Google Generative Language API structuur opbouwen
|
||||
$systemInstructions = [];
|
||||
$contents = [];
|
||||
@@ -131,7 +138,8 @@ function fociz_ai_chat_gemini(array $messages, array $options, array $aiSettings
|
||||
$payload['generationConfig']['responseMimeType'] = 'application/json';
|
||||
}
|
||||
|
||||
$endpoint = "https://generativelanguage.googleapis.com/v1beta/models/{$model}:generateContent";
|
||||
// Geef de sleutel zowel in de URL (?key=) als in de x-goog-api-key header mee voor proxy-ondersteuning
|
||||
$endpoint = "https://generativelanguage.googleapis.com/v1beta/models/{$model}:generateContent?key=" . urlencode($apiKey);
|
||||
|
||||
$response = fociz_http_request($endpoint, [
|
||||
'method' => 'POST',
|
||||
@@ -140,11 +148,20 @@ function fociz_ai_chat_gemini(array $messages, array $options, array $aiSettings
|
||||
'x-goog-api-key' => $apiKey,
|
||||
],
|
||||
'body' => $payload,
|
||||
'timeout' => 45,
|
||||
'timeout' => 20,
|
||||
]);
|
||||
|
||||
if ($response['status'] !== 200 || empty($response['json']['candidates'][0]['content']['parts'][0]['text'])) {
|
||||
$errMsg = $response['json']['error']['message'] ?? ($response['error'] ?? "Gemini API aanroep mislukt (HTTP {$response['status']})");
|
||||
$errMsg = null;
|
||||
if (!empty($response['json']['error']['message'])) {
|
||||
$errMsg = $response['json']['error']['message'];
|
||||
} elseif (!empty($response['error'])) {
|
||||
$errMsg = $response['error'];
|
||||
} elseif (!empty($response['body'])) {
|
||||
$errMsg = "Gemini fout (HTTP {$response['status']}): " . strip_tags(substr((string)$response['body'], 0, 200));
|
||||
} else {
|
||||
$errMsg = "Gemini API aanroep mislukt (HTTP {$response['status']})";
|
||||
}
|
||||
error_log("fociz_ai_chat_gemini fout: {$errMsg}");
|
||||
return [
|
||||
'success' => false,
|
||||
@@ -188,12 +205,19 @@ function fociz_ai_chat_gemini(array $messages, array $options, array $aiSettings
|
||||
* @return array
|
||||
*/
|
||||
function fociz_ai_chat_openai(array $messages, array $options, array $aiSettings): array {
|
||||
$apiKey = $aiSettings['ai_api_key'];
|
||||
$model = !empty($aiSettings['ai_model']) ? $aiSettings['ai_model'] : 'gpt-4o-mini';
|
||||
$apiKey = trim((string)($aiSettings['ai_api_key'] ?? ''));
|
||||
$model = !empty($aiSettings['ai_model']) ? trim((string)$aiSettings['ai_model']) : 'gpt-4o-mini';
|
||||
$temperature = $options['temperature'] ?? 0.3;
|
||||
$maxTokens = $options['max_tokens'] ?? 1500;
|
||||
$jsonMode = !empty($options['json_mode']);
|
||||
|
||||
if (empty($apiKey)) {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => 'Geen OpenAI API-sleutel ingevuld of geconfigureerd.',
|
||||
];
|
||||
}
|
||||
|
||||
$payload = [
|
||||
'model' => $model,
|
||||
'messages' => $messages,
|
||||
@@ -212,11 +236,20 @@ function fociz_ai_chat_openai(array $messages, array $options, array $aiSettings
|
||||
'Content-Type' => 'application/json',
|
||||
],
|
||||
'body' => $payload,
|
||||
'timeout' => 35,
|
||||
'timeout' => 20,
|
||||
]);
|
||||
|
||||
if ($response['status'] !== 200 || empty($response['json']['choices'][0]['message']['content'])) {
|
||||
$errMsg = $response['json']['error']['message'] ?? ($response['error'] ?? "OpenAI aanroep mislukt (HTTP {$response['status']})");
|
||||
$errMsg = null;
|
||||
if (!empty($response['json']['error']['message'])) {
|
||||
$errMsg = $response['json']['error']['message'];
|
||||
} elseif (!empty($response['error'])) {
|
||||
$errMsg = $response['error'];
|
||||
} elseif (!empty($response['body'])) {
|
||||
$errMsg = "OpenAI fout (HTTP {$response['status']}): " . strip_tags(substr((string)$response['body'], 0, 200));
|
||||
} else {
|
||||
$errMsg = "OpenAI API aanroep mislukt (HTTP {$response['status']})";
|
||||
}
|
||||
error_log("fociz_ai_chat_openai fout: {$errMsg}");
|
||||
return [
|
||||
'success' => false,
|
||||
|
||||
+2
-2
@@ -34,7 +34,7 @@ function is_https_request(): bool {
|
||||
/**
|
||||
* AES-256-GCM / AES-256-CBC encryptie helper voor gevoelige tokens (OAuth, API keys)
|
||||
*/
|
||||
function fociz_encrypt(string $plaintext): string {
|
||||
function fociz_encrypt(?string $plaintext): string {
|
||||
if (empty($plaintext)) return '';
|
||||
$key = hash('sha256', defined('APP_SECRET') ? APP_SECRET : 'fociz-maria-db-super-secret-key-998877', true);
|
||||
$iv = random_bytes(16);
|
||||
@@ -46,7 +46,7 @@ function fociz_encrypt(string $plaintext): string {
|
||||
/**
|
||||
* AES-256-CBC decryptie helper; decodeert indien versleuteld, laat legacy plaintext ongemoeid
|
||||
*/
|
||||
function fociz_decrypt(string $ciphertext): string {
|
||||
function fociz_decrypt(?string $ciphertext): string {
|
||||
if (empty($ciphertext)) return '';
|
||||
if (!str_starts_with($ciphertext, 'ENC:')) {
|
||||
return $ciphertext; // Plaintext legacy waarde
|
||||
|
||||
+37
-13
@@ -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,
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user