<?php


namespace OTP\Handler\Forms;

use OTP\Helper\FormSessionVars;
use OTP\Helper\MoMessages;
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;
class PieRegistrationForm extends FormHandler implements IFormHandler
{
    use Instance;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = FALSE;
        $this->_formSessionVar = FormSessionVars::PIE_REG;
        $this->_typePhoneTag = "\x6d\x6f\137\x70\151\x65\x5f\x70\150\157\156\145\x5f\x65\156\x61\x62\x6c\145";
        $this->_typeEmailTag = "\x6d\157\x5f\160\151\x65\x5f\x65\155\141\x69\x6c\x5f\x65\156\x61\142\x6c\x65";
        $this->_typeBothTag = "\155\157\x5f\160\x69\145\137\142\x6f\164\150\x5f\145\156\x61\142\x6c\x65";
        $this->_formKey = "\x50\x49\x45\137\x46\x4f\x52\x4d";
        $this->_formName = mo_("\120\x49\105\40\122\x65\147\x69\163\164\162\141\x74\151\x6f\156\x20\x46\157\x72\x6d");
        $this->_isFormEnabled = get_mo_option("\160\151\x65\137\x64\145\146\141\165\x6c\x74\x5f\x65\156\141\142\154\145");
        $this->_formDocuments = MoOTPDocs::PIE_FORM_LINK;
        parent::__construct();
    }
    function handleForm()
    {
        $this->_otpType = get_mo_option("\x70\x69\x65\137\145\156\x61\x62\x6c\x65\137\164\x79\x70\145");
        $this->_phoneKey = get_mo_option("\160\151\x65\137\x70\150\x6f\156\145\x5f\153\145\171");
        $this->_phoneFormId = $this->getPhoneFieldKey();
        add_action("\x70\151\145\137\162\145\x67\151\x73\164\x65\x72\x5f\142\145\146\x6f\162\x65\x5f\162\145\147\x69\163\x74\145\162\137\166\x61\x6c\x69\144\141\x74\x65", array($this, "\x6d\151\156\x69\x6f\x72\x61\156\x67\x65\137\160\151\x65\137\165\163\x65\x72\137\x72\145\x67\151\x73\x74\x72\141\x74\151\x6f\156"), 99, 1);
    }
    function isPhoneVerificationEnabled()
    {
        $HK = $this->getVerificationType();
        return $HK === VerificationType::PHONE || $HK === VerificationType::BOTH;
    }
    function miniorange_pie_user_registration()
    {
        global $errors;
        if (empty($errors->errors)) {
            goto pG;
        }
        return;
        pG:
        if (!$this->checkIfVerificationIsComplete()) {
            goto g3;
        }
        return;
        g3:
        if (!(empty($_POST[$this->_phoneFormId]) && $this->isPhoneVerificationEnabled())) {
            goto z1;
        }
        $errors->add("\x6d\157\x5f\x6f\164\160\x5f\x76\145\x72\x69\146\171", MoMessages::showMessage(MoMessages::ENTER_PHONE_DEFAULT));
        return;
        z1:
        $this->startTheOTPVerificationProcess($_POST["\x65\x5f\x6d\x61\x69\x6c"], "\53" . $_POST[$this->_phoneFormId]);
        if ($this->checkIfVerificationIsComplete()) {
            goto rl;
        }
        $errors->add("\x6d\x6f\137\x6f\x74\x70\137\166\145\x72\151\146\171", MoMessages::showMessage(MoMessages::ENTER_VERIFY_CODE));
        rl:
    }
    function checkIfVerificationIsComplete()
    {
        if (!SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, $this->getVerificationType())) {
            goto TP;
        }
        $this->unsetOTPSessionVariables();
        return TRUE;
        TP:
        return FALSE;
    }
    function startTheOTPVerificationProcess($Hu, $s9)
    {
        MoUtility::initialize_transaction($this->_formSessionVar);
        if (strcasecmp($this->_otpType, $this->_typePhoneTag) == 0) {
            goto mm;
        }
        if (strcasecmp($this->_otpType, $this->_typeBothTag) == 0) {
            goto Rx;
        }
        $this->sendChallenge('', $Hu, null, $s9, VerificationType::EMAIL);
        goto Ci;
        Rx:
        $this->sendChallenge('', $Hu, null, $s9, VerificationType::BOTH);
        Ci:
        goto LF;
        mm:
        $this->sendChallenge('', $Hu, null, $s9, VerificationType::PHONE);
        LF:
    }
    function getPhoneFieldKey()
    {
        $Qj = get_option("\x70\x69\145\x5f\x66\x69\x65\154\144\x73");
        if (!empty($Qj)) {
            goto gD;
        }
        return '';
        gD:
        $bq = maybe_unserialize($Qj);
        foreach ($bq as $yU) {
            if (!(strcasecmp(trim($yU["\x6c\x61\142\145\x6c"]), $this->_phoneKey) == 0)) {
                goto US;
            }
            return str_replace("\x2d", "\x5f", sanitize_title($yU["\164\171\x70\145"] . "\137" . (isset($yU["\x69\144"]) ? $yU["\x69\144"] : '')));
            US:
            hL:
        }
        WS:
        return '';
    }
    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);
    }
    public function unsetOTPSessionVariables()
    {
        SessionUtils::unsetSession(array($this->_txSessionId, $this->_formSessionVar));
    }
    public function getPhoneNumberSelector($fv)
    {
        if (!($this->isFormEnabled() && $this->isPhoneVerificationEnabled())) {
            goto Fy;
        }
        array_push($fv, "\151\156\x70\x75\x74\x23" . $this->_phoneFormId);
        Fy:
        return $fv;
    }
    function handleFormOptions()
    {
        if (MoUtility::areFormOptionsBeingSaved($this->getFormOption())) {
            goto LT;
        }
        return;
        LT:
        $this->_isFormEnabled = $this->sanitizeFormPOST("\x70\x69\145\x5f\x64\145\146\141\x75\x6c\164\x5f\145\156\x61\142\154\145");
        $this->_otpType = $this->sanitizeFormPOST("\160\x69\x65\x5f\x65\156\141\142\x6c\x65\x5f\164\171\160\x65");
        $this->_phoneKey = $this->sanitizeFormPOST("\x70\x69\145\x5f\160\x68\157\x6e\145\x5f\x66\x69\145\x6c\x64\x5f\153\x65\x79");
        update_mo_option("\x70\151\x65\137\x64\x65\x66\x61\x75\x6c\x74\x5f\x65\156\141\x62\154\x65", $this->_isFormEnabled);
        update_mo_option("\160\x69\x65\x5f\145\x6e\141\x62\154\x65\137\164\171\x70\x65", $this->_otpType);
        update_mo_option("\x70\x69\145\x5f\x70\x68\157\156\x65\x5f\153\145\x79", $this->_phoneKey);
    }
}
