<?php 
/**
* @version		$Id: offline.php 2657 2025-07-20 07:57:11Z IOS $
* @package		Elxis
* @subpackage	Templates / System
* @copyright	Copyright (c) 2006-2025 Elxis CMS (https://www.elxis.org). All rights reserved.
* @license		Elxis Public License ( https://www.elxis.org/elxis-public-license.html )
* @author		Elxis Team ( https://www.elxis.org )
* @description 	Elxis CMS is free software. Read the license for copyright notices and details
*/

defined('_ELXIS_') or die ('Direct access to this location is not allowed');

$rtlsfx = ($eLang->getinfo('DIR') == 'rtl') ? '-rtl' : '';
if (preg_match('#(\.png)$#i', $page->favicon)) {
	$favrel = 'rel="icon" type="image/png"';
} else {
	$favrel = 'rel="shortcut icon"';
}

$token = trim(eFactory::getSession()->get('token_loginform')); //check if another login module has already set the token
if ($token == '') {
	$token = md5(uniqid(rand(), true));
	eFactory::getSession()->set('token_loginform', $token);
}
$mts = filemtime(ELXIS_PATH.'/templates/system/css/standard'.$rtlsfx.'.css');
$mtx = filemtime(ELXIS_PATH.'/templates/system/css/exit'.$rtlsfx.'.css');
$mtj = filemtime(ELXIS_PATH.'/includes/js/elxis.js');
$site_url = $elxis->makeURL('', '', true);
$site_inurl = $elxis->makeURL('', 'inner.php', true);
$twofa = ($elxis->getConfig('TWOFA') != '') ? 1 : 0;

$vtip_txt_generic = sprintf($eLang->get('VERIFY_CODE_TIP'), '@@@');

switch ($elxis->getConfig('TWOFA')) {
	case 'sms': $vtip_txt = sprintf($eLang->get('VERIFY_CODE_TIP'), 'SMS'); break;
	case 'email': $vtip_txt = sprintf($eLang->get('VERIFY_CODE_TIP'), 'e-mail'); break;
	default: $vtip_txt = sprintf($eLang->get('VERIFY_CODE_TIP'), 'message'); break;
}

$nonce_str = ($page->nonce != '') ?	' nonce="'.$page->nonce.'"' : '';

echo $page->doctype."\n";
?>
<html<?php echo $page->htmlattributes; ?>>
<head>
	<base href="<?php echo $elxis->getConfig('URL'); ?>/">
	<meta http-equiv="content-type" content="<?php echo $page->contenttype; ?>; charset=utf-8">
	<meta name="generator" content="Elxis - Open Source CMS">
	<meta name="distribution" content="global">
	<meta name="robots" content="index, follow">
	<title><?php echo $page->title.' - '.$elxis->getConfig('SITENAME'); ?></title>
	<meta name="description" content="<?php echo $eLang->get('WEBSITE_OFFLINE'); ?>">
	<link <?php echo $favrel; ?> href="<?php echo $page->favicon; ?>">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="HandheldFriendly" content="true">
	<link rel="stylesheet" href="<?php echo $elxis->secureBase().'/templates/system/css/standard'.$rtlsfx.'.css?v=m'.$mts; ?>" type="text/css" media="all"<?php echo $nonce_str; ?>>
	<link rel="stylesheet" href="<?php echo $elxis->secureBase().'/templates/system/css/exit'.$rtlsfx.'.css?v=m'.$mtx; ?>" type="text/css" media="all"<?php echo $nonce_str; ?>>
	<script src="<?php echo $elxis->secureBase().'/includes/js/elxis.js?v=m'.$mtj; ?>"></script>
	<script<?php echo $nonce_str; ?>>
	function exit5SwitchLang() {
		if (!document.getElementById('exitlngfm')) { return; }
		var fmObj = document.getElementById('exitlngfm');
		var selObj = document.getElementById('exit5_select_lang');
		var admURL = selObj.options[selObj.selectedIndex].getAttribute('data-act');
		window.location.href = admURL;
	}
	var xloginwait = 0;
	function exit5Login(has2fa, otplogin) {
		if (xloginwait == 1) { return false; } //prevent double click
		if (otplogin == 1) {
			var btnelem = 'otpbtn';
		} else {
			var btnelem = 'loginbtn';
		}
		document.getElementById('exit5_message_error').className = 'elx5_invisible';
		var rnd = Math.floor((Math.random()*100)+1);
		var edata = { 'xaction': 'login', 'rnd': rnd };
		edata.uname = document.getElementById('uname').value.trim();
		edata.pword = document.getElementById('pword').value.trim();
		edata.token = document.getElementById('token').value;
		var login_action = document.getElementById('fmelxislogin').action;
		if (edata.uname == '') {
			document.getElementById('uname').focus();
			return false;
		}
		if (edata.pword == '') {
			document.getElementById('pword').focus();
			return false;
		}
		if (has2fa == 1) {
			if (otplogin == 1) {
				edata.otp = document.getElementById('vcode').value.trim();
				if (edata.otp == '') {
					document.getElementById('vcode').focus();
					return false;
				}
				if (isNaN(edata.otp)) {
					document.getElementById('vcode').focus();
					return false;
				}
				edata.xaction = 'login';
			} else {
				edata.xaction = 'makeotp';
			}
		}

		var successfunc = function(xreply) {
			xloginwait = 0;
			document.getElementById(btnelem).innerHTML = document.getElementById(btnelem).getAttribute('data-loginlng');
			try {
				var jsonObj = JSON.parse(xreply);
			} catch(e) {
				return false;
			}
			if (parseInt(jsonObj.success, 10) < 1) {
				if (jsonObj.message != '') {
					var msg = jsonObj.message;
				} else {
					var msg = 'Action failed!';
				}
				document.getElementById('exit5_message_error').innerHTML = msg;
				document.getElementById('exit5_message_error').className = 'exit5_message_error';
				setTimeout(function() { document.getElementById('exit5_message_error').className = 'elx5_invisible'; }, 3000);
				return false;
			} else {
				if (has2fa == 1) {
					if (otplogin == 1) {
						window.location.href = '<?php echo $site_url; ?>?rnd='+rnd;
					} else {
						//send method might change from sms to email if sms fail
						var tiptxt = document.getElementById('vcode_tip').getAttribute('data-genlng');
						if (jsonObj.method == 'sms') {
							document.getElementById('vcode_tip').innerHTML = tiptxt.replace('@@@', 'SMS');
						} else {
							document.getElementById('vcode_tip').innerHTML = tiptxt.replace('@@@', 'e-mail');
						}
						document.getElementById('fmelxisloginwrap').className = 'elx5_invisible';
						document.getElementById('fmelxis2faloginwrap').className = 'elx5_zero';
					}
				} else {
					window.location.href = '<?php echo $site_url; ?>?rnd='+rnd;
				}
			}
		};
		var errorfunc = function (XMLHttpRequest, textStatus, errorThrown) {
			xloginwait = 0;
			document.getElementById(btnelem).innerHTML = document.getElementById(btnelem).getAttribute('data-loginlng');
			document.getElementById('exit5_message_error').innerHTML = 'Login failed with error message '+errorThrown;
			document.getElementById('exit5_message_error').className = 'exit5_message_error';
			setTimeout(function() { document.getElementById('exit5_message_error').className = 'elx5_invisible'; }, 3000);
		};
		xloginwait = 1;
		document.getElementById(btnelem).innerHTML = document.getElementById(btnelem).getAttribute('data-waitlng');
		elxAjax('POST', login_action, edata, null, null, successfunc, errorfunc);
	}
	if (document.readyState != 'loading') {
		document.getElementById('fmelxislogin').addEventListener('submit', function(event){ event.preventDefault(); exit5Login(<?php echo $twofa; ?>, 0); return false; }, false);
		document.getElementById('fmelxis2falogin').addEventListener('submit', function(event){ event.preventDefault(); exit5Login(1, 1); return false; }, false);
		document.getElementById('exit5_select_lang').addEventListener('change', function(){ exit5SwitchLang(); }, false);
	} else if (document.addEventListener) {
		document.addEventListener('DOMContentLoaded', function(){ 
			document.getElementById('fmelxislogin').addEventListener('submit', function(event){ event.preventDefault(); exit5Login(<?php echo $twofa; ?>, 0); return false; }, false);
			document.getElementById('fmelxis2falogin').addEventListener('submit', function(event){ event.preventDefault(); exit5Login(1, 1); return false; }, false);
			document.getElementById('exit5_select_lang').addEventListener('change', function(){ exit5SwitchLang(); }, false);
		});
	} else {
		document.attachEvent('onreadystatechange', function() { 
			if (document.readyState == 'complete') {
				document.getElementById('fmelxislogin').addEventListener('submit', function(event){ event.preventDefault(); exit5Login(<?php echo $twofa; ?>, 0); return false; }, false);
				document.getElementById('fmelxis2falogin').addEventListener('submit', function(event){ event.preventDefault(); exit5Login(1, 1); return false; }, false);
				document.getElementById('exit5_select_lang').addEventListener('change', function(){ exit5SwitchLang(); }, false);
  			} 
		});
	}
	</script>
</head>
<body>
	<div class="exit5_pgwrapin">
		<div class="exit5_titlebox">
			<h1><?php echo $page->title; ?></h1>
			<div class="exit5_title_desc"><?php echo $page->msgtitle; ?></div>
		</div>
<?php 
		if ($elxis->getConfig('ONLINE') == 2) {
			$msg = $page->message.' '.$eLang->get('OWN_ADMIN_LOGIN');
		} else if ($elxis->getConfig('ONLINE') == 3) {
			$msg = $page->message.' '.$eLang->get('OWN_USER_LOGIN');
		} else {
			$msg = $page->message;
		}
		echo '<div class="exit5_message">'.$msg."</div>\n";
		if ($page->loginerror != '') {
			echo '<div id="exit5_message_error" class="exit5_message_error">'.$page->loginerror."</div>\n";
		} else {
			echo '<div id="exit5_message_error" class="elx5_invisible"></div>'."\n";
		}

		if (($elxis->getConfig('ONLINE') == 2) || ($elxis->getConfig('ONLINE') == 3)) {
?>
		<div id="fmelxisloginwrap" class="elx5_zero">
		<form name="fmelxislogin" id="fmelxislogin" method="post" action="<?php echo $site_inurl; ?>" class="elx5_form" autocomplete="off">
			<div class="elx5_dspace">
				<label for="uname" class="exit5_label"><?php echo $eLang->get('USERNAME').' / '.$eLang->get('EMAIL'); ?></label>
				<input type="text" name="uname" id="uname" value="" dir="ltr" class="exit5_smpgtext" placeholder="<?php echo $eLang->get('USERNAME').' / '.$eLang->get('EMAIL'); ?>" required="required" autocomplete="off">
			</div>
			<div class="elx5_dspace">
				<label for="pword" class="exit5_label"><?php echo $eLang->get('PASSWORD'); ?></label>
				<input type="password" name="pword" id="pword" value="" dir="ltr" class="exit5_smpgtext" placeholder="<?php echo $eLang->get('PASSWORD'); ?>" required="required" autocomplete="off">
			</div>
			<input type="hidden" name="remember" dir="ltr" value="1">
			<input type="hidden" name="auth_method" dir="ltr" value="elxis">
			<input type="hidden" name="token" id="token" dir="ltr" value="<?php echo $token; ?>">
			<div class="elx5_dspace">
				<button type="submit" name="loginbtn" id="loginbtn" class="exit5_btn" data-waitlng="<?php echo $eLang->get('PLEASE_WAIT'); ?>" data-loginlng="<?php echo $eLang->get('LOGIN'); ?>"><?php echo $eLang->get('LOGIN'); ?></button>
			</div>
		</form>
		</div>

		<div id="fmelxis2faloginwrap" class="elx5_invisible">
		<form name="fmelxis2falogin" id="fmelxis2falogin" method="post" action="<?php echo $site_inurl; ?>" class="elx5_form" autocomplete="off">
			<div class="elx5_dspace">
				<label for="vcode" class="exit5_label"><?php echo $eLang->get('VERIFY_CODE'); ?></label>
				<input type="text" name="vcode" id="vcode" value="" dir="ltr" class="exit5_bigtext" inputmode="numeric" pattern="[0-9]+" autocomplete="one-time-code" maxlength="6" placeholder="<?php echo $eLang->get('VERIFY_CODE'); ?>" required="required">
				<div class="elx5_tip" id="vcode_tip" data-genlng="<?php echo addslashes($vtip_txt_generic); ?>"><?php echo $vtip_txt; ?></div>
			</div>
			<div class="elx5_dspace">
				<input type="hidden" name="token" id="token2fa" dir="ltr" value="<?php echo $token; ?>">
				<button type="submit" name="otpbtn" id="otpbtn" class="exit5_btn" data-waitlng="<?php echo $eLang->get('PLEASE_WAIT'); ?>" data-loginlng="<?php echo $eLang->get('CONTINUE'); ?>"><?php echo $eLang->get('CONTINUE'); ?></button>
			</div>
			<a href="<?php echo $site_url; ?>" title="<?php echo $eLang->get('START_OVER'); ?>" class="exit5_restart"><?php echo $eLang->get('START_OVER'); ?></a>
		</form>
		</div>

<?php 
		}

		if (($elxis->getConfig('SITELANGS') == '') || (strpos($elxis->getConfig('SITELANGS'), ',') !== false)) {
			$infolangs = $eLang->getSiteLangs(true);
			$flagsdir = $elxis->secureBase().'/includes/libraries/elxis/language/flags32/';
			$curlng = $eLang->currentLang();
			echo '<form name="exitlngfm" id="exitlngfm" method="get" action="" class="elx5_form">'."\n";
			echo '<div class="exit5_lang_wrap">'."\n";
			echo '<div class="exit5_downpick">'.$eLang->get('CHOOSE_LANG').'</div>';
			if (isset($infolangs[$curlng])) {
				echo '<img src="'.$flagsdir.$curlng.'.png" alt="'.$infolangs[$curlng]['NAME'].' - '.$infolangs[$curlng]['NAME_ENG'].'" title="'.$infolangs[$curlng]['NAME'].' - '.$infolangs[$curlng]['NAME_ENG'].'"> ';
				echo '<div class="exit5_lang_selwrap">'."\n";
			}
			echo '<select name="lang" class="exit5_select_lang" id="exit5_select_lang">'."\n";
			foreach ($infolangs as $lng => $info) {
				$selected = ($curlng == $lng) ? ' selected="selected"' : '';
				$link = $elxis->makeURL($lng, '', true);
				echo '<option value="'.$lng.'"'.$selected.' data-act="'.$link.'">'.$info['NAME'].' - '.$info['NAME_ENG']."</option>\n";
			}
			echo "</select>\n";
			if (isset($infolangs[$curlng])) { echo "</div>\n"; }
			echo "</div>\n";
			echo '<input type="hidden" name="token" id="token2fa" dir="ltr" value="'.$token.'">'."\n";
			echo "</form>\n";
		}
?>
	</div><!-- exit5_pgwrapin -->
	<div class="exit5_pgfooter">
		<div class="exit5_pgfooter_copy"><a href="<?php echo $page->sitelink; ?>" title="<?php echo $elxis->getConfig('SITENAME'); ?>"><?php echo $elxis->getConfig('SITENAME'); ?></a> &copy; <?php echo date('Y'); ?></div>
		<a href="https://www.elxis.org" title="Elxis Open Source CMS" class="exit5_powerby">Powered by Elxis CMS &#169; 2006-<?php echo date('Y'); ?></a>
	</div>
</body>
</html>