<?php
/**
* For the brave souls who get this far: You are the chosen ones,
* the valiant knights of programming who toil away, without rest,
* fixing our most awful code. To you, true saviors, kings of men,
* I say this: never gonna give you up, never gonna let you down,
* never gonna run around and desert you. Never gonna make you cry,
* never gonna say goodbye. Never gonna tell a lie and hurt you.
*
*                                          - by stackoverflow.com
*/
	
	
//http://www.chartjs.org/docs/latest/
//https://github.com/ThingEngineer/PHP-MySQLi-Database-Class
//TODO: filesystem cache: https://github.com/cosenary/Simple-PHP-Cache
//TODO: log parser: https://github.com/kassner/log-parser
//https://www.w3schools.com/howto/howto_css_overlay.asp
//http://www.chartjs.org/docs/latest/

$isCLI = defined('STDIN') ? true : false;

define ('BASE_ROOT', dirname(dirname(__FILE__)).'/');
define ('APP_DIR', BASE_ROOT.'App/');

define ('ROUTERS_DIR', APP_DIR.'Routers/');
define ('CLASSES_DIR', APP_DIR.'Classes/'); //Framework core
define ('APP_VENDOR_DIR', APP_DIR.'Vendor/'); //Additional classes

define ('CONTROLLER_DIR', APP_DIR.'Controllers/');
define ('VIEWS_DIR', APP_DIR.'Views/');
define ('LAYOUTS_DIR', APP_DIR.'Layouts/');
define ('FUNCTIONS_DIR', APP_DIR.'Functions/');
define ('CACHE_DIR', APP_DIR.'Cache/');

//define ('CONFIGS_DIR', APP_DIR.'Configs/');
define ('CONFIGS_DIR', dirname(APP_DIR).'/AppConfigs/');

define ('LOCALE_DIR', APP_DIR.'Locale');

define ('VENDOR_DIR', BASE_ROOT.'vendor/'); //For composer, etc...
define ('RESOURCE_DIR', BASE_ROOT.'resource/');

define ('PUBLIC_HTML', dirname(dirname(__FILE__)).'/public_html/');
define ('PUBLIC_FILES', PUBLIC_HTML.'public_files/');
define ('AVATAR_IMG_DIR', PUBLIC_FILES.'avatars/');
define ('MEDIA_DIR', dirname(PUBLIC_HTML).'/media/');
define ('MEDIA_TMP_DIR', dirname(PUBLIC_HTML).'/media/tmp/');
define ('DATA_DIR', dirname(PUBLIC_HTML).'/data/');
define ('DATA_TMP_DIR', dirname(PUBLIC_HTML).'/data/tmp/');
define ('ATTACH_DIR', dirname(PUBLIC_HTML).'/data/files/');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require VENDOR_DIR.'autoload.php';
require CONFIGS_DIR.'autoload.php';
require CONFIGS_DIR.'config.php';

//Ensure the server time is Europe/Rome
date_default_timezone_set($config['settings']['default-timezone']);

require FUNCTIONS_DIR.'functions.php';

if (!is_writable(CACHE_DIR)) {
	echo '"Cache" directory (and its subdirectories) must be writable.';
	exit();
}

if (!is_writable(PUBLIC_FILES)) {
	echo '"public_files" directory (and its subdirectories) must be writable.';
	exit();
}

$GLOBALS['db'] = new \MysqliDb($config['db']['host'], $config['db']['user'], $config['db']['pass'], $config['db']['dbname']);
$GLOBALS['mailer'] = new PHPMailer(true); 

define('SUPERADMIN_ROLE_ID', 1);
define('ADMIN_ROLE_ID', 2);
define('MODERATOR_ROLE_ID', 3);
define('REFERRER_ROLE_ID', 4);
define('APPLICANT_ROLE_ID', 5);
define('GUEST_ROLE_ID', 6);
define('GLOBAL_MANAGER_ID', 8);
define('MANAGER_ID', 9);
define('STATUS_TECH_ID', 2);

if (!$isCLI) {
	
	//Do not change this order!!
	
	$app = new \Slim\App($config);

	require CONFIGS_DIR.'sessions.php';
	$GLOBALS['session'] = new Session();

	$GLOBALS['memoryCache'] = new MemoryCache();
	$GLOBALS['security'] = new Security();
	$GLOBALS['cookie'] = new Cookie();
	$GLOBALS['user'] = new User();
	$GLOBALS['locale'] = new l18n();
	$GLOBALS['layout'] = new Layout();
	$GLOBALS['logger'] = new Logger();
	
	/// ROUTERS ///
	include ROUTERS_DIR.'router.php';
}