$retrycount = 0; while ($retrycount < $maxretrycount) { $tok = $to->getRequestToken(SERVER_URL . "settings.php"); if (isset($tok['oauth_token']) && isset($tok['oauth_token_secret'])) { break; } $retrycount += 1; sleep($retrycount * 5); } $request_token = $tok['oauth_token']; $request_token_secret = $tok['oauth_token_secret']; $front->getUser()->updateRequestTokens($request_token, $request_token_secret); } if (!empty($access_token) && !empty($access_token_secret)) { $to = new LinkedInOAuth(LINKEDIN_API_KEY_PUBLIC, LINKEDIN_API_KEY_PRIVATE, $access_token, $access_token_secret); $profile_result = $to->oAuthRequest('http://api.linkedin.com/v1/people/~:(public-profile-url,three-current-positions:(title,company:(name)))'); $profile_data = simplexml_load_string($profile_result); if (strpos($profile_data->message, 'unauthorized') > 0) { $front->getUser()->deleteTokens(); $request_token = null; $request_token_secret = null; $access_token = null; $access_token_secret = null; } else { echo "<div id='content_linkedin'>"; echo "<a href='" . $profile_data->{'public-profile-url'} . "'>" . $profile_data->{'public-profile-url'} . "</a><br>"; echo "<ul>"; foreach ($profile_data->{'three-current-positions'}->position as $position) { echo "<li>" . $position->title . " at " . $position->company->name . "</li>"; } echo "</ul>";
public function handle_linkedin_authorization() { session_start(); global $xoouserultra; //require_once(ABSPATH . 'wp-includes/pluggable.php'); require_once xoousers_path . "libs/linkedin/oauth/linkedinoauth.php"; //get oauttokens $temp_user_session_id = session_id(); $oauthstate = get_option('uultra_linkedin_' . $temp_user_session_id); //get access token and access token secret $oauthstate = $xoouserultra->get_linkedin_access_token($oauthstate); if (!$oauthstate) { echo "empty "; } else { // We've been given some access tokens, so try and use them to make an API call, and // display the results. $accesstoken = $oauthstate['access_token']; $accesstokensecret = $oauthstate['access_token_secret']; //print_r($oauthstate); $to = new LinkedInOAuth($xoouserultra->get_option('social_media_linkedin_api_public'), $xoouserultra->get_option('social_media_linkedin_api_private'), $accesstoken, $accesstokensecret); $find_person = ':(first-name,last-name,email-address)'; $profile_result = $to->oAuthRequest('http://api.linkedin.com/v1/people/~' . $find_person); $profile_data = simplexml_load_string($profile_result); $profile_data = json_decode(json_encode($profile_data), 1); $u_name = $profile_data["first-name"]; $lname = $profile_data["last-name"]; $u_email = $profile_data["email-address"]; $u_user = $u_name . "-" . $lname; //Sanitize Login $user_login = str_replace('.', '-', $u_user); $user_login = sanitize_user($user_login, true); //check if exists //check if already registered $exists = email_exists($u_email); if (!$exists) { //generat3 random password $user_pass = wp_generate_password(12, false); //Build user data $user_data = array('user_login' => $user_login, 'display_name' => !empty($u_name) ? $u_name : $u_user, 'user_email' => $u_email, 'user_pass' => $user_pass); // Create a new user $user_id = wp_insert_user($user_data); if (!$user_id) { } else { $verify_key = $this->get_unique_verify_account_id(); update_user_meta($user_id, 'xoouser_ultra_very_key', $verify_key); update_user_meta($user_id, 'xoouser_ultra_social_signup', 2); update_user_meta($user_id, 'first_name', $u_name); update_user_meta($user_id, 'last_name', $lname); //set account status $this->user_account_status($user_id); //notify depending on status $this->user_account_notify($user_id, $u_email, $user_login, $user_pass); } } else { //if user already created then try to login automatically $users = get_users(array('meta_key' => 'xoouser_linked_in_id', 'meta_value' => $u_linked_in_id, 'meta_compare' => '=')); if (isset($users[0]->ID) && is_numeric($users[0]->ID)) { $returning = $users[0]->ID; $user_login = $users[0]->user_login; $user = get_user_by('login', $user_login); $user_id = $user->ID; } else { //get by using email, we already know the user exists at this point $user = get_user_by('email', $u_email); $user_id = $user->ID; update_user_meta($user_id, 'xoouser_ultra_social_signup', 2); update_user_meta($user_id, 'xoouser_linked_in_id', $u_linked_in_id); //set account status $this->user_account_status($user_id); $returning = ''; } if ($this->is_active($user_id)) { //is active then login wp_set_auth_cookie($user_id, true, $secure); } else { $this->errors[] = __('<strong>ERROR:</strong> YOUR ACCOUNT IS NOT ACTIVE YET.', 'xoousers'); } } //redirect $this->login_registration_afterlogin(); } }
/** * Login con linkedin * */ public function loginLinkedin() { try { //recuperar tokens de autentificacion $tok = $_SESSION['linkedin_token']; $to = new \LinkedInOAuth($this->linkedin_id, $this->linkedin_secret, $tok['oauth_token'], $tok['oauth_token_secret']); //obtenemos los tokens de acceso $tok = $to->getAccessToken($_GET['oauth_verifier']); //borramos los tokens de autentificacion de la session, ya no nos sirven //unset($_SESSION['linkedin_token']); if (empty($tok['oauth_token']) || empty($tok['oauth_token_secret'])) { $this->last_error = "oauth-linkedin-access-denied"; return false; } //guardar los tokens en la base datos si se quieren usar mas adelante! //con los tokens podems acceder a la info del user, hay que recrear el objecto con los tokens privados $this->tokens['linkedin']['token'] = $tok['oauth_token']; $this->tokens['linkedin']['secret'] = $tok['oauth_token_secret']; $profile_result = $to->oAuthRequest('http://api.linkedin.com/v1/people/~:(id,first-name,last-name,summary,public-profile-url,picture-url,headline,interests,twitter-accounts,member-url-resources:(url),positions:(company),location:(name))'); $profile_data = simplexml_load_string($profile_result); $this->user_data['name'] = trim($profile_data->{"first-name"} . " " . $profile_data->{"last-name"}); if ($profile_data->{"public-profile-url"}) { //linkedin link $this->user_data['linkedin'] = current($profile_data->{"public-profile-url"}); //username from url $this->user_data['username'] = basename($this->user_data['linkedin']); } if ($profile_data->{"member-url-resources"}->{"member-url"}) { $urls = array(); foreach ($profile_data->{"member-url-resources"}->{"member-url"} as $url) { $urls[] = current($url->url); } $this->user_data['website'] .= implode("\n", $urls); } if ($profile_data->headline) { $this->user_data['about'] = current($profile_data->headline); } if ($profile_data->location->name) { $this->user_data['location'] = current($profile_data->location->name); } if ($profile_data->{"picture-url"}) { $this->user_data['profile_image_url'] = current($profile_data->{"picture-url"}); } //si el usuario tiene especificada su cuenta twitter if ($profile_data->{"twitter-accounts"}->{"twitter-account"}) { $this->user_data['twitter'] = 'http://twitter.com/' . current($profile_data->{"twitter-accounts"}->{"twitter-account"}->{"provider-account-name"}); } //ver todos los datos disponibles: //print_r($profile_data);print_r($this->user_data);die; return true; } catch (Exception $e) { $this->last_error = $e->getMessage() . " 1/ " . get_class($e); return false; } return true; }