<?php

require_once dirname(__FILE__) . '/se-lib/bootstrap.php';

if (!Request::isHttps()) {
	header("Location: https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
	exit();
}

//session_save_path("./tmp") ;
session_start();

date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions

$errorReportingLevel = E_ALL & ~E_NOTICE;
error_reporting($errorReportingLevel);
ini_set('error_reporting', $errorReportingLevel);

#TEST $_SESSION['DEBUG'] = 3;// TODO: TEST
if (!isset($_SESSION['DEBUG'])) $_SESSION['DEBUG'] = 0;// set default value

if (file_exists(APP_PATH_CONFIG . DS . ".config_{$_SERVER['SERVER_NAME']}.php")) {
	require APP_PATH_CONFIG . DS . ".config_{$_SERVER['SERVER_NAME']}.php";
}
if (file_exists(APP_PATH_ROOT . DS . ".config.php")) include APP_PATH_ROOT . DS . ".config.php";

require_once APP_PATH_ROOT . "/superedit-SEF.php";
SEF('DEBUG_S');

S::init();// init session variables if not exists

if ($defaultRoute = Config::get('DEFAULT_ROUTE')) {
	$route = V::get('_route', '', $_REQUEST);
	if (empty($route)) {
		$_GET['_route'] = $defaultRoute;
		$_REQUEST['_route'] = $defaultRoute;
	}
}

User::auth();// die if not logged in

if (!V::get('SKIP_TIMER', '', $_REQUEST)) S::timeoutUpdate(true);// User is authorized - update logout time

try {
	$rootPostTask = V::get('_rootPostTask', '', $_REQUEST);
	if ($rootPostTask) Router::rootPostTask($rootPostTask);
} catch (Exception $e) {
	DBG::log($e);
	S::saveUserMessage(get_class($e), $e->getMessage());
}

if (Config::get('LEGACY_INIT_FUNCTIONS_OFF')) {
	$route = V::get('_route', '', $_REQUEST);
	if ($route) {
		Router::route($route);
	} else {
		UI::startHtml();
		UI::menu();
		UI::loadTemplate('defaultPage', []);
		UI::endHtml();
	}

	exit;
}


// LEGACY_INIT_FUNCTIONS:

$route = V::get('_route', '', $_REQUEST);
Router::route($route);

//STD INCLUDE 2010-01-14
require APP_PATH_ROOT . "/superedit-logistyka.php";

// register globals - legacy
$MENU_INIT = isset($_REQUEST['MENU_INIT'])? $_REQUEST['MENU_INIT'] : null;
$ARG1 = isset($_REQUEST['ARG1'])? $_REQUEST['ARG1'] : null;
$ARG1_VAL = isset($_REQUEST['ARG1_VAL'])? $_REQUEST['ARG1_VAL'] : null;

Lib::loadClass('UI');
if ('YES' != V::get('HEADER_NOT_INIT', '', $_REQUEST)) UI::gora();

if (!User::hasAccess('menu')) {
	UI::menu();
	UI::dol();
	exit;
}

SEF("TREEJS");

if ($_SESSION['DEBUG']) echo "FUNCTION_INIT()";
DETECT_TABLE_COLUMN();// TODO: $thiss tworzone na podstawie 'CURRENT_MENU' ustawianej w MENU_INIT, teraz raczej zbedne wywolanie
SEF('FUNCTION_INIT');
FUNCTION_INIT();
if ($_SESSION['DEBUG']) echo "MENU_INIT()";
SEF('MENU');
SEF('MENU_INIT');
MENU_INIT();

UI::dol();
if ($_SESSION['DEBUG']) {
	echo"<hr>_POST:\n<br><pre>";
	print_r($_POST);
	echo "<hr>_SESSION:\n<br>";
	print_r($_SESSION);
	echo"<hr>\n_GET:<br>\n";
	print_r($_GET);
	echo'</pre>';
}
