<?php require_once('../db.php'); ?>
<?php 
	/*$_POST["msisdn"] = "38891111111";
	$_POST["text"] = "neconcat poruke";
	$_POST["concat"] = "false";
	$_POST["concat-ref"] = "1";
	$_POST["concat-total"] = "2";
	$_POST["concat-part"] = "2";
	$_POST["api-key"] = "94a2af9d";*/
 ?>
<?php
//if ($_SERVER["REQUEST_METHOD"] == "POST") {
	$number = $_GET["phone"];
	$message = $_GET["msg"];
	$whosent = "1";
//}

// if there is post variables and api keys matches:
if (isset($_GET["phone"])) {
	
	// first check if client exists
	try {
		$core = Core::getInstance();
		$stmt = $core->dbh->prepare("SELECT client_id FROM myclients WHERE client_number = :client_number");
		$stmt -> bindParam("client_number", $number, PDO::PARAM_STR);
		$stmt -> execute();
		$row_count = $stmt -> rowCount();
		$row = $stmt -> fetch();
	} catch(PDOException $pe) {
		trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
	}
	
	// if client doesnt exists
	if ($row_count < 1) {
		/*
		try {
			$core = Core::getInstance();
			$stmt = $core->dbh->prepare("INSERT INTO myclients (client_number, init_message) VALUES (:client_number, :init_message)");
			$stmt -> bindParam("client_number", $number, PDO::PARAM_STR);
			$stmt -> bindParam("init_message", $message, PDO::PARAM_STR);
			
			if ($stmt->execute()) {
				echo "true";
			} else {
				echo "Message has not been added";
			}
		} catch(PDOException $pe) {
			trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
		}*/
		echo "Message has not been added";
	} else {
		// if client exists
		try {
			
				
				$core = Core::getInstance();
				$stmt = $core->dbh->prepare("INSERT INTO messages (mess_client_id, mess_message, mess_whosent) VALUES (:mess_client_id, :mess_message, :mess_whosent)");
				$stmt -> bindParam("mess_client_id", $row["client_id"], PDO::PARAM_INT);
				$stmt -> bindParam("mess_message", $message, PDO::PARAM_STR);
				$stmt -> bindParam("mess_whosent", $whosent, PDO::PARAM_STR);
				
				if ($stmt->execute()) {
					echo "true";
				} else {
					echo "Message has not been added";
				}
			
		} catch(PDOException $pe) {
			trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
		}
	}
}
?>