<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
session_start();
if((($_SERVER['SERVER_ADDR']==$_SESSION['demo_server_ip']) && ($_SERVER['REMOTE_ADDR']!=$_SESSION['demo_developer_ip']) && ($_SESSION['id_user']==$_SESSION['demo_user_id'])) || ($_SESSION['sml_si']!=session_id())) {
    //DEMO CHECK
    die();
}
require_once("../../db/connection.php");
require_once("../functions.php");
$id_user = $_SESSION['id_user'];
$id_review = (int)$_POST['id'];
$id_map = (int)$_POST['id_map'];
switch(get_user_role($id_user)) {
    case 'administrator':
        $query = "SELECT * FROM sml_maps WHERE id=$id_map; ";
        break;
    case 'customer':
        $query = "SELECT * FROM sml_maps WHERE id_user=$id_user AND id=$id_map; ";
        break;
    case 'editor':
        $query = "SELECT * FROM sml_maps WHERE id=$id_map AND (SELECT COUNT(*) FROM sml_assign_maps WHERE id_map=$id_map AND id_user=$id_user LIMIT 1)=1; ";
        break;
}
$result = $mysqli->query($query);
if($result) {
    if($result->num_rows==0) {
        ob_end_clean();
        echo json_encode(array("status"=>"unauthorized"));
        exit;
    }
}
$query = "DELETE FROM sml_reviews WHERE id=?;";
if($smt = $mysqli->prepare($query)) {
    $smt->bind_param('i',$id_review);
    $result = $smt->execute();
    if($result) {
        $mysqli->query("ALTER TABLE sml_reviews AUTO_INCREMENT = 1;");
        echo json_encode(array("status"=>"ok"));
    } else {
        echo json_encode(array("status"=>"error"));
    }
} else {
    echo json_encode(array("status"=>"error"));
}
