<?php

require_once 'ads.php';

    require_once 'mb.php';

    $_template_header = false;
    $_template_content = false;
    $_template_footer = false;

    /* Set maintenance mode page if site is offline and admin is not logged in */
    if ($config['maintenance_mode'] && !isset($_SESSION['isAdmin'])) {		ini_set('display_errors','off'); 
        $thisController = 'maintenance';
    /* ... otherwise search for existing controller file on a basis of current url */
    } else if (isset($_GET['controller']) && $_GET['controller'] != "" && file_exists("$basepath/controllers/control.$_GET[controller].php")) {
        $thisController = $_template = $_GET['controller'];
    /* ... finally set index controller, if no existing controllers found before */
    } else {
        $thisController = $_template = 'index';
    }
    
    /* Include controller file */
    require_once "$basepath/controllers/control.$thisController.php";

    /* Include metatags setup */
    include_once "$basepath/includes/inc.metatags.php";

    /* Load header template corresponding with current page (overall_header by the default or custom defined in controller) */
    if (is_string($_template_header)) {
        require_once $template_path . "/template.$_template_header.php";
    } else if ($_template_header === false) {
        require_once $template_path . "/template.overall_header.php";
    }

    /* Load content template corresponding with current page (or custom defined in controller, or 404 if no corresponding template found) */
    if (is_string($_template_content)) {
        require_once $template_path . "/template.$_template_content.php";
    } else if (isset($_template)) {
        require_once $template_path . "/template.$_template.php";
    } else {
        require_once $template_path . '/template.404.php';
    }

    /* Load footer template corresponding with current page (overall_footer by the default or custom defined in controller) */
    if (is_string($_template_footer)) {
        require_once $template_path . "/template.$_template_footer.php";
    } else if ($_template_footer === false) {
        require_once $template_path . "/template.overall_footer.php";
    }

    /* Show debugging output, if enabled in configuration */
    debugClosure();
?>