<?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 WPClientRegistration extends FormHandler implements IFormHandler
{
    use Instance;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = FALSE;
        $this->_formSessionVar = FormSessionVars::WP_CLIENT_REG;
        $this->_phoneKey = "\x77\x70\x5f\x63\157\156\x74\x61\x63\x74\137\x70\x68\157\156\145";
        $this->_phoneFormId = "\43\167\160\x63\137\x63\157\x6e\x74\141\143\164\137\160\x68\x6f\156\x65";
        $this->_formKey = "\x57\120\x5f\103\x4c\111\x45\x4e\x54\137\122\x45\x47";
        $this->_typePhoneTag = "\155\x6f\x5f\x77\160\137\x63\x6c\151\145\x6e\164\x5f\160\x68\157\156\145\137\x65\x6e\x61\142\x6c\x65";
        $this->_typeEmailTag = "\155\157\x5f\x77\x70\137\143\154\x69\145\156\164\137\x65\155\x61\x69\154\137\x65\156\x61\142\x6c\x65";
        $this->_typeBothTag = "\155\x6f\137\167\160\137\143\x6c\x69\x65\156\164\x5f\142\157\x74\150\137\145\156\x61\142\154\x65";
        $this->_formName = mo_("\x57\x50\x20\103\154\x69\145\x6e\164\40\122\145\147\x69\163\164\162\141\x74\151\x6f\x6e\x20\x46\x6f\x72\x6d");
        $this->_isFormEnabled = get_mo_option("\x77\x70\x5f\x63\154\x69\145\x6e\x74\137\145\156\141\x62\x6c\145");
        $this->_formDocuments = MoOTPDocs::WP_CLIENT_FORM;
        parent::__construct();
    }
    function handleForm()
    {
        $this->_otpType = get_mo_option("\x77\x70\137\143\154\151\145\x6e\x74\137\x65\x6e\x61\142\x6c\145\137\164\x79\x70\145");
        $this->_restrictDuplicates = get_mo_option("\x77\160\x5f\x63\x6c\x69\x65\x6e\x74\137\x72\145\163\164\162\x69\x63\x74\137\x64\165\x70\154\151\143\141\164\x65\163");
        add_filter("\x77\x70\143\x5f\x63\x6c\x69\145\x6e\x74\137\x72\145\x67\151\x73\x74\162\x61\x74\151\157\x6e\x5f\x66\x6f\x72\155\137\166\141\154\151\144\141\x74\151\x6f\x6e", array($this, "\x6d\x69\x6e\x69\157\162\x61\x6e\x67\x65\137\143\154\151\x65\x6e\164\137\162\145\147\151\x73\x74\162\x61\x74\x69\157\156\x5f\x76\x65\x72\151\x66\171"), 99, 1);
    }
    function isPhoneVerificationEnabled()
    {
        $Cw = $this->getVerificationType();
        return $Cw === VerificationType::PHONE || $Cw === VerificationType::BOTH;
    }
    function miniorange_client_registration_verify($errors)
    {
        $Cw = $this->getVerificationType();
        $sP = MoUtility::sanitizeCheck("\x63\157\x6e\x74\x61\143\164\137\x70\150\157\156\x65", $_POST);
        $j4 = MoUtility::sanitizeCheck("\x63\157\x6e\164\141\143\x74\x5f\145\155\141\x69\x6c", $_POST);
        $JM = MoUtility::sanitizeCheck("\x63\157\156\164\141\143\x74\x5f\165\163\x65\x72\156\x61\155\145", $_POST);
        if (!($this->_restrictDuplicates && $this->isPhoneNumberAlreadyInUse($sP, $this->_phoneKey))) {
            goto mX;
        }
        $errors .= mo_("\x50\150\x6f\x6e\x65\40\x6e\165\155\142\x65\x72\x20\x61\x6c\x72\x65\141\144\x79\x20\151\x6e\x20\165\x73\x65\x2e\x20\x50\x6c\x65\x61\x73\x65\x20\105\x6e\x74\145\x72\40\141\40\x64\151\x66\x66\x65\162\145\156\164\40\120\x68\157\156\x65\40\156\165\155\142\x65\x72\x2e");
        mX:
        if (MoUtility::isBlank($errors)) {
            goto Cc;
        }
        return $errors;
        Cc:
        if (!SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto qc;
        }
        if (SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, $Cw)) {
            goto Ml;
        }
        goto DG;
        qc:
        MoUtility::initialize_transaction($this->_formSessionVar);
        goto DG;
        Ml:
        $this->unsetOTPSessionVariables();
        return $errors;
        DG:
        return $this->startOTPTransaction($JM, $j4, $errors, $sP);
    }
    function startOTPTransaction($JM, $j4, $errors, $sP)
    {
        if (strcasecmp($this->_otpType, $this->_typePhoneTag) === 0) {
            goto x1;
        }
        if (strcasecmp($this->_otpType, $this->_typeBothTag) === 0) {
            goto A1;
        }
        $this->sendChallenge($JM, $j4, $errors, $sP, VerificationType::EMAIL);
        goto Mt;
        A1:
        $this->sendChallenge($JM, $j4, $errors, $sP, VerificationType::BOTH);
        Mt:
        goto Vc;
        x1:
        $this->sendChallenge($JM, $j4, $errors, $sP, VerificationType::PHONE);
        Vc:
        return $errors;
    }
    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)
    {
        SessionUtils::addStatus($this->_formSessionVar, self::VALIDATED, $Cw);
    }
    function isPhoneNumberAlreadyInUse($s9, $yU)
    {
        global $wpdb;
        $s9 = MoUtility::processPhoneNumber($s9);
        $rF = $wpdb->get_row("\123\105\x4c\105\x43\124\40\x60\x75\163\145\162\x5f\x69\144\140\40\106\x52\117\x4d\x20\140{$wpdb->prefix}\165\163\x65\162\x6d\145\164\x61\140\40\x57\x48\x45\x52\x45\40\x60\155\145\164\141\x5f\x6b\145\171\x60\x20\75\40\47{$yU}\47\40\x41\116\104\x20\140\x6d\x65\164\141\137\166\x61\154\165\145\x60\x20\75\40\x20\x27{$s9}\47");
        return !MoUtility::isBlank($rF);
    }
    public function unsetOTPSessionVariables()
    {
        SessionUtils::unsetSession(array($this->_txSessionId, $this->_formSessionVar));
    }
    public function getPhoneNumberSelector($fv)
    {
        if (!($this->isFormEnabled() && $this->isPhoneVerificationEnabled())) {
            goto Bn;
        }
        array_push($fv, $this->_phoneFormId);
        Bn:
        return $fv;
    }
    function handleFormOptions()
    {
        if (MoUtility::areFormOptionsBeingSaved($this->getFormOption())) {
            goto aK;
        }
        return;
        aK:
        $this->_isFormEnabled = $this->sanitizeFormPOST("\167\x70\137\x63\x6c\x69\x65\x6e\x74\137\x65\x6e\x61\x62\154\x65");
        $this->_otpType = $this->sanitizeFormPOST("\167\160\137\x63\154\151\x65\156\x74\137\x65\x6e\141\142\x6c\145\137\164\171\160\x65");
        $this->_restrictDuplicates = $this->getVerificationType() === VerificationType::PHONE ? $this->sanitizeFormPOST("\x77\x70\x5f\143\154\151\145\156\164\137\162\x65\163\x74\x72\x69\143\x74\x5f\144\x75\x70\x6c\151\x63\x61\164\x65\x73") : false;
        update_mo_option("\167\x70\137\143\154\x69\145\156\x74\x5f\x65\156\141\x62\x6c\x65", $this->_isFormEnabled);
        update_mo_option("\167\x70\137\x63\154\151\x65\x6e\x74\137\x65\156\x61\142\154\145\137\164\x79\160\145", $this->_otpType);
        update_mo_option("\x77\160\137\x63\x6c\151\x65\x6e\164\137\x72\145\163\x74\x72\x69\143\x74\x5f\x64\165\160\154\151\143\x61\x74\x65\163", $this->_restrictDuplicates);
    }
}
