-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.lib.php
511 lines (409 loc) · 12.8 KB
/
twitter.lib.php
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
<?php
/**
* @file
* Classes to implement the full Twitter API
*/
/**
* Exception handling class.
*/
class TwitterException extends Exception {
}
/**
* Primary Twitter API implementation class
* Supports the full REST API for twitter.
*/
class Twitter {
/**
* @var $format API format to use: can be json or xml
*/
protected $format = 'json';
/**
* @var $host The host to query against
*/
protected $host;
/**
* @var $source the twitter api 'source'
*/
protected $source = 'drupal';
/**
* @var $username Twitter username to use for authenticated requests
*/
protected $username;
/**
* @var $password Twitter password to use for authenticated requests
*/
protected $password;
/**
* Constructor for the Twitter class
*/
public function __construct($username = NULL, $password = NULL) {
if (!empty($username) && !empty($password)) {
$this->set_auth($username, $password);
}
$this->host = variable_get('twitter_host', 'api.twitter.com');
}
/**
* Set the username and password
*/
public function set_auth($username, $password) {
$this->username = $username;
$this->password = $password;
}
/**
* Set the current host. This is good for using laconi.ca instances
* and anything else that utilizes the Twitter API
*
* @param $host the hostname to set.
*/
public function set_host($host) {
$this->host = $host;
}
/**
* Get an array of TwitterStatus objects from an API endpoint
*/
protected function get_statuses($path, $params = array(), $use_auth = FALSE) {
$values = $this->call($path, $params, 'GET', $use_auth);
// Check on successfull call
if ($values) {
$statuses = array();
foreach ($values as $status) {
$statuses[] = new TwitterStatus($status);
}
return $statuses;
}
// Call might return FALSE , e.g. on failed authentication
else {
// As call allready throws an exception, we can return an empty array to
// break no code.
return array();
}
}
/**
* Fetch the public timeline
*
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-public_timeline
*/
public function public_timeline() {
return $this->get_statuses('statuses/public_timeline');
}
/**
* Fetch the authenticated user's friends timeline
*
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-friends_timeline
*/
public function friends_timeline($params = array()) {
return $this->get_statuses('statuses/friends_timeline', $params, TRUE);
}
/**
* Fetch a user's timeline
*
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-user_timeline
*/
public function user_timeline($id, $params = array(), $use_auth = FALSE) {
if (is_numeric($id)) {
$params['user_id'] = $id;
}
else {
$params['screen_name'] = $id;
}
return $this->get_statuses('statuses/user_timeline', $params, $use_auth);
}
/**
*
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-mentions
*/
public function mentions($params = array()) {
return $this->get_statuses('statuses/mentions', $params, TRUE);
}
/**
*
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses%C2%A0update
*/
public function status_update($status, $params = array()) {
$params['status'] = $status;
if ($this->source) {
$params['source'] = $this->source;
}
$values = $this->call('statuses/update', $params, 'POST', TRUE);
return new TwitterStatus($values);
}
/**
*
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-users%C2%A0show
*/
public function users_show($id, $use_auth = TRUE) {
$params = array();
if (is_numeric($id)) {
$params['user_id'] = $id;
}
else {
$params['screen_name'] = $id;
}
$values = $this->call('users/show', $params, 'GET', $use_auth);
return new TwitterUser($values);
}
/**
*
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-account%C2%A0verify_credentials
*/
public function verify_credentials() {
$values = $this->call('account/verify_credentials', array(), 'GET', TRUE);
if (!$values) {
return FALSE;
}
return new TwitterUser($values);
}
/**
* Method for calling any twitter api resource
*/
public function call($path, $params = array(), $method = 'GET', $use_auth = FALSE) {
$url = $this->create_url($path);
try {
if ($use_auth) {
$response = $this->auth_request($url, $params, $method);
}
else {
$response = $this->request($url, $params, $method);
}
}
catch (TwitterException $e) {
watchdog('twitter', '!message', array('!message' => $e->__toString()), WATCHDOG_ERROR);
return FALSE;
}
if (!$response) {
return FALSE;
}
return $this->parse_response($response);
}
/**
* Perform an authentication required request.
*/
protected function auth_request($path, $params = array(), $method = 'GET') {
if (empty($this->username) || empty($this->password)) {
return false;
}
return $this->request($path, $params, $method, TRUE);
}
/**
* Perform a request
*/
protected function request($url, $params = array(), $method = 'GET', $use_auth = FALSE) {
$data = '';
if (count($params) > 0) {
if ($method == 'GET') {
$url .= '?'. http_build_query($params, '', '&');
}
else {
$data = http_build_query($params, '', '&');
}
}
$headers = array();
if ($use_auth) {
$headers['Authorization'] = 'Basic '. base64_encode($this->username .':'. $this->password);
$headers['Content-type'] = 'application/x-www-form-urlencoded';
}
$response = drupal_http_request($url, array('headers' => $headers, 'method' => $method, 'data' => $data));
if (!isset($response->error)) {
return $response->data;
}
else {
$error = $response->error;
$data = $this->parse_response($response->data);
if (isset($data['error'])) {
$error = $data['error'];
}
throw new TwitterException($error);
}
}
protected function parse_response($response, $format = NULL) {
if (empty($format)) {
$format = $this->format;
}
switch ($format) {
case 'json':
// http://drupal.org/node/985544 - json_decode large integer issue
$length = strlen(PHP_INT_MAX);
$response = preg_replace('/"(id|in_reply_to_status_id)":(\d{' . $length . ',})/', '"\1":"\2"', $response);
return json_decode($response, TRUE);
}
}
protected function create_url($path, $format = NULL) {
if (is_null($format)) {
$format = $this->format;
}
$url = 'http://'. $this->host .'/'. $path;
if (!empty($format)) {
$url .= '.'. $this->format;
}
return $url;
}
}
/**
* A class to provide OAuth enabled access to the twitter API
*/
class TwitterOAuth extends Twitter {
protected $signature_method;
protected $consumer;
protected $token;
public function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
$this->signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
if (!empty($oauth_token) && !empty($oauth_token_secret)) {
$this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
}
}
public function get_request_token() {
$url = $this->create_url('oauth/request_token', '');
try {
$response = $this->auth_request($url);
}
catch (TwitterException $e) {
}
parse_str($response, $token);
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
return $token;
}
public function get_authorize_url($token) {
$url = $this->create_url('oauth/authorize', '');
$url.= '?oauth_token=' . $token['oauth_token'];
return $url;
}
public function get_authenticate_url($token) {
$url = $this->create_url('oauth/authenticate', '');
$url.= '?oauth_token=' . $token['oauth_token'];
return $url;
}
public function get_access_token() {
$url = $this->create_url('oauth/access_token', '');
try {
$response = $this->auth_request($url);
}
catch (TwitterException $e) {
}
parse_str($response, $token);
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
return $token;
}
public function auth_request($url, $params = array(), $method = 'GET') {
$request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $params);
$request->sign_request($this->signature_method, $this->consumer, $this->token);
switch ($method) {
case 'GET':
return $this->request($request->to_url());
case 'POST':
return $this->request($request->get_normalized_http_url(), $request->get_parameters(), 'POST');
}
}
}
class TwitterSearch extends Twitter {
protected $host;
/**
* Constructor for the Twitter class
*/
public function __construct() {
$this->host = variable_get('twitter_search_host', 'search.twitter.com');
}
public function search($params = array()) {
}
}
/**
* Class for containing an individual twitter status.
*/
class TwitterStatus {
/**
* @var created_at
*/
public $created_at;
public $id;
public $text;
public $source;
public $truncated;
public $favorited;
public $in_reply_to_status_id;
public $in_reply_to_user_id;
public $in_reply_to_screen_name;
public $user;
/**
* Constructor for TwitterStatus
*/
public function __construct($values = array()) {
$this->created_at = $values['created_at'];
$this->id = $values['id'];
$this->text = $values['text'];
$this->source = $values['source'];
$this->truncated = $values['truncated'];
$this->favorited = $values['favorited'];
$this->in_reply_to_status_id = $values['in_reply_to_status_id'];
$this->in_reply_to_user_id = $values['in_reply_to_user_id'];
$this->in_reply_to_screen_name = $values['in_reply_to_screen_name'];
if (isset($values['user'])) {
$this->user = new TwitterUser($values['user']);
}
}
}
class TwitterUser {
public $id;
public $screen_name;
public $name;
public $location;
public $description;
public $followers_count;
public $friends_count;
public $statuses_count;
public $favourites_count;
public $url;
public $protected;
public $profile_image_url;
public $profile_background_color;
public $profile_text_color;
public $profile_link_color;
public $profile_sidebar_fill_color;
public $profile_sidebar_border_color;
public $profile_background_image_url;
public $profile_background_tile;
public $verified;
public $created_at;
public $created_time;
public $utc_offset;
public $status;
protected $password;
protected $oauth_token;
protected $oauth_token_secret;
public function __construct($values = array()) {
$this->id = $values['id'];
$this->screen_name = $values['screen_name'];
$this->name = $values['name'];
$this->location = $values['location'];
$this->description = $values['description'];
$this->url = $values['url'];
$this->followers_count = $values['followers_count'];
$this->friends_count = $values['friends_count'];
$this->statuses_count = $values['statuses_count'];
$this->favourites_count = $values['favourites_count'];
$this->protected = $values['protected'];
$this->profile_image_url = $values['profile_image_url'];
$this->profile_background_color = $values['profile_background_color'];
$this->profile_text_color = $values['profile_text_color'];
$this->profile_link_color = $values['profile_link_color'];
$this->profile_sidebar_fill_color = $values['profile_sidebar_fill_color'];
$this->profile_sidebar_border_color = $values['profile_sidebar_border_color'];
$this->profile_background_image_url = $values['profile_background_image_url'];
$this->profile_background_tile = $values['profile_background_tile'];
$this->verified = $values['verified'];
$this->created_at = $values['created_at'];
if ($values['created_at'] && $created_time = strtotime($values['created_at'])) {
$this->created_time = $created_time;
}
$this->utc_offset = $values['utc_offset']?$values['utc_offset']:0;
if (isset($values['status'])) {
$this->status = new TwitterStatus($values['status']);
}
}
public function get_auth() {
return array('password' => $this->password, 'oauth_token' => $this->oauth_token, 'oauth_token_secret' => $this->oauth_token_secret);
}
public function set_auth($values) {
$this->oauth_token = isset($values['oauth_token'])?$values['oauth_token']:NULL;
$this->oauth_token_secret = isset($values['oauth_token_secret'])?$values['oauth_token_secret']:NULL;
}
}