<?php
session_start();
if (array_key_exists("ugly_debug", $_GET)) {
    $_SESSION['ugly_debug'] = $_GET['ugly_debug'];
}
if (array_key_exists("ugly_debug", $_SESSION)) {
    ini_set('display_errors', 1);
    error_reporting(-1);
}
date_default_timezone_set("Europe/Berlin");

$_SESSION['startTime'] = microtime(true);

/*
 * Start of localization system.
 */
if (array_key_exists("language", $_GET)) {
    $language = $_GET['language'];
} else if (array_key_exists("language", $_SESSION)) {
    $language = $_SESSION['language'];
} else {
    $language = "French";
}


if (! include_once './includes/localization/' . $language . 'Localization.inc.php') {
    $language = "French";
    include_once './includes/localization/' . $language . 'Localization.inc.php';
}

$_SESSION['language'] = $language;

/*
 * Insert modules following the array of types
*/
$modulesA = array(array('Menu', '0'), array('User', ''));

$modules = array();

foreach ($modulesA as $module) {
    $moduleType = $module[0];
    $moduleArgs = $module[1];
    if (! @include_once './modules/' . $moduleType . 'Module.inc.php') {
        break;
    }
    $moduleClass = $moduleType . "Module";
    $module = new $moduleClass($moduleArgs);

    if ($module instanceof ModuleInterface) {
        $modules[] = $module;
    }
}

/*
 * Include the page following $_GET['page']
*/
if (array_key_exists("page", $_GET) && ($_GET['page'])) {
    $pageType = $_GET['page'];
} else {
    $pageType = 'Index';
}

if (! @include_once './pages/' . $pageType . 'Page.inc.php') {
    include_once './pages/IndexPage.inc.php';
    $pageType = 'Index';
}

$pageClass = $pageType . 'Page';

try {
    $page = new $pageClass;
} catch (AccessDeniedException $e) {
    include_once './pages/ErrorPage.inc.php';
    $page = new ErrorPage();
}

if (!($page instanceof PageInterface)) {
    include_once './pages/IndexPage.inc.php';
    $page = new PageIndex();
}

include_once './pages/FooterPage.inc.php';
$footer = new FooterPage();

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="basicStyle.css" >
        <title><?php echo $page->getTitle() . " - " . Configuration::siteName; ?></title>
        <?php if (method_exists($page, "getHeaderEntry")) {
            echo $page->getHeaderEntry();
            echo "\n";
            } ?>
    </head>
    <body>
        <?php

        echo "<div id='wrap'>\n";

        echo "<div id='header'>\n";
        echo "<h1>" . $page->getTitle() . " - " . Configuration::siteName . "</h1>\n";
        echo "</div>\n";

        echo "<div id='sidebar'>\n";
        foreach ($modules as $module) {
            echo $module->getContent();
        }
        echo "</div>\n";

        echo "<div id='main'>\n";
        echo $page->getContent();
        echo "</div>\n";

        echo "<div id='footer'>\n";
        echo $footer->getContent();
        echo "</div>\n";

        echo "</div>\n";
        ?>
    </body>
</html>
