Viewing File: /home/quiczmwg/bitmaven.org/authenticator/signin-process.php

<?php
// Include your database connection file
@session_start();
require_once("../_db.php");

// Google reCAPTCHA secret key
$recaptchaSecretKey = "6LcS6esqAAAAAPtVu8FiRaZs6dTLJaIofjzg-XXH"; // Replace with your actual secret key

// Function to send email notification
function sendEmailNotification($email, $full_name, $userid, $subject) {
    $message = "<html>
    <head>
        <style>
            .container {
                font-family: Arial, sans-serif;
                background-color: #f4f4f4;
                padding: 20px;
                border-radius: 5px;
            }
            .green-button {
                display: inline-block;
                background-color: #4CAF50;
                color: white;
                padding: 10px 20px;
                text-align: center;
                text-decoration: none;
                font-size: 16px;
                border-radius: 5px;
                border: none;
                cursor: pointer;
            }
            .green-button:hover {
                background-color: #45a049;
            }
        </style>
    </head>
    <body>
        <div class='container'>
            <center>
                <img src='https://bitmaven.org/img/logo.png' alt='logo'/>
            </center>
            <h3>Dear, $full_name</h3>
            <p>Welcome to Bitmaven! Please verify your account:</p>
            <a href='https://bitmaven.org/verify.php?userid=$userid' class='green-button'>Verify Account</a>
            <p>Thank you for joining us!</p>
        </div>
    </body>
    </html>";

    // Set email headers
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=utf-8\r\n";
    $headers .= 'Reply-To: support@bitmaven.org' . "\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion();

    return mail($email, $subject, $message, $headers);
}

// Validate user input
function validateInput($full_name, $email, $username, $password, $cpassword) {
    $errors = [];

    if (empty($full_name) || empty($email) || empty($username) || empty($password) || empty($cpassword)) {
        $errors[] = 'Enter all required fields.';
    } elseif (strlen($password) < 7) {
        $errors[] = 'Password should be at least 7 characters long.';
    } elseif ($password !== $cpassword) {
        $errors[] = 'Passwords do not match.';
    }

    return $errors;
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $full_name = $_POST['full_name'];
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $cpassword = $_POST['cpassword'];
    $recaptchaResponse = $_POST['g-recaptcha-response'];

    // Validate reCAPTCHA
    $recaptchaUrl = "https://www.google.com/recaptcha/api/siteverify";
    $recaptchaData = [
        'secret' => $recaptchaSecretKey,
        'response' => $recaptchaResponse
    ];
    $recaptchaOptions = [
        'http' => [
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($recaptchaData)
        ]
    ];
    $recaptchaContext = stream_context_create($recaptchaOptions);
    $recaptchaVerify = file_get_contents($recaptchaUrl, false, $recaptchaContext);
    $recaptchaResult = json_decode($recaptchaVerify, true);

    if (!$recaptchaResult['success']) {
        header('Location: ../signin.php?status=error&message=Invalid reCAPTCHA. Please try again.');
        exit();
    }

    // Validate input fields
    $errors = validateInput($full_name, $email, $username, $password, $cpassword);
    if (!empty($errors)) {
        header('Location: ../signin.php?status=error&message=' . urlencode(implode(' ', $errors)));
        exit();
    }

    // Check if email already exists
    $checkEmailQuery = "SELECT * FROM user_login WHERE email = ?";
    $checkEmailStmt = $conn->prepare($checkEmailQuery);
    if ($checkEmailStmt) {
        $checkEmailStmt->bind_param("s", $email);
        $checkEmailStmt->execute();
        $checkEmailResult = $checkEmailStmt->get_result();
        $checkEmailStmt->close();

        if ($checkEmailResult->num_rows > 0) {
            header('Location: ../signin.php?status=error&message=User email already exists.');
            exit();
        }

        // Generate unique user ID
        $userid = generateUserId();
        $hashed_password = password_hash($password, PASSWORD_DEFAULT);
        $profit_balance = 0.00;

        // Insert user data
        $insertQuery = "INSERT INTO user_login (userid, full_name, email, username, password, profit_balance) VALUES (?, ?, ?, ?, ?, ?)";
        $insertStmt = $conn->prepare($insertQuery);

        if ($insertStmt) {
            $insertStmt->bind_param("sssssd", $userid, $full_name, $email, $username, $hashed_password, $profit_balance);
            $insertStmt->execute();

            if ($insertStmt->error) {
                header('Location: ../signin.php?status=error&message=Database error: ' . $insertStmt->error);
                exit();
            }

            // Send email verification
            $subject = "Welcome to Bitmaven";
            sendEmailNotification($email, $full_name, $userid, $subject);

            header('Location: ../signin.php?status=success&message=Registration successful. Verify your email.');
            exit();
        } else {
            header('Location: ../signin.php?status=error&message=Database error: ' . $conn->error);
            exit();
        }
    } else {
        header('Location: ../signin.php?status=error&message=Database error: ' . $conn->error);
        exit();
    }
} else {
    header('Location: ../signin.php?status=error&message=Invalid request.');
    exit();
}

// Function to generate a unique user ID
function generateUserId() {
    return 'OPM' . mt_rand(100000, 999999);
}
?>
Back to Directory File Manager
<