<?php
ob_start();

// TODO: ho dovuto aggiungere questo if altrimenti l'applicazione non partiva. Investigare
if(!headers_sent() && '' == session_id()) {
	header("Content-Type: text/html; charset=UTF-8");
	session_start();
}

$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$__start = $time;

ini_set("magic_quotes", "Off");
ini_set("register_globals", "Off");
ini_set('date.timezone', 'Europe/Rome');
error_reporting("E_ALL & ~E_NOTICE");

$script_filename = str_replace("\\", "/", __FILE__); // fix for Windows
$script_name = substr($script_filename, strrpos($script_filename, "/") + 1, 20);
$docroot = preg_replace("/\/$/", "", str_replace("\\", "/", $_SERVER['DOCUMENT_ROOT']));
$rootdir = str_replace($script_name, "", $script_filename);
$rootdir = substr(str_replace($docroot, "", $rootdir), 0, - 1);
$docroot = $docroot . $rootdir;

// short_open_tag deve essere a TRUE
if (ini_get("short_open_tag") == FALSE) {
	echo "<center>Devi impostare la variabile <b>short_open_tag</b> del tuo <b>php.ini</b> a <b><b>On</b></b>.</center>\n";
	exit();
}

// estensione gettext deve essere abilitata
if (! function_exists("gettext")) {
	echo "<center>L&rsquo;estensione gettext non &grave; abilitata. Devi attivare la funzione <b>gettext</b> per far funzionare correttamente GEST366</center>\n";
	exit();
}

if(file_exists(__DIR__."/config.inc.php")) include_once(__DIR__."/config.inc.php");

// Se manca il tema nel config carico quello di default
if (! isset($theme)) $theme = "default";

$lang = "it";
$jscript_modules = array();
$css_modules = array();

if (! isset($_SESSION['infos'])) {
	$_SESSION['infos'] = array();
}
if (! isset($_SESSION['errors'])) {
	$_SESSION['errors'] = array();
}
if (! isset($_SESSION['warnings'])) {
	$_SESSION['warnings'] = array();
}

include ($docroot . "/lib/dbo.class.php");
include ($docroot . "/lib/functions.php");
include ($docroot . "/lib/widgets.class.php");
include ($docroot . "/lib/photo.class.php");
include ($docroot . "/lib/htmlbuilder.php");
include ($docroot . "/lib/modulebuilder.php");
include ($docroot . "/lib/html-helpers.class.php");
include ($docroot . "/lib/autoload.php");
	include($docroot."/lib/class.phpmailer.php");
	include($docroot."/lib/PHPMailerAutoload.php");
	include($docroot."/lib/class.smtp.php");

register_shutdown_function("translateTemplate");

$version = getVersion();
$revision = getRevision();

$html = new HTMLHelper();

$dist = $rootdir . "/assets";
$css = $dist . "/css";
$js = $dist . "/js";
$img = $dist . "/img";

array_push($css_modules, $css . "/AdminLTE.min.css?v=" . $version);
array_push($css_modules, $css . "/switch.css?v=" . $version);
array_push($css_modules, $css . "/_all-skins.min.css?v=" . $version);
array_push($css_modules, $css . "/bootstrap.min.css?v=" . $version);
array_push($css_modules, $css . "/ges360_timeline.css" );
array_push($css_modules, array(
		"dir" => $css . "/font-awesome.min.css?v=" . $version,
		"media" => "all"
	));
array_push($css_modules, $css . "/jquery-ui.min.css?v=" . $version);
array_push($css_modules, $css . "/daterangepicker.min.css?v=" . $version);
array_push($css_modules, $css . "/timepicker.min.css?v=" . $version);
array_push($css_modules, $css . "/select2.min.css?v=" . $version);
array_push($css_modules, $css . "/select2-bootstrap.min.css?v=" . $version);
array_push($css_modules, $css . "/parsley.min.css?v=" . $version);
array_push($css_modules, $css . "/tooltipster.bundle.min.css?v=" . $version);
array_push($css_modules, $css . "/dataTables.bootstrap.min.css?v=" . $version);
array_push($css_modules, $css . "/scroller.bootstrap.min.css?v=" . $version);
array_push($css_modules, $css . "/style.min.css?v=" . $version);
array_push($css_modules, $css . "/themes.min.css?v=" . $version);
//array_push($css_modules, $css . "/bootstrap-datetimepicker.min.css?v=" . $version);



array_push($css_modules, array(
		"dir" => $css . "/print.min.css?v=" . $version,
		"media" => "print"
	));

array_push($jscript_modules, $js . "/jquery.min.js?v=" . $version);
array_push($jscript_modules, $js . "/bootstrap.min.js?v=" . $version);
array_push($jscript_modules, $js . "/jquery-ui.min.js?v=" . $version);
array_push($jscript_modules, $js . "/i18n/jquery-ui/datepicker-it.min.js?v=" . $version);
array_push($jscript_modules, $js . "/bootstrap-timepicker.min.js?v=" . $version);
array_push($jscript_modules, $js . "/jquery.dataTables.min.js?v=" . $version);
array_push($jscript_modules, $js . "/dataTables.scroller.min.js?v=" . $version);
array_push($jscript_modules, $js . "/select2.min.js?v=" . $version);
array_push($jscript_modules, $js . "/i18n/select2/it.min.js?v=" . $version);
array_push($jscript_modules, $js . "/parsley.min.js?v=" . $version);
array_push($jscript_modules, $js . "/i18n/parsleyjs/it.min.js?v=" . $version);
array_push($jscript_modules, $js . "/moment.min.js?v=" . $version);
array_push($jscript_modules, $js . "/daterangepicker.min.js?v=" . $version);
array_push($jscript_modules, $js . "/jquery.inputmask.bundle.min.js?v=" . $version);
array_push($jscript_modules, $js . "/autosize.min.js?v=" . $version);
array_push($jscript_modules, $js . "/app.min.js?v=" . $version);
array_push($jscript_modules, $js . "/jquery.slimscroll.min.js?v=" . $version);
array_push($jscript_modules, $js . "/tooltipster.bundle.min.js?v=" . $version);
array_push($jscript_modules, $js . "/sweetalert2.all.js");



array_push($jscript_modules, $rootdir . "/lib/functionsjs.php?v=" . $version);
if(isUserAutenticated()) array_push($jscript_modules, $rootdir . "/lib/init.js?v=" . $version);


// Imposto il periodo di visualizzazione record dal 01-01-yyy al 31-12-yyyy
if (! isset($_SESSION['period_start'])) {
	$_SESSION['period_start'] = date("Y") . "-01-01";
	$_SESSION['period_end'] = date("Y") . "-12-31";
}

if (isset($_GET['period_start'])) {
	$_SESSION['period_start'] = $html->form('period_start');
	$_SESSION['period_end'] = $html->form('period_end');
}

// devo verificare che esista il file config.inc.php per connettermi
if (isset($db_name)) {
	$dbo = new Dbo();
	$dbo_state = $dbo->connect();

	if ($dbo_state == true) {
		// Se sono ancora in fase di installazione, zz_modules non esiste ancora!
		$is_db_installed = $dbo->fetchNum("SHOW TABLES LIKE 'zz_modules'") > 0;
		if ($is_db_installed && isUserAutenticated()) {
			$id_module = filter('id_module');
			$id_record = filter('id_record');
			$id_modules = filter('id_modules');

			/*
			 * Creazione array con l'elenco dei moduli
			 * es. $modules['Anagrafiche']['nome_campo'];
			 */
			$rs = $dbo->fetchArray("SELECT * FROM `zz_modules` LEFT JOIN (SELECT `idmodule`, `permessi` FROM `zz_permessi` WHERE `idgruppo`=(SELECT `idgruppo` FROM `zz_utenti` WHERE `idutente`='".$_SESSION['idutente']."')) AS `zz_permessi` ON `zz_modules`.`id`=`zz_permessi`.`idmodule` LEFT JOIN (SELECT `idmodule`, `clause` FROM `zz_gruppi_modules` WHERE `idgruppo`=(SELECT `idgruppo` FROM `zz_utenti` WHERE `idutente`='".$_SESSION['idutente']."')) AS `zz_gruppi_modules` ON `zz_modules`.`id`=`zz_gruppi_modules`.`idmodule`");
			$modules_info = array();

			for($i = 0; $i < sizeof($rs); $i ++) {
				foreach ($rs[$i] as $name => $value) {
					if($name == "permessi" && (isAdminAutenticated() || $value == null)){
						if(isAdminAutenticated()) $value = 'rw';
						else $value = '-';
					}
					if($name != 'idmodule' && $name != 'updated_at' && $name != 'created_at' && $name != 'updated_by' && $name != 'created_by' && $name != 'clause') $modules_info[$rs[$i]['name']][$name] = $value;
					else if($name == 'clause') $additional_where[$rs[$i]['name']] = $value;
				}

				$modules_info[$rs[$i]['id']]['name'] = $rs[$i]['name'];

				if(isset($id_module) && $id_module == $rs[$i]['id']) {
					$module_name = $rs[$i]['name'];
					$pageTitle = ($rs[$i]['name2'] != '') ? $rs[$i]['name2'] : $rs[$i]['name'];
				}

			}

			$rs = $dbo->fetchArray("SELECT idanagrafica FROM zz_utenti WHERE idutente='".$_SESSION['idutente']."'");
			$user_idanagrafica = $rs[0]['idanagrafica'];

			$permissions =  array();
			if(isset($module_name)) array_push($permissions, $module_name);
			if(isset($id_modules)){
				$ids = explode(",", $id_modules);
				foreach ($ids as $id) {
					array_push($permissions, $modules_info[$id]['name']);
				}
				unset($id);
				unset($ids);
			}

			//Cache delle impostazioni
			$settings = array();
			$rs = $dbo->fetchArray("SELECT nome, valore FROM zz_impostazioni");

			for( $i=0; $i<sizeof($rs); $i++ ){
				$settings[ $rs[$i]['nome'] ] = $rs[$i]['valore'];
			}
		}

		include ($docroot . "/lib/permissions.php");

	}
	else if (strpos($_SERVER["SCRIPT_FILENAME"], "index.php") === false) redirect("index.php?op=logout");
}
else if (strpos($_SERVER["SCRIPT_FILENAME"], "index.php") === false) redirect("index.php?op=logout");


// Creazione widget
$Widget = new Widgets();

?>
