<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
ob_start();
session_start();
if((($_SERVER['SERVER_ADDR']==$_SESSION['demo_server_ip']) && ((!empty($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0] : $_SERVER['REMOTE_ADDR']))!=$_SESSION['demo_developer_ip']) && ($_SESSION['id_user']==$_SESSION['demo_user_id'])) || ($_SESSION['svt_si']!=session_id())) {
    die();
}
require_once("../../db/connection.php");
require_once("../functions.php");
$id_user = $_SESSION['id_user'];
$id_virtualtour = (int)$_POST['id_virtualtour'];
$settings = get_settings();
$where = "";
switch(get_user_role($id_user)) {
    case 'customer':
        $where = " AND id_user=$id_user ";
        break;
    case 'editor':
        $where = " AND id IN () ";
        $query = "SELECT GROUP_CONCAT(id_virtualtour) as ids FROM svt_assign_virtualtours WHERE id_user=$id_user;";
        $result = $mysqli->query($query);
        if($result) {
            if($result->num_rows==1) {
                $row=$result->fetch_array(MYSQLI_ASSOC);
                $ids = $row['ids'];
                $where = " AND id IN ($ids) ";
            }
        }
        break;
}
$code = "";
$name_vt = "";
$query = "SELECT code,name FROM svt_virtualtours WHERE id=$id_virtualtour $where LIMIT 1;";
$result = $mysqli->query($query);
if($result) {
    if($result->num_rows==1) {
        $row = $result->fetch_array(MYSQLI_ASSOC);
        $code = $row['code'];
        $name_vt = $row['name'];
    }
}
if(empty($code)) {
    ob_end_clean();
    echo json_encode(array("status"=>"error"));
    die();
}
if(get_user_role($id_user)=='administrator') {
    $query = "DELETE FROM svt_virtualtours WHERE id=$id_virtualtour; ";
} else {
    $query = "DELETE FROM svt_virtualtours WHERE id_user=$id_user AND id=$id_virtualtour; ";
}
$result = $mysqli->query($query);
if($result) {
    if(isset($_SESSION['id_virtualtour_sel'])) {
        if($_SESSION['id_virtualtour_sel']==$id_virtualtour) {
            unset($_SESSION['id_virtualtour_sel']);
            unset($_SESSION['name_virtualtour_sel']);
        }
    }
    $path = realpath(dirname(__FILE__).'/../..');
    $filter = array();
    if(file_exists($path.DIRECTORY_SEPARATOR.'video360'.DIRECTORY_SEPARATOR.$id_virtualtour.DIRECTORY_SEPARATOR)) {
        $files_video360 = new RecursiveIteratorIterator(
            new RecursiveCallbackFilterIterator(
                new RecursiveDirectoryIterator($path.DIRECTORY_SEPARATOR.'video360'.DIRECTORY_SEPARATOR.$id_virtualtour.DIRECTORY_SEPARATOR,RecursiveDirectoryIterator::SKIP_DOTS),
                function ($fileInfo, $key, $iterator) use ($filter) {
                    return true;
                }
            )
        );
        foreach ($files_video360 as $file) {
            $source_file = $file->getPathname();
            unlink($source_file);
        }
        rmdir($path.DIRECTORY_SEPARATOR.'video360'.DIRECTORY_SEPARATOR.$id_virtualtour.DIRECTORY_SEPARATOR);
    }
    session_write_close();
    $mysqli->query("ALTER TABLE svt_virtualtours AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_virtualtours_versions AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_forms_data AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_access_log AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_rooms AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_rooms_alt AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_rooms_access_log AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_gallery AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_icons AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_maps AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_markers AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_pois AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_poi_gallery AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_poi_gallery_lang AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_presentations AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_poi_embedded_gallery AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_poi_objects360 AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_products AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_product_images AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_video_projects AUTO_INCREMENT = 1;");
    $mysqli->query("ALTER TABLE svt_video_project_slides AUTO_INCREMENT = 1;");
    $id_vt_samples = $settings['id_vt_sample'];
    if (!empty($id_vt_samples)) {
        $id_vt_samples_array = explode(",", $id_vt_samples);
        if (in_array($id_virtualtour, $id_vt_samples_array)) {
            $key = array_search($id_virtualtour, $id_vt_samples_array);
            unset($id_vt_samples_array[$key]);
            $id_vt_samples = implode(",", $id_vt_samples_array);
            if (empty($id_vt_samples)) $id_vt_samples="0";
            $mysqli->query("UPDATE svt_settings SET id_vt_sample='$id_vt_samples';");
        }
    }
    $mysqli->query("UPDATE svt_plans SET id_vt_template=NULL WHERE id_vt_template=$id_virtualtour");
    $query = "SELECT id,id_vt_sample FROM svt_plans WHERE id_vt_sample IS NOT NULL AND id_vt_sample!='';";
    $result = $mysqli->query($query);
    if($result) {
        if($result->num_rows>0) {
            $row = $result->fetch_array(MYSQLI_ASSOC);
            $id_plan = $row['id'];
            $id_vt_samples = $row['id_vt_sample'];
            if (!empty($id_vt_samples)) {
                $id_vt_samples_array = explode(",", $id_vt_samples);
                if (in_array($id_virtualtour, $id_vt_samples_array)) {
                    $key = array_search($id_virtualtour, $id_vt_samples_array);
                    unset($id_vt_samples_array[$key]);
                    $id_vt_samples = implode(",", $id_vt_samples_array);
                    if (empty($id_vt_samples)) $id_vt_samples="0";
                    $mysqli->query("UPDATE svt_plans SET id_vt_sample='$id_vt_samples' WHERE id=$id_plan;");
                }
            }
        }
    }
    $mysqli->query("UPDATE svt_autoenhance_log SET deleted=1 WHERE id_room IN (SELECT id FROM svt_rooms WHERE id_virtualtour=$id_virtualtour);");
    $path = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR;
    if(file_exists($path . "favicons" . DIRECTORY_SEPARATOR . "v_$code")) {
        array_map('unlink', glob($path . "favicons" . DIRECTORY_SEPARATOR . "v_$code" . DIRECTORY_SEPARATOR ."*.*"));
        rmdir($path . "favicons" . DIRECTORY_SEPARATOR . "v_$code" . DIRECTORY_SEPARATOR);
    }
    set_user_log($id_user,'delete_virtual_tour',json_encode(array("id"=>$id_virtualtour,"name"=>$name_vt)),date('Y-m-d H:i:s', time()));
    if(isEnabled('shell_exec')) {
        require_once(__DIR__ . "/../../config/config.inc.php");
        if (defined('PHP_PATH')) {
            $path_php = PHP_PATH;
        } else {
            $path_php = '';
        }
        try {
            if(empty($path_php)) {
                $command = 'command -v php 2>&1';
                $output = shell_exec($command);
                if(empty($output)) $output = PHP_BINARY;
                $path_php = trim($output);
                $path_php = str_replace("sbin/php-fpm","bin/php",$path_php);
            }
            $path = realpath(dirname(__FILE__).'/../../services');
            $command = $path_php." ".$path.DIRECTORY_SEPARATOR."clean_images.php $id_user > /dev/null &";
            shell_exec($command);
        } catch (Exception $e) {}
    } else {
        include("../../services/clean_images.php");
    }
    update_user_space_storage($id_user,false);
    ob_end_clean();
    echo json_encode(array("status"=>"ok"));
} else {
    ob_end_clean();
    echo json_encode(array("status"=>"error"));
}