<?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 VisualFormBuilder extends FormHandler implements IFormHandler
{
    use Instance;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = TRUE;
        $this->_formSessionVar = FormSessionVars::VISUAL_FORM;
        $this->_typePhoneTag = "\155\157\x5f\166\x69\163\x75\x61\154\137\x66\157\x72\155\137\160\150\x6f\156\x65\137\145\156\x61\x62\154\145";
        $this->_typeEmailTag = "\x6d\157\137\166\x69\x73\165\x61\x6c\137\x66\x6f\162\x6d\x5f\145\x6d\x61\151\154\x5f\145\156\x61\142\154\x65";
        $this->_typeBothTag = "\155\x6f\x5f\x76\151\x73\x75\x61\154\137\x66\157\x72\155\x5f\142\157\164\x68\x5f\x65\x6e\141\142\154\145";
        $this->_formKey = "\x56\111\x53\x55\101\x4c\137\106\117\x52\115";
        $this->_formName = mo_("\126\x69\x73\165\x61\x6c\40\x46\x6f\162\155\x20\x42\165\151\x6c\x64\x65\x72");
        $this->_phoneFormId = array();
        $this->_isFormEnabled = get_mo_option("\166\x69\163\165\x61\x6c\137\x66\157\x72\155\x5f\145\156\141\x62\x6c\x65");
        $this->_buttonText = get_mo_option("\x76\x69\163\x75\141\x6c\x5f\x66\157\x72\x6d\x5f\142\x75\164\x74\157\156\137\x74\x65\170\x74");
        $this->_buttonText = !MoUtility::isBlank($this->_buttonText) ? $this->_buttonText : mo_("\103\154\151\143\x6b\40\110\x65\x72\145\x20\164\x6f\x20\x73\145\156\x64\x20\x4f\124\x50");
        $this->_generateOTPAction = "\x6d\x69\x6e\x69\x6f\162\x61\x6e\x67\145\55\x76\x66\x2d\x73\x65\156\144\55\157\164\160";
        $this->_validateOTPAction = "\x6d\x69\156\151\x6f\162\141\156\x67\x65\x2d\x76\x66\x2d\166\145\162\x69\146\171\55\143\x6f\x64\x65";
        $this->_formDocuments = MoOTPDocs::VISUAL_FORM_LINK;
        parent::__construct();
    }
    function handleForm()
    {
        $this->_otpType = get_mo_option("\x76\x69\x73\165\x61\154\x5f\146\157\x72\x6d\137\x65\x6e\x61\x62\x6c\x65\x5f\164\171\x70\x65");
        $this->_formDetails = maybe_unserialize(get_mo_option("\x76\x69\163\x75\141\x6c\x5f\146\157\x72\155\x5f\x6f\164\x70\x5f\145\x6e\x61\x62\154\145\144"));
        if (!(empty($this->_formDetails) || !$this->_isFormEnabled)) {
            goto jf;
        }
        return;
        jf:
        foreach ($this->_formDetails as $yU => $Fo) {
            array_push($this->_phoneFormId, "\43" . $Fo["\160\150\157\x6e\x65\x6b\x65\171"]);
            Ow:
        }
        pR:
        add_action("\167\x70\137\145\x6e\161\165\145\165\145\x5f\163\143\x72\x69\x70\x74\x73", array($this, "\x6d\157\137\145\156\161\165\x65\165\x65\x5f\x76\x66"));
        add_action("\167\x70\x5f\x61\x6a\x61\170\x5f{$this->_generateOTPAction}", array($this, "\137\x73\x65\x6e\x64\x5f\x6f\164\160\x5f\166\x66\x5f\141\152\x61\170"));
        add_action("\x77\x70\x5f\x61\x6a\141\x78\x5f\156\x6f\x70\x72\x69\x76\x5f{$this->_generateOTPAction}", array($this, "\137\163\145\156\x64\137\157\164\160\137\166\x66\x5f\141\x6a\x61\170"));
        add_action("\x77\160\137\141\x6a\x61\x78\137{$this->_validateOTPAction}", array($this, "\160\x72\157\143\145\x73\x73\x46\x6f\x72\155\101\156\144\x56\x61\154\x69\144\141\164\x65\117\x54\x50"));
        add_action("\167\160\x5f\141\x6a\x61\x78\137\156\157\160\162\151\166\x5f{$this->_validateOTPAction}", array($this, "\160\x72\x6f\x63\145\163\x73\x46\x6f\162\x6d\101\x6e\x64\126\141\154\x69\144\x61\164\x65\x4f\x54\120"));
    }
    function mo_enqueue_vf()
    {
        wp_register_script("\166\146\163\x63\x72\x69\160\164", MOV_URL . "\151\x6e\x63\x6c\x75\x64\145\x73\x2f\x6a\163\57\x76\x66\x73\143\x72\x69\160\x74\56\x6d\151\156\56\152\163", array("\152\161\x75\x65\162\x79"));
        wp_localize_script("\x76\146\163\143\x72\151\160\x74", "\155\157\166\146\166\141\162", array("\163\x69\x74\145\x55\x52\x4c" => wp_ajax_url(), "\x6f\164\x70\x54\171\160\145" => strcasecmp($this->_otpType, $this->_typePhoneTag), "\x66\x6f\162\155\104\x65\x74\x61\151\154\x73" => $this->_formDetails, "\142\165\164\x74\157\156\x74\x65\x78\x74" => $this->_buttonText, "\151\155\x67\x55\x52\114" => MOV_LOADER_URL, "\146\151\145\x6c\144\x54\x65\x78\164" => mo_("\x45\156\164\145\162\x20\x4f\124\120\x20\x68\x65\162\x65"), "\x67\156\157\x6e\x63\x65" => wp_create_nonce($this->_nonce), "\156\157\156\143\x65\113\x65\171" => wp_create_nonce($this->_nonceKey), "\166\x6e\157\x6e\143\145" => wp_create_nonce($this->_nonce), "\147\141\x63\164\151\157\x6e" => $this->_generateOTPAction, "\x76\141\143\164\x69\157\x6e" => $this->_validateOTPAction));
        wp_enqueue_script("\x76\x66\163\143\162\151\160\164");
    }
    function _send_otp_vf_ajax()
    {
        $this->validateAjaxRequest();
        if ($this->_otpType == $this->_typePhoneTag) {
            goto Vn;
        }
        $this->_send_vf_otp_to_email($_POST);
        goto T9;
        Vn:
        $this->_send_vf_otp_to_phone($_POST);
        T9:
    }
    function _send_vf_otp_to_phone($BT)
    {
        if (!MoUtility::sanitizeCheck("\x75\x73\x65\x72\x5f\160\x68\x6f\156\145", $BT)) {
            goto L7;
        }
        $this->startOTPVerification(trim($BT["\x75\163\145\162\137\x70\150\157\156\x65"]), NULL, trim($BT["\x75\x73\x65\x72\137\160\x68\x6f\156\x65"]), VerificationType::PHONE);
        goto VU;
        L7:
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_PHONE), MoConstants::ERROR_JSON_TYPE));
        VU:
    }
    function _send_vf_otp_to_email($BT)
    {
        if (!MoUtility::sanitizeCheck("\165\x73\x65\162\137\145\x6d\141\x69\154", $BT)) {
            goto Nj;
        }
        $this->startOTPVerification($BT["\x75\x73\145\162\x5f\145\155\x61\x69\x6c"], $BT["\165\x73\x65\x72\x5f\x65\155\141\x69\154"], NULL, VerificationType::EMAIL);
        goto Cn;
        Nj:
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_EMAIL), MoConstants::ERROR_JSON_TYPE));
        Cn:
    }
    private function startOTPVerification($hU, $Hu, $YN, $Cw)
    {
        MoUtility::initialize_transaction($this->_formSessionVar);
        if ($Cw === VerificationType::PHONE) {
            goto BL;
        }
        SessionUtils::addEmailVerified($this->_formSessionVar, $hU);
        goto oV;
        BL:
        SessionUtils::addPhoneVerified($this->_formSessionVar, $hU);
        oV:
        $this->sendChallenge('', $Hu, NULL, $YN, $Cw);
    }
    function processFormAndValidateOTP()
    {
        $this->validateAjaxRequest();
        $this->checkIfVerificationNotStarted();
        $this->checkIntegrityAndValidateOTP($_POST);
    }
    function checkIfVerificationNotStarted()
    {
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto fS;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_VERIFY_CODE), MoConstants::ERROR_JSON_TYPE));
        fS:
    }
    private function checkIntegrityAndValidateOTP($post)
    {
        $this->checkIntegrity($post);
        $this->validateChallenge($this->getVerificationType(), NULL, $post["\157\164\x70\137\164\157\x6b\x65\156"]);
    }
    private function checkIntegrity($post)
    {
        if ($this->isPhoneVerificationEnabled()) {
            goto Gk;
        }
        if (SessionUtils::isEmailVerifiedMatch($this->_formSessionVar, $post["\163\x75\x62\x5f\x66\151\145\x6c\x64"])) {
            goto Gh;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::EMAIL_MISMATCH), MoConstants::ERROR_JSON_TYPE));
        Gh:
        goto mP;
        Gk:
        if (SessionUtils::isPhoneVerifiedMatch($this->_formSessionVar, $post["\163\165\x62\137\x66\x69\x65\154\x64"])) {
            goto rp;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::PHONE_MISMATCH), MoConstants::ERROR_JSON_TYPE));
        rp:
        mP:
    }
    function handle_failed_verification($te, $j4, $sP, $Cw)
    {
        wp_send_json(MoUtility::createJson(MoUtility::_get_invalid_otp_method(), MoConstants::ERROR_JSON_TYPE));
    }
    function handle_post_verification($TG, $te, $j4, $xy, $sP, $yJ, $Cw)
    {
        $this->unsetOTPSessionVariables();
        wp_send_json(MoUtility::createJson(MoConstants::SUCCESS, MoConstants::SUCCESS_JSON_TYPE));
    }
    function unsetOTPSessionVariables()
    {
        SessionUtils::unsetSession(array($this->_txSessionId, $this->_formSessionVar));
    }
    public function getPhoneNumberSelector($fv)
    {
        if (!($this->_isFormEnabled && $this->isPhoneVerificationEnabled())) {
            goto z0;
        }
        $fv = array_merge($fv, $this->_phoneFormId);
        z0:
        return $fv;
    }
    function isPhoneVerificationEnabled()
    {
        $HK = $this->getVerificationType();
        return $HK == VerificationType::PHONE || $HK === VerificationType::BOTH;
    }
    function handleFormOptions()
    {
        if (MoUtility::areFormOptionsBeingSaved($this->getFormOption())) {
            goto WB;
        }
        return;
        WB:
        $form = $this->parseFormDetails();
        $this->_isFormEnabled = $this->sanitizeFormPOST("\x76\x69\x73\x75\x61\154\137\146\157\x72\155\x5f\x65\156\141\x62\154\145");
        $this->_otpType = $this->sanitizeFormPOST("\x76\151\163\165\x61\x6c\x5f\x66\157\162\155\137\x65\156\141\142\154\x65\137\x74\x79\160\x65");
        $this->_formDetails = !empty($form) ? $form : '';
        $this->_buttonText = $this->sanitizeFormPOST("\166\151\x73\x75\141\154\x5f\x66\157\x72\x6d\137\x62\165\x74\x74\157\x6e\137\164\x65\x78\x74");
        if (!$this->basicValidationCheck(BaseMessages::VISUAL_FORM_CHOOSE)) {
            goto Hl;
        }
        update_mo_option("\166\151\x73\165\x61\154\137\146\x6f\x72\155\137\x62\165\x74\x74\x6f\x6e\137\x74\145\170\x74", $this->_buttonText);
        update_mo_option("\x76\x69\163\x75\x61\x6c\x5f\x66\157\162\155\x5f\x65\156\x61\142\x6c\145", $this->_isFormEnabled);
        update_mo_option("\x76\151\x73\x75\141\154\137\x66\157\162\155\x5f\x65\x6e\141\x62\x6c\145\137\164\x79\x70\x65", $this->_otpType);
        update_mo_option("\166\x69\163\165\141\154\137\146\157\x72\155\137\157\x74\x70\x5f\x65\156\x61\142\x6c\145\x64", maybe_serialize($this->_formDetails));
        Hl:
    }
    function parseFormDetails()
    {
        $form = array();
        if (array_key_exists("\x76\x69\163\x75\x61\x6c\137\x66\157\162\155", $_POST)) {
            goto t1;
        }
        return array();
        t1:
        foreach (array_filter($_POST["\x76\151\x73\165\x61\x6c\x5f\x66\x6f\x72\155"]["\x66\x6f\x72\155"]) as $yU => $Fo) {
            $form[$Fo] = array("\x65\155\x61\x69\154\x6b\x65\x79" => $this->getFieldID($_POST["\166\x69\x73\x75\x61\154\x5f\146\157\x72\155"]["\x65\x6d\141\x69\x6c\153\145\171"][$yU], $Fo), "\160\150\x6f\x6e\145\153\x65\x79" => $this->getFieldID($_POST["\x76\151\x73\x75\141\154\137\146\x6f\162\x6d"]["\x70\150\157\x6e\x65\153\x65\171"][$yU], $Fo), "\x70\150\x6f\156\145\x5f\x73\x68\x6f\x77" => $_POST["\166\151\x73\165\141\154\x5f\x66\157\x72\155"]["\x70\150\157\156\145\x6b\x65\171"][$yU], "\145\155\141\x69\x6c\137\163\150\157\x77" => $_POST["\x76\151\x73\165\x61\154\x5f\146\x6f\x72\155"]["\x65\x6d\x61\x69\154\x6b\x65\x79"][$yU]);
            PK:
        }
        BP:
        return $form;
    }
    private function getFieldID($yU, $Cm)
    {
        global $wpdb;
        $nj = "\x53\105\114\105\103\124\x20\52\40\106\122\117\x4d\x20" . VFB_WP_FIELDS_TABLE_NAME . "\40\x77\x68\x65\162\x65\40\146\x69\145\154\x64\137\156\141\155\x65\40\75\x27" . $yU . "\47\x61\x6e\144\x20\146\x6f\162\155\x5f\151\x64\x20\75\40\47" . $Cm . "\x27";
        $EF = $wpdb->get_row($nj);
        return !MoUtility::isBlank($EF) ? "\166\146\142\x2d" . $EF->field_id : '';
    }
}
