<?php


namespace OTP\Handler\Forms;

use OTP\Helper\FormSessionVars;
use OTP\Helper\MoOTPDocs;
use OTP\Helper\MoPHPSessions;
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 MultiSiteFormRegistration extends FormHandler implements IFormHandler
{
    use Instance;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = FALSE;
        $this->_formSessionVar = FormSessionVars::MULTISITE;
        $this->_phoneFormId = "\x69\x6e\x70\165\x74\x5b\x6e\x61\155\145\75\155\x75\154\x74\x69\163\151\x74\x65\x5f\165\x73\145\x72\137\160\150\157\156\145\137\x6d\151\x6e\151\157\162\x61\x6e\x67\145\135";
        $this->_typePhoneTag = "\155\x6f\137\155\165\x6c\164\151\x73\x69\x74\145\x5f\143\157\x6e\x74\x61\143\164\x5f\x70\150\x6f\156\x65\137\x65\x6e\x61\x62\x6c\x65";
        $this->_typeEmailTag = "\155\x6f\x5f\x6d\x75\x6c\x74\x69\163\151\x74\145\137\143\x6f\x6e\x74\x61\x63\164\137\145\x6d\141\x69\154\137\x65\156\141\142\x6c\x65";
        $this->_formKey = "\127\120\137\123\x49\x47\116\x55\x50\x5f\x46\x4f\122\x4d";
        $this->_formName = mo_("\x57\157\x72\144\120\162\145\163\163\x20\x4d\x75\154\164\x69\x73\x69\164\x65\40\x53\x69\147\156\125\x70\x20\x46\157\x72\x6d");
        $this->_isFormEnabled = get_mo_option("\x6d\165\154\x74\151\163\x69\x74\x65\137\145\156\x61\x62\154\145");
        $this->_phoneKey = "\164\145\154\x65\x70\x68\157\156\145";
        $this->_formDocuments = MoOTPDocs::MULTISITE_REG_FORM;
        parent::__construct();
    }
    public function handleForm()
    {
        add_action("\167\x70\x5f\145\x6e\161\x75\145\165\x65\x5f\x73\143\162\x69\160\x74\163", array($this, "\x61\x64\x64\120\150\x6f\x6e\x65\106\151\145\x6c\x64\x53\143\162\x69\x70\164"));
        add_action("\x75\163\x65\162\x5f\x72\145\147\x69\163\x74\145\x72", array($this, "\x5f\163\x61\x76\145\x50\x68\x6f\x6e\145\x4e\165\155\x62\x65\162"), 10, 1);
        $this->_otpType = get_mo_option("\155\x75\x6c\164\151\163\151\x74\x65\137\x6f\x74\160\137\x74\x79\160\x65");
        if (array_key_exists("\157\x70\x74\151\x6f\156", $_POST)) {
            goto g2;
        }
        return;
        g2:
        switch (trim($_POST["\x6f\160\x74\x69\x6f\156"])) {
            case "\155\x75\x6c\x74\151\x73\x69\164\x65\137\162\x65\147\151\163\164\x65\162":
                $this->_sanitizeAndRouteData($_POST);
                goto HG;
            case "\155\151\x6e\x69\x6f\162\141\x6e\x67\145\x2d\166\141\x6c\x69\x64\141\x74\x65\x2d\157\x74\160\x2d\146\x6f\162\x6d":
                $this->_startValidation();
                goto HG;
        }
        aN:
        HG:
    }
    public function unsetOTPSessionVariables()
    {
        SessionUtils::unsetSession(array($this->_txSessionId, $this->_formSessionVar));
    }
    public function handle_post_verification($TG, $te, $j4, $xy, $sP, $yJ, $Cw)
    {
        SessionUtils::addStatus($this->_formSessionVar, self::VALIDATED, $Cw);
        $this->unsetOTPSessionVariables();
    }
    public function _savePhoneNumber($LG)
    {
        $YN = MoPHPSessions::getSessionVar("\x70\150\157\x6e\145\137\x6e\x75\x6d\142\x65\162\137\x6d\157");
        if (!$YN) {
            goto oR1;
        }
        update_user_meta($LG, $this->_phoneKey, $YN);
        oR1:
    }
    public function handle_failed_verification($te, $j4, $sP, $Cw)
    {
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto Mz;
        }
        return;
        Mz:
        $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 _sanitizeAndRouteData($MJ)
    {
        $EF = wpmu_validate_user_signup($_POST["\165\x73\145\162\137\156\141\x6d\145"], $_POST["\165\x73\x65\x72\137\145\155\141\x69\154"]);
        $errors = $EF["\145\x72\x72\157\x72\163"];
        if (!$errors->get_error_code()) {
            goto LY;
        }
        return false;
        LY:
        Moutility::initialize_transaction($this->_formSessionVar);
        if (strcasecmp($this->_otpType, $this->_typePhoneTag) == 0) {
            goto tk;
        }
        if (!(strcasecmp($this->_otpType, $this->_typeEmailTag) == 0)) {
            goto vT;
        }
        $this->_processEmail($MJ);
        vT:
        goto mj;
        tk:
        $this->_processPhone($MJ);
        mj:
        return false;
    }
    private function _startValidation()
    {
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto Rp;
        }
        return;
        Rp:
        $HK = $this->getVerificationType();
        if (!SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, $HK)) {
            goto Qn;
        }
        return;
        Qn:
        $this->validateChallenge($HK);
    }
    public function addPhoneFieldScript()
    {
        wp_enqueue_script("\155\165\x6c\x74\x69\x73\x69\164\x65\163\x63\x72\151\160\164", MOV_URL . "\151\x6e\143\154\x75\144\145\163\x2f\152\x73\57\x6d\x75\154\164\151\163\151\164\x65\x2e\x6d\x69\156\x2e\152\x73\x3f\x76\x65\162\163\x69\x6f\156\75" . MOV_VERSION, array("\x6a\x71\x75\x65\162\x79"));
    }
    private function _processPhone($MJ)
    {
        if (isset($MJ["\x6d\x75\154\164\151\x73\151\x74\x65\x5f\165\x73\x65\x72\137\160\150\157\156\145\x5f\155\151\x6e\x69\x6f\162\x61\x6e\x67\145"])) {
            goto Bp;
        }
        return;
        Bp:
        $this->sendChallenge('', '', null, trim($MJ["\155\x75\154\164\151\163\x69\164\145\137\165\x73\x65\162\137\160\150\x6f\x6e\145\x5f\x6d\151\156\151\157\162\x61\156\x67\145"]), VerificationType::PHONE);
    }
    private function _processEmail($MJ)
    {
        if (isset($MJ["\165\x73\145\x72\137\145\x6d\x61\x69\x6c"])) {
            goto WI;
        }
        return;
        WI:
        $this->sendChallenge('', $MJ["\165\163\x65\162\x5f\145\155\141\151\x6c"], null, null, VerificationType::EMAIL, '');
    }
    public function getPhoneNumberSelector($fv)
    {
        if (!self::isFormEnabled()) {
            goto J7;
        }
        array_push($fv, $this->_phoneFormId);
        J7:
        return $fv;
    }
    public function handleFormOptions()
    {
        if (MoUtility::areFormOptionsBeingSaved($this->getFormOption())) {
            goto QE;
        }
        return;
        QE:
        $this->_isFormEnabled = $this->sanitizeFormPOST("\x6d\x75\x6c\164\151\163\x69\164\x65\x5f\x65\156\x61\142\154\x65");
        $this->_otpType = $this->sanitizeFormPOST("\155\x75\154\164\151\163\151\x74\145\x5f\x63\x6f\x6e\x74\141\143\164\x5f\164\171\160\145");
        update_mo_option("\x6d\165\x6c\164\151\163\x69\164\x65\x5f\x65\x6e\141\x62\x6c\x65", $this->_isFormEnabled);
        update_mo_option("\x6d\x75\x6c\164\151\x73\151\164\145\x5f\x6f\164\x70\x5f\164\x79\x70\145", $this->_otpType);
    }
}
