Пример #1
0
<?php

require_once dirname(dirname(__FILE__)) . '/constants.php';
require_once dirname(__FILE__) . '/facebook.php';
require_once dirname(dirname(__FILE__)) . '/utils.php';
$client_id = get_option('social_connect_facebook_api_key');
$secret_key = get_option('social_connect_facebook_secret_key');
if (isset($_GET['code'])) {
    $code = $_GET['code'];
    parse_str(sc_http_get_contents("https://graph.facebook.com/oauth/access_token?" . 'client_id=' . $client_id . '&redirect_uri=' . home_url('index.php?social-connect=facebook-callback') . '&client_secret=' . $secret_key . '&code=' . urlencode($code)));
    $signature = social_connect_generate_signature($access_token);
    do_action('social_connect_before_register_facebook', $code, $signature, $access_token);
    ?>
	<html>
		<head>
			<script>
				function init() {
					window.opener.wp_social_connect({'action' : 'social_connect', 'social_connect_provider' : 'facebook',
						'social_connect_signature' : '<?php 
    echo $signature;
    ?>
',
						'social_connect_access_token' : '<?php 
    echo $access_token;
    ?>
'});

					window.close();
				}
			</script>
		</head>
function sc_social_connect_process_login($is_ajax = false)
{
    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);
        }
    } else {
        $redirect_to = admin_url();
    }
    $redirect_to = apply_filters('social_connect_redirect_to', $redirect_to);
    $social_connect_provider = $_REQUEST['social_connect_provider'];
    $sc_provider_identity_key = 'social_connect_' . $social_connect_provider . '_id';
    $sc_provided_signature = $_REQUEST['social_connect_signature'];
    switch ($social_connect_provider) {
        case 'facebook':
            social_connect_verify_signature($_REQUEST['social_connect_access_token'], $sc_provided_signature, $redirect_to);
            $fb_json = json_decode(sc_http_get_contents("https://graph.facebook.com/me?access_token=" . $_REQUEST['social_connect_access_token']));
            $sc_provider_identity = $fb_json->{'id'};
            $sc_email = $fb_json->{'email'};
            $sc_first_name = $fb_json->{'first_name'};
            $sc_last_name = $fb_json->{'last_name'};
            $sc_profile_url = $fb_json->{'link'};
            $sc_name = $sc_first_name . ' ' . $sc_last_name;
            $user_login = strtolower(str_replace(' ', '', $sc_first_name . $sc_last_name));
            break;
        case 'twitter':
            $sc_provider_identity = $_REQUEST['social_connect_twitter_identity'];
            social_connect_verify_signature($sc_provider_identity, $sc_provided_signature, $redirect_to);
            $sc_name = $_REQUEST['social_connect_name'];
            $names = explode(' ', $sc_name, 2);
            $sc_first_name = $names[0];
            $sc_last_name = $names[1];
            $sc_screen_name = $_REQUEST['social_connect_screen_name'];
            $sc_avatar = isset($_REQUEST['social_connect_avatar']) ? str_replace('http:', '', $_REQUEST['social_connect_avatar']) : '';
            $sc_profile_url = '';
            // Get host name from URL
            $site_url = parse_url(site_url());
            $sc_email = 'tw_' . md5($sc_provider_identity) . '@' . $site_url['host'];
            $user_login = $sc_screen_name;
            break;
        case 'google':
            $sc_provider_identity = $_REQUEST['social_connect_openid_identity'];
            social_connect_verify_signature($sc_provider_identity, $sc_provided_signature, $redirect_to);
            $sc_email = $_REQUEST['social_connect_email'];
            $sc_first_name = $_REQUEST['social_connect_first_name'];
            $sc_last_name = $_REQUEST['social_connect_last_name'];
            $sc_profile_url = '';
            $sc_name = $sc_first_name . ' ' . $sc_last_name;
            $user_login = strtolower(str_replace(' ', '', $sc_first_name . $sc_last_name));
            break;
        case 'google-plus':
            $sc_provider_identity = $_REQUEST['social_connect_google_id'];
            social_connect_verify_signature($sc_provider_identity, $sc_provided_signature, $redirect_to);
            $sc_email = $_REQUEST['social_connect_email'];
            $sc_first_name = $_REQUEST['social_connect_first_name'];
            $sc_last_name = $_REQUEST['social_connect_last_name'];
            $sc_profile_url = $_REQUEST['social_connect_profile_url'];
            $user_login = strtolower($sc_first_name . $sc_last_name);
            break;
        case 'yahoo':
            $sc_provider_identity = $_REQUEST['social_connect_openid_identity'];
            social_connect_verify_signature($sc_provider_identity, $sc_provided_signature, $redirect_to);
            $sc_email = $_REQUEST['social_connect_email'];
            $sc_name = $_REQUEST['social_connect_name'];
            $sc_username = $_REQUEST['social_connect_username'];
            $sc_profile_url = '';
            if ($sc_name == '') {
                if ($sc_username == '') {
                    $names = explode("@", $sc_email);
                    $sc_name = $names[0];
                    $sc_first_name = $sc_name;
                    $sc_last_name = '';
                } else {
                    $names = explode(' ', $sc_username, 2);
                    $sc_first_name = $names[0];
                    $sc_last_name = $names[1];
                }
            } else {
                $names = explode(' ', $sc_name, 2);
                $sc_first_name = $names[0];
                $sc_last_name = $names[1];
            }
            $user_login = strtolower(str_replace(' ', '', $sc_first_name . $sc_last_name));
            break;
        case 'wordpress':
            $sc_provider_identity = $_REQUEST['social_connect_openid_identity'];
            social_connect_verify_signature($sc_provider_identity, $sc_provided_signature, $redirect_to);
            $sc_email = $_REQUEST['social_connect_email'];
            $sc_name = $_REQUEST['social_connect_name'];
            $sc_profile_url = '';
            if (trim($sc_name) == '') {
                $names = explode("@", $sc_email);
                $sc_name = $names[0];
                $sc_first_name = $sc_name;
                $sc_last_name = '';
            } else {
                $names = explode(' ', $sc_name, 2);
                $sc_first_name = $names[0];
                $sc_last_name = $names[1];
            }
            $user_login = strtolower(str_replace(' ', '', $sc_first_name . $sc_last_name));
            break;
        default:
            break;
    }
    // Cookies used to display welcome message if already signed in recently using some provider
    setcookie("social_connect_current_provider", $social_connect_provider, time() + 3600, SITECOOKIEPATH, COOKIE_DOMAIN, false, true);
    // Get user by meta
    $user_id = social_connect_get_user_by_meta($sc_provider_identity_key, $sc_provider_identity);
    if ($user_id) {
        $user_data = get_userdata($user_id);
        $user_login = $user_data->user_login;
    } elseif ($user_id = email_exists($sc_email)) {
        // User not found by provider identity, check by email
        update_user_meta($user_id, $sc_provider_identity_key, $sc_provider_identity);
        $user_data = get_userdata($user_id);
        $user_login = $user_data->user_login;
    } else {
        // Create new user and associate provider identity
        if (get_option('users_can_register')) {
            $user_login = sc_get_unique_username($user_login);
            $userdata = array('user_login' => $user_login, 'user_email' => $sc_email, 'first_name' => $sc_first_name, 'last_name' => $sc_last_name, 'user_url' => $sc_profile_url, 'user_pass' => wp_generate_password());
            // Create a new user
            $user_id = wp_insert_user(apply_filters('social_connect_insert_user', $userdata));
            if ($user_id && is_integer($user_id)) {
                update_user_meta($user_id, $sc_provider_identity_key, $sc_provider_identity);
            }
            if (isset($sc_avatar) && $sc_avatar) {
                update_user_meta($user_id, 'social_connect_twitter_avatar', $sc_avatar);
            }
            do_action('social_connect_inserted_user', $user_id, $social_connect_provider);
        } else {
            add_filter('wp_login_errors', 'sc_login_errors');
            return;
        }
    }
    wp_set_auth_cookie($user_id);
    do_action('social_connect_login', $user_login);
    if ($is_ajax) {
        echo '{"redirect":"' . $redirect_to . '"}';
    } else {
        wp_safe_redirect($redirect_to);
    }
    exit;
}