<?php
// Start session for CAPTCHA
session_start();

// Include PHPMailer files manually
require_once 'PHPMailer/src/Exception.php';
require_once 'PHPMailer/src/PHPMailer.php';
require_once 'PHPMailer/src/SMTP.php';

// Note: If you manage to get Composer working, replace the above with:
// require_once 'vendor/autoload.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Generate CAPTCHA question if not exists or if refreshing
if (!isset($_SESSION['captcha_answer']) || isset($_GET['refresh_captcha'])) {
    $num1 = rand(1, 10);
    $num2 = rand(1, 10);
    $operations = ['+', '-', '*'];
    $operation = $operations[array_rand($operations)];
    
    switch ($operation) {
        case '+':
            $_SESSION['captcha_answer'] = $num1 + $num2;
            break;
        case '-':
            // Ensure positive result
            if ($num1 < $num2) {
                $temp = $num1;
                $num1 = $num2;
                $num2 = $temp;
            }
            $_SESSION['captcha_answer'] = $num1 - $num2;
            break;
        case '*':
            // Use smaller numbers for multiplication
            $num1 = rand(1, 5);
            $num2 = rand(1, 5);
            $_SESSION['captcha_answer'] = $num1 * $num2;
            break;
    }
    
    $_SESSION['captcha_question'] = "$num1 $operation $num2";
}

$message = '';
$messageType = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Sanitize and validate input
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $userMessage = trim($_POST['message'] ?? '');
    $captchaAnswer = trim($_POST['captcha'] ?? '');
    
    // Validation
    $errors = [];
    
    if (empty($name)) {
        $errors[] = 'Name is required.';
    }
    
    if (empty($email)) {
        $errors[] = 'Email is required.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = 'Please enter a valid email address.';
    }
    
    if (empty($userMessage)) {
        $errors[] = 'Message is required.';
    }
    
    // CAPTCHA validation
    if (empty($captchaAnswer)) {
        $errors[] = 'Please solve the math problem.';
    } elseif (!isset($_SESSION['captcha_answer']) || (int)$captchaAnswer !== $_SESSION['captcha_answer']) {
        $errors[] = 'Incorrect answer to the math problem. Please try again.';
        // Generate new CAPTCHA on wrong answer
        unset($_SESSION['captcha_answer']);
        unset($_SESSION['captcha_question']);
    }
    
    if (empty($errors)) {
        // Create a new PHPMailer instance
        $mail = new PHPMailer(true);
        
        try {
            // SMTP Configuration for OpenBSD mail server
            $mail->isSMTP();
            $mail->Host       = 'bokairc.com'; // Your OpenBSD mail server
            $mail->SMTPAuth   = true;
            $mail->Username   = 'noreply@bokairc.com'; // Your email username
            $mail->Password   = 'berlin23z'; // Your email password
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Use STARTTLS
            $mail->Port       = 587; // Submission port with STARTTLS
            
            // Optional: Enable debug output (remove in production)
            // $mail->SMTPDebug = SMTP::DEBUG_SERVER;
            
            // Recipients
            $mail->setFrom('noreply@bokairc.com', 'BokaIRC Contact Form');
            $mail->addAddress('support@bokairc.com', 'BokaIRC Support');
            $mail->addReplyTo($email, $name);
            
            // Content
            $mail->isHTML(false); // Set to plain text
            $mail->Subject = 'Contact Form Submission - BokaIRC.com';
            
            // Clean the message content
            $cleanMessage = preg_replace('/\r\n|\r|\n/', "\n", $userMessage);
            
            // Email body
            $emailBody = "New contact form submission from BokaIRC.com\n\n";
            $emailBody .= "Name: " . $name . "\n";
            $emailBody .= "Email: " . $email . "\n";
            $emailBody .= "Message:\n" . $cleanMessage . "\n\n";
            $emailBody .= "---\n";
            $emailBody .= "Sent from: " . $_SERVER['HTTP_HOST'] . "\n";
            $emailBody .= "IP Address: " . $_SERVER['REMOTE_ADDR'] . "\n";
            $emailBody .= "User Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\n";
            $emailBody .= "Date/Time: " . date('Y-m-d H:i:s') . "\n";
            
            $mail->Body = $emailBody;
            
            // Send the email
            $mail->send();
            
            $message = 'Thank you for your message! We will get back to you as soon as possible.';
            $messageType = 'success';
            // Clear form data and CAPTCHA on success
            $name = $email = $userMessage = '';
            unset($_SESSION['captcha_answer']);
            unset($_SESSION['captcha_question']);
            
        } catch (Exception $e) {
            $message = 'Sorry, there was an error sending your message. Please try again later or contact us directly at support@bokairc.com.';
            $messageType = 'error';
            error_log("PHPMailer Error: " . $mail->ErrorInfo);
        }
    } else {
        $message = 'Please correct the following errors:<br>• ' . implode('<br>• ', $errors);
        $messageType = 'error';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contact Us - BokaIRC.com</title>
	<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
    <link rel="manifest" href="/site.webmanifest">
    <link rel="stylesheet" href="style.css">

</head>
<body>
    <header>
        <img src="logo.png" alt="BokaIRC.com Logo" class="header-logo">
        <h1>Contact BokaIRC.com</h1>
    </header>

    <div class="container">
        <aside class="sidebar">
            <nav>
                <h3>Our Services</h3>
                <ul>
                    <li><a href="https://bnc.bokairc.com">BNC Panel</a></li>
                    <li><a href="https://mail.bokairc.com">Mail</a></li>
                    <li><a href="https://wiki.bokairc.com">Wiki</a></li>
                    <li><a href="https://forum.bokairc.com">Forum</a></li>
                </ul>

                <h3>External Links</h3>
                <ul>
                    <li><a href="https://wiki.ircnow.org/index.php?n=Adminforces.Training" target="_blank">Join SysAdmin Training</a></li>
                </ul>

                <h3>Navigation</h3>
                <ul>
                    <li><a href="index.html">Home</a></li>
                    <li><a href="staff.html">Staff</a></li>
                    <li><a href="servers.html">Servers</a></li>
                    <li><a href="contact.php">Contact</a></li>
                </ul>
            </nav>
        </aside>

        <main>
            <h2>Get in Touch</h2>
            <p>You can contact us by using our contact form below. We aim to respond to all inquiries within 24 hours.</p>

            <?php if (!empty($message)): ?>
                <div class="message <?php echo $messageType; ?>">
                    <?php echo $message; ?>
                </div>
            <?php endif; ?>

            <form class="contact-form" method="POST" action="contact.php">
                <label for="name">Name: *</label>
                <input type="text" id="name" name="name" value="<?php echo htmlspecialchars($name ?? '', ENT_QUOTES); ?>" required>

                <label for="email">Email: *</label>
                <input type="email" id="email" name="email" value="<?php echo htmlspecialchars($email ?? '', ENT_QUOTES); ?>" required>

                <label for="message">Message: *</label>
                <textarea id="message" name="message" rows="6" required><?php echo htmlspecialchars($userMessage ?? '', ENT_QUOTES); ?></textarea>

                <!-- CAPTCHA Section -->
                <div class="captcha-container">
                    <label for="captcha">Human Verification: *</label>
                    <div class="captcha-question">
                        What is <?php echo $_SESSION['captcha_question']; ?>?
                    </div>
                    <input type="number" id="captcha" name="captcha" class="captcha-input" required placeholder="Answer">
                    <a href="?refresh_captcha=1" class="refresh-captcha">New Question</a>
                    <div class="captcha-help">
                        Please solve the simple math problem above to verify you're human.
                    </div>
                </div>

                <button type="submit">Send Message</button>
            </form>

            <h2>Other Ways to Reach Us</h2>
            
            <h3>IRC Support</h3>
            <p>For immediate assistance, join our IRC network and visit:</p>
            <ul>
                <li><strong>#help</strong> - General help and support</li>
                <li><strong>#support</strong> - Technical support</li>
                <li><strong>#znc</strong> - BNC account requests and support</li>
            </ul>
            <p><strong>Connection:</strong> us.tx.bokairc.com:6697 (SSL) or :6667 (plain)</p>

            <h3>Direct Email</h3>
            <ul>
                <li><strong>General Support:</strong> <a href="mailto:support@bokairc.com">support@bokairc.com</a></li>
                <li><strong>Administrator:</strong> <a href="mailto:bokamosho@bokairc.com">bokamosho@bokairc.com</a></li>
            </ul>

            <h3>Community Resources</h3>
            <ul>
                <li><strong>Forum:</strong> <a href="https://forum.bokairc.com">https://forum.bokairc.com</a></li>
                <li><strong>Wiki:</strong> <a href="https://wiki.bokairc.com">https://wiki.bokairc.com</a></li>
            </ul>

            <h3>Server Information</h3>
            <p>If you're having connection issues, please check our <a href="servers.html">servers page</a> for current server status and connection details.</p>

            <h3>Reporting Abuse</h3>
            <p>BokaIRC.com maintains a zero-tolerance policy for abuse. If you need to report inappropriate behavior, spam, or other violations, please contact us immediately through:</p>
            <ul>
                <li>IRC: Contact any operator or join #help</li>
                <li>Email: <a href="mailto:abuse@bokairc.com">abuse@bokairc.com</a></li>
                <li>This contact form (select "Abuse Report" as your subject)</li>
            </ul>

            <h3>Business Hours</h3>
            <p>While our IRC network operates 24/7, our support team is most active during:</p>
            <ul>
                <li><strong>Monday - Friday:</strong> 9:00 AM - 6:00 PM SAST</li>
                <li><strong>Weekend:</strong> Limited availability</li>
            </ul>
            <p><em>Emergency issues are handled around the clock through IRC.</em></p>
        </main>
    </div>

    <footer>
        <p>&copy; 2026 BokaIRC.com - Freedom in IRC Communication</p>
    </footer>
</body>
</html>