<?php

class unsubscribeController extends baseController{
    function index() {

		$userModel = new squareusersModel();
		$unsubscribeModel = new unsubscribeModel();
	
		$user_id = null;
		$subscriber_info = null; 
		
		if (isset( $this->registry->args[1])) {
			$user_id = $this->registry->args[1]; 
			
			$subscriber_info = $userModel->getSubscriber($user_id); 
		}
		
		if (! $subscriber_info) {
			header("Location: /auth/login");
			exit;
		}	
		
		// 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/unsubscribe/GSIVSpw
		
		// UNSUBSCRIBE TO EMAILS
		if (isset($_GET['unsubscribe'])) {
			//echo "Clicked unsubscribe...";
			
			$db = Mysql::getInstance();
//			$subscriber = $db->where("ver_code = ?", $code)
//			->select_one("users", 'id');
			
			if (empty($user_id))
				return false;

			$db->where("ver_code = ?", $user_id)
			->update("users", [
				'is_subscribe' => 0
				]);	
				
			$db->insert("unsubscribe", [
				'user_id' => $subscriber_info['id'],
				'type' => 'email',
				'ip_address' => $ipaddress,
				'date_created'  => date("Y-m-d H:i:s")]);		
			
			//$this->$unsubscribeModel->unsubscribe($user_id);
			header("Location: /unsubscribe/".$user_id."?msg=success"); //.$user_id
			exit;
		}
		
		// UNSUBSCRIBE TO TEXT MESSAGES
		if (isset($_GET['unsubtotext'])) {
			//echo "Clicked unsubscribe...";
			
			$db = Mysql::getInstance();
//			$subscriber = $db->where("ver_code = ?", $code)
//			->select_one("users", 'id');
			
			if (empty($user_id))
				return false;

			$db->where("ver_code = ?", $user_id)
			->update("users", [
				'is_text' => 0
				]);	
				
			$db->insert("unsubscribe", [
				'user_id' => $subscriber_info['id'],
				'type' => 'text',
				'ip_address' => $ipaddress,
				'date_created'  => date("Y-m-d H:i:s")]);		
			
			//$this->$unsubscribeModel->unsubscribe($user_id);
			header("Location: /unsubscribe/".$user_id."?msg=success"); //.$user_id
			exit;
		}		
		
        require 'views/unsubscribe/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';
    }    
    
}
?>