34950-vm/register.php
Flatlogic Bot 94ddfaeca6 28283
2025-10-17 12:05:55 +00:00

110 lines
5.0 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
require_once 'mail/MailService.php';
header('Content-Type: application/json');
// --- Helper function to fetch webinar details ---
function get_webinar_details($id) {
if (empty($id)) return null;
try {
$stmt = db()->prepare("SELECT id, title, description, scheduled_at, presenter FROM webinars WHERE id = ?");
$stmt->execute([$id]);
return $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_log("Database error fetching webinar ID $id: " . $e->getMessage());
return null;
}
}
// --- Only allow POST requests ---
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
echo json_encode(['success' => false, 'error' => 'Invalid request method.']);
exit;
}
$webinar_id = filter_input(INPUT_POST, 'webinar_id', FILTER_VALIDATE_INT) ?: 1;
$webinar = get_webinar_details($webinar_id);
if (!$webinar) {
http_response_code(404);
echo json_encode(['success' => false, 'error' => 'Webinar not found.']);
exit;
}
// --- DATA CAPTURE ---
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$first_name = filter_input(INPUT_POST, 'first_name', FILTER_SANITIZE_STRING);
$last_name = filter_input(INPUT_POST, 'last_name', FILTER_SANITIZE_STRING);
$company = filter_input(INPUT_POST, 'company', FILTER_SANITIZE_STRING);
$how_did_you_hear = filter_input(INPUT_POST, 'how_did_you_hear', FILTER_SANITIZE_STRING);
$timezone = filter_input(INPUT_POST, 'timezone', FILTER_SANITIZE_STRING);
// --- VALIDATION ---
if (!$first_name || !$last_name || !$email) {
echo json_encode(['success' => false, 'error' => 'Please fill out all required fields.']);
exit;
}
try {
// --- CHECK IF ALREADY REGISTERED ---
$stmt = db()->prepare("SELECT id FROM attendees WHERE webinar_id = ? AND email = ?");
$stmt->execute([$webinar_id, $email]);
if ($stmt->fetch()) {
echo json_encode(['success' => false, 'error' => 'You are already registered for this webinar.']);
exit;
}
// --- REGISTER USER ---
// Generate a password hash from the email as we don't have a password field
$password_hash = password_hash($email . time(), PASSWORD_DEFAULT);
$sql = "INSERT INTO attendees (webinar_id, first_name, last_name, email, company, how_did_you_hear, password, timezone)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = db()->prepare($sql);
$stmt->execute([$webinar_id, $first_name, $last_name, $email, $company, $how_did_you_hear, $password_hash, $timezone]);
// --- SEND CONFIRMATION EMAIL ---
$webinar_date_obj = new DateTime('2025-12-29 13:00:00', new DateTimeZone('America/New_York'));
$subject = "Confirmation: You're Registered for Professional Vibe-Coding Webinar by Flatlogic";
$body_html = "<h1>You're in!</h1><p>Thanks for registering for our webinar: <strong>Professional Vibe-Coding Webinar by Flatlogic</strong>.</p><p>It will take place on <strong>" . $webinar_date_obj->format('l, F j, Y \a\t g:i A T') . "</strong>.</p><p>You can now log in to your dashboard to see the details.</p>";
MailService::sendMail($email, $subject, $body_html);
// --- PREPARE SUCCESS RESPONSE ---
$webinar_date = new DateTime('2025-12-29 13:00:00', new DateTimeZone('America/New_York'));
$webinar_date->setTimezone(new DateTimeZone('UTC'));
$start_time_utc = $webinar_date->format('Ymd\THis\Z');
$webinar_date->add(new DateInterval('PT1H')); // Assume 1 hour duration
$end_time_utc = $webinar_date->format('Ymd\THis\Z');
$google_link = 'https://www.google.com/calendar/render?action=TEMPLATE&text=' . urlencode('Professional Vibe-Coding Webinar by Flatlogic') . '&dates=' . $start_time_utc . '/' . $end_time_utc . '&details=' . urlencode('The fastest way to go from an idea to a working app you own, running on your server, with your database, using real frameworks.') . '&location=' . urlencode('Online') . '&ctz=UTC';
$ics_content = implode("\r\n", [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'PRODID:-//Flatlogic//Professional Vibe-Coding Webinar//EN',
'BEGIN:VEVENT',
'URL:' . 'http://' . $_SERVER['HTTP_HOST'],
'DTSTART;TZID=UTC:' . $start_time_utc,
'DTEND;TZID=UTC:' . $end_time_utc,
'SUMMARY:Professional Vibe-Coding Webinar by Flatlogic',
'DESCRIPTION:The fastest way to go from an idea to a working app you own, running on your server, with your database, using real frameworks.',
'LOCATION:Online',
'END:VEVENT',
'END:VCALENDAR'
]);
$outlook_link = 'data:text/calendar;charset=utf-8,' . rawurlencode($ics_content);
echo json_encode([
'success' => true,
'webinar_title' => 'Professional Vibe-Coding Webinar by Flatlogic',
'google_link' => $google_link,
'outlook_link' => $outlook_link
]);
} catch (Exception $e) {
error_log("Registration error: " . $e->getMessage());
http_response_code(500);
echo json_encode(['success' => false, 'error' => 'An unexpected server error occurred. Please try again.']);
}