aboutsummaryrefslogtreecommitdiff
path: root/mod/twitterservice/twitterservice_lib.php
diff options
context:
space:
mode:
Diffstat (limited to 'mod/twitterservice/twitterservice_lib.php')
-rw-r--r--mod/twitterservice/twitterservice_lib.php167
1 files changed, 103 insertions, 64 deletions
diff --git a/mod/twitterservice/twitterservice_lib.php b/mod/twitterservice/twitterservice_lib.php
index 7e8004215..074c7d16c 100644
--- a/mod/twitterservice/twitterservice_lib.php
+++ b/mod/twitterservice/twitterservice_lib.php
@@ -17,53 +17,75 @@ function twitterservice_allow_sign_on_with_twitter() {
if (!$consumer_key = elgg_get_plugin_setting('consumer_key', 'twitterservice')) {
return FALSE;
}
-
+
if (!$consumer_secret = elgg_get_plugin_setting('consumer_secret', 'twitterservice')) {
return FALSE;
}
-
+
return elgg_get_plugin_setting('sign_on', 'twitterservice') == 'yes';
}
+/**
+ * Forwards
+ *
+ * @todo what is this?
+ */
function twitterservice_forward() {
-
// sanity check
if (!twitterservice_allow_sign_on_with_twitter()) {
forward();
}
-
+
$callback = elgg_normalize_url("pg/twitterservice/login");
$request_link = twitterservice_get_authorize_url($callback);
-
+
forward($request_link, 'twitterservice');
}
+/**
+ * Log in a user with twitter.
+ */
function twitterservice_login() {
-
+
// sanity check
if (!twitterservice_allow_sign_on_with_twitter()) {
forward();
}
-
+
$token = twitterservice_get_access_token(get_input('oauth_verifier'));
if (!isset($token['oauth_token']) or !isset($token['oauth_token_secret'])) {
register_error(elgg_echo('twitterservice:login:error'));
forward();
}
-
- // attempt to find user
- $values = array(
- 'plugin:settings:twitterservice:access_key' => $token['oauth_token'],
- 'plugin:settings:twitterservice:access_secret' => $token['oauth_token_secret'],
+
+ // attempt to find user and log them in.
+ // else, create a new user.
+ $options = array(
+ 'type' => 'user',
+ 'plugin_user_setting_name_value_pairs' => array(
+ 'access_key' => $token['oauth_token'],
+ 'access_secret' => $token['oauth_token_secret'],
+ ),
+ 'limit' => 0
);
-
- if (!$users = get_entities_from_private_setting_multi($values, 'user', '', 0, '', 0)) {
+
+ $users = elgg_get_entities_from_plugin_user_settings($options);
+
+ if ($users) {
+ if (count($users) == 1 && login($users[0])) {
+ system_message(elgg_echo('twitterservice:login:success'));
+ } else {
+ system_message(elgg_echo('twitterservice:login:error'));
+ }
+
+ forward();
+ } else {
// need Twitter account credentials
$consumer_key = elgg_get_plugin_setting('consumer_key', 'twitterservice');
$consumer_secret = elgg_get_plugin_setting('consumer_secret', 'twitterservice');
$api = new TwitterOAuth($consumer_key, $consumer_secret, $token['oauth_token'], $token['oauth_token_secret']);
$twitter = $api->get('account/verify_credentials');
-
+
// backward compatibility for stalled-development Twitter Login plugin
$user = FALSE;
if ($twitter_user = get_user_by_username($token['screen_name'])) {
@@ -73,7 +95,7 @@ function twitterservice_login() {
$forward = '';
}
}
-
+
// create new user
if (!$user) {
// check new registration allowed
@@ -81,22 +103,22 @@ function twitterservice_login() {
register_error(elgg_echo('registerdisabled'));
forward();
}
-
+
// trigger a hook for plugin authors to intercept
- if (!trigger_plugin_hook('new_twitter_user', 'twitter_service', array('account' => $twitter), TRUE)) {
+ if (!elgg_trigger_plugin_hook('new_twitter_user', 'twitter_service', array('account' => $twitter), TRUE)) {
// halt execution
register_error(elgg_echo('twitterservice:login:error'));
forward();
}
-
+
// Elgg-ify Twitter credentials
$username = "{$twitter->screen_name}_twitter";
$display_name = $twitter->name;
$password = generate_random_cleartext_password();
-
+
// @hack Temporary, junk email account to allow user creation
$email = "$username@elgg.com";
-
+
try {
// create new account
if (!$user_id = register_user($username, $password, $display_name, $email)) {
@@ -107,50 +129,48 @@ function twitterservice_login() {
register_error($r->getMessage());
forward();
}
-
+
$user = new ElggUser($user_id);
-
+
// @hack Remove temporary email and forward to user settings page
// @todo Consider using a view to force valid email
$site_name = elgg_get_site_entity()->name;
system_message(elgg_echo('twitterservice:login:email', array($site_name)));
$user->email = '';
$user->save();
-
+
$forward = "pg/settings/user/{$user->username}";
}
-
+
// set twitter services tokens
elgg_set_plugin_user_setting('twitter_name', $token['screen_name'], $user->guid);
elgg_set_plugin_user_setting('access_key', $token['oauth_token'], $user->guid);
elgg_set_plugin_user_setting('access_secret', $token['oauth_token_secret'], $user->guid);
-
+
// pull in Twitter icon
twitterservice_update_user_avatar($user, $twitter->profile_image_url);
-
+
// login new user
if (login($user)) {
system_message(elgg_echo('twitterservice:login:success'));
} else {
system_message(elgg_echo('twitterservice:login:error'));
}
-
+
forward($forward, 'twitterservice');
- } elseif (count($users) == 1) {
- if (login($users[0])) {
- system_message(elgg_echo('twitterservice:login:success'));
- } else {
- system_message(elgg_echo('twitterservice:login:error'));
- }
-
- forward();
}
-
+
// register login error
register_error(elgg_echo('twitterservice:login:error'));
forward();
}
+/**
+ * Pull in the latest avatar from twitter.
+ *
+ * @param unknown_type $user
+ * @param unknown_type $file_location
+ */
function twitterservice_update_user_avatar($user, $file_location) {
$sizes = array(
'topbar' => array(16, 16, TRUE),
@@ -160,7 +180,7 @@ function twitterservice_update_user_avatar($user, $file_location) {
'large' => array(200, 200, FALSE),
'master' => array(550, 550, FALSE),
);
-
+
$filehandler = new ElggFile();
$filehandler->owner_guid = $user->getGUID();
foreach ($sizes as $size => $dimensions) {
@@ -176,7 +196,7 @@ function twitterservice_update_user_avatar($user, $file_location) {
$filehandler->write($image);
$filehandler->close();
}
-
+
return TRUE;
}
@@ -196,71 +216,90 @@ function twitterservice_authorize() {
register_error(elgg_echo('twitterservice:authorize:error'));
forward('pg/settings/plugins', 'twitterservice');
}
-
- // only one user per tokens
- $values = array(
- 'plugin:settings:twitterservice:access_key' => $token['oauth_token'],
- 'plugin:settings:twitterservice:access_secret' => $token['oauth_token_secret'],
+
+ // make sure no other users are registered to this twitter account.
+ $options = array(
+ 'type' => 'user',
+ 'plugin_user_setting_name_value_pairs' => array(
+ 'access_key' => $token['oauth_token'],
+ 'access_secret' => $token['oauth_token_secret'],
+ ),
+ 'limit' => 0
);
-
- if ($users = get_entities_from_private_setting_multi($values, 'user', '', 0, '', 0)) {
+
+ $users = elgg_get_entities_from_plugin_user_settings($options);
+
+ if ($users) {
foreach ($users as $user) {
// revoke access
- clear_plugin_usersetting('twitter_name', $user->getGUID());
- clear_plugin_usersetting('access_key', $user->getGUID());
- clear_plugin_usersetting('access_secret', $user->getGUID());
+ elgg_unset_plugin_user_setting('twitter_name', $user->getGUID());
+ elgg_unset_plugin_user_setting('access_key', $user->getGUID());
+ elgg_unset_plugin_user_setting('access_secret', $user->getGUID());
}
}
-
+
// register user's access tokens
elgg_set_plugin_user_setting('twitter_name', $token['screen_name']);
elgg_set_plugin_user_setting('access_key', $token['oauth_token']);
elgg_set_plugin_user_setting('access_secret', $token['oauth_token_secret']);
-
+
system_message(elgg_echo('twitterservice:authorize:success'));
forward('pg/settings/plugins', 'twitterservice');
}
+/**
+ * Remove twitter access for the currently logged in user.
+ */
function twitterservice_revoke() {
// unregister user's access tokens
- clear_plugin_usersetting('twitter_name');
- clear_plugin_usersetting('access_key');
- clear_plugin_usersetting('access_secret');
-
+ elgg_unset_plugin_user_setting('twitter_name');
+ elgg_unset_plugin_user_setting('access_key');
+ elgg_unset_plugin_user_setting('access_secret');
+
system_message(elgg_echo('twitterservice:revoke:success'));
forward('pg/settings/plugins', 'twitterservice');
}
-function twitterservice_get_authorize_url($callback=NULL) {
+/**
+ * Returns the url to authorize a user.
+ *
+ * @param string $callback The callback URL?
+ */
+function twitterservice_get_authorize_url($callback = NULL) {
global $SESSION;
-
+
$consumer_key = elgg_get_plugin_setting('consumer_key', 'twitterservice');
$consumer_secret = elgg_get_plugin_setting('consumer_secret', 'twitterservice');
-
+
// request tokens from Twitter
$twitter = new TwitterOAuth($consumer_key, $consumer_secret);
$token = $twitter->getRequestToken($callback);
-
+
// save token in session for use after authorization
$SESSION['twitterservice'] = array(
'oauth_token' => $token['oauth_token'],
'oauth_token_secret' => $token['oauth_token_secret'],
);
-
+
return $twitter->getAuthorizeURL($token['oauth_token']);
}
-function twitterservice_get_access_token($oauth_verifier=FALSE) {
+/**
+ * Returns the access token to use in twitter calls.
+ *
+ * @param unknown_type $oauth_verifier
+ */
+function twitterservice_get_access_token($oauth_verifier = FALSE) {
global $SESSION;
-
+
$consumer_key = elgg_get_plugin_setting('consumer_key', 'twitterservice');
$consumer_secret = elgg_get_plugin_setting('consumer_secret', 'twitterservice');
-
+
// retrieve stored tokens
$oauth_token = $SESSION['twitterservice']['oauth_token'];
$oauth_token_secret = $SESSION['twitterservice']['oauth_token_secret'];
$SESSION->offsetUnset('twitterservice');
-
+
// fetch an access token
$api = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
return $api->getAccessToken($oauth_verifier);