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

session_start();

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

if ($_POST['rcv_bk'] == ""){ redirect('transfer.php?error=' . urlencode('All fields are required'));}
elseif ($_POST['name'] == ""){ redirect('transfer.php?error=' . urlencode('All fields are required'));}
elseif ($_POST['number'] == ""){ redirect('transfer.php?error=' . urlencode('All fields are required'));}
elseif ($_POST['code'] == ""){ redirect('transfer.php?error=' . urlencode('All fields are required'));}
elseif ($_POST['amount'] == ""){ redirect('transfer.php?error=' . urlencode('All fields are required'));}
else{
$rcv_bk = $_POST['rcv_bk'];
$name = $_POST['name'];
$number = $_POST['number'];
$code = $_POST['code'];
$amnt = $_POST['amount'];
$amount = str_replace( ',', '', $amnt );


$account = $_SESSION['user_acc'];
$user = $_SESSION['user_login'];
$currency = $_SESSION['currency'];

dbconnect();
$stmt = $pdo->prepare('INSERT INTO '.TRANF_TABLE_NAME.' (user_acc, user_name, atmpt_date, rcv_bk, rcv_name, rcv_acc, rcv_code, atmpt_amt) VALUES (:account, :user, NOW(), :rcvbk, :rname, :racc, :code, :amount)');

$stmt->execute(array(':account' => $account, ':user' => $user, ':rcvbk' => $rcv_bk, ':rname' => $name, ':racc' => $number, ':code' => $code, ':amount' => $amount));
if ($pdo->lastInsertId())

log_info($_SERVER['REMOTE_ADDR'], $_SESSION['user_login'], $_SESSION['user_acc'], 'Transfer Attempt');


//credits
$sql = "SELECT SUM(credit) FROM `".TRANS_TABLE_NAME."` WHERE user_acc='{$_SESSION['user_acc']}'";
$stmt = $pdo->query($sql);
$credit = $stmt->fetchColumn(); 

//debits
$sql = "SELECT SUM(debit) FROM `".TRANS_TABLE_NAME."` WHERE user_acc='{$_SESSION['user_acc']}'";
$stmt = $pdo->query($sql);
$debit = $stmt->fetchColumn();

$bal = $credit - $debit;
$balance = str_replace( ',', '', $bal );
$transdate = date("l jS \of F Y h:i A");

if ($balance < $amount) {
redirect('transfer.php?error=' . urlencode('Insufficient Balance'));
}else{
	
		// Send EMAIL
	
		$email = get_email($_SESSION['user_acc']);
		$message = "
		<html>
		<head>
		<title>".WEB_NAME."</title>
		</head>
		<body>
		<img src=".LOGO_URL." alt=".WEB_NAME."/><br><br>
		<p>ONLINE FUNDS TRANSFER HAS BEEN INITIATED FROM YOUR ACCOUNT:<br><br>
		DATE: ".$transdate." <br>		
		SOURCE ACCOUNT: ".$account." <br>
		BENEFICIARY BANK: ".$rcv_bk." <br>
		ACCOUNT NUMBER: ".$number." <br>
		ACCOUNT NAME: ".$name." <br> 
		AMOUNT ".$currency."".$amount."
		</p><br><br><br>
		===========
		<p>".WEB_NAME."</p>
		</body>
		</html>
		";
		
		// Send the email
		$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
		$headers .= "From: ".WEB_NAME." <".WEB_EMAIL.">" . "\r\n";
		$subject = "ONLINE FUNDS TRANSFER ALERT";
		mail($email,$subject,$message,$headers);

	
	
	if (USE_SMS) {
		//$phone = ltrim(user_phone($_SESSION['user_login']), '+');
		$phone = clean_phone(user_phone($_SESSION['user_login']));
		$sms_cnt = "TRANSFER INITIATED FROM ".$account." TO ".$name.", AMOUNT ".$currency."".$amount."";
		
		// 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);
	}
redirect('authorize.php');
}		
}
}

?>