<?php


namespace OTP\Handler\Forms;

use mysql_xdevapi\Session;
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\VerificationLogic;
use OTP\Objects\VerificationType;
use OTP\Traits\Instance;
use ReflectionException;
use stdClass;
class SimplrRegistrationForm extends FormHandler implements IFormHandler
{
    use Instance;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = FALSE;
        $this->_formSessionVar = FormSessionVars::SIMPLR_REG;
        $this->_typePhoneTag = "\x6d\x6f\x5f\x70\x68\157\x6e\x65\137\x65\x6e\141\142\154\145";
        $this->_typeEmailTag = "\155\x6f\x5f\145\x6d\x61\151\154\x5f\x65\x6e\141\142\154\x65";
        $this->_typeBothTag = "\x6d\157\137\142\157\x74\150\137\x65\156\x61\x62\154\x65";
        $this->_formKey = "\x53\111\115\120\x4c\x52\137\x46\x4f\122\x4d";
        $this->_formName = mo_("\123\x69\x6d\x70\154\162\40\125\163\x65\x72\40\122\x65\x67\x69\x73\x74\162\141\164\151\157\x6e\x20\106\157\x72\155\40\x50\x6c\x75\163");
        $this->_isFormEnabled = get_mo_option("\x73\x69\155\x70\x6c\162\x5f\144\x65\146\141\x75\x6c\164\137\145\156\x61\x62\154\x65");
        $this->_formDocuments = MoOTPDocs::SIMPLR_FORM_LINK;
        parent::__construct();
    }
    function handleForm()
    {
        $this->_formKey = get_mo_option("\x73\151\155\x70\x6c\x72\x5f\146\x69\x65\154\x64\137\x6b\145\x79");
        $this->_otpType = get_mo_option("\163\151\155\x70\x6c\162\x5f\x65\156\x61\142\154\x65\x5f\164\x79\x70\x65");
        $this->_phoneFormId = "\x69\156\x70\165\164\133\156\141\155\145\75" . $this->_formKey . "\x5d";
        add_filter("\163\x69\x6d\x70\154\x72\137\166\x61\x6c\151\144\x61\164\145\137\146\x6f\162\155", array($this, "\163\151\x6d\160\154\162\x5f\163\x69\x74\145\137\x72\145\147\151\x73\164\162\141\164\151\157\156\137\x65\x72\162\x6f\162\163"), 10, 1);
    }
    function isPhoneVerificationEnabled()
    {
        $HK = $this->getVerificationType();
        return $HK === VerificationType::PHONE || $HK === VerificationType::BOTH;
    }
    function simplr_site_registration_errors($errors)
    {
        $xy = $sP = '';
        if (!(!empty($errors) || isset($_POST["\146\142\165\163\145\x72\x5f\x69\x64"]))) {
            goto WZ;
        }
        return $errors;
        WZ:
        foreach ($_POST as $yU => $Fo) {
            if ($yU == "\165\163\145\162\156\x61\x6d\x65") {
                goto wZ;
            }
            if ($yU == "\145\155\141\x69\x6c") {
                goto Ul;
            }
            if ($yU == "\x70\141\163\163\167\x6f\x72\x64") {
                goto Xj;
            }
            if ($yU == $this->_formKey) {
                goto V3;
            }
            $yJ[$yU] = $Fo;
            goto NU;
            wZ:
            $lQ = $Fo;
            goto NU;
            Ul:
            $R7 = $Fo;
            goto NU;
            Xj:
            $xy = $Fo;
            goto NU;
            V3:
            $sP = $Fo;
            NU:
            KZ:
        }
        RU:
        if (!(strcasecmp($this->_otpType, $this->_typePhoneTag) == 0 && !$this->processPhone($sP, $errors))) {
            goto HR;
        }
        return $errors;
        HR:
        $this->processAndStartOTPVerificationProcess($lQ, $R7, $errors, $sP, $xy, $yJ);
        return $errors;
    }
    function processPhone($sP, &$errors)
    {
        if (MoUtility::validatePhoneNumber($sP)) {
            goto Le;
        }
        global $phoneLogic;
        $errors[] .= str_replace("\x23\x23\160\x68\x6f\156\x65\x23\x23", $sP, $phoneLogic->_get_otp_invalid_format_message());
        add_filter($this->_formKey . "\x5f\x65\x72\162\157\x72\x5f\143\x6c\141\x73\x73", "\137\163\x72\145\147\137\162\x65\164\165\x72\x6e\137\145\x72\x72\x6f\162");
        return FALSE;
        Le:
        return TRUE;
    }
    function processAndStartOTPVerificationProcess($lQ, $R7, $errors, $sP, $xy, $yJ)
    {
        MoUtility::initialize_transaction($this->_formSessionVar);
        if (strcasecmp($this->_otpType, $this->_typePhoneTag) == 0) {
            goto ag;
        }
        if (strcasecmp($this->_otpType, $this->_typeBothTag) == 0) {
            goto c4;
        }
        $this->sendChallenge($lQ, $R7, $errors, $sP, VerificationType::EMAIL, $xy, $yJ);
        goto tA;
        c4:
        $this->sendChallenge($lQ, $R7, $errors, $sP, VerificationType::BOTH, $xy, $yJ);
        tA:
        goto jG;
        ag:
        $this->sendChallenge($lQ, $R7, $errors, $sP, VerificationType::PHONE, $xy, $yJ);
        jG:
    }
    function register_simplr_user($te, $j4, $xy, $sP, $yJ)
    {
        $BT = array();
        global $sreg;
        if ($sreg) {
            goto Xk;
        }
        $sreg = new stdClass();
        Xk:
        $BT["\165\163\x65\x72\156\141\155\x65"] = $te;
        $BT["\145\x6d\x61\151\154"] = $j4;
        $BT["\x70\x61\x73\163\167\x6f\x72\x64"] = $xy;
        if (!$this->_formKey) {
            goto Qc;
        }
        $BT[$this->_formKey] = $sP;
        Qc:
        $BT = array_merge($BT, $yJ);
        $xO = $yJ["\x61\x74\164\163"];
        $sreg->output = simplr_setup_user($xO, $BT);
        if (!MoUtility::isBlank($sreg->errors)) {
            goto Ge;
        }
        $this->checkMessageAndRedirect($xO);
        Ge:
    }
    function checkMessageAndRedirect($xO)
    {
        global $sreg, $simplr_options;
        $Hi = isset($xO["\x74\x68\x61\156\153\163"]) ? get_permalink($xO["\164\x68\141\x6e\153\x73"]) : (!MoUtility::isBlank($simplr_options->thank_you) ? get_permalink($simplr_options->thank_you) : '');
        if (MoUtility::isBlank($Hi)) {
            goto Jd;
        }
        wp_redirect($Hi);
        die;
        goto Mk;
        Jd:
        $sreg->success = $sreg->output;
        Mk:
    }
    function handle_failed_verification($te, $j4, $sP, $Cw)
    {
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto GX;
        }
        return;
        GX:
        $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();
        $this->register_simplr_user($te, $j4, $xy, $sP, $yJ);
    }
    public function unsetOTPSessionVariables()
    {
        SessionUtils::unsetSession(array($this->_txSessionId, $this->_formSessionVar));
    }
    public function getPhoneNumberSelector($fv)
    {
        if (!($this->isFormEnabled() && $this->isPhoneVerificationEnabled())) {
            goto hP;
        }
        array_push($fv, $this->_phoneFormId);
        hP:
        return $fv;
    }
    function handleFormOptions()
    {
        if (MoUtility::areFormOptionsBeingSaved($this->getFormOption())) {
            goto wD;
        }
        return;
        wD:
        $this->_isFormEnabled = $this->sanitizeFormPOST("\x73\x69\x6d\160\154\162\137\144\145\146\141\165\154\x74\137\x65\156\141\x62\x6c\145");
        $this->_otpType = $this->sanitizeFormPOST("\163\x69\155\160\154\x72\x5f\145\156\x61\142\x6c\x65\x5f\164\x79\160\x65");
        $this->_phoneKey = $this->sanitizeFormPOST("\x73\151\155\x70\154\162\137\160\x68\x6f\156\145\x5f\146\151\145\x6c\144\137\153\145\171");
        update_mo_option("\163\x69\x6d\160\154\x72\137\144\145\x66\x61\x75\x6c\164\137\145\156\141\x62\x6c\x65", $this->_isFormEnabled);
        update_mo_option("\163\151\x6d\x70\154\x72\x5f\145\x6e\x61\142\x6c\145\x5f\x74\x79\160\145", $this->_otpType);
        update_mo_option("\163\x69\155\x70\154\x72\137\x66\x69\x65\x6c\144\x5f\x6b\145\171", $this->_phoneKey);
    }
}
