<?php


namespace OTP\Handler;

use OTP\Helper\FormSessionVars;
use OTP\Helper\MoConstants;
use OTP\Helper\MoMessages;
use OTP\Helper\MoPHPSessions;
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 CustomForm extends FormHandler implements IFormHandler
{
    use Instance;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = TRUE;
        $this->_isAddOnForm = TRUE;
        $this->isEnabled = get_mo_option("\143\x66\x5f\x73\x75\x62\155\x69\x74\137\x69\144", "\155\157\137\157\x74\x70\x5f") ? true : false;
        $this->_formSessionVar = FormSessionVars::CUSTOMFORM;
        $this->_typePhoneTag = "\x6d\157\x5f\143\x75\163\x74\x6f\x6d\x46\x6f\162\x6d\x5f\160\x68\157\x6e\x65\137\x65\x6e\x61\x62\x6c\145";
        $this->_typeEmailTag = "\x6d\x6f\137\143\165\163\x74\157\155\106\157\162\155\137\145\155\141\x69\x6c\137\145\156\x61\142\x6c\145";
        $this->_isFormEnabled = $this->isEnabled;
        $this->_phoneFormId = stripslashes(get_mo_option("\143\146\137\146\x69\x65\154\144\x5f\x69\x64", "\x6d\x6f\137\157\164\160\137"));
        $this->_generateOTPAction = "\155\x69\156\151\157\x72\141\x6e\147\145\55\143\165\x73\x74\157\x6d\x46\x6f\x72\x6d\x2d\163\145\x6e\144\55\157\x74\x70";
        $this->_validateOTPAction = "\x6d\x69\x6e\x69\x6f\x72\x61\x6e\x67\145\x2d\x63\165\x73\x74\157\155\106\x6f\162\155\55\x76\x65\162\151\146\x79\55\143\x6f\144\145";
        $this->_checkValidatedOnSubmit = "\x6d\151\156\x69\x6f\x72\141\x6e\x67\145\x2d\x63\x75\163\x74\157\x6d\106\x6f\x72\x6d\x2d\166\145\162\x69\x66\171\55\x73\165\x62\155\x69\164";
        $this->_otpType = get_mo_option("\x63\146\x5f\145\x6e\x61\x62\x6c\145\x5f\164\171\160\145", "\x6d\x6f\137\157\x74\160\137");
        $this->_buttonText = get_mo_option("\143\146\137\x62\165\x74\x74\157\x6e\137\x74\145\x78\164", "\x6d\157\137\x6f\164\160\137");
        $this->_buttonText = !MoUtility::isBlank($this->_buttonText) ? $this->_buttonText : mo_("\103\154\151\143\x6b\40\110\145\x72\145\x20\164\x6f\x20\163\x65\156\x64\40\x4f\x54\x50");
        $this->validated = FALSE;
        parent::__construct();
        $this->handleForm();
    }
    function handleForm()
    {
        MoPHPSessions::checkSession();
        if ($this->isEnabled) {
            goto rt9;
        }
        return;
        rt9:
        add_action("\x77\160\137\x65\156\x71\165\145\x75\145\137\163\x63\x72\151\160\x74\163", array($this, "\x6d\x6f\x5f\145\x6e\161\x75\145\165\x65\137\x66\157\162\155\137\x73\143\162\x69\x70\164"));
        add_action("\x6c\x6f\x67\x69\156\137\145\x6e\161\165\145\x75\145\x5f\x73\x63\x72\x69\160\x74\x73", array($this, "\x6d\x6f\137\x65\156\x71\165\x65\x75\x65\137\146\x6f\162\155\x5f\163\143\x72\151\x70\164"));
        add_action("\167\x70\x5f\x61\152\x61\170\x5f{$this->_generateOTPAction}", array($this, "\137\x73\x65\156\144\x5f\x6f\164\x70"));
        add_action("\x77\160\x5f\x61\152\x61\x78\137\x6e\x6f\x70\x72\151\x76\137{$this->_generateOTPAction}", array($this, "\137\163\145\x6e\x64\x5f\x6f\164\160"));
        add_action("\x77\160\137\x61\x6a\x61\170\x5f{$this->_validateOTPAction}", array($this, "\x70\x72\157\143\145\163\163\x46\x6f\162\x6d\x41\x6e\x64\126\141\154\x69\144\141\164\145\x4f\124\x50"));
        add_action("\167\160\x5f\141\152\x61\x78\x5f\x6e\157\x70\x72\151\x76\x5f{$this->_validateOTPAction}", array($this, "\160\x72\x6f\x63\x65\163\x73\106\157\162\155\x41\156\144\126\x61\154\x69\144\141\164\145\x4f\x54\120"));
        add_action("\x77\x70\137\141\152\x61\x78\x5f{$this->_checkValidatedOnSubmit}", array($this, "\137\143\x68\x65\143\153\126\x61\x6c\x69\x64\x61\x74\145\144\x4f\x6e\123\x75\x62\x6d\151\x74"));
        add_action("\x77\160\x5f\141\x6a\141\170\137\156\x6f\160\162\151\166\137{$this->_checkValidatedOnSubmit}", array($this, "\137\143\150\145\x63\x6b\126\x61\154\151\x64\x61\164\145\144\117\156\x53\x75\142\155\x69\x74"));
        if (!SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, $this->getVerificationType())) {
            goto K1q;
        }
        $this->validated = TRUE;
        $this->unsetOTPSessionVariables();
        return;
        K1q:
    }
    function mo_enqueue_form_script()
    {
        wp_register_script($this->_formSessionVar, MOV_URL . "\151\156\143\154\165\144\x65\x73\x2f\152\163\x2f" . $this->_formSessionVar . "\56\x6a\163", array("\152\x71\x75\x65\x72\x79"));
        wp_localize_script($this->_formSessionVar, $this->_formSessionVar, array("\x73\151\x74\x65\125\122\x4c" => wp_ajax_url(), "\x6f\x74\x70\124\x79\160\145" => $this->getVerificationType(), "\x66\x6f\x72\155\x44\145\x74\141\151\154\x73" => $this->_formDetails, "\142\165\x74\164\157\x6e\x74\x65\x78\164" => $this->_buttonText, "\151\155\x67\x55\122\114" => MOV_LOADER_URL, "\x66\x69\145\x6c\144\124\x65\x78\164" => mo_("\x45\x6e\164\x65\162\x20\117\124\120\x20\150\x65\x72\x65"), "\147\156\x6f\x6e\143\145" => wp_create_nonce($this->_nonce), "\x6e\x6f\156\x63\145\x4b\x65\x79" => wp_create_nonce($this->_nonceKey), "\x76\x6e\x6f\x6e\143\145" => wp_create_nonce($this->_nonce), "\x67\141\x63\164\x69\157\x6e" => $this->_generateOTPAction, "\x76\141\x63\x74\151\x6f\156" => $this->_validateOTPAction, "\146\x69\145\154\x64\123\x65\x6c\145\143\164\x6f\162" => stripcslashes(get_mo_option("\143\x66\137\146\151\145\154\x64\137\x69\x64", "\x6d\157\137\x6f\x74\160\137")), "\163\x75\x62\x6d\x69\x74\x53\145\x6c\145\x63\164\157\162" => stripcslashes(get_mo_option("\143\x66\x5f\x73\165\142\155\151\x74\137\151\144", "\155\157\137\157\x74\x70\x5f")), "\163\x69\164\x65\125\122\x4c" => wp_ajax_url(), "\x73\x61\143\x74\x69\x6f\x6e" => $this->_checkValidatedOnSubmit));
        wp_enqueue_script($this->_formSessionVar);
        wp_enqueue_style("\155\x6f\x5f\x66\157\162\x6d\163\x5f\x63\163\163", MOV_FORM_CSS);
    }
    function _send_otp()
    {
        MoPHPSessions::checkSession();
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto i3p;
        }
        MoUtility::initialize_transaction($this->_formSessionVar);
        i3p:
        if (!(MoUtility::sanitizeCheck("\x6f\x74\160\124\x79\160\x65", $_POST) === VerificationType::PHONE)) {
            goto QLk;
        }
        $this->_processPhoneAndSendOTP($_POST);
        QLk:
        if (!(MoUtility::sanitizeCheck("\157\164\160\x54\x79\160\145", $_POST) === VerificationType::EMAIL)) {
            goto t6J;
        }
        $this->_processEmailAndSendOTP($_POST);
        t6J:
    }
    public function _checkValidatedOnSubmit()
    {
        if (!$this->validated) {
            goto F6P;
        }
        if (!$this->validated) {
            goto cwS;
        }
        wp_send_json(MoUtility::createJson(self::VALIDATED, MoConstants::SUCCESS_JSON_TYPE));
        cwS:
        goto D1E;
        F6P:
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::PLEASE_VALIDATE), MoConstants::ERROR_JSON_TYPE));
        D1E:
    }
    private function _processEmailAndSendOTP($BT)
    {
        MoPHPSessions::checkSession();
        if (!MoUtility::sanitizeCheck("\165\163\x65\162\x5f\x65\x6d\x61\151\154", $BT)) {
            goto MmL;
        }
        SessionUtils::addEmailVerified($this->_formSessionVar, $BT["\x75\163\145\x72\137\x65\x6d\x61\x69\154"]);
        $this->sendChallenge('', $BT["\165\x73\145\x72\x5f\x65\x6d\x61\151\154"], NULL, NULL, VerificationType::EMAIL);
        goto LNl;
        MmL:
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_EMAIL), MoConstants::ERROR_JSON_TYPE));
        LNl:
    }
    private function _processPhoneAndSendOTP($BT)
    {
        if (!MoUtility::sanitizeCheck("\x75\x73\145\162\x5f\160\x68\157\x6e\x65", $BT)) {
            goto axx;
        }
        SessionUtils::addPhoneVerified($this->_formSessionVar, $BT["\165\x73\145\x72\x5f\160\x68\x6f\x6e\x65"]);
        $this->sendChallenge('', NULL, NULL, $BT["\x75\163\x65\x72\137\160\x68\x6f\156\x65"], VerificationType::PHONE);
        goto vpD;
        axx:
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_PHONE), MoConstants::ERROR_JSON_TYPE));
        vpD:
    }
    function processFormAndValidateOTP()
    {
        MoPHPSessions::checkSession();
        $this->checkIfOTPSent();
        $this->checkIntegrityAndValidateOTP($_POST);
    }
    function checkIfOTPSent()
    {
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto xkL;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_VERIFY_CODE), MoConstants::ERROR_JSON_TYPE));
        xkL:
    }
    private function checkIntegrityAndValidateOTP($BT)
    {
        MoPHPSessions::checkSession();
        $this->checkIntegrity($BT);
        $this->validateChallenge($BT["\157\x74\x70\x54\x79\160\145"], NULL, $BT["\x6f\164\x70\x5f\x74\157\x6b\x65\x6e"]);
        if (SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, $BT["\x6f\x74\x70\124\171\x70\145"])) {
            goto rhb;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::INVALID_OTP), MoConstants::ERROR_JSON_TYPE));
        goto uxb;
        rhb:
        if (!($BT["\x6f\164\160\124\x79\x70\x65"] === VerificationType::PHONE)) {
            goto LtW;
        }
        SessionUtils::addPhoneSubmitted($this->_formSessionVar, $BT["\x75\x73\145\162\x5f\160\x68\x6f\156\145"]);
        LtW:
        if (!($BT["\157\x74\x70\x54\171\x70\145"] === VerificationType::EMAIL)) {
            goto xlL;
        }
        SessionUtils::addEmailSubmitted($this->_formSessionVar, $BT["\165\163\x65\162\x5f\x65\155\141\x69\154"]);
        xlL:
        wp_send_json(MoUtility::createJson(MoConstants::SUCCESS_JSON_TYPE, MoConstants::SUCCESS_JSON_TYPE));
        uxb:
    }
    private function checkIntegrity($BT)
    {
        if (!($BT["\x6f\164\160\124\171\x70\145"] === VerificationType::PHONE)) {
            goto abq;
        }
        if (SessionUtils::isPhoneVerifiedMatch($this->_formSessionVar, $BT["\165\x73\x65\162\137\x70\x68\x6f\156\x65"])) {
            goto B57;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::PHONE_MISMATCH), MoConstants::ERROR_JSON_TYPE));
        B57:
        abq:
        if (!($BT["\x6f\164\x70\124\171\160\145"] === VerificationType::EMAIL)) {
            goto EoC;
        }
        if (SessionUtils::isEmailVerifiedMatch($this->_formSessionVar, $BT["\165\x73\145\162\x5f\x65\155\141\x69\x6c"])) {
            goto ra4;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::EMAIL_MISMATCH), MoConstants::ERROR_JSON_TYPE));
        ra4:
        EoC:
    }
    function handle_failed_verification($te, $j4, $sP, $Cw)
    {
        MoPHPSessions::checkSession();
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto WWd;
        }
        return;
        WWd:
        SessionUtils::addStatus($this->_formSessionVar, self::VERIFICATION_FAILED, $Cw);
    }
    function handle_post_verification($TG, $te, $j4, $xy, $sP, $yJ, $Cw)
    {
        MoPHPSessions::checkSession();
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto j6Z;
        }
        return;
        j6Z:
        SessionUtils::addStatus($this->_formSessionVar, self::VALIDATED, $Cw);
    }
    public function unsetOTPSessionVariables()
    {
        MoPHPSessions::checkSession();
        SessionUtils::unsetSession(array($this->_formSessionVar, $this->_txSessionId));
    }
    public function getPhoneNumberSelector($fv)
    {
        if (!($this->isFormEnabled() && $this->isPhoneEnabled())) {
            goto vbb;
        }
        array_push($fv, $this->_phoneFormId);
        vbb:
        return $fv;
    }
    function isPhoneEnabled()
    {
        return $this->getVerificationType() == VerificationType::PHONE ? TRUE : FALSE;
    }
    function handleFormOptions()
    {
    }
    function getSubmitKeyDetails()
    {
        return stripcslashes(get_mo_option("\x63\x66\x5f\x73\165\142\x6d\151\164\137\x69\x64", "\x6d\x6f\x5f\157\x74\160\137"));
    }
    function getFieldKeyDetails()
    {
        return stripcslashes(get_mo_option("\x63\x66\137\x66\151\x65\154\x64\x5f\x69\144", "\x6d\x6f\137\x6f\164\x70\137"));
    }
}
