<?php

// Configuration
if (file_exists('config.php')) {
    require_once('config.php');
}

//if the flag for force https is on, do it// Force HTTPS for security 
if(defined('FORCE_HTTPS') && FORCE_HTTPS == 'on'){
    if($_SERVER["HTTPS"] != "on") {
        $pageURL = "Location: https://";
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
        header($pageURL);
    }
}

// Startup
require_once(DIR_SYSTEM . 'startup.php');

// Application Classes
require_once(DIR_SYSTEM . 'library/hov_user.php');
require_once(DIR_SYSTEM . 'library/game_server.php');
require_once(DIR_HOV_WEB . 'includes/hov-interface-api.php'); //the web features api
//require_once(DIR_HOV_WEB . 'includes/validated-interface-api.php'); //the external web features api, also req change in the hov_model to call the right class
require_once(DIR_HOV_WEB . 'includes/hov-api.php'); // the api for GS communication
require_once(DIR_MODEL . 'hov_model.php');
require_once(DIR_MODEL . 'common/main_model.php');

// Registry
$registry = new Registry();

// Loader
$loader = new Loader($registry);
$registry->set('load', $loader);

// Config
$config = new Config();
$registry->set('config', $config);

// Database
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$registry->set('db', $db);

$config->set('config_url', HTTP_SERVER);
$config->set('config_ssl', HTTPS_SERVER);

// Url
$url = new Url($config->get('config_url'), $config->get('config_secure') ? $config->get('config_ssl') : $config->get('config_url'));
$registry->set('url', $url);

// Log
$log = new Log($config->get('config_error_filename'));
$registry->set('log', $log);

function error_handler($errno, $errstr, $errfile, $errline) {
    global $log, $config;
//error_log("\r\nGlobal objects in error handler,  log: ".print_r($log,true)." - config: ".print_r($config,true)."\r\n");
    switch ($errno) {
        case E_NOTICE:
        case E_USER_NOTICE:
            $error = 'Notice';
            break;
        case E_WARNING:
        case E_USER_WARNING:
            $error = 'Warning';
            break;
        case E_ERROR:
        case E_USER_ERROR:
            $error = 'Fatal Error';
            break;
        default:
            $error = 'Unknown';
            break;
    }
    if(is_object($config)){
        if ($config->get('config_error_display')) {
            echo '<b>' . $error . '</b>: ' . $errstr . ' in <b>' . $errfile . '</b> on line <b>' . $errline . '</b>';
        }

        if ($config->get('config_error_log')) {
            $log->write('PHP ' . $error . ':  ' . $errstr . ' in ' . $errfile . ' on line ' . $errline);
        }
    }

    return true;
}

// Error Handler
set_error_handler('error_handler');

// Request
$request = new Request();
$registry->set('request', $request);

// Response
$response = new Response();
$response->addHeader('Content-Type: text/html; charset=utf-8');
$response->setCompression($config->get('config_compression'));
$registry->set('response', $response);

// Cache
$cache = new Cache();
$registry->set('cache', $cache);

// Session
$session = new Session();
$registry->set('session', $session);

// Document
$registry->set('document', new Document());

// Encryption
$registry->set('encryption', new Encryption($config->get('config_encryption')));

// User

//$registry->set('user', $user);

$registry->set('game_server', new GameServer($registry));


// Front Controller
$controller = new Front($registry);

// check logged in for certain pages
$controller->addPreAction(new Action('common/login/checkAuthenticated'));

// Router
if (isset($request->get['route'])) {
    $route = $request->get['route'];
    $action = new Action('common/'.$route);
    $page = $route;
} else {
    $action = new Action('common/play');
    $page = 'play';
}
$registry->set('page', $page);

// Dispatch
$controller->dispatch($action, new Action('common/error/not_found'));

// Output
$response->output();
?>