<?

	$aliases_arr	= $_SERVER['REQUEST_URI'];
	$aliases_arrX 	= $_SERVER['REQUEST_URI'];
	$aliases_arr 	= explode("/", $_SERVER['REQUEST_URI']);
	$aliases_arrX 	= explode("?", $_SERVER['REQUEST_URI']);
	
	array_shift($aliases_arr);
	array_shift($aliases_arrX);
	
	$folder = $aliases_arr[0]; // Директория расположения системы
	$module = $aliases_arr[1]; // Модуль системы
	
	define('ROOT', $_SERVER['DOCUMENT_ROOT'] . '/' . $folder . '/');
	define('CRMLINK', '//'. $_SERVER['HTTP_HOST'] . '/' . $folder . '/');
	
	/* Файлы функций модулей */
	$fileA 		= ROOT . 'admin/func-admin.php';
	$fileT 		= ROOT . 'modules/tasks/func-tasks.php';
	$fileC 		= ROOT . 'modules/clients/func-clients.php';
	$fileD 		= ROOT . 'modules/deals/func-deals.php';
	$fileU 		= ROOT . 'modules/users/func-users.php';
	$fileN 		= ROOT . 'modules/notes/func-notebook.php';
	$fileE 		= ROOT . 'modules/events/func-events.php';
	$fileQ 		= ROOT . 'modules/users/func-qchat.php';
	$fileR 		= ROOT . 'modules/clients/func-requests.php';
	$fileLT 	= ROOT . 'modules/letters/func-letters.php';
	$fileS 		= ROOT . 'functions/func-install.php';
	$fileIn 	= ROOT . 'install.php';
	
	/* Файлы баз данных модулей */
	$baseS 		= ROOT . 'crmdata/database/settings.xml';
	$baseT 		= ROOT . 'crmdata/database/tasks.xml';
	$baseC 		= ROOT . 'crmdata/database/clients.xml';
	$baseD 		= ROOT . 'crmdata/database/deals.xml';
	$baseU 		= ROOT . 'crmdata/database/users.xml';
	$baseE 		= ROOT . 'crmdata/database/events.xml';
	$baseL 		= ROOT . 'crmdata/database/logs.xml';
	$baseQ 		= ROOT . 'crmdata/database/qchat.xml';
	$baseB 		= ROOT . 'crmdata/database/users-ipblock.xml';
	$baseP 		= ROOT . 'crmdata/database/payments.xml';
	$baseR 		= ROOT . 'crmdata/database/requests.xml';
	$baseV 		= ROOT . 'crmdata/database/vacations.xml';
	$baseLT 	= ROOT . 'crmdata/database/letters.xml';
	$baseTMPL 	= ROOT . 'crmdata/database/templs.xml';
	
	define("BASEC", $baseC);
	
	require_once( ROOT . 'functions/func-db-create.php');
	require_once( ROOT . 'functions/func-getip.php');
	
	/* Переменные настроек */
	$dom_set 	= new DOMDocument;
	$dom_set	->load($baseS);
	$xpath_set 	= new DOMXPath($dom_set);
	
	$nCRMsn 			= $xpath_set->query('/settings/registration')	->item(0)->getElementsByTagName("sn")->item(0)->nodeValue;
	$nCRMusername 		= $xpath_set->query('/settings/registration')	->item(0)->getElementsByTagName("username")->item(0)->nodeValue;
	$nCRMuserphone 		= $xpath_set->query('/settings/registration')	->item(0)->getElementsByTagName("userphone")->item(0)->nodeValue;
	$nCRMuseremail 		= $xpath_set->query('/settings/registration')	->item(0)->getElementsByTagName("useremail")->item(0)->nodeValue;
	$nCRMuserword 		= $xpath_set->query('/settings/registration')	->item(0)->getElementsByTagName("userword")->item(0)->nodeValue;
	$nCRMrules	 		= $xpath_set->query('/settings/registration')	->item(0)->getElementsByTagName("rules")->item(0)->nodeValue;
	$nCRMdesignview 	= $xpath_set->query('/settings/design')			->item(0)->getElementsByTagName("view")->item(0)->nodeValue;
	$nCRMdesigntheme	= $xpath_set->query('/settings/design')			->item(0)->getElementsByTagName("theme")->item(0)->nodeValue;
	$nCRMutheme			= $xpath_set->query('/settings/design')			->item(0)->getElementsByTagName("utheme")->item(0)->nodeValue;
	$nCRMgradmin 		= $xpath_set->query('/settings/titles')			->item(0)->getElementsByTagName("groupadmin")->item(0)->nodeValue;
	$nCRMgrmanager 		= $xpath_set->query('/settings/titles')			->item(0)->getElementsByTagName("groupmanager")->item(0)->nodeValue;
	$nCRMgruser 		= $xpath_set->query('/settings/titles')			->item(0)->getElementsByTagName("groupuser")->item(0)->nodeValue;
	$nCRMgrguest 		= $xpath_set->query('/settings/titles')			->item(0)->getElementsByTagName("groupguest")->item(0)->nodeValue;
	$nCRMdeals 			= $xpath_set->query('/settings/titles')			->item(0)->getElementsByTagName("deals")->item(0)->nodeValue;
	$nCRMoffers 		= $xpath_set->query('/settings/titles')			->item(0)->getElementsByTagName("offers")->item(0)->nodeValue;
	$nCRMureg 			= $xpath_set->query('/settings/permission')		->item(0)->getElementsByTagName("registration")->item(0)->nodeValue;
	$nCRMloadfile 		= $xpath_set->query('/settings/permission')		->item(0)->getElementsByTagName("loadfiles")->item(0)->nodeValue;
	$nCRMdate 			= $xpath_set->query('/settings/registration')	->item(0)->getElementsByTagName("userdate")->item(0)->nodeValue;
	$nCRMfirstpayment 	= $xpath_set->query('/settings/other')			->item(0)->getElementsByTagName("firstpayment")->item(0)->nodeValue;
	$nCRMfilesize 		= $xpath_set->query('/settings/other')			->item(0)->getElementsByTagName("filesize")->item(0)->nodeValue;
	$nCRMfiletype 		= $xpath_set->query('/settings/other')			->item(0)->getElementsByTagName("filetype")->item(0)->nodeValue;
	$nCRMdate 			= $xpath_set->query('/settings/registration')	->item(0)->getElementsByTagName("userdate")->item(0)->nodeValue;
	$nCRMmailstat 		= $xpath_set->query('/settings/mail')			->item(0)->getElementsByTagName("status")->item(0)->nodeValue;
	$nCRMmailhost 		= $xpath_set->query('/settings/mail')			->item(0)->getElementsByTagName("host")->item(0)->nodeValue;
	$nCRMmailport 		= $xpath_set->query('/settings/mail')			->item(0)->getElementsByTagName("port")->item(0)->nodeValue;
	$nCRMmailname 		= $xpath_set->query('/settings/mail')			->item(0)->getElementsByTagName("uname")->item(0)->nodeValue;
	$nCRMmailpass 		= $xpath_set->query('/settings/mail')			->item(0)->getElementsByTagName("upass")->item(0)->nodeValue;
	$nCRMdealaccess 	= $xpath_set->query('/settings/permission')		->item(0)->getElementsByTagName("dealacc")->item(0)->nodeValue;
	$nCRMdateFrom 		= DateTime::createFromFormat( 'd.m.Y', date("d.m.Y") );
	$nCRMdateTo 		= DateTime::createFromFormat( 'd.m.Y', $nCRMdate );
	
	$numLogs 			= 1000; // Максимальное количество логов
	
	$wb_url = ((!empty($_SERVER['HTTPS'])) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'].'/'.$folder.'/';
	
	if(isset($nCRMdesignview) AND $nCRMdesignview == 'widescreen'){ $designView = '-fluid';}

	if($nCRMmailstat == 'on'){
		require_once( ROOT . 'functions/PHPmailer/func-phpmailer.php');
		require_once( ROOT . 'functions/PHPmailer/func-smtp.php');
		require_once( ROOT . 'functions/PHPmailer/func-exception.php');
	}
	
	require_once( ROOT . 'functions/func-general.php');
	require_once( ROOT . 'functions/func-login.php');
	require_once( ROOT . 'functions/func-counters.php');
	
	/* Уведомления */
	$notices 			= array();
	$notices_er 		= array();