<?php

global $connection;
        $connection = mysql_connect("localhost", "webstagi_optoutu", "Chang3M3") or die(mysql_error());
        mysql_select_db("webstagi_optout");
	
if($_POST)
{
require('constant.php');
    
   $user_senderid     = filter_var($_POST["senderid"], FILTER_SANITIZE_EMAIL);
   $user_phone     = filter_var($_POST["phone"], FILTER_SANITIZE_STRING);
	date_default_timezone_set('Asia/Dubai');
    $udate = date("Y-m-d H:i:s");
	
		
    if(empty($user_phone)) {
		$empty[] = "<b>Mobile Number</b>";		
	}else if(!empty($user_phone)){
		if(!preg_match("/^\d{12}$/",$user_phone)){
			$empty[] = "<b>Valid Mobile Number</b>";
			$success = 0;
		}
	}
	
	if(!empty($user_senderid)){
		if(!preg_match("/^[ A-Za-z0-9&-]*$/",$user_senderid)){
			$empty[] = "<b>Valid SenderID</b>";
			$success = 0;
		}
	}
		
	
	if(!empty($empty)) {
		$output = json_encode(array('type'=>'error', 'text' => implode(", ",$empty) . ' Required!'));
        die($output);
	}
	
	
	
	////reCAPTCHA validation
//	if (isset($_POST['g-recaptcha-response'])) {
//		
//		require('component/recaptcha/src/autoload.php');		
//		
//		$recaptcha = new \ReCaptcha\ReCaptcha(SECRET_KEY, new \ReCaptcha\RequestMethod\SocketPost());
//
//		$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
//
//		  if (!$resp->isSuccess()) {
//				$output = json_encode(array('type'=>'error', 'text' => '<b>Captcha</b> Validation Required!'));
//				die($output);				
//		  }	
//	}
	
	if($success !== 0){
	$sql="INSERT INTO unsubscribe_optout(mobile,senderid,udate) values('$user_phone','$user_senderid','$udate')";
    $result=mysql_query($sql);
	}

	if($result)	{
	
	$to = 'support@vectramind.com,satish.penjarla@vectramind.com,venkat@vectramind.com,murthy.dirisala@vectramind.com';
	//$to = 'murthy.dirisala@vectramind.com';
    $subject = 'UnSubscribe';
    $from = $user_phone;
	$headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	$headers .= 'From: '.$from."\r\n".
	'Reply-To: '.$from."\r\n" .
		'X-Mailer: PHP/' . phpversion();
		
	$message = '<html><body>';
	$message .= '<h1 style="color:#f40;">UnSubscribe!</h1>';
	$message .= '<p style="color:#080;font-size:18px;">Phone: '.$user_phone.' </p>';
	$message .= '<p style="color:#080;font-size:18px;">Sender ID:  '.$user_senderid.' </p>';
	$message .= '</body></html>';
	

	if(mail($to, $subject, $message, $headers)){
	    $output = json_encode(array('type'=>'message', 'text' => 'Unsubscribed Successfully'));
	    die($output);
	} else {
	    $output = json_encode(array('type'=>'error', 'text' => 'Unable to send email, please contact'.SENDER_EMAIL));
	    die($output);
	}
	
	}
}
?>