<?php
session_start();

define('DS', '/');
define('ROOTDIR', __DIR__);
define('APP', 'Application');
/**
 * load all important files
 */
require_once __DIR__ ."/autoload.php";
require_once __DIR__ ."/vendor/autoload.php";
require_once  ROOTDIR . DS . APP . "/Config/config.php";

use Symfony\Component\Debug\Debug;
use SCF\Core\DI;
use SCF\Core\Database;
use SCF\Core\System;
use SCF\Core\Languages;
use SCF\Core\Templates;
use SCF\Core\Authentication;
use SCF\Core\UserManager;
use SCF\Core\AclManager;

Debug::enable('E_ALL & ~E_NOTICE');

/**
 * Create instances of important classes
 */

$di = DI::getInstance();
$di->set('db',  new Database($config['dbext'], $config['dbhost'], $config['dbuser'], $config['dbpass'], $config['dbname']), true);
$di->set('router', 'AltoRouter', true);
$di->set('lang', new Languages(DI::getInstance()->get('db'),'DE'), true);
$di->set('auth', new Authentication(DI::getInstance()->get('db')), true);


$settings = new System(DI::getInstance()->get('db'));
$router = DI::getInstance()->get('router');
$lang = DI::getInstance()->get('lang');
$auth = DI::getInstance()->get('auth');

/**
 * Check login if Login Form was sended
 */
if($_POST['sender'] == 1) {
    $auth->loginAction($_POST['admin_username'], $_POST['admin_password'], 0);
}
/**
 * Check Login with every Page Reload
 */
if($auth->isLogin()) {
    $admin = new UserManager(DI::getInstance()->get('db'), $_SESSION['scf_userId']);
    $di->set('acl', new AclManager(DI::getInstance()->get('db'), $admin->getValue('userGroup')), true);

}
else {
    $admin = new UserManager(DI::getInstance()->get('db'), '0');
    $di->set('acl', new AclManager(DI::getInstance()->get('db'), '0'), true);
}

$acl = DI::getInstance()->get('acl');
$tpl = new Templates($settings, $auth, $acl, $router, $lang, $admin);