Example #1
0
/**
 * Synchronize social network information to a profile
 *
 * @param string $hook_name    the name of the hook
 * @param string $entity_type  the type of the hook
 * @param mixed  $return_value current return value
 * @param array  $params       supplied params
 *
 * @return void
 */
function socialink_sync_network_hook($hook_name, $entity_type, $return_value, $params)
{
    if (!empty($params) && is_array($params)) {
        if (isset($params["user"]) && isset($params["network"])) {
            $user = $params["user"];
            $network = $params["network"];
            if (socialink_is_available_network($network)) {
                switch ($network) {
                    case "twitter":
                        socialink_twitter_sync_profile_metadata($user->getGUID());
                        break;
                    case "linkedin":
                        socialink_linkedin_sync_profile_metadata($user->getGUID());
                        break;
                    case "facebook":
                        socialink_facebook_sync_profile_metadata($user->getGUID());
                        break;
                }
            }
        }
    }
}
Example #2
0
/**
 * Create a user based on LinkedIn information
 *
 * @param string $token LinkedIn access token
 *
 * @return bool|ElggUser
 */
function socialink_linkedin_create_user($token)
{
    if (empty($token) || !is_array($token)) {
        return false;
    }
    $keys = socialink_linkedin_available();
    if (empty($keys)) {
        return false;
    }
    $keys["oauth_token"] = $token["oauth_token"];
    $keys["oauth_secret"] = $token["oauth_token_secret"];
    $api = socialink_linkedin_get_api_object($keys);
    if (empty($api)) {
        return false;
    }
    try {
        // get user data
        $response = $api->profile("~:(first-name,last-name,email-address)");
    } catch (Exception $e) {
    }
    $api_result = socialink_linkedin_verify_response($response);
    if (empty($api_result)) {
        return false;
    }
    $api_result = json_decode($api_result);
    // build user information
    $name = $api_result->firstName . " " . $api_result->lastName;
    $email = $api_result->emailAddress;
    $pwd = generate_random_cleartext_password();
    $username = socialink_create_username_from_email($email);
    // check email address
    if (get_user_by_email($email)) {
        register_error(elgg_echo("socialink:networks:create_user:error:email"));
        return false;
    }
    try {
        // register user
        $user_guid = register_user($username, $pwd, $name, $email);
        if (empty($user_guid)) {
            return false;
        }
        // show hidden entities
        $access = access_get_show_hidden_status();
        access_show_hidden_entities(true);
        $user = get_user($user_guid);
        if (empty($user)) {
            access_show_hidden_entities($access);
            return false;
        }
        // save user tokens
        elgg_set_plugin_user_setting("linkedin_oauth_token", $token["oauth_token"], $user_guid, "socialink");
        elgg_set_plugin_user_setting("linkedin_oauth_secret", $token["oauth_token_secret"], $user_guid, "socialink");
        // no need for uservalidationbyemail
        elgg_unregister_plugin_hook_handler("register", "user", "uservalidationbyemail_disable_new_user");
        // sync user data
        socialink_linkedin_sync_profile_metadata($user->getGUID());
        // trigger hook for registration
        $params = array("user" => $user, "password" => $pwd, "friend_guid" => 0, "invitecode" => "");
        if (elgg_trigger_plugin_hook("register", "user", $params, true) !== false) {
            // return the user
            access_show_hidden_entities($access);
            return $user;
        }
        // restore hidden entities
        access_show_hidden_entities($access);
    } catch (Exception $e) {
    }
    return false;
}