<?php
/**
 * Registro — Nexora V6 Custom (layout auth glass).
 */
if (!defined('access') || !access) {
	die();
}

if (isLoggedIn()) {
	redirect();
}

$h = function ($s) {
	return htmlspecialchars((string) $s, ENT_QUOTES, 'UTF-8');
};

try {
	if (!mconfig('active')) {
		throw new Exception(lang('error_17', true));
	}

	if (isset($_POST['webengineRegister_submit'])) {
		try {
			if (!isset($_POST['nexora_terms_accept']) || (string) $_POST['nexora_terms_accept'] !== '1') {
				throw new Exception('Para crear tu cuenta debes leer y aceptar los Términos y Condiciones de Nexora VE Networks.');
			}

			$realm = NexoraRealm::requirePosted('nexora_realm');
			$Account = new Account($realm);

			if (mconfig('register_enable_recaptcha')) {
				if (!@include_once(__PATH_CLASSES__ . 'recaptcha/autoload.php')) {
					throw new Exception(lang('error_60'));
				}
				$recaptcha = new \ReCaptcha\ReCaptcha(mconfig('register_recaptcha_secret_key'));

				$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
				if (!$resp->isSuccess()) {
					throw new Exception(lang('error_18', true));
				}
			}

			$Account->registerAccount($_POST['webengineRegister_user'], $_POST['webengineRegister_pwd'], $_POST['webengineRegister_pwdc'], $_POST['webengineRegister_email']);
		} catch (Exception $ex) {
			message('error', $ex->getMessage());
		}
	}

	$t_title = lang('module_titles_txt_1', true);
	if ($t_title === 'ERROR' || $t_title === '') {
		$t_title = nexora_lang('nexora_auth_register_title', 'Crear cuenta');
	}
	$t_sub = nexora_lang('nexora_auth_register_sub', 'Únete al servidor en unos segundos.');
	$t_login_cta = nexora_lang('nexora_auth_have_account', '¿Ya tienes cuenta?');
	$t_login_link = lang('menu_txt_4', true);
	if ($t_login_link === 'ERROR' || $t_login_link === '') {
		$t_login_link = nexora_lang('nexora_auth_login_link', 'Iniciar sesión');
	}
?>
<div class="nex-auth nex-auth--register">
	<div class="nex-auth-card nex-glass-panel">
		<header class="nex-auth-head">
			<p class="nex-auth-eyebrow"><?php echo $h(nexora_lang('nexora_auth_eyebrow_join', 'Comienza tu aventura')); ?></p>
			<h1 class="nex-auth-title"><?php echo $h($t_title); ?></h1>
			<p class="nex-auth-sub"><?php echo $h('Una identidad Nexora para los dos servidores.'); ?></p>
		</header>

		<form class="nex-auth-form" action="" method="post">
			<?php nexoraDualRegistrationAuthField(); ?>
			<div class="nex-auth-field">
				<label class="nex-auth-label" for="webengineRegistration1"><?php echo lang('register_txt_1', true); ?></label>
				<input type="text" class="nex-auth-input" id="webengineRegistration1" name="webengineRegister_user" required autocomplete="username" maxlength="50">
				<p class="nex-auth-help"><?php echo langf('register_txt_6', array(config('username_min_len', true), config('username_max_len', true))); ?></p>
			</div>
			<div class="nex-auth-field">
				<label class="nex-auth-label" for="webengineRegistration2"><?php echo lang('register_txt_2', true); ?></label>
				<input type="password" class="nex-auth-input" id="webengineRegistration2" name="webengineRegister_pwd" required autocomplete="new-password">
				<p class="nex-auth-help"><?php echo langf('register_txt_7', array(config('password_min_len', true), config('password_max_len', true))); ?></p>
			</div>
			<div class="nex-auth-field">
				<label class="nex-auth-label" for="webengineRegistration3"><?php echo lang('register_txt_3', true); ?></label>
				<input type="password" class="nex-auth-input" id="webengineRegistration3" name="webengineRegister_pwdc" required autocomplete="new-password">
				<p class="nex-auth-help"><?php echo lang('register_txt_8', true); ?></p>
			</div>
			<div class="nex-auth-field">
				<label class="nex-auth-label" for="webengineRegistration4"><?php echo lang('register_txt_4', true); ?></label>
				<input type="text" class="nex-auth-input" id="webengineRegistration4" name="webengineRegister_email" required autocomplete="email">
				<p class="nex-auth-help"><?php echo lang('register_txt_9', true); ?></p>
			</div>

			<?php if (mconfig('register_enable_recaptcha')) { ?>
			<div class="nex-auth-field nex-auth-recaptcha">
				<div class="g-recaptcha" data-sitekey="<?php echo $h(mconfig('register_recaptcha_site_key')); ?>"></div>
				<script src="https://www.google.com/recaptcha/api.js" async defer></script>
			</div>
			<?php } ?>

			<label class="nex-auth-consent" for="nexoraTermsAccept">
				<input type="checkbox" id="nexoraTermsAccept" name="nexora_terms_accept" value="1" required<?php echo (isset($_POST['nexora_terms_accept']) && (string) $_POST['nexora_terms_accept'] === '1') ? ' checked' : ''; ?>>
				<span class="nex-auth-consent__box" aria-hidden="true"></span>
				<span class="nex-auth-consent__copy">
					He leído y acepto los <a href="<?php echo $h(rtrim(__BASE_URL__, '/') . '/tos/'); ?>" target="_blank" rel="noopener noreferrer">Términos y Condiciones de Registro y Uso</a> de Nexora VE Networks.
					<small>La aceptación es obligatoria para crear una cuenta.</small>
				</span>
			</label>

			<div class="nex-auth-actions">
				<button type="submit" name="webengineRegister_submit" value="submit" class="nex-btn nex-btn--gold nex-btn--block nex-auth-submit"><?php echo lang('register_txt_5', true); ?></button>
			</div>
		</form>

		<p class="nex-auth-switch">
			<?php echo $h($t_login_cta); ?>
			<a href="<?php echo $h(rtrim(__BASE_URL__, '/') . '/login/'); ?>"><?php echo $h($t_login_link); ?></a>
		</p>
	</div>
</div>
<?php
} catch (Exception $ex) {
	message('error', $ex->getMessage());
}
