<?php
/**
 * This file contains Create, read, update and delete user operations on miniOrange idp.
 *
 * @package miniorange-2-factor-authentication/handler/twofa
 */

namespace TwoFA\Onprem;

use TwoFA\Cloud\Customer_Cloud_Setup;
use TwoFA\Onprem\MO2f_Cloud_Onprem_Interface;
use TwoFA\Helper\MoWpnsUtility;
use TwoFA\Onprem\Miniorange_Password_2Factor_Login;
use TwoFA\Traits\Instance;
use TwoFA\Helper\MoWpnsConstants;
use TwoFA\Handler\Miniorange_Mobile_Login;
use TwoFA\Database\Mo2fDB;
use TwoFA\Helper\MocURL;
use WP_Error;
use TwoFA\Cloud\Two_Factor_Setup;
use TwoFA\Cloud\Mo2f_Cloud_Utility;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Class Miniorange_Password_2factor_Login.
 */
require 'class-miniorange-password-2factor-login.php';

/**
 * Including two-fa-setup-notification.php.
 */
require dirname( dirname( dirname( __FILE__ ) ) ) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'twofa' . DIRECTORY_SEPARATOR . 'two-fa-setup-notification.php';

if ( ! class_exists( 'Miniorange_Authentication' ) ) {
	/**
	 * Class Miniorange_Authentication.
	 */
	class Miniorange_Authentication {

		use Instance;

		/**
		 * Default customer key
		 *
		 * @var string
		 */
		private $default_customer_key = '16555';

		/**
		 * Default api key
		 *
		 * @var string
		 */
		private $default_api_key = 'fFd2XcvTGDemZvbw1bcUesNJWEqKbbUq';

		/**
		 * Class Mo2f_Cloud_Onprem_Interface object
		 *
		 * @var object
		 */
		private $mo2f_onprem_cloud_obj;

		/**
		 * Constructor.
		 */
		public function __construct() {
			$this->mo2f_onprem_cloud_obj = MO2f_Cloud_Onprem_Interface::instance();
			add_action( 'admin_init', array( $this, 'mo2f_auth_save_settings' ) );
			add_action( 'plugins_loaded', array( $this, 'mo2f_update_db_check' ) );

			if ( (int) ( MoWpnsUtility::get_mo2f_db_option( 'mo2f_activate_plugin', 'get_option' ) ) === 1 ) {
				$pass2fa_login = new Miniorange_Password_2Factor_Login();
				add_action( 'init', array( $pass2fa_login, 'miniorange_pass2login_redirect' ) );
				// for shortcode addon.
				add_action( 'login_form', array( $pass2fa_login, 'mo_2_factor_pass2login_show_wp_login_form' ), 10 );
				add_filter( 'mo2f_shortcode_rba_gauth', array( $this->mo2f_onprem_cloud_obj, 'mo2f_validate_google_auth' ), 10, 3 );
				add_filter( 'mo2f_shortcode_kba', array( $this->mo2f_onprem_cloud_obj, 'mo2f_register_kba_details' ), 10, 7 );
				add_filter( 'mo2f_update_info', array( $this->mo2f_onprem_cloud_obj, 'mo2f_update_user_info' ), 10, 5 );
				add_action(
					'mo2f_shortcode_form_fields',
					array(
						$pass2fa_login,
						'miniorange_pass2login_form_fields',
					),
					10,
					5
				);

				add_action( 'delete_user', array( $this, 'mo2f_delete_user' ) );

				add_filter( 'mo2f_gauth_service', array( $this->mo2f_onprem_cloud_obj, 'mo2f_google_auth_service' ), 10, 1 );

				if ( get_option( 'mo_2factor_admin_registration_status' ) === 'MO_2_FACTOR_CUSTOMER_REGISTERED_SUCCESS' || MO2F_IS_ONPREM ) {
					remove_filter( 'authenticate', 'wp_authenticate_username_password', 20 );

					add_filter( 'authenticate', array( $pass2fa_login, 'mo2f_check_username_password' ), 99999, 4 );
					add_action( 'init', array( $pass2fa_login, 'miniorange_pass2login_redirect' ) );
					add_action(
						'login_form',
						array(
							$pass2fa_login,
							'mo_2_factor_pass2login_show_wp_login_form',
						),
						10
					);

					add_action(
						'login_enqueue_scripts',
						array(
							$pass2fa_login,
							'mo_2_factor_enable_jquery_default_login',
						)
					);

					if ( get_site_option( 'mo2f_woocommerce_login_prompt' ) ) {
						add_action(
							'woocommerce_login_form',
							array(
								$pass2fa_login,
								'mo_2_factor_pass2login_show_wp_login_form',
							)
						);
					} elseif ( ! get_site_option( 'mo2f_woocommerce_login_prompt' ) && MoWpnsUtility::get_mo2f_db_option( 'mo2f_enable_2fa_prompt_on_login_page', 'site_option' ) ) {
						add_action(
							'woocommerce_login_form_end',
							array(
								$pass2fa_login,
								'mo_2_factor_pass2login_woocommerce',
							)
						);
					}
					add_action(
						'wp_enqueue_scripts',
						array(
							$pass2fa_login,
							'mo_2_factor_enable_jquery_default_login',
						)
					);

					// Actions for other plugins to use miniOrange 2FA plugin.
					add_action(
						'miniorange_pre_authenticate_user_login',
						array(
							$pass2fa_login,
							'mo2f_check_username_password',
						),
						1,
						4
					);
					add_action(
						'miniorange_post_authenticate_user_login',
						array(
							$pass2fa_login,
							'miniorange_initiate_2nd_factor',
						),
						1,
						3
					);
					add_action(
						'miniorange_collect_attributes_for_authenticated_user',
						array(
							$pass2fa_login,
							'mo2f_collect_device_attributes_for_authenticated_user',
						),
						1,
						2
					);
				}
			}
		}


		/**
		 * Define globals.
		 *
		 * @return void
		 */
		public function mo2f_define_global() {

			global $mo2fdb_queries;
			$mo2fdb_queries = new Mo2fDB();
		}
		/**
		 * Delete user.
		 *
		 * @param int $user_id User id.
		 *
		 * @return void
		 */
		public function mo2f_delete_user( $user_id ) {

			global $mo2fdb_queries;
			delete_user_meta( $user_id, 'mo2f_kba_challenge' );
			delete_user_meta( $user_id, 'mo2f_2FA_method_to_configure' );
			delete_user_meta( $user_id, 'Security Questions' );
			delete_user_meta( $user_id, 'mo2f_chat_id' );
			$mo2fdb_queries->delete_user_details( $user_id );
			delete_user_meta( $user_id, 'mo2f_2FA_method_to_test' );
		}

		/**
		 * Update database check.
		 */
		public function mo2f_update_db_check() {

			$userid = wp_get_current_user()->ID;
			add_option( 'mo2f_onprem_admin', $userid );
			if ( is_multisite() ) {
				add_site_option( 'mo2fa_superadmin', 1 );
			}
			if ( get_option( 'mo2f_network_features', 'not_exits' ) === 'not_exits' ) {
				do_action( 'mo2f_network_create_db' );
				update_option( 'mo2f_network_features', 1 );
			}
			if ( get_option( 'mo2f_encryption_key', 'not_exits' ) === 'not_exits' ) {
				$get_encryption_key = MO2f_Utility::random_str( 16 );
				update_option( 'mo2f_encryption_key', $get_encryption_key );
			}
			global $mo2fdb_queries;
			$user_id            = get_option( 'mo2f_miniorange_admin' );
			$current_db_version = get_option( 'mo2f_dbversion' );

			if ( $current_db_version < MoWpnsConstants::DB_VERSION ) {
				update_option( 'mo2f_dbversion', MoWpnsConstants::DB_VERSION );
				$mo2fdb_queries->generate_tables();
			}
			if ( MO2F_IS_ONPREM ) {
				$twofactordb = new Mo2fDB();
				$user_sync   = get_site_option( 'mo2f_user_sync' );
				if ( $user_sync < 1 ) {
					update_site_option( 'mo2f_user_sync', 1 );
					$twofactordb->get_all_onprem_userids();
				}
			}

			if ( $user_id && ! get_option( 'mo2f_login_option_updated' ) ) {
				$does_table_exist = $mo2fdb_queries->check_if_table_exists();
				if ( $does_table_exist ) {
					$check_if_user_column_exists = $mo2fdb_queries->check_if_user_column_exists( $user_id );
					if ( $check_if_user_column_exists ) {
						$selected_2_f_a_method = $mo2fdb_queries->get_user_detail( 'mo2f_configured_2FA_method', $user_id );

						update_option( 'mo2f_login_option_updated', 1 );
					}
				}
			}
		}

		/**
		 * Save settings on miniOrange authetication.
		 */
		public function mo2f_auth_save_settings() {

			if ( get_site_option( 'mo2f_plugin_redirect' ) ) {
				delete_site_option( 'mo2f_plugin_redirect' );
				$redirect_to_finish = add_query_arg(
					array(
						'page'         => 'mo2f-setup-wizard',
						'current-step' => 'welcome',
					),
					admin_url( 'admin.php' )
				);
				wp_safe_redirect( esc_url_raw( $redirect_to_finish ) );
				exit();
			}
			if ( array_key_exists( 'page', $_REQUEST ) && sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ) === 'mo_2fa_two_fa' ) {
				if ( ! session_id() || session_id() === '' || ! isset( $_SESSION ) ) {
					if ( session_status() !== PHP_SESSION_DISABLED ) {
						session_start();
					}
				}
			}

			global $user;
			global $mo2fdb_queries;
			$default_customer_key = $this->default_customer_key;
			$default_api_key      = $this->default_api_key;

			$user    = wp_get_current_user();
			$user_id = $user->ID;

			if ( current_user_can( 'manage_options' ) ) {
				if ( strlen( get_option( 'mo2f_encryption_key' ) ) > 17 ) {
					$get_encryption_key = MO2f_Utility::random_str( 16 );
					update_option( 'mo2f_encryption_key', $get_encryption_key );
				}

				if ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_auth_deactivate_account' ) {
					$nonce = isset( $_POST['mo_auth_deactivate_account_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo_auth_deactivate_account_nonce'] ) ) : null;
					if ( ! wp_verify_nonce( $nonce, 'mo-auth-deactivate-account-nonce' ) ) {
						$error = new WP_Error();
						$error->add( 'empty_username', '<strong>' . esc_html__( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . esc_html__( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );

						return $error;
					} else {
						$url = admin_url( 'plugins.php' );
						wp_safe_redirect( $url );
						exit();
					}
				} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_auth_remove_account' ) {
					$nonce = isset( $_POST['mo_auth_remove_account_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo_auth_remove_account_nonce'] ) ) : null;
					if ( ! wp_verify_nonce( $nonce, 'mo-auth-remove-account-nonce' ) ) {
						$error = new WP_Error();
						$error->add( 'empty_username', '<strong>' . esc_html__( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . esc_html__( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );
						return $error;
					} else {
						update_option( 'mo2f_register_with_another_email', 1 );
						$this->mo2f_auth_deactivate();
					}
				} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_skiplogin' ) {
					$nonce = isset( $_POST['mo2f_skiplogin_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_skiplogin_nonce'] ) ) : null;
					if ( ! wp_verify_nonce( $nonce, 'miniorange-2-factor-skiplogin-failed-nonce' ) ) {
						$error = new WP_Error();
						$error->add( 'empty_username', '<strong>' . esc_html__( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . esc_html__( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );
						return $error;
					} else {
						update_option( 'mo2f_tour_started', 2 );
					}
				} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_userlogout' ) {
					$nonce = isset( $_POST['mo2f_userlogout_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_userlogout_nonce'] ) ) : null;
					if ( ! wp_verify_nonce( $nonce, 'miniorange-2-factor-userlogout-failed-nonce' ) ) {
						$error = new WP_Error();
						$error->add( 'empty_username', '<strong>' . esc_html__( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . esc_html__( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );
						return $error;
					} else {
						update_option( 'mo2f_tour_started', 2 );
						wp_logout();
						wp_safe_redirect( admin_url() );
						exit();
					}
				} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_save_proxy_settings' ) {
					$nonce = isset( $_POST['mo2f_save_proxy_settings_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_save_proxy_settings_nonce'] ) ) : null;
					if ( ! wp_verify_nonce( $nonce, 'mo2f-save-proxy-settings-nonce' ) ) {
						$error = new WP_Error();
						$error->add( 'empty_username', '<strong>' . esc_html__( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . esc_html__( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );
						return $error;
					} else {
						$proxy_host     = isset( $_POST['proxyHost'] ) ? sanitize_text_field( wp_unslash( $_POST['proxyHost'] ) ) : null;
						$port_number    = isset( $_POST['portNumber'] ) ? sanitize_text_field( wp_unslash( $_POST['portNumber'] ) ) : null;
						$proxy_username = isset( $_POST['proxyUsername'] ) ? sanitize_user( wp_unslash( $_POST['proxyUsername'] ) ) : null;
						$proxy_password = isset( $_POST['proxyPass'] ) ? $_POST['proxyPass'] : null; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Password should not be sanitized.
						update_option( 'mo2f_proxy_host', $proxy_host );
						update_option( 'mo2f_port_number', $port_number );
						update_option( 'mo2f_proxy_username', $proxy_username );
						update_option( 'mo2f_proxy_password', $proxy_password );
						update_option( 'mo2f_message', 'Proxy settings saved successfully.' );
						$this->mo2f_auth_show_success_message();
					}
				} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_auth_register_customer' ) {    // register the admin to miniOrange.
					// miniorange_register_customer_nonce.
					$nonce = isset( $_POST['miniorange_register_customer_nonce'] ) ? sanitize_key( wp_unslash( $_POST['miniorange_register_customer_nonce'] ) ) : null;
					if ( ! wp_verify_nonce( $nonce, 'miniorange-register-customer-nonce' ) ) {
						$error = new WP_Error();
						$error->add( 'empty_username', '<strong>' . esc_html__( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . esc_html__( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );

						return $error;
					} else {
						// validate and sanitize.
						$email            = '';
						$password         = '';
						$confirm_password = '';

						$email1            = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : '';
						$password1         = isset( $_POST['password'] ) ? $_POST['password'] : ''; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Password should not be sanitized.
						$confirm_password1 = isset( $_POST['confirmPassword'] ) ? $_POST['confirmPassword'] : ''; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Password should not be sanitized.
						if ( MO2f_Utility::mo2f_check_empty_or_null( $email1 ) || MO2f_Utility::mo2f_check_empty_or_null( $password1 ) || MO2f_Utility::mo2f_check_empty_or_null( $confirm_password1 ) ) {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_ENTRY' ) );

							return;
						} elseif ( strlen( $password ) < 6 || strlen( $confirm_password ) < 6 ) {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'MIN_PASS_LENGTH' ) );
						} else {
							$email            = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Password should not be sanitized.
							$password         = isset( $_POST['password'] ) ? $_POST['password'] : ''; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Password should not be sanitized.
							$confirm_password = isset( $_POST['confirmPassword'] ) ? $_POST['confirmPassword'] : ''; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Password should not be sanitized.

							$email = strtolower( $email );

							$pattern = '/^[(\w)*(\!\@\#\$\%\^\&\*\.\-\_)*]+$/';

							if ( preg_match( $pattern, $password ) ) {
								if ( strcmp( $password, $confirm_password ) === 0 ) {
									update_option( 'mo2f_email', $email );

									$mo2fdb_queries->insert_user( $user_id, array( 'user_id' => $user_id ) );
									update_option( 'mo2f_password', stripslashes( $password ) );
									$customer     = new MocURL();
									$customer_key = json_decode( $customer->check_customer( $email ), true );

									if ( strcasecmp( $customer_key['status'], 'CUSTOMER_NOT_FOUND' ) === 0 ) {
										if ( 'ERROR' === $customer_key['status'] ) {
											update_option( 'mo2f_message', Mo2fConstants::lang_translate( $customer_key['message'] ) );
										} else {
											$this->mo2f_create_customer( $user );
											delete_user_meta( $user->ID, 'mo_2fa_verify_otp_create_account' );
											delete_user_meta( $user->ID, 'register_account_popup' );
											if ( get_user_meta( $user->ID, 'mo2f_2FA_method_to_configure' ) ) {
												update_user_meta( $user->ID, 'mo2f_configure_2FA', 1 );
											}
										}
									} else { // customer already exists, redirect him to login page.
										update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ACCOUNT_ALREADY_EXISTS' ) );
										update_option( 'mo_2factor_user_registration_status', 'MO_2_FACTOR_VERIFY_CUSTOMER' );
									}
								} else {
									update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'PASSWORDS_MISMATCH' ) );
									$this->mo2f_auth_show_error_message();
								}
							} else {
								update_option( 'mo2f_message', 'Password length between 6 - 15 characters. Only following symbols (!@#.$%^&*-_) should be present.' );
								$this->mo2f_auth_show_error_message();
							}
						}
					}
				} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_2factor_phone_verification' ) { // at registration time.
					$phone = isset( $_POST['phone_number'] ) ? sanitize_text_field( wp_unslash( $_POST['phone_number'] ) ) : '';
					$mo2fdb_queries->update_user_details( $user->ID, array( 'mo2f_user_phone' => $phone ) );

					$phone     = str_replace( ' ', '', $phone );
					$auth_type = 'SMS';

					$send_otp_response = json_decode( $this->mo2f_onprem_cloud_obj->send_otp_token( $phone, $auth_type, $default_customer_key, $default_api_key ), true );

					if ( strcasecmp( $send_otp_response['status'], 'SUCCESS' ) === 0 ) {
						$mo_2factor_user_registration_status = 'MO_2_FACTOR_OTP_DELIVERED_SUCCESS';
						$mo2fdb_queries->update_user_details( $user->ID, array( 'mo_2factor_user_registration_status' => $mo_2factor_user_registration_status ) );
						update_user_meta( $user->ID, 'mo_2fa_verify_otp_create_account', $send_otp_response['txid'] );

						if ( get_user_meta( $user->ID, 'mo2f_sms_otp_count', true ) ) {
							update_option( 'mo2f_message', 'Another One Time Passcode has been sent <b>( ' . get_user_meta( $user->ID, 'mo2f_sms_otp_count', true ) . ' )</b> for verification to ' . $phone );
							update_user_meta( $user->ID, 'mo2f_sms_otp_count', get_user_meta( $user->ID, 'mo2f_sms_otp_count', true ) + 1 );
						} else {
							update_option( 'mo2f_message', 'One Time Passcode has been sent for verification to ' . $phone );
							update_user_meta( $user->ID, 'mo2f_sms_otp_count', 1 );
						}

						$this->mo2f_auth_show_success_message();
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_WHILE_SENDING_SMS' ) );
						$mo_2factor_user_registration_status = 'MO_2_FACTOR_OTP_DELIVERED_FAILURE';
						$mo2fdb_queries->update_user_details( $user->ID, array( 'mo_2factor_user_registration_status' => $mo_2factor_user_registration_status ) );
						$this->mo2f_auth_show_error_message();
					}
				} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_2factor_resend_otp' ) { // resend OTP over email for admin.
					$nonce = isset( $_POST['mo_2factor_resend_otp_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo_2factor_resend_otp_nonce'] ) ) : null;
					if ( ! wp_verify_nonce( $nonce, 'mo-2factor-resend-otp-nonce' ) ) {
						$error = new WP_Error();
						$error->add( 'empty_username', '<strong>' . esc_html__( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . esc_html__( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );

						return $error;
					} else {
						$content = json_decode( $this->mo2f_onprem_cloud_obj->send_otp_token( get_option( 'mo2f_email' ), 'EMAIL', $default_customer_key, $default_api_key ), true );
						if ( strcasecmp( $content['status'], 'SUCCESS' ) === 0 ) {
							if ( get_user_meta( $user->ID, 'mo2f_email_otp_count', true ) ) {
								update_user_meta( $user->ID, 'mo2f_email_otp_count', get_user_meta( $user->ID, 'mo2f_email_otp_count', true ) + 1 );
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'RESENT_OTP' ) . ' <b>( ' . get_user_meta( $user->ID, 'mo2f_email_otp_count', true ) . ' )</b> to <b>' . ( get_option( 'mo2f_email' ) ) . '</b> ' . Mo2fConstants::lang_translate( 'ENTER_OTP' ) );
							} else {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'OTP_SENT' ) . '<b> ' . ( get_option( 'mo2f_email' ) ) . ' </b>' . Mo2fConstants::lang_translate( 'ENTER_OTP' ) );
								update_user_meta( $user->ID, 'mo2f_email_otp_count', 1 );
							}
							$mo_2factor_user_registration_status = 'MO_2_FACTOR_OTP_DELIVERED_SUCCESS';
							$mo2fdb_queries->update_user_details( $user->ID, array( 'mo_2factor_user_registration_status' => $mo_2factor_user_registration_status ) );
							update_user_meta( $user->ID, 'mo_2fa_verify_otp_create_account', $content['txId'] );
							$this->mo2f_auth_show_success_message();
						} else {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_IN_SENDING_EMAIL' ) );
							$mo_2factor_user_registration_status = 'MO_2_FACTOR_OTP_DELIVERED_FAILURE';
							$mo2fdb_queries->update_user_details( $user->ID, array( 'mo_2factor_user_registration_status' => $mo_2factor_user_registration_status ) );
							$this->mo2f_auth_show_error_message();
						}
					}
				} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_dismiss_notice_option' ) {
					update_option( 'mo2f_bug_fix_done', 1 );
				} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_2factor_validate_otp' ) { // validate OTP over email for admin.
					$nonce = isset( $_POST['mo_2factor_validate_otp_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo_2factor_validate_otp_nonce'] ) ) : null;
					if ( ! wp_verify_nonce( $nonce, 'mo-2factor-validate-otp-nonce' ) ) {
						$error = new WP_Error();
						$error->add( 'empty_username', '<strong>' . esc_html__( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . esc_html__( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );

						return $error;
					} else {
						// validation and sanitization.
						$otp_token = isset( $_POST['otp_token'] ) ? sanitize_text_field( wp_unslash( $_POST['otp_token'] ) ) : '';
						if ( MO2f_Utility::mo2f_check_empty_or_null( $otp_token ) ) {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_ENTRY' ) );
							$this->mo2f_auth_show_error_message();

							return;
						} else {
							$otp_token = sanitize_text_field( wp_unslash( $_POST['otp_token'] ) );
						}

						$transaction_id = get_user_meta( $user->ID, 'mo_2fa_verify_otp_create_account', true );

						$content = json_decode( $this->mo2f_onprem_cloud_obj->validate_otp_token( 'EMAIL', null, $transaction_id, $otp_token, $default_customer_key, $default_api_key ), true );

						if ( 'ERROR' === $content['status'] ) {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( $content['message'] ) );
						} else {
							if ( strcasecmp( $content['status'], 'SUCCESS' ) === 0 ) { // OTP validated.
								$this->mo2f_create_customer( $user );
								delete_user_meta( $user->ID, 'mo_2fa_verify_otp_create_account' );
								delete_user_meta( $user->ID, 'register_account_popup' );
								update_user_meta( $user->ID, 'mo2f_configure_2FA', 1 );
							} else {  // OTP Validation failed.
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_OTP' ) );
								$mo2fdb_queries->update_user_details( $user->ID, array( 'mo_2factor_user_registration_status' => 'MO_2_FACTOR_OTP_DELIVERED_FAILURE' ) );
							}
						}
					}
				} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_2factor_validate_user_otp' ) { // validate OTP over email for additional admin.
					// validation and sanitization.
					$nonce = isset( $_POST['mo_2factor_validate_user_otp_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo_2factor_validate_user_otp_nonce'] ) ) : null;

					if ( ! wp_verify_nonce( $nonce, 'mo-2factor-validate-user-otp-nonce' ) ) {
						$error = new WP_Error();
						$error->add( 'empty_username', '<strong>' . esc_html__( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . esc_html__( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );

						return $error;
					} else {
						$otp_token = '';
						if ( MO2f_Utility::mo2f_check_empty_or_null( sanitize_text_field( wp_unslash( $_POST['otp_token'] ) ) ) ) {
							update_option( 'mo2f_message', 'All the fields are required. Please enter valid entries.' );
							$this->mo2f_auth_show_error_message();

							return;
						} else {
							$otp_token = sanitize_text_field( wp_unslash( $_POST['otp_token'] ) );
						}

						$user_email = get_user_meta( $user->ID, 'user_email', true );

						$mo2f_transaction_id = isset( $_SESSION['mo2f_transactionId'] ) && ! empty( $_SESSION['mo2f_transactionId'] ) ? $_SESSION['mo2f_transactionId'] : get_option( 'mo2f_transactionId' );

						$content = json_decode( $this->mo2f_onprem_cloud_obj->validate_otp_token( 'EMAIL', '', $mo2f_transaction_id, $otp_token, get_option( 'mo2f_customerKey' ), get_option( 'mo2f_api_key' ) ), true );

						if ( 'ERROR' === $content['status'] ) {
							update_option( 'mo2f_message', $content['message'] );
							$this->mo2f_auth_show_error_message();
						} else {
							if ( strcasecmp( $content['status'], 'SUCCESS' ) === 0 ) { // OTP validated and generate QRCode.
								$this->mo2f_create_user( $user, $user_email );
								delete_user_meta( $user->ID, 'mo_2fa_verify_otp_create_account' );
							} else {
								update_option( 'mo2f_message', 'Invalid OTP. Please try again.' );
								$mo2fdb_queries->update_user_details( $user->ID, array( 'mo_2factor_user_registration_status' => 'MO_2_FACTOR_OTP_DELIVERED_FAILURE' ) );
								$this->mo2f_auth_show_error_message();
							}
						}
					}
				} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_2factor_send_query' ) { // Help me or support.
					$nonce = isset( $_POST['mo_2factor_send_query_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo_2factor_send_query_nonce'] ) ) : null;
					if ( ! wp_verify_nonce( $nonce, 'mo-2factor-send-query-nonce' ) ) {
						$error = new WP_Error();
						$error->add( 'empty_username', '<strong>' . esc_html__( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . esc_html__( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );

						return $error;
					} else {
						$query  = '';
						$email1 = isset( $_POST['EMAIL_MANDATORY'] ) ? sanitize_email( wp_unslash( $_POST['EMAIL_MANDATORY'] ) ) : '';
						$query1 = isset( $_POST['query'] ) ? sanitize_email( wp_unslash( $_POST['query'] ) ) : '';
						if ( MO2f_Utility::mo2f_check_empty_or_null( $email1 ) || MO2f_Utility::mo2f_check_empty_or_null( $query1 ) ) {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'EMAIL_MANDATORY' ) );
							$this->mo2f_auth_show_error_message();
							return;
						} else {
							$query      = isset( $_POST['query'] ) ? sanitize_text_field( wp_unslash( $_POST['query'] ) ) : '';
							$email      = isset( $_POST['EMAIL_MANDATORY'] ) ? sanitize_email( wp_unslash( $_POST['EMAIL_MANDATORY'] ) ) : '';
							$phone      = isset( $_POST['query_phone'] ) ? sanitize_text_field( wp_unslash( $_POST['query_phone'] ) ) : '';
							$contact_us = new MocURL();
							$submited   = json_decode( $contact_us->submit_contact_us( $email, $phone, $query ), true );
							if ( json_last_error() === JSON_ERROR_NONE ) {
								if ( is_array( $submited ) && array_key_exists( 'status', $submited ) && 'ERROR' === $submited['status'] ) {
									update_option( 'mo2f_message', Mo2fConstants::lang_translate( $submited['message'] ) );
									$this->mo2f_auth_show_error_message();
								} else {
									if ( false === $submited ) {
										update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_WHILE_SUBMITTING_QUERY' ) );
										$this->mo2f_auth_show_error_message();
									} else {
										update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'QUERY_SUBMITTED_SUCCESSFULLY' ) );
										$this->mo2f_auth_show_success_message();
									}
								}
							}
						}
					}
				} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'woocommerce_disable_login_prompt' ) {
					if ( isset( $_POST['woocommerce_login_prompt'] ) ) {
						update_site_option( 'mo2f_woocommerce_login_prompt', true );
					} else {
						update_site_option( 'mo2f_woocommerce_login_prompt', false );
					}
				} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_auth_advanced_options_save' ) {
					update_option( 'mo2f_message', 'Your settings are saved successfully.' );
					$this->mo2f_auth_show_success_message();
				} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_auth_sync_sms_transactions' ) {
					$customer = new Two_Factor_Setup_Onprem_Cloud();
					$content  = json_decode( $customer->get_customer_transactions( get_option( 'mo2f_customerKey' ), get_option( 'mo2f_api_key' ), get_site_option( 'mo2f_license_type' ) ), true );
					if ( ! array_key_exists( 'smsRemaining', $content ) ) {
						$sms_remaining = 0;
					} else {
						$sms_remaining = $content['smsRemaining'];
						if ( null === $sms_remaining ) {
							$sms_remaining = 0;
						}
					}
					update_option( 'mo2f_number_of_transactions', $sms_remaining );
				}
			}

			if ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_fix_database_error' ) {
				$nonce = isset( $_POST['mo2f_fix_database_error_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_fix_database_error_nonce'] ) ) : null;
				if ( ! wp_verify_nonce( $nonce, 'mo2f-fix-database-error-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					global $mo2fdb_queries;

					$mo2fdb_queries->database_table_issue();
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_registration_closed' ) {
				$nonce = isset( $_POST['mo2f_registration_closed_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_registration_closed_nonce'] ) ) : null;
				if ( ! wp_verify_nonce( $nonce, 'mo2f-registration-closed-nonce' ) ) {
					$error = new WP_Error();
					$error->add( 'empty_username', '<strong>' . esc_html__( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . esc_html__( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );
					return $error;
				} else {
					delete_user_meta( $user->ID, 'register_account_popup' );
					$mo2f_message = 'Please set up the second-factor by clicking on Configure button.';
					update_option( 'mo2f_message', $mo2f_message );
					$this->mo2f_auth_show_success_message();
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_goto_verifycustomer' ) {

				$nonce = isset( $_POST['mo2f_general_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_general_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'miniOrange_2fa_nonce' ) ) {
					$error = new WP_Error();
					$error->add( 'empty_username', '<strong>' . esc_html__( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . esc_html__( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );
					return $error;
				} else {
					$mo2fdb_queries->insert_user( $user_id, array( 'user_id' => $user_id ) );
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ENTER_YOUR_EMAIL_PASSWORD' ) );
					update_option( 'mo_2factor_user_registration_status', 'MO_2_FACTOR_VERIFY_CUSTOMER' );
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_2factor_gobackto_registration_page' ) { // back to registration page for admin.
				$nonce = isset( $_POST['mo_2factor_gobackto_registration_page_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo_2factor_gobackto_registration_page_nonce'] ) ) : null;
				if ( ! wp_verify_nonce( $nonce, 'mo-2factor-gobackto-registration-page-nonce' ) ) {
					$error = new WP_Error();
					$error->add( 'empty_username', '<strong>' . esc_html__( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . esc_html__( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );
					return $error;
				} else {
					delete_option( 'mo2f_email' );
					delete_option( 'mo2f_password' );
					update_option( 'mo2f_message', '' );

					MO2f_Utility::unset_session_variables( 'mo2f_transactionId' );
					delete_option( 'mo2f_transactionId' );
					delete_user_meta( $user->ID, 'mo2f_sms_otp_count' );
					delete_user_meta( $user->ID, 'mo2f_email_otp_count' );
					delete_user_meta( $user->ID, 'mo2f_email_otp_count' );
					$mo2fdb_queries->update_user_details( $user->ID, array( 'mo_2factor_user_registration_status' => 'REGISTRATION_STARTED' ) );
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_skip_feedback' ) {
				$nonce = isset( $_POST['mo2f_skip_feedback_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_skip_feedback_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-skip-feedback-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					deactivate_plugins( '/miniorange-2-factor-authentication/miniorange-2-factor-settings.php' );
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_2factor_resend_user_otp' ) { // resend OTP over email for additional admin and non-admin user.
				$nonce = isset( $_POST['mo_2factor_resend_user_otp_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo_2factor_resend_user_otp_nonce'] ) ) : null;
				if ( ! wp_verify_nonce( $nonce, 'mo-2factor-resend-user-otp-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					$content = json_decode( $this->mo2f_onprem_cloud_obj->send_otp_token( get_user_meta( $user->ID, 'user_email', true ), 'EMAIL', get_option( 'mo2f_customerKey' ), get_option( 'mo2f_api_key' ) ), true );
					if ( strcasecmp( $content['status'], 'SUCCESS' ) === 0 ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'OTP_SENT' ) . ' <b>' . ( get_user_meta( $user->ID, 'user_email', true ) ) . '</b>. ' . Mo2fConstants::lang_translate( 'ENTER_OTP' ) );
						update_user_meta( $user->ID, 'mo_2fa_verify_otp_create_account', $content['txId'] );
						$mo_2factor_user_registration_status = 'MO_2_FACTOR_OTP_DELIVERED_SUCCESS';
						$mo2fdb_queries->update_user_details( $user->ID, array( 'mo_2factor_user_registration_status' => $mo_2factor_user_registration_status ) );
						$this->mo2f_auth_show_success_message();
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_IN_SENDING_EMAIL' ) );
						$mo_2factor_user_registration_status = 'MO_2_FACTOR_OTP_DELIVERED_FAILURE';
						$mo2fdb_queries->update_user_details( $user->ID, array( 'mo_2factor_user_registration_status' => $mo_2factor_user_registration_status ) );
						$this->mo2f_auth_show_error_message();
					}
				}
			} elseif ( isset( $_POST['option'] ) && ( sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_configure_miniorange_authenticator_validate' || sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_auth_mobile_reconfiguration_complete' ) ) { // mobile registration successfully complete for all users.
				delete_option( 'mo2f_transactionId' );
				$session_variables = array( 'mo2f_qrCode', 'mo2f_transactionId', 'mo2f_show_qr_code' );
				MO2f_Utility::unset_session_variables( $session_variables );

				$email                       = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );
				$two_f_a_method_to_configure = isset( $_POST['mo2f_method'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_method'] ) ) : '';
				$current_method              = MO2f_Utility::mo2f_decode_2_factor( $two_f_a_method_to_configure, 'server' );
				$response                    = json_decode( $this->mo2f_onprem_cloud_obj->mo2f_update_user_info( $user->ID, true, $current_method, MoWpnsConstants::SUCCESS_RESPONSE, MoWpnsConstants::MO_2_FACTOR_PLUGIN_SETTINGS, true, $email, null ), true );

				if ( json_last_error() === JSON_ERROR_NONE ) { /* Generate Qr code */
					if ( 'ERROR' === $response['status'] ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( $response['message'] ) );

						$this->mo2f_auth_show_error_message();
					} elseif ( MoWpnsConstants::SUCCESS_RESPONSE === $response['status'] ) {
						$selected_method = $two_f_a_method_to_configure;

						delete_user_meta( $user->ID, 'mo2f_2FA_method_to_configure' );

						delete_user_meta( $user->ID, 'mo2f_configure_2FA' );
						mo2f_display_test_2fa_notification( $user );
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_PROCESS' ) );
						$this->mo2f_auth_show_error_message();
					}
				} else {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_REQ' ) );
					$this->mo2f_auth_show_error_message();
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_mobile_authenticate_success' ) { // mobile registration for all users(common).
				if ( current_user_can( 'manage_options' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'COMPLETED_TEST' ) );
				} else {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'COMPLETED_TEST' ) );
				}

				$session_variables = array( 'mo2f_qrCode', 'mo2f_transactionId', 'mo2f_show_qr_code' );
				MO2f_Utility::unset_session_variables( $session_variables );

				delete_user_meta( $user->ID, 'test_2FA' );
				$this->mo2f_auth_show_success_message();
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_mobile_authenticate_error' ) { // mobile registration failed for all users(common).
				$nonce = isset( $_POST['mo2f_mobile_authenticate_error_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_mobile_authenticate_error_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-mobile-authenticate-error-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'AUTHENTICATION_FAILED' ) );
					MO2f_Utility::unset_session_variables( 'mo2f_show_qr_code' );
					$this->mo2f_auth_show_error_message();
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_auth_setting_configuration' ) {
				$mo2fdb_queries->update_user_details( $user->ID, array( 'mo_2factor_user_registration_status' => MoWpnsConstants::MO_2_FACTOR_PLUGIN_SETTINGS ) );
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_auth_refresh_mobile_qrcode' ) { // refrsh Qrcode for all users.
				$session_id             = isset( $_POST['mo2f_session_id'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_session_id'] ) ) : null;
				$twofactor_transactions = new Mo2fDB();
				$exceeded               = $twofactor_transactions->check_alluser_limit_exceeded( $user_id );
				if ( $exceeded ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'USER_LIMIT_EXCEEDED' ) );
					$this->mo2f_auth_show_error_message();
					return;
				}
				$mo_2factor_user_registration_status = get_option( 'mo_2factor_user_registration_status' );
				if ( in_array(
					$mo_2factor_user_registration_status,
					array(
						'MO_2_FACTOR_INITIALIZE_TWO_FACTOR',
						'MO_2_FACTOR_INITIALIZE_MOBILE_REGISTRATION',
						'MO_2_FACTOR_PLUGIN_SETTINGS',
					),
					true
				) ) {
					$email = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );
					$this->mo2f_get_qr_code_for_mobile( $email, $user->ID, $session_id );
				} else {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'REGISTER_WITH_MO' ) );
					$this->mo2f_auth_show_error_message();
				}
			} elseif ( isset( $_POST['miniorange_get_started'] ) && isset( $_POST['miniorange_user_reg_nonce'] ) ) { // registration with miniOrange for additional admin and non-admin.
				$nonce = isset( $_POST['miniorange_user_reg_nonce'] ) ? sanitize_key( wp_unslash( $_POST['miniorange_user_reg_nonce'] ) ) : null;
				$mo2fdb_queries->insert_user( $user_id, array( 'user_id' => $user_id ) );
				if ( ! wp_verify_nonce( $nonce, 'miniorange-2-factor-user-reg-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_REQ' ) );
				} else {
					$email  = '';
					$email1 = isset( $_POST['mo_useremail'] ) ? sanitize_email( wp_unslash( $_POST['mo_useremail'] ) ) : '';
					if ( MO2f_Utility::mo2f_check_empty_or_null( $email1 ) ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ENTER_EMAILID' ) );

						return;
					} else {
						$email = sanitize_email( wp_unslash( $_POST['mo_useremail'] ) );
					}

					if ( $mo2fdb_queries->check_if_email_is_already_registered( $email ) ) {
						update_user_meta( $user->ID, 'user_email', $email );

						$enduser    = new Two_Factor_Setup_Onprem_Cloud();
						$check_user = json_decode( $enduser->mo_check_user_already_exist( $email ), true );

						if ( json_last_error() === JSON_ERROR_NONE ) {
							if ( 'ERROR' === $check_user['status'] ) {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( $check_user['message'] ) );
								$this->mo2f_auth_show_error_message();

								return;
							} elseif ( strcasecmp( $check_user['status'], 'USER_FOUND_UNDER_DIFFERENT_CUSTOMER' ) === 0 ) {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'EMAIL_IN_USE' ) );
								$this->mo2f_auth_show_error_message();

								return;
							} elseif ( strcasecmp( $check_user['status'], 'USER_FOUND' ) === 0 || strcasecmp( $check_user['status'], 'USER_NOT_FOUND' ) === 0 ) {
								$content = json_decode( $this->mo2f_onprem_cloud_obj->send_otp_token( $email, 'EMAIL', get_option( 'mo2f_customerKey' ), get_option( 'mo2f_api_key' ) ), true );
								if ( strcasecmp( $content['status'], 'SUCCESS' ) === 0 ) {
									update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'OTP_SENT' ) . ' <b>' . ( $email ) . '</b>. ' . Mo2fConstants::lang_translate( 'ENTER_OTP' ) );
									$_SESSION['mo2f_transactionId'] = $content['txId'];
									update_option( 'mo2f_transactionId', $content['txId'] );
									$mo_2factor_user_registration_status = 'MO_2_FACTOR_OTP_DELIVERED_SUCCESS';
									$mo2fdb_queries->update_user_details( $user->ID, array( 'mo_2factor_user_registration_status' => $mo_2factor_user_registration_status ) );
									update_user_meta( $user->ID, 'mo_2fa_verify_otp_create_account', $content['txId'] );
									$this->mo2f_auth_show_success_message();
								} else {
									$mo_2factor_user_registration_status = 'MO_2_FACTOR_OTP_DELIVERED_FAILURE';
									$mo2fdb_queries->update_user_details( $user->ID, array( 'mo_2factor_user_registration_status' => $mo_2factor_user_registration_status ) );
									update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_IN_SENDING_OTP_OVER_EMAIL' ) );
									$this->mo2f_auth_show_error_message();
								}
							}
						}
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'EMAIL_IN_USE' ) );
						$this->mo2f_auth_show_error_message();
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_2factor_backto_user_registration' ) { // back to registration page for additional admin and non-admin.
				delete_user_meta( $user->ID, 'user_email' );
				$mo2fdb_queries->delete_user_details( $user->ID );
				MO2f_Utility::unset_session_variables( 'mo2f_transactionId' );
				delete_option( 'mo2f_transactionId' );
			} elseif ( isset( $_POST['option'] ) && 'mo2f_validate_soft_token' === $_POST['option'] ) {  // validate Soft Token during test for all users.
				$otp_token  = '';
				$otp_token1 = isset( $_POST['otp_token'] ) ? sanitize_text_field( wp_unslash( $_POST['otp_token'] ) ) : '';
				if ( MO2f_Utility::mo2f_check_empty_or_null( $otp_token1 ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ENTER_VALUE' ) );
					$this->mo2f_auth_show_error_message();

					return;
				} else {
					$otp_token = sanitize_text_field( wp_unslash( $_POST['otp_token'] ) );
				}
				$email    = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );
				$customer = new MocURL();
				$content  = json_decode( $customer->miniorange_authenticator_validate( 'SOFT TOKEN', $email, $otp_token, get_option( 'mo2f_customerKey' ) ), true );
				if ( 'ERROR' === $content['status'] ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( $content['message'] ) );
					$this->mo2f_auth_show_error_message();
				} else {
					if ( strcasecmp( $content['status'], 'SUCCESS' ) === 0 ) { // OTP validated and generate QRCode.
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'COMPLETED_TEST' ) );

						delete_user_meta( $user->ID, 'test_2FA' );
						$this->mo2f_auth_show_success_message();
					} else {  // OTP Validation failed.
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_OTP' ) );
						$this->mo2f_auth_show_error_message();
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_validate_otp_over_Telegram' ) { // validate otp over Telegram.
				$nonce = isset( $_POST['mo2f_test_validate_otp_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_test_validate_otp_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-validate-otp-over-Telegram-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					$otp       = isset( $_POST['otp_token'] ) ? sanitize_text_field( wp_unslash( $_POST['otp_token'] ) ) : '';
					$otp_token = get_user_meta( $user->ID, 'mo2f_otp_token', true );

					$time          = get_user_meta( $user->ID, 'mo2f_telegram_time', true );
					$accepted_time = time() - 300;
					$time          = (int) $time;
					global $mo2fdb_queries;
					if ( (int) ( $otp_token ) === (int) $otp ) {
						if ( $accepted_time < $time ) {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'COMPLETED_TEST' ) );
							delete_user_meta( $user->ID, 'test_2FA' );
							delete_user_meta( $user->ID, 'mo2f_telegram_time' );

							$this->mo2f_auth_show_success_message();
						} else {
							update_option( 'mo2f_message', 'OTP has been expired please initiate another transaction for verification' );
							delete_user_meta( $user->ID, 'test_2FA' );
							$this->mo2f_auth_show_error_message();
						}
					} else {
						update_option( 'mo2f_message', 'Wrong OTP Please try again.' );
						$this->mo2f_auth_show_error_message();
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_validate_otp_over_sms' ) { // validate otp over sms and phone call during test for all users.
				$nonce = isset( $_POST['mo2f_test_validate_otp_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_test_validate_otp_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-validate-otp-over-sms-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					$otp_token  = '';
					$otp_token1 = isset( $_POST['otp_token'] ) ? sanitize_text_field( wp_unslash( $_POST['otp_token'] ) ) : '';
					if ( MO2f_Utility::mo2f_check_empty_or_null( $otp_token1 ) ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ENTER_VALUE' ) );
						$this->mo2f_auth_show_error_message();

						return;
					} else {
						$otp_token = isset( $_POST['otp_token'] ) ? sanitize_text_field( wp_unslash( $_POST['otp_token'] ) ) : '';
					}
					$mo2f_transaction_id       = get_user_meta( $user->ID, 'mo2f_transactionId', true );
					$email                     = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );
					$selected_2_2factor_method = $mo2fdb_queries->get_user_detail( 'mo2f_configured_2FA_method', $user->ID );
					$content                   = json_decode( $this->mo2f_onprem_cloud_obj->validate_otp_token( $selected_2_2factor_method, $email, $mo2f_transaction_id, $otp_token, get_option( 'mo2f_customerKey' ), get_option( 'mo2f_api_key' ) ), true );

					if ( 'ERROR' === $content['status'] ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( $content['message'] ) );
						$this->mo2f_auth_show_error_message();
					} else {
						if ( strcasecmp( $content['status'], 'SUCCESS' ) === 0 ) { // OTP validated.
							if ( current_user_can( 'manage_options' ) ) {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'COMPLETED_TEST' ) );
							} else {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'COMPLETED_TEST' ) );
							}

							delete_user_meta( $user->ID, 'test_2FA' );
							$this->mo2f_auth_show_success_message();
						} else {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_OTP' ) );
							$this->mo2f_auth_show_error_message();
						}
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_out_of_band_success' ) {
				$nonce = isset( $_POST['mo2f_out_of_band_success_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_out_of_band_success_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-out-of-band-success-nonce' ) ) {

					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {

					$show = 1;
					if ( MO2F_IS_ONPREM ) {

						$txid   = isset( $_POST['TxidEmail'] ) ? sanitize_text_field( wp_unslash( $_POST['TxidEmail'] ) ) : null;
						$status = get_site_option( $txid );
						if ( ! empty( $status ) ) {

							if ( 1 !== (int) $status ) {

								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_EMAIL_VER_REQ' ) );
								delete_user_meta( $user->ID, 'test_2FA' );
								delete_user_meta( $user->ID, 'mo2f_configure_2FA' );
								$show = 0;
								$this->mo2f_auth_show_error_message();
							}
						}
					}
					$mo2f_configured_2_f_a_method = $mo2fdb_queries->get_user_detail( 'mo2f_configured_2FA_method', $user->ID );
					if ( MO2F_IS_ONPREM && isset( $_POST['TxidEmail'] ) ) {
						$mo2f_configured_2_f_a_method = 'Email Verification';
					}

					$mo2f_email_verification_config_status = $mo2fdb_queries->get_user_detail( 'mo2f_EmailVerification_config_status', $user->ID );
					if ( ! current_user_can( 'manage_options' ) && 'OUT OF BAND EMAIL' === $mo2f_configured_2_f_a_method ) {

						if ( $mo2f_email_verification_config_status ) {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'COMPLETED_TEST' ) );
						} else {
							$email = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );
							$this->mo2f_onprem_cloud_obj->mo2f_update_user_info( $user->ID, true, $mo2f_configured_2_f_a_method, null, null, null, $email );

							update_option( 'mo2f_message', '<b> ' . Mo2fConstants::lang_translate( 'EMAIL_VERFI' ) . '</b> ' . Mo2fConstants::lang_translate( 'SET_AS_2ND_FACTOR' ) );
						}
					} else {

						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'Your email has been verified successfully.' ) );
					}
					$email      = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );
					$temp_email = $email ? $email : get_user_meta( $user->ID, 'tempEmail', true );
					delete_user_meta( $user->ID, 'mo2f_configure_2FA' );
					delete_user_meta( $user->ID, 'test_2FA' );
					$response = json_decode( $this->mo2f_onprem_cloud_obj->mo2f_update_user_info( $user->ID, true, $mo2f_configured_2_f_a_method, null, MoWpnsConstants::MO_2_FACTOR_PLUGIN_SETTINGS, true, $temp_email ), true );

					if ( $show ) {
						$this->mo2f_auth_show_success_message();
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_out_of_band_error' ) { // push and out of band email denied.
				$nonce = isset( $_POST['mo2f_out_of_band_error_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_out_of_band_error_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-out-of-band-error-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'DENIED_REQUEST' ) );
					delete_user_meta( $user->ID, 'mo2f_configure_2FA' );
					$temp_email = get_user_meta( $user->ID, 'tempEmail', true );
					delete_user_meta( $user->ID, 'test_2FA' );
					$this->mo2f_auth_show_error_message();
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_duo_authenticator_success_form' ) {
				$nonce = isset( $_POST['mo2f_duo_authenticator_success_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_duo_authenticator_success_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-duo-authenticator-success-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'COMPLETED_TEST' ) );

					delete_user_meta( $user->ID, 'test_2FA' );
					$mo2fdb_queries->update_user_details(
						$user->ID,
						array(
							'mo_2factor_user_registration_status' => MoWpnsConstants::MO_2_FACTOR_PLUGIN_SETTINGS,
							'mo2f_DuoAuthenticator_config_status' => true,
						)
					);

					$this->mo2f_auth_show_success_message();
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_duo_authenticator_error' ) { // push and out of band email denied.
				$nonce = isset( $_POST['mo2f_duo_authentcator_error_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_duo_authentcator_error_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-duo-authenticator-error-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					global  $mo2fdb_queries;

					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'DENIED_DUO_REQUEST' ) );
					delete_user_meta( $user->ID, 'test_2FA' );
					$mo2fdb_queries->update_user_details(
						$user->ID,
						array(
							'mobile_registration_status' => false,
						)
					);
					$this->mo2f_auth_show_error_message();
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_validate_google_authy_test' ) {
				$nonce = isset( $_POST['mo2f_test_validate_otp_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_test_validate_otp_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-validate-google-authy-test-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					$otp_token  = '';
					$otp_token1 = isset( $_POST['otp_token'] ) ? sanitize_text_field( wp_unslash( $_POST['otp_token'] ) ) : '';
					if ( MO2f_Utility::mo2f_check_empty_or_null( $otp_token1 ) ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ENTER_VALUE' ) );
						$this->mo2f_auth_show_error_message();
						return;
					} else {
						$otp_token = sanitize_text_field( wp_unslash( $_POST['otp_token'] ) );
					}
					$email = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );

					$content = json_decode( $this->mo2f_onprem_cloud_obj->validate_otp_token( MoWpnsConstants::GOOGLE_AUTHENTICATOR_UC, $email, null, $otp_token, get_option( 'mo2f_customerKey' ), get_option( 'mo2f_api_key' ) ), true );

					if ( json_last_error() === JSON_ERROR_NONE ) {
						if ( strcasecmp( $content['status'], 'SUCCESS' ) === 0 ) { // Google OTP validated.
							if ( current_user_can( 'manage_options' ) ) {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'COMPLETED_TEST' ) );
							} else {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'COMPLETED_TEST' ) );
							}

							delete_user_meta( $user->ID, 'test_2FA' );
							$this->mo2f_auth_show_success_message();
						} else {  // OTP Validation failed.
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_OTP' ) );
							$this->mo2f_auth_show_error_message();
						}
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_WHILE_VALIDATING_OTP' ) );
						$this->mo2f_auth_show_error_message();
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_validate_otp_over_email' ) {
				$nonce = isset( $_POST['mo2f_test_validate_otp_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_test_validate_otp_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-validate-otp-over-email-test-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					$otp_token  = '';
					$otp_token1 = isset( $_POST['otp_token'] ) ? sanitize_text_field( wp_unslash( $_POST['otp_token'] ) ) : '';
					if ( MO2f_Utility::mo2f_check_empty_or_null( $otp_token1 ) ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ENTER_VALUE' ) );
						$this->mo2f_auth_show_error_message();

						return;
					} else {
						$otp_token = sanitize_text_field( wp_unslash( $_POST['otp_token'] ) );
					}
					$email = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );

					$mo2f_transaction_id = get_user_meta( $user->ID, 'mo2f_transactionId', true );
					$content             = json_decode( $this->mo2f_onprem_cloud_obj->validate_otp_token( MoWpnsConstants::OTP_OVER_EMAIL, $email, $mo2f_transaction_id, $otp_token, get_option( 'mo2f_customerKey' ), get_option( 'mo2f_api_key' ) ), true );
					if ( json_last_error() === JSON_ERROR_NONE ) {
						if ( strcasecmp( $content['status'], 'SUCCESS' ) === 0 ) { // Google OTP validated.
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'COMPLETED_TEST' ) );
							delete_user_meta( $user->ID, 'mo2f_configure_2FA' );
							$mo2fdb_queries->update_user_details(
								$user->ID,
								array(
									'mo2f_configured_2FA_method' => 'OTP Over Email',
									'mo2f_OTPOverEmail_config_status' => true,
								)
							);
							delete_user_meta( $user->ID, 'test_2FA' );
							$this->mo2f_auth_show_success_message();
						} else {  // OTP Validation failed.
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_OTP' ) );
							$this->mo2f_auth_show_error_message();
						}
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_WHILE_VALIDATING_OTP' ) );
						$this->mo2f_auth_show_error_message();
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_google_appname' ) {
				$nonce = isset( $_POST['mo2f_google_appname_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_google_appname_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-google-appname-nonce' ) ) {
					$error = new WP_Error();
					$error->add( 'empty_username', '<strong>' . esc_html__( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . esc_html__( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );
					return $error;
				} else {
					update_option( 'mo2f_google_appname', ( ( isset( $_POST['mo2f_google_auth_appname'] ) && ! empty( $_POST['mo2f_google_auth_appname'] ) ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_google_auth_appname'] ) ) : DEFAULT_GOOGLE_APPNAME ) );
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_configure_google_authenticator_validate' ) {
				$nonce = isset( $_POST['mo2f_configure_google_authenticator_validate_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_configure_google_authenticator_validate_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-configure-google-authenticator-validate-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					$otp_token = isset( $_POST['google_token'] ) ? sanitize_text_field( wp_unslash( $_POST['google_token'] ) ) : null;
					$ga_secret = isset( $_POST['google_auth_secret'] ) ? sanitize_text_field( wp_unslash( $_POST['google_auth_secret'] ) ) : null;

					if ( MO2f_Utility::mo2f_check_number_length( $otp_token ) ) {
						$email                  = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );
						$user                   = wp_get_current_user();
						$email                  = ( empty( $email ) ) ? $user->user_email : $email;
						$twofactor_transactions = new Mo2fDB();
						$exceeded               = $twofactor_transactions->check_alluser_limit_exceeded( $user_id );

						if ( $exceeded ) {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'USER_LIMIT_EXCEEDED' ) );
							$this->mo2f_auth_show_error_message();
							return;
						}
						$google_response = json_decode( $this->mo2f_onprem_cloud_obj->mo2f_validate_google_auth( $email, $otp_token, $ga_secret ), true );
						if ( json_last_error() === JSON_ERROR_NONE ) {
							if ( MoWpnsConstants::SUCCESS_RESPONSE === $google_response['status'] ) {
								$response = json_decode( $this->mo2f_onprem_cloud_obj->mo2f_update_user_info( $user->ID, true, MoWpnsConstants::GOOGLE_AUTHENTICATOR, MoWpnsConstants::SUCCESS_RESPONSE, MoWpnsConstants::MO_2_FACTOR_PLUGIN_SETTINGS, true, $email, null ), true );

								if ( json_last_error() === JSON_ERROR_NONE ) {
									if ( MoWpnsConstants::SUCCESS_RESPONSE === $response['status'] ) {
										delete_user_meta( $user->ID, 'mo2f_2FA_method_to_configure' );

										delete_user_meta( $user->ID, 'mo2f_configure_2FA' );

										update_user_meta( $user->ID, 'mo2f_external_app_type', 'Google Authenticator' );
										mo2f_display_test_2fa_notification( $user );
										delete_user_meta( $user->ID, 'mo2f_google_auth' );
									} else {
										update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_PROCESS' ) );
										$this->mo2f_auth_show_error_message();
									}
								} else {
									update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_PROCESS' ) );
									$this->mo2f_auth_show_error_message();
								}
							} else {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_IN_SENDING_OTP_CAUSES' ) . '<br>1. ' . Mo2fConstants::lang_translate( 'INVALID_OTP' ) . '<br>2. ' . Mo2fConstants::lang_translate( 'APP_TIME_SYNC' ) . '<br>3.' . Mo2fConstants::lang_translate( 'SERVER_TIME_SYNC' ) );
								$this->mo2f_auth_show_error_message();
							}
						} else {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_WHILE_VALIDATING_USER' ) );
							$this->mo2f_auth_show_error_message();
						}
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ONLY_DIGITS_ALLOWED' ) );
						$this->mo2f_auth_show_error_message();
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_configure_duo_authenticator_validate_nonce' ) {
				$nonce = isset( $_POST['mo2f_configure_duo_authenticator_validate_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_configure_duo_authenticator_validate_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-configure-duo-authenticator-validate-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					delete_user_meta( $user->ID, 'mo2f_2FA_method_to_configure' );

					delete_user_meta( $user->ID, 'mo2f_configure_2FA' );
					delete_user_meta( $user->ID, 'user_not_enroll' );

					$update_details = new Miniorange_Password_2Factor_Login();
					$update_details->mo2fa_update_user_details( $user->ID, true, 'Duo Authenticator', MoWpnsConstants::SUCCESS_RESPONSE, MoWpnsConstants::MO_2_FACTOR_PLUGIN_SETTINGS, 1 );

					update_user_meta( $user->ID, 'mo2f_external_app_type', 'Duo Authenticator' );
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'VALIDATE_DUO' ) );
					$this->mo2f_auth_show_success_message();
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_configure_authy_authenticator' ) {
				$nonce = isset( $_POST['mo2f_configure_authy_authenticator_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_configure_authy_authenticator_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-configure-authy-authenticator-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					$authy          = new Mo2f_Cloud_Utility();
					$user_email     = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );
					$authy_response = json_decode( $authy->mo2f_google_auth_service( $user_email ), true );
					if ( json_last_error() === JSON_ERROR_NONE ) {
						if ( MoWpnsConstants::SUCCESS_RESPONSE === $authy_response['status'] ) {
							$mo2f_authy_keys                      = array();
							$mo2f_authy_keys['authy_qrCode']      = $authy_response['qrCodeData'];
							$mo2f_authy_keys['mo2f_authy_secret'] = $authy_response['secret'];
							$_SESSION['mo2f_authy_keys']          = $mo2f_authy_keys;
						} else {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_USER_REGISTRATION' ) );
							$this->mo2f_auth_show_error_message();
						}
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_USER_REGISTRATION' ) );
						$this->mo2f_auth_show_error_message();
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_configure_authy_authenticator_validate' ) {
				$nonce = isset( $_POST['mo2f_configure_authy_authenticator_validate_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_configure_authy_authenticator_validate_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-configure-authy-authenticator-validate-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					$otp_token    = isset( $_POST['mo2f_authy_token'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_authy_token'] ) ) : null;
					$authy_secret = isset( $_POST['mo2f_authy_secret'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_authy_secret'] ) ) : null;
					if ( MO2f_Utility::mo2f_check_number_length( $otp_token ) ) {
						$email          = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );
						$authy_response = json_decode( $this->mo2f_onprem_cloud_obj->mo2f_validate_google_auth( $email, $otp_token, $authy_secret ), true );
						if ( json_last_error() === JSON_ERROR_NONE ) {
							if ( MoWpnsConstants::SUCCESS_RESPONSE === $authy_response['status'] ) {
								$response = json_decode( $this->mo2f_onprem_cloud_obj->mo2f_update_user_info( $user->ID, true, MoWpnsConstants::AUTHY_AUTHENTICATOR, MoWpnsConstants::SUCCESS_RESPONSE, MoWpnsConstants::MO_2_FACTOR_PLUGIN_SETTINGS, true, $email, null ), true );

								if ( json_last_error() === JSON_ERROR_NONE ) {
									if ( MoWpnsConstants::SUCCESS_RESPONSE === $response['status'] ) {
										$mo2fdb_queries->update_user_details(
											$user->ID,
											array(
												'mo2f_GoogleAuthenticator_config_status' => false,
											)
										);
										update_user_meta( $user->ID, 'mo2f_external_app_type', 'Authy Authenticator' );
										delete_user_meta( $user->ID, 'mo2f_2FA_method_to_configure' );
										delete_user_meta( $user->ID, 'mo2f_configure_2FA' );

										mo2f_display_test_2fa_notification( $user );
									} else {
										update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_PROCESS' ) );
										$this->mo2f_auth_show_error_message();
									}
								} else {
									update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_PROCESS' ) );
									$this->mo2f_auth_show_error_message();
								}
							} else {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_IN_SENDING_OTP_CAUSES' ) . '<br>1. ' . Mo2fConstants::lang_translate( 'INVALID_OTP' ) . '<br>2. ' . Mo2fConstants::lang_translate( 'APP_TIME_SYNC' ) );
								$this->mo2f_auth_show_error_message();
							}
						} else {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_WHILE_VALIDATING_USER' ) );
							$this->mo2f_auth_show_error_message();
						}
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ONLY_DIGITS_ALLOWED' ) );
						$this->mo2f_auth_show_error_message();
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_save_kba' ) {
				$nonce = isset( $_POST['mo2f_save_kba_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_save_kba_nonce'] ) ) : null;
				if ( ! wp_verify_nonce( $nonce, 'mo2f-save-kba-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				}
				$twofactor_transactions = new Mo2fDB();
				$exceeded               = $twofactor_transactions->check_alluser_limit_exceeded( $user_id );
				if ( $exceeded ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'USER_LIMIT_EXCEEDED' ) );
					$this->mo2f_auth_show_error_message();
					return;
				}
				$kba_ques_ans_obj = new Miniorange_Password_2Factor_Login();
				$kba_ques_ans     = $kba_ques_ans_obj->mo2f_get_kba_details( $_POST );
				if ( MO2f_Utility::mo2f_check_empty_or_null( $kba_ques_ans['kba_q1'] ) || MO2f_Utility::mo2f_check_empty_or_null( $kba_ques_ans['kba_a1'] ) || MO2f_Utility::mo2f_check_empty_or_null( $kba_ques_ans['kba_q2'] ) || MO2f_Utility::mo2f_check_empty_or_null( $kba_ques_ans['kba_a2'] ) || MO2f_Utility::mo2f_check_empty_or_null( $kba_ques_ans['kba_q3'] ) || MO2f_Utility::mo2f_check_empty_or_null( $kba_ques_ans['kba_a3'] ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_ENTRY' ) );
					$this->mo2f_auth_show_error_message();
					return;
				}

				if ( 0 === strcasecmp( $kba_ques_ans['kba_q1'], $kba_ques_ans['kba_q2'] ) || 0 === strcasecmp( $kba_ques_ans['kba_q2'], $kba_ques_ans['kba_q3'] ) || 0 === strcasecmp( $kba_ques_ans['kba_q3'], $kba_ques_ans['kba_q1'] ) ) {
					update_option( 'mo2f_message', 'The questions you select must be unique.' );
					$this->mo2f_auth_show_error_message();
					return;
				}
				$email           = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );
				$email           = ( empty( $email ) ) ? $user->user_email : $email;
				$kba_reg_reponse = json_decode( $this->mo2f_onprem_cloud_obj->mo2f_register_kba_details( $email, $kba_ques_ans['kba_q1'], $kba_ques_ans['kba_a1'], $kba_ques_ans['kba_q2'], $kba_ques_ans['kba_a2'], $kba_ques_ans['kba_q3'], $kba_ques_ans['kba_a3'], $user->ID ), true );
				if ( json_last_error() === JSON_ERROR_NONE ) {
					if ( MoWpnsConstants::SUCCESS_RESPONSE === $kba_reg_reponse['status'] ) {
						if ( isset( $_POST['mobile_kba_option'] ) && sanitize_text_field( wp_unslash( $_POST['mobile_kba_option'] ) ) === 'mo2f_request_for_kba_as_emailbackup' ) {
							MO2f_Utility::unset_session_variables( 'mo2f_mobile_support' );

							delete_user_meta( $user->ID, 'mo2f_configure_2FA' );
							delete_user_meta( $user->ID, 'mo2f_2FA_method_to_configure' );

							$message = esc_html__( 'Your KBA as alternate 2 factor is configured successfully.', 'miniorange-2-factor-authentication' );
							update_option( 'mo2f_message', $message );
							$this->mo2f_auth_show_success_message();
						} else {
							$response = json_decode( $this->mo2f_onprem_cloud_obj->mo2f_update_user_info( $user->ID, true, MoWpnsConstants::SECURITY_QUESTIONS, MoWpnsConstants::SUCCESS_RESPONSE, MoWpnsConstants::MO_2_FACTOR_PLUGIN_SETTINGS, true, $email, null ), true );

							if ( json_last_error() === JSON_ERROR_NONE ) {
								if ( 'ERROR' === $response['status'] ) {
									update_option( 'mo2f_message', Mo2fConstants::lang_translate( $response['message'] ) );
									$this->mo2f_auth_show_error_message();
								} elseif ( MoWpnsConstants::SUCCESS_RESPONSE === $response['status'] ) {
									delete_user_meta( $user->ID, 'mo2f_configure_2FA' );
									mo2f_display_test_2fa_notification( $user );
								} else {
									update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_PROCESS' ) );
									$this->mo2f_auth_show_error_message();
								}
							} else {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_REQ' ) );
								$this->mo2f_auth_show_error_message();
							}
						}
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_WHILE_SAVING_KBA' ) );
						$this->mo2f_auth_show_error_message();

						return;
					}
				} else {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_WHILE_SAVING_KBA' ) );
					$this->mo2f_auth_show_error_message();

					return;
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_validate_kba_details' ) {
				$nonce = isset( $_POST['mo2f_authenticate_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_authenticate_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'miniorange-2-factor-soft-token-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					$kba_ans_1 = '';
					$kba_ans_2 = '';
					if ( MO2f_Utility::mo2f_check_empty_or_null( isset( $_POST['mo2f_answer_1'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_answer_1'] ) ) : null ) || MO2f_Utility::mo2f_check_empty_or_null( isset( $_POST['mo2f_answer_2'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_answer_2'] ) ) : null ) ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_ENTRY' ) );
						$this->mo2f_auth_show_error_message();

						return;
					} else {
						$kba_ans_1 = sanitize_text_field( wp_unslash( $_POST['mo2f_answer_1'] ) );
						$kba_ans_2 = sanitize_text_field( wp_unslash( $_POST['mo2f_answer_2'] ) );
					}
					// if the php session folder has insufficient permissions, temporary options to be used.
					$kba_questions = get_user_meta( $user->ID, 'mo_2_factor_kba_questions', true );

					$kba_ans = array();
					if ( ! MO2F_IS_ONPREM ) {
						$kba_ans[0] = $kba_questions[0]['question'];
						$kba_ans[1] = $kba_ans_1;
						$kba_ans[2] = $kba_questions[1]['question'];
						$kba_ans[3] = $kba_ans_2;
					}
					// if the php session folder has insufficient permissions, temporary options to be used.
					$mo2f_transaction_id   = get_option( 'mo2f_transactionId' );
					$kba_validate_response = json_decode( $this->mo2f_onprem_cloud_obj->validate_otp_token( 'KBA', null, $mo2f_transaction_id, $kba_ans, get_option( 'mo2f_customerKey' ), get_option( 'mo2f_api_key' ) ), true );
					if ( json_last_error() === JSON_ERROR_NONE ) {
						if ( strcasecmp( $kba_validate_response['status'], 'SUCCESS' ) === 0 ) {
							delete_option( 'mo2f_transactionId' );
							delete_option( 'kba_questions' );
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'COMPLETED_TEST' ) );
							delete_user_meta( $user->ID, 'test_2FA' );
							$this->mo2f_auth_show_success_message();
						} else {  // KBA Validation failed.
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_ANSWERS' ) );
							$this->mo2f_auth_show_error_message();
						}
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_configure_otp_over_sms_send_otp' ) { // sendin otp for configuring OTP over SMS.
				$nonce = isset( $_POST['mo2f_configure_otp_over_sms_send_otp_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_configure_otp_over_sms_send_otp_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-configure-otp-over-sms-send-otp-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					$phone = isset( $_POST['phone'] ) ? sanitize_text_field( wp_unslash( $_POST['phone'] ) ) : '';

					if ( MO2f_Utility::mo2f_check_empty_or_null( $phone ) ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_ENTRY' ) );
						$this->mo2f_auth_show_error_message();

						return;
					}

					$phone              = str_replace( ' ', '', $phone );
					$session_id_encrypt = isset( $_POST['mo2f_session_id'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_session_id'] ) ) : null;

					MO2f_Utility::mo2f_set_transient( $session_id_encrypt, 'user_phone', $phone );

					update_option( 'user_phone_temp', $phone );
					$current_method = 'SMS';

					$content = json_decode( $this->mo2f_onprem_cloud_obj->send_otp_token( $phone, $current_method, get_option( 'mo2f_customerKey' ), get_option( 'mo2f_api_key' ) ), true );

					if ( json_last_error() === JSON_ERROR_NONE ) { /* Generate otp token */
						if ( 'ERROR' === $content['status'] ) {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( $content['message'] ) );
							$this->mo2f_auth_show_error_message();
						} elseif ( MoWpnsConstants::SUCCESS_RESPONSE === $content['status'] ) {
							MO2f_Utility::mo2f_set_transient( $session_id_encrypt, 'mo2f_transactionId', $content['txId'] );

							update_option( 'mo2f_transactionId', $content['txId'] );
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'OTP_SENT' ) . ' ' . $phone . ' .' . Mo2fConstants::lang_translate( 'ENTER_OTP' ) );
							update_option( 'mo2f_number_of_transactions', MoWpnsUtility::get_mo2f_db_option( 'mo2f_number_of_transactions', 'get_option' ) - 1 );
							$mo2f_sms = get_site_option( 'cmVtYWluaW5nT1RQVHJhbnNhY3Rpb25z' );
							if ( $mo2f_sms > 0 ) {
								update_site_option( 'cmVtYWluaW5nT1RQVHJhbnNhY3Rpb25z', $mo2f_sms - 1 );
							}

							$this->mo2f_auth_show_success_message();
						} else {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( $content['message'] ) );
							$this->mo2f_auth_show_error_message();
						}
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_REQ' ) );
						$this->mo2f_auth_show_error_message();
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_configure_otp_over_sms_validate' && isset( $_POST['mo2f_configure_otp_over_sms_validate_nonce'] ) ) {
				$nonce = isset( $_POST['mo2f_configure_otp_over_sms_validate_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_configure_otp_over_sms_validate_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-configure-otp-over-sms-validate-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					$twofactor_transactions = new Mo2fDB();
					$exceeded               = $twofactor_transactions->check_alluser_limit_exceeded( $user_id );

					if ( $exceeded ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'USER_LIMIT_EXCEEDED' ) );
						$this->mo2f_auth_show_error_message();
						return;
					}
					$otp_token  = '';
					$otp_token1 = isset( $_POST['otp_token'] ) ? sanitize_text_field( wp_unslash( $_POST['otp_token'] ) ) : '';
					if ( MO2f_Utility::mo2f_check_empty_or_null( $otp_token1 ) ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_ENTRY' ) );
						$this->mo2f_auth_show_error_message();

						return;
					} else {
						$otp_token          = isset( $_POST['otp_token'] ) ? sanitize_text_field( wp_unslash( $_POST['otp_token'] ) ) : '';
						$session_id_encrypt = isset( $_POST['mo2f_session_id'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_session_id'] ) ) : null;
					}
					$mo2f_transaction_id = MO2f_Utility::mo2f_get_transient( $session_id_encrypt, 'mo2f_transactionId' );

					$user_phone                   = MO2f_Utility::mo2f_get_transient( $session_id_encrypt, 'user_phone' );
					$mo2f_configured_2_f_a_method = get_user_meta( $user->ID, 'mo2f_2FA_method_to_configure', true );
					$phone                        = $mo2fdb_queries->get_user_detail( 'mo2f_user_phone', $user->ID );
					$content                      = json_decode( $this->mo2f_onprem_cloud_obj->validate_otp_token( $mo2f_configured_2_f_a_method, null, $mo2f_transaction_id, $otp_token, get_option( 'mo2f_customerKey' ), get_option( 'mo2f_api_key' ) ), true );

					if ( 'ERROR' === $content['status'] ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( $content['message'] ) );
						$this->mo2f_auth_show_error_message();
					} elseif ( strcasecmp( $content['status'], 'SUCCESS' ) === 0 ) { // OTP validated.
						if ( $phone && strlen( $phone ) >= 4 ) {
							if ( $user_phone !== $phone ) {
								$mo2fdb_queries->update_user_details( $user->ID, array( 'mobile_registration_status' => false ) );
							}
						}
						$email = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );

						$two_f_a_method_to_configure = get_user_meta( $user->ID, 'mo2f_2FA_method_to_configure', true );
						$current_method              = MO2f_Utility::mo2f_decode_2_factor( $two_f_a_method_to_configure, 'server' );
						$response                    = array();
						if ( MO2F_IS_ONPREM ) {
							$response['status'] = MoWpnsConstants::SUCCESS_RESPONSE;
							if ( 'SMS' === $current_method ) {
								$mo2fdb_queries->update_user_details( $user->ID, array( 'mo2f_configured_2FA_method' => 'OTP Over SMS' ) );
							} else {
								$mo2fdb_queries->update_user_details( $user->ID, array( 'mo2f_configured_2FA_method' => $current_method ) ); // why is this needed?
							}
						} else {
							$response = json_decode( $this->mo2f_onprem_cloud_obj->mo2f_update_user_info( $user->ID, true, $current_method, MoWpnsConstants::SUCCESS_RESPONSE, MoWpnsConstants::MO_2_FACTOR_PLUGIN_SETTINGS, true, $email, $user_phone ), true );
						}

						if ( json_last_error() === JSON_ERROR_NONE ) {
							if ( 'ERROR' === $response['status'] ) {
								MO2f_Utility::unset_session_variables( 'user_phone' );
								delete_option( 'user_phone_temp' );

								update_option( 'mo2f_message', Mo2fConstants::lang_translate( $response['message'] ) );
								$this->mo2f_auth_show_error_message();
							} elseif ( MoWpnsConstants::SUCCESS_RESPONSE === $response['status'] ) {
								delete_user_meta( $user->ID, 'mo2f_configure_2FA' );
								delete_user_meta( $user->ID, 'mo2f_2FA_method_to_configure' );

								MO2f_Utility::unset_session_variables( 'user_phone' );
								delete_option( 'user_phone_temp' );

								mo2f_display_test_2fa_notification( $user );
							} else {
								MO2f_Utility::unset_session_variables( 'user_phone' );
								delete_option( 'user_phone_temp' );
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_PROCESS' ) );
								$this->mo2f_auth_show_error_message();
							}
						} else {
							MO2f_Utility::unset_session_variables( 'user_phone' );
							delete_option( 'user_phone_temp' );
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_REQ' ) );
							$this->mo2f_auth_show_error_message();
						}
					} else {  // OTP Validation failed.
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_OTP' ) );
						$this->mo2f_auth_show_error_message();
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_2factor_test_prompt_cross' ) {
				$nonce = isset( $_POST['mo2f_2factor_test_prompt_cross_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_2factor_test_prompt_cross_nonce'] ) ) : null;
				if ( ! wp_verify_nonce( $nonce, 'mo2f-2factor-test-prompt-cross-nonce' ) ) {
					update_user_meta( $user->ID, 'mo2f_otp_send_true', true );
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				}
				mo2f_display_test_2fa_notification( $user );
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_configure_duo_authenticator' ) {
				$nonce = isset( $_POST['mo2f_configure_duo_authenticator_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_configure_duo_authenticator_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-configure-duo-authenticator' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					if ( isset( $_POST['ikey'] ) && sanitize_key( $_POST['ikey'] ) === '' || isset( $_POST['skey'] ) && sanitize_key( $_POST['skey'] ) === '' || empty( $_POST['apihostname'] ) && esc_url_raw( wp_unslash( $_POST['apihostname'] ) ) === '' ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'Some field is missing, please fill all required details.' ) );
						$this->mo2f_auth_show_error_message();
						return;
					} else {
						update_site_option( 'mo2f_d_integration_key', isset( $_POST['ikey'] ) ? sanitize_key( $_POST['ikey'] ) : '' );
						update_site_option( 'mo2f_d_secret_key', isset( $_POST['skey'] ) ? sanitize_key( $_POST['skey'] ) : '' );
						update_site_option( 'mo2f_d_api_hostname', isset( $_POST['apihostname'] ) ? esc_url_raw( wp_unslash( $_POST['apihostname'] ) ) : '' );

						$ikey = isset( $_POST['ikey'] ) ? sanitize_key( wp_unslash( $_POST['ikey'] ) ) : '';
						$skey = isset( $_POST['skey'] ) ? sanitize_key( wp_unslash( $_POST['skey'] ) ) : '';
						$host = isset( $_POST['apihostname'] ) ? esc_url_raw( wp_unslash( $_POST['apihostname'] ) ) : '';

						include_once dirname( dirname( dirname( __FILE__ ) ) ) . DIRECTORY_SEPARATOR . 'handler' . DIRECTORY_SEPARATOR . 'twofa' . DIRECTORY_SEPARATOR . 'two-fa-duo-handler.php';

						$duo_up_response = ping( $skey, $ikey, $host );

						if ( 'OK' === $duo_up_response['response']['stat'] ) {
							$duo_check_credentials = check( $skey, $ikey, $host );

							if ( 'OK' !== $duo_check_credentials['response']['stat'] ) {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'Not the valid credential, please enter valid keys' ) );
								$this->mo2f_auth_show_error_message();
								return;
							}
						} else {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'Duo server is not responding right now, please try after some time' ) );
							$this->mo2f_auth_show_error_message();
							return;
						}
						update_site_option( 'duo_credentials_save_successfully', 1 );
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'Setting saved successfully.' ) );
						$this->mo2f_auth_show_success_message();
						return;
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_configure_duo_authenticator_abc' ) {
				$nonce = isset( $_POST['mo2f_configure_duo_authenticator_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_configure_duo_authenticator_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-configure-duo-authenticator-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					include_once dirname( dirname( dirname( __FILE__ ) ) ) . DIRECTORY_SEPARATOR . 'handler' . DIRECTORY_SEPARATOR . 'twofa' . DIRECTORY_SEPARATOR . 'two-fa-duo-handler.php';
					$ikey = get_site_option( 'mo2f_d_integration_key' );
					$skey = get_site_option( 'mo2f_d_secret_key' );
					$host = get_site_option( 'mo2f_d_api_hostname' );

					$user_email = $user->user_email;

					$duo_preauth = preauth( $user_email, true, $skey, $ikey, $host );

					if ( 'OK' === $duo_preauth['response']['stat'] ) {
						if ( isset( $duo_preauth['response']['response']['status_msg'] ) && 'Account is active' === $duo_preauth['response']['response']['status_msg'] ) {
							update_user_meta( $user->ID, 'user_not_enroll', true );
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'This user is already available on duo, please send push notification to setup push notification as two factor.' ) );
							$this->mo2f_auth_show_success_message();
							return;
						} elseif ( isset( $duo_preauth['response']['response']['enroll_portal_url'] ) ) {
							$duo_enroll_url = $duo_preauth['response']['response']['enroll_portal_url'];
							update_user_meta( $user->ID, 'user_not_enroll_on_duo_before', $duo_enroll_url );
							update_user_meta( $user->ID, 'user_not_enroll', true );
						} else {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'Your account is inactive from duo side, please contact to your administrator.' ) );
							$this->mo2f_auth_show_error_message();
							return;
						}
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'Invalid or missing parameters, or a user with this name already exists.' ) );
						$this->mo2f_auth_show_error_message();
						return;
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'duo_mobile_send_push_notification_inside_plugin' ) {
				$nonce = isset( $_POST['duo_mobile_send_push_notification_inside_plugin_nonce'] ) ? sanitize_key( wp_unslash( $_POST['duo_mobile_send_push_notification_inside_plugin_nonce'] ) ) : null;

				if ( ! isset( $_POST['duo_mobile_send_push_notification_inside_plugin_nonce'] ) || ! wp_verify_nonce( $nonce, 'mo2f-send-duo-push-notification-inside-plugin-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				}
			} elseif ( ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_save_free_plan_auth_methods' ) ) { // user clicks on Set 2-Factor method.
				$nonce = isset( $_POST['miniorange_save_form_auth_methods_nonce'] ) ? sanitize_key( wp_unslash( $_POST['miniorange_save_form_auth_methods_nonce'] ) ) : null;
				if ( ! wp_verify_nonce( $nonce, 'miniorange-save-form-auth-methods-nonce' ) ) {
					$error = new WP_Error();
					$error->add( 'empty_username', '<strong>' . esc_html__( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . esc_html__( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );
					return $error;
				} else {
					$configured_method = isset( $_POST['mo2f_configured_2FA_method_free_plan'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_configured_2FA_method_free_plan'] ) ) : '';
					$cloud_methods     = array( 'OTPOverSMS', 'miniOrangeQRCodeAuthentication', 'miniOrangePushNotification', 'miniOrangeSoftToken' );

					if ( 'OTPOverSMS' === $configured_method ) {
						$configured_method = 'OTP Over SMS';
					}

					// limit exceed check.
					$exceeded = $mo2fdb_queries->check_alluser_limit_exceeded( $user_id );

					if ( $exceeded ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'USER_LIMIT_EXCEEDED' ) );
						$this->mo2f_auth_show_error_message();
						return;
					}
					$selected_2_f_a_method = MO2f_Utility::mo2f_decode_2_factor( isset( $_POST['mo2f_configured_2FA_method_free_plan'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_configured_2FA_method_free_plan'] ) ) : ( isset( $_POST['mo2f_selected_action_standard_plan'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_selected_action_standard_plan'] ) ) : '' ), 'wpdb' );
					$onprem_methods        = array( 'Google Authenticator', 'Security Questions', 'OTPOverTelegram', 'DuoAuthenticator' );
					$mo2fdb_queries->insert_user( $user->ID );
					if ( MO2F_IS_ONPREM && ! in_array( $selected_2_f_a_method, $onprem_methods, true ) ) {
						foreach ( $cloud_methods as $cloud_method ) {
							$is_end_user_registered = $mo2fdb_queries->get_user_detail( 'mo2f_' . $cloud_method . '_config_status', $user->ID );
							if ( ! is_null( $is_end_user_registered ) && 1 === $is_end_user_registered ) {
								break;
							}
						}
					} else {
						$is_end_user_registered = $mo2fdb_queries->get_user_detail( 'user_registration_with_miniorange', $user->ID );
					}
					$is_customer_registered = false;

					if ( ! MO2F_IS_ONPREM || 'miniOrangeSoftToken' === $configured_method || 'miniOrangeQRCodeAuthentication' === $configured_method || 'miniOrangePushNotification' === $configured_method || 'OTPOverSMS' === $configured_method || 'OTP Over SMS' === $configured_method ) {
						$is_customer_registered = get_option( 'mo2f_api_key' ) ? true : false;
					}
					$email = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );
					if ( ! isset( $email ) || is_null( $email ) || empty( $email ) ) {
						$email = $user->user_email;
					}
					$is_end_user_registered = $is_end_user_registered ? $is_end_user_registered : false;
					$allowed                = false;
					if ( get_option( 'mo2f_miniorange_admin' ) ) {
						$allowed = wp_get_current_user()->ID === get_option( 'mo2f_miniorange_admin' );
					}

					if ( ! MO2F_IS_ONPREM && $is_customer_registered && ! $is_end_user_registered && ! $allowed ) {
						$enduser    = new Two_Factor_Setup_Onprem_Cloud();
						$check_user = json_decode( $enduser->mo_check_user_already_exist( $email ), true );
						if ( json_last_error() === JSON_ERROR_NONE ) {
							if ( 'ERROR' === $check_user['status'] ) {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( $check_user['message'] ) );
								$this->mo2f_auth_show_error_message();
								return;
							} elseif ( strcasecmp( $check_user['status'], 'USER_FOUND' ) === 0 ) {
								$mo2fdb_queries->update_user_details(
									$user->ID,
									array(
										'user_registration_with_miniorange' => 'SUCCESS',
										'mo2f_user_email' => $email,
									)
								);
								update_site_option( base64_encode( 'totalUsersCloud' ), intval( get_site_option( base64_encode( 'totalUsersCloud' ) ) ) + 1 ); //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
							} elseif ( strcasecmp( $check_user['status'], 'USER_NOT_FOUND' ) === 0 ) {
								$content = json_decode( $enduser->mo_create_user( $user, $email ), true );
								if ( json_last_error() === JSON_ERROR_NONE ) {
									if ( strcasecmp( $content['status'], 'SUCCESS' ) === 0 ) {
										update_site_option( base64_encode( 'totalUsersCloud' ), intval( get_site_option( base64_encode( 'totalUsersCloud' ) ) ) + 1 ); //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
										$mo2fdb_queries->update_user_details(
											$user->ID,
											array(
												'user_registration_with_miniorange' => 'SUCCESS',
												'mo2f_user_email' => $email,
											)
										);
									}
								}
							} elseif ( strcasecmp( $check_user['status'], 'USER_FOUND_UNDER_DIFFERENT_CUSTOMER' ) === 0 ) {
								$mo2fa_login_message = esc_html__( 'The email associated with your account is already registered in miniOrange. Please Choose another email or contact miniOrange.', 'miniorange-2-factor-authentication' );
								update_option( 'mo2f_message', $mo2fa_login_message );
								$this->mo2f_auth_show_error_message();
							}
						}
					}

					update_user_meta( $user->ID, 'mo2f_2FA_method_to_configure', $selected_2_f_a_method );
					if ( MO2F_IS_ONPREM ) {
						if ( 'EmailVerification' === $selected_2_f_a_method ) {
							$selected_2_f_a_method = 'Email Verification';
						}
						if ( 'OTPOverEmail' === $selected_2_f_a_method ) {
							$selected_2_f_a_method = 'OTP Over Email';
						}
						if ( 'OTPOverSMS' === $selected_2_f_a_method ) {
							$selected_2_f_a_method = 'OTP Over SMS';
						}
						if ( 'OTPOverTelegram' === $selected_2_f_a_method ) {
							$selected_2_f_a_method = 'OTP Over Telegram';
						}
						if ( 'DuoAuthenticator' === $selected_2_f_a_method ) {
							$selected_2_f_a_method = 'Duo Authenticator';
						}
					}
					if ( MO2F_IS_ONPREM && ( 'Google Authenticator' === $selected_2_f_a_method || 'Security Questions' === $selected_2_f_a_method || 'OTP Over Email' === $selected_2_f_a_method || 'Email Verification' === $selected_2_f_a_method || 'OTP Over Telegram' === $selected_2_f_a_method || 'Duo Authenticator' === $selected_2_f_a_method ) ) {
						$is_customer_registered = 1;
					}
					if ( $is_customer_registered ) {
						$selected_2_f_a_method = MO2f_Utility::mo2f_decode_2_factor( isset( $_POST['mo2f_configured_2FA_method_free_plan'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_configured_2FA_method_free_plan'] ) ) : sanitize_text_field( wp_unslash( $_POST['mo2f_selected_action_standard_plan'] ) ), 'wpdb' );
						$selected_action       = isset( $_POST['mo2f_selected_action_free_plan'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_selected_action_free_plan'] ) ) : sanitize_text_field( wp_unslash( $_POST['mo2f_selected_action_standard_plan'] ) );
						$user_phone            = '';
						if ( isset( $_SESSION['user_phone'] ) ) {
							$user_phone = 'false' !== $_SESSION['user_phone'] ? sanitize_text_field( $_SESSION['user_phone'] ) : $mo2fdb_queries->get_user_detail( 'mo2f_user_phone', $user->ID );
						}
						// set it as his 2-factor in the WP database and server.
						if ( 'OTPOverTelegram' === $selected_2_f_a_method ) {
							$selected_2_f_a_method = 'OTP Over Telegram';
						}
						if ( 'DuoAuthenticator' === $selected_2_f_a_method ) {
							$selected_2_f_a_method = 'Duo Authenticator';
						}
						if ( 'select2factor' === $selected_action ) {
							if ( 'OTP Over SMS' === $selected_2_f_a_method && 'false' === $user_phone ) {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'PHONE_NOT_CONFIGURED' ) );
								$this->mo2f_auth_show_error_message();
							} else {
								// update in the WordPress DB.
								$email        = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );
								$customer_key = get_option( 'mo2f_customerKey' );
								$api_key      = get_option( 'mo2f_api_key' );

								$mo2fdb_queries->update_user_details( $user->ID, array( 'mo2f_configured_2FA_method' => $selected_2_f_a_method ) );
								// update the server.
								if ( ! MO2F_IS_ONPREM ) {
									$this->mo2f_save_2_factor_method( $user, $selected_2_f_a_method );
								}
								if ( ! in_array(
									$selected_2_f_a_method,
									array(
										'miniOrange QR Code Authentication',
										'miniOrange Soft Token',
										'miniOrange Push Notification',
										'Google Authenticator',
										'Security Questions',
										'Authy Authenticator',
										'Email Verification',
										'OTP Over SMS',
										'OTP Over Email',
										'OTP Over SMS and Email',
										'Hardware Token',
									),
									true
								) ) {
									update_site_option( 'mo2f_enable_2fa_prompt_on_login_page', 0 );
								}
							}
						} elseif ( 'configure2factor' === $selected_action ) {
							// show configuration form of respective Two Factor method.
							update_user_meta( $user->ID, 'mo2f_configure_2FA', 1 );
							update_user_meta( $user->ID, 'mo2f_2FA_method_to_configure', $selected_2_f_a_method );
						}
					} else {
						update_option( 'mo_2factor_user_registration_status', 'REGISTRATION_STARTED' );
						update_user_meta( $user->ID, 'register_account_popup', 1 );
						update_option( 'mo2f_message', '' );
					}
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_enable_2FA_for_users_option' ) {
				$nonce = isset( $_POST['mo2f_enable_2FA_for_users_option_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_enable_2FA_for_users_option_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-enable-2FA-for-users-option-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					update_option( 'mo2f_enable_2fa_for_users', isset( $_POST['mo2f_enable_2fa_for_users'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_enable_2fa_for_users'] ) ) : 0 );
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_disable_proxy_setup_option' ) {
				$nonce = isset( $_POST['mo2f_disable_proxy_setup_option_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_disable_proxy_setup_option_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-disable-proxy-setup-option-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					delete_option( 'mo2f_proxy_host' );
					delete_option( 'mo2f_port_number' );
					delete_option( 'mo2f_proxy_username' );
					delete_option( 'mo2f_proxy_password' );
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'Proxy Configurations Reset.' ) );
					$this->mo2f_auth_show_success_message();
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_enable_2FA_option' ) {
				$nonce = isset( $_POST['mo2f_enable_2FA_option_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_enable_2FA_option_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-enable-2FA-option-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					update_option( 'mo2f_enable_2fa', isset( $_POST['mo2f_enable_2fa'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_enable_2fa'] ) ) : 0 );
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_2factor_test_authentication_method' ) {
				// network security feature.
				$nonce = isset( $_POST['mo_2factor_test_authentication_method_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo_2factor_test_authentication_method_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo-2factor-test-authentication-method-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					update_user_meta( $user->ID, 'test_2FA', 1 );

					$selected_2_f_a_method        = isset( $_POST['mo2f_configured_2FA_method_test'] ) ? sanitize_text_field( wp_unslash( $_POST['mo2f_configured_2FA_method_test'] ) ) : '';
					$selected_2_f_a_method_server = MO2f_Utility::mo2f_decode_2_factor( $selected_2_f_a_method, 'server' );
					$email                        = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );
					$customer_key                 = get_option( 'mo2f_customerKey' );
					$api_key                      = get_option( 'mo2f_api_key' );

					if ( 'Security Questions' === $selected_2_f_a_method ) {
						$response = json_decode( $this->mo2f_onprem_cloud_obj->send_otp_token( $email, $selected_2_f_a_method_server, $customer_key, $api_key ), true );

						if ( json_last_error() === JSON_ERROR_NONE ) { /* Generate KBA Questions*/
							if ( MoWpnsConstants::SUCCESS_RESPONSE === $response['status'] ) {
								update_option( 'mo2f_transactionId', $response['txId'] );
								$questions = array();

								$questions[0] = $response['questions'][0];
								$questions[1] = $response['questions'][1];
								update_user_meta( $user->ID, 'mo_2_factor_kba_questions', $questions );

								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ANSWER_SECURITY_QUESTIONS' ) );
								$this->mo2f_auth_show_success_message();
							} elseif ( 'ERROR' === $response['status'] ) {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_FETCHING_QUESTIONS' ) );
								$this->mo2f_auth_show_error_message();
							}
						} else {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_FETCHING_QUESTIONS' ) );
							$this->mo2f_auth_show_error_message();
						}
					} elseif ( 'miniOrange Push Notification' === $selected_2_f_a_method ) {
						$customer = new MocURL();
						$response = json_decode( $customer->miniorange_auth_challenge( $email, $selected_2_f_a_method_server, $customer_key, $api_key ), true );
						if ( json_last_error() === JSON_ERROR_NONE ) { /* Generate Qr code */
							if ( 'ERROR' === $response['status'] ) {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( $response['message'] ) );
								$this->mo2f_auth_show_error_message();
							} else {
								if ( MoWpnsConstants::SUCCESS_RESPONSE === $response['status'] ) {
									update_user_meta( $user->ID, 'mo2f_transactionId', $response['txId'] );
									update_user_meta( $user->ID, 'mo2f_show_qr_code', 'MO_2_FACTOR_SHOW_QR_CODE' );

									update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'PUSH_NOTIFICATION_SENT' ) );
									$this->mo2f_auth_show_success_message();
								} else {
									$session_variables = array( 'mo2f_qrCode', 'mo2f_transactionId', 'mo2f_show_qr_code' );
									MO2f_Utility::unset_session_variables( $session_variables );

									delete_option( 'mo2f_transactionId' );
									update_option( 'mo2f_message', 'An error occurred while processing your request. Please Try again.' );
									$this->mo2f_auth_show_error_message();
								}
							}
						} else {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_REQ' ) );
							$this->mo2f_auth_show_error_message();
						}
					} elseif ( 'OTP Over Telegram' === $selected_2_f_a_method ) {
						$user      = wp_get_current_user();
						$chat_i_d  = get_user_meta( $user->ID, 'mo2f_chat_id', true );
						$otp_token = '';
						for ( $i = 1; $i < 7; $i++ ) {
							$otp_token .= wp_rand( 0, 9 );
						}

						update_user_meta( $user->ID, 'mo2f_otp_token', $otp_token );
						update_user_meta( $user->ID, 'mo2f_telegram_time', time() );

						$url      = esc_url( MoWpnsConstants::TELEGRAM_OTP_LINK );
						$postdata = array(
							'mo2f_otp_token' => $otp_token,
							'mo2f_chatid'    => $chat_i_d,
						);

						$args = array(
							'method'    => 'POST',
							'timeout'   => 10,
							'sslverify' => false,
							'headers'   => array(),
							'body'      => $postdata,
						);

						$mo2f_api = new Mo2f_Api();
						$data     = $mo2f_api->mo2f_wp_remote_post( $url, $args );

						if ( MoWpnsConstants::SUCCESS_RESPONSE === $data ) {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'OTP_SENT' ) . 'your telegram number.' . Mo2fConstants::lang_translate( 'ENTER_OTP' ) );
							$this->mo2f_auth_show_success_message();
						} else {
							update_option( 'mo2f_message', 'An Error has occured while sending the OTP. Please verify your chat ID.' );
							$this->mo2f_auth_show_error_message();
						}
					} elseif ( 'OTP Over SMS' === $selected_2_f_a_method || 'OTP Over Email' === $selected_2_f_a_method ) {
						$phone = $mo2fdb_queries->get_user_detail( 'mo2f_user_phone', $user->ID );
						$check = 1;
						if ( 'OTP Over Email' === $selected_2_f_a_method ) {
							$phone = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );
							if ( MoWpnsUtility::get_mo2f_db_option( 'cmVtYWluaW5nT1RQ', 'site_option' ) <= 0 ) {
								update_site_option( 'bGltaXRSZWFjaGVk', 1 );
								$check = 0;
							}
						}

						if ( 1 === $check ) {
							$response = json_decode( $this->mo2f_onprem_cloud_obj->send_otp_token( $phone, $selected_2_f_a_method_server, $customer_key, $api_key ), true );

						} else {
							$response['status'] = 'FAILED';
						}
						if ( strcasecmp( $response['status'], 'SUCCESS' ) === 0 ) {
							if ( MoWpnsConstants::OTP_OVER_EMAIL === $selected_2_f_a_method || MoWpnsConstants::EMAIL_VERIFICATION === $selected_2_f_a_method ) {
								$cm_vt_y_wlua_w5n_t1_r_q = MoWpnsUtility::get_mo2f_db_option( 'cmVtYWluaW5nT1RQ', 'site_option' );
								if ( $cm_vt_y_wlua_w5n_t1_r_q > 0 ) {
									update_site_option( 'cmVtYWluaW5nT1RQ', $cm_vt_y_wlua_w5n_t1_r_q - 1 );
								}
							} elseif ( 'OTP Over SMS' === $selected_2_f_a_method ) {
								$mo2f_sms = get_site_option( 'cmVtYWluaW5nT1RQVHJhbnNhY3Rpb25z' );
								if ( $mo2f_sms > 0 ) {
									update_site_option( 'cmVtYWluaW5nT1RQVHJhbnNhY3Rpb25z', $mo2f_sms - 1 );
								}
							}
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'OTP_SENT' ) . ' <b>' . ( $phone ) . '</b>. ' . Mo2fConstants::lang_translate( 'ENTER_OTP' ) );
							update_option( 'mo2f_number_of_transactions', MoWpnsUtility::get_mo2f_db_option( 'mo2f_number_of_transactions', 'get_option' ) - 1 );
							update_user_meta( $user->ID, 'mo2f_transactionId', $response['txId'] );
							update_option( 'mo2f_transactionId', $response['txId'] );
							$this->mo2f_auth_show_success_message();
						} else {
							if ( ! MO2F_IS_ONPREM || 'OTP Over SMS' === $selected_2_f_a_method ) {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_IN_SENDING_OTP' ) );
							} else {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_IN_SENDING_OTP_ONPREM' ) );
							}

							$this->mo2f_auth_show_error_message();
						}
					} elseif ( 'miniOrange QR Code Authentication' === $selected_2_f_a_method ) {
						$customer = new MocURL();
						$response = json_decode( $customer->miniorange_auth_challenge( $email, $selected_2_f_a_method_server, $customer_key, $api_key ), true );
						if ( json_last_error() === JSON_ERROR_NONE ) { /* Generate Qr code */
							if ( 'ERROR' === $response['status'] ) {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( $response['message'] ) );
								$this->mo2f_auth_show_error_message();
							} else {
								if ( MoWpnsConstants::SUCCESS_RESPONSE === $response['status'] ) {
									update_user_meta( $user->ID, 'mo2f_qrCode', $response['qrCode'] );
									update_user_meta( $user->ID, 'mo2f_transactionId', $response['txId'] );
									update_user_meta( $user->ID, 'mo2f_show_qr_code', 'MO_2_FACTOR_SHOW_QR_CODE' );

									update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SCAN_QR_CODE' ) );
									$this->mo2f_auth_show_success_message();
								} else {
									update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_PROCESS' ) );
									$this->mo2f_auth_show_error_message();
								}
							}
						} else {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_REQ' ) );
							$this->mo2f_auth_show_error_message();
						}
					} elseif ( 'Email Verification' === $selected_2_f_a_method ) {
						global $mo2f_onprem_cloud_obj;
						$mo2f_onprem_cloud_obj->mo2f_email_verification_call( $user );
					}

					update_user_meta( $user->ID, 'mo2f_2FA_method_to_test', $selected_2_f_a_method );
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_go_back' ) {
				$nonce = isset( $_POST['mo2f_go_back_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_go_back_nonce'] ) ) : null;

				if ( ! wp_verify_nonce( $nonce, 'mo2f-go-back-nonce' ) ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SOMETHING_WENT_WRONG' ) );
					$this->mo2f_auth_show_error_message();
					return;
				} else {
					$session_variables = array(
						'mo2f_qrCode',
						'mo2f_transactionId',
						'mo2f_show_qr_code',
						'user_phone',
						'mo2f_google_auth',
						'mo2f_mobile_support',
						'mo2f_authy_keys',
					);
					MO2f_Utility::unset_session_variables( $session_variables );
					delete_option( 'mo2f_transactionId' );
					delete_user_meta( $user->ID, 'user_phone_temp' );
					delete_user_meta( $user->ID, 'test_2FA' );
					delete_user_meta( $user->ID, 'mo2f_configure_2FA' );
					delete_user_meta( $user->ID, 'mo2f_otp_send_true' );
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_reset_duo_configuration' ) {
				$nonce = isset( $_POST['mo2f_duo_reset_configuration_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_duo_reset_configuration_nonce'] ) ) : null;
				if ( ! wp_verify_nonce( $nonce, 'mo2f-duo-reset-configuration-nonce' ) ) {
					$error = new WP_Error();
					$error->add( 'empty_username', '<strong>' . __( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . __( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );
					return $error;
				} else {
					delete_site_option( 'duo_credentials_save_successfully' );
					delete_user_meta( $user->ID, 'user_not_enroll' );
					delete_site_option( 'mo2f_d_integration_key' );
					delete_site_option( 'mo2f_d_secret_key' );
					delete_site_option( 'mo2f_d_api_hostname' );
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'RESET_DUO_CONFIGURATON' ) );
					$this->mo2f_auth_show_success_message();
				}
			} elseif ( isset( $_POST['option'] ) && sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo2f_2factor_generate_backup_codes' ) {
				$nonce = isset( $_POST['mo_2factor_generate_backup_codes_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo_2factor_generate_backup_codes_nonce'] ) ) : null;
				if ( ! wp_verify_nonce( $nonce, 'mo-2factor-generate-backup-codes-nonce' ) ) {
					$error = new WP_Error();
					$error->add( 'empty_username', '<strong>' . __( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . __( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );
					return $error;
				} else {
					$codes = MO2f_Utility::mo2f_mail_and_download_codes();

					if ( 'TransientActive' === $codes ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'TRANSIENT_ACTIVE' ) );
						$this->mo2f_auth_show_error_message();
					}

					if ( 'InternetConnectivityError' === $codes ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INTERNET_CONNECTIVITY_ERROR' ) );
						$this->mo2f_auth_show_error_message();
					}

					if ( 'LimitReached' === $codes || 'UserLimitReached' === $codes || 'AllUsed' === $codes || 'invalid_request' === $codes ) {
						$id = get_current_user_id();
						update_user_meta( $id, 'mo_backup_code_generated', 1 );
						update_user_meta( $id, 'mo_backup_code_downloaded', 1 );

						if ( 'AllUsed' === $codes ) {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'USED_ALL_BACKUP_CODES' ) );
						} elseif ( 'LimitReached' === $codes ) {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'BACKUP_CODE_LIMIT_REACH' ) );
						} elseif ( 'UserLimitReached' === $codes ) {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'BACKUP_CODE_DOMAIN_LIMIT_REACH' ) );
						} elseif ( 'invalid_request' === $codes ) {
							update_user_meta( $id, 'mo_backup_code_generated', 0 );
							update_user_meta( $id, 'mo_backup_code_downloaded', 0 );
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'BACKUP_CODE_INVALID_REQUEST' ) );
						}

						$this->mo2f_auth_show_error_message();
					}
				}
			} elseif ( isset( $_POST['option'] ) && isset( $_POST[ $_POST['option'] ] ) ) {
				$val = str_replace( 'mo2f_unblock_user_', '', sanitize_text_field( wp_unslash( $_POST['option'] ) ) );

				$nonce = isset( $_POST['mo2f_unblock_form_nonce'] ) ? sanitize_key( wp_unslash( $_POST['mo2f_unblock_form_nonce'] ) ) : '';
				if ( ! wp_verify_nonce( $nonce, 'mo2f-unblock-form-nonce' ) ) {
					$error = new WP_Error();
					$error->add( 'empty_username', '<strong>' . __( 'ERROR', 'miniorange-2-factor-authentication' ) . '</strong>: ' . __( 'Invalid Request.', 'miniorange-2-factor-authentication' ) );
					return $error;
				} else {
					update_site_option( 'mo2f_user_login_status_' . $val, 0 );
					update_site_option( 'mo2f_is_user_blocked_' . $val, 0 );
					update_site_option( 'mo2f_grace_period_status_' . $val, 0 );
				}
			}
		}
		/**
		 * Delete user details on deativation.
		 */
		public function mo2f_auth_deactivate() {

			global $mo2fdb_queries;
			$mo2f_register_with_another_email = get_option( 'mo2f_register_with_another_email' );
			if ( $mo2f_register_with_another_email ) {
				update_option( 'mo2f_register_with_another_email', 0 );
				$mo2fdb_queries->mo2f_delete_user_details();
			}
		}


		/**
		 * Show succes message on authentication.
		 *
		 * @return void
		 */
		public function mo2f_auth_show_success_message() {

			do_action( 'wpns_show_message', get_option( 'mo2f_message' ), 'SUCCESS' );
		}
		/**
		 * Create customer on miniOrange IDP.
		 *
		 * @param object $user user object.
		 * @return void
		 */
		public function mo2f_create_customer( $user ) {

			global $mo2fdb_queries;
			delete_user_meta( $user->ID, 'mo2f_sms_otp_count' );
			delete_user_meta( $user->ID, 'mo2f_email_otp_count' );
			$customer     = new Customer_Cloud_Setup();
			$customer_key = json_decode( $customer->create_customer(), true );

			if ( 'ERROR' === $customer_key['status'] ) {
				update_option( 'mo2f_message', Mo2fConstants::lang_translate( $customer_key['message'] ) );
				$this->mo2f_auth_show_error_message();
			} else {
				if ( strcasecmp( $customer_key['status'], 'CUSTOMER_USERNAME_ALREADY_EXISTS' ) === 0 ) {    // admin already exists in miniOrange.
					$content      = $customer->get_customer_key();
					$customer_key = json_decode( $content, true );

					if ( json_last_error() === JSON_ERROR_NONE ) {
						if ( array_key_exists( 'status', $customer_key ) && 'ERROR' === $customer_key['status'] ) {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( $customer_key['message'] ) );
							$this->mo2f_auth_show_error_message();
						} else {
							if ( isset( $customer_key['id'] ) && ! empty( $customer_key['id'] ) ) {
								update_option( 'mo2f_customerKey', $customer_key['id'] );
								update_option( 'mo2f_api_key', $customer_key['apiKey'] );
								update_option( 'mo2f_customer_token', $customer_key['token'] );
								update_option( 'mo2f_app_secret', $customer_key['appSecret'] );
								update_option( 'mo2f_miniorange_admin', $user->ID );
								delete_option( 'mo2f_password' );
								$email = get_option( 'mo2f_email' );

								update_option( 'mo_2factor_admin_registration_status', 'MO_2_FACTOR_CUSTOMER_REGISTERED_SUCCESS' );
								$this->mo2f_onprem_cloud_obj->mo2f_update_user_info( $user->ID, true, MoWpnsConstants::OUT_OF_BAND_EMAIL_UC, MoWpnsConstants::SUCCESS_RESPONSE, MoWpnsConstants::MO_2_FACTOR_PLUGIN_SETTINGS, true, $email, null, 'API_2FA', true );

								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ACCOUNT_RETRIEVED_SUCCESSFULLY' ) . ' <b>' . Mo2fConstants::lang_translate( 'EMAIL_VERFI' ) . '</b> ' . Mo2fConstants::lang_translate( 'DEFAULT_2ND_FACTOR' ) . ' <a href=\"admin.php?page=miniOrange_2_factor_settings&amp;mo2f_tab=mobile_configure\" >' . Mo2fConstants::lang_translate( 'CLICK_HERE' ) . '</a> ' . Mo2fConstants::lang_translate( 'CONFIGURE_2FA' ) );
								$this->mo2f_auth_show_success_message();
							} else {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_CREATE_ACC_OTP' ) );
								$mo_2factor_user_registration_status = 'MO_2_FACTOR_OTP_DELIVERED_FAILURE';
								$mo2fdb_queries->update_user_details( $user->ID, array( 'mo_2factor_user_registration_status' => $mo_2factor_user_registration_status ) );
								$this->mo2f_auth_show_error_message();
							}
						}
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_EMAIL_OR_PASSWORD' ) );
						$mo_2factor_user_registration_status = 'MO_2_FACTOR_VERIFY_CUSTOMER';
						update_option( 'mo_2factor_user_registration_status', $mo_2factor_user_registration_status );
						$this->mo2f_auth_show_error_message();
					}
				} else {
					if ( isset( $customer_key['id'] ) && ! empty( $customer_key['id'] ) ) {
						update_option( 'mo2f_customerKey', $customer_key['id'] );
						update_option( 'mo2f_api_key', $customer_key['apiKey'] );
						update_option( 'mo2f_customer_token', $customer_key['token'] );
						update_option( 'mo2f_app_secret', $customer_key['appSecret'] );
						update_option( 'mo2f_miniorange_admin', $user->ID );
						delete_option( 'mo2f_password' );

						$email = get_option( 'mo2f_email' );

						update_option( 'mo2f_is_NC', 1 );
						update_option( 'mo2f_is_NNC', 1 );

						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ACCOUNT_CREATED' ) );
						$mo_2factor_user_registration_status = 'MO_2_FACTOR_PLUGIN_SETTINGS';
						$mo2fdb_queries->update_user_details(
							$user->ID,
							array(
								'mo2f_2factor_enable_2fa_byusers' => 1,
								'user_registration_with_miniorange' => MoWpnsConstants::SUCCESS_RESPONSE,
								'mo2f_configured_2FA_method' => 'NONE',
								'mo2f_user_email' => $email,
								'mo_2factor_user_registration_status' => $mo_2factor_user_registration_status,
							)
						);

						update_option( 'mo_2factor_admin_registration_status', 'MO_2_FACTOR_CUSTOMER_REGISTERED_SUCCESS' );

						$this->mo2f_onprem_cloud_obj->mo2f_update_user_info( $user->ID, null, 'NONE', MoWpnsConstants::SUCCESS_RESPONSE, $mo_2factor_user_registration_status, true, $email, null, 'API_2FA', true );

						$this->mo2f_auth_show_success_message();

						$mo2f_customer_selected_plan = get_option( 'mo2f_customer_selected_plan' );
						if ( ! empty( $mo2f_customer_selected_plan ) ) {
							delete_option( 'mo2f_customer_selected_plan' );
							header( 'Location: admin.php?page=mo_2fa_upgrade' );
						} else {
							header( 'Location: admin.php?page=mo_2fa_two_fa' );
						}
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_CREATE_ACC_OTP' ) );
						$mo_2factor_user_registration_status = 'MO_2_FACTOR_OTP_DELIVERED_FAILURE';
						$mo2fdb_queries->update_user_details( $user->ID, array( 'mo_2factor_user_registration_status' => $mo_2factor_user_registration_status ) );
						$this->mo2f_auth_show_error_message();
					}
				}
			}
		}
		/**
		 * Show error messages.
		 *
		 * @return void
		 */
		public function mo2f_auth_show_error_message() {

			do_action( 'wpns_show_message', get_option( 'mo2f_message' ), 'ERROR' );
		}
		/**
		 * Create user on miniOrange.
		 *
		 * @param object $user User object.
		 * @param string $email user email.
		 * @return void
		 */
		public function mo2f_create_user( $user, $email ) {

			global $mo2fdb_queries;
			$email   = strtolower( $email );
			$enduser = new Two_Factor_Setup_Onprem_Cloud();

			$check_user = json_decode( $this->mo2f_onprem_cloud_obj->mo_check_user_already_exist( $email ), true );

			if ( json_last_error() === JSON_ERROR_NONE ) {
				if ( 'ERROR' === $check_user['status'] ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( $check_user['message'] ) );
					$this->mo2f_auth_show_error_message();
				} else {
					if ( strcasecmp( $check_user['status'], 'USER_FOUND' ) === 0 ) {

						delete_user_meta( $user->ID, 'user_email' );
						$this->mo2f_onprem_cloud_obj->mo2f_update_user_info( $user->ID, null, 'NONE', MoWpnsConstants::SUCCESS_RESPONSE, MoWpnsConstants::MO_2_FACTOR_PLUGIN_SETTINGS, true, $email, null, 'API_2FA', true );
						$message = Mo2fConstants::lang_translate( 'REGISTRATION_SUCCESS' );
						update_option( 'mo2f_message', $message );
						$this->mo2f_auth_show_success_message();
						header( 'Location: admin.php?page=mo_2fa_two_fa' );
					} elseif ( strcasecmp( $check_user['status'], 'USER_NOT_FOUND' ) === 0 ) {
						$content = json_decode( $enduser->mo_create_user( $user, $email ), true );
						if ( json_last_error() === JSON_ERROR_NONE ) {
							if ( 'ERROR' === $content['status'] ) {
								update_option( 'mo2f_message', Mo2fConstants::lang_translate( $content['message'] ) );
								$this->mo2f_auth_show_error_message();
							} else {
								if ( strcasecmp( $content['status'], 'SUCCESS' ) === 0 ) {
									delete_user_meta( $user->ID, 'user_email' );
									$this->mo2f_onprem_cloud_obj->mo2f_update_user_info( $user->ID, null, 'NONE', MoWpnsConstants::SUCCESS_RESPONSE, MoWpnsConstants::MO_2_FACTOR_PLUGIN_SETTINGS, true, $email, null, 'API_2FA', true );

									$message = Mo2fConstants::lang_translate( 'REGISTRATION_SUCCESS' );
									update_option( 'mo2f_message', $message );
									$this->mo2f_auth_show_success_message();
									header( 'Location: admin.php?page=mo_2fa_two_fa' );
								} else {
									update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_USER_REGISTRATION' ) );
									$this->mo2f_auth_show_error_message();
								}
							}
						} else {
							update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_USER_REGISTRATION' ) );
							$this->mo2f_auth_show_error_message();
						}
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_USER_REGISTRATION' ) );
						$this->mo2f_auth_show_error_message();
					}
				}
			} else {
				update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_USER_REGISTRATION' ) );
				$this->mo2f_auth_show_error_message();
			}
		}
		/**
		 * Get QR code for mobile.
		 *
		 * @param string $email user email.
		 * @param int    $id user id.
		 * @param string $session_id user session id.
		 * @return void
		 */
		public function mo2f_get_qr_code_for_mobile( $email, $id, $session_id = null ) {

			$register_mobile = new Two_Factor_Setup_Onprem_Cloud();
			$content         = $register_mobile->register_mobile( $email );

			$response = json_decode( $content, true );
			if ( json_last_error() === JSON_ERROR_NONE ) {
				if ( 'ERROR' === $response['status'] ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( $response['message'] ) );
					$session_variables = array( 'mo2f_qrCode', 'mo2f_transactionId', 'mo2f_show_qr_code' );
					MO2f_Utility::unset_session_variables( $session_variables );
					delete_option( 'mo2f_transactionId' );
					$this->mo2f_auth_show_error_message();
				} else {
					if ( 'IN_PROGRESS' === $response['status'] ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'SCAN_QR_CODE' ) );
						MO2f_Utility::mo2f_set_transient( $session_id, 'mo2f_qrCode', $response['qrCode'] );
						MO2f_Utility::mo2f_set_transient( $session_id, 'mo2f_transactionId', $response['txId'] );
						update_user_meta( $id, 'mo2f_transactionId', $response['txId'] );
						MO2f_Utility::mo2f_set_transient( $session_id, 'mo2f_show_qr_code', 'MO_2_FACTOR_SHOW_QR_CODE' );

						$this->mo2f_auth_show_success_message();
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_PROCESS' ) );
						$session_variables = array( 'mo2f_qrCode', 'mo2f_transactionId', 'mo2f_show_qr_code' );
						MO2f_Utility::unset_session_variables( $session_variables );
						delete_option( 'mo2f_transactionId' );
						$this->mo2f_auth_show_error_message();
					}
				}
			}
		}
		/**
		 * Save 2-factor method of a user.
		 *
		 * @param object $user user object.
		 * @param string $mo2f_configured_2_f_a_method configured 2FA method of a user.
		 * @return void
		 */
		public function mo2f_save_2_factor_method( $user, $mo2f_configured_2_f_a_method ) {

			global $mo2fdb_queries;
			$email          = $mo2fdb_queries->get_user_detail( 'mo2f_user_email', $user->ID );
			$phone          = $mo2fdb_queries->get_user_detail( 'mo2f_user_phone', $user->ID );
			$current_method = MO2f_Utility::mo2f_decode_2_factor( $mo2f_configured_2_f_a_method, 'server' );

			$response = json_decode( $this->mo2f_onprem_cloud_obj->mo2f_update_user_info( $user->ID, true, $current_method, null, MoWpnsConstants::MO_2_FACTOR_PLUGIN_SETTINGS, null, $email, null ), true );

			if ( json_last_error() === JSON_ERROR_NONE ) {
				if ( 'ERROR' === $response['status'] ) {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( $response['message'] ) );
					$this->mo2f_auth_show_error_message();
				} elseif ( MoWpnsConstants::SUCCESS_RESPONSE === $response['status'] ) {
					$configured_2_f_a_method = empty( $mo2f_configured_2_f_a_method ) ? $mo2fdb_queries->get_user_detail( 'mo2f_configured_2FA_method', $user->ID ) : $mo2f_configured_2_f_a_method;

					if ( in_array( $configured_2_f_a_method, array( 'Google Authenticator', 'Authy Authenticator' ), true ) ) {
						update_user_meta( $user->ID, 'mo2f_external_app_type', $configured_2_f_a_method );
					}

					delete_user_meta( $user->ID, 'mo2f_configure_2FA' );

					if ( 'OTP Over Email' === $configured_2_f_a_method || 'OTP Over SMS' === $configured_2_f_a_method ) {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( $configured_2_f_a_method ) . ' ' . Mo2fConstants::lang_translate( 'SET_2FA_otp' ) );
					} else {
						update_option( 'mo2f_message', Mo2fConstants::lang_translate( $configured_2_f_a_method ) . ' ' . Mo2fConstants::lang_translate( 'SET_2FA' ) );
					}

					$this->mo2f_auth_show_success_message();
				} else {
					update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'ERROR_DURING_PROCESS' ) );
					$this->mo2f_auth_show_error_message();
				}
			} else {
				update_option( 'mo2f_message', Mo2fConstants::lang_translate( 'INVALID_REQ' ) );
				$this->mo2f_auth_show_error_message();
			}
		}

		/**
		 * Low otp alert.
		 *
		 * @param string $auth_type authentication type.
		 * @return void
		 */
		public static function mo2f_low_otp_alert( $auth_type ) {

			global $image_path;
			$email = get_option( 'mo2f_email' ) ? get_option( 'mo2f_email' ) : get_option( 'admin_email' );
			if ( MO2F_IS_ONPREM ) {
				$count = 0;
				if ( 'email' === $auth_type ) {
					$subject = 'Two Factor Authentication(Low Email Alert)';
					$count   = get_site_option( 'cmVtYWluaW5nT1RQ' ) - 1; // database value is updated after public function call.
					$string  = 'Email';
				} elseif ( 'sms' === $auth_type ) {
					$subject = 'Two Factor Authentication(Low SMS Alert)';
					$count   = get_site_option( 'cmVtYWluaW5nT1RQVHJhbnNhY3Rpb25z' ) - 1; // database value is updated after public function call.
					$string  = 'SMS';
				}
				$admin_url = network_site_url();
				$url       = explode( '/wp-admin/admin.php?page=mo_2fa_upgrade', $admin_url );
				$headers   = array( 'Content-Type: text/html; charset=UTF-8' );
				$headers[] = 'Cc: 2fasupport <mfasupport@xecurify.com>';
				$message   = '<table cellpadding="25" style="margin:0px auto">
			<tbody>
			<td>
			<td>
			<table cellpadding="24" width="584px" style="margin:0 auto;max-width:584px;background-color:#f6f4f4;border:1px solid #a8adad">
			<tbody>
			<td>
			<td><img src="' . $image_path . 'includes/images/xecurify-logo.png" alt="Xecurify" style="color:#5fb336;text-decoration:none;display:block;width:auto;height:auto;max-height:35px" class="CToWUd"></td>
			</tr>
			</tbody>
			</table>
			<table cellpadding="24" style="background:#fff;border:1px solid #a8adad;width:584px;border-top:none;color:#4d4b48;font-family:Arial,Helvetica,sans-serif;font-size:13px;line-height:18px">
			<tbody>
			<td>
			<td>
			<p style="margin-top:0;margin-bottom:20px">Dear Customer,</p>
			<p style="margin-top:0;margin-bottom:20px"> You are going to exhaust all your ' . $string . '. You have only <b>' . $count . '</b> ' . $string . ' remaining. You can recharge || add ' . $string . ' to your account: <a href=' . MoWpnsConstants::RECHARGELINK . '>Recharge</a></p>
			<p style="margin-top:0;margin-bottom:10px">After Recharge you can continue using your current plan. To know more about our plans you can also visit our site: <a href=' . $url[0] . '/wp-admin/admin.php?page=mo_2fa_upgrade>2FA Plans</a>.</p>
			<p style="margin-top:0;margin-bottom:10px">If you do not wish to recharge, we advise you to <a href=' . $url[0] . '/wp-admin/admin.php?page=mo_2fa_two_fa>change the 2FA method</a> before you have no ' . $string . ' left. In case you get locked out, please use this guide to gain access: <a href=' . MoWpnsConstants::ONPREMISELOCKEDOUT . '>Guide link</a></p>
			<p style="margin-top:0;margin-bottom:20px">For more information, you can contact us directly at 2fasupport@xecurify.com.</p>
			<p style="margin-top:0;margin-bottom:15px">Thank you,<br>miniOrange Team</p>
			<p style="margin-top:0;margin-bottom:0px;font-size:11px">Disclaimer: This email and any files transmitted with it are confidential and intended solely for the use of the individual || entity to whom they are addressed.</p>
			</div></div></td>
			</tr>
			</tbody>
			</table>
			</td>
			</tr>
			</tbody>
			</table>';
				$result    = wp_mail( $email, $subject, $message, $headers );
				if ( $result ) {
					$current_time_in_millis = round( microtime( true ) * 1000 );
					update_site_option( 'mo2f_message', Mo2fConstants::lang_translate( 'VERIFICATION_EMAIL_SENT' ) . '<b> ' . $email . '</b>. ' . Mo2fConstants::lang_translate( 'ACCEPT_LINK_TO_VERIFY_EMAIL' ) );
				}
			}
		}

		/**
		 * Check if a customer is registered.
		 *
		 * @return boolean
		 */
		public static function mo2f_is_customer_registered() {
			$email        = get_option( 'mo2f_email' );
			$customer_key = get_option( 'mo2f_customerKey' );
			if ( ! $email || ! $customer_key || ! is_numeric( trim( $customer_key ) ) ) {
				return 0;
			} else {
				return 1;
			}
		}
	}
	new Miniorange_Authentication();
}

