<?php
/**
 *  Copyright (C) 2021  Double Bastion LLC
 *
 *  This file is part of Roundpin, which is licensed under the
 *  GNU Affero General Public License Version 3.0. The license terms
 *  are detailed in the "LICENSE.txt" file located in the root directory.
 */

session_start();

if (isset($_POST['s_ajax_call']) && ($_POST['s_ajax_call'] == $_SESSION['validate_s_access'])) {

 define('ACCESSCONST', TRUE);

 require('db-connect.php');

    $username = $_POST['username'];
    $contactName = $_POST['contact_name'];

    // Get the id of the current user from the 'app_users' table
    $queryid = $mysqli->query("SELECT id, username, enabled FROM app_users WHERE BINARY username = '$username' AND enabled = 1");
    $queryres = $queryid->fetch_array();
    $userID = $queryres[0];

    // Remove the contact from the 'contacts' table
    $querycont = $mysqli->query("DELETE FROM contacts WHERE user_id = '$userID' AND contact_name = '$contactName'");
    if ($querycont) { $message = 'success'; } else { $message = 'failure'; }

    $delresult = ['removeresult' => $message];
    echo json_encode($delresult);

} else {
     header("Location: roundpin-login.php");
}

?>
