ERROR: Could not connect to 4Login server.', '4login-for-secure-and-smart-access'));
} elseif ($result) {
// FOURLOGIN Authenticate success
//check if FOURLOGIN may create new user.
if ( 'yes' == $fourlogin_options[FOURLOGIN_OPTION_DONT_CREATE_USER] ){
$error = new WP_Error('incorrect_password', __('ERROR: This site is only for members.', '4login-for-secure-and-smart-access'));
return $error;
}
// user create and login
$created_user = array('user_login' => $username,
'user_pass' => '',
'user_email' => $username,
'role' => $fourlogin_options[FOURLOGIN_OPTION_DEFAULT_ROLE],
);
$user_id = wp_insert_user( apply_filters( 'fourlogin_create_user', $created_user ) );
if (!$user_id || is_wp_error($user_id)) {
// failed in create user
$error = new WP_Error('incorrect_password', __( 'ERROR: Something is wrong in this site.', '4login-for-secure-and-smart-access') );
} else {
// register success.
// add user_meta that shows 'this user was made by fourlogin'.
add_user_meta( $user_id, 'made_by_fourlogin', 1 );
if ( 'none' != $fourlogin_options[FOURLOGIN_OPTION_SEND_NEW_USER_NOTICE_TO] ){
fourlogin_new_user_notification( $user_id, $fourlogin_options[FOURLOGIN_OPTION_SEND_NEW_USER_NOTICE_TO]);
}
$error = new WP_User($user_id);
}
} else {
//FOURLOGIN Authenticate failed
$error = new WP_Error(
'incorrect_password',
__('ERROR: Login failed. Considerable reasons are below.', '4login-for-secure-and-smart-access' )
);
$error -> add('incorrect_password', __('・Wrong user.', '4login-for-secure-and-smart-access') );
$error -> add('incorrect_password', __('・Invalid password.', '4login-for-secure-and-smart-access' ) );
$error -> add('incorrect_password', __('・Blocked by the login protection.', '4login-for-secure-and-smart-access') );
}
} else {
// user is not yet registered to WordPress and input is not email, so can't try authenticate in FOURLOGIN
$error = new WP_Error(
'incorrect_password',
__('ERROR: Invalid Password or Username. Input correctly.', '4login-for-secure-and-smart-access')
);
}
} else {
// user is already registered to WordPress but failed to login. so try authentification by FOURLOGIN
//get user by the input data(email or user_login).
$registered_wp_user = get_user_by('email', $username)? get_user_by('email', $username) : get_user_by('login', $username) ;
//try authentication by FOURLOGIN
$result = fourlogin_login_authenticate_server_api($registered_wp_user -> user_email, $password );
if ( is_wp_error($result) ) {
//failed in Connect to FOURLOGIN server
$error = new WP_Error('incorrect_password', __('ERROR: Could not connect to 4Login server.', '4login-for-secure-and-smart-access'));
if ( isset( $registered_wp_user->caps['administrator'] ) && true == $registered_wp_user->caps['administrator'] ) {
// Display the temporarily login link if the administrator's login.
$message = sprintf(
/* translators: %s: login URL with FOURLOGIN mode */
__('
Login without 4Login temporarily ?', '4login-for-secure-and-smart-access'),
fourlogin_get_login_url('templogin', $username)
);
} else {
$message = '';
}
$error->add('incorrect_password', $message);
} elseif ( $result ) {
//FOURLOGIN Auth Success
if ( ! get_user_meta( $registered_wp_user -> ID, 'made_by_fourlogin', true ) ){
//Existing user logged in with FourLogin, save that information as user_meta 3.
add_user_meta( $registered_wp_user -> ID, 'made_by_fourlogin', 3);
}
$error = $registered_wp_user;
} else {
//FOURLOGIN Auth failed
$error = new WP_Error(
'incorrect_password',
__('ERROR: Login failed. Considerable reasons are below.', '4login-for-secure-and-smart-access' )
);
$error -> add('incorrect_password', __('・Wrong user.', '4login-for-secure-and-smart-access') );
$error -> add('incorrect_password', __('・Invalid password.', '4login-for-secure-and-smart-access' ) );
$error -> add('incorrect_password', __('・Blocked by the login protection.', '4login-for-secure-and-smart-access') );
}
}
} else {
//WordPress login success
if ( isset($_POST['fourlogin_login_code']) ) {
//have one time login code
$result = fourlogin_compare_logincode($user->ID, sanitize_text_field(wp_unslash($_POST['fourlogin_login_code'])));
if ( !$result ){
$message = __('ERROR: Input the temporary login code correctly.', '4login-for-secure-and-smart-access');
}
} else {
//have no additional code. Check if allowed login with WordPress password.
if ( ! get_user_meta( $user->ID, 'made_by_fourlogin', true ) || 3 == get_user_meta( $user->ID, 'made_by_fourlogin', true ) ){
// if you do not have any roles, you cannot login with WordPress password
if ( !isset($user->roles[0]) ) {
$result = false;
$message = __( 'ERROR: You are not allowed login with WordPress password.', '4login-for-secure-and-smart-access' );
}
else {
$result = isset( $fourlogin_options[FOURLOGIN_OPTION_ALLOW_WP_LOGIN][$user->roles[0]])? true : false ;
if ( !$result ){
$message = __( 'ERROR: You are not allowed login with WordPress password.', '4login-for-secure-and-smart-access' );
if ( isset( $user->caps['administrator'] ) && true == $user->caps['administrator'] ){
$message .= sprintf(
/* translators: %s: login URL with FOURLOGIN mode */
__( '
Login without 4Login temporarily ?', '4login-for-secure-and-smart-access' ),
fourlogin_get_login_url( 'templogin', $username )
);
}
}
}
} else {
//Usually, users who were made by FOURLOGIN can not log in successfully with WordPress password.
//And we do not allow such kinds of login.
$result = false;
$message = __( 'ERROR: You are not allowed login with WordPress password.', '4login-for-secure-and-smart-access' );
}
}
if ( $result ) {
// Authenticate success in ether way
$error = $user;
} else {
// Authenticate failed.
$error = new WP_Error(
'incorrect_password',
$message
);
}
}
if ( !is_wp_error( $error ) && isset( $_POST['fourlogin_memberCard'] ) ){
//if you want to use user_data for member card, use this function.
fourlogin_show_member_card( $error );
exit;
}
if ( !is_wp_error( $error ) && 1 == get_user_meta( $error->ID, 'made_by_fourlogin', true ) && !empty( $fourlogin_options[FOURLOGIN_OPTION_DEFAULT_ROLE] ) ){
//If user is made by FOURLOGIN and not yet registered username, redirect to "profile" page in order to change "user_login".
$_REQUEST['redirect_to'] = admin_url('profile.php');
add_filter( 'login_redirect', 'fourlogin_redirect_to', 10, 2 );
} elseif ( !is_wp_error( $error ) && isset( $_POST['fourlogin_redirect_to'] ) ){
//If you want to guide users who used FourLogin slot login to an objective page, please set it in FourLogin members site.
$_REQUEST['redirect_to'] = sanitize_url(wp_unslash($_POST['fourlogin_redirect_to']));
add_filter( 'login_redirect', 'fourlogin_redirect_to', 10, 2 );
}
if ( is_multisite() ){
//user is exists and authenticate successfully and new to the blog.
if ( ! is_wp_error( $error ) ){
$id = get_current_blog_id();
if ( ! is_user_member_of_blog( $error->ID, $id ) ){
//check if FOURLOGIN may create new user.
if ( 'yes' == $fourlogin_options[FOURLOGIN_OPTION_DONT_CREATE_USER] ){
$error = new WP_Error( 'incorrect_password', __('ERROR: This site is only for members.', '4login-for-secure-and-smart-access'));
return $error;
}
add_user_to_blog( $id, $error->ID, $fourlogin_options[FOURLOGIN_OPTION_DEFAULT_ROLE] );
if ( 'none' != $fourlogin_options[FOURLOGIN_OPTION_SEND_NEW_USER_NOTICE_TO] ){
fourlogin_new_user_notification( $error->ID, $fourlogin_options[FOURLOGIN_OPTION_SEND_NEW_USER_NOTICE_TO]);
}
}
}
}
return $error;
}
/**
* If "redirect_to" was posted, set redirect_to.
* @param string $redirect_to
* @param string $requested_redirect_to
*/
function fourlogin_redirect_to( $redirect_to, $requested_redirect_to ){
if( ! empty( $requested_redirect_to ) ){
return $requested_redirect_to;
}
return $redirect_to;
}
add_filter( 'login_redirect', 'fourlogin_redirect_to', 10, 2 );
/**
* This is the method that send an email with the Login Code to user
*
* @param $username the user name, it was input by user
*/
function fourlogin_send_login_code( $username ) {
$user = get_user_by( 'login', $username )? get_user_by( 'login', $username ):get_user_by( 'email', $username );
if ( isset( $user->caps['administrator'] ) && true == $user->caps['administrator'] ){
// Create a login code for login temporarily, and save it to user meta
fourlogin_create_logincode( $user->ID );
} else {
//Usually not called. This method is for the administrator. So if not admin, redirect to usual login form.
wp_safe_redirect( wp_login_url() );
exit;
}
$args = is_array( get_user_meta( $user->ID, "fourlogin_login_code", true ) )? get_user_meta( $user->ID, "fourlogin_login_code", true ): array();
if ( isset($args['email_sent']) && false == $args['email_sent'] || !isset( $args['email_sent'] ) ) {
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
// Make the message
$message = __('A temporary login code was sent to your email address for login without 4Login.', '4login-for-secure-and-smart-access')."\r\n\r\n";
$message .= sprintf(
/* translators: %s: Your temporarily login code */
__('Your temporarily login code is "%s".', '4login-for-secure-and-smart-access'), $args['login_code']
)."\r\n";
// Update the email_sent flag of user meta by email send result
$args['email_sent'] = wp_mail(
$user->data->user_email,
sprintf(
/* translators: %s: The title of your site */
__('[%s] Your temporarily login code', '4login-for-secure-and-smart-access'),
$blogname
),
$message
);
update_user_meta( $user->ID, "fourlogin_login_code", $args );
}
}
/**
* This is the method that authenticate by FOURLOGIN server API
*
* @param $username
* the user name, it was input by user
* @param $register
* is it register or not
* @return authentication result
* WP_Error: server error
* true: success
* false: failed
*/
function fourlogin_login_authenticate_server_api($username, $password ) {
$fourlogin_options = fourlogin_options_get();
$result = fourlogin_login_authenticate_post(
$fourlogin_options[FOURLOGIN_OPTIONS_SERVER_API_URI],
$username,
$password,
$fourlogin_options[FOURLOGIN_APP_SERVICE_ID] );
return $result;
}
/**
* This is the method that post the authenitication info to FOURLOGIN server API
*
* @param $url
* URL to FOURLOGIN server API
* @param $username
* user name
* @param $password
* password input by user
* @return authentication result
* WP_Error: server error
* true: success
* false: failed
*/
function fourlogin_login_authenticate_post($url, $username, $password, $id) {
$response = wp_remote_post(
$url,
array(
'sslverify' => false,
'body' => array( 'app_service_id' => $id,
'email' => $username,
'password' => $password,
'response_format'=>'json',
),
)
);
if ( is_wp_error( $response ) ) {
// failed in wp_remote_post
return $response;
} else {
// wp_remote_post success
return fourlogin_login_response_purse( $response['body'] );
}
}
/**
* if you want to use user_data for member card, use this function.
* @param WP_User $user
* @return information about user.as a member card.
*/
function fourlogin_show_member_card( $user ){
if ( apply_filters( 'fourlogin_show_member_card', true, $user ) ) {
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$message = sprintf(
/* translators: %s: blog name */
esc_html__("You are a member of %s", '4login-for-secure-and-smart-access'),
"" . $blogname . "
"
);
echo wp_kses_post($message);
echo ( esc_html($user->display_name) . '
' );
echo ( 'Your role is ' . esc_html($user->roles[0]) . '
' );
echo ( 'You registered on ' . esc_html($user->user_registered) . '
' );
}
}
/**
* Define for FOURLOGIN server API response "code"
*/
define('FOURLOGIN_SVRAPI_RESPCODE_SUCCESS', '000');
/**
* This is the method that purse the response from FOURLOGIN server API
*
* @param body
* response body data (usually json)
* @return authentication result
* true: success
* false: failed
*/
function fourlogin_login_response_purse($body) {
$elements = json_decode( $body );
if ( isset( $elements->code ) ){
switch ( $elements->code ) {
case FOURLOGIN_SVRAPI_RESPCODE_SUCCESS:
$result = true;
break;
default:
$result = false;
break;
}
} else {
$result = false;
}
return $result;
}
/**
* This is the method that generate a password by FOURLOGIN server API
*
* @return a password, it was generated by FOURLOGIN
*/
function fourlogin_login_generate_password() {
$password = wp_generate_password( 8, false );
return $password;
}
/**
* Get the login URL with FOURLOGIN mode
*
* @param $fourlogin_mode
* FOURLOGIN mode
* @param $username
* user name
* @return the login URL with FOURLOGIN mode
*/
function fourlogin_get_login_url($fourlogin_mode = '', $username = '') {
$nonce = wp_create_nonce('fourlogin_mode_nonce');
$args = array(
'fourloginmode' => $fourlogin_mode,
'log' => $username,
'fourloginmode_nonce' => $nonce
);
$login_url = add_query_arg($args, wp_login_url());
return $login_url;
}
/**
* Create a logincode, save it to user meta, and sent it to user by email
*
* @param $user_id
* user ID
*/
function fourlogin_create_logincode( $user_id ) {
// Generate a temp password
$code = fourlogin_login_generate_password();
$args = is_array( get_user_meta( $user_id, 'fourlogin_login_code', true) )? get_user_meta( $user_id, 'fourlogin_login_code', true) : array();
if ( !isset( $args['email_sent'] ) || false == $args['email_sent'] ){
$args['login_code'] = $code;
// Save the logincode to user meta
if ( update_user_meta( $user_id, "fourlogin_login_code", $args ) != true) {
add_user_meta( $user_id, "fourlogin_login_code", $args );
}
}
}
/**
* This is the method that compare the logincode
*
* @param $user_id
* user ID
* @param $logincode
* temporal login code
* @return compare result(if equal, return true)
*/
function fourlogin_compare_logincode($user_id, $logincode) {
if ($logincode == ''){
return false;
}
$args = get_user_meta( $user_id, "fourlogin_login_code", true );
delete_user_meta( $user_id, "fourlogin_login_code" );
return ( strcmp($args['login_code'], $logincode ) == 0 );
}
function fourlogin_new_user_notification_message($user,$blogname){
$message = sprintf(
/* translators: %s: The title of your site */
__( 'New user registration on your site %s:', '4login-for-secure-and-smart-access' ), $blogname
) . "\r\n\r\n";
$message .= sprintf(
/* translators: %s: Your username */
__( 'Username: %s', '4login-for-secure-and-smart-access' ), $user->user_login
) . "\r\n\r\n";
$message .= sprintf(
/* translators: %s: Your email */
__( 'Email: %s', '4login-for-secure-and-smart-access' ), $user->user_email
) . "\r\n\r\n";
$message .= __( 'This user was created by 4Login', '4login-for-secure-and-smart-access' ) . "\r\n";
return $message;
}
/**
* Email to a newly-registered user.
* A new user registration notification is also sent to admin email.
*
* @param int $user_id User ID.
* @param string $notify Optional. Type of notification that should happen. Accepts 'admin' or an empty
* string (admin only), 'user', or 'both' (admin and user). Default empty.
*/
function fourlogin_new_user_notification( $user_id, $notify = '' ) {
$switched_locale = switch_to_locale( get_locale() );
// The blogname option is escaped with esc_html on the way into the database in sanitize_option
// we want to reverse this for the plain text arena of emails.
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
//get general user's information(email)
$target_users = get_users();
//get new user's information
$new_user = get_userdata( $user_id );
//roles
$target_role_users = array('subscriber', 'contributor', 'author', 'editor');
//message subject
$message_subject = sprintf(/* translators: %s: The title of your site */
__( '[%s] New User Registration', '4login-for-secure-and-smart-access' )
, $blogname
);
//message content
$notification_message = fourlogin_new_user_notification_message($new_user,$blogname);
//For both
if ( 'both' === $notify) {
foreach ( $target_users as $both_user ) {
wp_mail( $both_user->user_email, $message_subject, $notification_message);
}
if ( $switched_locale ) {
restore_previous_locale();
}
}
//For admin
elseif ( 'admin' === $notify ) {
foreach ( $target_users as $admin ) {
// check if user has administrator role
if ( in_array('administrator',$admin->roles, true)) {
wp_mail( $admin->user_email, $message_subject, $notification_message );
}
}
if ( $switched_locale ) {
restore_previous_locale();
}
}
//For user
elseif("user" === $notify){
foreach ( $target_users as $general_user ) {
// check if user has user role
if ( array_intersect( $target_role_users, $general_user->roles) ) {
wp_mail( $general_user->user_email, $message_subject, $notification_message );
}
}
if ( $switched_locale ) {
restore_previous_locale();
}
}
$message = sprintf(
/* translators: %s: The title of your site */
__( 'You are successfully registered to the site %s.', '4login-for-secure-and-smart-access' ), $blogname
) . "\r\n\r\n";
$message .= wp_login_url() . "\r\n\r\n";
wp_mail( $new_user->user_email, sprintf(
/* translators: %s: The title of your site */
__( '[%s] New User Registration', '4login-for-secure-and-smart-access' ),
$blogname ),
apply_filters( 'fourlogin_new_user_noticemail', $message )
);
}
/**
* Check if a user is made by FourLogin when the user want to make new password.
* This plugin do not let to remake password if the user is made by this plugin.
* As the password can be a cause of password hacking.
*
* @param WP_Error $errors
* @return WP_Error || dont return If error occured, return WP_Error. Else do nothing.
*/
function fourlogin_dont_let_lostpassword( $errors ){
if ( $errors->get_error_code() ){
return $errors;
}
if (isset($_POST["user_login_nonce"])) {
$nonce = sanitize_key(wp_unslash($_POST["user_login_nonce"]));
if ( isset($_POST['user_login']) && wp_verify_nonce($nonce, "user_login_action")){
$user = get_user_by( 'email', trim( sanitize_user(wp_unslash( $_POST['user_login'] )) ) );
if ( ! $user ){
get_user_by('login', trim( sanitize_user(wp_unslash( $_POST['user_login'] )) ) );
}
}
}
else {
$user = false;
}
$made_by_fourlogin = get_user_meta( $user->ID, 'made_by_fourlogin', true );
if ( empty($made_by_fourlogin) || 3 === $made_by_fourlogin ){
} else {
return $errors->add( 'invalidcombo', __( 'ERROR: You are not allowed login with WordPress password.', '4login-for-secure-and-smart-access' ));
}
}
add_action( 'lostpassword_post', 'fourlogin_dont_let_lostpassword' );