<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
global $LANG;
global $configverssion_id;
global $configapp_version;
global $server;

$query="SELECT * FROM sms_gateways WHERE type = '2way'";
$result = mysqli_query($server,$query) or die(mysqli_error($server));
while ($rows = mysqli_fetch_assoc($result)) { 
	$recipient_field = $rows['recipient_field'];
	$sender_field = $rows['sender_field'];
	$message_field = $rows['message_field'];
	
	if(isset($_REQUEST[$message_field])) {
		$to = mysqli_real_escape_string($server,$_REQUEST[$recipient_field]);
		$from = mysqli_real_escape_string($server,$_REQUEST[$sender_field]);
		$body = mysqli_real_escape_string($server,$_REQUEST[$message_field]);
		$date = date('Y-m-d H:i:s');
		if(!empty($to) && !empty($from)) {
			$customer_id = shortcodeOwner($to);
			//inser inbox
			mysqli_query($server,"INSERT INTO inbox (`sender`, `message`,`recipient`,`date`,`cost`, `customer_id`) 
         VALUES ('$from', '$body', '$to','$date', '0', '$customer_id')");
		    $inboxed_id = mysqli_insert_id($server);
			//check if this is sms chat
			if(inSMSChat($from)) {
				$datesc = time();
				mysqli_query($server, "INSERT INTO smschat (`customer_id`, `number`, `sender`, `receiver`, `message`, `date`, `seen`, `sent`)  VALUES ('$customer_id', '$from', '$from', '$to', '$body','$datesc', '0', '1');");
			}
		 
			$exiting = checkOptOutKey($body,$to);
			$joining = checkOptinKey($body,$to);
			if($exiting >0) {
				$customer_id = marketinglistData($exiting,'customer_id');
				mysqli_query($server,"UPDATE `inbox` SET `customer_id` = '$customer_id' WHERE  `id` = '$inboxed_id'");
				mysqli_query($server,"DELETE FROM marketingcontacts WHERE phone LIKE '%$from%' AND marketinglist = '$exiting'");
				if(!empty(marketinglistData($exiting,'optout_message'))) {
					$message = marketinglistData($exiting,'optout_message');
					$sender_id = marketinglistData($exiting,'default_sender_id');
					if(!empty($message)) {
						$message = strtr($message, array("{name}" => '', 
							"{phone}" => $from
						));	
					$country = getDestinationCountry($from);
					mysqli_query($server, "INSERT INTO sentmessages (`message`,`recipients`, `customer_id`, `type`, `media`, `date`, `sender_id`, `phonebook_id`, `marketinglist_id`, `duration`, `file`,`country`,`status`,`sent_from`, `gateway_id`, `ip`, `notice`, `to_count`) 
					VALUES ('$message','$from', '$customer_id', 'sms', '','".date('Y-m-d')."', '$sender_id','0','0', '0', '','$country', 'queued', 'portal', '0', '0.0.0.0', '', '1');") ;
					}
				}
			}
			if($joining >0) {
				$customer_id = marketinglistData($joining,'customer_id');
				mysqli_query($server,"UPDATE `inbox` SET `customer_id` = '$customer_id' WHERE `id` = '$inboxed_id'");
				if(contactExist($from,$joining,'marketing') < 1) {
					mysqli_query($server, "INSERT INTO marketingcontacts (`name`,`phone`, `marketinglist`) VALUES ('', '$from', '$joining');");
				}
				if(!empty(marketinglistData($joining,'optin_message'))) {
					$message = marketinglistData($joining,'optin_message');
					$sender_id = marketinglistData($joining,'default_sender_id');
					if(!empty($message)) {
						$message = strtr($message, array("{name}" => '', 
							"{phone}" => $from
						));	
					$country = getDestinationCountry($from);
					mysqli_query($server, "INSERT INTO sentmessages (`message`,`recipients`, `customer_id`, `type`, `media`, `date`, `sender_id`, `phonebook_id`, `marketinglist_id`, `duration`, `file`,`country`,`status`,`sent_from`, `gateway_id`, `ip`, `notice`, `to_count`) 
					VALUES ('$message','$from', '$customer_id', 'sms', '','".date('Y-m-d')."', '$sender_id','0','0', '0', '','$country', 'queued', 'portal', '0', '0.0.0.0', '', '1');") ;
					}
				}
			}
		}
	}	
}
