<?php
session_start();
	ini_set('error_reporting', E_ALL);
	ini_set('display_errors', 0);
	ini_set('display_startup_errors', 0);

require_once('./functions/crm_settings.php');
require_once('./functions/crm_version.php');

if (file_exists($fileIn) OR $nCRMsn == ''){
	include_once($fileIn);
} else {
	if ($auth->isAuth() and $sessionIPCheck->length < 1 AND $_SESSION["folder"] == $folder OR $aliases_arr[1] =='workbox-info' OR $aliases_arr[1] =='donate') {
		if ($nCRMsn != '' and $nCRMdateTo > $nCRMdateFrom) {
			switch(TRUE) {
				/**/
				case($aliases_arr[1] =='notes' OR $aliases_arr[1] ==''): {
					if (file_exists($baseT) and file_exists($fileT)) { include_once($fileT); }
					if (file_exists($fileN)) { include_once($fileN); }
					include_once(ROOT . 'modules/main/page-main.php');
				}
				/**/
				case($aliases_arr[1] ==''): {
					if (file_exists($baseT) and file_exists($fileT)) { include_once($fileT); }
					if (file_exists($fileN)) { include_once($fileN); }
					include_once(ROOT . 'modules/main/page-main.php');
				}
				break;
				/**/
				case($aliases_arr[1] =='admin'): {
					if (file_exists($fileA)) { include_once($fileA); }
					include_once(ROOT . 'admin/index.php');
				}
				break;
				/**/
				case($aliases_arr[1] =='_service'): {
					if ($GroupCheck == 'admin') {
						include_once(ROOT . 'modules/_service.php');
					} else {
						include_once(ROOT . 'modules/access-denied.php');
					}
				}
				break;
				/**/
				case($aliases_arr[1] =='search'): {
						include_once(ROOT . 'modules/main/page-search.php');
				}
				break;
				/**/
				case($aliases_arr[1] == '' OR $aliases_arr[1] == $folder): {
					if ($GroupCheck == 'admin') {
						header('Location: /' . $folder . '/admin');
						exit;
					}
					if (file_exists($baseT) and file_exists($fileT)) { include_once($fileT); }
					if (file_exists($fileN)) { include_once($fileN); }
					include_once(ROOT . 'modules/main/page-main.php');
				}
				break;
				/**/
				case($aliases_arr[1] =='tasks'): {
					if ($GroupCheck == 'admin' or $GroupCheck == 'manager' or isset($_GET['new']) or isset($_GET['view']) or isset($_GET['edit'])) {
						if (file_exists($baseT) and file_exists($fileT)) { include_once($fileT); }
						include_once(ROOT . 'modules/tasks/page-tasks.php');
					} else {
						include_once(ROOT . 'modules/access-denied.php');
					}
				}
				break;
				/**/
				case($aliases_arr[1] =='tasks-archive'): {
					if ($GroupCheck == 'admin' or $GroupCheck == 'manager') {
						if (file_exists($baseT) and file_exists($fileT)) { include_once($fileT); }
						include_once(ROOT . 'modules/tasks/page-tasks-archive.php');
					} else {
						include_once(ROOT . 'modules/access-denied.php');
					}
				}
				break;
				/**/
				case($aliases_arr[1] =='timeline'): {
					if (file_exists($baseT) and file_exists($fileT)) { include_once($fileT); }
					include_once(ROOT . 'modules/tasks/page-timeline.php');
				}
				break;
				/**/
				case($aliases_arr[1] =='clients'): {
					if ($GroupCheck == 'admin' or $GroupCheck == 'manager') {
						if (file_exists($baseC) and file_exists($fileC)) { include_once($fileC); }
						include_once(ROOT . 'modules/clients/page-clients.php');
					} else {
						include_once(ROOT . 'modules/access-denied.php');
					}
				}
				break;
				/**/
				case($aliases_arr[1] =='requests'): {
					if ($GroupCheck == 'admin' or $GroupCheck == 'manager') {
						if (file_exists($baseR) and file_exists($fileR)) { include_once($fileR); }
						include_once(ROOT . 'modules/clients/page-requests.php');
					} else {
						include_once(ROOT . 'modules/access-denied.php');
					}
				}
				break;
				/**/
				case($aliases_arr[1] =='deals'): {
					if (!strstr($nCRMdealaccess,$LoginCheck)) {
						include_once(ROOT . 'modules/access-denied.php');
					} else {
						if (file_exists($baseD) and file_exists($fileD)) { include_once($fileD); }
						include_once(ROOT . 'modules/deals/page-deals.php');
					}
				}
				break;
				/**/
				case($aliases_arr[1] =='payments'): {
					if (!strstr($nCRMdealaccess,$LoginCheck)) {
						include_once(ROOT . 'modules/access-denied.php');
					} else {
						if (file_exists($baseD) and file_exists($fileD)) { include_once($fileD); }
						include_once(ROOT . 'modules/deals/page-payments.php');
					}
				}
				break;
				/**/
				case($aliases_arr[1] =='vacations'): {
					if (file_exists($baseU) and file_exists($fileU)) { include_once($fileU); }
					include_once(ROOT . 'modules/users/page-vacations.php');
				}
				break;
				/**/
				case($aliases_arr[1] =='deals-archive'): {
					if (!strstr($nCRMdealaccess,$LoginCheck)) {
						include_once(ROOT . 'modules/access-denied.php');
					} else {
						if (file_exists($baseD) and file_exists($fileD)) { include_once($fileD); }
						include_once(ROOT . 'modules/deals/page-deals-archive.php');
					}
				}
				break;
				/**/
				case($aliases_arr[1] =='users'): {
					if (file_exists($baseU) and file_exists($fileU)) { include_once($fileU); }
					include_once(ROOT . 'modules/users/page-users.php');
				}
				break;
				/**/
				case($aliases_arr[1] =='letters'): {
					if (file_exists($baseLT) and file_exists($fileLT)) { include_once($fileLT); }
					include_once(ROOT . 'modules/letters/page-letters.php');
				}
				break;
				/**/
				case($aliases_arr[1] =='events'): {
					if (file_exists($baseE) and file_exists($fileE)) { include_once($fileE); }
					include_once(ROOT . 'modules/events/page-events.php');
				}
				break;
				/**/
				case($aliases_arr[1] =='statistic'): {
					if (!strstr($nCRMdealaccess,$LoginCheck)) {
						include_once(ROOT . 'modules/access-denied.php');
					} else {
						include_once(ROOT . 'modules/statistic/page-statistic.php');
					}
				}
				break;
				/**/
				case($aliases_arr[1] =='workbox-info'): {
					include_once(ROOT . 'modules/workbox-info.php');
				}
				break;
				/**/
				case($aliases_arr[1] =='donate'): {
					include_once(ROOT . 'modules/donate.php');
				}
				break;
				/**/
				case($aliases_arr[1] =='qchat'): {
					include_once(ROOT . 'modules/users/page-qchat.php');
				}
				break;
				/**/
				case($aliases_arr[1] =='noaccess'): {
					include_once(ROOT . 'modules/access-denied.php');
				}
				break;
				/**/
				default: {
					include_once(ROOT . 'modules/access-denied.php');
				}
				break;
			}
		} else {
			include_once(ROOT . 'modules/expired-license.php');
		}
	} else {
		require_once(ROOT . 'templates/login-form.php');
	} 
}
switch(TRUE) {
	case($aliases_arr[1] =='workbox-info'): {
		include_once(ROOT . 'modules/workbox-info.php');
	}
	break;
	case($aliases_arr[1] =='donate'): {
		include_once(ROOT . 'modules/donate.php');
	}
	break;
}
/**/

if(isset($_GET['install']) AND isset($_GET['sn']) AND isset($_GET['sword']) AND $_GET['sn'] !='' AND $_GET['sword'] !=''){
	$dom_install = new DOMDocument;
	$dom_install->load($baseS);
	$xpath_install = new DOMXPath($dom_install);
	
	$crm_profile = $xpath_install->query('/settings/registration[sn="'.$_GET['sn'].'" and userword="'.$_GET['sword'].'"]');
	if($crm_profile->length > 0){
		$install_file = ROOT . 's_'. md5(md5($_SERVER["SERVER_NAME"]).md5($folder)) . '.php';
		if(file_exists($baseS)){unlink($baseS);}  
		if(file_exists($install_file)){rename($install_file, ROOT . 'install.php');};
		header('Location:/' . $folder . '/');
		exit;
	}
	
	
}