<?php
require_once('../config/config.php');
require_once('../foo/functions.php');
session_start();

if(isset($_POST['submit']))
{

if ($_POST['alrt_cnt'] == ""){ redirect('sms.php?error=' . urlencode('Your message cannot be empty'));}
elseif ($_POST['alrt_rcvr'] == ""){ redirect('sms.php?error=' . urlencode('Select SMS Receiver'));}
else{
dbconnect();
$sms_cnt = $_POST['alrt_cnt'];
$rcvr = $_POST['alrt_rcvr'];
//echo $_POST['alrt_rcvr'];
//exit();
$phone = clean_phone(user_phone($_POST['alrt_rcvr']));
$sms_rcvr = "$rcvr"." (+$phone)";
$sms_sndr = "Admin";




$stmt = $pdo->prepare('INSERT INTO '.SMS_TABLE_NAME.' (sms_cnt, sms_sender, sms_rcvr, sms_date) VALUES (:cnt, :sndr, :rcvr, NOW())');

$stmt->execute(array(':cnt' => $sms_cnt, ':sndr' => $sms_sndr, ':rcvr' => $sms_rcvr));
if ($pdo->lastInsertId()){
	
	if (USE_SMS) {
		
		// Send SMS
		$url="http://www.easy2text.com/index.php";

		$postfields = array(
			'app' => SMS_APP,
			'ta' => SMS_TA,
			'u' => SMS_USER,
			'p' => SMS_WORD,
			'to' => "$phone",
			'from' => SMS_SENDER,
			'msg' => "$sms_cnt"
		);

		if (!$curld = curl_init()) {
			exit;
		}

		curl_setopt($curld, CURLOPT_POST, true);
		curl_setopt($curld, CURLOPT_POSTFIELDS, $postfields);
		curl_setopt($curld, CURLOPT_URL,$url);
		curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);

		$output = curl_exec($curld);
		curl_close ($curld);
		
	}

log_info($_SERVER['REMOTE_ADDR'], $_SESSION['admin_login'], $sms_rcvr, 'SMS Alert Sent Successfully');
redirect('sms.php?msg=' . urlencode('SMS Alert Successfully Sent'));
}		
}
}

?>