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


$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 == 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"); 
  
 



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




    $sqlz=$con->query("UPDATE users SET  pro_pic = '$img_url'  WHERE  user_id = '$user_id' AND auth = '$auth'");
      
      
  
if($sqlx)
{

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

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


}

$con->close();

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