<?php


namespace OTP\Handler\Forms;

use OTP\Helper\FormSessionVars;
use OTP\Helper\MoConstants;
use OTP\Helper\MoMessages;
use OTP\Helper\MoOTPDocs;
use OTP\Helper\MoUtility;
use OTP\Helper\SessionUtils;
use OTP\Objects\BaseMessages;
use OTP\Objects\FormHandler;
use OTP\Objects\IFormHandler;
use OTP\Objects\VerificationLogic;
use OTP\Objects\VerificationType;
use OTP\Traits\Instance;
use ReflectionException;
use um\core\Form;
use WP_Error;
class UltimateMemberRegistrationForm extends FormHandler implements IFormHandler
{
    use Instance;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = get_mo_option("\165\x6d\137\x69\163\137\x61\152\141\170\x5f\x66\157\x72\x6d");
        $this->_formSessionVar = FormSessionVars::UM_DEFAULT_REG;
        $this->_typePhoneTag = "\x6d\x6f\x5f\x75\x6d\x5f\x70\x68\157\156\145\x5f\x65\x6e\141\142\154\145";
        $this->_typeEmailTag = "\x6d\157\137\x75\155\x5f\x65\x6d\x61\x69\154\137\145\156\x61\x62\154\x65";
        $this->_typeBothTag = "\x6d\x6f\x5f\x75\x6d\137\x62\157\164\x68\137\145\156\x61\x62\154\x65";
        $this->_phoneKey = get_mo_option("\x75\155\x5f\160\150\x6f\x6e\145\x5f\x6b\145\x79");
        $this->_phoneKey = $this->_phoneKey ? $this->_phoneKey : "\155\157\142\151\x6c\x65\x5f\156\165\155\x62\x65\162";
        $this->_phoneFormId = "\151\x6e\x70\165\164\133\x6e\141\155\x65\x5e\75\x27" . $this->_phoneKey . "\47\135";
        $this->_formKey = "\x55\114\124\x49\x4d\101\x54\105\137\x46\117\122\115";
        $this->_formName = mo_("\125\x6c\x74\x69\155\x61\164\145\x20\115\x65\155\x62\x65\x72\40\122\x65\147\x69\x73\x74\x72\141\164\x69\x6f\156\x20\x46\x6f\162\155");
        $this->_isFormEnabled = get_mo_option("\165\155\x5f\144\145\146\x61\165\x6c\x74\137\145\156\141\142\154\x65");
        $this->_restrictDuplicates = get_mo_option("\165\155\137\x72\x65\x73\164\162\x69\143\x74\x5f\144\x75\160\154\151\143\141\164\x65\163");
        $this->_buttonText = get_mo_option("\165\x6d\x5f\x62\x75\x74\164\157\x6e\x5f\164\x65\170\x74");
        $this->_buttonText = !MoUtility::isBlank($this->_buttonText) ? $this->_buttonText : mo_("\103\x6c\x69\x63\x6b\x20\110\145\x72\x65\40\x74\x6f\40\163\145\x6e\x64\x20\117\124\x50");
        $this->_formKey = get_mo_option("\165\x6d\x5f\166\145\x72\151\146\x79\x5f\x6d\145\164\x61\x5f\x6b\145\x79");
        $this->_formDocuments = MoOTPDocs::UM_ENABLED;
        parent::__construct();
    }
    function handleForm()
    {
        $this->_otpType = get_mo_option("\165\155\x5f\x65\x6e\x61\142\x6c\145\137\x74\x79\160\145");
        if ($this->isUltimateMemberV2Installed()) {
            goto eg;
        }
        add_action("\x75\x6d\x5f\163\165\142\x6d\151\x74\x5f\146\157\x72\155\x5f\145\x72\x72\157\162\163\137\150\157\157\x6b\137", array($this, "\155\x69\156\151\157\162\x61\x6e\x67\145\137\x75\x6d\x5f\160\150\157\156\x65\x5f\166\141\x6c\x69\x64\x61\x74\151\157\156"), 99, 1);
        add_action("\165\x6d\x5f\142\145\x66\x6f\x72\x65\x5f\156\145\167\x5f\165\x73\145\162\137\x72\x65\147\151\x73\164\x65\162", array($this, "\x6d\x69\156\x69\x6f\x72\x61\156\x67\145\x5f\165\x6d\x5f\165\163\x65\x72\x5f\x72\x65\x67\151\163\x74\x72\141\x74\151\157\x6e"), 99, 1);
        goto KM;
        eg:
        add_action("\x75\x6d\x5f\163\165\x62\x6d\x69\164\137\x66\x6f\x72\155\x5f\145\162\x72\x6f\x72\163\137\150\157\x6f\x6b\137\x5f\x72\145\147\151\163\164\x72\141\x74\151\x6f\x6e", array($this, "\x6d\x69\x6e\151\157\162\141\156\147\x65\137\x75\155\62\x5f\160\150\157\156\x65\x5f\166\141\x6c\151\x64\x61\164\x69\157\156"), 99, 1);
        add_filter("\x75\155\x5f\x72\x65\147\151\x73\164\x72\x61\x74\151\x6f\156\137\x75\x73\x65\x72\137\162\x6f\x6c\x65", array($this, "\155\151\156\151\157\x72\141\156\147\x65\x5f\x75\155\x32\x5f\x75\163\x65\162\x5f\162\x65\147\x69\x73\x74\x72\x61\164\x69\157\156"), 99, 2);
        KM:
        if (!($this->_isAjaxForm && $this->_otpType != $this->_typeBothTag)) {
            goto VH;
        }
        add_action("\167\x70\x5f\145\x6e\x71\x75\x65\x75\145\137\x73\143\162\151\160\164\163", array($this, "\x6d\x69\156\x69\157\162\141\x6e\x67\145\137\162\145\147\151\163\164\x65\162\x5f\165\x6d\137\x73\x63\162\151\x70\164"));
        $this->routeData();
        VH:
    }
    function isUltimateMemberV2Installed()
    {
        if (function_exists("\151\163\137\x70\x6c\165\x67\x69\x6e\137\x61\x63\x74\x69\166\x65")) {
            goto u4;
        }
        include_once ABSPATH . "\x77\x70\x2d\141\x64\155\151\x6e\x2f\151\x6e\x63\154\165\x64\145\x73\57\160\x6c\165\147\151\156\x2e\160\x68\x70";
        u4:
        return is_plugin_active("\165\154\x74\151\155\141\164\145\55\x6d\x65\x6d\142\x65\x72\57\x75\154\164\x69\155\141\x74\145\x2d\155\x65\x6d\x62\145\162\x2e\x70\150\160");
    }
    private function routeData()
    {
        if (array_key_exists("\157\x70\x74\151\x6f\x6e", $_GET)) {
            goto wg;
        }
        return;
        wg:
        switch (trim($_GET["\x6f\x70\x74\x69\x6f\156"])) {
            case "\155\151\156\151\157\x72\141\x6e\147\x65\x2d\x75\x6d\55\141\152\x61\170\x2d\166\145\162\151\146\x79":
                $this->sendAjaxOTPRequest();
                goto oT;
        }
        R9:
        oT:
    }
    private function sendAjaxOTPRequest()
    {
        MoUtility::initialize_transaction($this->_formSessionVar);
        $this->validateAjaxRequest();
        $zt = MoUtility::sanitizeCheck("\x75\163\x65\x72\137\x70\150\157\156\x65", $_POST);
        $j4 = MoUtility::sanitizeCheck("\x75\x73\x65\162\137\145\x6d\x61\x69\x6c", $_POST);
        if ($this->_otpType === $this->_typePhoneTag) {
            goto fJ;
        }
        SessionUtils::addEmailVerified($this->_formSessionVar, $j4);
        goto d0;
        fJ:
        $this->checkDuplicates($zt, $this->_phoneKey, null);
        SessionUtils::addPhoneVerified($this->_formSessionVar, $zt);
        d0:
        $this->startOtpTransaction(null, $j4, null, $zt, null, null);
    }
    function miniorange_register_um_script()
    {
        wp_register_script("\x6d\157\166\x75\155", MOV_URL . "\x69\156\143\154\165\144\145\163\x2f\152\x73\57\165\155\x72\x65\x67\56\155\151\x6e\56\152\x73", array("\x6a\x71\x75\x65\x72\171"));
        wp_localize_script("\155\x6f\166\165\x6d", "\155\x6f\x75\155\166\x61\162", array("\163\151\x74\145\x55\122\x4c" => site_url(), "\x6f\x74\160\124\171\x70\x65" => $this->_otpType, "\156\x6f\x6e\143\145" => wp_create_nonce($this->_nonce), "\x62\x75\164\164\x6f\156\x74\145\170\x74" => mo_($this->_buttonText), "\x66\151\145\154\x64" => $this->_otpType === $this->_typePhoneTag ? $this->_phoneKey : "\165\163\x65\x72\x5f\x65\x6d\141\151\x6c", "\151\155\147\x55\x52\114" => MOV_LOADER_URL));
        wp_enqueue_script("\x6d\157\x76\165\155");
    }
    function isPhoneVerificationEnabled()
    {
        $sY = $this->getVerificationType();
        return $sY === VerificationType::PHONE || $sY === VerificationType::BOTH;
    }
    function miniorange_um2_user_registration($bZ, $bN)
    {
        $HK = $this->getVerificationType();
        if (SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, $HK)) {
            goto JA;
        }
        if (!SessionUtils::isOTPInitialized($this->_formSessionVar) && $this->_isAjaxForm) {
            goto Pj;
        }
        MoUtility::initialize_transaction($this->_formSessionVar);
        $bN = $this->extractArgs($bN);
        $this->startOtpTransaction($bN["\x75\x73\x65\x72\137\154\x6f\x67\x69\x6e"], $bN["\x75\x73\145\162\137\x65\x6d\141\x69\154"], new WP_Error(), $bN[$this->_phoneKey], $bN["\x75\x73\x65\162\x5f\x70\141\x73\163\x77\157\162\144"], null);
        goto Ht;
        JA:
        $this->unsetOTPSessionVariables();
        return $bZ;
        goto Ht;
        Pj:
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::PLEASE_VALIDATE), MoConstants::ERROR_JSON_TYPE));
        Ht:
        return $bZ;
    }
    private function extractArgs($bN)
    {
        return array("\165\163\x65\x72\137\154\157\x67\x69\x6e" => $bN["\165\163\x65\162\x5f\x6c\x6f\147\151\156"], "\165\163\x65\162\x5f\145\155\x61\151\154" => $bN["\165\x73\145\162\137\145\155\141\151\x6c"], $this->_phoneKey => $bN[$this->_phoneKey], "\165\x73\x65\162\x5f\160\x61\x73\x73\x77\157\x72\144" => $bN["\165\x73\145\162\x5f\160\x61\x73\163\167\157\162\144"]);
    }
    function miniorange_um_user_registration($bN)
    {
        $errors = new WP_Error();
        MoUtility::initialize_transaction($this->_formSessionVar);
        foreach ($bN as $yU => $Fo) {
            if ($yU == "\x75\x73\145\162\137\154\x6f\147\151\156") {
                goto eV;
            }
            if ($yU == "\165\x73\145\162\137\x65\155\141\x69\x6c") {
                goto RQ;
            }
            if ($yU == "\x75\163\x65\162\137\x70\x61\x73\x73\x77\x6f\x72\x64") {
                goto w4;
            }
            if ($yU == $this->_phoneKey) {
                goto eo;
            }
            $yJ[$yU] = $Fo;
            goto NY;
            eV:
            $lQ = $Fo;
            goto NY;
            RQ:
            $R7 = $Fo;
            goto NY;
            w4:
            $xy = $Fo;
            goto NY;
            eo:
            $sP = $Fo;
            NY:
            Og:
        }
        wf:
        $this->startOtpTransaction($lQ, $R7, $errors, $sP, $xy, $yJ);
    }
    function startOtpTransaction($lQ, $R7, $errors, $sP, $xy, $yJ)
    {
        if (strcasecmp($this->_otpType, $this->_typePhoneTag) == 0) {
            goto Jo;
        }
        if (strcasecmp($this->_otpType, $this->_typeBothTag) == 0) {
            goto cp;
        }
        $this->sendChallenge($lQ, $R7, $errors, $sP, VerificationType::EMAIL, $xy, $yJ);
        goto bZ;
        Jo:
        $this->sendChallenge($lQ, $R7, $errors, $sP, VerificationType::PHONE, $xy, $yJ);
        goto bZ;
        cp:
        $this->sendChallenge($lQ, $R7, $errors, $sP, VerificationType::BOTH, $xy, $yJ);
        bZ:
    }
    function miniorange_um2_phone_validation($bN)
    {
        $form = UM()->form();
        foreach ($bN as $yU => $Fo) {
            if ($this->_isAjaxForm && $yU === $this->_formKey) {
                goto PE;
            }
            if ($yU === $this->_phoneKey) {
                goto vM;
            }
            goto mZ;
            PE:
            $this->checkIntegrityAndValidateOTP($form, $Fo, $bN);
            goto mZ;
            vM:
            $this->processPhoneNumbers($Fo, $yU, $form);
            mZ:
            xk:
        }
        QY:
    }
    private function processPhoneNumbers($Fo, $yU, $form = null)
    {
        global $phoneLogic;
        if (MoUtility::validatePhoneNumber($Fo)) {
            goto sA;
        }
        $Kl = str_replace("\x23\43\160\150\157\x6e\x65\x23\43", $Fo, $phoneLogic->_get_otp_invalid_format_message());
        $form->add_error($yU, $Kl);
        sA:
        $this->checkDuplicates($Fo, $yU, $form);
    }
    private function checkDuplicates($Fo, $yU, $form = null)
    {
        if (!($this->_restrictDuplicates && $this->isPhoneNumberAlreadyInUse($Fo, $yU))) {
            goto Xv;
        }
        $Kl = MoMessages::showMessage(MoMessages::PHONE_EXISTS);
        if ($this->_isAjaxForm && SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto pQ;
        }
        $form->add_error($yU, $Kl);
        goto Wb;
        pQ:
        wp_send_json(MoUtility::createJson($Kl, MoConstants::ERROR_JSON_TYPE));
        Wb:
        Xv:
    }
    private function checkIntegrityAndValidateOTP($form, $Fo, array $bN)
    {
        $HK = $this->getVerificationType();
        $this->checkIntegrity($form, $bN, $HK);
        $this->validateChallenge($HK, NULL, $Fo);
        if (SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, $HK)) {
            goto FP;
        }
        $form->add_error($this->_formKey, MoUtility::_get_invalid_otp_method());
        FP:
    }
    private function checkIntegrity($Ns, array $bN, $HK)
    {
        if (strcasecmp($this->_otpType, $this->_typePhoneTag) == 0) {
            goto Ve;
        }
        if (!(strcasecmp($this->_otpType, $this->_typeEmailTag) == 0)) {
            goto Tm;
        }
        if (SessionUtils::isEmailVerifiedMatch($this->_formSessionVar, $bN["\x75\x73\145\x72\137\x65\155\141\x69\154"])) {
            goto Xt;
        }
        $Ns->add_error($this->_formKey, MoMessages::showMessage(MoMessages::EMAIL_MISMATCH));
        Xt:
        Tm:
        goto c_;
        Ve:
        if (SessionUtils::isPhoneVerifiedMatch($this->_formSessionVar, $bN[$this->_phoneKey])) {
            goto vz;
        }
        $Ns->add_error($this->_formKey, MoMessages::showMessage(MoMessages::PHONE_MISMATCH));
        vz:
        c_:
    }
    function miniorange_um_phone_validation($bN)
    {
        global $ultimatemember;
        foreach ($bN as $yU => $Fo) {
            if ($this->_isAjaxForm && $yU === $this->_formKey) {
                goto p3;
            }
            if ($yU === $this->_phoneKey) {
                goto Bw;
            }
            goto Ma;
            p3:
            $this->checkIntegrityAndValidateOTP($ultimatemember->form, $Fo, $bN);
            goto Ma;
            Bw:
            $this->processPhoneNumbers($Fo, $yU, $ultimatemember->form);
            Ma:
            xv:
        }
        vD:
    }
    function isPhoneNumberAlreadyInUse($s9, $yU)
    {
        global $wpdb;
        MoUtility::processPhoneNumber($s9);
        $jO = "\123\105\x4c\105\x43\x54\x20\x60\165\x73\x65\x72\x5f\151\x64\x60\40\x46\x52\117\x4d\x20\140{$wpdb->prefix}\165\x73\145\162\155\x65\164\x61\x60\x20\x57\110\105\122\x45\40\140\x6d\x65\x74\x61\137\x6b\x65\x79\x60\x20\x3d\40\47{$yU}\x27\40\101\x4e\104\x20\140\x6d\x65\164\x61\137\x76\141\x6c\165\x65\x60\x20\x3d\x20\40\x27{$s9}\x27";
        $rF = $wpdb->get_row($jO);
        return !MoUtility::isBlank($rF);
    }
    function handle_failed_verification($te, $j4, $sP, $Cw)
    {
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto o8;
        }
        return;
        o8:
        $HK = $this->getVerificationType();
        $Qg = $HK === VerificationType::BOTH ? TRUE : FALSE;
        if ($this->_isAjaxForm) {
            goto cW;
        }
        miniorange_site_otp_validation_form($te, $j4, $sP, MoUtility::_get_invalid_otp_method(), $HK, $Qg);
        cW:
    }
    function handle_post_verification($TG, $te, $j4, $xy, $sP, $yJ, $Cw)
    {
        if (function_exists("\x69\163\137\x70\x6c\x75\147\x69\156\137\x61\x63\164\151\x76\x65")) {
            goto Dz;
        }
        include_once ABSPATH . "\x77\x70\55\x61\x64\x6d\151\156\57\x69\x6e\x63\x6c\x75\144\x65\x73\57\160\x6c\x75\x67\151\156\56\x70\x68\x70";
        Dz:
        if ($this->isUltimateMemberV2Installed()) {
            goto gQ;
        }
        $this->register_ultimateMember_user($te, $j4, $xy, $sP, $yJ);
        goto OI;
        gQ:
        SessionUtils::addStatus($this->_formSessionVar, self::VALIDATED, $Cw);
        OI:
    }
    function register_ultimateMember_user($te, $j4, $xy, $sP, $yJ)
    {
        $bN = array();
        $bN["\x75\x73\x65\x72\x5f\154\157\x67\151\x6e"] = $te;
        $bN["\x75\163\x65\x72\x5f\x65\155\141\x69\x6c"] = $j4;
        $bN["\165\x73\145\x72\137\160\141\x73\163\x77\x6f\x72\144"] = $xy;
        $bN = array_merge($bN, $yJ);
        $LG = wp_create_user($te, $xy, $j4);
        $this->unsetOTPSessionVariables();
        do_action("\x75\155\137\141\146\x74\145\x72\x5f\x6e\x65\x77\137\x75\x73\145\x72\137\162\145\147\x69\x73\x74\x65\162", $LG, $bN);
    }
    public function unsetOTPSessionVariables()
    {
        SessionUtils::unsetSession(array($this->_txSessionId, $this->_formSessionVar));
    }
    public function getPhoneNumberSelector($fv)
    {
        if (!($this->isFormEnabled() && $this->isPhoneVerificationEnabled())) {
            goto Fx;
        }
        array_push($fv, $this->_phoneFormId);
        Fx:
        return $fv;
    }
    function handleFormOptions()
    {
        if (MoUtility::areFormOptionsBeingSaved($this->getFormOption())) {
            goto Y_;
        }
        return;
        Y_:
        $this->_isFormEnabled = $this->sanitizeFormPOST("\165\x6d\137\144\145\x66\141\x75\154\164\137\x65\156\141\x62\x6c\x65");
        $this->_otpType = $this->sanitizeFormPOST("\x75\x6d\x5f\x65\x6e\141\x62\x6c\145\x5f\x74\x79\160\145");
        $this->_restrictDuplicates = $this->_otpType != $this->_typePhoneTag ? '' : $this->sanitizeFormPOST("\x75\x6d\x5f\162\145\163\x74\x72\151\143\164\137\144\x75\160\154\x69\x63\141\x74\x65\163");
        $this->_isAjaxForm = $this->sanitizeFormPOST("\165\155\137\x69\163\x5f\141\x6a\141\x78\x5f\146\157\162\x6d");
        $this->_buttonText = $this->sanitizeFormPOST("\x75\x6d\137\142\165\164\164\x6f\156\x5f\x74\x65\x78\164");
        $this->_formKey = $this->sanitizeFormPOST("\165\155\x5f\166\x65\162\x69\x66\x79\x5f\x6d\x65\164\141\x5f\x6b\x65\171");
        $this->_phoneKey = $this->sanitizeFormPOST("\165\x6d\137\160\150\x6f\x6e\x65\x5f\153\145\171");
        if (!$this->basicValidationCheck(BaseMessages::UM_CHOOSE)) {
            goto HD;
        }
        update_mo_option("\165\155\137\160\150\x6f\156\145\137\x6b\145\x79", $this->_phoneKey);
        update_mo_option("\165\x6d\x5f\144\145\146\x61\165\x6c\x74\x5f\145\156\141\x62\154\x65", $this->_isFormEnabled);
        update_mo_option("\x75\x6d\137\145\x6e\141\142\154\145\137\164\x79\160\x65", $this->_otpType);
        update_mo_option("\x75\155\x5f\x72\x65\163\164\x72\151\x63\164\137\x64\165\160\x6c\151\x63\x61\164\145\163", $this->_restrictDuplicates);
        update_mo_option("\x75\x6d\137\151\x73\x5f\141\152\141\x78\x5f\146\x6f\x72\x6d", $this->_isAjaxForm);
        update_mo_option("\165\155\137\142\x75\164\164\x6f\156\137\164\x65\x78\x74", $this->_buttonText);
        update_mo_option("\165\155\x5f\166\x65\162\x69\146\171\x5f\155\145\164\141\x5f\153\145\171", $this->_formKey);
        HD:
    }
}
