<?php
declare(strict_types=1);
/**
 * AzerothCore WoW registration — direct INSERT with SRP6 salt/verifier (GMP).
 * Mount /private/config.php with $dbHost, $dbPort, $dbName, $dbUser, $dbPass.
 */

function h($v): string
{
    return htmlspecialchars((string) ($v ?? ''), ENT_QUOTES, 'UTF-8');
}

$configPath = '/private/config.php';
if (!is_file($configPath)) {
    http_response_code(500);
    exit('Registration configuration missing.');
}
require_once $configPath;

function wow_calculate_srp6_verifier(string $usernameUpper, string $passwordUpper, string $saltBinary): string
{
    if (!function_exists('gmp_init')) {
        throw new RuntimeException('PHP GMP extension required for SRP6.');
    }
    $g = gmp_init(7);
    $N = gmp_init('894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7', 16);
    $h1 = sha1(strtoupper($usernameUpper . ':' . $passwordUpper), true);
    $h2 = sha1($saltBinary . $h1, true);
    $h2i = gmp_import($h2, 1, GMP_LSW_FIRST);
    $verifier = gmp_powm($g, $h2i, $N);
    $out = gmp_export($verifier, 1, GMP_LSW_FIRST);

    return str_pad($out, 32, "\0", STR_PAD_RIGHT);
}

function wow_db(): ?mysqli
{
    global $dbHost, $dbPort, $dbName, $dbUser, $dbPass;
    $mysqli = @new mysqli($dbHost, $dbUser, $dbPass, $dbName, (int) $dbPort);
    if ($mysqli->connect_errno) {
        return null;
    }

    return $mysqli;
}

$username = '';
$error = '';
$success = false;
$successMessage = '';
$sqlProof = '';
$verifyCount = '';
$commandProof = '';
$debugOutput = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = strtoupper(trim($_POST['username'] ?? ''));
    $passwordRaw = (string) ($_POST['password'] ?? '');
    $password = strtoupper(trim($passwordRaw));

    if ($username === '' || $passwordRaw === '') {
        $error = 'Username and password are required.';
    } elseif (!preg_match('/^[A-Z0-9_]{3,16}$/', $username)) {
        $error = 'Username must be 3-16 characters and use only letters, numbers, or underscore.';
    } elseif (strlen($password) < 6 || strlen($password) > 20) {
        $error = 'Password must be between 6 and 20 characters.';
    } else {
        $sqlProof = 'SELECT id FROM account WHERE username = ? (bound: ' . $username . ')';
        $mysqli = wow_db();
        if (!$mysqli) {
            $error = 'Could not connect to the auth database.';
        } else {
            $stmt = $mysqli->prepare('SELECT id FROM account WHERE username = ? LIMIT 1');
            if (!$stmt) {
                $error = 'Database error (prepare).';
                $debugOutput = $mysqli->error;
                $mysqli->close();
            } else {
                $stmt->bind_param('s', $username);
                $stmt->execute();
                $res = $stmt->get_result();
                $exists = $res && $res->num_rows > 0;
                $stmt->close();

                if ($exists) {
                    $error = 'That account name is already in use.';
                    $verifyCount = '1';
                    $mysqli->close();
                } else {
                    $salt = null;
                    $verifier = null;
                    try {
                        $salt = random_bytes(32);
                        $verifier = wow_calculate_srp6_verifier($username, $password, $salt);
                    } catch (Throwable $e) {
                        $error = 'Could not compute secure credentials.';
                        $debugOutput = $e->getMessage();
                    }

                    if ($error === '' && $salt !== null && $verifier !== null) {
                        $email = $username . '@players.local';
                        $regMail = $email;
                        $expansion = 2;
                        $ins = $mysqli->prepare(
                            'INSERT INTO account (username, salt, verifier, email, reg_mail, expansion, joindate) VALUES (?, ?, ?, ?, ?, ?, NOW())'
                        );
                        if (!$ins) {
                            $error = 'Database error (insert prepare).';
                            $debugOutput = $mysqli->error;
                        } else {
                            $ins->bind_param('sssssi', $username, $salt, $verifier, $email, $regMail, $expansion);
                            if (!$ins->execute()) {
                                $error = 'Account creation failed.';
                                $debugOutput = $ins->error ?: $mysqli->error;
                            } else {
                                $chk = $mysqli->prepare('SELECT COUNT(*) AS c FROM account WHERE username = ?');
                                $cnt = 0;
                                if ($chk) {
                                    $chk->bind_param('s', $username);
                                    $chk->execute();
                                    $row = $chk->get_result()->fetch_assoc();
                                    $cnt = (int) ($row['c'] ?? 0);
                                    $chk->close();
                                }
                                $verifyCount = (string) $cnt;
                                if ($cnt > 0) {
                                    $success = true;
                                    $successMessage = 'Account created successfully. Your account now exists in the auth database.';
                                    $commandProof = 'SRP6 direct insert (salt + verifier) into acore_auth.account';
                                } else {
                                    $error = 'Insert completed but verification query did not find the new account.';
                                }
                            }
                            $ins->close();
                        }
                    }
                    $mysqli->close();
                }
            }
        }
    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create WoW Account | Core Game Hosting</title>
    <style>
        * { box-sizing: border-box; }

        html, body {
            margin: 0;
            padding: 0;
            min-height: 100%;
            font-family: Arial, Helvetica, sans-serif;
            background: #020814;
            color: #e8f3ff;
        }

        body {
            background:
                linear-gradient(rgba(1, 6, 16, 0.55), rgba(1, 6, 16, 0.88)),
                radial-gradient(circle at 20% 20%, rgba(111, 196, 255, 0.12), transparent 24%),
                radial-gradient(circle at 80% 18%, rgba(95, 160, 255, 0.10), transparent 20%),
                url('assets/lich-king.jpg') center top / cover no-repeat fixed;
        }

        .page-shell {
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .topbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            gap: 20px;
            padding: 20px 32px;
            background: rgba(2, 10, 24, 0.72);
            border-bottom: 1px solid rgba(90, 150, 255, 0.16);
            backdrop-filter: blur(10px);
        }

        .brand {
            color: #8cc8ff;
            text-decoration: none;
            font-weight: 900;
            font-size: 24px;
        }

        .nav {
            display: flex;
            gap: 16px;
            flex-wrap: wrap;
        }

        .nav a {
            color: #dcecff;
            text-decoration: none;
            font-weight: 700;
            font-size: 15px;
        }

        .nav a:hover {
            color: #7fc4ff;
        }

        .main {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 48px 20px;
        }

        .panel {
            width: 100%;
            max-width: 1180px;
            display: grid;
            grid-template-columns: 1.05fr 0.95fr;
            gap: 28px;
            align-items: stretch;
        }

        .info-card,
        .form-card,
        .success-card {
            background: rgba(6, 16, 32, 0.84);
            border: 1px solid rgba(87, 160, 255, 0.18);
            border-radius: 22px;
            box-shadow:
                0 24px 60px rgba(0, 0, 0, 0.46),
                inset 0 1px 0 rgba(255,255,255,0.03);
            backdrop-filter: blur(14px);
        }

        .info-card {
            padding: 34px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            min-height: 560px;
        }

        .eyebrow {
            display: inline-block;
            margin-bottom: 16px;
            padding: 8px 14px;
            border-radius: 999px;
            background: rgba(140, 215, 255, 0.10);
            border: 1px solid rgba(140, 215, 255, 0.22);
            color: #d8efff;
            font-size: 12px;
            font-weight: 800;
            letter-spacing: 0.16em;
            text-transform: uppercase;
        }

        h1, h2 {
            margin: 0 0 14px;
            line-height: 1.02;
            letter-spacing: -0.03em;
        }

        h1 {
            font-size: clamp(42px, 6vw, 72px);
            color: #f5f9ff;
        }

        h2 {
            font-size: clamp(30px, 4vw, 44px);
            color: #f5f9ff;
        }

        .lead {
            margin: 0 0 24px;
            color: #c4d9ee;
            font-size: 19px;
            line-height: 1.7;
            max-width: 760px;
        }

        .feature-grid {
            display: grid;
            grid-template-columns: repeat(2, minmax(0, 1fr));
            gap: 14px;
            margin-top: 24px;
        }

        .feature {
            padding: 16px;
            border-radius: 16px;
            background: rgba(3, 11, 22, 0.72);
            border: 1px solid rgba(72, 130, 220, 0.14);
        }

        .feature .label {
            display: block;
            margin-bottom: 8px;
            color: #7fbfff;
            font-size: 12px;
            font-weight: 800;
            letter-spacing: 0.16em;
            text-transform: uppercase;
        }

        .feature .value {
            color: #ffffff;
            font-size: 18px;
            font-weight: 800;
            line-height: 1.3;
        }

        .form-card,
        .success-card {
            padding: 30px;
        }

        .small-note {
            margin: 12px 0 0;
            color: #9db8d5;
            font-size: 14px;
            line-height: 1.6;
        }

        .alert {
            margin: 0 0 20px;
            padding: 14px 16px;
            border-radius: 14px;
            font-weight: 700;
            line-height: 1.5;
        }

        .alert-error {
            background: rgba(160, 24, 36, 0.18);
            border: 1px solid rgba(255, 110, 110, 0.28);
            color: #ffb1b1;
        }

        .alert-success {
            background: rgba(24, 120, 72, 0.18);
            border: 1px solid rgba(100, 220, 150, 0.28);
            color: #b8ffd2;
        }

        .form-grid {
            display: grid;
            gap: 16px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #cfe3f8;
            font-size: 14px;
            font-weight: 700;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            min-height: 52px;
            padding: 0 14px;
            border-radius: 12px;
            border: 1px solid rgba(87, 160, 255, 0.16);
            background: rgba(3, 10, 20, 0.94);
            color: #ffffff;
            font-size: 16px;
            outline: none;
        }

        input[type="text"]:focus,
        input[type="password"]:focus {
            border-color: rgba(104, 181, 255, 0.48);
            box-shadow: 0 0 0 3px rgba(0, 120, 255, 0.12);
        }

        .btn-row {
            display: flex;
            flex-wrap: wrap;
            gap: 14px;
            margin-top: 16px;
        }

        .btn,
        button {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            min-height: 52px;
            padding: 0 22px;
            border-radius: 14px;
            border: none;
            cursor: pointer;
            text-decoration: none;
            font-size: 17px;
            font-weight: 800;
            transition: transform 0.18s ease, box-shadow 0.18s ease;
        }

        .btn-primary,
        button {
            color: #fff;
            background: linear-gradient(180deg, #258fff 0%, #0d5fd0 100%);
            box-shadow: 0 14px 28px rgba(0, 95, 210, 0.28);
        }

        .btn-primary:hover,
        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 16px 34px rgba(0, 115, 255, 0.36);
        }

        .btn-secondary {
            color: #e8f3ff;
            background: rgba(10, 20, 36, 0.86);
            border: 1px solid rgba(87, 160, 255, 0.18);
        }

        .proof-box {
            margin-top: 20px;
            padding: 16px;
            border-radius: 14px;
            background: rgba(2, 10, 18, 0.94);
            border: 1px solid rgba(87, 160, 255, 0.14);
        }

        .proof-box h3 {
            margin: 0 0 10px;
            color: #f2f7ff;
            font-size: 16px;
        }

        .proof-box code,
        .proof-box pre {
            white-space: pre-wrap;
            word-break: break-word;
            color: #9fd1ff;
            font-family: Consolas, Monaco, monospace;
            font-size: 13px;
            line-height: 1.6;
            margin: 0;
        }

        .success-list {
            margin: 18px 0 0;
            padding-left: 18px;
            color: #c9def2;
            line-height: 1.8;
        }

        .footer-note {
            margin-top: 18px;
            color: #92afd0;
            font-size: 13px;
            line-height: 1.6;
        }

        @media (max-width: 980px) {
            .panel {
                grid-template-columns: 1fr;
            }

            .topbar {
                flex-direction: column;
                align-items: flex-start;
                padding: 18px;
            }
        }

        @media (max-width: 640px) {
            .main {
                padding: 22px 14px;
            }

            .info-card,
            .form-card,
            .success-card {
                padding: 22px 18px;
                min-height: auto;
            }

            .feature-grid {
                grid-template-columns: 1fr;
            }

            .btn-row {
                flex-direction: column;
            }

            .btn,
            button {
                width: 100%;
            }
        }
    </style>
</head>
<body>
<div class="page-shell">
    <header class="topbar">
        <a class="brand" href="https://coregamehosting.com">Core Game Hosting</a>
        <nav class="nav">
            <a href="https://coregamehosting.com">Home</a>
            <a href="https://www.coregamehosting.com/games/wow/index.html">WoW</a>
            <a href="https://www.coregamehosting.com/games/wokf/index.html">WoKF</a>
            <a href="https://www.coregamehosting.com/services/index.html">Minecraft</a>
            <a href="https://www.coregamehosting.com/support/donate.html">Donate</a>
        </nav>
    </header>

    <main class="main">
        <div class="panel">
            <section class="info-card">
                <div>
                    <div class="eyebrow">Wrath of the Lich King</div>
                    <h1>Create your WoW account</h1>
                    <p class="lead">
                        Make your account, verify it properly, then move straight into the client and setup flow.
                        No blank dead-end message, no guessing whether it actually worked.
                    </p>

                    <div class="feature-grid">
                        <div class="feature">
                            <span class="label">Version</span>
                            <span class="value">WoTLK 3.3.5a</span>
                        </div>
                        <div class="feature">
                            <span class="label">Status</span>
                            <span class="value">Playable</span>
                        </div>
                        <div class="feature">
                            <span class="label">Account Flow</span>
                            <span class="value">Create + Verify</span>
                        </div>
                        <div class="feature">
                            <span class="label">Next Step</span>
                            <span class="value">Download + Realmlist</span>
                        </div>
                    </div>
                </div>

                <p class="footer-note">
                    Free to play. Donations are optional and only help cover platform costs.
                </p>
            </section>

            <?php if ($success): ?>
                <section class="success-card">
                    <div class="eyebrow">Success</div>
                    <h2>Account created</h2>

                    <div class="alert alert-success"><?= h($successMessage) ?></div>

                    <ul class="success-list">
                        <li><strong>Username:</strong> <?= h($username) ?></li>
                        <li><strong>Database verification count:</strong> <?= h($verifyCount) ?></li>
                        <li><strong>Status:</strong> Account found after creation check</li>
                    </ul>

                    <div class="proof-box">
                        <h3>SQL verification used</h3>
                        <code><?= h($sqlProof) ?></code>
                    </div>

                    <div class="proof-box">
                        <h3>Registration method</h3>
                        <code><?= h($commandProof) ?></code>
                    </div>

                    <div class="btn-row">
                        <a class="btn btn-primary" href="https://www.coregamehosting.com/downloads/index.html">Download Client</a>
                        <a class="btn btn-secondary" href="https://www.coregamehosting.com/games/wow/index.html">Back to WoW Page</a>
                    </div>
                </section>
            <?php else: ?>
                <section class="form-card">
                    <div class="eyebrow">Create WoW Account</div>
                    <h2>Account creation</h2>

                    <?php if ($error): ?>
                        <div class="alert alert-error"><?= h($error) ?></div>
                    <?php endif; ?>

                    <form method="post" class="form-grid">
                        <div>
                            <label for="username">Username</label>
                            <input
                                id="username"
                                name="username"
                                type="text"
                                maxlength="16"
                                required
                                value="<?= h($username) ?>"
                                placeholder="Enter username"
                            >
                        </div>

                        <div>
                            <label for="password">Password</label>
                            <input
                                id="password"
                                name="password"
                                type="password"
                                minlength="6"
                                maxlength="20"
                                required
                                placeholder="Enter password"
                            >
                        </div>

                        <button type="submit">Create Account</button>
                    </form>

                    <p class="small-note">
                        Username format: 3-16 characters, letters / numbers / underscore only.
                        Password: 6-20 characters.
                    </p>

                    <?php if ($sqlProof): ?>
                        <div class="proof-box">
                            <h3>SQL check used</h3>
                            <code><?= h($sqlProof) ?></code>
                        </div>
                    <?php endif; ?>

                    <?php if ($debugOutput): ?>
                        <div class="proof-box">
                            <h3>Debug output</h3>
                            <pre><?= h($debugOutput) ?></pre>
                        </div>
                    <?php endif; ?>
                </section>
            <?php endif; ?>
        </div>
    </main>
</div>
</body>
</html>
