function dln_social_login_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_login_redirect_to', $redirect_to); $social_login_provider = $_REQUEST['social_login_provider']; $dln_provider_identity_key = 'social_login_' . $social_login_provider . '_id'; $dln_provided_signature = $_REQUEST['social_login_signature']; switch ($social_login_provider) { case 'facebook': $user_login = $username = $dln_provider_identity = $dln_provider_identity_key = $dln_email = $dln_first_name = $dln_last_name = $dln_profile_url = ''; social_login_verify_signature($_REQUEST['social_login_access_token'], $dln_provided_signature, $redirect_to); $fb_json = json_decode(dln_curl_get_contents("https://graph.facebook.com/me?access_token=" . $_REQUEST['social_login_access_token'])); if (isset($fb_json->{'id'})) { $dln_provider_identity = $fb_json->{'id'}; } else { die($fb_json); } if (isset($fb_json->{'email'})) { $dln_email = $fb_json->{'email'}; } if (isset($fb_json->{'first_name'})) { $dln_first_name = $fb_json->{'first_name'}; } if (isset($fb_json->{'last_name'})) { $dln_last_name = $fb_json->{'last_name'}; } if (isset($fb_json->{'username'})) { $username = $fb_json->{'username'}; } if (isset($fb_json->{'link'})) { $dln_profile_url = $fb_json->{'link'}; $dln_name = $dln_first_name . ' ' . $dln_last_name; $user_login = $username ? strtolower($username) : $dln_email; } break; } // Cookies used to display welcome message if already signed in recently using some provider setcookie("social_login_current_provider", $social_login_provider, time() + 3600, SITECOOKIEPATH, COOKIE_DOMAIN, false, true); // Get user by meta $user_id = social_login_get_user_by_meta($dln_provider_identity_key, $dln_provider_identity); if ($user_id) { $user_data = get_userdata($user_id); $user_login = $user_data->user_login; } elseif ($user_id = email_exists($dln_email)) { // User not found by provider identity, check by email update_user_meta($user_id, $dln_provider_identity_key, $dln_provider_identity); $user_data = get_userdata($user_id); $user_login = $user_data->user_login; } else { // Create new user and associate provider identity if (username_exists($user_login)) { $user_login = apply_filters('social_login_username_exists', strtolower("dln_" . md5($social_login_provider . $dln_provider_identity))); } $userdata = array('user_login' => $user_login, 'user_email' => $dln_email, 'first_name' => $dln_first_name, 'last_name' => $dln_last_name, 'user_url' => $dln_profile_url, 'user_pass' => wp_generate_password()); // Create a new user $user_id = wp_insert_user($userdata); if ($user_id && is_integer($user_id)) { update_user_meta($user_id, $dln_provider_identity_key, $dln_provider_identity); } } if ($user_id instanceof WP_Error) { var_dump($user_id); die; } if ($dln_provider_identity) { $exist_ui = dln_get_field('dln_users', 'fbid', "userid={$user_id}"); $access_token = $_REQUEST['social_login_access_token']; if (!$exist_ui) { dln_insert_field('dln_users', "(`userid`, `fbid`, `access_token`, `crawl`) VALUES({$user_id}, '{$dln_provider_identity}', '{$access_token}', 0)"); } } wp_set_auth_cookie($user_id); do_action('social_login_login', $user_login); if ($is_ajax) { echo '{"redirect":"' . $redirect_to . '"}'; } else { wp_safe_redirect($redirect_to); } exit; }
/** * function to get list friends in facebook using ajax * * @return json $list */ function dln_ajax_list_friend_fb() { global $wpdb; if (!isset($_POST['dln_nonce_check']) || !wp_verify_nonce($_POST['dln_nonce_check'], 'dln_nonce_check')) { return; } if (!$_POST['fbid']) { return 'false'; } $fbid = $_POST['fbid']; $table = $wpdb->prefix . 'dln_users'; $result = $wpdb->get_row($wpdb->prepare("SELECT access_token FROM {$table} WHERE fbid = %s", $fbid)); if (!$result) { return 0; } $request = "https://graph.facebook.com/{$fbid}?fields=friends&access_token={$result->access_token}"; $json_list = dln_curl_get_contents($request); echo $json_list; exit; }
<?php require_once dirname(dirname(__FILE__)) . '/constants.php'; require_once dirname(__FILE__) . '/base_facebook.php'; require_once dirname(__FILE__) . '/facebook.php'; require_once dirname(dirname(__FILE__)) . '/utils.php'; $client_id = get_option('social_login_facebook_api_key'); $secret_key = get_option('social_login_facebook_secret_key'); if (isset($_GET['code'])) { $code = $_GET['code']; $client_id = get_option('social_login_facebook_api_key'); $secret_key = get_option('social_login_facebook_secret_key'); parse_str(dln_curl_get_contents("https://graph.facebook.com/oauth/access_token?" . 'client_id=' . $client_id . '&redirect_uri=' . urlencode(SOCIAL_LOGIN_PLUGIN_URL . '/facebook/callback.php') . '&client_secret=' . $secret_key . '&code=' . urlencode($code))); $signature = social_login_generate_signature($access_token); ?> <html> <head> <script> function init() { window.opener.wp_social_login({'action' : 'social_login', 'social_login_provider' : 'facebook', 'social_login_signature' : '<?php echo $signature; ?> ', 'social_login_access_token' : '<?php echo $access_token; ?> '}); window.close(); }