<?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();
// header('Set-Cookie: PHPSESSID= ' . session_id() . '; SameSite=strict; Secure=true; HttpOnly=true;');
if (isset($_POST['s_ajax_call']) && ($_POST['s_ajax_call'] == $_SESSION['validate_s_access'])) {
define('ACCESSCONST', TRUE);
require('db-connect.php');
$username = $_POST['username'];
$selectedGroups = $_POST['selectedgroups'];
$selectedGroupsArr = explode("|", $selectedGroups);
array_shift($selectedGroupsArr);
array_pop($selectedGroupsArr);
// Get the id of the current user from the 'app_users' table
$queryid = $mysqli->prepare("SELECT id, username FROM app_users WHERE BINARY username = ?");
$queryid->bind_param("s", $username);
$queryid->execute();
$queryres = $queryid->get_result()->fetch_assoc();
$userID = $queryres['id'];
// Get the contacts for the current user
$querycont = $mysqli->prepare("SELECT id, user_id, contact_name, contact_desc, contact_long_desc, address_street, address_zip, address_town, address_country, address_state,
extension_number, contact_mobile, contact_num1, contact_num2, contact_fax, contact_email, profile_picture_c, groups, date_added, date_modified
FROM contacts WHERE user_id = ?");
$querycont->bind_param("i", $userID);
$querycont->execute();
$queryres = $querycont->get_result();
$msgdel = 0;
while ($querycontres = $queryres->fetch_assoc()) {
$contactGroupsArr = explode("|", $querycontres['groups']);
array_shift($contactGroupsArr);
array_pop($contactGroupsArr);
$gck = 0;
foreach ($contactGroupsArr as $key => $arrval) {
if (in_array($arrval, $selectedGroupsArr)) { $gck++; }
}
if ($gck > 0) {
// Remove the contact from the 'contacts' table
$querycont = $mysqli->prepare("DELETE FROM contacts WHERE id = ? AND user_id = ?");
$querycont->bind_param("ii", $querycontres['id'], $userID);
if ($querycont->execute()) { /* OK */ } else { $msgdel++; }
}
}
if ($msgdel == 0) { $messagedel = "success"; } else { $messagedel = "failure"; }
// Reindex the 'contacts' table
$reindexset = $mysqli->prepare("SET @resetrec = 0");
$reindexup = $mysqli->prepare("UPDATE contacts SET id = @resetrec := @resetrec + 1");
$reindexalt = $mysqli->prepare("ALTER TABLE contacts auto_increment = 1");
if ($reindexset->execute() && $reindexup->execute() && $reindexalt->execute()) { $reindexmsg = "success"; } else { $reindexmsg = "failure"; }
$delresult = array('removeresult' => $messagedel, 'reindex' => $reindexmsg);
echo json_encode($delresult);
} else {
header("Location: ../login.php");
}
?>