1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
<?php
/**
* Common library of functions used by Twitter Services.
*
* @package twitter_api
*/
/**
* Tests if the system admin has enabled Sign-On-With-Twitter
*
* @param void
* @return bool
*/
function twitter_api_allow_sign_on_with_twitter() {
if (!$consumer_key = elgg_get_plugin_setting('consumer_key', 'twitter_api')) {
return FALSE;
}
if (!$consumer_secret = elgg_get_plugin_setting('consumer_secret', 'twitter_api')) {
return FALSE;
}
return elgg_get_plugin_setting('sign_on', 'twitter_api') == 'yes';
}
/**
* Forwards the user to twitter to authenticate
*
* This includes the login URL as the callback
*/
function twitter_api_forward() {
// sanity check
if (!twitter_api_allow_sign_on_with_twitter()) {
forward();
}
$callback = elgg_normalize_url("twitter_api/login");
$request_link = twitter_api_get_authorize_url($callback);
forward($request_link, 'twitter_api');
}
/**
* Log in a user referred from Twitter's OAuth API
*
* If the user has already linked their account with Twitter, it is a seamless
* login. If this is a first time login (or a user from deprecated twitter login
* plugin), we create a new account (update the account).
*
* If a plugin wants to be notified when someone logs in with twitter or a new
* twitter user signs up, register for the standard login or create user events
* and check for 'twitter_api' context.
*
* The user has to be redirected from Twitter for this to work. It depends on
* the Twitter OAuth data.
*/
function twitter_api_login() {
// sanity check
if (!twitter_api_allow_sign_on_with_twitter()) {
forward();
}
$token = twitter_api_get_access_token(get_input('oauth_verifier'));
if (!isset($token['oauth_token']) or !isset($token['oauth_token_secret'])) {
register_error(elgg_echo('twitter_api:login:error'));
forward();
}
// 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,
);
$users = elgg_get_entities_from_plugin_user_settings($options);
if ($users) {
if (count($users) == 1 && login($users[0])) {
system_message(elgg_echo('twitter_api:login:success'));
} else {
register_error(elgg_echo('twitter_api:login:error'));
}
forward(elgg_get_site_url());
} else {
$consumer_key = elgg_get_plugin_setting('consumer_key', 'twitter_api');
$consumer_secret = elgg_get_plugin_setting('consumer_secret', 'twitter_api');
$api = new TwitterOAuth($consumer_key, $consumer_secret, $token['oauth_token'], $token['oauth_token_secret']);
$twitter = $api->get('account/verify_credentials');
// backward compatibility for deprecated Twitter Login plugin
$user = FALSE;
if ($twitter_user = get_user_by_username($token['screen_name'])) {
if (($screen_name = $twitter_user->twitter_screen_name) && ($screen_name == $token['screen_name'])) {
// convert existing account
$user = $twitter_user;
$forward = '';
}
}
// create new user
if (!$user) {
$user = twitter_api_create_user($twitter);
$site_name = elgg_get_site_entity()->name;
system_message(elgg_echo('twitter_api:login:email', array($site_name)));
$forward = "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
twitter_api_update_user_avatar($user, $twitter->profile_image_url);
// login new user
if (login($user)) {
system_message(elgg_echo('twitter_api:login:success'));
} else {
system_message(elgg_echo('twitter_api:login:error'));
}
forward($forward, 'twitter_api');
}
// register login error
register_error(elgg_echo('twitter_api:login:error'));
forward();
}
/**
* Create a new user from Twitter information
*
* @param object $twitter Twitter OAuth response
* @return ElggUser
*/
function twitter_api_create_user($twitter) {
// check new registration allowed
if (!twitter_api_allow_new_users_with_twitter()) {
register_error(elgg_echo('registerdisabled'));
forward();
}
// Elgg-ify Twitter credentials
$username = $twitter->screen_name;
while (get_user_by_username($username)) {
// @todo I guess we just hope this is good enough
$username = $twitter->screen_name . '_' . rand(1000, 9999);
}
$password = generate_random_cleartext_password();
$name = $twitter->name;
$user = new ElggUser();
$user->username = $username;
$user->name = $name;
$user->access_id = ACCESS_PUBLIC;
$user->salt = generate_random_cleartext_password();
$user->password = generate_user_password($user, $password);
$user->owner_guid = 0;
$user->container_guid = 0;
if (!$user->save()) {
register_error(elgg_echo('registerbad'));
forward();
}
return $user;
}
/**
* Pull in the latest avatar from twitter.
*
* @param ElggUser $user
* @param string $file_location
*/
function twitter_api_update_user_avatar($user, $file_location) {
// twitter's images have a few suffixes:
// _normal
// _resonably_small
// _mini
// the twitter app here returns _normal. We want standard, so remove the suffix.
// @todo Should probably check that it's an image file.
$file_location = str_replace('_normal.jpg', '.jpg', $file_location);
$sizes = array(
'topbar' => array(16, 16, TRUE),
'tiny' => array(25, 25, TRUE),
'small' => array(40, 40, TRUE),
'medium' => array(100, 100, TRUE),
'large' => array(200, 200, FALSE),
'master' => array(550, 550, FALSE),
);
$filehandler = new ElggFile();
$filehandler->owner_guid = $user->getGUID();
foreach ($sizes as $size => $dimensions) {
$image = get_resized_image_from_existing_file(
$file_location,
$dimensions[0],
$dimensions[1],
$dimensions[2]
);
$filehandler->setFilename("profile/$user->guid$size.jpg");
$filehandler->open('write');
$filehandler->write($image);
$filehandler->close();
}
// update user's icontime
$user->icontime = time();
}
/**
* User-initiated Twitter authorization
*
* Callback action from Twitter registration. Registers a single Elgg user with
* the authorization tokens. Will revoke access from previous users when a
* conflict exists.
*
* Depends upon {@link twitter_api_get_authorize_url} being called previously
* to establish session request tokens.
*/
function twitter_api_authorize() {
$token = twitter_api_get_access_token();
if (!isset($token['oauth_token']) || !isset($token['oauth_token_secret'])) {
register_error(elgg_echo('twitter_api:authorize:error'));
forward('settings/plugins', 'twitter_api');
}
// 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
);
$users = elgg_get_entities_from_plugin_user_settings($options);
if ($users) {
foreach ($users as $user) {
// revoke access
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']);
// trigger authorization hook
elgg_trigger_plugin_hook('authorize', 'twitter_api', array('token' => $token));
system_message(elgg_echo('twitter_api:authorize:success'));
forward('settings/plugins', 'twitter_api');
}
/**
* Remove twitter access for the currently logged in user.
*/
function twitter_api_revoke() {
// unregister user's access tokens
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('twitter_api:revoke:success'));
forward('settings/plugins', 'twitter_api');
}
/**
* Gets the url to authorize a user.
*
* @param string $callback The callback URL
*/
function twitter_api_get_authorize_url($callback = NULL, $login = true) {
global $SESSION;
$consumer_key = elgg_get_plugin_setting('consumer_key', 'twitter_api');
$consumer_secret = elgg_get_plugin_setting('consumer_secret', 'twitter_api');
// request tokens from Twitter
$twitter = new TwitterOAuth($consumer_key, $consumer_secret);
$token = $twitter->getRequestToken($callback);
// save token in session for use after authorization
$SESSION['twitter_api'] = array(
'oauth_token' => $token['oauth_token'],
'oauth_token_secret' => $token['oauth_token_secret'],
);
return $twitter->getAuthorizeURL($token['oauth_token'], $login);
}
/**
* Returns the access token to use in twitter calls.
*
* @param unknown_type $oauth_verifier
*/
function twitter_api_get_access_token($oauth_verifier = FALSE) {
global $SESSION;
$consumer_key = elgg_get_plugin_setting('consumer_key', 'twitter_api');
$consumer_secret = elgg_get_plugin_setting('consumer_secret', 'twitter_api');
// retrieve stored tokens
$oauth_token = $SESSION['twitter_api']['oauth_token'];
$oauth_token_secret = $SESSION['twitter_api']['oauth_token_secret'];
$SESSION->offsetUnset('twitter_api');
// fetch an access token
$api = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
return $api->getAccessToken($oauth_verifier);
}
/**
* Checks if this site is accepting new users.
* Admins can disable manual registration, but some might want to allow
* twitter-only logins.
*/
function twitter_api_allow_new_users_with_twitter() {
$site_reg = elgg_get_config('allow_registration');
$twitter_reg = elgg_get_plugin_setting('new_users');
if ($site_reg || (!$site_reg && $twitter_reg == 'yes')) {
return true;
}
return false;
}
|