<?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();
if(!get_user_role($id_user)=='administrator') {
    ob_end_clean();
    echo json_encode(array("status"=>"error"));
    exit;
}
$name = strip_tags($_POST['name']);
$position = (int)$_POST['position'];
$n_virtual_tours = ($_POST['n_virtual_tours']=="") ? -1 : (int)$_POST['n_virtual_tours'];
$n_virtual_tours_month = ($_POST['n_virtual_tours_month']=="") ? -1 : (int)$_POST['n_virtual_tours_month'];
$n_rooms = ($_POST['n_rooms']=="") ? -1 : (int)$_POST['n_rooms'];
$n_rooms_tour = ($_POST['n_rooms_tour']=="") ? -1 : (int)$_POST['n_rooms_tour'];
$n_markers = ($_POST['n_markers']=="") ? -1 : (int)$_POST['n_markers'];
$n_pois = ($_POST['n_pois']=="") ? -1 : (int)$_POST['n_pois'];
$n_gallery_images = ($_POST['n_gallery_images']=="") ? -1 : (int)$_POST['n_gallery_images'];
$n_ai_generate_month = ($_POST['n_ai_generate_month']=="") ? -1 : (int)$_POST['n_ai_generate_month'];
$n_autoenhance_generate_month = ($_POST['n_autoenhance_generate_month']=="") ? -1 : (int)$_POST['n_autoenhance_generate_month'];
$days = ($_POST['days']=="") ? -1 : (int)$_POST['days'];
$max_file_size_upload = ($_POST['max_file_size_upload']=="") ? -1 : (int)$_POST['max_file_size_upload'];
$max_storace_space = ($_POST['max_storace_space']=="") ? -1 : (int)$_POST['max_storace_space'];
$create_landing = (int)$_POST['create_landing'];
$create_showcase = (int)$_POST['create_showcase'];
$create_globes = (int)$_POST['create_globes'];
$create_gallery = (int)$_POST['create_gallery'];
$create_presentation = (int)$_POST['create_presentation'];
$create_video360 = (int)$_POST['create_video360'];
$create_video_projects = (int)$_POST['create_video_projects'];
$enable_live_session = (int)$_POST['enable_live_session'];
$enable_meeting = (int)$_POST['enable_meeting'];
$enable_chat = (int)$_POST['enable_chat'];
$enable_voice_commands = (int)$_POST['enable_voice_commands'];
$enable_share = (int)$_POST['enable_share'];
$enable_snapshot = (int)$_POST['enable_snapshot'];
$enable_device_orientation = (int)$_POST['enable_device_orientation'];
$enable_webvr = (int)$_POST['enable_webvr'];
$enable_logo = (int)$_POST['enable_logo'];
$enable_nadir_logo = (int)$_POST['enable_nadir_logo'];
$enable_song = (int)$_POST['enable_song'];
$enable_comments = (int)$_POST['enable_comments'];
$enable_forms = (int)$_POST['enable_forms'];
$enable_annotations = (int)$_POST['enable_annotations'];
$enable_panorama_video = (int)$_POST['enable_panorama_video'];
$enable_ai_room = (int)$_POST['enable_ai_room'];
$enable_rooms_multiple = (int)$_POST['enable_rooms_multiple'];
$enable_rooms_protect = (int)$_POST['enable_rooms_protect'];
$enable_info_box = (int)$_POST['enable_info_box'];
$enable_context_info = (int)$_POST['enable_context_info'];
$enable_maps = (int)$_POST['enable_maps'];
$enable_icons_library = (int)$_POST['enable_icons_library'];
$enable_media_library = (int)$_POST['enable_media_library'];
$enable_music_library = (int)$_POST['enable_music_library'];
$enable_sound_library = (int)$_POST['enable_sound_library'];
$enable_password_tour = (int)$_POST['enable_password_tour'];
$enable_expiring_dates = (int)$_POST['enable_expiring_dates'];
$enable_export_vt = (int)$_POST['enable_export_vt'];
$enable_download_slideshow = (int)$_POST['enable_download_slideshow'];
$enable_statistics = (int)$_POST['enable_statistics'];
$enable_auto_rotate = (int)$_POST['enable_auto_rotate'];
$enable_flyin = (int)$_POST['enable_flyin'];
$enable_multires = (int)$_POST['enable_multires'];
$enable_shop = (int)$_POST['enable_shop'];
$enable_learning = (int)$_POST['enable_learning'];
$enable_dollhouse = (int)$_POST['enable_dollhouse'];
$enable_editor_ui = (int)$_POST['enable_editor_ui'];
$enable_custom_html = (int)$_POST['enable_custom_html'];
$enable_metatag = (int)$_POST['enable_metatag'];
$enable_loading_iv = (int)$_POST['enable_loading_iv'];
$enable_measurements = (int)$_POST['enable_measurements'];
$enable_multilanguage = (int)$_POST['enable_multilanguage'];
$enable_auto_translation = (int)$_POST['enable_auto_translation'];
$enable_poweredby = (int)$_POST['enable_poweredby'];
$enable_avatar_video = (int)$_POST['enable_avatar_video'];
$enable_import_export = (int)$_POST['enable_import_export'];
$enable_intro_slider = (int)$_POST['enable_intro_slider'];
$price = str_replace(",",".",strip_tags($_POST['price']));
$price2 = str_replace(",",".",strip_tags($_POST['price2']));
if(empty($price)) $price=0;
if($price<0) $price=0;
$price = (float)$price;
if(empty($price2)) $price2=0;
if($price2<0) $price2=0;
$price2 = (float)$price2;
$currency = strip_tags($_POST['currency']);
$custom_features = strip_tags($_POST['custom_features']);
$visible = (int)$_POST['visible'];
$external_url = strip_tags($_POST['external_url']);
$frequency = strip_tags($_POST['frequency']);
$interval_count = $_POST['interval_count'];
if(empty($interval_count)) $interval_count=1;
if($interval_count<1) $interval_count=1;
if($interval_count>12) $interval_count=12;
$interval_count = (int)$interval_count;
$customize_menu = strip_tags($_POST['customize_menu']);
$expire_tours = (int)$_POST['expire_tours'];
$button_type = $_POST['button_type'];
$button_text = strip_tags($_POST['button_text']);
$button_icon = strip_tags($_POST['button_icon']);
$override_template = (int)$_POST['override_template'];
$override_sample = (int)$_POST['override_sample'];
$id_vt_template = (int)$_POST['id_vt_template'];
if($id_vt_template==0) $id_vt_template=NULL;
$enable_sample = (int)$_POST['enable_sample'];
$id_vt_sample = strip_tags($_POST['id_vt_sample']);
$ai_generate_mode = strip_tags($_POST['ai_generate_mode']);
$autoenhance_generate_mode = strip_tags($_POST['autoenhance_generate_mode']);
$enable_autoenhance_room = (int)$_POST['enable_autoenhance_room'];
$n_gsv_publish_month = ($_POST['n_gsv_publish_month']=="") ? -1 : (int)$_POST['n_gsv_publish_month'];
$gsv_publish_mode = strip_tags($_POST['gsv_publish_mode']);
$enable_gsv_publish = (int)$_POST['enable_gsv_publish'];
$enable_custom_domain = (int)$_POST['enable_custom_domain'];
$n_custom_domain = ($_POST['n_custom_domain']=="") ? -1 : (int)$_POST['n_custom_domain'];
$price_label = strip_tags($_POST['price_label']);
$description = strip_tags($_POST['description']);
$show_features = (int)$_POST['show_features'];
$array_lang = json_decode($_POST['array_lang'],true);
$settings = get_settings();
if(($price>0) && ($settings['stripe_enabled'] || $settings['paypal_enabled']) && ($frequency=='recurring')) {
    $days = -1;
}
$query = "INSERT INTO svt_plans(price,price2,n_virtual_tours,n_virtual_tours_month,n_rooms,n_rooms_tour,n_markers,n_pois,n_gallery_images,days,create_landing,create_gallery,create_presentation,create_video360,enable_live_session,max_file_size_upload,max_storage_space,enable_chat,enable_voice_commands,enable_share,enable_device_orientation,enable_webvr,enable_logo,enable_nadir_logo,enable_song,enable_forms,enable_annotations,enable_panorama_video,enable_rooms_multiple,enable_rooms_protect,enable_info_box,enable_context_info,enable_maps,enable_icons_library,enable_media_library,enable_music_library,enable_password_tour,enable_expiring_dates,enable_statistics,enable_auto_rotate,enable_flyin,enable_multires,enable_meeting,create_showcase,create_globes,enable_export_vt,enable_shop,enable_dollhouse,enable_editor_ui,enable_custom_html,enable_metatag,enable_loading_iv,enable_measurements,visible,interval_count,expire_tours,name,currency,custom_features,external_url,frequency,customize_menu,enable_download_slideshow,create_video_projects,button_type,button_text,button_icon,enable_comments,enable_ai_room,n_ai_generate_month,enable_sound_library,enable_multilanguage,enable_auto_translation,enable_poweredby,override_template,override_sample,id_vt_template,enable_sample,id_vt_sample,enable_avatar_video,enable_import_export,ai_generate_mode,enable_intro_slider,enable_autoenhance_room,autoenhance_generate_mode,n_autoenhance_generate_month,n_gsv_publish_month,gsv_publish_mode,enable_gsv_publish,enable_custom_domain,n_custom_domain,position,enable_snapshot,enable_learning,price_label,description,show_features) 
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?); ";
if ($smt = $mysqli->prepare($query)) {
    $smt->bind_param('ddiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiissssssiisssiiiiiiiiiiisiisiisiisiiiiiissi',$price,$price2,$n_virtual_tours,$n_virtual_tours_month,$n_rooms,$n_rooms_tour,$n_markers,$n_pois,$n_gallery_images,$days,$create_landing,$create_gallery,$create_presentation,$create_video360,$enable_live_session,$max_file_size_upload,$max_storace_space,$enable_chat,$enable_voice_commands,$enable_share,$enable_device_orientation,$enable_webvr,$enable_logo,$enable_nadir_logo,$enable_song,$enable_forms,$enable_annotations,$enable_panorama_video,$enable_rooms_multiple,$enable_rooms_protect,$enable_info_box,$enable_context_info,$enable_maps,$enable_icons_library,$enable_media_library,$enable_music_library,$enable_password_tour,$enable_expiring_dates,$enable_statistics,$enable_auto_rotate,$enable_flyin,$enable_multires,$enable_meeting,$create_showcase,$create_globes,$enable_export_vt,$enable_shop,$enable_dollhouse,$enable_editor_ui,$enable_custom_html,$enable_metatag,$enable_loading_iv,$enable_measurements,$visible,$interval_count,$expire_tours,$name,$currency,$custom_features,$external_url,$frequency,$customize_menu,$enable_download_slideshow,$create_video_projects,$button_type,$button_text,$button_icon,$enable_comments,$enable_ai_room,$n_ai_generate_month,$enable_sound_library,$enable_multilanguage,$enable_auto_translation,$enable_poweredby,$override_template,$override_sample,$id_vt_template,$enable_sample,$id_vt_sample,$enable_avatar_video,$enable_import_export,$ai_generate_mode,$enable_intro_slider,$enable_autoenhance_room,$autoenhance_generate_mode,$n_autoenhance_generate_month,$n_gsv_publish_month,$gsv_publish_mode,$enable_gsv_publish,$enable_custom_domain,$n_custom_domain,$position,$enable_snapshot,$enable_learning,$price_label,$description,$show_features);
    $result = $smt->execute();
    if ($result) {
        $id = $mysqli->insert_id;
        save_input_langs($array_lang,'svt_plans_lang','id_plan',$id);
        ob_end_clean();
        echo json_encode(array("status"=>"ok","id"=>$id));
    } else {
        ob_end_clean();
        echo json_encode(array("status"=>"error"));
    }
} else {
    ob_end_clean();
    echo json_encode(array("status"=>"error"));
}