<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
session_start();
ob_start();
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest') {
    die();
}
require_once("../../db/connection.php");
require_once("../../backend/functions.php");
unset($_SESSION['lt_color']);
unset($_SESSION['live_tracking_only_admin']);
$id = strip_tags($_POST['id']);
$id_map = (int)$_POST['id_map'];
$auth_settings = sml_get_viewer_auth_settings($id_map);
$id_user = ((int)$auth_settings['viewer_auth_mode']>0) ? sml_get_viewer_user_id() : null;
$require_login = sml_viewer_feature_requires_login($id_map,'live_tracking');
session_write_close();
if($require_login && empty($id_user)) {
    $mysqli->close();
    exit;
}
if(!empty($id_user)) {
    $query = "DELETE FROM sml_live_tracking WHERE id=? AND id_map=? AND (id_user=? OR id_user IS NULL);";
} else {
    $query = "DELETE FROM sml_live_tracking WHERE id=? AND id_map=?;";
}
if($smt = $mysqli->prepare($query)) {
    if(!empty($id_user)) {
        $smt->bind_param('sii',$id,$id_map,$id_user);
    } else {
        $smt->bind_param('si',$id,$id_map);
    }
    $smt->execute();
}
$mysqli->close();
