<?php
session_start();

header('Access-Control-Allow-Origin: *');

header('Access-Control-Allow-Methods: GET, POST');

header("Access-Control-Allow-Headers: X-Requested-With");


if(isset($_POST['sender_id']) && isset($_POST['recipient_id']))
{
require 'config.php';


function check_input($par) {
  $par = trim($par);
  $par = stripslashes($par);
  $par = htmlspecialchars($par);
  return $par;
}



$sender_id=check_input($_POST['sender_id']);
$recipient_id=check_input($_POST['recipient_id']);
$message=check_input($_POST['message']);



$sender_id = mysqli_escape_string($con, $sender_id);
$recipient_id = mysqli_escape_string($con, $recipient_id);
$message=mysqli_escape_string($con, $message);



$auth=check_input($_POST['auth']);
$user_id=check_input($_POST['sender_id']);
$auth = mysqli_escape_string($con, $auth);
$user_id=mysqli_escape_string($con, $user_id);



$sql="SELECT * FROM users WHERE user_id = '$user_id' AND auth = '$auth'";
$result=$con->query($sql) or die ("error: Server error ".mysqli_error($con));
$count=mysqli_num_rows($result);

if ($count == 0)
{
        $suc = "User Does Not Exist"; 
         $status = false; 

}
else
{


$sql1="SELECT * FROM users WHERE  user_id='$sender_id'";
$result1=$con->query($sql1) or die ("error: Server error ".mysqli_error($con));
$count1=mysqli_num_rows($result1);

if($count1 == 1)
{

$row = mysqli_fetch_array($result1);
$fname = $row['firstname'];
$lname = $row['lastname'];
$sender_id = $row['user_id'];
$sender_name = $fname." ".$lname;
}


$sql="SELECT * FROM users WHERE  user_id='$recipient_id'";
$result=$con->query($sql) or die ("error1: Server error ".mysqli_error($con));
$count=mysqli_num_rows($result);

if($count == 1)
{
$rows = mysqli_fetch_array($result);
$fname = $rows['firstname'];
$lname = $rows['lastname'];
$recipient_id = $rows['user_id'];
$recipient_name = $fname." ".$lname;

}



function check_number(){

require 'config.php';

    $unique_number = rand(0000000000, 9999999999);

    $sqlx = $con->query("SELECT * FROM messages WHERE msg_id = '$unique_number'")  or die ("error2: ".mysqli_error($con));
    $exists = mysqli_num_rows($sqlx);

    if ($exists >0){
        $results = check_number();
    }
     else{
            $results = $unique_number;
        return $results;
     }
}

$msg_id = check_number();
  

date_default_timezone_set("Africa/Lagos");

  $date_t = date("d-M-Y"); 
  $time_t = date("H:i"); 
  $date_t2 = date("d-m-Y");




  $sql2=$con->query("INSERT INTO messages  (msg_id, sender, sender_id, recipient, recipient_id, message, date_t, time_t, date_t2, status) 
      VALUES ('$msg_id', '$sender_name', '$sender_id', '$recipient_name', '$recipient_id', '$message',  '$date_t', '$time_t', '$date_t2', 'New')") or die("Error: ".mysqli_error($con));
      

$suc = "Successful"; 
$status = true; 

}
}
$con->close();

$output = array('status' => $status, 'success'=>$suc, 'msg_id' => $msg_id, 'message' => $message, 'sender' => $sender_name, 'sender_id' => $sender_id, 'recipient' => $recipient_name, 'recipient_id' => $recipient_id);
echo json_encode($output); //output JSON data

?>
