Ejemplo n.º 1
0
function wsl_process_login()
{
    if (!isset($_REQUEST['action']) || $_REQUEST['action'] != "wordpress_social_login") {
        return;
    }
    if (isset($_REQUEST['redirect_to']) && $_REQUEST['redirect_to'] != '') {
        $redirect_to = $_REQUEST['redirect_to'];
        // Redirect to https if user wants ssl
        if (isset($secure_cookie) && $secure_cookie && false !== strpos($redirect_to, 'wp-admin')) {
            $redirect_to = preg_replace('|^http://|', 'https://', $redirect_to);
        }
        if (strpos($redirect_to, 'wp-admin')) {
            $redirect_to = get_option('wsl_settings_redirect_url');
        }
    }
    if (empty($redirect_to)) {
        $redirect_to = get_option('wsl_settings_redirect_url');
    }
    if (empty($redirect_to)) {
        $redirect_to = site_url();
    }
    try {
        // load hybridauth
        require_once dirname(__FILE__) . "/../hybridauth/Hybrid/Auth.php";
        // selected provider name
        $provider = @trim(strip_tags($_REQUEST["provider"]));
        // build required configuratoin for this provider
        if (!get_option('wsl_settings_' . $provider . '_enabled')) {
            throw new Exception('Unknown or disabled provider');
        }
        $config = array();
        $config["base_url"] = plugins_url() . '/' . basename(dirname(__FILE__)) . '/hybridauth/';
        $config["providers"] = array();
        $config["providers"][$provider] = array();
        $config["providers"][$provider]["enabled"] = true;
        // provider application id ?
        if (get_option('wsl_settings_' . $provider . '_app_id')) {
            $config["providers"][$provider]["keys"]["id"] = get_option('wsl_settings_' . $provider . '_app_id');
        }
        // provider application key ?
        if (get_option('wsl_settings_' . $provider . '_app_key')) {
            $config["providers"][$provider]["keys"]["key"] = get_option('wsl_settings_' . $provider . '_app_key');
        }
        // provider application secret ?
        if (get_option('wsl_settings_' . $provider . '_app_secret')) {
            $config["providers"][$provider]["keys"]["secret"] = get_option('wsl_settings_' . $provider . '_app_secret');
        }
        // create an instance for Hybridauth
        $hybridauth = new Hybrid_Auth($config);
        // try to authenticate the selected $provider
        if ($hybridauth->isConnectedWith($provider)) {
            $adapter = $hybridauth->getAdapter($provider);
            $hybridauth_user_profile = $adapter->getUserProfile();
        } else {
            throw new Exception('User not connected with ' . $provider . '!');
        }
        $user_email = $hybridauth_user_profile->email;
    } catch (Exception $e) {
        die("Unspecified error. #" . $e->getCode());
    }
    $user_id = null;
    // if the user email is verified, then try to map to legacy account if exist
    // > Currently only Facebook, Google, Yahaoo and Foursquare do provide the verified user email.
    if (!empty($hybridauth_user_profile->emailVerified)) {
        $user_id = (int) email_exists($hybridauth_user_profile->emailVerified);
    }
    // try to get user by meta if not
    if (!$user_id) {
        $user_id = (int) wsl_get_user_by_meta($provider, $hybridauth_user_profile->identifier);
    }
    // if user found
    if ($user_id) {
        $user_data = get_userdata($user_id);
        $user_login = $user_data->user_login;
    } else {
        // generate a valid user login
        $user_login = trim(str_replace(' ', '_', strtolower($hybridauth_user_profile->displayName)));
        if (empty($user_login)) {
            $user_login = $hybridauth_user_profile->lastName . " " . $hybridauth_user_profile->firstName;
        }
        // user name should be unique
        if (username_exists($user_login)) {
            $i = 1;
            $user_login_tmp = $user_login;
            do {
                $user_login_tmp = $user_login . "_" . $i++;
            } while (username_exists($user_login_tmp));
            $user_login = $user_login_tmp;
        }
        // generate an email if none
        if (!isset($user_email) or !is_email($user_email)) {
            $user_email = strtolower($provider . "_user_" . $user_login) . "@example.com";
        }
        // email should be unique
        if (email_exists($user_email)) {
            do {
                $user_email = md5(uniqid(wp_rand(10000, 99000))) . "@example.com";
            } while (email_exists($user_email));
        }
        $user_login = sanitize_user($user_login, true);
        if (!validate_username($user_login)) {
            $user_login = strtolower($provider) . "_user_" . md5($hybridauth_user_profile->identifier);
        }
        $userdata = array('user_login' => $user_login, 'user_email' => $user_email, 'first_name' => $hybridauth_user_profile->firstName, 'last_name' => $hybridauth_user_profile->lastName, 'display_name' => !empty($hybridauth_user_profile->displayName) ? $hybridauth_user_profile->displayName : $user_login, 'user_url' => $hybridauth_user_profile->profileURL, 'description' => $hybridauth_user_profile->description, 'user_pass' => wp_generate_password());
        // Create a new user
        $user_id = wp_insert_user($userdata);
        // Send notifications
        if (get_option('wsl_settings_users_notification')) {
            if (get_option('wsl_settings_users_notification') == 1) {
                wsl_admin_notification($user_id, $provider);
            }
        }
        // update user metadata
        if ($user_id && is_integer($user_id)) {
            update_user_meta($user_id, $provider, $hybridauth_user_profile->identifier);
        } else {
            if (is_wp_error($user_id)) {
                //- http://wordpress.org/support/topic/plugin-wordpress-social-login-error-with-vkontake-provider?replies=1#post-2796109
                echo $user_id->get_error_message();
            } else {
                die("An error occurred while creating a new user!");
            }
        }
    }
    $user_age = $hybridauth_user_profile->age;
    // not that precise you say... well welcome to my world
    if (!$user_age && (int) $hybridauth_user_profile->birthYear) {
        $user_age = (int) date("Y") - (int) $hybridauth_user_profile->birthYear;
    }
    update_user_meta($user_id, 'wsl_user', $provider);
    update_user_meta($user_id, 'wsl_user_gender', $hybridauth_user_profile->gender);
    update_user_meta($user_id, 'wsl_user_age', $user_age);
    update_user_meta($user_id, 'wsl_user_image', $hybridauth_user_profile->photoURL);
    wp_set_auth_cookie($user_id);
    wp_safe_redirect($redirect_to);
    exit;
}
/**
* Create a new wordpress user
*
* Ref: http://codex.wordpress.org/Function_Reference/wp_insert_user
*/
function wsl_process_login_create_wp_user($provider, $hybridauth_user_profile, $requested_user_login, $requested_user_email)
{
    // HOOKABLE:
    do_action("wsl_process_login_create_wp_user_start", $provider, $hybridauth_user_profile, $requested_user_login, $requested_user_email);
    $user_login = '';
    $user_email = '';
    // if coming from "complete registration form"
    if ($requested_user_login) {
        $user_login = $requested_user_login;
    }
    if ($requested_user_email) {
        $user_email = $requested_user_email;
    }
    if (!$user_email) {
        $user_email = $hybridauth_user_profile->email;
    }
    // Verify that an email address has been given to us. Whether it's unique will be verified later
    // by the Wordpress core, during the wp_insert_user() call below.
    if (!isset($user_email) or !is_email($user_email)) {
        return wsl_process_login_render_notice_page(_wsl__('A valid email is required to connect this website', 'wordpress-social-login'));
    }
    if (!$user_login) {
        // attempt to generate user_login from hybridauth user profile display name
        $user_login = $hybridauth_user_profile->displayName;
        // sanitize user login
        $user_login = sanitize_user($user_login, true);
        // remove spaces and dots
        $user_login = trim(str_replace(array(' ', '.'), '_', $user_login));
        $user_login = trim(str_replace('__', '_', $user_login));
        // if user profile display name is not provided
        if (empty($user_login)) {
            $user_login = sanitize_user(current(explode('@', $user_email)), true);
        }
        // user name should be unique
        if (username_exists($user_login)) {
            $i = 1;
            $user_login_tmp = $user_login;
            do {
                $user_login_tmp = $user_login . "_" . $i++;
            } while (username_exists($user_login_tmp));
            $user_login = $user_login_tmp;
        }
    }
    $display_name = $hybridauth_user_profile->displayName;
    if (empty($display_name)) {
        $display_name = $hybridauth_user_profile->firstName;
    }
    if (empty($display_name)) {
        $display_name = strtolower($provider) . "_user";
    }
    $userdata = array('user_login' => $user_login, 'user_email' => $user_email, 'display_name' => $display_name, 'first_name' => $hybridauth_user_profile->firstName, 'last_name' => $hybridauth_user_profile->lastName, 'user_url' => $hybridauth_user_profile->profileURL, 'description' => $hybridauth_user_profile->description, 'user_pass' => wp_generate_password());
    // Bouncer::Membership level
    $wsl_settings_bouncer_new_users_membership_default_role = get_option('wsl_settings_bouncer_new_users_membership_default_role');
    // if level eq "default", we set role to wp default user role
    if ($wsl_settings_bouncer_new_users_membership_default_role == "default") {
        $userdata['role'] = get_option('default_role');
    } elseif ($wsl_settings_bouncer_new_users_membership_default_role && $wsl_settings_bouncer_new_users_membership_default_role != 'wslnorole') {
        $userdata['role'] = $wsl_settings_bouncer_new_users_membership_default_role;
    }
    // Bouncer::User Moderation
    // > if Bouncer::User Moderation is enabled (Yield to Theme My Login), then we overwrite the user role to 'pending'
    # http://www.jfarthing.com/development/theme-my-login/user-moderation/
    if (get_option('wsl_settings_bouncer_new_users_moderation_level') > 100) {
        $userdata['role'] = "pending";
    }
    // HOOKABLE: change the user data
    $userdata = apply_filters('wsl_hook_process_login_alter_wp_insert_user_data', $userdata, $provider, $hybridauth_user_profile);
    // DEPRECIATED: as of 2.2.3
    // $userdata = apply_filters( 'wsl_hook_process_login_alter_userdata', $userdata, $provider, $hybridauth_user_profile );
    // HOOKABLE: This action runs just before creating a new wordpress user.
    do_action('wsl_hook_process_login_before_wp_insert_user', $userdata, $provider, $hybridauth_user_profile);
    // DEPRECIATED: as of 2.2.3
    // do_action( 'wsl_hook_process_login_before_insert_user', $userdata, $provider, $hybridauth_user_profile );
    // HOOKABLE: This action runs just before creating a new wordpress user, it delegate user insert to a custom function.
    $user_id = apply_filters('wsl_hook_process_login_delegate_wp_insert_user', $userdata, $provider, $hybridauth_user_profile);
    // Create a new WordPress user
    if (!$user_id || !is_integer($user_id)) {
        $user_id = wp_insert_user($userdata);
    }
    // do not continue without user_id
    if (!$user_id || !is_integer($user_id)) {
        if (is_wp_error($user_id)) {
            return wsl_process_login_render_notice_page(_wsl__("An error occurred while creating a new user: "******"An error occurred while creating a new user!", 'wordpress-social-login'));
    }
    // wp_insert_user may fail on first and last name meta, expliciting setting to correct.
    update_user_meta($user_id, 'first_name', apply_filters('pre_user_first_name', $userdata['first_name']));
    update_user_meta($user_id, 'last_name', apply_filters('pre_user_last_name', $userdata['last_name']));
    // Send notifications
    if (get_option('wsl_settings_users_notification') == 1) {
        wsl_admin_notification($user_id, $provider);
    }
    // HOOKABLE: This action runs just after a wordpress user has been created
    // > Note: At this point, the user has been added to wordpress database, but NOT CONNECTED.
    do_action('wsl_hook_process_login_after_wp_insert_user', $user_id, $provider, $hybridauth_user_profile);
    // DEPRECIATED: as of 2.2.3
    // do_action( 'wsl_hook_process_login_after_create_wp_user', $user_id, $provider, $hybridauth_user_profile );
    // returns the user created user id
    return $user_id;
}
Ejemplo n.º 3
0
function wsl_process_login_create_wp_user($provider, $hybridauth_user_profile, $request_user_login, $request_user_email)
{
    // HOOKABLE: any action to fire right before a user created on database
    do_action('wsl_hook_process_login_before_create_wp_user');
    $user_login = null;
    $user_email = null;
    // if coming from "complete registration form"
    if ($request_user_email && $request_user_login) {
        $user_login = $request_user_login;
        $user_email = $request_user_email;
    } else {
        // generate a valid user login
        $user_login = trim(str_replace(' ', '_', strtolower($hybridauth_user_profile->displayName)));
        $user_email = $hybridauth_user_profile->email;
        if (empty($user_login)) {
            $user_login = trim($hybridauth_user_profile->lastName . " " . $hybridauth_user_profile->firstName);
        }
        if (empty($user_login)) {
            $user_login = strtolower($provider) . "_user_" . md5($hybridauth_user_profile->identifier);
        }
        // user name should be unique
        if (username_exists($user_login)) {
            $i = 1;
            $user_login_tmp = $user_login;
            do {
                $user_login_tmp = $user_login . "_" . $i++;
            } while (username_exists($user_login_tmp));
            $user_login = $user_login_tmp;
        }
        // generate an email if none
        if (!isset($user_email) or !is_email($user_email)) {
            $user_email = strtolower($provider . "_user_" . $user_login) . "@example.com";
        }
        // email should be unique
        if (email_exists($user_email)) {
            do {
                $user_email = md5(uniqid(wp_rand(10000, 99000))) . "@example.com";
            } while (email_exists($user_email));
        }
        $user_login = sanitize_user($user_login, true);
        if (!validate_username($user_login)) {
            $user_login = strtolower($provider) . "_user_" . md5($hybridauth_user_profile->identifier);
        }
    }
    $display_name = $hybridauth_user_profile->displayName;
    if ($request_user_login || empty($display_name)) {
        $display_name = $user_login;
    }
    $userdata = array('user_login' => $user_login, 'user_email' => $user_email, 'display_name' => $display_name, 'first_name' => $hybridauth_user_profile->firstName, 'last_name' => $hybridauth_user_profile->lastName, 'user_url' => $hybridauth_user_profile->profileURL, 'description' => $hybridauth_user_profile->description, 'user_pass' => wp_generate_password());
    // Bouncer :: Membership level
    if (get_option('wsl_settings_bouncer_new_users_membership_default_role') != "default") {
        $userdata['role'] = get_option('wsl_settings_bouncer_new_users_membership_default_role');
    }
    // Bouncer :: User Moderation : None
    if (get_option('wsl_settings_bouncer_new_users_moderation_level') == 1) {
        // well do nothing..
    }
    // Bouncer :: User Moderation : Yield to Theme My Login plugin
    if (get_option('wsl_settings_bouncer_new_users_moderation_level') > 100) {
        $userdata['role'] = "pending";
    }
    // HOOKABLE: change the user data
    if (apply_filters('wsl_hook_process_login_alter_userdata', $userdata, $provider, $hybridauth_user_profile)) {
        $userdata = apply_filters('wsl_hook_process_login_alter_userdata', $userdata, $provider, $hybridauth_user_profile);
    }
    // HOOKABLE: any action to fire right before a user created on database
    do_action('wsl_hook_process_login_before_insert_user', $userdata, $provider, $hybridauth_user_profile);
    // HOOKABLE: delegate user insert to a custom function
    $user_id = apply_filters('wsl_hook_process_login_alter_insert_user', $userdata, $provider, $hybridauth_user_profile);
    // Create a new user
    if (!$user_id || !is_integer($user_id)) {
        $user_id = wp_insert_user($userdata);
    }
    // update user metadata
    if ($user_id && is_integer($user_id)) {
        update_user_meta($user_id, $provider, $hybridauth_user_profile->identifier);
    } else {
        if (is_wp_error($user_id)) {
            echo $user_id->get_error_message();
        } else {
            return wsl_render_notices_pages(_wsl__("An error occurred while creating a new user!", 'wordpress-social-login'));
        }
    }
    // Send notifications
    if (get_option('wsl_settings_users_notification') == 1) {
        wsl_admin_notification($user_id, $provider);
    }
    // HOOKABLE: any action to fire right after a user created on database
    do_action('wsl_hook_process_login_after_create_wp_user', $user_id, $provider, $hybridauth_user_profile);
    return array($user_id, $user_login, $user_email);
}