<?php

class verifyController extends baseController{
    function index() {

		$userModel = new squareusersModel();
		$verifyModel = new verifyModel();
		
		$ver_code = $this->registry->args[1];
		
		// GET IP ADDRESS
		// https://stackoverflow.com/questions/15699101/get-the-client-ip-address-using-php
		$ipaddress = '';
		if (isset($_SERVER['HTTP_CLIENT_IP']))
			$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
		else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
			$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
		else if (isset($_SERVER['HTTP_X_FORWARDED']))
			$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
		else if (isset($_SERVER['HTTP_FORWARDED_FOR']))
			$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
		else if (isset($_SERVER['HTTP_FORWARDED']))
			$ipaddress = $_SERVER['HTTP_FORWARDED'];
		else if (isset($_SERVER['REMOTE_ADDR']))
			$ipaddress = $_SERVER['REMOTE_ADDR'];
		else
			$ipaddress = 'UNKNOWN';
		//return $ipaddress;
		
		//https://chattrboxx.com/verify/1/RlBsNc2?msg=success
		//https://chattrboxx.com/verify/2/GCi8naO?v
//		https://chattrboxx.com/verify/1/RlBsNc2?msg=success
//		https://flipmob.com/verify/JynP6Uk?msg=success

		if (!empty($ver_code)) {
			$db = Mysql::getInstance();
			//			$subscriber = $db->where("ver_code = ?", $code)
			//			->select_one("users", 'id');

			$verStatus = $db->where("ver_code = ?", $ver_code)
			->update("smssubs", [
				'verified' => 1,
				'ver_ip' => $ipaddress
			]);
			
			//echo $verStatus;
			if ($verStatus) {
				$_GET['msg'] = 1;
				//header("Location: /verify/".$ver_code."?msg=success");
				//exit;				
			} else {
				$_GET['error'] = 1;
			}
		}
		
		//$ver_code = $this->registry->args[2];
		
		//print_r($ver_code);
		
//		if (!$phone_info) {
//			header("Location: /auth/login");
//			exit;
//		}			
		
       //require 'views/verify/index.tpl.php';   
		require('views/verify/index.tpl.php');
    }
    
    // Maybe this was for the free SMS Text Message Numbers
    function indexBKP() {

		$userModel = new squareusersModel();
		$verifyModel = new verifyModel();
	
		$phone_id = null;
		$phone_info = null; 
		
		$phone_id = $this->registry->args[1];
		$ver_code = $this->registry->args[2];
		
		// GET IP ADDRESS
		// https://stackoverflow.com/questions/15699101/get-the-client-ip-address-using-php
		$ipaddress = '';
		if (isset($_SERVER['HTTP_CLIENT_IP']))
			$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
		else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
			$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
		else if (isset($_SERVER['HTTP_X_FORWARDED']))
			$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
		else if (isset($_SERVER['HTTP_FORWARDED_FOR']))
			$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
		else if (isset($_SERVER['HTTP_FORWARDED']))
			$ipaddress = $_SERVER['HTTP_FORWARDED'];
		else if (isset($_SERVER['REMOTE_ADDR']))
			$ipaddress = $_SERVER['REMOTE_ADDR'];
		else
			$ipaddress = 'UNKNOWN';
		//return $ipaddress;
		
		if (isset($phone_id) && isset($ver_code)) { 
			$check_phone_info = $verifyModel->get($phone_id); 
			if ($check_phone_info['ver_code']== $ver_code) {
				$phone_info = $verifyModel->get($phone_id);
			} else {
				$_GET['nomatch'] = 1;
			}
			//print_r($phone_info);
		} else {
			$_GET['ooops'] = 1;
		}
		
		//https://chattrboxx.com/verify/1/RlBsNc2?msg=success
		//https://chattrboxx.com/verify/2/GCi8naO?v
//		https://chattrboxx.com/verify/1/RlBsNc2?msg=success
//		https://flipmob.com/verify/1/RlBsNc2?v
		if (isset($_GET['v'])) {
			$db = Mysql::getInstance();
			//			$subscriber = $db->where("ver_code = ?", $code)
			//			->select_one("users", 'id');

			$db->where("id = ?", $phone_id)
			->update("phone_numbers", [
				'isVer' => 1,
				'ip_address' => $ipaddress
			]);

			//$this->$unsubscribeModel->unsubscribe($user_id);
			header("Location: /verify/".$phone_id."/".$ver_code."?msg=success"); //.$user_id
			exit;		
		}
		
		//$ver_code = $this->registry->args[2];
		
		//print_r($ver_code);
		
//		if (!$phone_info) {
//			header("Location: /auth/login");
//			exit;
//		}			
		
       //require 'views/verify/index.tpl.php';   
		require('views/verify/index.tpl.php');
    }
    
    // 
	function subscribe() {
			
		if ($_POST) {	
			
			//$subemail = !empty($_POST['email']) ? $_POST['email'] : false;
			
			$authModel = new authsqModel();
			$authModel->subscribe($_POST);		    	

			$subscriber_email = stripslashes(htmlentities($_POST['email']));  
			$db = Mysql::getInstance();
			$subs = $db->where("email = ?", $subscriber_email)
							->limit(1)
							->select_one("subscribers");
							 //$subscriber = $db->select_one("subscribers");
							 
			if (!empty($subs)) 
			$code = $subs['code'];
				
			$headers  = 'From: Pre-Order <contact@orderitfrom.com>' . "\r\n";
			$headers .= 'Reply-To: contact@orderitfrom.com' . "\r\n"; 
			$headers .= "MIME-Version: 1.0\r\n";
			$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";	 				
				
            $msg= "<center><img src='https://yb.orderitfrom.com/static/system/email_hdr.jpg' style='width:100%;' /></center>
                    <div style='text-align:left;'>
                    <br /><br />
                    Hello! You have successfully subscribed.
                    <br /><br />
                    Thank you for your support - we truly appreciate it.
                    <br /><br />
                    - SuperApps Team<br />
                    </div>"; 			
			//if(!empty($subemail)){
			     mail($subscriber_email, "You subscribed! Thanks!", $msg, $headers,"-fcontact@orderitfrom.com");    
			//} else {
			//     mail("preorderforsquare@gmail.com", "Your Pre-Order Equity Partner Code!", $msg, $headers,"-fcontact@getpreorder.com");
			//}
						
			
			//header('Location: /vip-success');
			exit;
		}		
		
		//mail("sillystats@gmail.com", "Subscriber Alert2!", "You have a new Subscriber!", "From: Pre-Order <contact@getpreorder.com>");
	
        //$redirect_url  = "/profile/myDashboard";
        //if (!empty($_GET['referer_url']))
        //    $redirect_url = $_GET['referer_url'];
		
		require 'views/content_pages/ssthx.tpl.php';
    }    
    
    function reserveNow(){
		$userModel = new userModel();
		if (isset($_POST['c'])) {
			$userModel->reserveAppWrapr($_SESSION['user']['id']);
			//echo 1;
			//			header("Location: /waitlist-tips");
			//			exit;
			//echo "Reserve...";
			//$userModel->reserveAppWrapr($_SESSION['user']['id']);

			//$ref = $_POST['ref'];
			//$ref = mysqli_real_escape_string($this->conn,$_POST['ref']);
			//			$db = Mysql::getInstance();
			//			$ref = stripslashes(htmlentities($refcode));
			//			$refid = $db->where("refcode = ?", $ref) // we also have ref in the table in the last column
			//			->select_one("users", 'id');
			//
			//			if ($refid > 0) {
			//				return $refid['id']; // Not Available
			//			}
		} else {
			echo 0;
		}	
    }
    
}
?>