<?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\VerificationType;
use OTP\Traits\Instance;
use ReflectionException;
class FormidableForm extends FormHandler implements IFormHandler
{
    use Instance;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = TRUE;
        $this->_formSessionVar = FormSessionVars::FORMIDABLE_FORM;
        $this->_typePhoneTag = "\155\157\x5f\x66\x72\155\x5f\x66\157\162\155\x5f\160\150\x6f\x6e\x65\x5f\x65\156\141\x62\154\145";
        $this->_typeEmailTag = "\x6d\x6f\x5f\146\x72\x6d\x5f\146\157\162\x6d\137\145\x6d\141\x69\x6c\137\145\156\x61\142\x6c\145";
        $this->_formKey = "\x46\117\122\x4d\x49\x44\x41\x42\114\105\x5f\x46\x4f\x52\x4d";
        $this->_formName = mo_("\106\x6f\x72\155\x69\144\141\142\x6c\x65\40\x46\157\x72\x6d\163");
        $this->_isFormEnabled = get_mo_option("\x66\x72\x6d\137\x66\157\x72\x6d\137\145\156\x61\x62\x6c\145");
        $this->_buttonText = get_mo_option("\x66\x72\x6d\x5f\x62\165\x74\x74\157\x6e\137\x74\x65\x78\x74");
        $this->_buttonText = !MoUtility::isBlank($this->_buttonText) ? $this->_buttonText : mo_("\x43\x6c\151\x63\x6b\40\x48\x65\162\145\40\x74\157\x20\163\145\x6e\144\x20\x4f\x54\x50");
        $this->_generateOTPAction = "\x6d\151\156\151\157\x72\141\156\x67\x65\137\146\x72\x6d\x5f\x67\145\x6e\x65\x72\141\x74\145\x5f\157\x74\x70";
        $this->_formDocuments = MoOTPDocs::FORMIDABLE_FORM_LINK;
        parent::__construct();
    }
    function handleForm()
    {
        $this->_otpType = get_mo_option("\x66\x72\155\x5f\x66\x6f\x72\x6d\x5f\x65\156\x61\142\154\145\137\164\171\160\145");
        $this->_formDetails = maybe_unserialize(get_mo_option("\146\x72\x6d\x5f\146\157\162\x6d\137\157\x74\160\137\x65\156\x61\142\x6c\145\144"));
        $this->_phoneFormId = array();
        if (!(empty($this->_formDetails) || !$this->_isFormEnabled)) {
            goto Mp;
        }
        return;
        Mp:
        foreach ($this->_formDetails as $yU => $Fo) {
            array_push($this->_phoneFormId, "\x23" . $Fo["\160\x68\157\x6e\145\153\x65\171"] . "\40\x69\x6e\160\x75\x74");
            Pl:
        }
        SQ:
        add_filter("\146\162\x6d\x5f\166\x61\x6c\151\x64\141\164\x65\137\146\151\145\x6c\x64\137\x65\156\164\162\x79", array($this, "\155\151\x6e\151\157\162\x61\156\147\x65\137\x6f\x74\160\x5f\166\141\154\x69\x64\141\164\151\x6f\156"), 11, 4);
        add_action("\x77\x70\137\141\x6a\x61\170\137{$this->_generateOTPAction}", array($this, "\x5f\x73\145\156\x64\x5f\x6f\164\x70\137\146\x72\x6d\137\x61\152\x61\x78"));
        add_action("\167\x70\x5f\x61\152\x61\170\137\x6e\x6f\x70\162\x69\166\137{$this->_generateOTPAction}", array($this, "\137\x73\145\x6e\144\137\157\164\160\x5f\146\x72\x6d\137\141\152\x61\170"));
        add_action("\167\160\x5f\145\x6e\161\165\145\x75\145\x5f\163\x63\x72\x69\x70\x74\x73", array($this, "\155\x69\x6e\x69\157\162\141\x6e\147\x65\x5f\162\145\147\x69\x73\x74\145\162\137\146\157\162\x6d\x69\144\x61\x62\154\145\x5f\163\143\x72\151\x70\164"));
    }
    function miniorange_register_formidable_script()
    {
        wp_register_script("\x6d\x6f\x66\x6f\x72\x6d\x69\144\x61\142\154\145", MOV_URL . "\x69\156\x63\x6c\x75\x64\145\163\x2f\x6a\x73\x2f\x66\x6f\162\x6d\151\144\x61\142\154\x65\56\155\x69\x6e\x2e\152\x73", array("\152\x71\165\145\x72\171"));
        wp_localize_script("\x6d\157\146\157\162\x6d\x69\144\x61\142\x6c\x65", "\x6d\157\x66\x6f\162\x6d\x69\144\x61\x62\154\x65", array("\163\x69\164\145\125\x52\114" => wp_ajax_url(), "\157\164\160\124\171\x70\x65" => $this->_otpType, "\146\x6f\162\x6d\x6b\145\x79" => strcasecmp($this->_otpType, $this->_typePhoneTag) == 0 ? "\160\x68\x6f\156\145\153\x65\171" : "\x65\155\141\151\154\153\145\x79", "\x6e\157\156\x63\x65" => wp_create_nonce($this->_nonce), "\142\165\x74\x74\x6f\156\164\145\x78\x74" => mo_($this->_buttonText), "\151\155\x67\x55\122\114" => MOV_LOADER_URL, "\146\157\162\x6d\x73" => $this->_formDetails, "\147\x65\x6e\145\x72\x61\x74\145\125\122\114" => $this->_generateOTPAction));
        wp_enqueue_script("\155\x6f\146\157\162\x6d\x69\144\141\142\x6c\x65");
    }
    function _send_otp_frm_ajax()
    {
        $this->validateAjaxRequest();
        if ($this->_otpType == $this->_typePhoneTag) {
            goto la;
        }
        $this->_send_frm_otp_to_email($_POST);
        goto o3;
        la:
        $this->_send_frm_otp_to_phone($_POST);
        o3:
    }
    function _send_frm_otp_to_phone($BT)
    {
        if (!MoUtility::sanitizeCheck("\165\163\145\162\137\160\150\157\x6e\x65", $BT)) {
            goto Kd;
        }
        $this->sendOTP(trim($BT["\165\163\x65\162\137\x70\150\x6f\156\x65"]), NULL, trim($BT["\x75\x73\145\162\x5f\x70\150\157\x6e\145"]), VerificationType::PHONE);
        goto xD;
        Kd:
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_PHONE), MoConstants::ERROR_JSON_TYPE));
        xD:
    }
    function _send_frm_otp_to_email($BT)
    {
        if (!MoUtility::sanitizeCheck("\x75\x73\145\162\137\x65\155\x61\151\154", $BT)) {
            goto o5;
        }
        $this->sendOTP($BT["\165\163\x65\162\137\145\x6d\x61\x69\154"], $BT["\165\x73\145\162\x5f\x65\x6d\141\151\x6c"], NULL, VerificationType::EMAIL);
        goto Hs;
        o5:
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_EMAIL), MoConstants::ERROR_JSON_TYPE));
        Hs:
    }
    private function sendOTP($hU, $Hu, $YN, $Cw)
    {
        MoUtility::initialize_transaction($this->_formSessionVar);
        if ($Cw === VerificationType::PHONE) {
            goto Mv;
        }
        SessionUtils::addEmailVerified($this->_formSessionVar, $hU);
        goto AI;
        Mv:
        SessionUtils::addPhoneVerified($this->_formSessionVar, $hU);
        AI:
        $this->sendChallenge('', $Hu, NULL, $YN, $Cw);
    }
    function miniorange_otp_validation($errors, $bj, $Fo, $bN)
    {
        if (!($this->getFieldId("\x76\145\162\151\146\171\137\x73\x68\157\167", $bj) !== $bj->id)) {
            goto HH;
        }
        return $errors;
        HH:
        if (MoUtility::isBlank($errors)) {
            goto BU;
        }
        return $errors;
        BU:
        if ($this->hasOTPBeenSent($errors, $bj)) {
            goto zV;
        }
        return $errors;
        zV:
        if (!$this->isMisMatchEmailOrPhone($errors, $bj)) {
            goto UV;
        }
        return $errors;
        UV:
        if ($this->isValidOTP($Fo, $bj, $errors)) {
            goto RA;
        }
        return $errors;
        RA:
        return $errors;
    }
    private function hasOTPBeenSent(&$errors, $bj)
    {
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto Sm;
        }
        $Kl = MoMessages::showMessage(BaseMessages::ENTER_VERIFY_CODE);
        if ($this->isPhoneVerificationEnabled()) {
            goto OO;
        }
        $errors["\x66\x69\x65\154\144" . $this->getFieldId("\x65\155\x61\x69\154\x5f\x73\150\157\x77", $bj)] = $Kl;
        goto a8;
        OO:
        $errors["\x66\x69\x65\154\x64" . $this->getFieldId("\160\x68\x6f\156\145\137\x73\150\157\x77", $bj)] = $Kl;
        a8:
        return false;
        Sm:
        return true;
    }
    private function isMisMatchEmailOrPhone(&$errors, $bj)
    {
        $uZ = $this->getFieldId($this->isPhoneVerificationEnabled() ? "\x70\x68\157\156\x65\137\x73\x68\x6f\167" : "\x65\155\x61\151\x6c\137\163\x68\x6f\167", $bj);
        $Rf = $_POST["\151\x74\145\x6d\137\155\145\x74\141"][$uZ];
        if ($this->checkPhoneOrEmailIntegrity($Rf)) {
            goto oh;
        }
        if ($this->isPhoneVerificationEnabled()) {
            goto uQ;
        }
        $errors["\146\x69\145\154\x64" . $this->getFieldId("\x65\155\141\x69\x6c\137\163\150\x6f\x77", $bj)] = MoMessages::showMessage(BaseMessages::EMAIL_MISMATCH);
        goto U0;
        uQ:
        $errors["\146\151\x65\154\144" . $this->getFieldId("\x70\x68\157\156\x65\x5f\163\x68\157\x77", $bj)] = MoMessages::showMessage(BaseMessages::PHONE_MISMATCH);
        U0:
        return true;
        oh:
        return false;
    }
    private function isValidOTP($Fo, $bj, &$errors)
    {
        $Cw = $this->getVerificationType();
        $this->validateChallenge($Cw, NULL, $Fo);
        if (SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, $Cw)) {
            goto w2;
        }
        $errors["\146\151\145\x6c\144" . $this->getFieldId("\x76\x65\x72\151\146\x79\x5f\163\150\157\x77", $bj)] = MoUtility::_get_invalid_otp_method();
        return false;
        goto Eq;
        w2:
        $this->unsetOTPSessionVariables();
        return true;
        Eq:
    }
    private function checkPhoneOrEmailIntegrity($Rf)
    {
        if ($this->isPhoneVerificationEnabled()) {
            goto Qr;
        }
        return SessionUtils::isEmailVerifiedMatch($this->_formSessionVar, $Rf);
        goto WF;
        Qr:
        return SessionUtils::isPhoneVerifiedMatch($this->_formSessionVar, $Rf);
        WF:
    }
    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);
    }
    function unsetOTPSessionVariables()
    {
        SessionUtils::unsetSession(array($this->_txSessionId, $this->_formSessionVar));
    }
    public function getPhoneNumberSelector($fv)
    {
        if (!($this->_isFormEnabled && $this->isPhoneVerificationEnabled())) {
            goto fL;
        }
        $fv = array_merge($fv, $this->_phoneFormId);
        fL:
        return $fv;
    }
    function isPhoneVerificationEnabled()
    {
        return $this->getVerificationType() === VerificationType::PHONE;
    }
    function handleFormOptions()
    {
        if (MoUtility::areFormOptionsBeingSaved($this->getFormOption())) {
            goto Jl;
        }
        return;
        Jl:
        $form = $this->parseFormDetails();
        $this->_isFormEnabled = $this->sanitizeFormPOST("\146\x72\x6d\137\x66\x6f\162\155\x5f\145\x6e\x61\x62\154\x65");
        $this->_otpType = $this->sanitizeFormPOST("\146\x72\155\137\x66\157\x72\155\x5f\x65\x6e\141\142\154\x65\x5f\x74\x79\160\145");
        $this->_formDetails = !empty($form) ? $form : '';
        $this->_buttonText = $this->sanitizeFormPOST("\x66\162\155\x5f\x62\165\x74\x74\x6f\x6e\x5f\164\x65\170\x74");
        if (!$this->basicValidationCheck(BaseMessages::FORMIDABLE_CHOOSE)) {
            goto oo;
        }
        update_mo_option("\x66\162\155\x5f\x62\x75\x74\164\x6f\156\x5f\164\x65\170\x74", $this->_buttonText);
        update_mo_option("\x66\162\x6d\137\146\157\x72\x6d\x5f\x65\156\x61\142\154\x65", $this->_isFormEnabled);
        update_mo_option("\x66\162\x6d\137\146\x6f\x72\x6d\137\x65\x6e\x61\x62\154\145\137\164\x79\x70\x65", $this->_otpType);
        update_mo_option("\146\162\x6d\x5f\146\157\x72\155\137\157\164\160\137\x65\156\141\142\154\145\144", maybe_serialize($this->_formDetails));
        oo:
    }
    function parseFormDetails()
    {
        $form = array();
        if (array_key_exists("\146\x72\155\137\x66\x6f\162\155", $_POST)) {
            goto a6;
        }
        return array();
        a6:
        foreach (array_filter($_POST["\x66\162\155\x5f\146\x6f\x72\155"]["\146\x6f\x72\x6d"]) as $yU => $Fo) {
            $form[$Fo] = array("\145\155\141\x69\154\153\x65\171" => "\x66\162\x6d\x5f\x66\x69\x65\154\x64\x5f" . $_POST["\146\x72\155\137\x66\x6f\162\155"]["\x65\155\x61\x69\x6c\x6b\x65\x79"][$yU] . "\137\143\157\x6e\x74\x61\151\x6e\145\x72", "\x70\150\x6f\x6e\145\x6b\145\171" => "\x66\x72\155\x5f\x66\151\x65\154\x64\137" . $_POST["\x66\162\x6d\x5f\146\x6f\162\x6d"]["\x70\x68\x6f\156\x65\153\145\x79"][$yU] . "\137\x63\x6f\x6e\164\x61\151\x6e\145\162", "\166\145\162\x69\146\x79\113\145\x79" => "\x66\x72\155\x5f\146\151\x65\154\x64\x5f" . $_POST["\x66\x72\155\137\x66\157\x72\155"]["\x76\145\162\151\146\171\x4b\145\x79"][$yU] . "\137\143\x6f\156\x74\x61\x69\x6e\x65\x72", "\160\x68\157\x6e\145\x5f\163\x68\157\167" => $_POST["\x66\x72\x6d\x5f\x66\157\162\x6d"]["\x70\x68\x6f\x6e\145\x6b\145\171"][$yU], "\145\155\x61\151\154\137\163\x68\157\167" => $_POST["\x66\x72\155\x5f\146\157\162\x6d"]["\x65\x6d\141\x69\x6c\153\x65\171"][$yU], "\166\145\x72\x69\x66\171\137\x73\150\157\167" => $_POST["\146\x72\x6d\137\x66\x6f\162\155"]["\x76\x65\x72\x69\x66\171\113\145\x79"][$yU]);
            VE:
        }
        SW:
        return $form;
    }
    function getFieldId($yU, $bj)
    {
        return $this->_formDetails[$bj->form_id][$yU];
    }
}
