
<?php
header('Content-Type: text/html'); 
include('connection.php');
require "twilio/Services/Twilio.php";
require 'vendors/autoload.php';

// Load our `.env` variables
$dotenv = Dotenv\Dotenv::create(__DIR__);
$dotenv->load();

date_default_timezone_set('America/Los_Angeles');
$new222 = date('Y-m-d H:i:s');
$date_time = date('Y-m-d H:i:s');
$from= $_REQUEST['From'];
$to= $_REQUEST['To'];
$body1= $_REQUEST['Body'];
$body12= strtolower($_REQUEST['Body']);
$from=substr($from,1);
$to=substr($to,1);
$mediaUrl ='';
$mediaUrl = $_REQUEST['MediaUrl0'];


$json = json_decode(file_get_contents("php://input"), true);
$ccccc=print_r($json);
$record_type = count($json);
$record_type=implode(" ",$json[0]);
$record_type = $json['data']['payload'];
$record_type222 = $json['data']['event_type'];


$fromp=$record_type['from'];
//print_r($fromp[phone_number]);
$fromhis=$fromp['phone_number'];

$top=$record_type['to'];
//print_r($top[phone_number]);

$tohis=$top[0]['phone_number'];
$to1=$to;
$fromhis=substr($fromhis,1);
$tohis=substr($tohis,1);


$bodyhis=$record_type['text'];


$body1 = mysqli_real_escape_string($con,$body1);
if($body12 == 'stop' || $body12 == 'Stop')
{
	$query = mysqli_query($con,"insert into tapp_blacklist(number,keyword,datetime) values('".$from."','stop','".$new222."')");
}
if($body12 == 'start' || $body12 == 'Start')
{
	$query = mysqli_query($con,"delete from tapp_blacklist where number ='".$from."'");
}

$sqlrec4 = mysqli_query($con,"insert into tapp_msg_receive (sender,body,date_time,read_status,msg_read,twilio_num,mediaUrl) values('".$fromhis."','".$bodyhis."','".$date_time."','N','0','".$tohis."','".$mediaUrl."')");
$sqlrec5 = mysqli_query($con,"insert into table_data (sender,body,date_time,sending_status) values('".$fromhis."','".$bodyhis."','".$new222."','R')");


	$query = mysqli_query($con,"select * from tapp_twilio_number  where number like '%".$to."'") ;


$get_keyword = mysqli_query($con,"select * from tapp_unsub_keywords")or die(mysqli_error($con));
while($row = mysqli_fetch_array($query) )
	{
	 $sid=$row['twilio_sid'];
	$token=$row['twilio_token'];
	 $service_type=$row['service_type'];

	}
		 	
	$check_blacklist = mysqli_query($con,"select * from tapp_blacklist where number like '%".$from."%'");
	if(mysqli_num_rows($check_blacklist)<1)

	{
while($rowdata = mysqli_fetch_array($get_keyword))
{

if (strpos($body12, strtolower($rowdata['keyword'])) !== false) {
    $message = $rowdata['message'];
	$AccountSid = $sid;

 $AuthToken = $token;
$num=$from;

$twilio_number=$to;

 $mes=$body1;



$message= mysqli_real_escape_string($con, stripslashes($message));
$msg=$message;
 $num1='+'.$num;
$client = new Services_Twilio(getenv('sid'), getenv('token'));

// $mediaUrl ='';

	try
	{
		$sms = $client->account->messages->sendMessage($twilio_number,$num1,$msg);

		 $sms->sid;
		$sql1 = mysqli_query($con,"INSERT INTO tapp_sent_msg_log(sms_number,twilio_num,message,images,bulk_name,date_time) VALUES ('".$num."','".$twilio_number."','".$message."','".$mediaUrl."','".$unique_id."','".$new222."' )");
		$sqlrec5 = mysqli_query($con,"insert into table_data (sender,body,date_time,sending_status) values('".$num."','".$msg."','".$new222."','S')");
		$_SESSION['flash_message'] = 'Message has been sent to '.$num1;
	}
	catch (Exception $e)
	{
	 $e->getMessage();
		$sql1 = mysqli_query($con,"INSERT INTO tapp_sent_msg_failed(sms_number,twilio_num,message,images,'bulk_name',date_time) VALUES ('".$num."','".$twilio_number."','".$message."','".$mediaUrl."','".$unique_id."','".$new222."' )");
$_SESSION['failure_message'] = "Sorry! SMS couldn't sent to ".$num1;
	}
}	
}

	}
	if(isset($_REQUEST['MediaUrl0']))

{
    
    ?>
    <Response>
    <Message to="+18042779410"><Body> <?=htmlspecialchars(substr($_REQUEST['From'] . ": " . $_REQUEST['Body'], 0, 1600))?></Body><Media><?=$_REQUEST['MediaUrl0']?> </Media></Message>
</Response>
    <?php 
    
}
else
{?>
 <Response>
    <Message to="+18042779410"> <?=htmlspecialchars(substr($_REQUEST['From'] . ": " . $_REQUEST['Body'], 0, 1600))?> </Message>
</Response>

    <?php 
}?>



