<?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;
class UltimateMemberProfileForm extends FormHandler implements IFormHandler
{
    use Instance;
    private $_verifyFieldKey;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = TRUE;
        $this->_formSessionVar = FormSessionVars::UM_PROFILE_UPDATE;
        $this->_typePhoneTag = "\x6d\157\137\165\x6d\137\160\162\157\x66\x69\154\x65\137\x70\150\x6f\156\145\x5f\x65\156\x61\142\x6c\145";
        $this->_typeEmailTag = "\155\x6f\137\165\x6d\137\160\x72\157\146\x69\154\x65\137\145\155\141\151\154\137\145\156\141\142\x6c\145";
        $this->_typeBothTag = "\x6d\x6f\x5f\165\155\137\160\x72\x6f\x66\x69\x6c\x65\x5f\142\157\x74\150\137\145\156\141\142\154\x65";
        $this->_formKey = "\x55\x4c\x54\x49\115\x41\x54\x45\137\120\122\117\106\x49\x4c\x45\x5f\106\117\122\x4d";
        $this->_verifyFieldKey = "\166\x65\162\151\x66\x79\x5f\146\151\145\x6c\x64";
        $this->_formName = mo_("\125\154\x74\x69\x6d\x61\164\145\x20\x4d\145\155\142\x65\162\40\x50\x72\x6f\x66\151\154\x65\57\101\143\143\157\165\x6e\164\40\106\157\x72\155");
        $this->_isFormEnabled = get_mo_option("\165\x6d\137\160\162\157\146\151\x6c\145\137\x65\156\x61\142\154\145");
        $this->_restrictDuplicates = get_mo_option("\165\x6d\137\160\162\157\146\151\154\x65\x5f\162\x65\163\164\162\151\x63\164\137\144\165\160\154\151\x63\141\164\145\163");
        $this->_buttonText = get_mo_option("\x75\x6d\137\x70\162\x6f\x66\x69\154\145\137\142\165\x74\164\x6f\x6e\137\164\145\x78\164");
        $this->_buttonText = !MoUtility::isBlank($this->_buttonText) ? $this->_buttonText : mo_("\x43\154\151\143\x6b\x20\x48\x65\162\x65\x20\x74\x6f\x20\163\x65\156\x64\x20\117\x54\x50");
        $this->_emailKey = "\165\x73\x65\162\x5f\145\155\141\x69\x6c";
        $this->_phoneKey = get_mo_option("\x75\155\x5f\160\162\x6f\x66\x69\x6c\145\137\160\x68\157\156\x65\137\153\x65\171");
        $this->_phoneKey = $this->_phoneKey ? $this->_phoneKey : "\155\x6f\x62\151\154\145\137\x6e\x75\x6d\x62\x65\x72";
        $this->_phoneFormId = "\x69\x6e\x70\x75\x74\x5b\x6e\x61\x6d\x65\x5e\75\x27{$this->_phoneKey}\47\x5d";
        $this->_formDocuments = MoOTPDocs::UM_PROFILE;
        parent::__construct();
    }
    public function handleForm()
    {
        $this->_otpType = get_mo_option("\x75\x6d\137\160\x72\x6f\x66\151\154\x65\137\x65\x6e\x61\142\x6c\145\137\164\x79\x70\145");
        add_action("\x77\160\137\x65\x6e\x71\x75\145\x75\x65\137\x73\x63\162\x69\x70\164\x73", array($this, "\155\x69\156\x69\157\162\x61\x6e\147\145\137\162\145\x67\151\163\x74\x65\162\x5f\x75\x6d\x5f\x73\143\x72\x69\x70\164"));
        add_action("\x75\x6d\137\x73\x75\x62\155\151\164\137\141\x63\143\x6f\x75\156\164\137\x65\162\162\x6f\x72\x73\137\x68\x6f\x6f\153", array($this, "\x6d\151\x6e\x69\x6f\162\x61\156\x67\x65\137\x75\x6d\x5f\x76\141\154\x69\144\x61\x74\x69\157\x6e"), 99, 1);
        add_action("\x75\x6d\x5f\141\144\144\137\x65\162\x72\x6f\x72\x5f\x6f\x6e\137\x66\x6f\x72\x6d\137\163\165\142\x6d\151\x74\x5f\166\141\154\x69\144\x61\x74\151\157\156", array($this, "\155\151\x6e\x69\x6f\x72\x61\x6e\x67\x65\x5f\165\x6d\137\x70\x72\157\x66\x69\x6c\x65\137\x76\x61\x6c\x69\144\x61\164\x69\157\156"), 1, 3);
        $this->routeData();
    }
    private function isAccountVerificationEnabled()
    {
        return strcasecmp($this->_otpType, $this->_typeEmailTag) == 0 || strcasecmp($this->_otpType, $this->_typeBothTag) == 0;
    }
    private function isProfileVerificationEnabled()
    {
        return strcasecmp($this->_otpType, $this->_typePhoneTag) == 0 || strcasecmp($this->_otpType, $this->_typeBothTag) == 0;
    }
    private function routeData()
    {
        if (array_key_exists("\x6f\x70\164\x69\157\x6e", $_GET)) {
            goto Ys;
        }
        return;
        Ys:
        switch (trim($_GET["\157\x70\x74\151\x6f\x6e"])) {
            case "\155\151\156\x69\157\162\141\156\x67\x65\x2d\165\x6d\x2d\x61\x63\x63\x2d\141\x6a\141\170\55\166\145\162\x69\x66\171":
                $this->sendAjaxOTPRequest();
                goto by;
        }
        Gy:
        by:
    }
    private function sendAjaxOTPRequest()
    {
        MoUtility::initialize_transaction($this->_formSessionVar);
        $this->validateAjaxRequest();
        $zt = MoUtility::sanitizeCheck("\x75\x73\145\162\137\160\150\x6f\156\145", $_POST);
        $j4 = MoUtility::sanitizeCheck("\x75\x73\145\162\x5f\x65\x6d\141\x69\x6c", $_POST);
        $jN = MoUtility::sanitizeCheck("\157\164\160\x5f\x72\x65\161\x75\x65\x73\x74\x5f\x74\171\160\x65", $_POST);
        $this->startOtpTransaction($j4, $zt, $jN);
    }
    private function startOtpTransaction($R7, $sP, $jN)
    {
        if (strcasecmp($jN, $this->_typePhoneTag) == 0) {
            goto Xs;
        }
        SessionUtils::addEmailVerified($this->_formSessionVar, $R7);
        $this->sendChallenge(null, $R7, null, $sP, VerificationType::EMAIL, null, null);
        goto gE;
        Xs:
        $this->checkDuplicates($sP, $this->_phoneKey);
        SessionUtils::addPhoneVerified($this->_formSessionVar, $sP);
        $this->sendChallenge(null, $R7, null, $sP, VerificationType::PHONE, null, null);
        gE:
    }
    private function checkDuplicates($Fo, $yU)
    {
        if (!($this->_restrictDuplicates && $this->isPhoneNumberAlreadyInUse($Fo, $yU))) {
            goto Or1;
        }
        $Kl = MoMessages::showMessage(MoMessages::PHONE_EXISTS);
        wp_send_json(MoUtility::createJson($Kl, MoConstants::ERROR_JSON_TYPE));
        Or1:
    }
    private function getUserData($yU)
    {
        $current_user = wp_get_current_user();
        if ($yU === $this->_phoneKey) {
            goto eL;
        }
        return $current_user->user_email;
        goto Cr;
        eL:
        global $wpdb;
        $jO = "\x53\105\x4c\x45\103\124\x20\x6d\145\164\x61\137\x76\x61\154\x75\x65\x20\106\122\x4f\115\x20\x60{$wpdb->prefix}\x75\163\x65\162\x6d\x65\x74\141\x60\40\127\x48\x45\x52\105\40\x60\x6d\145\164\x61\137\x6b\145\171\x60\40\x3d\40\47{$yU}\47\40\x41\116\x44\40\140\x75\x73\145\x72\137\x69\144\x60\40\x3d\x20{$current_user->ID}";
        $rF = $wpdb->get_row($jO);
        return isset($rF) ? $rF->meta_value : '';
        Cr:
    }
    private function checkFormSession($form)
    {
        if (SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, $this->getVerificationType())) {
            goto S4;
        }
        $form->add_error($this->_emailKey, MoUtility::_get_invalid_otp_method());
        $form->add_error($this->_phoneKey, MoUtility::_get_invalid_otp_method());
        goto Qk;
        S4:
        $this->unsetOTPSessionVariables();
        Qk:
    }
    private function getUmFormObj()
    {
        if ($this->isUltimateMemberV2Installed()) {
            goto nh;
        }
        global $ultimatemember;
        return $ultimatemember->form;
        goto jR;
        nh:
        return UM()->form();
        jR:
    }
    function isUltimateMemberV2Installed()
    {
        if (function_exists("\x69\163\x5f\160\x6c\x75\147\151\156\137\141\143\x74\x69\166\145")) {
            goto Cq;
        }
        include_once ABSPATH . "\x77\160\55\x61\144\155\151\156\x2f\151\156\143\x6c\165\144\x65\163\x2f\x70\x6c\165\x67\151\x6e\56\160\x68\160";
        Cq:
        return is_plugin_active("\165\154\x74\151\x6d\141\x74\145\x2d\155\x65\155\142\x65\x72\57\x75\154\164\151\155\141\164\x65\x2d\x6d\145\155\142\145\x72\56\160\150\x70");
    }
    function isPhoneNumberAlreadyInUse($s9, $yU)
    {
        global $wpdb;
        MoUtility::processPhoneNumber($s9);
        $jO = "\x53\105\114\x45\103\x54\x20\140\165\163\145\x72\137\x69\144\140\x20\106\x52\117\x4d\x20\x60{$wpdb->prefix}\x75\x73\145\x72\155\145\164\141\140\40\127\110\105\x52\x45\40\140\x6d\145\164\x61\x5f\153\145\x79\140\40\x3d\40\x27{$yU}\x27\40\101\116\104\x20\x60\155\145\164\141\137\166\x61\x6c\x75\145\140\x20\75\x20\40\x27{$s9}\47";
        $rF = $wpdb->get_row($jO);
        return !MoUtility::isBlank($rF);
    }
    public function miniorange_register_um_script()
    {
        wp_register_script("\155\x6f\166\x75\155\160\x72\157\146\x69\154\145", MOV_URL . "\x69\x6e\143\154\165\x64\145\163\x2f\x6a\163\x2f\x6d\x6f\x75\x6d\x70\162\157\146\x69\x6c\x65\x2e\155\151\x6e\x2e\x6a\163", array("\152\x71\x75\x65\162\171"));
        wp_localize_script("\155\x6f\x76\165\155\160\x72\157\146\151\x6c\x65", "\155\157\165\x6d\x61\x63\x76\x61\162", array("\163\x69\164\x65\x55\122\114" => site_url(), "\x6f\x74\x70\x54\171\x70\145" => $this->_otpType, "\x65\x6d\x61\151\x6c\x4f\x74\x70\124\171\160\145" => $this->_typeEmailTag, "\160\150\x6f\x6e\x65\x4f\x74\160\124\x79\160\145" => $this->_typePhoneTag, "\x62\x6f\x74\150\117\x54\x50\124\171\x70\145" => $this->_typeBothTag, "\156\157\x6e\143\x65" => wp_create_nonce($this->_nonce), "\142\165\x74\x74\x6f\x6e\x54\145\170\x74" => mo_($this->_buttonText), "\151\155\147\125\x52\114" => MOV_LOADER_URL, "\x66\x6f\x72\x6d\113\x65\x79" => $this->_verifyFieldKey, "\145\155\x61\x69\154\126\141\154\165\x65" => $this->getUserData($this->_emailKey), "\x70\150\x6f\x6e\x65\x56\x61\154\x75\x65" => $this->getUserData($this->_phoneKey), "\160\x68\157\156\145\x4b\145\x79" => $this->_phoneKey));
        wp_enqueue_script("\x6d\x6f\x76\165\155\160\162\x6f\x66\151\x6c\x65");
    }
    private function userHasNotChangeData($k8, $bN)
    {
        $BT = $this->getUserData($k8);
        return strcasecmp($BT, $bN[$k8]) != 0;
    }
    public function miniorange_um_validation($bN, $k8 = "\x75\163\145\x72\x5f\x65\155\x61\151\x6c")
    {
        $tN = MoUtility::sanitizeCheck("\x6d\x6f\144\145", $bN);
        if (!(!$this->userHasNotChangeData($k8, $bN) && $tN != "\162\145\x67\x69\163\x74\x65\x72")) {
            goto wU;
        }
        $form = $this->getUmFormObj();
        if ($this->isValidationRequired($k8) && !SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto m1;
        }
        foreach ($bN as $yU => $Fo) {
            if ($yU === $this->_verifyFieldKey) {
                goto gP;
            }
            if ($yU === $this->_phoneKey) {
                goto pB;
            }
            goto JL;
            gP:
            $this->checkIntegrityAndValidateOTP($form, $Fo, $bN);
            goto JL;
            pB:
            $this->processPhoneNumbers($Fo, $form);
            JL:
            Ly:
        }
        fs:
        goto P7;
        m1:
        $yU = $this->isProfileVerificationEnabled() && $tN == "\x70\x72\x6f\146\x69\154\x65" ? $this->_phoneKey : $this->_emailKey;
        $form->add_error($yU, MoMessages::showMessage(MoMessages::PLEASE_VALIDATE));
        P7:
        wU:
    }
    private function isValidationRequired($k8)
    {
        return $this->isAccountVerificationEnabled() && $k8 === "\x75\163\x65\x72\137\x65\x6d\x61\151\154" || $this->isProfileVerificationEnabled() && $k8 === $this->_phoneKey;
    }
    public function miniorange_um_profile_validation($form, $yU, $bN)
    {
        if (!($yU === $this->_phoneKey)) {
            goto YI;
        }
        $this->miniorange_um_validation($bN, $this->_phoneKey);
        YI:
    }
    private function processPhoneNumbers($Fo, $form)
    {
        global $phoneLogic;
        if (MoUtility::validatePhoneNumber($Fo)) {
            goto XY;
        }
        $Kl = str_replace("\x23\x23\x70\x68\157\156\145\x23\x23", $Fo, $phoneLogic->_get_otp_invalid_format_message());
        $form->add_error($this->_phoneKey, $Kl);
        XY:
        $this->checkDuplicates($Fo, $this->_phoneKey);
    }
    private function checkIntegrityAndValidateOTP($form, $Fo, array $bN)
    {
        $this->checkIntegrity($form, $bN);
        if (!($form->count_errors() > 0)) {
            goto vW;
        }
        return;
        vW:
        $this->validateChallenge($this->getVerificationType(), NULL, $Fo);
        $this->checkFormSession($form);
    }
    private function checkIntegrity($Ns, array $bN)
    {
        if (!$this->isProfileVerificationEnabled()) {
            goto A2;
        }
        if (!SessionUtils::isPhoneVerifiedMatch($this->_formSessionVar, $bN[$this->_phoneKey])) {
            goto R_;
        }
        $Ns->add_error($this->_phoneKey, MoMessages::showMessage(MoMessages::PHONE_MISMATCH));
        R_:
        A2:
        if (!$this->isAccountVerificationEnabled()) {
            goto Ao;
        }
        if (!SessionUtils::isEmailVerifiedMatch($this->_formSessionVar, $bN[$this->_emailKey])) {
            goto rw;
        }
        $Ns->add_error($this->_emailKey, MoMessages::showMessage(MoMessages::EMAIL_MISMATCH));
        rw:
        Ao:
    }
    public function unsetOTPSessionVariables()
    {
        SessionUtils::unsetSession(array($this->_txSessionId, $this->_formSessionVar));
    }
    public function handle_post_verification($TG, $te, $j4, $xy, $sP, $yJ, $Cw)
    {
        SessionUtils::addStatus($this->_formSessionVar, self::VALIDATED, $Cw);
    }
    public function handle_failed_verification($te, $j4, $sP, $Cw)
    {
        SessionUtils::addStatus($this->_formSessionVar, self::VERIFICATION_FAILED, $Cw);
    }
    public function getPhoneNumberSelector($fv)
    {
        if (!($this->isFormEnabled() && $this->isProfileVerificationEnabled())) {
            goto cU;
        }
        array_push($fv, $this->_phoneFormId);
        cU:
        return $fv;
    }
    public function handleFormOptions()
    {
        if (MoUtility::areFormOptionsBeingSaved($this->getFormOption())) {
            goto qH;
        }
        return;
        qH:
        $this->_isFormEnabled = $this->sanitizeFormPOST("\165\155\137\160\x72\157\x66\151\154\145\x5f\145\156\141\142\x6c\145");
        $this->_otpType = $this->sanitizeFormPOST("\165\x6d\x5f\160\162\x6f\146\x69\154\145\x5f\x65\156\141\142\154\145\x5f\164\171\x70\145");
        $this->_buttonText = $this->sanitizeFormPOST("\165\x6d\x5f\160\x72\157\x66\x69\154\x65\x5f\142\x75\164\164\157\156\137\164\145\170\x74");
        $this->_restrictDuplicates = $this->sanitizeFormPOST("\x75\x6d\x5f\160\162\x6f\x66\151\154\x65\x5f\x72\145\x73\164\x72\151\143\164\137\144\165\160\x6c\x69\143\x61\x74\145\x73");
        $this->_phoneKey = $this->sanitizeFormPOST("\165\155\x5f\160\162\x6f\146\151\154\145\137\160\x68\x6f\156\x65\137\x6b\145\171");
        if (!$this->basicValidationCheck(BaseMessages::UM_PROFILE_CHOOSE)) {
            goto rB;
        }
        update_mo_option("\x75\x6d\137\160\162\157\146\x69\x6c\145\137\x65\x6e\141\142\x6c\x65", $this->_isFormEnabled);
        update_mo_option("\165\x6d\x5f\160\162\157\x66\151\154\x65\137\145\156\141\142\x6c\x65\x5f\x74\x79\x70\145", $this->_otpType);
        update_mo_option("\x75\155\137\x70\162\x6f\x66\151\154\x65\x5f\x62\x75\x74\x74\157\x6e\137\164\145\170\x74", $this->_buttonText);
        update_mo_option("\x75\155\x5f\x70\x72\x6f\x66\x69\154\145\x5f\162\x65\163\164\x72\x69\x63\x74\x5f\144\165\x70\154\x69\143\x61\x74\145\x73", $this->_restrictDuplicates);
        update_mo_option("\x75\x6d\137\x70\162\157\x66\151\x6c\145\x5f\160\150\157\156\x65\137\x6b\145\x79", $this->_phoneKey);
        rB:
    }
}
