36129-vm/index.php
Flatlogic Bot 9511faa967 HalalReels
2025-11-23 16:26:48 +00:00

295 lines
15 KiB
PHP

<?php
require_once __DIR__ . '/mail/MailService.php';
require_once __DIR__ . '/ai/LocalAIApi.php';
require_once __DIR__ . '/db/config.php';
// Fetch Affiliate Products
try {
$pdo = db();
$stmt = $pdo->query('SELECT id, name FROM affiliate_products ORDER BY name ASC');
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_log('Database Error: ' . $e->getMessage());
$products = []; // Ensure products is an array even on error
}
// Form & AI Processing
$form_submitted = false;
$form_success = false;
$form_message = '';
$generated_script = '';
$filtered_script = '';
$ai_error = '';
$video_clips = [];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$form_submitted = true;
$name = htmlspecialchars($_POST['name'] ?? 'N/A');
$email = htmlspecialchars($_POST['email'] ?? 'N/A');
$topic = htmlspecialchars($_POST['topic'] ?? '');
$product_id = $_POST['product_id'] ?? null;
$selected_product = null;
if ($product_id && !empty($products)) {
foreach ($products as $p) {
if ($p['id'] == $product_id) {
$selected_product = $p;
break;
}
}
}
// 1. Send Email Notification
$email_message = "Reel Request: " . $topic;
if ($selected_product) {
$email_message .= "\nAffiliate Product: " . htmlspecialchars($selected_product['name']);
}
$to = getenv('MAIL_TO') ?: null;
$res = MailService::sendContactMessage($name, $email, $email_message, $to, 'New Reel Generation Request');
if (!empty($res['success'])) {
$form_success = true;
$form_message = 'Your request has been submitted! The AI is now generating your script below.';
} else {
$form_success = false;
$form_message = 'Sorry, there was an error sending your request. Please try again later.';
error_log('MailService Error: ' . ($res['error'] ?? 'Unknown'));
}
// 2. AI Script Generation & Filtering
if ($form_success && !empty($topic)) {
$system_prompt = 'You are an expert scriptwriter for short, high-retention social media videos. Create a script up to 60 seconds long.';
$user_prompt = 'Write a video script about: ' . $topic;
if ($selected_product) {
$user_prompt .= "\n\nIncorporate the following affiliate product naturally into the script: " . htmlspecialchars($selected_product['name']) . ".";
}
$generation_prompt = [
'input' => [
['role' => 'system', 'content' => $system_prompt],
['role' => 'user', 'content' => $user_prompt],
],
];
$generation_resp = LocalAIApi::createResponse($generation_prompt);
if (!empty($generation_resp['success'])) {
$generated_script = LocalAIApi::extractText($generation_resp);
if (!empty($generated_script)) {
$filtering_prompt = [
'input' => [
['role' => 'system', 'content' => 'You are a content moderator. Review the following script. Remove or rewrite any parts that are not compliant. Non-compliant ("haram") content includes explicit material, alcohol, pork, gambling, violence, offensive language, and religious insensitivity. Be conservative in your filtering. Output only the clean, final script. Do not add any introductory text or commentary.'],
['role' => 'user', 'content' => $generated_script],
],
];
$filtering_resp = LocalAIApi::createResponse($filtering_prompt);
if (!empty($filtering_resp['success'])) {
$filtered_script = LocalAIApi::extractText($filtering_resp);
if (empty($filtered_script)) {
$ai_error = 'The AI moderated the content but returned an empty script. This can happen with sensitive topics.';
} else {
// 3. AI Keyword Extraction
$keyword_prompt = [
'input' => [
['role' => 'system', 'content' => 'You are a keyword extraction specialist. From the following script, extract 3-5 relevant keywords for a stock video search. The keywords should be single words or short phrases that visually represent the scenes. Output only a comma-separated list of keywords. For example: "sunrise, coffee, laptop, city view".'],
['role' => 'user', 'content' => $filtered_script],
],
];
$keyword_resp = LocalAIApi::createResponse($keyword_prompt);
if (!empty($keyword_resp['success'])) {
$keywords = LocalAIApi::extractText($keyword_resp);
if (!empty($keywords)) {
// 4. Fetch Video Clips
$api_url = 'http://' . $_SERVER['HTTP_HOST'] . '/api/pexels.php?query=' . urlencode($keywords) . '&orientation=portrait';
$videos_json = @file_get_contents($api_url);
if ($videos_json) {
$videos_data = json_decode($videos_json, true);
if (!empty($videos_data['videos'])) {
$video_clips = $videos_data['videos'];
}
}
}
}
}
} else {
$ai_error = 'The AI failed to moderate the script. Error: ' . ($filtering_resp['error'] ?? 'Unknown');
error_log('AI Filtering Error: ' . ($filtering_resp['error'] ?? 'Unknown'));
}
} else {
$ai_error = 'The AI generated an empty script. Please try a different topic.';
}
} else {
$ai_error = 'The AI failed to generate a script. Error: ' . ($generation_resp['error'] ?? 'Unknown');
error_log('AI Generation Error: ' . ($generation_resp['error'] ?? 'Unknown'));
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Reel Generator</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body class="bg-dark text-white">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="index.php">AI Reel Creator</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="index.php#features">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.php#create">Create</a>
</li>
<li class="nav-item">
<a class="nav-link" href="products.php">Products</a>
</li>
</ul>
</div>
</div>
</nav>
<main class="container my-5 pt-5">
<section id="hero" class="text-center py-5">
<h1 class="display-4 fw-bold">AI-Powered Reels. Pure Content.</h1>
<p class="lead col-lg-6 mx-auto">Generate high-retention video scripts, automatically filtered for brand safety. Focus on your message, we'll handle the rest.</p>
<a href="#create" class="btn btn-primary-custom btn-lg">Start Creating</a>
</section>
<section id="features" class="py-5">
<div class="row text-center g-4">
<div class="col-md-4">
<div class="card bg-darker h-100 p-3">
<div class="card-body">
<i class="bi bi-robot fs-1 text-primary"></i>
<h3 class="card-title mt-3">AI Generation</h3>
<p class="card-text">From a simple idea to a full video script in seconds. Our AI crafts engaging content tailored for short-form video.</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card bg-darker h-100 p-3">
<div class="card-body">
<i class="bi bi-shield-check fs-1 text-primary"></i>
<h3 class="card-title mt-3">Haram-Free Content</h3>
<p class="card-text">Automatic, conservative filtering removes non-compliant elements, ensuring your content is always safe for your audience.</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card bg-darker h-100 p-3">
<div class="card-body">
<i class="bi bi-link-45deg fs-1 text-primary"></i>
<h3 class="card-title mt-3">Affiliate Integration</h3>
<p class="card-text">Easily attach affiliate products to your content and track performance with our simple, built-in tools.</p>
</div>
</div>
</div>
</div>
</section>
<section id="create" class="py-5 bg-darker rounded p-4 p-md-5 mt-5">
<h2 class="text-center mb-4">Generate Your Script</h2>
<?php if ($form_submitted): ?>
<div class="alert <?php echo $form_success ? 'alert-success' : 'alert-danger'; ?>">
<?php echo $form_message; ?>
</div>
<?php endif; ?>
<form action="index.php#create" method="POST">
<div class="mb-3">
<label for="name" class="form-label">Your Name</label>
<input type="text" class="form-control bg-dark text-white" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Your Email</label>
<input type="email" class="form-control bg-dark text-white" id="email" name="email" required>
</div>
<div class="mb-3">
<label for="topic" class="form-label">Topic / Idea</label>
<textarea class="form-control bg-dark text-white" id="topic" name="topic" rows="3" placeholder="e.g., '''a 30-second video on the benefits of waking up early'''" required></textarea>
</div>
<?php if (!empty($products)): ?>
<div class="mb-3">
<label for="product_id" class="form-label">Affiliate Product (Optional)</label>
<select class="form-select bg-dark text-white" id="product_id" name="product_id">
<option value="">None</option>
<?php foreach ($products as $product): ?>
<option value="<?php echo htmlspecialchars($product['id']); ?>">
<?php echo htmlspecialchars($product['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?>
<button type="submit" class="btn btn-primary w-100">Generate AI Script</button>
</form>
<?php if ($form_submitted && ($filtered_script || $ai_error)): ?>
<div id="ai-result" class="mt-5">
<h3 class="text-center">Your AI-Generated Script</h3>
<?php if ($ai_error): ?>
<div class="alert alert-warning mt-3">
<strong>AI Error:</strong> <?php echo htmlspecialchars($ai_error); ?>
</div>
<?php endif; ?>
<?php if ($filtered_script): ?>
<div class="card bg-dark mt-3">
<div class="card-body">
<pre class="text-white" style="white-space: pre-wrap;"><?php echo htmlspecialchars($filtered_script); ?></pre>
</div>
</div>
<?php endif; ?>
<?php if (!empty($video_clips)): ?>
<div id="video-clips" class="mt-5">
<h3 class="text-center">Suggested Halal Video Clips</h3>
<p class="text-center text-muted">Based on your script, here are some pre-filtered, royalty-free video clips you can download and use.</p>
<div class="row g-4 mt-3">
<?php foreach ($video_clips as $clip): ?>
<div class="col-md-4">
<div class="card bg-darker h-100 shadow-sm">
<a href="<?php echo htmlspecialchars($clip['download_url']); ?>" target="_blank" rel="noopener noreferrer" title="View and Download Video">
<img src="<?php echo htmlspecialchars($clip['thumbnail']); ?>" class="card-img-top" alt="Video thumbnail" style="aspect-ratio: 9/16; object-fit: cover;">
</a>
<div class="card-body text-center d-flex flex-column">
<a href="<?php echo htmlspecialchars($clip['download_url']); ?>" target="_blank" rel="noopener noreferrer" class="btn btn-sm btn-outline-light mt-auto">Download</a>
</div>
<div class="card-footer text-center" style="background-color: #212529;">
<small class="text-muted">by <a href="<?php echo htmlspecialchars($clip['photographer_url']); ?>" target="_blank" rel="noopener noreferrer" class="text-white-50"><?php echo htmlspecialchars($clip['photographer']); ?></a></small>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
</section>
</main>
<footer class="bg-darker text-center py-4 mt-5">
<div class="container">
<p class="mb-0">&copy; <?php echo date("Y"); ?> AI Reel Generator. All Rights Reserved.</p>
</div>
</footer>
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
</body>
</html>