<?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["msisdn"];
	$message = remove_emoji($_GET["text"]);
	if(isset($_GET['concat'])){
	    $concat = $_GET["concat"];
	} else {
	    $concat = "";
	}
	if(isset($_GET['concat-ref'])){
	    $concat_ref = $_GET["concat-ref"];
	} else {
	    $concat_ref = "";
	}
	if(isset($_GET['concat-total'])){
	    $concat_total = $_GET["concat-total"];
	} else {
	    $concat_total = "";
	}
	if(isset($_GET['concat-part'])){
	    $concat_part = $_GET["concat-part"];
	} else {
	    $concat_part = "";
	}
	/*$concat = $_GET["concat"];
	$concat_ref = $_GET["concat-ref"];
	$concat_total = $_GET["concat-total"];
	$concat_part = $_GET["concat-part"];*/
	$api_key = $_GET["api-key"];
	$lookup_string = $concat_part." => ";
	$whosent = "0";
	$concatenated_message = $concat_part." => ".$message;
	$mc_concat = "1";
//}

// if there is post variables and api keys matches:
if (isset($_GET["msisdn"]) && strcmp($api_key, $cf_api_key) == 0) {
	
	// 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);
		}
	} else {
		// if client exists
		try {
			// if message is multipart!!!!
			if ($concat == "true") {
				// first check if message already exists
				try {
					$core = Core::getInstance();
					$stmt = $core->dbh->prepare("SELECT * FROM messages LEFT JOIN messages_concat on mess_id = mc_message_id LEFT JOIN myclients ON mess_client_id = client_id WHERE client_number = :number AND mc_concat_ref = :mc_concat_ref AND mc_concat_total = :mc_concat_total AND (mess_message LIKE :term1 OR mess_message LIKE :term2)");
					$term1 = $lookup_string;
					$term2 = "%".$lookup_string;
					$stmt -> bindParam("number", $number, PDO::PARAM_STR);
					$stmt -> bindParam("mc_concat_ref", $concat_ref, PDO::PARAM_INT);
					$stmt -> bindParam("mc_concat_total", $concat_total, PDO::PARAM_INT);
					$stmt -> bindParam("term1", $term1);
					$stmt -> bindParam("term2", $term2);
					$stmt -> execute();
					$row_temp = $stmt -> rowCount();
					if ($row_temp > 0) {
						exit();
					}
				} catch (PDOException $pe) {
					trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
				}
				// first check if message already exists
				try {
					$core = Core::getInstance();
					$stmt = $core->dbh->prepare("SELECT * FROM messages LEFT JOIN messages_concat on mess_id = mc_message_id LEFT JOIN myclients ON mess_client_id = client_id WHERE client_number = :number AND mc_concat_ref = :mc_concat_ref AND mc_concat_total = :mc_concat_total AND (mess_message NOT LIKE :term1 AND mess_message NOT LIKE :term2)");
					$term1 = $lookup_string;
					$term2 = "%".$lookup_string;
					
					$stmt -> bindParam("number", $number, PDO::PARAM_STR);
					$stmt -> bindParam("mc_concat_ref", $concat_ref, PDO::PARAM_INT);
					$stmt -> bindParam("mc_concat_total", $concat_total, PDO::PARAM_INT);
					$stmt -> bindParam("term1", $term1);
					$stmt -> bindParam("term2", $term2);
					$stmt -> execute();
					//$stmt -> debugDumpParams();
					$mess_row = $stmt -> fetchAll(PDO::FETCH_ASSOC);
					$mess_row_count = $stmt -> rowCount();
					
					//die(print_r($mess_row));
					//die($mess_row[0]["mess_id"]);
					
					
					if ($mess_row_count < 1) {
						// add new message to database
						$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", $concatenated_message, PDO::PARAM_STR);
						$stmt -> bindParam("mess_whosent", $whosent, PDO::PARAM_STR);
						$stmt -> execute();
						$last_insert_id = $core->dbh->lastInsertId();
						
						$core = Core::getInstance();
						$stmt = $core->dbh->prepare("INSERT INTO messages_concat (mc_message_id, mc_concat, mc_concat_ref, mc_concat_total, mc_concat_parts) VALUES (:mc_message_id, :mc_concat, :mc_concat_ref, :mc_concat_total, :mc_concat_parts)");
						$stmt -> bindParam("mc_message_id", $last_insert_id, PDO::PARAM_INT);
						$stmt -> bindParam("mc_concat", $mc_concat, PDO::PARAM_STR);
						$stmt -> bindParam("mc_concat_ref", $concat_ref, PDO::PARAM_STR);
						$stmt -> bindParam("mc_concat_total", $concat_total, PDO::PARAM_STR);
						$stmt -> bindParam("mc_concat_parts", $concat_part, PDO::PARAM_STR);
						$stmt -> execute();
					} else {
						// update existing in database
						
						// ovdje sam stao
						$core = Core::getInstance();
						$stmt = $core->dbh->prepare("UPDATE messages SET mess_message = CONCAT(mess_message, '|||', :message) WHERE mess_id = :mess_id");
						$stmt -> bindParam("mess_id", $mess_row[0]["mess_id"], PDO::PARAM_INT);
						$stmt -> bindParam("message", $concatenated_message, PDO::PARAM_STR);
						
						$stmt -> execute();
					}
				} catch (PDOException $pe) {
					trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
				}
			} else {
				
				$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);
		}
	}
}
?>