<?php


namespace OTP\Handler\Forms;

use OTP\Helper\FormSessionVars;
use OTP\Helper\MoOTPDocs;
use OTP\Helper\MoUtility;
use OTP\Helper\SessionUtils;
use OTP\Objects\FormHandler;
use OTP\Objects\IFormHandler;
use OTP\Objects\VerificationType;
use OTP\Traits\Instance;
use ReflectionException;
use WP_Error;
class WpEmemberForm extends FormHandler implements IFormHandler
{
    use Instance;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = FALSE;
        $this->_formSessionVar = FormSessionVars::EMEMBER;
        $this->_typePhoneTag = "\x6d\157\x5f\145\155\145\x6d\142\145\x72\x5f\160\x68\x6f\156\145\137\145\x6e\x61\x62\x6c\145";
        $this->_typeEmailTag = "\155\x6f\137\145\155\145\x6d\142\x65\x72\x5f\x65\155\x61\151\154\x5f\x65\x6e\x61\142\x6c\145";
        $this->_typeBothTag = "\x6d\x6f\x5f\x65\155\145\x6d\142\x65\x72\x5f\x62\x6f\164\x68\x5f\145\156\x61\x62\x6c\x65";
        $this->_formKey = "\x57\x50\x5f\x45\x4d\105\x4d\102\x45\122";
        $this->_formName = mo_("\127\120\40\145\x4d\x65\x6d\x62\x65\162");
        $this->_isFormEnabled = get_mo_option("\145\x6d\145\155\x62\x65\x72\137\x64\x65\x66\141\165\154\x74\137\x65\156\141\x62\154\x65");
        $this->_phoneKey = "\x77\x70\137\x65\155\x65\155\x62\x65\162\x5f\160\150\x6f\x6e\x65";
        $this->_phoneFormId = "\151\156\160\x75\164\x5b\x6e\141\x6d\145\x3d" . $this->_phoneKey . "\135";
        $this->_formDocuments = MoOTPDocs::EMEMBER_FORM_LINK;
        parent::__construct();
    }
    function handleForm()
    {
        $this->_otpType = get_mo_option("\x65\x6d\x65\x6d\x62\x65\162\137\x65\156\141\142\154\x65\137\164\x79\160\x65");
        if (!(array_key_exists("\145\x6d\145\155\142\145\x72\x5f\144\x73\x63\x5f\156\x6f\156\x63\145", $_POST) && !array_key_exists("\157\x70\x74\x69\x6f\x6e", $_POST))) {
            goto dH;
        }
        $this->miniorange_emember_user_registration();
        dH:
    }
    function isPhoneVerificationEnabled()
    {
        $Cw = $this->getVerificationType();
        return $Cw === VerificationType::PHONE || $Cw === VerificationType::BOTH;
    }
    function miniorange_emember_user_registration()
    {
        if (!$this->validatePostFields()) {
            goto vQ;
        }
        $s9 = array_key_exists($this->_phoneKey, $_POST) ? $_POST[$this->_phoneKey] : NULL;
        $this->startTheOTPVerificationProcess($_POST["\167\160\x5f\x65\x6d\x65\155\142\x65\x72\x5f\x75\163\x65\162\x5f\x6e\141\155\x65"], $_POST["\167\x70\137\x65\x6d\145\x6d\142\x65\x72\137\x65\155\x61\x69\154"], $s9);
        vQ:
    }
    function startTheOTPVerificationProcess($lQ, $Hu, $s9)
    {
        MoUtility::initialize_transaction($this->_formSessionVar);
        $errors = new WP_Error();
        if (strcasecmp($this->_otpType, $this->_typePhoneTag) === 0) {
            goto k_;
        }
        if (strcasecmp($this->_otpType, $this->_typeBothTag) === 0) {
            goto dh;
        }
        $this->sendChallenge($lQ, $Hu, $errors, $s9, VerificationType::EMAIL);
        goto wv;
        dh:
        $this->sendChallenge($lQ, $Hu, $errors, $s9, VerificationType::BOTH);
        wv:
        goto kG;
        k_:
        $this->sendChallenge($lQ, $Hu, $errors, $s9, VerificationType::PHONE);
        kG:
    }
    function handle_failed_verification($te, $j4, $sP, $Cw)
    {
        $HK = $this->getVerificationType();
        $Qg = $HK === VerificationType::BOTH ? TRUE : FALSE;
        miniorange_site_otp_validation_form($te, $j4, $sP, MoUtility::_get_invalid_otp_method(), $HK, $Qg);
    }
    function validatePostFields()
    {
        if (!is_blocked_ip(get_real_ip_addr())) {
            goto UD;
        }
        return FALSE;
        UD:
        if (!(emember_wp_username_exists($_POST["\x77\160\137\145\155\145\x6d\142\x65\162\x5f\x75\x73\145\x72\137\x6e\141\x6d\145"]) || emember_username_exists($_POST["\x77\160\137\145\x6d\x65\155\x62\x65\162\x5f\165\163\145\162\x5f\156\141\155\x65"]))) {
            goto hJ;
        }
        return FALSE;
        hJ:
        if (!(is_blocked_email($_POST["\x77\160\x5f\x65\155\x65\155\142\x65\x72\137\145\x6d\x61\151\x6c"]) || emember_registered_email_exists($_POST["\x77\160\x5f\145\x6d\x65\155\x62\x65\x72\137\x65\155\x61\x69\154"]) || emember_wp_email_exists($_POST["\167\x70\137\145\155\145\x6d\x62\145\162\x5f\x65\x6d\141\151\x6c"]))) {
            goto Mb;
        }
        return FALSE;
        Mb:
        if (!(isset($_POST["\x65\115\145\x6d\142\x65\x72\x5f\x52\x65\147\x69\x73\164\x65\x72"]) && array_key_exists("\167\160\x5f\145\x6d\145\155\142\x65\x72\137\160\x77\144\x5f\162\x65", $_POST) && $_POST["\x77\x70\137\x65\155\145\x6d\142\145\x72\x5f\160\x77\144"] != $_POST["\167\160\x5f\145\155\x65\155\142\x65\x72\137\x70\167\144\137\162\145"])) {
            goto o1;
        }
        return FALSE;
        o1:
        return TRUE;
    }
    function handle_post_verification($TG, $te, $j4, $xy, $sP, $yJ, $Cw)
    {
        $this->unsetOTPSessionVariables();
    }
    public function unsetOTPSessionVariables()
    {
        SessionUtils::unsetSession(array($this->_txSessionId, $this->_formSessionVar));
    }
    public function getPhoneNumberSelector($fv)
    {
        if (!($this->isFormEnabled() && $this->isPhoneVerificationEnabled())) {
            goto Fn1;
        }
        array_push($fv, $this->_phoneFormId);
        Fn1:
        return $fv;
    }
    function handleFormOptions()
    {
        if (MoUtility::areFormOptionsBeingSaved($this->getFormOption())) {
            goto CV;
        }
        return;
        CV:
        $this->_isFormEnabled = $this->sanitizeFormPOST("\145\x6d\145\155\x62\145\x72\137\144\145\146\x61\x75\154\164\137\145\x6e\x61\142\x6c\145");
        $this->_otpType = $this->sanitizeFormPOST("\x65\x6d\x65\x6d\142\x65\x72\x5f\x65\x6e\141\x62\154\x65\137\x74\x79\160\x65");
        update_mo_option("\145\x6d\x65\x6d\142\x65\x72\137\144\145\146\141\165\x6c\164\137\145\156\141\142\x6c\x65", $this->_isFormEnabled);
        update_mo_option("\145\155\145\155\x62\x65\162\137\x65\156\141\x62\154\145\137\164\171\160\145", $this->_otpType);
    }
}
