<?php

if (isset($_POST['availability'])) {
    include 'dbconnect.php';
    $phone = $_POST['phone'];
    $phone = substr($phone,-9);
    findUser($db, $phone);
}

function findUser($db, $phone)
{
    $stmt = pg_prepare($db, "find_user", 'SELECT token,profile FROM reg_contacts WHERE iphone = $1');
    $result = pg_execute($db, "find_user", array($phone));

    if (pg_num_rows($result) == 1) {
        $row = pg_fetch_assoc($result);
        $token = $row["token"];
     
        $response = array(
            "success" => true,
            "response" => "User exists",
            "token" => $row["token"],
            "profile"=>$row['profile']
        );

        echo json_encode($response);
    } else {
        $response = array(
            "success" => false,
            "reason"=>0,
            "response" => "User doesn't exist"
        );
        echo json_encode($response);
    }
}
?>