<?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']);
$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);
$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'];


date_default_timezone_set("Africa/Lagos");

  $date_t = date("d-M-Y h:i:s a"); 
  



$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'];
$views = $rows2['views'];
$views = $views+2;
$notify = $firstname." ".$lastname." Liked your post.";
$name = $firstname." ".$lastname;
$pro_pic = $p_url."/profiles/".$user_id.".png";



$sqlr=$con->query("SELECT * FROM likes WHERE post_id = '$post_id' AND user_id = '$user_id' AND posted_by = '$posted_by'") or die ("error: Server error ".mysqli_error($con));
$countr=mysqli_num_rows($sqlr);

if ($countr == 1)
{
$likes = $likes-1;
$sqlx=$con->query("DELETE  FROM likes WHERE post_id = '$post_id' AND user_id = '$user_id' AND posted_by = '$posted_by'") or die ("error: Server error ".mysqli_error($con));
$sqlz=$con->query("UPDATE timeline SET  likes = '$likes' WHERE post_id = '$post_id' AND user_id = '$posted_by'");
} 
     
else
{
$likes = $likes+1;
$sqlx=$con->query("INSERT INTO likes  (user_id, user, post_id, posted_by,  date_t) 
      VALUES ('$user_id', '$email', '$post_id', '$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  likes = '$likes', views = '$views'  WHERE post_id = '$post_id' AND user_id = '$posted_by'");
}

if($sqlx && $sqlz)
{
$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, 'likes' => $likes);
echo json_encode($output); //output JSON data
}
?>
