<?php

function delete_user_n_post_meta($user_exist,$user_id,$post_id){
	if ($user_exist) {
		delete_user_meta($user_id, "emilotpauthn");
		delete_user_meta($user_id, "emilotpsent");
		delete_user_meta($user_id, "emilotptime");
		delete_user_meta($user_id, "emilotplsttim");
		delete_user_meta($user_id, "emilotpactn");
		delete_user_meta($user_id, "emilotpatmpt");
	} else {
		wp_delete_post($post_id, true);
	}
}
function get_emilotpauthn_post_id($key, $value) {
	global $wpdb;
	$meta = $wpdb->get_results("SELECT * FROM `".$wpdb->postmeta."` WHERE meta_key='".$wpdb->escape($key)."' AND meta_value='".$wpdb->escape($value)."'");
	if (is_array($meta) && !empty($meta) && isset($meta[0])) {
		$meta = $meta[0];
	}
	if (is_object($meta)) {
		return $meta->post_id;
	}
	else {
		return 0;
	}
}

function emailotpauthn_is_ajaxapicron_call($eotpa_callerid){
	/*
	$log_process_dtl = "New Call from: " . $eotpa_callerid . " ::: Type:" .
    ((defined('DOING_AJAX') && DOING_AJAX) ? "AJAX" :
    ((defined('REST_REQUEST') && REST_REQUEST) ? "REST" :
    ((defined('DOING_CRON') && DOING_CRON) ? "CRON" : "NORMAL"))) . " ::: " .
    (!empty($_REQUEST['action']) ? "Action:" . sanitize_text_field($_REQUEST['action']) . " ::: " : "") .
    ((defined('REST_REQUEST') && REST_REQUEST) ? "Route:" . (isset($_SERVER['REQUEST_URI']) ? esc_url_raw($_SERVER['REQUEST_URI']) : "N.A.") . " ::: " : "") .
	"Method:" . (empty($_SERVER['REQUEST_METHOD']) ? "N.A." : $_SERVER['REQUEST_METHOD']) . " ::: " .
    "Script:" . (empty($_SERVER['SCRIPT_NAME']) ? "N.A." : $_SERVER['SCRIPT_NAME']) . " ::: " .
    "User:" . (function_exists('is_user_logged_in') ? (is_user_logged_in() ? "Member" : "Guest") : "N.A.") . " ;;; ";
	eotpa_caller_logging($log_process_dtl);
	*/
    
    if ($eotpa_callerid == 5 || $eotpa_callerid == 6) {
		//if (!empty($_REQUEST['action']) && strcasecmp($_REQUEST["action"],"emailotpauthnaction")==0) return false;
        if (defined('DOING_AJAX') && DOING_AJAX) return true;
        if (defined('REST_REQUEST') && REST_REQUEST) return true;
        if (defined('DOING_CRON') && DOING_CRON) return true;
    }

    return false;
}

function eotpa_remove_autotriggered($del_usersemail,$del_usersid=0){
	if(!empty($del_usersemail)) delete_transient("_eotpa_form_autotriggered_" . $del_usersemail);
	if(!empty($del_usersid)) {
		delete_user_meta( $del_usersid, "_eotpa_2fa_redirected_user");
		delete_user_meta( $del_usersid, "_eotpa_2fa_enforced_redirected");
	}
}

function emailotpauthn_formtexts(){
	global $extrnlcvrbl,$formtexts;
	// The following goto label is used only to support upgrading the saved option via the "if" block below.
	setformnoticentxt:
	if(!isset($formtexts) || empty($formtexts)) emailotpauthn_load_notesntexts();
	$formnoticentxt = get_fix_option("emailotpauthn_formnoticentxt","not_initiated");
	if(strcasecmp($formnoticentxt,"not_initiated")==0){
		if(isset($formtexts) && !empty($formtexts)){
			$newnoticentxt = "";
			$nodx = 0;
			foreach ($formtexts as $formtext) {
				if(isset($formtext[2]) && !empty($formtext[2])){
					if(!isset($extrnlcvrbl[$nodx]) || empty($extrnlcvrbl[$nodx])){
						$extrnlcvrbl[$nodx] = $formtext[2];
						$newnoticentxt .= $formtext[2] . "#-ntbreak-#";
					}
				}
				$nodx++;
			}
			$newnoticentxt = trim($newnoticentxt,"#-ntbreak-#");
			update_option("emailotpauthn_formnoticentxt", bin2hex($newnoticentxt));
		}
	} else {
		$formnoticentxt = "#-ntbreak-#" . hex2bin($formnoticentxt);
		$savednoticentxts = explode("#-ntbreak-#",$formnoticentxt);
		// The following "if" block updates the previously saved external text to the upgraded format for version 6.3.5.
		if(isset($formtexts) && !empty($formtexts)){
			$countsavednoticentxts = count($savednoticentxts);
			$countformtexts = count($formtexts);
			if($countsavednoticentxts < count($formtexts)){
				if($countsavednoticentxts < 67) {
					$formtexts[66][2] = $savednoticentxts[64];
					$savednoticentxts[64] = $formtexts[64][2];
					$savednoticentxts[65] = $formtexts[65][2];
					$savednoticentxts[66] = $formtexts[66][2];
				}
				for ($formtxtlen = $countsavednoticentxts; $formtxtlen < $countformtexts; $formtxtlen++) {
					$savednoticentxts[$formtxtlen] = $formtexts[$formtxtlen][2];
				}
				$formnoticentxt = trim(implode("#-ntbreak-#",$savednoticentxts),"#-ntbreak-#");
				update_option("emailotpauthn_formnoticentxt", bin2hex($formnoticentxt));
				goto setformnoticentxt;
			}
		}
		$nodx = 0;
		foreach ($savednoticentxts as $savednoticentxt) {
			if(isset($savednoticentxt)){
				$extrnlcvrbl[$nodx] = $savednoticentxt;
				$formtexts[$nodx][2] = $extrnlcvrbl[$nodx];
			}
			$nodx++;
		}
	}
}
function emailotpauthn_settingsdetails($getthisval="",$setthisval=""){
	$emptysetting = "not_initiated";
	$defaultsettings = array(
		"iscornrstyl"=>0,
		"isshortbuttons"=>0,
		"hideautofileml"=>0,
		"hidescrollbars"=>0,
		"isoneinputbox"=>0,
		"swichtolgoutbtn"=>0,
		"setupwizardstep"=>0,
		"emailotpauthn_poppopcimeg"=>100,
		"emailotpauthn_bkdrpopcimg"=>100,
		"emailotpauthn_resetdefaults"=>0,
		"emailotpauthn_is_externalcss"=>0,
		"emailotpauthn_logination"=>0,
		"emailotpauthn_redirectaftrlogin"=>0,
		"emailotpauthn_allowloginlinks"=>0,
		"emailotpauthn_emailsettings"=>0,
		"emailotpauthn_gnrtprocesslog"=>0,
		"emailotpauthn_authentication"=>0,
		"emailotpauthn_askuseremail"=>0,
		"emailotpauthn_enbladvncadmin"=>0,
		"emailotpauthn_wrongotplogout"=>0,
		"emailotpauthn_mmbrvrfynrdrct"=>0,
		"emailotpauthn_mmbristokenkey"=>0,
		"emailotpauthn_customization"=>0,
		"emailotpauthn_redirectfor2fa"=>0,
		"emailotpauthn_the2fatoadmins"=>0,
		"emailotpauthn_2fa_time_out"=>0,
		"emailotpauthn_2fa_on_login"=>0,
		"emailotpauthn_2fa_on_regtr"=>0,
		"emailotpauthn_2fa_sessions"=>0,
		"emailotpauthn_2fa_on_oldsn"=>0,
		"emailotpauthn_2fa_on_newsn"=>0,
		"emailotpauthn_generateotplog"=>0,
		"emailotpauthn_rgstrauthnticn"=>0,
		"emailotpauthn_regtrdusername"=>0,
		"emailotpauthn_rgstrshowterms"=>0,
		"emailotpauthn_rgstrthnlogdin"=>0,
		"emailotpauthn_rgstrthnredrct"=>0,
		"emailotpauthn_guestauthnticn"=>0,
		"emailotpauthn_gustvrfynrdrct"=>0,
		"emailotpauthn_gustistokenkey"=>0,
		"emailotpauthn_isblockemails"=>0,
		"emailotpauthn_isallowemails"=>0,
		"emailotpauthn_isblockmailid"=>0,
		"emailotpauthn_excldlogdinusr"=>0,
		"emailotpauthn_excldlogdinmbr"=>0,
		"emailotpauthn_addexternalcss"=>0,
		"emailotpauthn_firelimit"=>0,
		"emailotpauthn_duralimit"=>0,
		"emailotpauthn_validfor"=>0,
		"emailotpauthn_cleanaftrdays"=>0,
		"emailotpauthn_poppboxblur"=>0,
		"emailotpauthn_popupbgblur"=>0,
		"emailotpauthn_frmbrdrwdth"=>0,
		"emailotpauthn_rtryatmptlimit"=>0,
		"emailotpauthn_tokenvlidfrmbr"=>0,
		"emailotpauthn_tokenvlidfrgst"=>0,
		"emailotpauthn_isactvfrmmail"=>0,
		"emailotpauthn_useexternalcss"=>0,
		"emailotpauthn_otplogedpostid"=>0,
		"emailotpauthn_popupboxmargin"=>0,
		"emailotpauthn_frmnoticsiz"=>83,
		"emailotpauthn_licensekey"=>"Not_Required",
		"emailotpauthn_hedrxtstyle"=>"font-family:Verdana, Geneva, sans-serif!important;color:#ffffff;font-size:22px;font-weight:400!important;letter-spacing:0.2px;word-spacing:0px;",
		"emailotpauthn_headercolor"=>"#881100",
		"emailotpauthn_inptxtstyle"=>"font-family:Verdana, Geneva, sans-serif!important;color:#666666;font-size:16px;font-weight:400!important;letter-spacing:0px;word-spacing:0px;",
		"emailotpauthn_inptbxcolor"=>"#FFFFDD",
		"emailotpauthn_butnxtstyle"=>"font-family:Verdana, Geneva, sans-serif!important;color:#ffffff;font-size:16px;font-weight:600!important;letter-spacing:0.5px;word-spacing:1px;",
		"emailotpauthn_buttoncolor"=>"#772200",
		"emailotpauthn_bodyxtstyle"=>"font-family:Verdana, Geneva, sans-serif!important;color:#000000;font-size:16px;font-weight:400!important;letter-spacing:0px;word-spacing:0px;",
		"emailotpauthn_bodybgcolor"=>"#DDAA88",
		"emailotpauthn_poppbgcolor"=>"#00000080",
		"emailotpauthn_frmbrdrcolr"=>"#ffffff",
		"emailotpauthn_frmnoticclr"=>"#bb0000",
		"emailotpauthn_poppboximeg"=>"",
		"emailotpauthn_backdrpimeg"=>"",
		"emailotpauthn_redirect_url"=>"",
		"emailotpauthn_redirect2faurl"=>"",
		"emailotpauthn_rdrct2faurlout"=>"",
		"emailotpauthn_rgstrdefltrole"=>"",
		"emailotpauthn_rgstrtermouurl"=>"",
		"emailotpauthn_rgstrredrcturl"=>"",
		"emailotpauthn_mmbrvrfrdcturl"=>"",
		"emailotpauthn_mmbrofrdcturls"=>"",
		"emailotpauthn_mmbrrjtrdcturl"=>"",
		"emailotpauthn_gustvrfrdcturl"=>"",
		"emailotpauthn_gustofrdcturls"=>"",
		"emailotpauthn_gustrjtrdcturl"=>"",
		"emailotpauthn_redirect_out"=>"",
		"emailotpauthn_sendfromemail"=>"",
		"emailotpauthn_sendfromcmail"=>"",
		"emailotpauthn_popuphorzalign"=>"",
	);
	$savedsettings = $defaultsettings;
	//The below 2 lines of code is UMOVS2NVS
	$predsettingspending = true;
	savedsettingscheck:
	$settingsdetls = get_option('emailotpauthn_settingsdetls',$emptysetting);
	$settingsdetls = isset($settingsdetls) ? (empty(trim($settingsdetls)) ? $emptysetting : $settingsdetls) : $emptysetting;
	if(strcasecmp($settingsdetls,$emptysetting)==0){
		//The below IF section code is UMOVS2NVS
		if($predsettingspending) {
			$setingsmigrator = emailotpauthn_setingsmigrator($defaultsettings);
			$predsettingspending = false;
			if($setingsmigrator) goto savedsettingscheck;
		}
		$newsettingsvals = json_encode($defaultsettings);
		if(!empty($newsettingsvals)) update_option("emailotpauthn_settingsdetls", $newsettingsvals);
	} else {
		$savedsettings = json_decode($settingsdetls,true);
		if (json_last_error() !== 0) $savedsettings = unserialize($settingsdetls); // This line is supporting the version 6.2.3 to 6.3.3 settings. UMOVS2NVS
		//The below IF section code is UMOVS2NVS
		if(!isset($savedsettings["emailotpauthn_emailsettings"])){
			if(isset($savedsettings["setupwizardstep"])){
				if($savedsettings["setupwizardstep"]==12){
					$savedsettings["setupwizardstep"]=10;
				}
			}
		}
		if (!empty(array_diff_key($defaultsettings, $savedsettings))) {
			$defaultsettings = array_merge($defaultsettings, $savedsettings);
			delete_option("emailotpauthn_settingsdetls");
			$predsettingspending = false;
			goto savedsettingscheck;
		}
		if(!empty($setthisval)){
			if(str_contains($setthisval,"[;;;]")){
				$setthisprms = explode("[;;;]",$setthisval);
				foreach ($setthisprms as $setthisprm) {
					$setthisvals = explode("[:::]",$setthisprm);
					if(count($setthisvals) == 2){
						if(is_numeric($setthisvals[1])) $setthisvals[1] += 0;
						$savedsettings[$setthisvals[0]] = $setthisvals[1];
					}
				}
			} else {
				$setthisvals = explode("[:::]",$setthisval);
				if(count($setthisvals) == 2){
					if(is_numeric($setthisvals[1])) $setthisvals[1] += 0;
					$savedsettings[$setthisvals[0]] = $setthisvals[1];
				}
			}
			$newsettingsvals = json_encode($savedsettings);
			if(!empty($newsettingsvals)) update_option("emailotpauthn_settingsdetls", $newsettingsvals);
		}
	}
	if(empty($getthisval)){
		global $eotpasettings;
		$eotpasettings = $savedsettings;
	} else {
		$dfltsettingval = $defaultsettings[$getthisval];
		$dfltsettingval = isset($dfltsettingval) ? (empty(trim($dfltsettingval)) ? ($dfltsettingval===false ? $dfltsettingval : "0") : $dfltsettingval) : "0";
		$savdsettingval = $savedsettings[$getthisval];
		$savdsettingval = isset($savdsettingval) ? (empty(trim($savdsettingval)) ? ($savdsettingval===false ? $savdsettingval : $dfltsettingval) : $savdsettingval) : $dfltsettingval;
		if(is_numeric($savdsettingval)) $savdsettingval += 0; else $savdsettingval = strval($savdsettingval);
		return $savdsettingval;
	}
}
function emailotpauthn_setingsmigrator($migrator_defaultsettings){
	//This fumction is UMOVS2NVS [used to migrate older and version 6.2.4 settings to the new version settings].
	if ( strcasecmp("not-exists",get_option( "emailotpauthn_emailsettings", "not-exists" )) != 0 ) {
		$migrator_defaultsettings["iscornrstyl"] = get_fix_option("iscornrstyl",$migrator_defaultsettings["iscornrstyl"]);
		$migrator_defaultsettings["isshortbuttons"] = get_fix_option("isshortbuttons",$migrator_defaultsettings["isshortbuttons"]);
		$migrator_defaultsettings["hideautofileml"] = get_fix_option("hideautofileml",$migrator_defaultsettings["hideautofileml"]);
		$migrator_defaultsettings["hidescrollbars"] = get_fix_option("hidescrollbars",$migrator_defaultsettings["hidescrollbars"]);
		$migrator_defaultsettings["isoneinputbox"] = get_fix_option("isoneinputbox",$migrator_defaultsettings["isoneinputbox"]);
		$migrator_defaultsettings["setupwizardstep"] = get_fix_option("setupwizardstep",$migrator_defaultsettings["setupwizardstep"]);
		$migrator_defaultsettings["emailotpauthn_poppopcimeg"] = get_fix_option("emailotpauthn_poppopcimeg",$migrator_defaultsettings["emailotpauthn_poppopcimeg"]);
		$migrator_defaultsettings["emailotpauthn_bkdrpopcimg"] = get_fix_option("emailotpauthn_bkdrpopcimg",$migrator_defaultsettings["emailotpauthn_bkdrpopcimg"]);
		$migrator_defaultsettings["emailotpauthn_resetdefaults"] = get_fix_option("emailotpauthn_resetdefaults",$migrator_defaultsettings["emailotpauthn_resetdefaults"]);
		$migrator_defaultsettings["emailotpauthn_is_externalcss"] = get_fix_option("emailotpauthn_is_externalcss",$migrator_defaultsettings["emailotpauthn_is_externalcss"]);
		$migrator_defaultsettings["emailotpauthn_logination"] = get_fix_option("emailotpauthn_logination",$migrator_defaultsettings["emailotpauthn_logination"]);
		$migrator_defaultsettings["emailotpauthn_redirectaftrlogin"] = get_fix_option("emailotpauthn_redirectaftrlogin",$migrator_defaultsettings["emailotpauthn_redirectaftrlogin"]);
		$migrator_defaultsettings["emailotpauthn_emailsettings"] = get_fix_option("emailotpauthn_emailsettings",$migrator_defaultsettings["emailotpauthn_emailsettings"]);
		$migrator_defaultsettings["emailotpauthn_gnrtprocesslog"] = get_fix_option("emailotpauthn_gnrtprocesslog",$migrator_defaultsettings["emailotpauthn_gnrtprocesslog"]);
		$migrator_defaultsettings["emailotpauthn_authentication"] = get_fix_option("emailotpauthn_authentication",$migrator_defaultsettings["emailotpauthn_authentication"]);
		$migrator_defaultsettings["emailotpauthn_askuseremail"] = get_fix_option("emailotpauthn_askuseremail",$migrator_defaultsettings["emailotpauthn_askuseremail"]);
		$migrator_defaultsettings["emailotpauthn_wrongotplogout"] = get_fix_option("emailotpauthn_wrongotplogout",$migrator_defaultsettings["emailotpauthn_wrongotplogout"]);
		$migrator_defaultsettings["emailotpauthn_mmbrvrfynrdrct"] = get_fix_option("emailotpauthn_mmbrvrfynrdrct",$migrator_defaultsettings["emailotpauthn_mmbrvrfynrdrct"]);
		$migrator_defaultsettings["emailotpauthn_mmbristokenkey"] = get_fix_option("emailotpauthn_mmbristokenkey",$migrator_defaultsettings["emailotpauthn_mmbristokenkey"]);
		$migrator_defaultsettings["emailotpauthn_customization"] = get_fix_option("emailotpauthn_customization",$migrator_defaultsettings["emailotpauthn_customization"]);
		$migrator_defaultsettings["emailotpauthn_redirectfor2fa"] = get_fix_option("emailotpauthn_redirectfor2fa",$migrator_defaultsettings["emailotpauthn_redirectfor2fa"]);
		$migrator_defaultsettings["emailotpauthn_the2fatoadmins"] = get_fix_option("emailotpauthn_the2fatoadmins",$migrator_defaultsettings["emailotpauthn_the2fatoadmins"]);
		$migrator_defaultsettings["emailotpauthn_generateotplog"] = get_fix_option("emailotpauthn_generateotplog",$migrator_defaultsettings["emailotpauthn_generateotplog"]);
		$migrator_defaultsettings["emailotpauthn_rgstrauthnticn"] = get_fix_option("emailotpauthn_rgstrauthnticn",$migrator_defaultsettings["emailotpauthn_rgstrauthnticn"]);
		$migrator_defaultsettings["emailotpauthn_regtrdusername"] = get_fix_option("emailotpauthn_regtrdusername",$migrator_defaultsettings["emailotpauthn_regtrdusername"]);
		$migrator_defaultsettings["emailotpauthn_rgstrshowterms"] = get_fix_option("emailotpauthn_rgstrshowterms",$migrator_defaultsettings["emailotpauthn_rgstrshowterms"]);
		$migrator_defaultsettings["emailotpauthn_rgstrthnlogdin"] = get_fix_option("emailotpauthn_rgstrthnlogdin",$migrator_defaultsettings["emailotpauthn_rgstrthnlogdin"]);
		$migrator_defaultsettings["emailotpauthn_rgstrthnredrct"] = get_fix_option("emailotpauthn_rgstrthnredrct",$migrator_defaultsettings["emailotpauthn_rgstrthnredrct"]);
		$migrator_defaultsettings["emailotpauthn_guestauthnticn"] = get_fix_option("emailotpauthn_guestauthnticn",$migrator_defaultsettings["emailotpauthn_guestauthnticn"]);
		$migrator_defaultsettings["emailotpauthn_gustvrfynrdrct"] = get_fix_option("emailotpauthn_gustvrfynrdrct",$migrator_defaultsettings["emailotpauthn_gustvrfynrdrct"]);
		$migrator_defaultsettings["emailotpauthn_gustistokenkey"] = get_fix_option("emailotpauthn_gustistokenkey",$migrator_defaultsettings["emailotpauthn_gustistokenkey"]);
		$migrator_defaultsettings["emailotpauthn_isblockemails"] = get_fix_option("emailotpauthn_isblockemails",$migrator_defaultsettings["emailotpauthn_isblockemails"]);
		$migrator_defaultsettings["emailotpauthn_isallowemails"] = get_fix_option("emailotpauthn_isallowemails",$migrator_defaultsettings["emailotpauthn_isallowemails"]);
		$migrator_defaultsettings["emailotpauthn_isblockmailid"] = get_fix_option("emailotpauthn_isblockmailid",$migrator_defaultsettings["emailotpauthn_isblockmailid"]);
		$migrator_defaultsettings["emailotpauthn_excldlogdinusr"] = get_fix_option("emailotpauthn_excldlogdinusr",$migrator_defaultsettings["emailotpauthn_excldlogdinusr"]);
		$migrator_defaultsettings["emailotpauthn_excldlogdinmbr"] = get_fix_option("emailotpauthn_excldlogdinmbr",$migrator_defaultsettings["emailotpauthn_excldlogdinmbr"]);
		$migrator_defaultsettings["emailotpauthn_addexternalcss"] = get_fix_option("emailotpauthn_addexternalcss",$migrator_defaultsettings["emailotpauthn_addexternalcss"]);
		$migrator_defaultsettings["emailotpauthn_firelimit"] = get_fix_option("emailotpauthn_firelimit",$migrator_defaultsettings["emailotpauthn_firelimit"]);
		$migrator_defaultsettings["emailotpauthn_duralimit"] = get_fix_option("emailotpauthn_duralimit",$migrator_defaultsettings["emailotpauthn_duralimit"]);
		$migrator_defaultsettings["emailotpauthn_validfor"] = get_fix_option("emailotpauthn_validfor",$migrator_defaultsettings["emailotpauthn_validfor"]);
		$migrator_defaultsettings["emailotpauthn_cleanaftrdays"] = get_fix_option("emailotpauthn_cleanaftrdays",$migrator_defaultsettings["emailotpauthn_cleanaftrdays"]);
		$migrator_defaultsettings["emailotpauthn_poppboxblur"] = get_fix_option("emailotpauthn_poppboxblur",$migrator_defaultsettings["emailotpauthn_poppboxblur"]);
		$migrator_defaultsettings["emailotpauthn_popupbgblur"] = get_fix_option("emailotpauthn_popupbgblur",$migrator_defaultsettings["emailotpauthn_popupbgblur"]);
		$migrator_defaultsettings["emailotpauthn_frmbrdrwdth"] = get_fix_option("emailotpauthn_frmbrdrwdth",$migrator_defaultsettings["emailotpauthn_frmbrdrwdth"]);
		$migrator_defaultsettings["emailotpauthn_rtryatmptlimit"] = get_fix_option("emailotpauthn_rtryatmptlimit",$migrator_defaultsettings["emailotpauthn_rtryatmptlimit"]);
		$migrator_defaultsettings["emailotpauthn_tokenvlidfrmbr"] = get_fix_option("emailotpauthn_tokenvlidfrmbr",$migrator_defaultsettings["emailotpauthn_tokenvlidfrmbr"]);
		$migrator_defaultsettings["emailotpauthn_tokenvlidfrgst"] = get_fix_option("emailotpauthn_tokenvlidfrgst",$migrator_defaultsettings["emailotpauthn_tokenvlidfrgst"]);
		$migrator_defaultsettings["emailotpauthn_isactvfrmmail"] = get_fix_option("emailotpauthn_isactvfrmmail",$migrator_defaultsettings["emailotpauthn_isactvfrmmail"]);
		$migrator_defaultsettings["emailotpauthn_useexternalcss"] = get_fix_option("emailotpauthn_useexternalcss",$migrator_defaultsettings["emailotpauthn_useexternalcss"]);
		$migrator_defaultsettings["emailotpauthn_otplogedpostid"] = get_fix_option("emailotpauthn_otplogedpostid",$migrator_defaultsettings["emailotpauthn_otplogedpostid"]);
		$migrator_defaultsettings["emailotpauthn_popupboxmargin"] = get_fix_option("emailotpauthn_popupboxmargin",$migrator_defaultsettings["emailotpauthn_popupboxmargin"]);
		$migrator_defaultsettings["emailotpauthn_frmnoticsiz"] = get_fix_option("emailotpauthn_frmnoticsiz",$migrator_defaultsettings["emailotpauthn_frmnoticsiz"]);
		$migrator_defaultsettings["emailotpauthn_licensekey"] = get_fix_option("emailotpauthn_licensekey",$migrator_defaultsettings["emailotpauthn_licensekey"]);
		$migrator_defaultsettings["emailotpauthn_hedrxtstyle"] = get_fix_option("emailotpauthn_hedrxtstyle",$migrator_defaultsettings["emailotpauthn_hedrxtstyle"]);
		$migrator_defaultsettings["emailotpauthn_headercolor"] = get_fix_option("emailotpauthn_headercolor",$migrator_defaultsettings["emailotpauthn_headercolor"]);
		$migrator_defaultsettings["emailotpauthn_inptxtstyle"] = get_fix_option("emailotpauthn_inptxtstyle",$migrator_defaultsettings["emailotpauthn_inptxtstyle"]);
		$migrator_defaultsettings["emailotpauthn_inptbxcolor"] = get_fix_option("emailotpauthn_inptbxcolor",$migrator_defaultsettings["emailotpauthn_inptbxcolor"]);
		$migrator_defaultsettings["emailotpauthn_butnxtstyle"] = get_fix_option("emailotpauthn_butnxtstyle",$migrator_defaultsettings["emailotpauthn_butnxtstyle"]);
		$migrator_defaultsettings["emailotpauthn_buttoncolor"] = get_fix_option("emailotpauthn_buttoncolor",$migrator_defaultsettings["emailotpauthn_buttoncolor"]);
		$migrator_defaultsettings["emailotpauthn_bodyxtstyle"] = get_fix_option("emailotpauthn_bodyxtstyle",$migrator_defaultsettings["emailotpauthn_bodyxtstyle"]);
		$migrator_defaultsettings["emailotpauthn_bodybgcolor"] = get_fix_option("emailotpauthn_bodybgcolor",$migrator_defaultsettings["emailotpauthn_bodybgcolor"]);
		$migrator_defaultsettings["emailotpauthn_poppbgcolor"] = get_fix_option("emailotpauthn_poppbgcolor",$migrator_defaultsettings["emailotpauthn_poppbgcolor"]);
		$migrator_defaultsettings["emailotpauthn_frmbrdrcolr"] = get_fix_option("emailotpauthn_frmbrdrcolr",$migrator_defaultsettings["emailotpauthn_frmbrdrcolr"]);
		$migrator_defaultsettings["emailotpauthn_frmnoticclr"] = get_fix_option("emailotpauthn_frmnoticclr",$migrator_defaultsettings["emailotpauthn_frmnoticclr"]);
		$migrator_defaultsettings["emailotpauthn_poppboximeg"] = get_fix_option("emailotpauthn_poppboximeg",$migrator_defaultsettings["emailotpauthn_poppboximeg"]);
		$migrator_defaultsettings["emailotpauthn_backdrpimeg"] = get_fix_option("emailotpauthn_backdrpimeg",$migrator_defaultsettings["emailotpauthn_backdrpimeg"]);
		$migrator_defaultsettings["emailotpauthn_redirect_url"] = get_fix_option("emailotpauthn_redirect_url",$migrator_defaultsettings["emailotpauthn_redirect_url"]);
		$migrator_defaultsettings["emailotpauthn_redirect2faurl"] = get_fix_option("emailotpauthn_redirect2faurl",$migrator_defaultsettings["emailotpauthn_redirect2faurl"]);
		$migrator_defaultsettings["emailotpauthn_rgstrdefltrole"] = get_fix_option("emailotpauthn_rgstrdefltrole",$migrator_defaultsettings["emailotpauthn_rgstrdefltrole"]);
		$migrator_defaultsettings["emailotpauthn_rgstrtermouurl"] = get_fix_option("emailotpauthn_rgstrtermouurl",$migrator_defaultsettings["emailotpauthn_rgstrtermouurl"]);
		$migrator_defaultsettings["emailotpauthn_rgstrredrcturl"] = get_fix_option("emailotpauthn_rgstrredrcturl",$migrator_defaultsettings["emailotpauthn_rgstrredrcturl"]);
		$migrator_defaultsettings["emailotpauthn_mmbrvrfrdcturl"] = get_fix_option("emailotpauthn_mmbrvrfrdcturl",$migrator_defaultsettings["emailotpauthn_mmbrvrfrdcturl"]);
		$migrator_defaultsettings["emailotpauthn_mmbrofrdcturls"] = get_fix_option("emailotpauthn_mmbrofrdcturls",$migrator_defaultsettings["emailotpauthn_mmbrofrdcturls"]);
		$migrator_defaultsettings["emailotpauthn_mmbrrjtrdcturl"] = get_fix_option("emailotpauthn_mmbrrjtrdcturl",$migrator_defaultsettings["emailotpauthn_mmbrrjtrdcturl"]);
		$migrator_defaultsettings["emailotpauthn_gustvrfrdcturl"] = get_fix_option("emailotpauthn_gustvrfrdcturl",$migrator_defaultsettings["emailotpauthn_gustvrfrdcturl"]);
		$migrator_defaultsettings["emailotpauthn_gustofrdcturls"] = get_fix_option("emailotpauthn_gustofrdcturls",$migrator_defaultsettings["emailotpauthn_gustofrdcturls"]);
		$migrator_defaultsettings["emailotpauthn_gustrjtrdcturl"] = get_fix_option("emailotpauthn_gustrjtrdcturl",$migrator_defaultsettings["emailotpauthn_gustrjtrdcturl"]);
		$migrator_defaultsettings["emailotpauthn_redirect_out"] = get_fix_option("emailotpauthn_redirect_out",$migrator_defaultsettings["emailotpauthn_redirect_out"]);
		$migrator_defaultsettings["emailotpauthn_sendfromemail"] = get_fix_option("emailotpauthn_sendfromemail",$migrator_defaultsettings["emailotpauthn_sendfromemail"]);
		$migrator_defaultsettings["emailotpauthn_sendfromcmail"] = get_fix_option("emailotpauthn_sendfromcmail",$migrator_defaultsettings["emailotpauthn_sendfromcmail"]);
		$migrator_defaultsettings["emailotpauthn_popuphorzalign"] = get_fix_option("emailotpauthn_popuphorzalign",$migrator_defaultsettings["emailotpauthn_popuphorzalign"]);
		
		$migrator_newsettingsvals = json_encode($migrator_defaultsettings);
		if(!empty($migrator_newsettingsvals)) update_option("emailotpauthn_settingsdetls", $migrator_newsettingsvals);
		
		update_option("emailotpauthn_isdemofilplcd","");
		$migrator_email_subject = get_fix_option("emailotpauthn_email_subject","");
		if(emailotpauthn_isascii($migrator_email_subject)) update_option("emailotpauthn_email_subject",bin2hex($migrator_email_subject));
		$migrator_email_body = get_fix_option("emailotpauthn_email_body","");
		if(emailotpauthn_isascii($migrator_email_body)) update_option("emailotpauthn_email_body",bin2hex($migrator_email_body));
		$migrator_blockedemails = get_fix_option("emailotpauthn_blockedemails","");
		if(emailotpauthn_isascii($migrator_blockedemails)) update_option("emailotpauthn_blockedemails",bin2hex($migrator_blockedemails));
		$migrator_allowedemails = get_fix_option("emailotpauthn_allowedemails","");
		if(emailotpauthn_isascii($migrator_allowedemails)) update_option("emailotpauthn_allowedemails",bin2hex($migrator_allowedemails));
		$migrator_blockedmailid = get_fix_option("emailotpauthn_blockedmailid","");
		if(emailotpauthn_isascii($migrator_blockedmailid)) update_option("emailotpauthn_blockedmailid",bin2hex($migrator_blockedmailid));
		$migrator_dfltextrnalcss = get_fix_option("emailotpauthn_dfltextrnalcss","");
		if(emailotpauthn_isascii($migrator_dfltextrnalcss)) update_option("emailotpauthn_dfltextrnalcss",bin2hex($migrator_dfltextrnalcss));
		$migrator_mdfdextrnalcss = get_fix_option("emailotpauthn_mdfdextrnalcss","");
		if(emailotpauthn_isascii($migrator_mdfdextrnalcss)) update_option("emailotpauthn_mdfdextrnalcss",bin2hex($migrator_mdfdextrnalcss));
		$migrator_formnoticentxt = get_fix_option("emailotpauthn_formnoticentxt","");
		if(emailotpauthn_isascii($migrator_formnoticentxt)) update_option("emailotpauthn_formnoticentxt",bin2hex($migrator_formnoticentxt));
		
		return true;
	}
	return false;
}
function emailotpauthn_isascii(string $eotpastr) {
	if(empty($eotpastr)) return false;
    if (str_starts_with(strtolower($eotpastr), '0x')) {
        $eotpastr = substr($eotpastr, 2);
    }
    return (!ctype_xdigit($eotpastr));
}

function get_fix_option($optnval,$dflt){
	$notexist = "not-exists";
	$sival = get_option($optnval,$notexist);
	if($sival == null)
		$sival = $notexist;
	elseif(!isset($sival))
		$sival = $notexist;
	//if(empty($sival)) $sival = $dflt;
	if($sival === $notexist) $sival = $dflt;
	if(is_numeric($sival)) $sival = floatval($sival); else $sival = strval($sival);
	return $sival;
}
function logging_data($rcrd){
    file_put_contents("D:\logs.txt", $rcrd . PHP_EOL, FILE_APPEND | LOCK_EX);
}
function eotpa_caller_logging($recrds){
	$dir_file = eotpa_make_plogdir();
	if(!empty($dir_file)) @file_put_contents($dir_file . "/caller_details.txt", wp_date("H:i:s d-M-Y") ." -> ". $recrds . PHP_EOL, FILE_APPEND | LOCK_EX);
}
function process_logging($rcrds){
	global $eotpasettings;
	$gnrtprocesslog = $eotpasettings["emailotpauthn_gnrtprocesslog"];
	$dir_file = eotpa_make_plogdir();
	if ($gnrtprocesslog && !empty($dir_file)) {
		$crnt_time = wp_date("H:i:s d-M-Y");
		$logfilename = strtotime($crnt_time) . '_' . substr(md5(mt_rand()), 0, 11);
		$logfilename = @file_put_contents($dir_file . "/" . $logfilename, current_datetime()->format("H:i:s.v d-M-Y") ." -> ". str_replace('\n', PHP_EOL, $rcrds), LOCK_EX);
	}
}
function eotpa_make_plogdir(){
	$dir_file = dirname(__FILE__) . "/plogfiles";
	if (!is_dir($dir_file)) @mkdir($dir_file, 0755, true);
	if (is_dir($dir_file) && is_writable($dir_file)){
		$indexfilepath = $dir_file . "/index.php";
		if (!file_exists($indexfilepath)) {
			$indexfilecontent = chr(60) . chr(63) . "php" . PHP_EOL . "/* Silence is golden. */" . PHP_EOL . chr(63) . chr(62);
			$indexfilecontent = @file_put_contents($indexfilepath, $indexfilecontent, LOCK_EX);
		}
		return $dir_file;
	}
	return "";
}

?>