diff --git a/api.php b/api.php index 279d1ea..8cb781f 100644 --- a/api.php +++ b/api.php @@ -1,13 +1,23 @@ true, + 'user' => [ + 'name' => 'User', + 'preferred_username' => 'anonymous@nano-banana-pro.com' + ] +]; + // Check authentication (with graceful fallback) try { if (file_exists(__DIR__ . '/AuthMiddleware.php')) { @@ -289,6 +299,23 @@ try { // Add to conversation history $sessionManager->addToHistory($prompt, $inputImage ? 'edit' : 'generate'); + // Log to webhook + try { + $userEmail = $authStatus['user']['preferred_username'] ?? $authStatus['user']['email'] ?? 'anonymous@nano-banana-pro.com'; + $webhookSettings = [ + 'prompt' => $prompt, + 'aspectRatio' => $aspectRatio, + 'imageSize' => $imageSize, + 'actionType' => $inputImage ? 'edit' : 'generate', + 'model' => 'Google Imagen 3' + ]; + + logImageGeneration($prompt, $imageData['base64'], $imageData['mime_type'], $webhookSettings, $userEmail, $inputImage ? 'edit' : 'generate'); + } catch (Exception $e) { + // Don't fail if webhook fails + error_log("Webhook logging failed: " . $e->getMessage()); + } + echo json_encode([ 'success' => true, 'message' => 'Image generated successfully' diff --git a/enhance_prompt.php b/enhance_prompt.php index e68699c..581a530 100644 --- a/enhance_prompt.php +++ b/enhance_prompt.php @@ -5,9 +5,19 @@ */ require_once 'config.php'; +require_once 'webhook_logger.php'; header('Content-Type: application/json'); +// Initialize auth status with default +$authStatus = [ + 'authenticated' => true, + 'user' => [ + 'name' => 'User', + 'preferred_username' => 'anonymous@nano-banana-pro.com' + ] +]; + // Check authentication (with graceful fallback) try { if (file_exists(__DIR__ . '/AuthMiddleware.php')) { @@ -222,6 +232,24 @@ try { $enhancedPrompt = trim($result['candidates'][0]['content']['parts'][0]['text']); + // Log to webhook + try { + $userEmail = $authStatus['user']['preferred_username'] ?? $authStatus['user']['email'] ?? 'anonymous@nano-banana-pro.com'; + $webhookSettings = [ + 'sceneDescription' => $sceneDescription, + 'camera' => $camera, + 'lens' => $lens, + 'application' => $application, + 'aspectRatio' => $aspectRatio, + 'creativeFreedom' => $creativeFreedom + ]; + + logPromptEnhancement($sceneDescription, $enhancedPrompt, $webhookSettings, $userEmail); + } catch (Exception $e) { + // Don't fail if webhook fails + error_log("Webhook logging failed in enhance_prompt: " . $e->getMessage()); + } + echo json_encode([ 'success' => true, 'enhancedPrompt' => $enhancedPrompt diff --git a/webhook_logger.php b/webhook_logger.php new file mode 100644 index 0000000..7da610b --- /dev/null +++ b/webhook_logger.php @@ -0,0 +1,140 @@ + bool, 'http_code' => int, 'response' => string] + */ +function sendToWebhook($data) { + // Webhook URL (Make.com integration) + $webhookUrl = 'https://hook.us1.make.celonis.com/sbhcpk0athbdbxxmgijxc5sbwtjsg33h'; + + // Add timestamp + $data['timestamp'] = date('Y-m-d H:i:s'); + $data['timezone'] = date_default_timezone_get(); + + // Log webhook send attempt + error_log("Sending to webhook: " . json_encode(array_keys($data))); + + try { + $ch = curl_init($webhookUrl); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json' + ]); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 10 second timeout + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); + + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $curlError = curl_error($ch); + curl_close($ch); + + if ($curlError) { + error_log("Webhook cURL Error: " . $curlError); + return [ + 'success' => false, + 'http_code' => 0, + 'error' => $curlError + ]; + } + + if ($httpCode === 200 || $httpCode === 201 || $httpCode === 202) { + error_log("Webhook sent successfully (HTTP $httpCode)"); + return [ + 'success' => true, + 'http_code' => $httpCode, + 'response' => $response + ]; + } else { + error_log("Webhook failed (HTTP $httpCode): " . $response); + return [ + 'success' => false, + 'http_code' => $httpCode, + 'response' => $response + ]; + } + + } catch (Exception $e) { + error_log("Webhook exception: " . $e->getMessage()); + return [ + 'success' => false, + 'error' => $e->getMessage() + ]; + } +} + +/** + * Log image generation to webhook + * + * @param string $prompt The prompt used + * @param string $imageBase64 Base64 encoded image + * @param string $mimeType Image MIME type + * @param array $settings Generation settings + * @param string $userEmail User email + * @param string $actionType Type of action (generate, edit, upload) + */ +function logImageGeneration($prompt, $imageBase64, $mimeType, $settings, $userEmail, $actionType = 'generate') { + $webhookData = [ + 'client' => 'Internal', // Hardcoded for Nano Banana Pro + 'deliverableNumber' => 'NANO-' . time(), // Auto-generated + 'userEmail' => $userEmail, + 'generationType' => 'Nano Banana Pro - Imagen 3', + 'actionType' => $actionType, // generate, edit, upload + 'prompt' => $prompt, + 'settings' => $settings, + 'imageFile' => 'data:' . $mimeType . ';base64,' . $imageBase64 + ]; + + return sendToWebhook($webhookData); +} + +/** + * Log prompt enhancement to webhook + * + * @param string $originalPrompt Original scene description + * @param string $enhancedPrompt AI-enhanced prompt + * @param array $settings Studio settings (camera, lens, application, etc.) + * @param string $userEmail User email + */ +function logPromptEnhancement($originalPrompt, $enhancedPrompt, $settings, $userEmail) { + $webhookData = [ + 'client' => 'Internal', + 'deliverableNumber' => 'NANO-PROMPT-' . time(), + 'userEmail' => $userEmail, + 'generationType' => 'Nano Banana Pro - Prompt Enhancement', + 'actionType' => 'prompt_enhancement', + 'originalPrompt' => $originalPrompt, + 'enhancedPrompt' => $enhancedPrompt, + 'settings' => $settings + ]; + + return sendToWebhook($webhookData); +} + +/** + * Log user action to webhook (general purpose) + * + * @param string $action Action name + * @param array $data Action data + * @param string $userEmail User email + */ +function logUserAction($action, $data, $userEmail) { + $webhookData = [ + 'client' => 'Internal', + 'deliverableNumber' => 'NANO-ACTION-' . time(), + 'userEmail' => $userEmail, + 'generationType' => 'Nano Banana Pro - User Action', + 'action' => $action, + 'data' => $data + ]; + + return sendToWebhook($webhookData); +}