<?php

	ini_set('display_errors','on');
	error_reporting(E_ALL);// ^ E_DEPRECATED ^ E_NOTICE ^ E_WARNING);

	session_cache_expire( 43829 ); // 1 month
	session_start ();

	require __DIR__ . '/config.php';
	require LIB_DIR . '/request.php';
	require LIB_DIR . '/tenacity.php';
	require LIB_DIR . '/contact.php';
	require LIB_DIR . '/product.php';
	require LIB_DIR . '/sale.php';
	require LIB_DIR . '/DB.php';
	require LIB_DIR . '/money_format.php';

	Request::decode ();

	require LIB_DIR.'/account.php';

	if(!empty($_GET['controller'])) $_REQUEST['controller'] = $_GET['controller'];
	if(!empty($_GET['action'])) $_REQUEST['action'] = $_GET['action'];
	foreach ($_GET as $key => $value) {
		if(empty($_REQUEST[$key])) $_REQUEST[$key] = $value; //in php7 request does not contain get anymore
	}

	if( array_key_exists( 'username', $_REQUEST ) && array_key_exists( 'password', $_REQUEST )){
		if( Account::login( $_REQUEST['username'], $_REQUEST['password'] ) ){
			header( 'Location: /static/index' );
			exit();
		}
	}

	if( !Account::isValidSession( )){
		if( $_REQUEST['controller'] != 'static' && $_REQUEST['action'] != 'login' ){
			header( 'Location: /static/login' );
			exit( );
		}
	}

	if( empty($_REQUEST['controller']) ) $_REQUEST['controller'] = 'static';
	if( empty($_REQUEST['action']) ) $_REQUEST['action'] = 'index';

	if (is_file (CONTROLLER_DIR . '/' . basename ($_REQUEST['controller']).'.php')) {
		require CONTROLLER_DIR . '/' . basename ($_REQUEST['controller']).'.php';
	} else {
		die ('Controller '.basename ($_REQUEST['controller']).' does not exist');
	}
