<?php

 $request_dir = $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']);	

if(isset($_GET['refid'])){
	
$afi = mysqli_real_escape_string($conn, $_GET['refid']);
	
	}else{
	
	$afi = "system";	
		}



$aff = filter_var($afi, FILTER_SANITIZE_STRING);

$aff = str_replace( array( '+', ',', '-', '/', '(', ')',   '_', '//', 'drop', 'update', 'delete', 'insert', 'add',  '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $aff);
	
	
$qrysite = mysqli_query($conn,"SELECT * FROM settings");
$sql_logo = mysqli_fetch_array($qrysite);
$sitename =$sql_logo['sitename'];
$siturl = $_SERVER['SERVER_NAME'];

$qryaff = mysqli_query($conn,"SELECT * FROM users WHERE refid='$aff' ");
$getref = mysqli_fetch_array($qryaff);

if($aff === $getref['refid']){

$refby = " ".$getref['firstname']." ".$getref['lastname']." ";
$refbyid = $getref['refid'];

$invitecount = $getref['refcount'] + 1;

	
	}else{
		
	$refby = "System";	
	$refbyid = "System";
	$refcount = 1;
		}
							
if(isset($_POST['sub'])){
										


$email = mysqli_real_escape_string($conn, $_POST['email']);
$pass = mysqli_real_escape_string($conn, $_POST['pass']);
$phone = mysqli_real_escape_string($conn, $_POST['phone']);
$fname = mysqli_real_escape_string($conn, $_POST['fname']);
$lname = mysqli_real_escape_string($conn, $_POST['lname']);



$email = filter_var($email, FILTER_SANITIZE_STRING);
$phone = filter_var($phone, FILTER_SANITIZE_STRING);
$fname = filter_var($fname, FILTER_SANITIZE_STRING);
$lname = filter_var($lname, FILTER_SANITIZE_STRING);
$pass = filter_var($pass, FILTER_SANITIZE_STRING);


$email = str_replace( array( '+', ',', '-', '/', '(', ')',   '_', '//', 'drop', 'update', 'delete', 'insert', 'add',  '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $email);
$phone = str_replace( array( '+', ',', '-', '/', '(', ')',   '_', '//', 'drop', 'update', 'delete', 'insert', 'add',  '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $phone);
$fname = str_replace( array( '+', ',', '-', '/', '(', ')',   '_', '//', 'drop', 'update', 'delete', 'insert', 'add',  '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $fname);
$lname = str_replace( array( '+', ',', '-', '/', '(', ')',   '_', '//', 'drop', 'update', 'delete', 'insert', 'add',  '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $lname);
$pass = str_replace( array( '+', ',', '-', '/', '(', ')',   '_', '//', 'drop', 'update', 'delete', 'insert', 'add',  '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $pass);
	


$hash = password_hash($pass, PASSWORD_DEFAULT);
$uid = mt_rand(108367652312,398166660123);
$mypass = md5($pass);
										
$level = "free";
$block = "Block";
$activate = "Activate";
$status = "Pending";
$bal = 0;
$cur = "₦";
$log=$_SERVER['REMOTE_ADDR'];
$acctype = "normal";
$country = $_POST['country'];
$accno = substr(str_shuffle("12345678967891"), 0, 11);

$sms = mt_rand(1000,615628);
$ref = mt_rand(1000,100000);
								        

$refwallet = 0;


$pincredit = 0;

$blockpro = '0';

$refcount = 0;

$query_bank = mysqli_query($conn,"SELECT * FROM add_bank");
$bank = mysqli_fetch_array($query_bank);

$bankname1 = $bank['BankName'];
$accno1 = $bank['AccNo'];
$accname1 = $bank['AccName'];

$bankname2 = $bank['BankName2'];
$accno2 = $bank['AccNo2'];
$accname2 = $bank['AccName2'];

$bankname3 = $bank['BankName3'];
$accno3 = $bank['AccNo3'];
$accname3 = $bank['AccName3'];


$mt = substr(str_shuffle("0123456789678901"), 0, 12);
$otp = substr(str_shuffle("0123456789678901"), 0, 6);
$mgs = "You have not verified your Email Address yet.";


	
if(!empty($pass) && !empty($email) && !empty($phone) && !empty($fname) && !empty($lname)){
	

		
$reflink = 'https://'.$request_dir.'/register.php?refid=';
$activationlink = 'https://'.$request_dir.'/registration.php?refid='.$ref.'';
$refunverified = 0;
$refverified = 0;	
	
	
			
			
		// Validate reCAPTCHA box 
        if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){ 
            // Google reCAPTCHA API secret key 
            $secretKey = $capsec; 
             
            // Verify the reCAPTCHA response 
            $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secretKey.'&response='.$_POST['g-recaptcha-response']); 
             
            // Decode json data 
              $response = json_decode($verifyResponse); 
            

            
            $responseData = $response->success;
            //$hostname = $response->hostname;
            
           //echo $responseData;
			
		// If reCAPTCHA response is valid 
            if($responseData == true){ 
                
											
		$validphone = preg_replace("/[^0-9]/", '', $phone);
		
	
		if(strlen($validphone) == 11)  {									
									
								
	$check=mysqli_query($conn,"SELECT * FROM users WHERE email='$email' OR phone='$phone' ");
   
    $num = mysqli_num_rows($check);
    
    
    if ($num ==0){ //If there is already such username...

$sql = mysqli_query($conn,"INSERT into users(firstname,lastname,email,pass,password,phone,level,IPaddress,bal,acctype,status,country,accno,refid,refcount,refby,refwallet,refbyid,otp,credit,del,date) VALUES ('$fname','$lname','$email','$mypass','$hash','$validphone','$level','$log','$bal','$acctype','$status','$country','$accno','$ref','$refcount','$refby','$refwallet','$refbyid','$otp','$mt','$del','$date')") or die(mysqli_error());

 
$sql2 = mysqli_query($conn,"INSERT into earnings (username,email,transaction,commission,withdrawal,alltime,status,del,action,date,dateTime) VALUES('$fname $lname','$email','0','0','0','0','Active','$del','$action','$date','$dateTime')") or die(mysqli_error());



	
										
echo'<div class="alert alert-success">
		<button type="button" class="close" data-dismiss="alert">×</button>
		<strong>Account Created Successfully. <br>Please login to Continue</strong> 
<br>
		<div class="card-footer bg-white">
                <p> <a href="index.php" class="text-secondary">Click Here To Login.</a></p>
            </div>
									</div>';										

	$updaff = mysqli_query($conn,"UPDATE users SET refcount='$invitecount' WHERE refid='$aff' ");								
	
									
				
// send email

$from = "".$sql_logo['sitename']."<support@".$_SERVER['SERVER_NAME'].">"; //the email address from which this is sent
$to = "$email"; //the email address you're sending the message to
$subject = "Welcome to ".$sql_logo['sitename']." "; //the subject of the message

// To send HTML mail, the Content-type header must be set
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "Return-Path: ".$sql_logo['sitename']."<".$sql_logo['sitename'].">\r\n";
$headers .= "Organization: ".$sql_logo['sitename']."\r\n";
 
// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();

$message = "

<html>
<head>
<style>
.table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 50%;
}

td {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}


.button {
  background-color: #008CBA; /* Blue */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  border-radius: 8px;
  cursor: pointer;
  
  -webkit-transition-duration: 0.4s; /* Safari */
  transition-duration: 0.4s;
}

.button:hover {
  background-color: #4CAF50; /* Green */
  color: white;
}

</style>
</head>

<body>

<img src='https://".$request_dir."/sitelogo/".$sql_logo['sitelogo']."'>

<h3>Dear $fname,</h3>

We are glad to welcome you on Nigeria <br>
most trusted Instant & Automated digital recharge solution.<p> 

We have got lots of stuff to eliminate stress<br>
recharging your devices and in turn put some money <br>
in your wallet while doing so.<p> <br>

".$sql_logo['sitename']." give you instant and automated recharge for Airtime, <br>
Internet Data, Gotv, Dstv, Startimes, Electric Token, Bank Transfer, Bulk SMS <br>
Recharge Card Printing, Smile Internet, ETC<p> <br>


<strong>Your Registration details are as follows:</strong> <p>

<table class='table' >
  
  <tr>
    <td>Email:</td>
    <td>$email</td>
  </tr>

<tr>
    <td>Phone:</td>
    <td> $validphone</td>
  </tr>




  <tr>
    <td>Password:</td>
    <td> What You chosed</td>
  </tr>
  

  
</table> <p>



To Continue With Your registration process, kindly verify your Email address using the link below:<br><br>

<a href='https://".$request_dir."/forgot-password/emailverification.php?refid=".$mt."'><button class='button'>Click Here To Verify Your Email!</button></a> <br><br>

If the Above link is not working, the copy the code below into your browsing:<br><br>

https://".$request_dir."/forgot-password/emailverification.php?refid=".$mt."

<br>



<p>
Our support team is standby to assist you wherever you need help.<p>
 
 <strong>Email:</strong> ".$sql_logo['email']." 
<p>


Warm Regards, <p>


".$sql_logo['sitename']." <br>
".$sql_logo['mobile']."

</body><html>";

//now mail
$DoMail = mail($to,$subject,$message,$headers);
             	

$qryApi = mysqli_query($conn,"SELECT * FROM api_setting");
$apidata = mysqli_fetch_array($qryApi);
$api = $apidata['smartkey'];
$senderID = $apidata['smsUserid'];
$smsphone = $apidata['smsPhone'];

$query_rec = mysqli_query($conn,"SELECT * FROM billing");
$bil = mysqli_fetch_array($query_rec);
$bi = $bil['sms'];


$txt2 = "Dear $fname, Your Registration on $sitename was Successful.  
Code: $sms
Check Your Email for more details.
www.$siturl";

$message = utf8_encode($txt2);
$aa = urlencode($message);
$from = utf8_encode($senderID);
$sender = urlencode($from);

	   
	   
    
		
		
    }

else {
	
	
	
	echo '<div class="alert alert-danger">
		<button type="button" class="close" data-dismiss="alert">×</button>
		<strong>Account Already Created! </strong> 
<br>
		<div class="card-footer bg-white">
                <p> <a href="index.php" class="text-secondary">Click Here To Login.</a></p>
            </div>
									</div>';
	
}
			// check if phone is valid
			
		}
		else{echo '<div class="alert alert-danger">
		<button type="button" class="close" data-dismiss="alert">×</button>
		<strong>Enter a valid phone number</strong> 
									</div>'; }
									
									
									
            }else{echo '<div class="alert alert-danger">
	<button type="button" class="close" data-dismiss="alert">×</button>
		<strong>Robot verification failed, please try again.</strong> 
									</div>';}							
									
        }else{ echo '<div class="alert alert-danger">
		<button type="button" class="close" data-dismiss="alert">×</button>
		<strong>Please check on the reCAPTCHA box.</strong> 
									</div>';
		
            
        }
									
									
												
											}
												
											else{
												echo'<div class="alert alert-danger">
										<button type="button" class="close" data-dismiss="alert">×</button>
										<strong>Enter required field</strong>  
									</div>'; }
									
									mysqli_close($conn);
										}

?>