<?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['user_id']))
{
require 'config.php';


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



$auth=check_input($_POST['auth']);
$user_id=check_input($_POST['user_id']);
$lat=check_input($_POST['lat']);
$lng=check_input($_POST['lng']);
$comment=check_input($_POST['comment']);
$post_id=check_input($_POST['post_id']);
$posted_by=check_input($_POST['posted_by']);


$auth = mysqli_escape_string($con, $auth);
$user_id=mysqli_escape_string($con, $user_id);
$lat=mysqli_escape_string($con, $lat);
$lng=mysqli_escape_string($con, $lng);
$comment=mysqli_escape_string($con, $comment);
$post_id=mysqli_escape_string($con, $post_id);
$posted_by=mysqli_escape_string($con, $posted_by);




$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 == 1)
{
$rows = mysqli_fetch_array($result);
$phone = $rows['phone'];
$email = $rows['email'];
$firstname = $rows['firstname'];
$lastname = $rows['lastname'];
//$posted_by = $firstname." ".$lastname;

date_default_timezone_set("Africa/Lagos");

  $date_t = date("d-M-Y h:i:s a"); 
  
 
// include 'src/get_locality.php';
// $address= getaddress($lat,$lng);


function check_number(){

require 'config.php';

    $unique_number = rand(1000000000000, 9999999999999);

    $sql = $con->query("SELECT * FROM comments WHERE comment_id = '$unique_number'")  or die ("error: ".mysqli_error($con));
    $exists = mysqli_num_rows($sql);

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

$comment_id = check_number();



$sql2="SELECT * FROM timeline WHERE post_id = '$post_id' AND user_id = '$posted_by'";
$result2=$con->query($sql2) or die ("error: Server error ".mysqli_error($con));
$count2=mysqli_num_rows($result2);

if ($count2 == 1)
{
$rows2 = mysqli_fetch_array($result2);
$likes = $rows2['likes'];
$comments = $rows2['comments'];
$views = $rows2['views'];
$views = $views+5;
$comments = $comments+1;
$notify = $firstname." ".$lastname." commented on your post.";
$firstnamex = $rows2['firstname'];
$lastnamex = $rows2['lastname'];
$name = $firstnamex." ".$lastnamex;
$pro_pic= $p_url."/profiles/".$user_id.".png?time=".time();


  $sqlx=$con->query("INSERT INTO comments  (user_id, post_id, pro_pic, comment, comment_id, firstname, lastname, lat, lng, location, image, posted_by,  date_t) 
      VALUES ('$user_id', '$post_id', '$pro_pic', '$comment', '$comment_id',  '$firstname', '$lastname', '$lat', '$lng', '$address', '', '$posted_by',   '$date_t')") or die("Error: ".mysqli_error($con));
      
     $sqly=$con->query("INSERT INTO notifications  (user_id, name, pro_pic,  post_id, notification, posted_by,  date_t) 
      VALUES ('$posted_by', '$name', '$pro_pic', '$post_id', '$notify', '$posted_by', '$date_t')") or die("Error: ".mysqli_error($con));
      

    $sqlz=$con->query("UPDATE timeline SET  comments = '$comments', views = '$views'  WHERE post_id = '$post_id' AND user_id = '$posted_by'");

      
      
  
if($sqlx)
{

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

}
}
else
{
         $suc = "Post Does Not Exist"; 
         $status = false; 


}

}
else
{
         $suc = "User Does Not Exist"; 
         $status = false; 
}

$con->close();

$output = array('status' => $status, 'success'=>$suc);
echo json_encode($output); //output JSON data
}
?>
