<?php 
/**
* @version		$Id: pwchange.php 2707 2025-08-04 17:43:03Z 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');

$currenturl = '';
if ($pageparams) {
	if (isset($pageparams['currenturl'])) {
		if (trim($pageparams['currenturl']) != '') {
			$currenturl = $pageparams['currenturl'];
		}
	}
}
if ($currenturl == '') {
	if (defined('ELXIS_ADMIN')) {
		$currenturl = $elxis->makeAURL('', '', true);
	} else {
		$currenturl = $elxis->makeURL('', '', true);
	}
}

if (defined('ELXIS_ADMIN')) {
	$basehref = $elxis->secureBase().'/'.ELXIS_ADIR.'/';
	$site_inurl = $elxis->makeAURL('', 'inner.php', true);
} else {
	$basehref = $elxis->getConfig('URL').'/';
	$site_inurl = $elxis->makeURL('', 'inner.php', true);
}

$policy = (int)$elxis->getConfig('PWPOLICY');
if ($policy < 5) {//normal
	$pattern = '[A-Za-z0-9_!@\(\)\{\}\[\]\-\#\&\$]{8,}';
	$mintitle = sprintf($eLang->get('PASS_MUSTLENGTH'), '8');
} else {//high
	$pattern = '[A-Za-z0-9_!@\(\)\{\}\[\]\-\#\&\$]{12,}';
	$mintitle = sprintf($eLang->get('PASS_MUSTLENGTH'), '12');
}
$rtlsfx = ($eLang->getinfo('DIR') == 'rtl') ? '-rtl' : '';
if (preg_match('#(\.png)$#i', $page->favicon)) {
	$favrel = 'rel="icon" type="image/png"';
} else {
	$favrel = 'rel="shortcut icon"';
}

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

$token = md5(uniqid(rand(), true));
eFactory::getSession()->set('token_cpwform', $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');

echo $page->doctype."\n";
?>
<html<?php echo $page->htmlattributes; ?>>
<head>
	<base href="<?php echo $basehref; ?>">
	<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="noindex, nofollow">
	<title><?php echo $page->title.' - '.$elxis->getConfig('SITENAME'); ?></title>
	<meta name="description" content="<?php echo $page->msgtitle; ?>">
	<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; ?>"<?php echo $nonce_str; ?>></script>
	<script<?php echo $nonce_str; ?>>
	var xchangewait = 0;
	function exit5Change() {
		if (xchangewait == 1) { return false; } //prevent double click

		document.getElementById('exit5_message_error').className = 'elx5_invisible';
		var rnd = Math.floor((Math.random()*100)+1);
		var edata = { 'rnd': rnd };
		edata.curpword = document.getElementById('curpword').value.trim();
		edata.newpword = document.getElementById('newpword').value.trim();
		edata.cnewpword = document.getElementById('cnewpword').value.trim();
		edata.token = document.getElementById('token').value;
		edata.xaction = 'changepw';
		if (edata.curpword == '') { document.getElementById('curpword').focus(); return false; }
		if (edata.newpword == '') { document.getElementById('newpword').focus(); return false; }
		if (edata.cnewpword == '') { document.getElementById('cnewpword').focus(); return false; }
		if (edata.newpword != edata.cnewpword) { document.getElementById('cnewpword').focus(); return false; }

		var login_action = document.getElementById('fmelxiscpw').action;

		var successfunc = function(xreply) {
			xchangewait = 0;
			document.getElementById('changebtn').innerHTML = document.getElementById('changebtn').getAttribute('data-changelng');
			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'; }, 5000);
				return false;
			} else {
				document.getElementById('exit5_message_error').innerHTML = jsonObj.message;
				document.getElementById('exit5_message_error').className = 'exit5_message_success';
				setTimeout(function() { window.location.href = '<?php echo $currenturl; ?>'; }, 3000);
			}
		};
		var errorfunc = function (XMLHttpRequest, textStatus, errorThrown) {
			xchangewait = 0;
			document.getElementById('changebtn').innerHTML = document.getElementById('changebtn').getAttribute('data-changelng');
			document.getElementById('exit5_message_error').innerHTML = 'Action failed with error message '+errorThrown;
			document.getElementById('exit5_message_error').className = 'exit5_message_error';
			setTimeout(function() { document.getElementById('exit5_message_error').className = 'elx5_invisible'; }, 5000);
		};
		xchangewait = 1;
		document.getElementById('changebtn').innerHTML = document.getElementById('changebtn').getAttribute('data-waitlng');
		elxAjax('POST', login_action, edata, null, null, successfunc, errorfunc);
	}
	if (document.readyState != 'loading') {
		document.getElementById('fmelxiscpw').addEventListener('submit', function(e){ e.preventDefault(); exit5Change(); return false; }, false);
	} else if (document.addEventListener) {
		document.addEventListener('DOMContentLoaded', function(){
			document.getElementById('fmelxiscpw').addEventListener('submit', function(e){ e.preventDefault(); exit5Change(); return false; }, false);
		});
	} else {
		document.attachEvent('onreadystatechange', function() { if (document.readyState == 'complete') { 
			document.getElementById('fmelxiscpw').addEventListener('submit', function(e){ e.preventDefault(); exit5Change(); return false; }, 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>
		<div id="exit5_message_error" class="elx5_invisible"></div>
		<form name="fmelxiscpw" id="fmelxiscpw" method="post" action="<?php echo $site_inurl; ?>" class="elx5_form" autocomplete="off">
<?php 
			echo '<div class="elx5_dspace">'."\n";
			echo '<label for="curpword" class="exit5_label">'.$eLang->get('CUR_PWORD')."</label>\n";
			echo '<input type="password" name="curpword" id="curpword" value="" dir="ltr" class="exit5_smpgtext exit5_smpgtextcls" placeholder="'.$eLang->get('CUR_PWORD').'" required="required" autocomplete="new-password">'."\n";
			echo "</div>\n";
			echo '<div class="elx5_dspace">'."\n";
			echo '<label for="newpword" class="exit5_label">'.$eLang->get('NEW_PWORD')."</label>\n";
			echo '<input type="password" name="newpword" id="newpword" value="" pattern="'.$pattern.'" title="'.$mintitle.'. Acceptable characters are A-Z a-z 0-9 _-.!@#$&(){}[]" dir="ltr" class="exit5_smpgtext exit5_smpgtextcls" placeholder="'.$eLang->get('NEW_PWORD').'" required="required" autocomplete="new-password">'."\n";
			echo "</div>\n";
			echo '<div class="elx5_dspace">'."\n";
			echo '<label for="cnewpword" class="exit5_label">'.$eLang->get('NEW_CONFPWORD')."</label>\n";
			echo '<input type="password" name="cnewpword" id="cnewpword" value="" pattern="'.$pattern.'" title="'.$mintitle.'. Acceptable characters are A-Z a-z 0-9 _-.!@#$&(){}[]" dir="ltr" class="exit5_smpgtext exit5_smpgtextcls" placeholder="'.$eLang->get('NEW_CONFPWORD').'" required="required" autocomplete="new-password">'."\n";
			echo "</div>\n";
?>
			<input type="hidden" name="token" id="token" dir="ltr" value="<?php echo $token; ?>">
			<div class="elx5_vlspace">
				<button type="submit" name="changebtn" id="changebtn" class="exit5_btn" data-waitlng="<?php echo $eLang->get('PLEASE_WAIT'); ?>" data-changelng="<?php echo $page->buttontext; ?>"><?php echo $page->buttontext; ?></button>
			</div>
		</form>
	</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>