<?php
// definizione delle costanti
include('config.php');

// plugins
require_once 'vendor/autoload.php';

// include basic classes
require 'classes/Tools.php';
require 'classes/Controller.php';
require 'classes/Db.php';
require 'classes/Configuration.php';
require 'classes/User.php';
require 'classes/Model.php';
require 'classes/Notification.php';
require 'classes/Common.php';

require 'includes/function.html_input.php';
	
// devo sistemare il routing
if(!headers_sent())
{
	// richiesta da backoffice
	session_start();
	$uri = $_REQUEST['r'];
} else {
	// richiesta da frontoffice
	$uri = substr($_SERVER['REQUEST_URI'], 1);
	$urls = explode('?', $uri);
	$uri = $urls[0];
}

setlocale(LC_ALL, 'it_IT.UTF-8');

$run = 0; // non eseguire il modello

$smarty = new Smarty();
$smarty->setTemplateDir(__DIR__ . '/models');

$smarty->caching = false;
$smarty->assign('root_dir', ROOT_DIR);
$smarty->assign('site_logo', SITE_LOGO);


Controller::setController($uri);

$model = Controller::getModel();
$method = Controller::getMethod();

$smarty->assign('model', $model);
$smarty->assign('method', $method);

$now=time();

$perms=array('5.91.30.160');

/*if (isset($_SESSION['username-tsrm']) && ($_SESSION['username-tsrm']=='info@mauroscript.it' || $_SESSION['username-tsrm']=='assistenza@swdweb.it')){
	$perms[]=$_SERVER['REMOTE_ADDR'];	
}*/

if (!in_array($_SERVER['REMOTE_ADDR'], $perms)){
	$message .= '<h2>Sito in costruzione</h2>';
	
	echo $message;
	exit();
}

if(file_exists(__DIR__ . '/models/'.$model.'/'.$model.'.php')) // se il modello esiste
{
	// autenticazione.
	if(isset($_COOKIE[COOKIE_NAME]) && Tools::CheckCookie($_COOKIE[COOKIE_NAME]) !== false)
		$_SESSION['username'] = Tools::CheckCookie($_COOKIE[COOKIE_NAME]);

	if(!isset($_SESSION['username']) || strlen($_SESSION['username']) < 4)
		$run = 0;

	// fine autenticazione
	user::SetUser($_SESSION['username']);
	$smarty->assign('immagine', User::GetImmagine());
	$smarty->assign('upload_dir', FRONTEND_UPLOAD_DIR);
	$smarty->assign('sede_di_lavoro', User::GetSedeDiLavoro());
	$smarty->assign('sede_di_lavoro_iscritto', User::GetDatiIscritto());
	$smarty->assign('iscritto_statoiscrizione', User::GetStatoIscrizione());
	$_SESSION['iscritto_statoiscrizione']=User::GetStatoIscrizione();
	$smarty->assign('n_iscrizione', User::GetNumeroIscrizione());

	if( User::Can(User::GetGroup(), $model, $method) ) // se il suo gruppo può eseguirlo
		$run = 1;

	if( User::Can('public', $model, $method) ) // se il modello è pubblico
		$run = 1;

	if($run == 1)
	{
		$smarty->assign('message', Tools::showMessage());
		$smarty->assign('username', User::GetUsername());
		$smarty->assign('group', User::GetGroup());
		$smarty->assign('user_id', User::GetId());
		$smarty->assign('nome_utente', User::GetNome());

		//includo il file
		include(__DIR__ . '/models/'.$model.'/'.$model.'.php');
		$t = new $model($model);

		// metodo
		if(method_exists($model, $method))
			$t->$method();
		else
			$smarty->display('basic/404.tpl');
		//exit();
	} else {
		header('Location: '.ROOT_DIR.'accedi');
	}

} else { // modello non esiste
	die('il modello non esiste.');
}
