-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.pages.inc
442 lines (379 loc) · 13.6 KB
/
twitter.pages.inc
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
/**
* Form builder; Twitter settings form.
*/
function twitter_admin_form($form, &$form_state) {
$form['twitter_import'] = array(
'#type' => 'checkbox',
'#title' => t('Import and display the Twitter statuses of site users who have entered their Twitter account information.'),
'#default_value' => variable_get('twitter_import', 1),
);
$form['twitter_expire'] = array(
'#type' => 'select',
'#title' => t('Delete old statuses'),
'#default_value' => variable_get('twitter_expire', 0),
'#options' => array(0 => t('Never')) + drupal_map_assoc(array(604800, 2592000, 7776000, 31536000), 'format_interval'),
'#states' => array(
'visible' => array(
':input[name=twitter_import]' => array('checked' => TRUE),
),
),
);
$form['oauth'] = array(
'#type' => 'fieldset',
'#title' => t('OAuth Settings'),
'#access' => module_exists('oauth_common'),
'#description' => t('To enable OAuth based access for twitter, you must <a href="@url">register your application</a> with Twitter and add the provided keys here.', array('@url' => 'https://dev.twitter.com/apps/new')),
);
$form['oauth']['callback_url'] = array(
'#type' => 'item',
'#title' => t('Callback URL'),
'#markup' => url('twitter/oauth', array('absolute' => TRUE)),
);
$form['oauth']['twitter_consumer_key'] = array(
'#type' => 'textfield',
'#title' => t('OAuth Consumer key'),
'#default_value' => variable_get('twitter_consumer_key', NULL),
);
$form['oauth']['twitter_consumer_secret'] = array(
'#type' => 'textfield',
'#title' => t('OAuth Consumer secret'),
'#default_value' => variable_get('twitter_consumer_secret', NULL),
);
return system_settings_form($form);
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function twitter_user_settings($account) {
module_load_include('inc', 'twitter');
$output = array();
if (!empty($account->twitter_accounts)) {
$output['list_form'] = drupal_get_form('twitter_account_list_form', $account->twitter_accounts);
}
$output['form'] = drupal_get_form('twitter_account_form', $account);
return $output;
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function twitter_account_list_form($form, $form_state, $twitter_accounts = array()) {
$form['#tree'] = TRUE;
$form['accounts'] = array();
foreach ($twitter_accounts as $twitter_account) {
$form['accounts'][] = _twitter_account_list_row($twitter_account);
}
if (!empty($twitter_accounts)) {
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save changes'),
);
}
return $form;
}
function _twitter_account_list_row($account) {
$form['#account'] = $account;
$form['id'] = array(
'#type' => 'value',
'#value' => $account->id,
);
$form['uid'] = array(
'#type' => 'value',
'#value' => $account->uid,
);
$form['screen_name'] = array(
'#type' => 'value',
'#value' => $account->screen_name,
);
$form['image'] = array(
'#markup' => theme('image', array('path' => $account->profile_image_url)),
);
$form['visible_name'] = array(
'#markup' => l($account->screen_name, 'http://www.twitter.com/' . $account->screen_name),
);
$form['description'] = array(
'#markup' => filter_xss($account->description),
);
$form['protected'] = array(
'#markup' => empty($account->protected) ? t('No') : t('Yes'),
);
// Here we use user_access('import own tweets') to check permission
// instead of user_access('import own tweets', $account->uid)
// because we allow roles with sufficient permission to overwrite
// the user's import settings.
if (variable_get('twitter_import', TRUE) && user_access('import own tweets')) {
$form['import'] = array(
'#type' => 'checkbox',
'#default_value' => user_access('import own tweets') ? $account->import : '',
);
}
$form['delete'] = array(
'#type' => 'checkbox',
);
return $form;
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function theme_twitter_account_list_form($variables) {
$form = $variables['form'];
if (variable_get('twitter_import', TRUE) && user_access('import own tweets')) {
$header = array('', t('Name'), t('Description'), t('Private'), t('Import'), t('Delete'));
}
else {
$header = array('', t('Name'), t('Description'), t('Private'), t('Delete'));
}
if (user_access('make twitter accounts global')) {
$header[] = '';
}
$rows = array();
foreach (element_children($form['accounts']) as $key) {
$element = &$form['accounts'][$key];
if (variable_get('twitter_import', TRUE) && user_access('import own tweets')) {
$row = array(
drupal_render($element['image']),
drupal_render($element['id']) . drupal_render($element['screen_name']) . drupal_render($element['visible_name']),
drupal_render($element['description']),
drupal_render($element['protected']),
drupal_render($element['import']),
drupal_render($element['delete']),
);
}
else {
$row = array(
drupal_render($element['image']),
drupal_render($element['id']) . drupal_render($element['screen_name']) . drupal_render($element['visible_name']),
drupal_render($element['description']),
drupal_render($element['protected']),
drupal_render($element['delete']),
);
}
if (user_access('make twitter accounts global')) {
$label = ($element['#account']->is_global) ? t('remove global') : t('make global');
$row[] = l($label, 'user/' . $element['#account']->uid . '/edit/twitter/global/' . $element['#account']->id);
}
$rows[] = $row;
}
$output = theme('table', array('header' => $header, 'rows' => $rows));
$output .= drupal_render_children($form);
return $output;
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function twitter_account_list_form_submit($form, &$form_state) {
$accounts = $form_state['values']['accounts'];
foreach ($accounts as $account) {
if (empty($account['delete'])) {
twitter_account_save($account);
drupal_set_message(t('The Twitter account settings were updated.'));
}
else {
twitter_account_delete($account['id'], $account['screen_name']);
drupal_set_message(t('The Twitter account was deleted.'));
}
}
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function twitter_user_make_global($form, $form_state, $account, $twitter_uid) {
module_load_include('inc', 'twitter');
$twitter_account = twitter_account_load($twitter_uid);
$form = array();
$form['uid'] = array(
'#type' => 'value',
'#value' => $account->uid,
);
$form['twitter_uid'] = array(
'#type' => 'value',
'#value' => $twitter_uid,
);
if ($twitter_account->is_global) {
$text = t('Are you sure you want to remove %screen_name from the global accounts?', array('%screen_name' => $twitter_account->screen_name));
$description = t('This means other users will no longer be allowed to post using this account.');
}
else {
$text = t('Are you sure you want to allow other users to access the %screen_name account?', array('%screen_name' => $twitter_account->screen_name));
$description = t('This will allow other users to post using this account.');
}
return confirm_form($form, $text, 'user/' . $account->uid . '/edit/twitter', $description);
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function twitter_user_make_global_submit($form, &$form_state) {
db_update('twitter_account')
->expression('is_global', '(1 - is_global)')
->condition('twitter_uid', $form_state['values']['twitter_uid'])
->execute();
$form_state['redirect'] = 'user/' . $form_state['values']['uid'] . '/edit/twitter';
}
/**
* Form to add a Twitter account
*
* If OAuth is not enabled, a text field lets users to add their
* Twitter screen name. If it is, a submit button redirects to
* Twitter.com asking for authorisation.
*/
function twitter_account_form($form, $form_state, $account = NULL) {
if (empty($account)) {
global $user;
$account = $user;
}
$form['uid'] = array(
'#type' => 'value',
'#value' => $account->uid,
);
if (_twitter_use_oauth()) {
$form['#validate'] = array('twitter_account_oauth_validate');
}
else {
$form['screen_name'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#title' => t('Twitter user name'),
);
$form['import'] = array(
'#type' => 'checkbox',
'#title' => t('Import statuses from this account'),
'#default_value' => TRUE,
'#access' => FALSE,
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add account'),
);
return $form;
}
/**
* Implements hook_FORM_ID_submit()
*
* Loads Twitter account details and adds them to the user account
*/
function twitter_account_form_submit($form, &$form_state) {
module_load_include('lib.php', 'twitter');
module_load_include('inc', 'twitter');
$name = $form_state['values']['screen_name'];
$twitter = new Twitter($name);
$account = $twitter->users_show($name, false);
twitter_account_save($account, TRUE, user_load($form_state['values']['uid']));
}
/**
* If OAuth is enabled, intercept submission of 'Add Account' form on
* user/%/edit/twitter page and redirect to Twitter for auth.
*/
function twitter_account_oauth_validate($form, &$form_state) {
module_load_include('lib.php', 'oauth_common');
module_load_include('lib.php', 'twitter');
$key = variable_get('twitter_consumer_key', '');
$secret = variable_get('twitter_consumer_secret', '');
if ($key == '' || $secret == '') {
form_set_error('', t('Please configure your Twitter consumer key and secret.'));
}
$twitter = new TwitterOAuth($key, $secret);
$token = $twitter->get_request_token();
$_SESSION['twitter_oauth']['account'] = user_load($form['uid']['#value']);
$_SESSION['twitter_oauth']['token'] = $token;
$_SESSION['twitter_oauth']['destination'] = $_GET['q'];
drupal_goto($twitter->get_authorize_url($token));
}
/**
* @TODO This code should probably be reviewed.
*
* Wrapper to call drupal_form_submit() which wasn't required in D6.
*/
function twitter_oauth_callback() {
$form_state['values']['oauth_token'] = $_GET['oauth_token'];
drupal_form_submit('twitter_oauth_callback_form', $form_state);
}
/**
* Form builder function. In D6 this form was built in response to the
* oauth return request from Twitter, and the setting of
* $form['#post'] seems to have caused the form to be validated and
* processed.
*/
function twitter_oauth_callback_form($form, &$form_state) {
$form['#post']['oauth_token'] = $_GET['oauth_token'];
$form['oauth_token'] = array(
'#type' => 'hidden',
'#default_value' => $_GET['oauth_token'],
);
return $form;
}
/**
* Validate results from Twitter OAuth return request.
*/
function twitter_oauth_callback_form_validate($form, &$form_state) {
$key = variable_get('twitter_consumer_key', '');
$secret = variable_get('twitter_consumer_secret', '');
if ($key == '' || $secret == '') {
form_set_error('', t('Please configure your Twitter consumer key and secret.'));
}
if (isset($_SESSION['twitter_oauth'])) {
$form_state['twitter_oauth'] = $_SESSION['twitter_oauth'];
unset($_SESSION['twitter_oauth']);
}
else {
form_set_error('oauth_token', 'Invalid Twitter OAuth request');
}
if (isset($form_state['twitter_oauth']['token'])) {
$token = $form_state['twitter_oauth']['token'];
if (!is_array($token) || !$key || !$secret) {
form_set_error('oauth_token', t('Invalid Twitter OAuth request'));
}
if ($token['oauth_token'] != $form_state['values']['oauth_token']) {
form_set_error('oauth_token', t('Invalid OAuth token.'));
}
}
else {
form_set_error('oauth_token', t('Invalid Twitter OAuth request'));
}
module_load_include('lib.php', 'oauth_common');
module_load_include('lib.php', 'twitter');
module_load_include('inc', 'twitter');
if ($twitter = new TwitterOAuth($key, $secret, $token['oauth_token'], $token['oauth_token_secret'])) {
if ($response = $twitter->get_access_token()) {
$form_state['twitter_oauth']['response'] = $response;
}
else {
form_set_error('oauth_token', t('Invalid Twitter OAuth request'));
}
}
else {
form_set_error('oauth_token', t('Invalid Twitter OAuth request'));
}
}
/**
* Handle a Twitter OAuth return request and store the account creds
* in the DB. Redirects to user/%/edit/twitter
*
* @TODO Redirect better.
*
* I don't much like the use of drupal_goto() here as it might
* interfere with other modules trying to piggyback on the form
* submission, but setting $form['redirect'] was leaving us at the
* twitter/oauth URL.
*/
function twitter_oauth_callback_form_submit(&$form, &$form_state) {
$key = variable_get('twitter_consumer_key', '');
$secret = variable_get('twitter_consumer_secret', '');
$response = $form_state['twitter_oauth']['response'];
$twitter = new TwitterOAuth($key, $secret, $response['oauth_token'], $response['oauth_token_secret']);
$twitter_account = $twitter->users_show($response['screen_name']);
$twitter_account->set_auth($response);
$account = $form_state['twitter_oauth']['account'];
twitter_account_save($twitter_account, TRUE, $account);
$form['#programmed'] = FALSE;
$form_state['redirect'] = url('user/' . $account->uid . '/edit/twitter');
// redirect isn't firing - because we're using drupal_submit_form()?
// - so adding drupal_goto() here (but not liking it).
drupal_goto('user/' . $account->uid . '/edit/twitter');
}