<?php


namespace OTP\Handler\Forms;

use OTP\Helper\FormSessionVars;
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 NinjaForm extends FormHandler implements IFormHandler
{
    use Instance;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = FALSE;
        $this->_formSessionVar = FormSessionVars::NINJA_FORM;
        $this->_typePhoneTag = "\x6d\x6f\137\156\151\156\x6a\141\137\146\157\162\x6d\x5f\x70\x68\x6f\x6e\x65\137\x65\x6e\141\142\x6c\145";
        $this->_typeEmailTag = "\x6d\x6f\137\156\151\x6e\152\x61\137\146\157\x72\155\x5f\145\155\141\x69\154\x5f\145\x6e\x61\142\154\145";
        $this->_typeBothTag = "\x6d\x6f\137\x6e\x69\156\x6a\x61\137\x66\x6f\162\155\137\x62\x6f\x74\150\137\145\156\x61\142\x6c\x65";
        $this->_formKey = "\x4e\x49\116\x4a\101\137\106\117\x52\115";
        $this->_formName = mo_("\116\x69\156\152\141\x20\106\x6f\x72\x6d\163\40\x28\x20\x42\145\x6c\x6f\x77\x20\166\145\x72\163\x69\157\x6e\40\63\56\60\40\x29");
        $this->_isFormEnabled = get_mo_option("\156\151\156\152\x61\137\146\157\x72\x6d\137\145\x6e\x61\x62\154\145");
        $this->_formDocuments = MoOTPDocs::NINJA_FORMS_LINK;
        parent::__construct();
    }
    function handleForm()
    {
        $this->_otpType = get_mo_option("\156\x69\x6e\x6a\x61\x5f\x66\157\162\x6d\x5f\x65\156\x61\142\154\145\137\164\171\x70\145");
        $this->_formDetails = maybe_unserialize(get_mo_option("\156\151\156\x6a\141\137\146\157\x72\155\137\157\164\x70\x5f\145\x6e\x61\x62\154\x65\144"));
        if (!empty($this->_formDetails)) {
            goto uX;
        }
        return;
        uX:
        foreach ($this->_formDetails as $yU => $Fo) {
            array_push($this->_phoneFormId, "\151\x6e\x70\165\164\x5b\x6e\x61\x6d\145\75\156\x69\x6e\x6a\x61\x5f\x66\x6f\162\x6d\163\137\x66\151\x65\154\x64\137" . $Fo["\x70\x68\x6f\156\x65\x6b\x65\171"] . "\x5d");
            fH:
        }
        zF:
        if (!$this->checkIfOTPOptions()) {
            goto m0;
        }
        return;
        m0:
        if (!$this->checkIfNinjaFormSubmitted()) {
            goto QF;
        }
        $this->_handle_ninja_form_submit($_REQUEST);
        QF:
    }
    function checkIfOTPOptions()
    {
        return array_key_exists("\x6f\x70\x74\151\x6f\156", $_POST) && (strpos($_POST["\157\160\164\151\x6f\x6e"], "\166\x65\162\151\146\x69\143\x61\x74\x69\x6f\x6e\x5f\162\x65\x73\145\x6e\x64\137\x6f\164\160") || $_POST["\x6f\x70\x74\x69\157\x6e"] == "\x6d\x69\x6e\x69\157\x72\x61\x6e\147\145\x2d\x76\x61\154\151\x64\x61\164\x65\55\x6f\164\160\x2d\146\x6f\162\x6d" || $_POST["\x6f\160\164\151\157\x6e"] == "\155\151\x6e\151\157\162\141\156\147\x65\x2d\166\141\x6c\x69\x64\141\164\145\55\x6f\x74\160\x2d\143\x68\x6f\x69\143\145\55\x66\157\x72\155");
    }
    function checkIfNinjaFormSubmitted()
    {
        return array_key_exists("\x5f\156\x69\x6e\152\x61\137\x66\157\x72\x6d\163\x5f\144\151\163\x70\154\141\171\x5f\163\165\x62\x6d\x69\164", $_REQUEST) && array_key_exists("\137\146\x6f\162\155\137\151\x64", $_REQUEST);
    }
    function isPhoneVerificationEnabled()
    {
        $Cw = $this->getVerificationType();
        return $Cw === VerificationType::PHONE || $Cw === VerificationType::BOTH;
    }
    function isEmailVerificationEnabled()
    {
        $Cw = $this->getVerificationType();
        return $Cw === VerificationType::EMAIL || $Cw === VerificationType::BOTH;
    }
    function _handle_ninja_form_submit($O6)
    {
        if (array_key_exists($O6["\x5f\146\x6f\x72\x6d\137\151\x64"], $this->_formDetails)) {
            goto kM;
        }
        return;
        kM:
        $nt = $this->_formDetails[$O6["\x5f\x66\x6f\x72\155\137\151\x64"]];
        $R7 = $this->processEmail($nt, $O6);
        $s9 = $this->processPhone($nt, $O6);
        $this->miniorange_ninja_form_user($R7, null, $s9);
    }
    function processPhone($nt, $O6)
    {
        if (!$this->isPhoneVerificationEnabled()) {
            goto q6;
        }
        $bj = "\156\x69\x6e\x6a\x61\x5f\146\157\x72\155\x73\x5f\x66\151\145\x6c\144\x5f" . $nt["\160\150\157\156\145\x6b\145\x79"];
        return array_key_exists($bj, $O6) ? $O6[$bj] : NULL;
        q6:
        return null;
    }
    function processEmail($nt, $O6)
    {
        if (!$this->isEmailVerificationEnabled()) {
            goto Jk;
        }
        $bj = "\x6e\x69\156\x6a\141\x5f\146\157\162\x6d\x73\137\146\x69\x65\154\x64\x5f" . $nt["\145\x6d\x61\151\x6c\x6b\x65\x79"];
        return array_key_exists($bj, $O6) ? $O6[$bj] : NULL;
        Jk:
        return null;
    }
    function miniorange_ninja_form_user($j4, $F1, $sP)
    {
        MoUtility::initialize_transaction($this->_formSessionVar);
        $errors = new WP_Error();
        if (strcasecmp($this->_otpType, $this->_typePhoneTag) == 0) {
            goto mw;
        }
        if (strcasecmp($this->_otpType, $this->_typeBothTag) == 0) {
            goto Lz;
        }
        $this->sendChallenge($F1, $j4, $errors, $sP, VerificationType::EMAIL);
        goto MS;
        Lz:
        $this->sendChallenge($F1, $j4, $errors, $sP, VerificationType::BOTH);
        MS:
        goto P0;
        mw:
        $this->sendChallenge($F1, $j4, $errors, $sP, VerificationType::PHONE);
        P0:
    }
    function handle_failed_verification($te, $j4, $sP, $Cw)
    {
        $HK = $this->getVerificationType();
        $Qg = $HK === VerificationType::BOTH ? TRUE : FALSE;
        miniorange_site_otp_validation_form($te, $j4, $sP, MoUtility::_get_invalid_otp_method(), $HK, $Qg);
    }
    function handle_post_verification($TG, $te, $j4, $xy, $sP, $yJ, $Cw)
    {
        $this->unsetOTPSessionVariables();
    }
    public 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 handleFormOptions()
    {
        if (MoUtility::areFormOptionsBeingSaved($this->getFormOption())) {
            goto vb;
        }
        return;
        vb:
        if (!isset($_POST["\155\x6f\137\x63\165\163\x74\x6f\x6d\x65\x72\x5f\x76\141\154\x69\x64\x61\x74\x69\x6f\156\x5f\x6e\x6a\x61\137\145\x6e\x61\142\154\x65"])) {
            goto oO;
        }
        return;
        oO:
        $form = $this->parseFormDetails();
        $this->_isFormEnabled = $this->sanitizeFormPOST("\156\151\x6e\152\141\137\146\x6f\x72\155\137\145\156\141\142\154\x65");
        $this->_otpType = $this->sanitizeFormPOST("\x6e\151\156\x6a\x61\137\x66\157\162\x6d\137\x65\156\141\x62\x6c\x65\x5f\164\171\x70\145");
        $this->_formDetails = !empty($form) ? $form : '';
        update_mo_option("\156\151\156\152\x61\x5f\146\157\x72\155\137\145\x6e\141\142\154\145", $this->_isFormEnabled);
        update_mo_option("\x6e\x6a\141\137\145\156\x61\142\154\x65", 0);
        update_mo_option("\156\151\x6e\x6a\x61\x5f\x66\x6f\x72\x6d\x5f\145\156\x61\142\x6c\x65\x5f\x74\x79\160\x65", $this->_otpType);
        update_mo_option("\156\x69\x6e\x6a\141\x5f\146\x6f\x72\x6d\x5f\157\164\x70\137\x65\x6e\141\142\x6c\x65\x64", maybe_serialize($this->_formDetails));
    }
    function parseFormDetails()
    {
        $form = array();
        if (array_key_exists("\156\x69\156\152\x61\137\146\157\162\155", $_POST)) {
            goto Gd;
        }
        return array();
        Gd:
        foreach (array_filter($_POST["\x6e\151\156\152\x61\137\146\157\x72\155"]["\146\x6f\x72\155"]) as $yU => $Fo) {
            $form[$Fo] = array("\x65\x6d\x61\x69\154\153\145\x79" => $_POST["\x6e\x69\x6e\152\x61\137\146\x6f\x72\x6d"]["\145\x6d\141\151\x6c\x6b\x65\171"][$yU], "\x70\150\157\156\145\x6b\x65\171" => $_POST["\x6e\x69\156\152\141\x5f\146\x6f\162\x6d"]["\x70\150\157\x6e\145\x6b\x65\x79"][$yU]);
            E3:
        }
        gu:
        return $form;
    }
}
