<?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])) {
		$hook_to = $to = mysqli_real_escape_string($server,$_REQUEST[$recipient_field]);
		$hook_from = $from = mysqli_real_escape_string($server,$_REQUEST[$sender_field]);
		$hook_body = $body = mysqli_real_escape_string($server,$_REQUEST[$message_field]);
		$hook_date = $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'");
				$queryML="SELECT * FROM marketingcontacts WHERE phone LIKE '%$from%' AND marketinglist = '$exiting' LIMIT 1";
				$resultML = mysqli_query($server,$queryML);
				$rowsML = mysqli_fetch_assoc($resultML);
				$name = $rowsML['name'];
				
				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}" => $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) {
					$name = "";
					mysqli_query($server, "INSERT INTO marketingcontacts (`name`,`phone`, `marketinglist`) VALUES ('$name', '$from', '$joining');");
				} else {
					$queryML="SELECT * FROM marketingcontacts WHERE phone LIKE '%$from%' AND marketinglist = '$exiting' LIMIT 1";
					$resultML = mysqli_query($server,$queryML);
					$rowsML = mysqli_fetch_assoc($resultML);
					$name = $rowsML['name'];	
				}
				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}" => $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');") ;
					}
				}
			}
			//call hooks
			$vars = array("from"=>$hook_from, 
						 "to"=>$hook_to,
						 "message"=>$hook_body, 
						 "customer"=>$customer_id
					 );
			$_SESSION['EventVals'] = $vars;
			global $hooks; $hooks->do_action('OnIncomingMessage');
		} //end if empty to...
	}	
}
$vars = array();
$_SESSION['EventVals'] = $vars;
global $hooks; $hooks->do_action('CustomIncomingMessage');