<?php require_once __DIR__.DIRECTORY_SEPARATOR.'common.php'; 

$scheme = define_url_scheme();
$mediaRoot = define_media_root();


if($myUser->preference('passwordTime')!='' && !$myUser->superadmin){
	$basepage = explode('?',$page);
	$basepage = $basepage[0];
	if(is_numeric($conf->get('password_delay')) && $basepage!="account.php" && ( (time() - ($conf->get('password_delay') * 86400) > $myUser->preference('passwordTime'))  )  ) header('location: account.php?error=Votre mot de passe est trop vieux, veuillez le renouveller ci dessous.');
}


//Redirige la home si définie dans les settings
if(($page=='index.php' || $page==basename(ROOT_URL) || $page=='') && !isset($_['module']) && !empty($conf->get('home_page')))
	header('location:'.$conf->get('home_page'));

$cssModule = array();
foreach(array('module','page','section') as $term){
	if(isset($_[$term])) $cssModule[]= preg_replace('/([^a-z])/i', '-',$term.'-'.$_[$term]);
}

if(!isset($_['admin_login']) && file_exists('enabled.maintenance') && !$myUser->connected()){
    $_['error'] ? header('Location: maintenance.php?error='.$_['error']) : header('Location: maintenance.php');
    exit();
} else {
	$mainMenu = array();
	Plugin::callHook("menu_main", array(&$mainMenu));
	uasort ($mainMenu , function($a,$b){return $a['sort']>$b['sort']?1:-1;});
	
	$userMenu = array();
	if ($myUser->connected()){
		Plugin::callHook("menu_user", array(&$userMenu));
		uasort ($userMenu , function($a,$b){return $a['sort']>$b['sort']?1:-1;});
	}
	?>
	<!DOCTYPE html>
	<html class="html <?php echo implode(' ',$cssModule); ?>" lang="fr">
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<meta name="description" content="">
		<meta name="author" content="">
		<!-- <link rel="shortcut icon" type="image/x-icon" href="img/favicon.ico"> -->
    	<link rel="shortcut icon" type="image/png" href="action.php?action=general_favicon_download" />
	
		<?php 
		$title=$myUser->connected() ? 'Accueil':'Connexion';
		foreach($mainMenu as $item):
			$title = (!empty($item['url']) && strpos($page, $item['url']) !== false) ? $item['label'] : $title;
		endforeach;
		foreach($userMenu as $item):
			$title = (isset($item['url']) && strpos($page, $item['url']) !== false) ? $item['label'] : $title;
		endforeach;
		?>
	
		<title><?php echo PROGRAM_NAME.' - '.$title ?></title>
	
		<!-- Bootstrap core CSS -->
		<link href="<?php echo $mediaRoot ?>/css/bootstrap.min.css" rel="stylesheet">
		<!-- Jquery ui -->
		<link rel="stylesheet" href="<?php echo $mediaRoot ?>/css/jquery-ui.min.css">
		<link rel="stylesheet" href="<?php echo $mediaRoot ?>/css/jquery.timepicker.min.css">
		<!-- Font awesome -->
		<link rel="stylesheet" href="<?php echo $mediaRoot ?>/css/fontawesome-all.min.css">
		<!-- Trumbowyg -->
		<link href="<?php echo $mediaRoot ?>/css/trumbowyg.min.css" rel="stylesheet">
		<link href="<?php echo $mediaRoot ?>/css/trumbowyg.table.css" rel="stylesheet">
		<link href="<?php echo $mediaRoot ?>/css/trumbowyg.colors.css" rel="stylesheet">
		<!-- Lato font-->
		<link href="https://fonts.googleapis.com/css?family=Lato:300,400" rel="stylesheet">
		<!-- Custom styles for this template -->
		<link href="<?php echo $mediaRoot ?>/css/main.css?v=<?php echo $cacheVersion; ?>" rel="stylesheet">
		<!-- Décommenter la ligne d'après pour override le thème général -->
		<link href="css/theme.css" rel="stylesheet">
		<!-- Plugin css files -->
		<?php echo Plugin::callCss($mediaRoot,$cacheVersion); ?>
	</head>
	
	<body>
		<!-- Fixed navbar -->
		<nav id="mainMenu" class="navbar navbar-expand-md navbar-dark fixed-top noPrint">
			<a class="navbar-brand" style="background-image: url('action.php?action=general_logo_download')" href="index.php"><?php echo $conf->get('show_application_name') ? PROGRAM_NAME : ''; ?></a>
			<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
				<div class="menu">
					<div class="menu-icon">
						<div class="menu-line menu-line-1"></div>
						<div class="menu-line menu-line-2"></div>
						<div class="menu-line menu-line-3"></div>
						<div class="menu-line menu-line-4"></div>
						<div class="menu-line menu-line-5"></div>
					</div>
				</div>
			</button>
			<div class="collapse navbar-collapse" id="navbarCollapse">
				<ul class="navbar-nav navbar-main">
					<?php foreach($mainMenu as $item): ?>
						<?php if (isset($item['label']) && $item['label'] == 'Réglages') $page = basename($_SERVER['PHP_SELF']); 
						$classes = isset($item['classes'])? $item['classes']: '';
						if(isset($item['url']) 
							&& ((!empty($item['url']) 
							&& strpos($page, $item['url']) !== false 
							&& $item['url'] != 'index.php') || $page == $item['url'])
							|| (isset($item['active']) && $item['active'] ==true)
						){
							$classes .= ' active';
						} 
						?>
						<li data-id="<?php echo isset($item['id'])?$item['id']:''; ?>"
							class="nav-item <?php echo $classes; ?> ">
							
							<a class="nav-link" 
								title="<?php echo isset($item['title'])?$item['title']:''; ?>" 
								target="<?php echo isset($item['target'])?$item['target']:''; ?>" 
								onclick="<?php echo isset($item['onclick'])?$item['onclick']:''; ?>" 
								<?php echo isset($item['url'])? 'href="'.$item['url'].'"':''; ?>>
								<?php echo (isset($item['icon'])?'<i class="'.$item['icon'].'"></i> ':'').'<span>'.(isset($item['label'])?$item['label']:'').'</span>'; ?>
								<?php echo isset($item['html'])? $item['html']:''; ?>
							</a>
						</li>
					<?php endforeach; ?>
				</ul>
				<?php Plugin::callHook("header", array()); ?>
				<div id="loginHeader" class="ml-auto text-right">
					
					<?php Plugin::callHook("login_header", array()); ?>
					<div class="dropdown user-dropdown-menu">
						<button class="btn btn-dark dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
							<?php if ($myUser->connected()): ?>
								<img src="<?php echo $myUser->getAvatar(); ?>" class="avatar-mini avatar-rounded avatar-login" title="<?php echo $myUser->fullName(); ?>">
							<?php else: ?>
							Non connecté - Connexion
							<?php endif;?>
						</button>
						<div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuButton">
	
							<?php if (!$myUser->connected()): ?>
							<div class="dropdown-item login-item" > 
								<form id="loginForm" method="post" action="action.php?action=login" class="form-inline mt-mb-0">
									<label for="login">Identifiant</label>
									<input name="login" id="login" maxlength="64" class="form-control w-100 pb-1" type="text" required="required" autofocus="true">
									<label for="password">Mot de passe</label> 
									<input data-type="password" name="password" id="password" class="form-control w-100" type="password" required="required">
									<div class="form-check w-100 mt-2 mb-1">
                                              <input class="form-check-input" data-type="checkbox" type="checkbox" id="rememberMe" name="rememberMe">
									      <label class="form-check-label" for="rememberMe">Se souvenir de moi</label>
									</div>
									<input class="btn btn-success btn-small w-100" type="submit" value="Connexion" onclick="toggle_preloader_to('#loginRequest', 'div.container-fluid');">
									<a href="account.lost.php" class="d-block w-100 text-center mt-2 mb-1">Mot de passe oublié ?</a>
								</form>
							</div>
							<?php else: ?>
								<div class="font-weight-bold text-primary p-2 text-center"><?php echo $myUser->fullName(); ?></div>
							<?php foreach($userMenu as $item): ?>
								<?php if(isset($item['custom'])):
									echo $item['custom'];
									else: ?>
									<a class="dropdown-item" href="<?php echo $item['url']; ?>">
										<?php echo (isset($item['icon'])?'<i class="'.$item['icon'].'"></i> ':'').$item['label']; ?>
									</a>
								<?php endif; ?>
							<?php endforeach; ?>
							<?php endif; ?>
						</div>
					</div>
				</div>
			</div>
		</nav>
	
		<!-- Begin page content -->
		<div class="container-fluid">
<?php } ?>