<?php
$isTrialCopy = true;
if (!defined("ABSPATH")) {
    exit();
} // Exit if accessed directly

global $extrnlcvrbl,$inline_used,$extrnlvrbl,$eotpasettings;
$inline_used = false;

if (!class_exists("emailotpauthnFront")) {
	include(dirname(dirname(__FILE__))."/lib/emailotpauthn-external-vjs-class.php");
	$extrnlcvrbl = $extrnlvrbl;
    class emailotpauthnFront
    {
        /**
         * Construct the plugin object
         */
        public function __construct()
        {
            // Is enable settings from admin
			if (emailotpauthn_is_ajaxapicron_call(1)) return;
            $isEnable = get_fix_option("emailotpauthn_enable",0);
            if (!$isEnable) return;
            //front-end hooks action
			global $eotpasettings;
			if(!isset($eotpasettings) || count($eotpasettings) < 5) emailotpauthn_settingsdetails();
			add_action("clear_auth_cookie", "emailotpauthn_actions_on_logout");
			add_action("wp_logout", "emailotpauthn_actions_on_logout");
            add_action("wp_footer", [&$this, "emailotpauthn_popup_html"]);
            add_action("wp_ajax_nopriv_emailotpauthnaction", [ &$this, "emailotpauthn_login_action", ]); // for logged-out users only.
            add_action("wp_enqueue_scripts", [ &$this, "emailotpauthn_enqueue_scripts_hooks", ],9999);
            add_shortcode("email_otp_authn", [&$this, "email_otp_authn_func"]);
        } // END public function __construct

        public function email_otp_authn_func($atts)
        {
			global $extrnlcvrbl,$inline_used;
			if($inline_used) return "";
			if(empty($extrnlcvrbl)) emailotpauthn_formtexts();
			$title = $extrnlcvrbl[1];
			$title = isset($atts["title"]) ? (empty(trim($atts["title"])) ? $title : $atts["title"]) : $title;
			$type = "popup";
            $type = isset($atts["type"]) ? (empty(trim($atts["type"])) ? $type : $atts["type"]) : $type;

			$type = substr(strtolower(trim(strip_tags($type))),0,12);
            $title = substr(trim(strip_tags($title)),0,250);

			if($type=="inline") {
				$setintrnlvars = array("dflt_action"=>"");
				
				ob_start();
				$button = "";
				$button = $this->emailotpauthn_popup_html($setintrnlvars);
				$obgetclean = ob_get_clean();
				if(!empty($obgetclean)) $button = $obgetclean;
				//$inline_used = true;
			} else {
				if($type=="autopopup"){
					$isemptytitle = isset($atts["title"]) ? (empty(trim($atts["title"])) ? true : false) : true;
					if($isemptytitle) $title = "";
				}
				if($type!="autopopup") $type = "";
				$button = '<span class="emailotpauthn-shortcode emailotpauthn-popup '. $type .'"><a href="javascript:">' . $title . "</a></span>";
			}

            return $button;
        }
        public function emailotpauthn_enqueue_scripts_hooks()
        {
            //*
			//check user logged or not
			if(is_user_logged_in())
				return;
			//*/

			if (emailotpauthn_is_ajaxapicron_call(2)) return;
			global $extrnlcvrbl,$eotpasettings;
			if(empty($extrnlcvrbl)) emailotpauthn_formtexts();

			$hideautofileml = $eotpasettings["hideautofileml"];
			$hidescrollbars = $eotpasettings["hidescrollbars"];
			$isoneinputbox = $eotpasettings["isoneinputbox"];
			$allowloginlinks = $eotpasettings["emailotpauthn_allowloginlinks"];

			if(!boolval($isoneinputbox))
				wp_enqueue_script( "emailotpauthn-boxscript", EMAILOTPAUTHN_PLUGIN_DIR_URL . '/lib/js/emailotpauthn-boxscript.js', array(), '3.0.0');
			if(boolval($allowloginlinks))
				wp_enqueue_script( "emailotpauthn-loginlink", EMAILOTPAUTHN_PLUGIN_DIR_URL . '/lib/js/emailotpauthn-loginlink.js', array(), '3.0.0');
			wp_enqueue_script( "emailotpauthn-jvascript", EMAILOTPAUTHN_PLUGIN_DIR_URL . '/lib/js/emailotpauthn-class.js', array(), '3.0.0');

			$setextrnlvars = array(
				"isoneinputbox"=>$isoneinputbox,
				"allowloginlinks"=>$allowloginlinks,
				"admin_ajax_url"=>admin_url("admin-ajax.php"),
				"is_logged_in"=>0,
				"hideautofileml"=>$hideautofileml,
				"hidescrollbars"=>$hidescrollbars,
				"rgstrshowterms"=>0,
				"logged_in_email"=>"",
				"is_2fa_redirect"=>0,
				"extrnltxt_002"=>$extrnlcvrbl[2],
				"extrnltxt_003"=>$extrnlcvrbl[3],
				"extrnltxt_004"=>$extrnlcvrbl[4],
				"extrnltxt_027"=>$extrnlcvrbl[27],
				"extrnltxt_056"=>$extrnlcvrbl[56],
				"extrnltxt_057"=>$extrnlcvrbl[57],
				"extrnltxt_058"=>$extrnlcvrbl[58],
				"extrnltxt_059"=>$extrnlcvrbl[59],
				"extrnltxt_067"=>$extrnlcvrbl[67],
				"extrnltxt_068"=>$extrnlcvrbl[68],
				"extrnltxt_069"=>$extrnlcvrbl[69],
				"extrnltxt_070"=>$extrnlcvrbl[70],
				"extrnlsection"=>emailotpauthn_license_allowed(true)
				);
			$jsreturnarray = json_encode($setextrnlvars,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT);

			wp_register_script( "emailotpauthn-inlinescript", "" );
			wp_enqueue_script( "emailotpauthn-inlinescript" );
			wp_add_inline_script("emailotpauthn-inlinescript",'window.emailotpauthn_jsvariables = ' . $jsreturnarray . ';', 'before' );

			// CSS
			$iscornrstyl = $eotpasettings["iscornrstyl"];
			$isshortbuttons = $eotpasettings["isshortbuttons"];
			$poppboximeg = $eotpasettings["emailotpauthn_poppboximeg"];
			$poppopcimeg = $eotpasettings["emailotpauthn_poppopcimeg"];
			$backdrpimeg = $eotpasettings["emailotpauthn_backdrpimeg"];
			$bkdrpopcimg = $eotpasettings["emailotpauthn_bkdrpopcimg"];
			$headercolor = $eotpasettings["emailotpauthn_headercolor"];
			$inptbxcolor = $eotpasettings["emailotpauthn_inptbxcolor"];
			$buttoncolor = $eotpasettings["emailotpauthn_buttoncolor"];
			$bodybgcolor = $eotpasettings["emailotpauthn_bodybgcolor"];
			$poppbgcolor = $eotpasettings["emailotpauthn_poppbgcolor"];
			$poppboxblur = $eotpasettings["emailotpauthn_poppboxblur"];
			$popupbgblur = $eotpasettings["emailotpauthn_popupbgblur"];
			$frmbrdrwdth = $eotpasettings["emailotpauthn_frmbrdrwdth"];
			$frmbrdrcolr = $eotpasettings["emailotpauthn_frmbrdrcolr"];
			if($poppboxblur > 100)
				$poppboxblur = 50;
			elseif($poppboxblur < 0)
				$poppboxblur = 0;
			else
				$poppboxblur = number_format($poppboxblur / 2,2);
			if($popupbgblur > 100)
				$popupbgblur = 50;
			elseif($popupbgblur < 0)
				$popupbgblur = 0;
			else
				$popupbgblur = number_format($popupbgblur / 2,2);
			if($frmbrdrwdth > 50)
				$frmbrdrwdth = 50;
			elseif($frmbrdrwdth < 0)
				$frmbrdrwdth = 0;
			if($poppopcimeg < 0)
				$poppopcimeg = 0;
			else
				$poppopcimeg = round($poppopcimeg/100, 2);
			if($bkdrpopcimg < 0)
				$bkdrpopcimg = 0;
			else
				$bkdrpopcimg = round($bkdrpopcimg/100, 2);

			$hedrxtstyle = $eotpasettings["emailotpauthn_hedrxtstyle"];
			$inptxtstyle = $eotpasettings["emailotpauthn_inptxtstyle"];
			$butnxtstyle = $eotpasettings["emailotpauthn_butnxtstyle"];
			$bodyxtstyle = $eotpasettings["emailotpauthn_bodyxtstyle"];
			$popuphorzalign = $eotpasettings["emailotpauthn_popuphorzalign"];
			$popupboxmargin = $eotpasettings["emailotpauthn_popupboxmargin"];
			$frmnoticclr = $eotpasettings["emailotpauthn_frmnoticclr"];
			$frmnoticsiz = $eotpasettings["emailotpauthn_frmnoticsiz"];
			$frmnoticsiz = $frmnoticsiz/100;
			$setextrnlvars = array(
				"isoneinputbox"=>$isoneinputbox,
				"iscornrstyl"=>$iscornrstyl,
				"isshortbuttons"=>$isshortbuttons,
 				"poppboximeg"=>$poppboximeg,
				"poppopcimeg"=>$poppopcimeg,
				"backdrpimeg"=>$backdrpimeg,
				"bkdrpopcimg"=>$bkdrpopcimg,
				"hedrxtstyle"=>$hedrxtstyle,
				"headercolor"=>$headercolor,
				"inptxtstyle"=>$inptxtstyle,
				"inptbxcolor"=>$inptbxcolor,
				"butnxtstyle"=>$butnxtstyle,
				"buttoncolor"=>$buttoncolor,
				"bodyxtstyle"=>$bodyxtstyle,
				"bodybgcolor"=>$bodybgcolor,
				"poppbgcolor"=>$poppbgcolor,
				"poppboxblur"=>$poppboxblur,
				"popupbgblur"=>$popupbgblur,
				"frmbrdrwdth"=>$frmbrdrwdth,
				"frmbrdrcolr"=>$frmbrdrcolr,
				"popuphorzalign"=>$popuphorzalign,
				"popupboxmargin"=>$popupboxmargin,
				"frmnoticclr"=>$frmnoticclr,
				"frmnoticsiz"=>$frmnoticsiz,
				"extrnlsection"=>emailotpauthn_license_allowed(true)
				);
			$emailotpauthncss = emailotpauthn_popup_extrnl_css($setextrnlvars);

			$dfltextrnalcss = get_fix_option('emailotpauthn_dfltextrnalcss',"");
			$dfltextrnalcss = hex2bin($dfltextrnalcss);
			if(empty($dfltextrnalcss)){
				$dfltextrnalcss = $emailotpauthncss;
				$eotpa_separator = '#eotpafakeseparatorid';
				$eotpa_csspos = strpos($dfltextrnalcss, $eotpa_separator);
				if ($eotpa_csspos !== false) $dfltextrnalcss = substr($dfltextrnalcss, 0, $eotpa_csspos);
				update_option("emailotpauthn_dfltextrnalcss", bin2hex($dfltextrnalcss));
			}
            // register css
            wp_register_style("emailotpauthn-inlinecss", "");
            wp_enqueue_style("emailotpauthn-inlinecss");
            wp_add_inline_style("emailotpauthn-inlinecss", $emailotpauthncss);
        }
        /**
         * @hooks wp_footer
         * hook to add html into site footer
         */
        public function emailotpauthn_popup_html($SortCodeVars=0)
        {
			//check user logged or not
			if (emailotpauthn_is_ajaxapicron_call(3)) return;
			if(is_user_logged_in())
			{
				return;
			}

			global $extrnlcvrbl,$inline_used,$eotpasettings;
			if($inline_used) return;
			$logged_in_title = $extrnlcvrbl[6];
			$logged_in_email = '';
			$logged_in_readonly = '';
			$logged_in_boxtype = 'email';
			$logged_in_message = '';
			$swichtolgoutbtn = 0;

			$isoneinputbox = $eotpasettings["isoneinputbox"];

            $enableLogin = get_fix_option("emailotpauthn_enable",0);
			$rdrct2faurlout = '#';
			$formtype = "popup";
			if(is_array($SortCodeVars)) $formtype = "inline";
			$setextrnlvars = array(
				"formtype"=>$formtype,
				"dflt_action"=>"",
				"isoneinputbox"=>$isoneinputbox,
				"swichtolgoutbtn"=>$swichtolgoutbtn,
				"rdrct2faurlout"=>$rdrct2faurlout,
				"logged_in_title"=>$logged_in_title,
				"logged_in_boxtype"=>$logged_in_boxtype,
				"logged_in_email"=>$logged_in_email,
				"logged_in_readonly"=>$logged_in_readonly,
				"logged_in_message"=>$logged_in_message,
				"rgstrtermouurl"=>"",
				"header_submit"=>$extrnlcvrbl[2],
				"header_verify"=>$extrnlcvrbl[2],
				"extrnltxt_003"=>$extrnlcvrbl[3],
				"extrnltxt_004"=>$extrnlcvrbl[4],
				"extrnltxt_060"=>$extrnlcvrbl[60],
				"extrnltxt_061"=>$extrnlcvrbl[61],
				"extrnltxt_062"=>$extrnlcvrbl[62],
				"extrnltxt_063"=>$extrnlcvrbl[63],
				"extrnltxt_064"=>$extrnlcvrbl[64],
				"extrnltxt_066"=>$extrnlcvrbl[66],
				"extrnlsection"=>emailotpauthn_license_allowed(true)
			);

			$emailotpauthhtml = emailotpauthn_popup_extrnl_html($setextrnlvars);
			//logging_data($emailotpauthhtml);

			if(is_array($SortCodeVars)) return $emailotpauthhtml; else echo $emailotpauthhtml;

        }
        /*
         * Send OTP Email on User Email
         *
         * */
        public function emailotpauthn_send_otp($from_email, $useremail, $otp)
        {
			global $extrnlcvrbl;
            // send OTP over email
			$otp_subject = $extrnlcvrbl[9] . get_bloginfo("name");
            $otp_message = '<table width="100%" cellpadding="0" cellspacing="0" align="center" bgcolor="f5f5f5">
								 <tr>
									<td>
										<table width="650" align="center">
											<tr>
												<td>
													<p class="font_18 pd_lft_25">'.$extrnlcvrbl[10].'</p>
													<p class="font_17">'.$extrnlcvrbl[11].'<strong>' . $otp . '</strong></p>
													<p class="font_17">'.$extrnlcvrbl[12] . home_url() . '</p>
												</td>
											</tr>
										</table>

									<table  width="100%" height="40" bgcolor="c5c5c5"  align="center" cellpadding="0" cellspacing="0">
											<tr>
												<td valign="top" align="center">
												<p>This secure OTP is generated by : <span style="font-weight:bold;color:#000066;">Email OTP Authenticator</span></p>
												</td>
											</tr>
										</table>
										</td>
									</tr>
							</table>';

            $from = $from_email; //get_bloginfo("admin_email");

            $headers[] = "Content-Type: text/html; charset=UTF-8";

            //From
            if ($from != "") {
                $headers[] = "From:" . $from;
            }

			$otpmail = false;
            $otpmail = wp_mail( $useremail, $otp_subject, $otp_message, $headers );
            return $otpmail;
            die();
        }
        /*
         * Handle all login form request
         *
         * */
        public function emailotpauthn_login_action()
        {
            //global $wpdb;
            // check security
            //if (wp_doing_ajax()) { check_ajax_referer("emailotpauthn_filed_once_val", "emailotpauthnsecurity"); } // First check the nonce, if it fails the function will break
			$is_nonce_verified = check_ajax_referer("emailotpauthn_filed_once_val", "emailotpauthnsecurity");
			if($is_nonce_verified === false) wp_die();

			global $extrnlcvrbl,$eotpasettings;
			if(empty($extrnlcvrbl)) emailotpauthn_formtexts();
			$emailotpauthnzplussecurity = "invalid";
			if ( isset($_POST["emailotpauthnzplussecurity"]) ) $emailotpauthnzplussecurity = sanitize_text_field( $_POST["emailotpauthnzplussecurity"] );

			$is_autotriggered = false;
			$emailotpauthnsubmitidstr = isset($_POST["emailotpauthnsubmitid"]) ? sanitize_text_field($_POST["emailotpauthnsubmitid"]) : "";
			if ( (!empty($emailotpauthnsubmitidstr)) ) {
				if (strpos($emailotpauthnsubmitidstr, "autotriggered_") !== false) {
					$is_autotriggered = true;
					$emailotpauthnsubmitidstr = str_replace("autotriggered_","",$emailotpauthnsubmitidstr);
				}
				if( empty(get_transient("emailotpauthn_submitidstr_" . $emailotpauthnsubmitidstr)) ) {
					set_transient("emailotpauthn_submitidstr_" . $emailotpauthnsubmitidstr, $emailotpauthnsubmitidstr, 300);
				} else {
					return;
				}
			} else {
				return;
			}
            $email_otp = sanitize_text_field($_POST["email_otp"]);
			if(!isset($email_otp)) $email_otp = 0;
			if(empty($email_otp)) $email_otp = 0;
			$useremail = "";
			if(is_email( $_POST["email"] )){
				$useremail = sanitize_email($_POST["email"]);
			} else {
				$responsestr = json_encode([ "status" => 0, "message" => $extrnlcvrbl[15], "response" => $extrnlcvrbl[15], ]);
				call_emailotpauthn_external_function(0,$responsestr,"");
			}
			$ispre_autotriggered = get_transient("_eotpa_form_autotriggered_" . $useremail);
			if(empty($ispre_autotriggered)) $ispre_autotriggered = false;
			if($is_autotriggered && $email_otp == 0) {
				if($ispre_autotriggered){
					$responsestr = json_encode([ "status" => 0, "rspnum" => 37, "message" => $extrnlcvrbl[67], "response" => $extrnlcvrbl[67], ]);
					call_emailotpauthn_external_function(0,$responsestr,$useremail);
				} else {
					set_transient( "_eotpa_form_autotriggered_" . $useremail, 1, 60 );
				}
			}
            $validateotp = sanitize_text_field($_POST["validateotp"]);
			$ilogination = $eotpasettings["emailotpauthn_logination"];
			if ($ilogination == 0){
				$responsestr = json_encode([ "status" => 0, "rspnum" => 33, "message" => $extrnlcvrbl[18], "response" => $extrnlcvrbl[18], ]);
				call_emailotpauthn_external_function(0,$responsestr,$useremail);
			}
            // check zplus security
            if ($emailotpauthnzplussecurity != "") {
                $responsestr = json_encode([
					"status" => 0,
					"message" => $extrnlcvrbl[21],
					"response" => $extrnlcvrbl[22],
					]);
                call_emailotpauthn_external_function(0,$responsestr,$useremail);
            }
            // check is request for generate otp or submit otp
            if ($email_otp == 0) {
                // required fields
                if (empty($useremail)) {
                    $responsestr = json_encode([ "status" => 0, "message" => $extrnlcvrbl[23], "response" => $extrnlcvrbl[24], ]);
                    call_emailotpauthn_external_function(0,$responsestr,$useremail);
                }

				$isactvfrmmail = $eotpasettings["emailotpauthn_isactvfrmmail"];
				$sendfromcmail = $eotpasettings["emailotpauthn_sendfromcmail"];
				if($isactvfrmmail == 0 || empty($sendfromcmail)){
                    $responsestr = json_encode([ "status" => 0, "rspnum" => 34, "message" => $extrnlcvrbl[25], "response" => $extrnlcvrbl[25], ]);
					call_emailotpauthn_external_function(0,$responsestr,$useremail);
				}

                //check user already registered or not
                $user_id = email_exists($useremail);
                if (!$user_id && false == email_exists($useremail)) {
                    $responsestr = json_encode([ "status" => 0, "message" => "User does not exist.", "response" => "User does not exist", ]);
                    call_emailotpauthn_external_function(0,$responsestr,$useremail);
                } else {
                    //check type of login request
                    // send otp on email address
					$newotp = sprintf("%06d", random_int(1, 999999));
					$otpmail = $this->emailotpauthn_send_otp($sendfromcmail, $useremail, $newotp);
					if ($otpmail) {
						$encrypted_otp = password_hash($newotp, PASSWORD_DEFAULT);
						update_user_meta($user_id, "emilotpauthn", $encrypted_otp);
						update_user_meta($user_id, "emilotpatmpt", 0);
                        $json_arg["response"] = "Success";
                        $json_arg["message"] = $extrnlcvrbl[34];
                        $json_arg["status"] = 1;
                        $json_arg["sendotp"] = 1;
                    } else {
                        $json_arg["response"] = $extrnlcvrbl[29];
                        $json_arg["message"] = $extrnlcvrbl[29];
                        $json_arg["status"] = 0;
                        $json_arg["sendotp"] = 0;
                    }
                    $responsestr = json_encode($json_arg);
                    call_emailotpauthn_external_function($json_arg["status"],$responsestr,$useremail);
                }
            } else {
                // check otp valid or not				
                $user_id = email_exists($useremail);
				if(! $user_id) wp_die();
				$emilotpatmpt = get_user_meta($user_id, "emilotpatmpt", true);
				if (!is_numeric($emilotpatmpt)) $emilotpatmpt = 0;
				$db_otp = get_user_meta($user_id, "emilotpauthn", true);
				if(empty($db_otp)){
					$responsestr = json_encode([ "status" => 0, "message" => $extrnlcvrbl[35], "response" => $extrnlcvrbl[35], "sendotp" => 0, ]);
					call_emailotpauthn_external_function(0,$responsestr,$useremail);
				}
				$is_otp_verified = (password_verify($email_otp, $db_otp) && $validateotp != 0);
				if ($is_otp_verified && $emilotpatmpt < 999) {
					$logination = $eotpasettings["emailotpauthn_logination"];
					if($logination) {
						$user = get_user_by("email", $useremail);
						if ( is_wp_error( $user ) ) wp_die();
						wp_clear_auth_cookie();
						wp_set_current_user($user_id, $user->user_login);
						wp_set_auth_cookie($user_id);
						do_action("wp_login", $user->user_login, $user);
						if (is_user_logged_in()) {
							$redirecturl = $eotpasettings["emailotpauthn_redirectaftrlogin"];
							$url = $eotpasettings["emailotpauthn_redirect_url"];
							if(! $redirecturl) $url = "";
							$responsestr = json_encode([
								"status" => 1,
								"message" => $extrnlcvrbl[50],
								"response" => $extrnlcvrbl[43],
								"sendotp" => 0,
								"redirect" => $url,
							]);
							//update_user_meta($user_id, "emilotpauthn", ""); // reset otp
							//update_user_meta($user_id, "emilotpatmpt", 0);
							delete_user_meta($user_id, "emilotpauthn");
							delete_user_meta($user_id, "emilotpatmpt");
							call_emailotpauthn_external_function(1,$responsestr,$useremail);
						}
					}
					$url = ""; //(isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] === "on" ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
					$responsestr = json_encode([
						"status" => 1,
						"message" => "Not Logged In",
						"response" => "OTP Matched but not logged in",
						"sendotp" => 0,
						"redirect" => $url,
					]);
					call_emailotpauthn_external_function(1,$responsestr,$useremail);
				} else {
					if($emilotpatmpt < 999) {
						$emilotpatmpt++;
						$loginatmptlimit = $eotpasettings["emailotpauthn_rtryatmptlimit"];
						if ($loginatmptlimit > 0) {
							if ($emilotpatmpt >= $loginatmptlimit) {
								$emilotpatmpt = -1;
							} else {
								update_user_meta( $user_id, "emilotpatmpt", $emilotpatmpt );
							}
						}
					} else {
						$emilotpatmpt = -2;
					}
					if ($emilotpatmpt < 0) {
						if($emilotpatmpt > -2) update_user_meta( $user_id, "emilotpatmpt", 999 );
						$responsestr = json_encode([ "status" => 0, "message" => $extrnlcvrbl[54].' <input type="button" class="generateOtp" value="'.$extrnlcvrbl[27].'" name="resendotp" />', "response" => $extrnlcvrbl[54], "sendotp" => 0, ]);
						//update_user_meta($user_id, "emilotpauthn", "");
						//update_user_meta($user_id, "emilotpatmpt", 0);
						delete_user_meta($user_id, "emilotpauthn");
						delete_user_meta($user_id, "emilotpatmpt");
						call_emailotpauthn_external_function(0,$responsestr,$useremail);
					} else {
						$responsestr = json_encode([
							"status" => 0,
							"message" => $extrnlcvrbl[55].' <input type="button" class="generateOtp" value="'.$extrnlcvrbl[27].'" name="resendotp" />',
							"response" => $extrnlcvrbl[55],
							"sendotp" => 0,
						]);
						call_emailotpauthn_external_function(0,$responsestr,$useremail);
					}
				}
            }
        }
    }
}

function emailotpauthn_license_allowed($getnum=false){
	//"Lite";"Business";"Enterprise","Enterprise_Demo";
	if($getnum)	return 1; else return "Lite";
}
require_once("emailotpauthn-common-class.php");

function call_emailotpauthn_external_function($respstatus,$responsstr,$useremail=''){
	$email_otp = 0;
	if(isset($_POST["email_otp"])) $email_otp = sanitize_text_field($_POST["email_otp"]);
	if(!empty($email_otp) && !empty($useremail)) eotpa_remove_autotriggered($usermail);
	eotpa_echoOTPresponse($responsstr);
	if($respstatus==0) $useremail='';
	do_action('emailotpauthn_external_php_function',$respstatus,$useremail);
	/*
		// Use this funtion in your theme function.php file to hook any programe with successful/failure OTP authentication attempt.
		// Beware, this function will not be running on a different thread.
		// OTP authentication attempt will be finished after the completion of this function.
		// $respstatus = 0 for failure attempt and 1 for successful attempt.
		function emailotpauthn_external_php_function($respstatus) { ....... }
	*/
	wp_die();
}
function eotpa_echoOTPresponse($eotparesponse){
	ob_start();
	header('Content-Type: application/json; charset=utf-8');
	echo $eotparesponse;
	$eotparesponseout = ob_get_clean();
	echo $eotparesponseout;
	flush();
}

function emailotpauthn_actions_on_logout(){
	$loggingoutuserid = get_current_user_id();
    if (!$loggingoutuserid && !empty($_COOKIE[LOGGED_IN_COOKIE])) {
        $loggingoutuserid = wp_validate_auth_cookie($_COOKIE[LOGGED_IN_COOKIE], 'logged_in');
    }
	if(!empty($loggingoutuserid)){
		$ExistingLogoutUser = get_user_by("id", $loggingoutuserid);
		if(!empty($ExistingLogoutUser)) $ExistingLogoutUser = $ExistingLogoutUser->user_email;
		if(!empty($ExistingLogoutUser)) eotpa_remove_autotriggered($ExistingLogoutUser,0);
	}
}

//init class
if (class_exists("emailotpauthnFront")) {
    // instantiate the plugin class
    $emailotpauthnFront = new emailotpauthnFront();
}
