<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
ob_start();
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");
if(empty($_POST['id_map'])) {
    die();
}
$id_map = (int)$_POST['id_map'];
$code = "";
$logo_exist = "";
$favicon_ok = 1;
$query = "SELECT code,logo FROM sml_maps WHERE id=$id_map LIMIT 1;";
$result = $mysqli->query($query);
if($result) {
    if($result->num_rows==1) {
        $row = $result->fetch_array(MYSQLI_ASSOC);
        $code = $row['code'];
        $logo_exist = $row['logo'];
    }
}
$id_user = $_POST['id_user'];
$name = strip_tags($_POST['name']);
$main_color_hex = strip_tags($_POST['main_color_hex']);
$map_style = $_POST['map_style'];
$map_style_satellite = $_POST['map_style_satellite'];
$map_type = $_POST['map_type'];
$quality_preset = strip_tags($_POST['quality_preset']);
$quality_resolution = strip_tags($_POST['quality_resolution']);
$update_while_animating = (int)$_POST['update_while_animating'];
$update_while_interacting = (int)$_POST['update_while_interacting'];
$load_tiles_while_animating = (int)$_POST['load_tiles_while_animating'];
$load_tiles_while_interacting = (int)$_POST['load_tiles_while_interacting'];
$preload_tiles = (int)$_POST['preload_tiles'];
$render_buffer = (int)$_POST['render_buffer'];
$logo = $_POST['logo'];
$logo_mobile = $_POST['logo_mobile'];
$maptiler_api = strip_tags($_POST['maptiler_api']);
$maptiler_api_satellite = strip_tags($_POST['maptiler_api_satellite']);
$ga_tracking_id = strip_tags($_POST['ga_tracking_id']);
$show_list = (int)$_POST['show_list'];
$limit_list = (int)$_POST['limit_list'];
$default_zoom = $_POST['default_zoom'];
$selected_zoom = $_POST['selected_zoom'];
$duration_zoom = (int)$_POST['duration_zoom'];
$density_color = $_POST['density_color'];
$enable_reviews = (int)$_POST['enable_reviews'];
$enable_add_photo = (int)$_POST['enable_add_photo'];
$viewer_auth_mode = max(0,min(2,(int)($_POST['viewer_auth_mode'] ?? 0)));
$add_marker_require_login = (int)($_POST['add_marker_require_login'] ?? 0);
$review_require_login = (int)($_POST['review_require_login'] ?? 0);
$add_photo_require_login = (int)($_POST['add_photo_require_login'] ?? 0);
$issue_report_require_login = (int)($_POST['issue_report_require_login'] ?? 0);
$live_tracking_require_login = (int)($_POST['live_tracking_require_login'] ?? 0);
$enable_directions = (int)$_POST['enable_directions'];
$enable_popup_directions = (int)$_POST['enable_popup_directions'];
$enable_streetview = (int)$_POST['enable_streetview'];
$enable_share = (int)$_POST['enable_share'];
$language = $_POST['language'];
$street_basemap = $_POST['url_street'];
$satellite_basemap = $_POST['url_sat'];
$street_maxzoom = (int)$_POST['street_maxzoom'];
$street_minzoom = (int)$_POST['street_minzoom'];
$satellite_maxzoom = (int)$_POST['satellite_maxzoom'];
$satellite_minzoom = (int)$_POST['satellite_minzoom'];
fix_zoom_levels($street_minzoom, $street_maxzoom);
fix_zoom_levels($satellite_minzoom, $satellite_maxzoom);
$cluster_distance = $_POST['cluster_distance'];
if($cluster_distance=="") $cluster_distance=25;
$cluster_animation = $_POST['cluster_animation'];
if($cluster_animation=="") $cluster_animation=300;
$density_color_tolerance = $_POST['density_color_tolerance'];
if(empty($density_color_tolerance)) $density_color_tolerance=1;
$cat_filter = $_POST['cat_filter'];
$nav_markers = $_POST['nav_markers'];
$enable_add_marker = (int)$_POST['enable_add_marker'];
$font_viewer = $_POST['font_viewer'];
$sheet_detail = $_POST['sheet_detail'];
$default_view = $_POST['default_view'];
$default_my_location = $_POST['default_my_location'];
$my_location_zoom = $_POST['my_location_zoom'];
$search_highlight = $_POST['search_highlight'];
$enable_search = (int)$_POST['enable_search'];
$enable_search_location = (int)$_POST['enable_search_location'];
$enable_list = (int)$_POST['enable_list'];
$enable_categories = (int)$_POST['enable_categories'];
$fit_categories = (int)$_POST['fit_categories'];
$enable_story = (int)$_POST['enable_story'];
$street_attributions = $_POST['street_attributions'];
$satellite_attributions = $_POST['satellite_attributions'];
$markers_size = $_POST['markers_size'];
$markers_color_hex = $_POST['markers_color_hex'];
$markers_icon_color_hex = $_POST['markers_icon_color_hex'];
if(empty($markers_icon_color_hex)) $markers_icon_color_hex="#ffffff";
$markers_id_icon_library = $_POST['markers_id_icon_library'];
$markers_icon = strip_tags($_POST['markers_icon']);
$markers_color_hex_hover = $_POST['markers_color_hex_hover'];
$markers_icon_color_hex_hover = $_POST['markers_icon_color_hex_hover'];
$order_by = $_POST['order_by'];
$weather = $_POST['weather'];
$cat_filter_type = $_POST['cat_filter_type'];
$no_category_sel_mode = (int)$_POST['no_category_sel_mode'];
$category_featured_mode = $_POST['category_featured_mode'];
$enable_globe = $_POST['enable_globe'];
$enable_popup = $_POST['enable_popup'];
$sheet_mobile = $_POST['sheet_mobile'];
$logo_height = (int) $_POST['logo_height'];
$logo_height_mobile = (int) $_POST['logo_height_mobile'];
$logo_link = strip_tags($_POST['logo_link']);
$pin_marker_size = $_POST['pin_marker_size'];
$pin_marker_color_hex = $_POST['pin_color_hex'];
$pin_marker_icon_color_hex = $_POST['pin_icon_color_hex'];
if(empty($pin_marker_icon_color_hex)) $pin_marker_icon_color_hex="#ff0000";
$pin_marker_id_icon_library = $_POST['pin_id_icon_library'];
$pin_marker_icon = strip_tags($_POST['pin_icon']);
$pin_marker_align = strip_tags($_POST['pin_marker_align']);
$pin_marker_enable = (int)$_POST['pin_marker_enable'];
$direction_map = strip_tags($_POST['direction_map']);
$streetview_map = strip_tags($_POST['streetview_map']);
$auto_start = (int)$_POST['auto_start'];
$loading_visible = (int)$_POST['loading_visible'];
$lazy_loading = max(0,min(2,(int)($_POST['lazy_loading'] ?? 0)));
$lazy_loading_max_markers = (int)($_POST['lazy_loading_max_markers'] ?? 3000);
if($lazy_loading_max_markers<1) $lazy_loading_max_markers=3000;
if($lazy_loading_max_markers>50000) $lazy_loading_max_markers=50000;
$loading_background_color = strip_tags($_POST['loading_background_color']);
$loading_text_color = strip_tags($_POST['loading_text_color']);
$background_image = strip_tags($_POST['background_image']);
$background_image_mobile = strip_tags($_POST['background_image_mobile']);
$search_radius_enabled = (int)$_POST['search_radius_enabled'];
$search_radius_m = (int)$_POST['search_radius_m'];
$default_extra_fields = $_POST['default_extra_fields'];
$default_am_fields = $_POST['default_am_fields'];
$default_am_placeholders = $_POST['default_am_placeholders'];
$share_providers = strip_tags($_POST['share_providers']);
$add_marker_fields_mandatory = strip_tags($_POST['add_marker_fields_mandatory']);
$enable_share_map = (int)$_POST['enable_share_map'];
$use_street_attributions = (int)$_POST['use_street_attributions'];
$use_satellite_attributions = (int)$_POST['use_satellite_attributions'];
$enable_map_street = (int)$_POST['enable_map_street'];
$enable_map_satellite = (int)$_POST['enable_map_satellite'];
$map_street_provider = strip_tags($_POST['map_street_provider']);
$map_satellite_provider = strip_tags($_POST['map_satellite_provider']);
$here_api = strip_tags($_POST['here_api']);
$here_api_satellite = strip_tags($_POST['here_api_satellite']);
$here_lang = strip_tags($_POST['here_lang']);
$here_lang_satellite = strip_tags($_POST['here_lang_satellite']);
$google_api = strip_tags($_POST['google_api']);
$google_api_satellite = strip_tags($_POST['google_api_satellite']);
$google_lang = strip_tags($_POST['google_lang']);
$google_lang_satellite = strip_tags($_POST['google_lang_satellite']);
$enable_intro = (int)$_POST['enable_intro'];
$intro_image = strip_tags($_POST['intro_image']);
$intro_params = $_POST['intro_params'];
$cookie_consent = (int)$_POST['cookie_consent'];
$unit_map = $_POST['unit_map'];
$marker_images_num = (int)$_POST['marker_images_num'];
$marker_images_compression = (int)$_POST['marker_images_compression'];
$marker_images_max_size = (int)$_POST['marker_images_max_size'];
$my_location_mode = strip_tags($_POST['my_location_mode']);
$street_basemap_overlay = strip_tags($_POST['street_basemap_overlay']);
$street_overlay_opacity= (float)$_POST['street_overlay_opacity'];
$satellite_basemap_overlay = strip_tags($_POST['satellite_basemap_overlay']);
$satellite_overlay_opacity= (float)$_POST['satellite_overlay_opacity'];
$info_enable = (int)$_POST['info_enable'];
$info_auto_open = (int)$_POST['info_auto_open'];
$info_icon = strip_tags($_POST['info_icon']);
$info_content = $_POST['info_content'];
$song = strip_tags($_POST['song']);
$song_link = strip_tags($_POST['song_link']);
$song_bg_volume = (float)$_POST['song_bg_volume'];
$song_autoplay = (int)$_POST['song_autoplay'];
$song_loop = (int)$_POST['song_loop'];
$tts_enable = (int)$_POST['tts_enable'];
$live_tracking = (int)$_POST['live_tracking'];
$live_tracking_interval = (int)$_POST['live_tracking_interval'];
$live_tracking_timeout = (int)$_POST['live_tracking_timeout'];
$live_tracking_only_admin = (int)$_POST['live_tracking_only_admin'];
if($live_tracking_interval==0) $live_tracking_interval=5000;
if($live_tracking_timeout<$live_tracking_interval){
    $live_tracking_timeout = $live_tracking_interval*2;
}
if($live_tracking_timeout==0) $live_tracking_timeout=10000;
$custom_html = htmlspecialchars_decode($_POST['custom_html']);
$marker_sel_zoom = (float)$_POST['marker_sel_zoom'];
$pwa_enable = (int)$_POST['pwa_enable'];
$include_sitemap = (int)$_POST['include_sitemap'];
$show_categories_marker = (int)$_POST['show_categories_marker'];
$id_categories = $_POST['id_categories'];
$openfreemap_street_url = strip_tags($_POST['openfreemap_street_url']);
$order_by_distance = (int)$_POST['order_by_distance'];
$intro_only_direct = (int)$_POST['intro_only_direct'];
$enable_issue_reports = (int)$_POST['enable_issue_reports'];
$default_prefix = strip_tags($_POST['default_prefix']);
if(empty($default_prefix)) $default_prefix = NULL;
$add_marker_multiple_category = (int)$_POST['add_marker_multiple_category'];
$languages_enabled = $_POST['languages_enabled'];
$address_format = $_POST['address_format'];
$add_markers_countries_allowed = $_POST['add_markers_countries_allowed'];
$enable_last_update = (int)$_POST['enable_last_update'];
$show_legal = (int)$_POST['show_legal'];
$show_scale_control = (int)$_POST['show_scale_control'];
$enable_measure = (int)$_POST['enable_measure'];
$wake_lock = (int)$_POST['wake_lock'];
$array_lang = json_decode($_POST['array_lang'],true);
$query = "UPDATE sml_maps SET id_user=?,languages_enabled=?,name=?,main_color_hex=?,map_style=?,logo=?,maptiler_api=?,ga_tracking_id=?,show_list=?,default_zoom=?,selected_zoom=?,density_color=?,markers_size=?,enable_reviews=?,enable_directions=?,enable_streetview=?,enable_share=?,language=?,street_basemap=?,satellite_basemap=?,street_maxzoom=?,satellite_maxzoom=?,cluster_distance=?,cat_filter=?,nav_markers=?,enable_add_marker=?,font_viewer=?,sheet_detail=?,default_view=?,default_my_location=?,my_location_zoom=?,density_color_tolerance=?,enable_search=?,enable_list=?,enable_categories=?,enable_story=?,enable_search_location=?,street_attributions=?,satellite_attributions=?,markers_icon=?,markers_id_icon_library=?,markers_color_hex=?,markers_icon_color_hex=?,search_highlight=?,order_by=?,weather=?,cat_filter_type=?,enable_globe=?,enable_popup=?,sheet_mobile=?,logo_height=?,logo_link=?,logo_mobile=?,logo_height_mobile=?,markers_color_hex_hover=?,markers_icon_color_hex_hover=?,pin_marker_icon=?,pin_marker_id_icon_library=?,pin_marker_size=?,pin_marker_color_hex=?,pin_marker_icon_color_hex=?,pin_marker_enable=?,pin_marker_align=?,direction_map=?,streetview_map=?,auto_start=?,loading_background_color=?,loading_text_color=?,background_image=?,background_image_mobile=?,map_type=?,search_radius_enabled=?,search_radius_m=?,default_extra_fields=?,default_am_fields=?,default_am_placeholders=?,share_providers=?,enable_share_map=?,maptiler_api_satellite=?,map_style_satellite=?,use_street_attributions=?,use_satellite_attributions=?,enable_map_street=?,enable_map_satellite=?,map_street_provider=?,map_satellite_provider=?,here_api=?,here_api_satellite=?,enable_intro=?,intro_image=?,intro_params=?,cookie_consent=?,google_api=?,google_api_satellite=?,here_lang=?,here_lang_satellite=?,google_lang=?,google_lang_satellite=?,unit_map=?,marker_images_num=?,fit_categories=?,my_location_mode=?,no_category_sel_mode=?,street_basemap_overlay=?,street_overlay_opacity=?,satellite_basemap_overlay=?,satellite_overlay_opacity=?,street_minzoom=?,satellite_minzoom=?,info_content=?,info_enable=?,info_icon=?,info_auto_open=?,song=?,song_bg_volume=?,tts_enable=?,live_tracking=?,live_tracking_interval=?,live_tracking_timeout=?,live_tracking_only_admin=?,song_autoplay=?,custom_html=?,cluster_animation=?,song_loop=?,marker_sel_zoom=?,pwa_enable=?,show_categories_marker=?,enable_popup_directions=?,openfreemap_street_url=?,order_by_distance=?,add_marker_fields_mandatory=?,intro_only_direct=?,enable_issue_reports=?,default_prefix=?,duration_zoom=?,limit_list=?,include_sitemap=?,song_link=?,marker_images_compression=?,marker_images_max_size=?,add_marker_multiple_category=?,category_featured_mode=?,address_format=?,loading_visible=?,enable_last_update=?,quality_preset=?,quality_resolution=?,update_while_animating=?,update_while_interacting=?,load_tiles_while_animating=?,load_tiles_while_interacting=?,preload_tiles=?,render_buffer=?,enable_add_photo=?,add_markers_countries_allowed=?,show_legal=?,enable_measure=?,show_scale_control=?,lazy_loading=?,lazy_loading_max_markers=?,wake_lock=? WHERE id=?;";
if($smt = $mysqli->prepare($query)) {
    $smt->bind_param('isssssssiiiidiiiisssiiiiiisisiiiiiiiisssississsiisississsidssisssisssssiissssissiiiissssississsssssiisisdsdiisisisdiiiiiisiidiiisisiisiiisiiissiissiiiiiiisiiiiiii',$id_user,$languages_enabled,$name,$main_color_hex,$map_style,$logo,$maptiler_api,$ga_tracking_id,$show_list,$default_zoom,$selected_zoom,$density_color,$markers_size,$enable_reviews,$enable_directions,$enable_streetview,$enable_share,$language,$street_basemap,$satellite_basemap,$street_maxzoom,$satellite_maxzoom,$cluster_distance,$cat_filter,$nav_markers,$enable_add_marker,$font_viewer,$sheet_detail,$default_view,$default_my_location,$my_location_zoom,$density_color_tolerance,$enable_search,$enable_list,$enable_categories,$enable_story,$enable_search_location,$street_attributions,$satellite_attributions,$markers_icon,$markers_id_icon_library,$markers_color_hex,$markers_icon_color_hex,$search_highlight,$order_by,$weather,$cat_filter_type,$enable_globe,$enable_popup,$sheet_mobile,$logo_height,$logo_link,$logo_mobile,$logo_height_mobile,$markers_color_hex_hover,$markers_icon_color_hex_hover,$pin_marker_icon,$pin_marker_id_icon_library,$pin_marker_size,$pin_marker_color_hex,$pin_marker_icon_color_hex,$pin_marker_enable,$pin_marker_align,$direction_map,$streetview_map,$auto_start,$loading_background_color,$loading_text_color,$background_image,$background_image_mobile,$map_type,$search_radius_enabled,$search_radius_m,$default_extra_fields,$default_am_fields,$default_am_placeholders,$share_providers,$enable_share_map,$maptiler_api_satellite,$map_style_satellite,$use_street_attributions,$use_satellite_attributions,$enable_map_street,$enable_map_satellite,$map_street_provider,$map_satellite_provider,$here_api,$here_api_satellite,$enable_intro,$intro_image,$intro_params,$cookie_consent,$google_api,$google_api_satellite,$here_lang,$here_lang_satellite,$google_lang,$google_lang_satellite,$unit_map,$marker_images_num,$fit_categories,$my_location_mode,$no_category_sel_mode,$street_basemap_overlay,$street_overlay_opacity,$satellite_basemap_overlay,$satellite_overlay_opacity,$street_minzoom,$satellite_minzoom,$info_content,$info_enable,$info_icon,$info_auto_open,$song,$song_bg_volume,$tts_enable,$live_tracking,$live_tracking_interval,$live_tracking_timeout,$live_tracking_only_admin,$song_autoplay,$custom_html,$cluster_animation,$song_loop,$marker_sel_zoom,$pwa_enable,$show_categories_marker,$enable_popup_directions,$openfreemap_street_url,$order_by_distance,$add_marker_fields_mandatory,$intro_only_direct,$enable_issue_reports,$default_prefix,$duration_zoom,$limit_list,$include_sitemap,$song_link,$marker_images_compression,$marker_images_max_size,$add_marker_multiple_category,$category_featured_mode,$address_format,$loading_visible,$enable_last_update,$quality_preset,$quality_resolution,$update_while_animating,$update_while_interacting,$load_tiles_while_animating,$load_tiles_while_interacting,$preload_tiles,$render_buffer,$enable_add_photo,$add_markers_countries_allowed,$show_legal,$enable_measure,$show_scale_control,$lazy_loading,$lazy_loading_max_markers,$wake_lock,$id_map);
    $result = $smt->execute();
    if($result) {
        $query_auth = "UPDATE sml_maps SET viewer_auth_mode=?,add_marker_require_login=?,review_require_login=?,add_photo_require_login=?,issue_report_require_login=?,live_tracking_require_login=? WHERE id=?;";
        if($smt_auth = $mysqli->prepare($query_auth)) {
            $smt_auth->bind_param('iiiiiii',$viewer_auth_mode,$add_marker_require_login,$review_require_login,$add_photo_require_login,$issue_report_require_login,$live_tracking_require_login,$id_map);
            $smt_auth->execute();
        }
        $mysqli->query("DELETE FROM sml_category_map_assoc WHERE id_map=$id_map;");
        foreach ($id_categories as $id_category) {
            $id_category = (int)$id_category;
            $mysqli->query("INSERT INTO sml_category_map_assoc(id_map,id_category) VALUES($id_map,$id_category);");
        }
        save_input_langs($array_lang,'sml_maps_lang','id_map',$id_map);
        $path = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR;
        if($logo!=$logo_exist) {
            if(file_exists($path . "favicons" . DIRECTORY_SEPARATOR . "m_$code")) {
                array_map('unlink', glob($path . "favicons" . DIRECTORY_SEPARATOR . "m_$code" . DIRECTORY_SEPARATOR ."*.*"));
                rmdir($path . "favicons" . DIRECTORY_SEPARATOR . "m_$code" . DIRECTORY_SEPARATOR);
            }
        }
        $favicon_ok = generate_favicons('map',$id_map);
        require_once("../../services/clean_images.php");
        ob_end_clean();
        echo json_encode(array("status"=>"ok","favicon"=>$favicon_ok));
    } else {
        ob_end_clean();
        echo json_encode(array("status"=>"error"));
    }
} else {
    ob_end_clean();
    echo json_encode(array("status"=>"error"));
}
