<?php
if(isLoggedIn()) redirect();
try {
    if(!isset($_GET['player'])) throw new Exception(lang('referralsystem_error_7',true));
    $realm=isset($_GET['realm']) ? NexoraRealm::validate($_GET['realm']) : NexoraRealm::active();
    NexoraRealm::selectPublic($realm,true);
    $player=trim((string)$_GET['player']);
    if(!Validator::AlphaNumeric($player)) throw new Exception(lang('referralsystem_error_7',true));
    $db=Connection::Database('MuOnline',$realm);
    $exists=$db->query_fetch_single('SELECT TOP 1 '. _CLMN_CHR_NAME_ .' AS n FROM '. _TBL_CHR_ .' WHERE '. _CLMN_CHR_NAME_ .'=?',array($player));
    if(!is_array($exists)) throw new Exception(lang('referralsystem_error_7',true));
    $secure=!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off';
    $options=array('expires'=>time()+86400,'path'=>'/','secure'=>$secure,'httponly'=>true,'samesite'=>'Lax');
    setcookie('nexora_referral_player',$player,$options);
    setcookie('nexora_referral_realm',$realm,$options);
    redirect(1,'register/?realm='.rawurlencode($realm));
} catch(Exception $ex) { message('error',$ex->getMessage()); }
