<?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\FormHandler;
use OTP\Objects\IFormHandler;
use OTP\Objects\VerificationType;
use OTP\Traits\Instance;
use ReflectionException;
class WPFormsPlugin extends FormHandler implements IFormHandler
{
    use Instance;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = TRUE;
        $this->_formSessionVar = FormSessionVars::WPFORM;
        $this->_phoneFormId = array();
        $this->_formKey = "\127\120\106\x4f\x52\x4d\123";
        $this->_typePhoneTag = "\x6d\x6f\137\x77\x70\x66\x6f\x72\155\x5f\160\x68\157\x6e\x65\137\145\156\x61\142\x6c\x65";
        $this->_typeEmailTag = "\155\x6f\137\167\160\146\x6f\x72\x6d\x5f\x65\x6d\141\x69\x6c\137\x65\x6e\x61\x62\154\145";
        $this->_typeBothTag = "\155\x6f\x5f\167\160\x66\x6f\162\x6d\x5f\142\x6f\x74\150\137\145\156\x61\x62\154\145";
        $this->_formName = mo_("\x57\120\x46\157\x72\x6d\x73");
        $this->_isFormEnabled = get_mo_option("\167\160\x66\157\162\x6d\x5f\x65\156\141\x62\154\145");
        $this->_buttonText = get_mo_option("\x77\x70\x66\157\x72\x6d\x73\x5f\x62\165\x74\164\157\x6e\x5f\x74\x65\x78\x74");
        $this->_buttonText = !MoUtility::isBlank($this->_buttonText) ? $this->_buttonText : mo_("\x53\145\156\x64\x20\x4f\124\120");
        $this->_generateOTPAction = "\x6d\151\x6e\151\x6f\162\x61\156\x67\145\55\167\160\x66\x6f\x72\x6d\x2d\163\x65\x6e\144\55\157\x74\x70";
        $this->_validateOTPAction = "\155\x69\156\x69\x6f\x72\x61\156\147\x65\55\167\x70\x66\x6f\x72\155\55\x76\145\162\x69\146\x79\x2d\x63\157\144\145";
        $this->_formDocuments = MoOTPDocs::WP_FORMS_LINK;
        parent::__construct();
    }
    function handleForm()
    {
        $this->_otpType = get_mo_option("\x77\x70\146\157\162\x6d\137\145\156\141\x62\154\145\137\x74\x79\160\145");
        $this->_formDetails = maybe_unserialize(get_mo_option("\167\160\x66\x6f\x72\x6d\x5f\x66\x6f\x72\155\163"));
        if (!empty($this->_formDetails)) {
            goto KN;
        }
        return;
        KN:
        if (!($this->_otpType === $this->_typePhoneTag || $this->_otpType === $this->_typeBothTag)) {
            goto Cw;
        }
        foreach ($this->_formDetails as $yU => $Fo) {
            array_push($this->_phoneFormId, "\43\x77\160\x66\x6f\x72\155\x73\55" . $yU . "\55\146\151\x65\x6c\144\137" . $Fo["\x70\x68\x6f\156\x65\x6b\x65\171"]);
            t4:
        }
        IM:
        Cw:
        add_filter("\167\x70\146\157\x72\x6d\x73\x5f\160\x72\x6f\143\145\x73\163\x5f\x69\156\x69\164\151\x61\154\x5f\x65\162\x72\157\162\163", array($this, "\x76\x61\x6c\x69\144\141\164\145\x46\157\x72\x6d"), 1, 2);
        add_action("\167\x70\137\145\x6e\161\165\145\x75\x65\137\163\143\162\151\x70\x74\x73", array($this, "\x6d\x6f\137\x65\x6e\161\x75\145\x75\x65\137\x77\160\x66\x6f\162\155\x73"));
        add_action("\167\160\x5f\x61\x6a\141\170\137{$this->_generateOTPAction}", array($this, "\137\x73\x65\x6e\144\x5f\x6f\164\160"));
        add_action("\x77\160\x5f\x61\152\x61\x78\137\x6e\157\160\x72\151\166\137{$this->_generateOTPAction}", array($this, "\x5f\x73\x65\156\144\137\x6f\x74\x70"));
        add_action("\x77\x70\137\141\x6a\141\x78\137{$this->_validateOTPAction}", array($this, "\160\x72\157\x63\x65\x73\x73\x46\157\x72\x6d\101\x6e\x64\126\141\x6c\x69\144\141\164\x65\x4f\124\x50"));
        add_action("\x77\x70\137\141\x6a\x61\x78\x5f\156\x6f\x70\x72\x69\166\137{$this->_validateOTPAction}", array($this, "\160\162\x6f\143\x65\163\x73\106\x6f\162\155\x41\x6e\144\x56\x61\154\151\x64\141\x74\x65\x4f\124\120"));
    }
    function mo_enqueue_wpforms()
    {
        wp_register_script("\x6d\x6f\x77\x70\146\x6f\162\155\x73", MOV_URL . "\x69\156\x63\x6c\x75\144\x65\163\x2f\x6a\x73\57\155\x6f\x77\x70\x66\157\162\155\163\x2e\x6d\151\x6e\56\152\163", array("\x6a\161\x75\145\x72\171"));
        wp_localize_script("\155\x6f\x77\x70\146\157\x72\155\x73", "\x6d\157\x77\x70\x66\157\162\155\x73", array("\x73\151\164\145\125\x52\x4c" => wp_ajax_url(), "\157\x74\x70\124\x79\x70\x65" => $this->ajaxProcessingFields(), "\146\157\162\x6d\x44\x65\x74\141\x69\154\163" => $this->_formDetails, "\x62\x75\164\164\x6f\x6e\x74\145\170\x74" => $this->_buttonText, "\166\141\154\x69\144\141\x74\x65\x64" => $this->getSessionDetails(), "\x69\x6d\x67\125\122\114" => MOV_LOADER_URL, "\x66\x69\145\154\144\x54\x65\170\164" => mo_("\105\156\164\145\162\40\x4f\x54\120\40\150\x65\x72\x65"), "\x67\x6e\x6f\x6e\x63\145" => wp_create_nonce($this->_nonce), "\156\x6f\x6e\143\x65\113\145\171" => wp_create_nonce($this->_nonceKey), "\x76\x6e\x6f\156\143\x65" => wp_create_nonce($this->_nonce), "\x67\141\143\x74\151\157\156" => $this->_generateOTPAction, "\x76\141\x63\x74\x69\157\156" => $this->_validateOTPAction));
        wp_enqueue_script("\x6d\x6f\x77\x70\146\x6f\x72\x6d\163");
    }
    function getSessionDetails()
    {
        return array(VerificationType::EMAIL => SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, VerificationType::EMAIL), VerificationType::PHONE => SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, VerificationType::PHONE));
    }
    function _send_otp()
    {
        MoUtility::initialize_transaction($this->_formSessionVar);
        if ("\155\157\x5f\x77\160\146\x6f\x72\155\x5f" . $_POST["\x6f\164\x70\x54\x79\x70\x65"] . "\x5f\145\x6e\x61\x62\154\x65" === $this->_typePhoneTag) {
            goto z6;
        }
        $this->_processEmailAndSendOTP($_POST);
        goto h2;
        z6:
        $this->_processPhoneAndSendOTP($_POST);
        h2:
    }
    private function _processEmailAndSendOTP($BT)
    {
        if (!MoUtility::sanitizeCheck("\165\x73\x65\162\x5f\145\x6d\141\151\154", $BT)) {
            goto r2;
        }
        SessionUtils::addEmailVerified($this->_formSessionVar, $BT["\x75\x73\145\162\x5f\145\x6d\x61\151\x6c"]);
        $this->sendChallenge('', $BT["\165\x73\x65\x72\137\x65\x6d\x61\151\x6c"], NULL, NULL, VerificationType::EMAIL);
        goto V2;
        r2:
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_EMAIL), MoConstants::ERROR_JSON_TYPE));
        V2:
    }
    private function _processPhoneAndSendOTP($BT)
    {
        if (!MoUtility::sanitizeCheck("\165\163\145\162\137\x70\150\x6f\x6e\x65", $BT)) {
            goto tg;
        }
        SessionUtils::addPhoneVerified($this->_formSessionVar, $BT["\x75\163\x65\162\137\x70\150\x6f\x6e\145"]);
        $this->sendChallenge('', NULL, NULL, $BT["\165\163\145\162\137\160\x68\x6f\x6e\x65"], VerificationType::PHONE);
        goto p8;
        tg:
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_PHONE), MoConstants::ERROR_JSON_TYPE));
        p8:
    }
    function processFormAndValidateOTP()
    {
        $this->validateAjaxRequest();
        $this->checkIfOTPSent();
        $this->checkIntegrityAndValidateOTP($_POST);
    }
    function checkIfOTPSent()
    {
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto d5;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_VERIFY_CODE), MoConstants::ERROR_JSON_TYPE));
        d5:
    }
    private function checkIntegrityAndValidateOTP($BT)
    {
        $this->checkIntegrity($BT);
        $this->validateChallenge($BT["\157\x74\160\x54\x79\160\145"], NULL, $BT["\157\x74\x70\x5f\164\x6f\x6b\145\156"]);
        if (SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, $BT["\x6f\164\x70\124\x79\x70\x65"])) {
            goto Qz;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::INVALID_OTP), MoConstants::ERROR_JSON_TYPE));
        goto md;
        Qz:
        wp_send_json(MoUtility::createJson(MoConstants::SUCCESS_JSON_TYPE, MoConstants::SUCCESS_JSON_TYPE));
        md:
    }
    private function checkIntegrity($BT)
    {
        if ($BT["\x6f\x74\160\124\171\160\145"] === "\160\150\x6f\x6e\145") {
            goto ww;
        }
        if (SessionUtils::isEmailVerifiedMatch($this->_formSessionVar, $BT["\165\163\x65\x72\137\x65\x6d\x61\151\x6c"])) {
            goto e1;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::EMAIL_MISMATCH), MoConstants::ERROR_JSON_TYPE));
        e1:
        goto ea;
        ww:
        if (SessionUtils::isPhoneVerifiedMatch($this->_formSessionVar, $BT["\x75\x73\x65\x72\137\160\x68\x6f\x6e\145"])) {
            goto z2;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::PHONE_MISMATCH), MoConstants::ERROR_JSON_TYPE));
        z2:
        ea:
    }
    public function validateForm($errors, $nt)
    {
        $yg = $nt["\x69\x64"];
        if (array_key_exists($yg, $this->_formDetails)) {
            goto ss;
        }
        return $errors;
        ss:
        $nt = $this->_formDetails[$yg];
        if (empty($errors)) {
            goto bl;
        }
        return $errors;
        bl:
        if (!($this->_otpType === $this->_typeEmailTag || $this->_otpType === $this->_typeBothTag)) {
            goto UJ;
        }
        $errors = $this->processEmail($nt, $errors, $yg);
        UJ:
        if (!($this->_otpType === $this->_typePhoneTag || $this->_otpType === $this->_typeBothTag)) {
            goto kP;
        }
        $errors = $this->processPhone($nt, $errors, $yg);
        kP:
        if (!empty($errors)) {
            goto na;
        }
        $this->unsetOTPSessionVariables();
        na:
        return $errors;
    }
    function processEmail($nt, $errors, $yg)
    {
        $Kw = $nt["\x65\155\x61\151\154\x6b\145\x79"];
        if (SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, VerificationType::EMAIL)) {
            goto qj;
        }
        $errors[$yg][$Kw] = MoMessages::showMessage(MoMessages::ENTER_VERIFY_CODE);
        qj:
        if (SessionUtils::isEmailVerifiedMatch($this->_formSessionVar, $_POST["\167\160\x66\x6f\162\x6d\163"]["\146\x69\x65\x6c\144\163"][$Kw])) {
            goto YP;
        }
        $errors[$yg][$Kw] = MoMessages::showMessage(MoMessages::EMAIL_MISMATCH);
        YP:
        return $errors;
    }
    function processPhone($nt, $errors, $yg)
    {
        $Kw = $nt["\x70\150\157\x6e\145\x6b\x65\171"];
        if (SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, VerificationType::PHONE)) {
            goto Wj;
        }
        $errors[$yg][$Kw] = MoMessages::showMessage(MoMessages::ENTER_VERIFY_CODE);
        Wj:
        if (SessionUtils::isPhoneVerifiedMatch($this->_formSessionVar, $_POST["\x77\x70\146\x6f\162\x6d\x73"]["\146\x69\145\154\x64\x73"][$Kw])) {
            goto X03;
        }
        $errors[$yg][$Kw] = MoMessages::showMessage(MoMessages::PHONE_MISMATCH);
        X03:
        return $errors;
    }
    function handle_failed_verification($te, $j4, $sP, $Cw)
    {
        SessionUtils::addStatus($this->_formSessionVar, self::VERIFICATION_FAILED, $Cw);
    }
    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->_otpType === $this->_typePhoneTag || $this->_otpType === $this->_typeBothTag))) {
            goto MkP;
        }
        $fv = array_merge($fv, $this->_phoneFormId);
        MkP:
        return $fv;
    }
    function handleFormOptions()
    {
        if (MoUtility::areFormOptionsBeingSaved($this->getFormOption())) {
            goto EuF;
        }
        return;
        EuF:
        $form = $this->parseFormDetails();
        $this->_isFormEnabled = $this->sanitizeFormPOST("\167\x70\x66\157\x72\x6d\137\x65\156\x61\x62\154\x65");
        $this->_otpType = $this->sanitizeFormPOST("\x77\160\x66\157\x72\x6d\x5f\145\156\141\142\154\145\137\164\x79\160\x65");
        $this->_buttonText = $this->sanitizeFormPOST("\x77\x70\x66\x6f\x72\155\x73\x5f\142\x75\x74\164\x6f\x6e\x5f\x74\x65\170\164");
        $this->_formDetails = !empty($form) ? $form : '';
        update_mo_option("\x77\x70\x66\157\162\x6d\x5f\145\156\141\x62\x6c\145", $this->_isFormEnabled);
        update_mo_option("\167\x70\146\157\162\x6d\x5f\145\x6e\x61\142\154\x65\x5f\164\x79\160\x65", $this->_otpType);
        update_mo_option("\x77\160\x66\x6f\162\x6d\163\137\142\x75\x74\164\157\x6e\137\x74\145\x78\x74", $this->_buttonText);
        update_mo_option("\x77\x70\146\157\x72\155\x5f\146\157\x72\x6d\163", maybe_serialize($this->_formDetails));
    }
    function parseFormDetails()
    {
        $form = array();
        if (array_key_exists("\x77\x70\146\x6f\162\x6d\x5f\146\x6f\x72\155", $_POST)) {
            goto ycN;
        }
        return $form;
        ycN:
        foreach (array_filter($_POST["\x77\160\x66\157\x72\155\137\x66\x6f\162\155"]["\146\157\x72\155"]) as $yU => $Fo) {
            $nt = $this->getFormDataFromID($Fo);
            if (!MoUtility::isBlank($nt)) {
                goto d0m;
            }
            goto dsB;
            d0m:
            $U7 = $this->getFieldIDs($_POST, $yU, $nt);
            $form[$Fo] = array("\145\155\141\x69\x6c\x6b\145\x79" => $U7["\x65\x6d\141\151\154\113\145\171"], "\x70\x68\x6f\x6e\145\x6b\145\171" => $U7["\160\150\x6f\x6e\145\x4b\145\171"], "\166\145\162\x69\x66\x79\113\x65\171" => $U7["\166\145\x72\151\x66\171\113\x65\x79"], "\160\x68\157\x6e\x65\137\x73\150\157\x77" => $_POST["\x77\160\146\157\162\x6d\x5f\x66\x6f\x72\x6d"]["\160\150\x6f\156\145\x6b\145\171"][$yU], "\145\155\141\x69\x6c\137\163\150\157\167" => $_POST["\x77\x70\146\157\162\x6d\137\x66\x6f\x72\155"]["\145\155\x61\x69\154\153\145\x79"][$yU], "\x76\145\x72\x69\x66\171\137\x73\150\157\x77" => $_POST["\167\x70\x66\157\162\x6d\x5f\x66\157\x72\x6d"]["\x76\145\x72\x69\146\171\x4b\x65\x79"][$yU]);
            dsB:
        }
        eTm:
        return $form;
    }
    private function getFormDataFromID($yg)
    {
        if (!Moutility::isBlank($yg)) {
            goto J0j;
        }
        return '';
        J0j:
        $form = get_post(absint($yg));
        if (!MoUtility::isBlank($yg)) {
            goto znR;
        }
        return '';
        znR:
        return wp_unslash(json_decode($form->post_content));
    }
    private function getFieldIDs($BT, $yU, $nt)
    {
        $U7 = array("\x65\155\141\x69\x6c\113\x65\171" => '', "\x70\x68\157\x6e\145\x4b\x65\x79" => '', "\x76\145\x72\x69\x66\171\113\145\171" => '');
        if (!empty($BT)) {
            goto DsP;
        }
        return $U7;
        DsP:
        foreach ($nt->fields as $bj) {
            if (property_exists($bj, "\154\x61\142\145\x6c")) {
                goto jn3;
            }
            goto N21;
            jn3:
            if (!(strcasecmp($bj->label, $BT["\167\x70\x66\157\162\x6d\137\146\x6f\162\x6d"]["\145\x6d\x61\x69\154\153\x65\171"][$yU]) === 0)) {
                goto lDZ;
            }
            $U7["\145\x6d\x61\x69\154\x4b\145\x79"] = $bj->id;
            lDZ:
            if (!(strcasecmp($bj->label, $BT["\167\160\x66\x6f\162\155\x5f\x66\x6f\x72\155"]["\160\150\157\x6e\x65\153\x65\x79"][$yU]) === 0)) {
                goto uYt;
            }
            $U7["\160\150\x6f\156\145\x4b\x65\171"] = $bj->id;
            uYt:
            if (!(strcasecmp($bj->label, $BT["\167\x70\146\x6f\162\155\137\x66\x6f\x72\155"]["\x76\x65\x72\x69\146\171\x4b\145\171"][$yU]) === 0)) {
                goto zJY;
            }
            $U7["\166\x65\162\151\146\x79\113\145\171"] = $bj->id;
            zJY:
            N21:
        }
        a2x:
        return $U7;
    }
}
