<?php


namespace OTP\Handler\Forms;

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\Objects\BaseMessages;
use OTP\Traits\Instance;
use ReflectionException;
use WP_Error;
class YourOwnForm extends FormHandler implements IFormHandler
{
    use Instance;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = TRUE;
        $this->_formKey = "\131\x4f\x55\122\x5f\117\x57\116\137\106\x4f\122\115";
        $this->_formName = mo_("\x3c\163\x70\x61\x6e\x20\163\x74\171\154\145\x3d\47\143\157\x6c\157\x72\x3a\x67\162\145\x65\156\x27\x20\76\74\x62\x3e\103\x61\156\47\164\x20\x46\151\156\144\x20\171\x6f\165\162\40\106\x6f\162\x6d\77\x20\x54\162\x79\40\155\145\x21\74\x2f\142\x3e\x3c\57\x73\160\141\156\76");
        $this->_formSessionVar = FormSessionVars::CUSTOMFORM;
        $this->_formDetails = maybe_unserialize(get_mo_option("\143\165\163\x74\x6f\x6d\137\146\x6f\162\155\x5f\x6f\164\160\137\x65\156\141\x62\154\145\144"));
        $this->_typePhoneTag = "\155\157\x5f\143\x75\x73\164\157\155\106\157\x72\x6d\x5f\160\150\157\156\145\x5f\x65\x6e\x61\x62\x6c\145";
        $this->_typeEmailTag = "\155\157\137\x63\165\x73\164\x6f\x6d\106\157\x72\155\x5f\x65\155\x61\x69\154\x5f\x65\156\x61\142\x6c\145";
        $this->_isFormEnabled = get_mo_option("\x63\165\163\164\157\155\x5f\x66\157\162\x6d\x5f\x63\x6f\156\x74\x61\143\164\137\145\x6e\141\x62\x6c\x65");
        $this->_generateOTPAction = "\x6d\151\x6e\151\157\x72\x61\156\147\x65\55\x63\165\163\164\157\x6d\106\x6f\162\155\x2d\x73\x65\x6e\144\55\x6f\164\160";
        $this->_validateOTPAction = "\x6d\151\x6e\x69\x6f\162\141\156\147\145\x2d\x63\165\163\x74\x6f\155\x46\x6f\162\155\55\x76\x65\162\151\146\171\x2d\x63\x6f\x64\x65";
        $this->_checkValidatedOnSubmit = "\155\151\156\151\x6f\162\141\x6e\x67\x65\55\x63\165\163\164\x6f\155\x46\x6f\x72\155\x2d\166\x65\x72\x69\x66\171\55\163\x75\x62\x6d\151\x74";
        $this->_otpType = get_mo_option("\143\165\x73\x74\x6f\155\137\146\157\x72\155\x5f\x65\x6e\x61\x62\x6c\145\x5f\x74\171\x70\145");
        $this->_buttonText = get_mo_option("\143\x75\x73\x74\157\155\137\x66\157\x72\x6d\x5f\142\x75\x74\x74\157\x6e\137\x74\145\x78\x74");
        $this->_buttonText = !MoUtility::isBlank($this->_buttonText) ? $this->_buttonText : mo_("\103\x6c\x69\143\153\40\110\145\x72\145\x20\x74\157\40\163\145\156\x64\40\x4f\x54\120");
        $this->validated = FALSE;
        parent::__construct();
        $this->handleForm();
    }
    function handleForm()
    {
        MoPHPSessions::checkSession();
        if ($this->_isFormEnabled) {
            goto EC;
        }
        return;
        EC:
        $this->_formFieldId = $this->getFieldKeyDetails();
        $this->_formSubmitId = $this->getSubmitKeyDetails();
        add_action("\x77\x70\137\145\x6e\x71\165\145\165\145\137\163\x63\162\x69\160\x74\163", array($this, "\x6d\157\x5f\145\x6e\x71\165\145\x75\x65\x5f\146\x6f\x72\x6d\x5f\163\143\162\151\x70\164"));
        add_action("\154\x6f\x67\x69\156\137\145\156\161\165\x65\165\x65\137\163\x63\x72\151\x70\x74\163", array($this, "\x6d\157\x5f\x65\x6e\x71\x75\145\x75\x65\x5f\x66\157\x72\x6d\137\163\143\162\x69\x70\164"));
        add_action("\167\160\x5f\x61\152\x61\170\137{$this->_generateOTPAction}", array($this, "\x5f\163\145\156\x64\137\157\164\160"));
        add_action("\x77\160\x5f\141\x6a\x61\170\x5f\156\157\160\x72\x69\166\137{$this->_generateOTPAction}", array($this, "\x5f\163\145\x6e\x64\x5f\x6f\x74\160"));
        add_action("\x77\160\x5f\x61\x6a\x61\170\x5f{$this->_validateOTPAction}", array($this, "\160\162\157\143\x65\x73\x73\x46\x6f\162\x6d\101\x6e\144\x56\141\x6c\151\x64\141\164\145\117\124\x50"));
        add_action("\167\x70\137\141\x6a\141\x78\137\156\x6f\160\162\x69\166\137{$this->_validateOTPAction}", array($this, "\x70\x72\157\x63\x65\163\x73\x46\x6f\x72\x6d\101\156\x64\x56\x61\154\x69\144\141\x74\x65\x4f\x54\120"));
        add_action("\x77\x70\x5f\x61\x6a\141\x78\137{$this->_checkValidatedOnSubmit}", array($this, "\137\x63\x68\x65\143\153\x56\x61\154\x69\x64\141\164\x65\144\117\156\123\165\x62\x6d\151\x74"));
        add_action("\x77\x70\137\x61\x6a\141\170\x5f\156\157\160\162\151\x76\x5f{$this->_checkValidatedOnSubmit}", array($this, "\137\x63\x68\145\x63\x6b\x56\x61\x6c\x69\144\x61\164\145\144\x4f\156\x53\x75\142\155\151\x74"));
        if (!SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, $this->getVerificationType())) {
            goto eE;
        }
        $this->validated = TRUE;
        $this->unsetOTPSessionVariables();
        return;
        eE:
    }
    function mo_enqueue_form_script()
    {
        wp_register_script($this->_formSessionVar, MOV_URL . "\x69\156\x63\x6c\x75\x64\145\x73\57\152\163\57" . $this->_formSessionVar . "\56\152\163", array("\152\161\165\x65\x72\x79"));
        wp_localize_script($this->_formSessionVar, $this->_formSessionVar, array("\163\x69\164\145\x55\122\x4c" => wp_ajax_url(), "\157\164\x70\x54\171\160\x65" => $this->getVerificationType(), "\x66\157\162\155\x44\x65\164\x61\151\154\163" => $this->_formDetails, "\x62\x75\164\x74\x6f\156\x74\x65\170\164" => $this->_buttonText, "\151\x6d\147\125\122\114" => MOV_LOADER_URL, "\146\x69\145\154\144\x54\x65\170\164" => mo_("\105\156\x74\x65\x72\40\x4f\x54\x50\x20\150\145\x72\x65"), "\147\x6e\x6f\156\x63\x65" => wp_create_nonce($this->_nonce), "\156\157\156\143\x65\113\145\171" => wp_create_nonce($this->_nonceKey), "\166\x6e\157\x6e\x63\x65" => wp_create_nonce($this->_nonce), "\147\141\x63\x74\151\x6f\x6e" => $this->_generateOTPAction, "\x76\x61\143\x74\x69\157\156" => $this->_validateOTPAction, "\163\141\143\164\151\157\x6e" => $this->_checkValidatedOnSubmit, "\x66\151\x65\154\x64\x53\x65\154\x65\143\x74\157\162" => $this->_formFieldId, "\163\x75\142\x6d\x69\x74\x53\x65\154\145\143\x74\x6f\162" => $this->_formSubmitId));
        wp_enqueue_script($this->_formSessionVar);
        wp_enqueue_style("\x6d\157\137\x66\x6f\162\155\163\137\x63\x73\163", MOV_FORM_CSS);
    }
    function _send_otp()
    {
        MoPHPSessions::checkSession();
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto ls;
        }
        MoUtility::initialize_transaction($this->_formSessionVar);
        ls:
        if (!(MoUtility::sanitizeCheck("\x6f\x74\x70\124\x79\160\145", $_POST) === VerificationType::PHONE)) {
            goto CK;
        }
        $this->_processPhoneAndSendOTP($_POST);
        CK:
        if (!(MoUtility::sanitizeCheck("\157\164\160\x54\x79\160\x65", $_POST) === VerificationType::EMAIL)) {
            goto Aj;
        }
        $this->_processEmailAndSendOTP($_POST);
        Aj:
    }
    public function _checkValidatedOnSubmit()
    {
        if (!$this->validated) {
            goto S5;
        }
        if (!$this->validated) {
            goto ZE;
        }
        wp_send_json(MoUtility::createJson(self::VALIDATED, MoConstants::SUCCESS_JSON_TYPE));
        ZE:
        goto ih;
        S5:
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::PLEASE_VALIDATE), MoConstants::ERROR_JSON_TYPE));
        ih:
    }
    private function _processEmailAndSendOTP($BT)
    {
        MoPHPSessions::checkSession();
        if (!MoUtility::sanitizeCheck("\x75\x73\145\x72\x5f\x65\x6d\141\x69\154", $BT)) {
            goto Sq;
        }
        SessionUtils::addEmailVerified($this->_formSessionVar, $BT["\165\163\145\x72\x5f\x65\x6d\x61\x69\154"]);
        $this->sendChallenge('', $BT["\x75\163\145\x72\137\x65\155\x61\151\154"], NULL, NULL, VerificationType::EMAIL);
        goto db;
        Sq:
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_EMAIL), MoConstants::ERROR_JSON_TYPE));
        db:
    }
    private function _processPhoneAndSendOTP($BT)
    {
        if (!MoUtility::sanitizeCheck("\x75\163\x65\162\x5f\160\150\x6f\156\x65", $BT)) {
            goto l6;
        }
        SessionUtils::addPhoneVerified($this->_formSessionVar, $BT["\165\x73\x65\x72\137\160\150\157\156\x65"]);
        $this->sendChallenge('', NULL, NULL, $BT["\165\163\x65\x72\x5f\x70\150\157\x6e\145"], VerificationType::PHONE);
        goto fA;
        l6:
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_PHONE), MoConstants::ERROR_JSON_TYPE));
        fA:
    }
    function processFormAndValidateOTP()
    {
        MoPHPSessions::checkSession();
        $this->checkIfOTPSent();
        $this->checkIntegrityAndValidateOTP($_POST);
    }
    function checkIfOTPSent()
    {
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto Wv;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_VERIFY_CODE), MoConstants::ERROR_JSON_TYPE));
        Wv:
    }
    private function checkIntegrityAndValidateOTP($BT)
    {
        MoPHPSessions::checkSession();
        $this->checkIntegrity($BT);
        $this->validateChallenge($BT["\157\x74\x70\124\x79\x70\x65"], NULL, $BT["\157\x74\160\137\164\157\x6b\x65\x6e"]);
        if (SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, $BT["\x6f\164\160\124\171\160\145"])) {
            goto Lh;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::INVALID_OTP), MoConstants::ERROR_JSON_TYPE));
        goto Uu;
        Lh:
        if (!($BT["\157\x74\160\x54\171\160\x65"] === VerificationType::PHONE)) {
            goto Vq;
        }
        SessionUtils::addPhoneSubmitted($this->_formSessionVar, $BT["\x75\163\x65\162\x5f\x70\x68\157\x6e\145"]);
        Vq:
        if (!($BT["\x6f\x74\x70\124\x79\x70\x65"] === VerificationType::EMAIL)) {
            goto wX;
        }
        SessionUtils::addEmailSubmitted($this->_formSessionVar, $BT["\165\x73\x65\162\x5f\x65\x6d\141\151\154"]);
        wX:
        wp_send_json(MoUtility::createJson(MoConstants::SUCCESS_JSON_TYPE, MoConstants::SUCCESS_JSON_TYPE));
        Uu:
    }
    private function checkIntegrity($BT)
    {
        if (!($BT["\x6f\164\160\124\171\160\145"] === VerificationType::PHONE)) {
            goto Kv;
        }
        if (SessionUtils::isPhoneVerifiedMatch($this->_formSessionVar, $BT["\x75\163\145\x72\137\160\150\157\x6e\145"])) {
            goto KT;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::PHONE_MISMATCH), MoConstants::ERROR_JSON_TYPE));
        KT:
        Kv:
        if (!($BT["\157\x74\x70\124\x79\x70\x65"] === VerificationType::EMAIL)) {
            goto j9;
        }
        if (SessionUtils::isEmailVerifiedMatch($this->_formSessionVar, $BT["\165\163\145\x72\137\x65\x6d\141\x69\154"])) {
            goto ZR;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::EMAIL_MISMATCH), MoConstants::ERROR_JSON_TYPE));
        ZR:
        j9:
    }
    function handle_failed_verification($te, $j4, $sP, $Cw)
    {
        MoPHPSessions::checkSession();
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto uY;
        }
        return;
        uY:
        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 SJ;
        }
        return;
        SJ:
        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 Vm;
        }
        array_push($fv, $this->_formFieldId);
        Vm:
        return $fv;
    }
    function isPhoneEnabled()
    {
        return $this->getVerificationType() == VerificationType::PHONE ? TRUE : FALSE;
    }
    private function parseFormDetails()
    {
        $form = array();
        if (array_key_exists("\x63\165\x73\x74\157\x6d\137\x66\x6f\x72\x6d", $_POST)) {
            goto mH;
        }
        return array();
        mH:
        $Cw = $_POST["\x6d\x6f\137\143\165\163\164\x6f\155\x65\162\x5f\166\x61\154\151\x64\x61\164\151\x6f\x6e\137\143\x75\x73\164\157\x6d\x5f\x66\x6f\x72\x6d\x5f\145\x6e\141\142\x6c\145\x5f\164\171\160\145"] == $this->_typePhoneTag ? "\160\x68\x6f\156\145" : "\145\155\x61\151\154";
        foreach (array_filter($_POST["\x63\x75\x73\x74\157\x6d\x5f\146\157\x72\155"]["\146\x6f\162\x6d"]) as $yU => $Fo) {
            $form[$Fo] = array("\163\165\142\x6d\x69\164\x5f\151\144" => $_POST["\x63\165\x73\164\157\x6d\x5f\x66\157\162\x6d"][$Cw]["\x73\x75\x62\155\151\164\x5f\x69\144"], "\146\151\145\x6c\144\137\x69\x64" => $_POST["\x63\x75\163\164\157\x6d\x5f\x66\x6f\162\x6d"][$Cw]["\146\x69\x65\x6c\144\x5f\x69\x64"]);
            Br:
        }
        jI:
        return $form;
    }
    function handleFormOptions()
    {
        $form = $this->parseFormDetails();
        if (MoUtility::areFormOptionsBeingSaved($this->getFormOption())) {
            goto zw;
        }
        return;
        zw:
        $this->_formDetails = !empty($form) ? $form : '';
        $this->_isFormEnabled = $this->sanitizeFormPOST("\143\165\x73\x74\x6f\155\x5f\146\157\162\155\137\x63\x6f\156\164\141\x63\x74\137\145\156\141\142\154\x65");
        $this->_otpType = $this->sanitizeFormPOST("\143\165\x73\x74\x6f\x6d\x5f\146\157\162\155\137\145\156\141\x62\154\x65\137\x74\x79\x70\145");
        $this->_buttonText = $this->sanitizeFormPOST("\143\165\x73\164\x6f\x6d\137\x66\x6f\162\155\x5f\x62\165\x74\164\x6f\156\x5f\164\145\170\x74");
        if (!$this->basicValidationCheck(BaseMessages::CUSTOM_CHOOSE)) {
            goto i0;
        }
        update_mo_option("\x63\x75\163\164\x6f\x6d\137\146\157\x72\155\137\x6f\164\160\x5f\x65\156\141\x62\x6c\x65\144", maybe_serialize($this->_formDetails));
        update_mo_option("\x63\165\x73\164\x6f\155\137\x66\x6f\x72\x6d\x5f\x63\x6f\x6e\164\x61\x63\164\137\145\156\x61\x62\x6c\145", $this->_isFormEnabled);
        update_mo_option("\143\165\163\164\157\155\137\x66\157\x72\x6d\137\145\x6e\x61\x62\x6c\x65\x5f\x74\171\x70\145", $this->_otpType);
        update_mo_option("\143\165\163\x74\x6f\x6d\137\146\x6f\x72\x6d\137\x62\x75\x74\164\x6f\156\137\x74\x65\x78\x74", $this->_buttonText);
        i0:
    }
    function getSubmitKeyDetails()
    {
        if (!empty($this->_formDetails)) {
            goto Jv;
        }
        return;
        Jv:
        return stripcslashes($this->_formDetails[1]["\163\165\142\155\151\x74\137\151\x64"]);
    }
    function getFieldKeyDetails()
    {
        if (!empty($this->_formDetails)) {
            goto ci;
        }
        return;
        ci:
        return stripcslashes($this->_formDetails[1]["\146\x69\145\x6c\144\x5f\151\x64"]);
    }
}
