<?php
if(!file_exists("config/config.inc.php")) {
    header("Location: install/start.php");
} else {
    if(!file_exists("index.html")) {
        require_once('db/connection.php');
        $query = "SELECT code,name,meta_title,meta_description,meta_image,background_image,pwa_enable FROM sml_maps WHERE show_in_first_page=1 AND active=1 LIMIT 1;";
        $result = $mysqli->query($query);
        if($result) {
            if($result->num_rows==1) {
                $row = $result->fetch_array(MYSQLI_ASSOC);
                $code = $row['code'];
                $name = $row['name'];
                $pwa_enable = $row['pwa_enable'];
                if(empty($row['meta_title'])) {
                    $meta_title = $name;
                } else {
                    $meta_title = $row['meta_title'];
                }
                if(empty($row['meta_description'])) {
                    $meta_description = '';
                } else {
                    $meta_description = $row['meta_description'];
                }
                $background_image = $row['background_image'];
                if(empty($row['meta_image'])) {
                    $meta_image = $background_image;
                } else {
                    $meta_image = $row['meta_image'];
                }
                $html_meta_image = "";
                $html_meta_description = "";
                if(!empty($meta_image)) {
                    $html_meta_image = '<meta itemprop="image" content="viewer/content/'.$meta_image.'">
                                        <meta property="og:image" content="viewer/content/'.$meta_image.'" />
                                        <meta property="twitter:image" content="viewer/content/'.$meta_image.'">';
                }
                if(!empty($meta_description)) {
                    $html_meta_description = '<meta itemprop="description" content="'.$meta_description.'">
                                            <meta name="description" content="'.$meta_description.'"/>
                                            <meta property="og:description" content="'.$meta_description.'" />
                                            <meta property="twitter:description" content="'.$meta_description.'">';
                }
                $favicons = print_favicons_map($code);
                $manifest_html = '';
                if ($pwa_enable) {
                    $manifest = get_manifest($code);
                    if (!empty($manifest)) {
                        $manifest_html = <<<HTML_CODE
<script>
if (window.self === window.top) {
    document.addEventListener('DOMContentLoaded', function() {
        const script = document.createElement('script');
        script.src = "viewer/js/pwa-install.js?v=0.5.7";
        document.head.appendChild(script);
        document.body.insertAdjacentHTML(
            'beforeend',
            `<pwa-install id="pwa-install" use-local-storage="false" manifest-url="{$manifest}"></pwa-install>`
        );
    });
}
</script>
HTML_CODE;
                    }
                }
$html = <<<HTML_CODE
<html>
    <head>
        <title>$meta_title</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, maximum-scale=1, minimum-scale=1">
        $favicons
        <meta property="og:type" content="website">
        <meta property="twitter:card" content="summary_large_image">
        <meta property="og:url" content="viewer/index.php?code=$code">
        <meta property="twitter:url" content="viewer/index.php?code=$code">
        <meta itemprop="name" content="$meta_title">
        <meta property="og:title" content="$meta_title">
        <meta property="twitter:title" content="$meta_title">
        $html_meta_image
        $html_meta_description
        <style>
            html, body { margin: 0; padding: 0; overflow: hidden; }
            iframe { height: 100%; }
        </style>
    </head>
    <body>
        <iframe allowfullscreen allow="accelerometer; camera; display-capture; fullscreen; geolocation; gyroscope; magnetometer; microphone; midi; xr-spatial-tracking;" width="100%" height="100%" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="viewer/index.php?code=$code"></iframe>
        <script>
        if ('serviceWorker' in navigator) {
            navigator.serviceWorker.register('viewer/service-worker.js', {
                scope: 'viewer/'
            });
        }
        </script>
        $manifest_html
    </body>
</html>
HTML_CODE;
                echo $html;
                exit;
            }
        }

        $query = "SELECT code,name,meta_title,meta_description,meta_image,pwa_enable FROM sml_showcases WHERE show_in_first_page=1 LIMIT 1;";
        $result = $mysqli->query($query);
        if($result) {
            if($result->num_rows==1) {
                $row = $result->fetch_array(MYSQLI_ASSOC);
                $code_showcase = $row['code'];
                $name_showcase = $row['name'];
                $pwa_enable = $row['pwa_enable'];
                if(empty($row['meta_title'])) {
                    $meta_title = $name_showcase;
                } else {
                    $meta_title = $row['meta_title'];
                }
                $meta_description = $row['meta_description'];
                $meta_image = $row['meta_image'];
                $html_meta_image = "";
                $html_meta_description = "";
                if(!empty($meta_image)) {
                    $html_meta_image = '<meta itemprop="image" content="viewer/content/'.$meta_image.'">
                                        <meta property="og:image" content="viewer/content/'.$meta_image.'" />
                                        <meta property="twitter:image" content="viewer/content/'.$meta_image.'">';
                }
                if(!empty($meta_description)) {
                    $html_meta_description = '<meta itemprop="description" content="'.$meta_description.'">
                                            <meta name="description" content="'.$meta_description.'"/>
                                            <meta property="og:description" content="'.$meta_description.'" />
                                            <meta property="twitter:description" content="'.$meta_description.'">';
                }
                $url_iframe = "showcase/index.php?code=$code_showcase";
                $favicons = print_favicons_showcase($code_showcase);
                $manifest_html = '';
                if ($pwa_enable) {
                    $manifest = get_manifest_showcase($code_showcase);
                    if (!empty($manifest)) {
                        $manifest_html = <<<HTML_CODE
<script>
if (window.self === window.top) {
    document.addEventListener('DOMContentLoaded', function() {
        const script = document.createElement('script');
        script.src = "viewer/js/pwa-install.js?v=0.5.7";
        document.head.appendChild(script);
        document.body.insertAdjacentHTML(
            'beforeend',
            `<pwa-install id="pwa-install" use-local-storage="false" manifest-url="{$manifest}"></pwa-install>`
        );
    });
}
</script>
HTML_CODE;
                    }
                }
                $html = <<<HTML_CODE
<html>
    <head>
        <title>$meta_title</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, maximum-scale=1, minimum-scale=1">
        $favicons
        <meta property="og:type" content="website">
        <meta property="twitter:card" content="summary_large_image">
        <meta property="og:url" content="$url_iframe">
        <meta property="twitter:url" content="$url_iframe">
        <meta itemprop="name" content="$meta_title">
        <meta property="og:title" content="$meta_title">
        <meta property="twitter:title" content="$meta_title">
        $html_meta_image
        $html_meta_description
        <style>
            html, body { margin: 0; padding: 0; overflow: hidden; }
            iframe { height: 100%; }
        </style>
    </head>
    <body>
        <iframe allow="accelerometer; camera; display-capture; fullscreen; geolocation; gyroscope; magnetometer; microphone; midi; xr-spatial-tracking;" width="100%" height="100%" frameborder="0" scrolling="yes" marginheight="0" marginwidth="0" src="$url_iframe"></iframe>
        <script>
        if ('serviceWorker' in navigator) {
            navigator.serviceWorker.register('showcase/service-worker.js', {
                scope: 'showcase/'
            });
        }
        </script>
        $manifest_html
    </body>
</html>
HTML_CODE;
                echo $html;
                exit;
            }
        }
        header("Location: backend/login.php");
    } else {
        header("Location: index.html");
    }
}

function print_favicons_map($code) {
    $path = '';
    $version = time();
    $path_m = 'm_'.$code.'/';
    if (file_exists(dirname(__FILE__).'/favicons/m_'.$code.'/favicon.ico')) {
        $path = $path_m;
    } else if (file_exists(dirname(__FILE__).'/favicons/custom/favicon.ico')) {
        $path = 'custom/';
    }
    $path = "favicons/".$path;
    if (file_exists(dirname(__FILE__).'/favicons/m_'.$code.'/site.webmanifest')) {
        $manifest = '<link rel="manifest" href="favicons/'.$path_m.'site.webmanifest?v='.$version.'">';
    } else {
        $manifest = "";
    }
    $favicon16 = "";
    if (file_exists(dirname(__FILE__).'/favicons/'.$path_m.'/favicon-16x16.png')) {
        $favicon16 = '<link rel="icon" type="image/png" sizes="16x16" href="'.$path.'favicon-16x16.png?v='.$version.'">';
    }
    $favicon32 = "";
    if (file_exists(dirname(__FILE__).'/favicons/'.$path_m.'/favicon-32x32.png')) {
        $favicon32 = '<link rel="icon" type="image/png" sizes="32x32" href="'.$path.'favicon-32x32.png?v='.$version.'">';
    }
    $favicon96 = "";
    if (file_exists(dirname(__FILE__).'/favicons/'.$path_m.'/favicon-96x96.png')) {
        $favicon96 = '<link rel="icon" type="image/png" sizes="96x96" href="'.$path.'favicon-96x96.png?v='.$version.'">';
    }
    return '<link rel="apple-touch-icon" sizes="180x180" href="'.$path.'apple-touch-icon.png?v='.$version.'">
    '.$favicon16.$favicon32.$favicon96.'
    '.$manifest.'
    <link rel="mask-icon" href="'.$path.'safari-pinned-tab.svg?v='.$version.'" color="#ffffff">
    <link rel="shortcut icon" href="'.$path.'favicon.ico?v='.$version.'">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="msapplication-config" content="'.$path.'browserconfig.xml?v='.$version.'">
    <meta name="theme-color" content="#ffffff">';
}
function print_favicons_showcase($code) {
    $path = '';
    $version = time();
    $path_m = 's_'.$code.'/';
    if (file_exists(dirname(__FILE__).'/favicons/s_'.$code.'/favicon.ico')) {
        $path = 's_'.$code.'/';
    } else if (file_exists(dirname(__FILE__).'/favicons/custom/favicon.ico')) {
        $path = 'custom/';
    }
    $favicon16 = "";
    if (file_exists(dirname(__FILE__).'/favicons/'.$path_m.'/favicon-16x16.png')) {
        $favicon16 = '<link rel="icon" type="image/png" sizes="16x16" href="'.$path.'favicon-16x16.png?v='.$version.'">';
    }
    $favicon32 = "";
    if (file_exists(dirname(__FILE__).'/favicons/'.$path_m.'/favicon-32x32.png')) {
        $favicon32 = '<link rel="icon" type="image/png" sizes="32x32" href="'.$path.'favicon-32x32.png?v='.$version.'">';
    }
    $favicon96 = "";
    if (file_exists(dirname(__FILE__).'/favicons/'.$path_m.'/favicon-96x96.png')) {
        $favicon96 = '<link rel="icon" type="image/png" sizes="96x96" href="'.$path.'favicon-96x96.png?v='.$version.'">';
    }
    return '<link rel="apple-touch-icon" sizes="180x180" href="favicons/'.$path.'apple-touch-icon.png?v='.$version.'">
   '.$favicon16.$favicon32.$favicon96.'
    <link rel="manifest" href="favicons/'.$path_m.'site.webmanifest?v='.$version.'">
    <link rel="mask-icon" href="favicons/'.$path.'safari-pinned-tab.svg?v='.$version.'" color="#ffffff">
    <link rel="shortcut icon" href="favicons/'.$path.'favicon.ico?v='.$version.'">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="msapplication-config" content="favicons/'.$path.'browserconfig.xml?v='.$version.'">
    <meta name="theme-color" content="#ffffff">';
}
function get_manifest($code) {
    $version = time();
    $path_m = 'm_'.$code.'/';
    if (file_exists(dirname(__FILE__).'/favicons/m_'.$code.'/site.webmanifest')) {
        $manifest = 'favicons/'.$path_m.'site.webmanifest?v='.$version;
    } else {
        $manifest = "";
    }
    return $manifest;
}
function get_manifest_showcase($code) {
    $version = time();
    $path_m = 's_'.$code.'/';
    if (file_exists(dirname(__FILE__).'/favicons/s_'.$code.'/site.webmanifest')) {
        $manifest = 'favicons/'.$path_m.'site.webmanifest?v='.$version;
    } else {
        $manifest = "";
    }
    return $manifest;
}