<?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 RegistrationMagicForm extends FormHandler implements IFormHandler
{
    use Instance;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = FALSE;
        $this->_formSessionVar = FormSessionVars::CRF_DEFAULT_REG;
        $this->_typePhoneTag = "\155\157\x5f\x63\x72\x66\137\160\150\x6f\x6e\x65\x5f\145\156\141\x62\x6c\x65";
        $this->_typeEmailTag = "\155\157\x5f\143\162\146\137\x65\x6d\141\x69\154\137\x65\x6e\141\x62\x6c\x65";
        $this->_typeBothTag = "\x6d\x6f\x5f\x63\x72\x66\137\142\157\164\150\137\145\156\x61\142\x6c\x65";
        $this->_formKey = "\x43\x52\106\x5f\x46\x4f\122\x4d";
        $this->_formName = mo_("\x43\x75\x73\x74\x6f\155\40\125\x73\145\x72\x20\x52\x65\x67\x69\163\x74\162\x61\164\151\157\x6e\40\x46\157\162\x6d\x20\x42\165\151\x6c\144\145\162\40\50\x52\145\x67\x69\163\164\x72\x61\164\x69\x6f\x6e\x20\115\x61\147\151\x63\51");
        $this->_isFormEnabled = get_mo_option("\143\162\146\137\x64\145\146\141\165\154\164\137\x65\156\141\x62\x6c\145");
        $this->_phoneFormId = array();
        $this->_formDocuments = MoOTPDocs::CRF_FORM_ENABLE;
        parent::__construct();
    }
    function handleForm()
    {
        $this->_otpType = get_mo_option("\143\162\146\x5f\145\156\141\142\154\x65\x5f\x74\x79\x70\145");
        $this->_formDetails = maybe_unserialize(get_mo_option("\x63\x72\x66\137\157\x74\x70\137\145\x6e\141\142\x6c\145\x64"));
        if (!empty($this->_formDetails)) {
            goto wr;
        }
        return;
        wr:
        foreach ($this->_formDetails as $yU => $Fo) {
            array_push($this->_phoneFormId, "\x69\x6e\160\165\164\x5b\x6e\141\155\145\75" . $this->getFieldID($Fo["\160\x68\x6f\x6e\x65\x6b\145\x79"], $yU) . "\135");
            bE:
        }
        Xd:
        if ($this->checkIfPromptForOTP()) {
            goto Jj;
        }
        return;
        Jj:
        $this->_handle_crf_form_submit($_REQUEST);
    }
    private function checkIfPromptForOTP()
    {
        if (!(array_key_exists("\157\x70\x74\x69\157\156", $_POST) || !array_key_exists("\162\155\x5f\146\157\162\x6d\137\x73\x75\142\x5f\x69\144", $_POST))) {
            goto Ns;
        }
        return FALSE;
        Ns:
        foreach ($this->_formDetails as $yU => $Fo) {
            if (!(strpos($_POST["\x72\x6d\x5f\146\x6f\x72\x6d\137\163\x75\142\137\x69\144"], "\x66\157\162\155\137" . $yU . "\x5f") !== FALSE)) {
                goto Nh;
            }
            MoUtility::initialize_transaction($this->_formSessionVar);
            SessionUtils::setFormOrFieldId($this->_formSessionVar, $yU);
            return TRUE;
            Nh:
            mq:
        }
        q0:
        return FALSE;
    }
    private function isPhoneVerificationEnabled()
    {
        $HK = $this->getVerificationType();
        return $HK === VerificationType::PHONE || $HK === VerificationType::BOTH;
    }
    private function isEmailVerificationEnabled()
    {
        $HK = $this->getVerificationType();
        return $HK === VerificationType::EMAIL || $HK === VerificationType::BOTH;
    }
    private function _handle_crf_form_submit($O6)
    {
        $R7 = $this->isEmailVerificationEnabled() ? $this->getCRFEmailFromRequest($O6) : '';
        $s9 = $this->isPhoneVerificationEnabled() ? $this->getCRFPhoneFromRequest($O6) : '';
        $this->miniorange_crf_user($R7, isset($O6["\x75\163\x65\x72\137\156\141\155\145"]) ? $O6["\x75\x73\145\162\x5f\156\141\155\145"] : NULL, $s9);
        $this->checkIfValidated();
    }
    private function checkIfValidated()
    {
        if (!SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, $this->getVerificationType())) {
            goto Zn;
        }
        $this->unsetOTPSessionVariables();
        Zn:
    }
    private function getCRFEmailFromRequest($O6)
    {
        $Cm = SessionUtils::getFormOrFieldId($this->_formSessionVar);
        $Nk = $this->_formDetails[$Cm]["\145\x6d\x61\x69\x6c\153\x65\171"];
        return $this->getFormPostSubmittedValue($this->getFieldID($Nk, $Cm), $O6);
    }
    private function getCRFPhoneFromRequest($O6)
    {
        $Cm = SessionUtils::getFormOrFieldId($this->_formSessionVar);
        $Px = $this->_formDetails[$Cm]["\160\150\x6f\x6e\x65\153\145\x79"];
        return $this->getFormPostSubmittedValue($this->getFieldID($Px, $Cm), $O6);
    }
    private function getFormPostSubmittedValue($RD, $O6)
    {
        return isset($O6[$RD]) ? $O6[$RD] : '';
    }
    private function getFieldID($yU, $Ae)
    {
        global $wpdb;
        $xe = $wpdb->prefix . "\x72\x6d\137\x66\151\145\154\144\163";
        $eY = $wpdb->get_row("\x53\x45\114\x45\x43\x54\40\x2a\40\106\x52\x4f\115\x20{$xe}\x20\x77\150\x65\x72\x65\x20\146\x6f\162\x6d\x5f\151\x64\x20\x3d\40\x27" . $Ae . "\x27\x20\x61\x6e\x64\40\146\x69\x65\154\x64\137\154\x61\x62\145\x6c\x20\75\47" . $yU . "\x27");
        return isset($eY) ? ($eY->field_type == "\115\x6f\x62\151\x6c\145" ? "\124\145\170\164\142\157\x78" : $eY->field_type) . "\137" . $eY->field_id : "\156\x75\154\154";
    }
    private function miniorange_crf_user($j4, $F1, $sP)
    {
        MoUtility::initialize_transaction($this->_formSessionVar);
        $errors = new WP_Error();
        if (strcasecmp($this->_otpType, $this->_typePhoneTag) == 0) {
            goto QX;
        }
        if (strcasecmp($this->_otpType, $this->_typeBothTag) == 0) {
            goto JB;
        }
        $this->sendChallenge($F1, $j4, $errors, $sP, VerificationType::EMAIL);
        goto I2;
        JB:
        $this->sendChallenge($F1, $j4, $errors, $sP, VerificationType::BOTH);
        I2:
        goto zR;
        QX:
        $this->sendChallenge($F1, $j4, $errors, $sP, VerificationType::PHONE);
        zR:
    }
    function handle_failed_verification($te, $j4, $sP, $Cw)
    {
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto gk;
        }
        return;
        gk:
        $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 handle_post_verification($TG, $te, $j4, $xy, $sP, $yJ, $Cw)
    {
        SessionUtils::addStatus($this->_formSessionVar, self::VALIDATED, $Cw);
    }
    public function unsetOTPSessionVariables()
    {
        SessionUtils::unsetSession(array($this->_txSessionId, $this->_formSessionVar));
    }
    public function getPhoneNumberSelector($fv)
    {
        if (!($this->isFormEnabled() && $this->isPhoneVerificationEnabled())) {
            goto vZ;
        }
        $fv = array_merge($fv, $this->_phoneFormId);
        vZ:
        return $fv;
    }
    function handleFormOptions()
    {
        if (MoUtility::areFormOptionsBeingSaved($this->getFormOption())) {
            goto B7;
        }
        return;
        B7:
        $form = $this->parseFormDetails();
        $this->_formDetails = !empty($form) ? $form : '';
        $this->_isFormEnabled = $this->sanitizeFormPOST("\143\162\x66\x5f\144\x65\146\141\165\154\x74\x5f\x65\x6e\141\142\x6c\x65");
        $this->_otpType = $this->sanitizeFormPOST("\143\162\x66\137\145\x6e\141\142\x6c\145\137\x74\x79\x70\145");
        update_mo_option("\x63\162\x66\x5f\144\145\146\x61\165\x6c\164\x5f\x65\156\141\142\x6c\x65", $this->_isFormEnabled);
        update_mo_option("\143\162\x66\137\x65\x6e\x61\x62\x6c\x65\x5f\x74\x79\x70\x65", $this->_otpType);
        update_mo_option("\x63\x72\x66\x5f\x6f\164\160\137\x65\156\141\x62\x6c\x65\x64", maybe_serialize($this->_formDetails));
    }
    function parseFormDetails()
    {
        $form = array();
        if (!(!array_key_exists("\x63\x72\x66\137\x66\x6f\x72\x6d", $_POST) && empty($_POST["\x63\162\x66\137\146\x6f\x72\155"]["\146\x6f\162\155"]))) {
            goto fk;
        }
        return $form;
        fk:
        foreach (array_filter($_POST["\143\x72\146\x5f\146\x6f\162\155"]["\146\x6f\x72\155"]) as $yU => $Fo) {
            $form[$Fo] = array("\145\155\141\x69\x6c\x6b\145\x79" => $_POST["\x63\x72\x66\x5f\146\x6f\162\155"]["\145\155\x61\x69\154\153\145\171"][$yU], "\160\150\157\156\x65\153\x65\171" => $_POST["\x63\162\146\137\146\157\x72\x6d"]["\160\x68\157\x6e\x65\153\145\171"][$yU], "\x65\155\141\x69\x6c\x5f\x73\x68\x6f\x77" => $_POST["\x63\x72\146\137\x66\x6f\162\x6d"]["\x65\x6d\141\x69\x6c\153\x65\171"][$yU], "\160\x68\157\156\x65\137\163\x68\x6f\x77" => $_POST["\143\x72\x66\137\146\x6f\x72\x6d"]["\160\150\157\156\x65\x6b\145\171"][$yU]);
            fb:
        }
        UL:
        return $form;
    }
}
