<?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'];
session_write_close();
$id_virtualtour = (int)$_POST['id_virtualtour'];
if(!check_elem_ownership($id_user,$id_virtualtour)) {
    ob_end_clean();
    echo json_encode(array("status"=>"error"));
    die();
}
$password = strip_tags($_POST['password']);
$password_title = strip_tags($_POST['password_title']);
$password_description = strip_tags($_POST['password_description']);
$protect_type = strip_tags($_POST['protect_type']);
$protect_send_email = (int)$_POST['protect_send_email'];
$protect_email = strip_tags($_POST['protect_email']);
$protect_lead_webhook_url = strip_tags($_POST['protect_lead_webhook_url']);
$protect_remember = (int)$_POST['protect_remember'];
$protect_mc_form = $_POST['protect_mc_form'];
$protect_lead_params = $_POST['protect_lead_params'];
$array_lang = json_decode($_POST['array_lang'],true);
if(empty($password)) {
    $query = "UPDATE svt_virtualtours SET password=NULL,protect_lead_webhook_url=?,password_title=?,password_description=?,protect_type=?,protect_send_email=?,protect_email=?,protect_remember=?,protect_mc_form=?,protect_lead_params=? WHERE id=?;";
    if($smt = $mysqli->prepare($query)) {
        $smt->bind_param('ssssisissi',$protect_lead_webhook_url,$password_title,$password_description,$protect_type,$protect_send_email,$protect_email,$protect_remember,$protect_mc_form,$protect_lead_params,$id_virtualtour);
        $result = $smt->execute();
        if ($result) {
            save_input_langs($array_lang,'svt_virtualtours_lang','id_virtualtour',$id_virtualtour);
            ob_end_clean();
            echo json_encode(array("status"=>"ok"));
        } else {
            ob_end_clean();
            echo json_encode(array("status"=>"error"));
        }
    } else {
        ob_end_clean();
        echo json_encode(array("status"=>"error"));
    }
} else {
    if($password=="keep_password") {
        $query = "UPDATE svt_virtualtours SET protect_lead_webhook_url=?,password_title=?,password_description=?,protect_type=?,protect_send_email=?,protect_email=?,protect_remember=?,protect_mc_form=?,protect_lead_params=? WHERE id=?;";
        if($smt = $mysqli->prepare($query)) {
            $smt->bind_param('ssssisissi',$protect_lead_webhook_url,$password_title,$password_description,$protect_type,$protect_send_email,$protect_email,$protect_remember,$protect_mc_form,$protect_lead_params,$id_virtualtour);
            $result = $smt->execute();
            if ($result) {
                save_input_langs($array_lang,'svt_virtualtours_lang','id_virtualtour',$id_virtualtour);
                ob_end_clean();
                echo json_encode(array("status"=>"ok"));
            } else {
                ob_end_clean();
                echo json_encode(array("status"=>"error"));
            }
        } else {
            ob_end_clean();
            echo json_encode(array("status"=>"error"));
        }
    } else {
        $query = "UPDATE svt_virtualtours SET protect_lead_webhook_url=?,password=MD5(?),password_title=?,password_description=?,protect_type=?,protect_send_email=?,protect_email=?,protect_remember=?,protect_mc_form=?,protect_lead_params=? WHERE id=?;";
        if($smt = $mysqli->prepare($query)) {
            $smt->bind_param('sssssisissi',$protect_lead_webhook_url,$password,$password_title,$password_description,$protect_type,$protect_send_email,$protect_email,$protect_remember,$protect_mc_form,$protect_lead_params,$id_virtualtour);
            $result = $smt->execute();
            if ($result) {
                save_input_langs($array_lang,'svt_virtualtours_lang','id_virtualtour',$id_virtualtour);
                ob_end_clean();
                echo json_encode(array("status"=>"ok"));
            } else {
                ob_end_clean();
                echo json_encode(array("status"=>"error"));
            }
        } else {
            ob_end_clean();
            echo json_encode(array("status"=>"error"));
        }
    }
}