One Hat Cyber Team
Your IP :
216.73.216.36
Server IP :
162.240.179.46
Server :
Linux vps-14493116.nutrivittasaude.com.br 5.14.0-611.49.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Apr 21 16:39:08 EDT 2026 x86_64
Server Software :
Apache
PHP Version :
8.2.31
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
home
/
lifeprimeti
/
meta.lifeprimeti.com.br
/
api
/
View File Name :
ia_executar.php
<?php require_once __DIR__ . '/../config/database.php'; header('Content-Type: application/json; charset=utf-8'); requireAuth(); if ($_SERVER['REQUEST_METHOD'] !== 'POST') { echo json_encode(['success' => false, 'error' => 'Metodo nao permitido']); exit; } $input = json_decode(file_get_contents('php://input'), true); $promptId = (int)($input['prompt_id'] ?? 0); if (!$promptId) { echo json_encode(['success' => false, 'error' => 'ID do prompt nao informado']); exit; } $empresaId = getEmpresaId(); $stmt = $pdo->prepare("SELECT * FROM ia_prompts WHERE id=? AND empresa_id=? AND ativo=1"); $stmt->execute([$promptId, $empresaId]); $prompt = $stmt->fetch(); if (!$prompt) { echo json_encode(['success' => false, 'error' => 'Prompt nao encontrado ou inativo']); exit; } $config = getConfig(); $apiKey = ''; switch ($prompt['ia_provedor']) { case 'openai': $apiKey = $config['openai_api_key'] ?? ''; break; case 'gemini': $apiKey = $config['gemini_api_key'] ?? ''; break; case 'claude': $apiKey = $config['claude_api_key'] ?? ''; break; } if (empty($apiKey)) { echo json_encode(['success' => false, 'error' => 'Chave de API nao configurada para ' . $prompt['ia_provedor']]); exit; } try { $resposta = ''; switch ($prompt['ia_provedor']) { case 'openai': $resposta = executarOpenAI($apiKey, $prompt); break; case 'gemini': $resposta = executarGemini($apiKey, $prompt); break; case 'claude': $resposta = executarClaude($apiKey, $prompt); break; } echo json_encode(['success' => true, 'resposta' => $resposta]); } catch (Exception $e) { echo json_encode(['success' => false, 'error' => $e->getMessage()]); } function executarOpenAI($apiKey, $prompt) { $data = [ 'model' => $prompt['modelo'] ?: 'gpt-4o-mini', 'messages' => [['role' => 'user', 'content' => $prompt['prompt_texto']]], 'temperature' => (float)$prompt['temperatura'], 'max_tokens' => (int)$prompt['max_tokens'], ]; $resp = fazerRequest('https://api.openai.com/v1/chat/completions', 'Bearer ' . $apiKey, $data); return $resp['choices'][0]['message']['content'] ?? 'Sem resposta'; } function executarGemini($apiKey, $prompt) { $modelo = $prompt['modelo'] ?: 'gemini-2.0-flash'; $data = [ 'contents' => [['parts' => [['text' => $prompt['prompt_texto']]]]], 'generationConfig' => [ 'temperature' => (float)$prompt['temperatura'], 'maxOutputTokens' => (int)$prompt['max_tokens'], ], ]; $url = "https://generativelanguage.googleapis.com/v1beta/models/{$modelo}:generateContent?key=" . $apiKey; $resp = fazerRequest($url, '', $data); return $resp['candidates'][0]['content']['parts'][0]['text'] ?? 'Sem resposta'; } function executarClaude($apiKey, $prompt) { $data = [ 'model' => $prompt['modelo'] ?: 'claude-3-5-haiku-latest', 'max_tokens' => (int)$prompt['max_tokens'], 'temperature' => (float)$prompt['temperatura'], 'messages' => [['role' => 'user', 'content' => $prompt['prompt_texto']]], ]; $resp = fazerRequest('https://api.anthropic.com/v1/messages', 'x-api-key: ' . $apiKey, $data, ['anthropic-version: 2023-06-01']); return $resp['content'][0]['text'] ?? 'Sem resposta'; } function fazerRequest($url, $auth, $data, $extraHeaders = []) { $ch = curl_init($url); $headers = ['Content-Type: application/json']; if ($auth) { if (strpos($auth, 'x-api-key:') === 0) { $headers[] = $auth; } else { $headers[] = 'Authorization: ' . $auth; } } foreach ($extraHeaders as $h) { $headers[] = $h; } curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($data), CURLOPT_HTTPHEADER => $headers, CURLOPT_TIMEOUT => 60, CURLOPT_SSL_VERIFYPEER => true, ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); if ($error) throw new Exception('Erro de conexao: ' . $error); if ($httpCode >= 400) { $body = json_decode($response, true); $msg = $body['error']['message'] ?? $body['error']['msg'] ?? $response; throw new Exception('API retornou erro ' . $httpCode . ': ' . (is_string($msg) ? $msg : json_encode($msg))); } $decoded = json_decode($response, true); if (!$decoded) throw new Exception('Resposta invalida da API'); return $decoded; } ?>