<?
//Vendor Autoloader
include(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/vendor/autoload.php');

include(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/mysyde/DcAutoloader.php');

require_once("mysyde_log.php");
$dcAutoloader = new DcAutoloader();
$dcAutoloader->register();

//Load environment variables from config if exists
$envDir = rtrim(dirname(dirname(__DIR__)), '/') . '/config';
if (is_dir($envDir)) {
    $dotenv = new \Dotenv\Dotenv($envDir);
    $dotenv->load();
}

extract_site_request_uri_variables();

function extract_site_request_uri_variables()
{
    $site_base_pattern = '#/mysyde/([^/]+)/([^/]+)/([^/]+/){0,1}([^/]+/){0,1}([^/]+/){0,1}([^/]+/){0,1}([^/]+/){0,1}#';
    $site_base_pattern_matches = [];
    preg_match($site_base_pattern, $_SERVER['REQUEST_URI'], $site_base_pattern_matches);
    if (
        array_key_exists(1, $site_base_pattern_matches)
        && !empty($site_base_pattern_matches[1])
        && array_key_exists(2, $site_base_pattern_matches)
        && !empty($site_base_pattern_matches[2])
    ) {
        $_GET['site'] = filter_var($site_base_pattern_matches[1], FILTER_SANITIZE_STRING);
        if (empty($_GET['site'])) {
            $_GET['site'] = get_site_code_from_domain();
        }
        $_GET['language'] = filter_var($site_base_pattern_matches[2], FILTER_SANITIZE_STRING);


        //Try to max category level 5 (index 3 to 8)
        $noOfPathSegments = count($site_base_pattern_matches);
        if ($noOfPathSegments > 6) {
            $noOfPathSegments = 6;
        }

        for ($i = 3; $i <= $noOfPathSegments; $i++) {
            $levelindex = $i - 2;
            $matchedPattern = array_key_exists($i, $site_base_pattern_matches) ? $site_base_pattern_matches[$i] : '';
            $code = filter_var(rtrim($matchedPattern, '/'), FILTER_SANITIZE_STRING);
            $_GET['level_' . $levelindex] = $code;
        }
    }
    if (empty($_GET['site'])) {
        $_GET['site'] = get_site_code_from_domain();
    }
}

function get_site_code_from_domain()
{
    $prepStatement = 'SELECT id,code FROM main_site WHERE site_url = :domain ORDER BY id LIMIT 1';

    $siteDomain = $_SERVER['SERVER_NAME'];
    $siteCode = '';
    $dbHost = getenv('MAIN_MYSQL_DB_HOST');
    $dbPort = (int)getenv('MAIN_MYSQL_DB_PORT');
    $dbUser = getenv('MAIN_MYSQL_DB_USER');
    $dbSchema = getenv('MAIN_MYSQL_DB_SCHEMA');
    $dbPass = getenv('MAIN_MYSQL_DB_PASS');

    try {
        $pdo = new \DynCom\mysyde\common\classes\PDOQueryWrapper($dbHost, $dbPort, $dbSchema, $dbUser, $dbPass);
        $params = [
            [':domain', $siteDomain, PDO::PARAM_STR],
        ];
        $pdo->setQuery($prepStatement);
        $pdo->prepareQuery();
        $pdo->bindParameters($params);
        $pdo->executePreparedStatement();
        $resArr = $pdo->getResultArray();
        if (array_key_exists(0, $resArr) && array_key_exists('id', $resArr[0]) && $resArr[0]['id'] > 0) {
            $siteCode = $resArr[0]['code'];
        }
    } catch (Exception $e) {
        //Do noting
    }
    return $siteCode;
}

$forgetError = '';
?>
<? header('Content-Type: text/html; charset=UTF-8') ?>
<? require_once("../common/common_functions.inc.php"); ?>
<? require_once((local_environment()) ? "../dc.config.php" : "../dc-server.config.php"); ?>
<? require_once("admin_functions.inc.php"); ?>
<? require_once("start.inc.php"); ?>
<? if (!$GLOBALS["visitor"]["admin_login"] && $_GET['action'] != 'forget_password' && $_GET['action'] != 'set_password') {
    die(require_once("login.inc.php"));
}
    if($_GET['action'] == 'forget_password'){
       
        die(require_once("forget_password.inc.php"));
        
    }
    if($_GET['action'] == 'set_password' && isset($_SESSION['user_email']) && isset($_SESSION['code_set'])){
       
        die(require_once("set_password.inc.php"));
    }
?>
<!DOCTYPE html>
<html lang="de">
<? require_once("../common/version_comment.inc.php"); ?>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title><?= admin_site_title() ?></title>
    <script type="text/javascript" src="/plugins/jquery/jquery-1.6.4.min.js"></script>
    <script type="text/javascript" src="/plugins/jquery/jquery-ui-1.8.16.custom.min.js"></script>
    <script type="text/javascript" src="/plugins/formdata/formdata.js"></script>
    <script type="text/javascript" src="/plugins/jquery/jquery.cookie.js"></script>
    <script type="text/javascript" src="/plugins/jQuery-actual/jquery.actual.min.js"></script>
    <script type="text/javascript"
        src="/mysyde/admin/admin.js?t=<?= filemtime($_SERVER['DOCUMENT_ROOT'] . '/mysyde/admin/admin.js')?>"></script>
    <script type="text/javascript" src="/plugins/ckeditor/ckeditor.js"></script>
    <script type="text/javascript" src="/plugins/ckeditor/adapters/jquery.js"></script>
    <script type="text/javascript" src="/plugins/ckfinder/ckfinder.js"></script>
    <script type="text/javascript" src="/plugins/dropzone/dropzone.js"></script>
    <script type="text/javascript" src="/layout/admin/js/bc.backend.js"></script>
    <script type="text/javascript" src="/plugins/jquery-nestedSortable/jquery.mjs.nestedSortable.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/chart.js@3.8.0/dist/chart.min.js"></script>
    <link rel="apple-touch-icon" sizes="180x180" href="/layout/admin/img/favicon/favicon.ico">
    <link rel="icon" type="image/png" sizes="32x32" href="/layout/admin/img/favicon/favicon.ico">
    <link rel="icon" type="image/png" sizes="16x16" href="/layout/admin/img/favicon/favicon.ico">
    <link rel="apple-touch-icon" sizes="180x180" href="/layout/admin/img/favicon/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="/layout/admin/img/favicon/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="/layout/admin/img/favicon/favicon-16x16.png">
    <link rel="manifest" href="/layout/admin/img/favicon/site.webmanifest">
    <meta name="msapplication-TileColor" content="#fff">
    <meta name="theme-color" content="#ffffff">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
    <link href="/plugins/jqueryui/css/metro/jquery-ui.css" rel="stylesheet" type="text/css" />
    <link
        href="/layout/admin/css/mysyde.css?t=<?= filemtime($_SERVER['DOCUMENT_ROOT'] . '/layout/admin/css/mysyde.css')?>"
        rel="stylesheet" type="text/css" />
    <link
        href="/layout/admin/css/responsive.css?t=<?= filemtime($_SERVER['DOCUMENT_ROOT'] . '/layout/admin/css/responsive.css')?>"
        rel="stylesheet" type="text/css" />
    <link href="/plugins/dropzone/dropzone.css" rel="stylesheet" type="text/css" />
</head>


<body class="<?php echo $_GET["level_1"].' '.$_GET["level_2"]?>">

    <div id="container">
    <button class="menu-toggle" 
                aria-controls="primary-navigation"
                aria-expanded="false">
          <span class="visually-hidden">Menu</span>
          <div class="hamburger" aria-hidden="true"></div>
        </button>
        <div id="header-wrapper">
            <div class="logo"><a title="MySyde Intranet" href="/mysyde"><img alt="MySyde Intranet Logo" src="/layout/admin/img/icons/MySyde_Intranet.png"></a></div>
            <div id="header">
                <div id="header_menu">
                    <? require_once("header.inc.php"); ?>
                </div>
            </div>

            <div id="menu" class="<?php echo $GLOBALS["menu_opened"] === true ? 'menu_opened' : ''; ?>">
            </div>
        </div>
        <div id="content" class="site">
            <? require_once("content.inc.php"); ?>

        </div>
        <div id="content-collection">
            <div id="overlay"></div>
            <div id="overlayWrapper">
                <div id="overlayContent"></div>
            </div>
        </div>
    </div>
    <?php function loadRepeater(){?>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"
        integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <script
        src="/plugins/jquery/jquery.form-repeater.js?time=<?= filemtime($_SERVER['DOCUMENT_ROOT'] . '/plugins/jquery/jquery.form-repeater.js')?>"
        type="text/javascript"></script>

    <?php } ?>
</body>


</html>
<? require_once("close.inc.php"); ?>