<?php
session_start();

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

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

header("Access-Control-Allow-Headers: X-Requested-With");
header('Content-Type: application/json; charset=utf-8');


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']);
$content=check_input($_POST['content']);


$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);
$content=mysqli_escape_string($con, $content);




$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"); 
  
 
 include 'src/get_locality.php';
 $address= getaddress($lat,$lng);




function check_number(){

require 'config.php';

    $unique_number = rand(1000000000, 9999999999);

    $sql = $con->query("SELECT * FROM timeline WHERE post_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;
     }
}

$post_id = check_number();



$time = time();
$file_name = $_FILES["img"]["name"];
$extension = pathinfo($file_name, PATHINFO_EXTENSION);

if ($extension == "mp4")
{
$save_video =  $time.'-'.$user_id;

$video = $_FILES["img"]["tmp_name"]; 
  // Compress size and upload image 
            $save_video=$save_video.'.mp4';
            $path ='../posts/'.$save_video;
    
if (move_uploaded_file($video, $path))
{
    $img_url = $p_url."/posts/".$save_video;          
}  
$file_type = "Video";

}

else
{
  $save_image =  $time.'-'.$user_id;
  $image = $_FILES["img"]["tmp_name"]; 
  // Compress size and upload image 
            $save_image=$save_image.'.png';
            $path ='../posts/'.$save_image;
    
if (move_uploaded_file($image, $path))
{
    $img_url = $p_url."/posts/".$save_image;  
}  
$file_type = "Image";  
}



/*$img   =  $_POST['img'];
$fileX =  $post_id.'.png';
$img = str_replace('data:image/png;base64,', '',$img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = "../posts/$fileX";
$success = file_put_contents($file, $data);
$img_url = $p_url."/posts/".$fileX;
*/




  $sqlx=$con->query("INSERT INTO timeline  (user_id, user, firstname, lastname, lat, lng, location,  post, post_id, post_type, comments, likes, shares, views, image,  date_t) 
      VALUES ('$user_id', '$email', '$firstname', '$lastname', '$lat', '$lng', '$address', '$content', '$post_id', '$file_type', '0', '0', '0', '0', '$img_url',  '$date_t')") or die("Error: ".mysqli_error($con));
      
      
  
if($sqlx)
{

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

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


}

$con->close();

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