<?php if ( ! is_user_logged_in() ) {

	// Login
	
	if ( isset( $_POST['action'] ) && $_POST['action'] == 'gp_login' ) {

		$username = esc_sql( $_REQUEST['log'] );
		$password = esc_sql( $_REQUEST['pwd'] );
		$remember = esc_sql( $_REQUEST['rememberme'] );

		if ( $remember ) { $remember = 'true'; } else { $remember = 'false'; }
		$login_data = array();
		
		if ( ( function_exists( 'cptch_check_custom_form' ) && cptch_check_custom_form() === true ) OR ( function_exists( 'cptchpr_check_custom_form' ) && cptchpr_check_custom_form() === true ) OR ( ! function_exists( 'cptch_check_custom_form' ) && ! function_exists( 'cptchpr_check_custom_form' ) ) ) {
			$login_data['user_login'] = $username;
			$login_data['user_password'] = $password;
			$login_data['remember'] = $remember;
			$user_verify = wp_signon( $login_data, false ); 
		}					
	
		if ( empty( $_POST['log'] ) && empty( $_POST['pwd'] ) ) {
			echo "<span class='error'>" . __( 'Please enter your username and password', 'gp_lang' ) . "</span>";
			exit();
		} elseif ( empty( $_POST['log'] ) ) {
			echo "<span class='error'>" . __( 'Please enter your username', 'gp_lang' ) . "</span>";
			exit();
		} elseif ( empty( $_POST['pwd'] ) ) {
			echo "<span class='error'>" . __( 'Please enter your password', 'gp_lang' ) . "</span>";
			exit();
		}
			
		$user_data = get_user_by( 'login', $username );
		if ( ( function_exists( 'cptch_check_custom_form' ) && cptch_check_custom_form() !== true ) OR ( function_exists( 'cptchpr_check_custom_form' ) && cptchpr_check_custom_form() !== true ) ) {
			echo "<span class='error'>" . __( 'Please complete the CAPTCHA', 'gp_lang' ) . "</span>";	
			exit();
		} elseif ( empty( $user_data ) && is_wp_error( $user_verify ) ) {
			echo "<span class='error'>" . __( 'Invalid username and password', 'gp_lang' ) . "</span>";
			exit();
		} elseif ( ! empty( $user_data ) && is_wp_error( $user_verify ) ) {
			echo "<span class='error'>" . __( 'Invalid password', 'gp_lang' ) . "</span>";
			exit();   
		} else {
			echo "<script type='text/javascript'>window.location.reload();</script>";
			exit();
		}

	}
	

	// Register
	
	if ( isset( $_POST['action'] ) && $_POST['action'] == 'gp_register' ) {

		$info = array();
		$info['user_nicename'] = $info['nickname'] = $info['display_name'] = $info['first_name'] = $info['user_login'] = sanitize_user( $_POST['user_login'] ) ;
		$info['user_pass'] = sanitize_text_field( $_POST['user_pass'] );
		$info['user_email'] = sanitize_email( $_POST['user_email'] );
		
		if ( empty( $_POST['user_login'] ) OR empty( $_POST['user_email'] ) OR empty( $_POST['user_pass'] ) OR ( ( function_exists( 'cptch_check_custom_form' ) && cptch_check_custom_form() !== true ) OR ( function_exists( 'cptchpr_check_custom_form' ) && cptchpr_check_custom_form() !== true ) ) ) {
			$user_register = '';
			echo "<span class='error'>" . __( 'Please complete all fields', 'gp_lang' ) . "</span>";
			exit();
		} else {
			$user_register = wp_insert_user( $info );
		}	
	
 		if ( is_wp_error( $user_register ) ) {	
			$error = $user_register->get_error_codes();
			if ( in_array( 'empty_user_login', $error ) ) {
				echo "<span class='error'>" . __( $user_register->get_error_message( 'empty_user_login' ), 'gp_lang' ) . "</span>";	
				exit();
			} elseif ( in_array( 'existing_user_login', $error ) ) {
				echo "<span class='error'>" . __( 'This username is already registered.', 'gp_lang' ) . "</span>";	
				exit();
			} elseif ( in_array( 'existing_user_email', $error ) ) {
				echo "<span class='error'>" . __( 'This email address is already registered.', 'gp_lang' ) . "</span>";	
				exit(); 
			}
		} else {
			wp_new_user_notification( $user_register, sanitize_text_field( $_POST['user_pass'] ) );
			echo "<span class='success'>" . __( 'An email has been sent with your details.', 'gp_lang' ) . "</span>";	
			exit(); 
		}

	}
	
		
	// Lost Password
	
	function gp_validate_url() {
		global $post;
		$page_url = esc_url( home_url() );
		$urlget = strpos( $page_url, '?' );
		if ( $urlget === false ) {
			$concate = "?";
		} else {
			$concate = "&";
		}
		return $page_url . $concate;
	}
	
	if ( isset( $_GET['key'] ) && $_GET['action'] == 'reset_pwd' ) {
	
		$reset_key = $_GET['key'];
		$user_login = $_GET['login'];
		$user_data = $wpdb->get_row( $wpdb->prepare( "SELECT ID, user_login, user_email FROM $wpdb->users WHERE user_activation_key = %s AND user_login = %s", $reset_key, $user_login ) );
	
		$user_login = $user_data->user_login;
		$user_email = $user_data->user_email;
	
		if ( ! empty( $reset_key ) && ! empty( $user_data ) ) {
		
			$new_password = wp_generate_password( 7, false );
			wp_set_password( $new_password, $user_data->ID );
			$message = __( 'Your new password for the account at:', 'gp_lang' ) . "\r\n\r\n";
			$message .= get_option( 'siteurl' ) . "\r\n\r\n";
			$message .= sprintf( __( 'Username: %s', 'gp_lang' ), $user_login ) . "\r\n\r\n";
			$message .= sprintf( __( 'Password: %s', 'gp_lang' ), $new_password ) . "\r\n\r\n";
			$message .= __( 'You can now login with your new password at: ', 'gp_lang' ) . get_option( 'siteurl' )."#login" . "\r\n\r\n";
		
			if ( $message && ! wp_mail( $user_email, __( 'Password Reset Request', 'gp_lang' ), $message ) ) {
				echo "<span class='error'>" . __( 'Email failed to send for some unknown reason', 'gp_lang' ) . "</span>";
				exit();
			} else {
				$redirect_to = home_url() . '#login?action=reset_success';
				wp_safe_redirect( $redirect_to );
				exit();
			}
			
		} else {
		
			exit( 'Not a valid key.' );
			
		}
		
	}

	if ( isset( $_POST['action'] ) && $_POST['action'] == 'gp_lost_password' ) {

		if ( ! wp_verify_nonce( $_POST['gp_pwd_nonce'], 'gp_pwd_nonce' ) ) {
			exit( 'No trick please' );
		}  

		if ( empty( $_POST['user_login'] ) ) {
			echo "<span class='error'>" . __( 'Please enter your username or email address', 'gp_lang' ) . "</span>";
			exit();
		}
	
		$user_input = esc_sql( trim( $_POST['user_login'] ) );
	
		if ( strpos( $user_input, '@' ) ) {
			$user_data = get_user_by( 'email', $user_input );
			if ( empty( $user_data ) || $user_data->caps['administrator'] == 1 ) { //delete the condition $user_data->caps[administrator] == 1, if you want to allow password reset for admins also
				echo "<span class='error'>" . __( 'Invalid email address', 'gp_lang' ) . "</span>";
				exit();
			}
		} else {
			$user_data = get_user_by( 'login', $user_input );
			if ( empty( $user_data ) || $user_data->caps[administrator] == 1 ) { //delete the condition $user_data->caps[administrator] == 1, if you want to allow password reset for admins also
				echo "<span class='error'>" . __( 'Invalid username', 'gp_lang' )."</span>";
				exit();
			}
		}
	
		$user_login = $user_data->user_login;
		$user_email = $user_data->user_email;
	
		$key = $wpdb->get_var( $wpdb->prepare( "SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login ) );
		if ( empty( $key ) ) { // generate reset key
			$key = wp_generate_password( 20, false );
			$wpdb->update( $wpdb->users, array( 'user_activation_key' => $key ), array( 'user_login' => $user_login ) );	
		}
	
		// Mailing reset details to the user
		$message = __( 'Someone requested that the password be reset for the following account:', 'gp_lang' ) . "\r\n\r\n";
		$message .= get_option( 'siteurl' ) . "\r\n\r\n";
		$message .= sprintf( __( 'Username: %s', 'gp_lang' ), $user_login ) . "\r\n\r\n";
		$message .= __( 'If this was a mistake, just ignore this email and nothing will happen.', 'gp_lang' ) . "\r\n\r\n";
		$message .= __( 'To reset your password, visit the following address:', 'gp_lang' ) . "\r\n\r\n";
		$message .= gp_validate_url() . "action=reset_pwd&key=$key&login=" . rawurlencode( $user_login ) . "\r\n";
	
		if ( $message && ! wp_mail( $user_email, __( 'Password Reset Request', 'gp_lang' ), $message ) ) {
			echo "<span class='error'>" . __( 'Email failed to send for some unknown reason.', 'gp_lang' ) . "</span>";
			exit();
		} else {
			echo "<span class='success'>" . __( 'We have just sent you an email with instructions to reset your password.', 'gp_lang' ) . "</span>";
			exit();
		}
	
	}

}

?>