<?php
/**
 * WebEngine CMS
 * https://webenginecms.org/
 * 
 * @version 1.2.5
 * @author Gabo Development <https://gabodevelop.com/>
 * @copyright (c) 2026 Gabriel Development, All Rights Reserved
 * 
 * Licensed under the MIT license
 * http://opensource.org/licenses/MIT
 */

if(!isLoggedIn()) redirect(1,'login');

$pageTitle = lang('module_titles_txt_6',true) ?: 'Change Password';

try {
	if(!mconfig('active')) throw new Exception(lang('error_47',true));
	$common = new common();
	if(mconfig('change_password_email_verification') && $common->hasActivePasswordChangeRequest($_SESSION['userid'])) throw new Exception(lang('error_19',true));
	if(isset($_POST['webenginePassword_submit'])) {
		try {
			$Account = new Account();
			if(mconfig('change_password_email_verification')) $Account->changePasswordProcess_verifyEmail($_SESSION['userid'], $_SESSION['username'], $_POST['webenginePassword_current'], $_POST['webenginePassword_new'], $_POST['webenginePassword_newconfirm'], $_SERVER['REMOTE_ADDR']);
			else $Account->changePasswordProcess($_SESSION['userid'], $_SESSION['username'], $_POST['webenginePassword_current'], $_POST['webenginePassword_new'], $_POST['webenginePassword_newconfirm']);
		} catch (Exception $ex) {
			message('error', $ex->getMessage());
		}
	}
	echo '<div class="nexora-usercp-page">';
	echo '<header class="nexora-usercp-page-header"><h1 class="nexora-usercp-page-title">'.htmlspecialchars($pageTitle).'</h1></header>';
	echo '<div class="nexora-usercp-panel"><div class="nexora-usercp-panel-body">';
	echo '<form class="nexora-usercp-form" action="" method="post">';
	echo '<div class="nexora-usercp-field"><label class="nexora-usercp-label" for="webenginePassword_current">'.lang('changepassword_txt_1',true).'</label><input type="password" class="nexora-usercp-input" id="webenginePassword_current" name="webenginePassword_current" required></div>';
	echo '<div class="nexora-usercp-field"><label class="nexora-usercp-label" for="webenginePassword_new">'.lang('changepassword_txt_2',true).'</label><input type="password" class="nexora-usercp-input" id="webenginePassword_new" name="webenginePassword_new" required></div>';
	echo '<div class="nexora-usercp-field"><label class="nexora-usercp-label" for="webenginePassword_newconfirm">'.lang('changepassword_txt_3',true).'</label><input type="password" class="nexora-usercp-input" id="webenginePassword_newconfirm" name="webenginePassword_newconfirm" required></div>';
	echo '<div class="nexora-usercp-field"><button type="submit" name="webenginePassword_submit" value="submit" class="nexora-usercp-btn">'.lang('changepassword_txt_4',true).'</button></div>';
	echo '</form></div></div></div>';
} catch(Exception $ex) {
	message('error', $ex->getMessage());
}