]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/twitterapi.php
e2b0ba164d016edf947f734a8f8a3b79eefc44b3
[quix0rs-gnu-social.git] / lib / twitterapi.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 class TwitterapiAction extends Action {
23
24         var $auth_user;
25
26         function handle($args) {
27                 parent::handle($args);
28         }
29
30         function twitter_user_array($profile, $get_notice=false) {
31
32                 $twitter_user = array();
33
34                 $twitter_user['name'] = $profile->getBestName();
35                 $twitter_user['followers_count'] = $this->count_subscriptions($profile);
36                 $twitter_user['screen_name'] = $profile->nickname;
37                 $twitter_user['description'] = ($profile->bio) ? $profile->bio : NULL;
38                 $twitter_user['location'] = ($profile->location) ? $profile->location : NULL;
39                 $twitter_user['id'] = intval($profile->id);
40
41                 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
42
43                 $twitter_user['profile_image_url'] = ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE);
44                 $twitter_user['protected'] = 'false'; # not supported by Laconica yet
45                 $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : NULL;
46
47                 if ($get_notice) {
48                         $notice = $profile->getCurrentNotice();
49                         if ($notice) {
50                                 # don't get user!
51                                 $twitter_user['status'] = $this->twitter_status_array($notice, false);
52                         }
53                 }
54
55                 return $twitter_user;
56         }
57
58         function twitter_status_array($notice, $include_user=true) {
59
60                 $profile = $notice->getProfile();
61
62                 $twitter_status = array();
63                 $twitter_status['text'] = $notice->content;
64                 $twitter_status['truncated'] = 'false'; # Not possible on Laconica
65                 $twitter_status['created_at'] = $this->date_twitter($notice->created);
66                 $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ? intval($notice->reply_to) : NULL;
67                 $twitter_status['source'] = $this->source_link($notice->source);
68                 $twitter_status['id'] = intval($notice->id);
69                 $twitter_status['in_reply_to_user_id'] = ($notice->reply_to) ? $this->replier_by_reply(intval($notice->reply_to)) : NULL;
70
71                 if (isset($this->auth_user)) {
72                         common_debug("auth user set: " . $this->auth_user->nickname);
73                         $twitter_status['favorited'] = ($this->auth_user->hasFave($notice)) ? 'true' : 'false';
74                 } else {
75                         common_debug("no auth user set");
76                         $twitter_status['favorited'] = 'false';
77                 }
78
79                 if ($include_user) {
80                         # Don't get notice (recursive!)
81                         $twitter_user = $this->twitter_user_array($profile, false, $user);
82                         $twitter_status['user'] = $twitter_user;
83                 }
84
85                 return $twitter_status;
86         }
87
88         function twitter_rss_entry_array($notice) {
89
90                 $profile = $notice->getProfile();
91
92                 $server = common_config('site', 'server');
93                 $entry = array();
94
95                 $entry['content'] = $profile->nickname . ': ' . common_xml_safe_str($notice->content);
96                 $entry['title'] = $entry['content'];
97                 $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
98                 $entry['published'] = common_date_iso8601($notice->created);
99                 $entry['id'] = "tag:$server,2008:$entry[link]";
100                 $entry['updated'] = $entry['published'];
101
102                 # RSS Item specific
103                 $entry['description'] = $entry['content'];
104                 $entry['pubDate'] = common_date_rfc2822($notice->created);
105                 $entry['guid'] = $entry['link'];
106
107                 return $entry;
108         }
109
110         function twitter_rss_dmsg_array($message) {
111
112                 $server = common_config('site', 'server');
113                 $entry = array();
114
115                 $entry['title'] = sprintf('Message from %s to %s',
116                         $message->getFrom()->nickname, $message->getTo()->nickname);
117
118                 $entry['content'] = common_xml_safe_str($message->content);
119                 $entry['link'] = $message->uri;
120                 $entry['published'] = common_date_iso8601($message->created);
121                 $entry['id'] = "tag:$server,2008:$entry[link]";
122                 $entry['updated'] = $entry['published'];
123
124                 # RSS Item specific
125                 $entry['description'] = $entry['content'];
126                 $entry['pubDate'] = common_date_rfc2822($message->created);
127                 $entry['guid'] = $entry['link'];
128
129                 return $entry;
130         }
131
132         function twitter_dmsg_array($message) {
133
134                 $twitter_dm = array();
135
136                 $from_profile = $message->getFrom();
137                 $to_profile = $message->getTo();
138
139                 $twitter_dm['id'] = $message->id;
140                 $twitter_dm['sender_id'] = $message->from_profile;
141                 $twitter_dm['text'] = $message->content;
142                 $twitter_dm['recipient_id'] = $message->to_profile;
143                 $twitter_dm['created_at'] = $this->date_twitter($message->created);
144                 $twitter_dm['sender_screen_name'] = $from_profile->nickname;
145                 $twitter_dm['recipient_screen_name'] = $to_profile->nickname;
146                 $twitter_dm['sender'] = $this->twitter_user_array($from_profile, false);
147                 $twitter_dm['recipient'] = $this->twitter_user_array($to_profile, false);
148
149                 return $twitter_dm;
150         }
151
152         function show_twitter_xml_status($twitter_status) {
153                 common_element_start('status');
154                 foreach($twitter_status as $element => $value) {
155                         switch ($element) {
156                         case 'user':
157                                 $this->show_twitter_xml_user($twitter_status['user']);
158                                 break;
159                         case 'text':
160                                 common_element($element, NULL, common_xml_safe_str($value));
161                                 break;
162                         default:
163                                 common_element($element, NULL, $value);
164                         }
165                 }
166                 common_element_end('status');
167         }
168
169         function show_twitter_xml_user($twitter_user, $role='user') {
170                 common_element_start($role);
171                 foreach($twitter_user as $element => $value) {
172                         if ($element == 'status') {
173                                 $this->show_twitter_xml_status($twitter_user['status']);
174                         } else {
175                                 common_element($element, NULL, $value);
176                         }
177                 }
178                 common_element_end($role);
179         }
180
181         function show_twitter_rss_item($entry) {
182                 common_element_start('item');
183                 common_element('title', NULL, $entry['title']);
184                 common_element('description', NULL, $entry['description']);
185                 common_element('pubDate', NULL, $entry['pubDate']);
186                 common_element('guid', NULL, $entry['guid']);
187                 common_element('link', NULL, $entry['link']);
188                 common_element_end('item');
189         }
190
191         function show_twitter_atom_entry($entry) {
192             common_element_start('entry');
193                 common_element('title', NULL, $entry['title']);
194                 common_element('content', array('type' => 'html'), $entry['title']);
195                 common_element('id', NULL, $entry['id']);
196                 common_element('published', NULL, $entry['published']);
197                 common_element('updated', NULL, $entry['updated']);
198                 common_element('link', array('href' => $entry['link'], 'rel' => 'alternate', 'type' => 'text/html'), NULL);
199                 common_element_end('entry');
200         }
201
202         function show_json_objects($objects) {
203                 print(json_encode($objects));
204         }
205
206         function show_single_xml_status($notice) {
207                 $this->init_document('xml');
208                 $twitter_status = $this->twitter_status_array($notice);
209                 $this->show_twitter_xml_status($twitter_status);
210                 $this->end_document('xml');
211         }
212
213         function show_single_json_status($notice) {
214                 $this->init_document('json');
215                 $status = $this->twitter_status_array($notice);
216                 $this->show_json_objects($status);
217                 $this->end_document('json');
218         }
219
220         function show_single_xml_dmsg($message) {
221                 $this->init_document('xml');
222                 $dmsg = $this->twitter_dmsg_array($message);
223                 $this->show_twitter_xml_dmsg($dmsg);
224                 $this->end_document('xml');
225         }
226
227         function show_single_json_dmsg($message) {
228                 $this->init_document('json');
229                 $dmsg = $this->twitter_dmsg_array($message);
230                 $this->show_json_objects($dmsg);
231                 $this->end_document('json');
232         }
233
234         function show_twitter_xml_dmsg($twitter_dm) {
235                 common_element_start('direct_message');
236                 foreach($twitter_dm as $element => $value) {
237                         switch ($element) {
238                         case 'sender':
239                         case 'recipient':
240                                 $this->show_twitter_xml_user($value, $element);
241                                 break;
242                         case 'text':
243                                 common_element($element, NULL, common_xml_safe_str($value));
244                                 break;
245                         default:
246                                 common_element($element, NULL, $value);
247                         }
248                 }
249                 common_element_end('direct_message');
250         }
251
252         function show_xml_timeline($notice) {
253
254                 $this->init_document('xml');
255                 common_element_start('statuses', array('type' => 'array'));
256
257                 if (is_array($notice)) {
258                         foreach ($notice as $n) {
259                                 $twitter_status = $this->twitter_status_array($n);
260                                 $this->show_twitter_xml_status($twitter_status);
261                         }
262                 } else {
263                         while ($notice->fetch()) {
264                                 $twitter_status = $this->twitter_status_array($notice);
265                                 $this->show_twitter_xml_status($twitter_status);
266                         }
267                 }
268
269                 common_element_end('statuses');
270                 $this->end_document('xml');
271         }
272
273         function show_rss_timeline($notice, $title, $link, $subtitle, $suplink=NULL) {
274
275                 $this->init_document('rss');
276
277                 common_element_start('channel');
278                 common_element('title', NULL, $title);
279                 common_element('link', NULL, $link);
280                 if (!is_null($suplink)) {
281                         # For FriendFeed's SUP protocol
282                         common_element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
283                                                                                  'rel' => 'http://api.friendfeed.com/2008/03#sup',
284                                                                                  'href' => $suplink,
285                                                                                  'type' => 'application/json'));
286                 }
287                 common_element('description', NULL, $subtitle);
288                 common_element('language', NULL, 'en-us');
289                 common_element('ttl', NULL, '40');
290
291
292                 if (is_array($notice)) {
293                         foreach ($notice as $n) {
294                                 $entry = $this->twitter_rss_entry_array($n);
295                                 $this->show_twitter_rss_item($entry);
296                         }
297                 } else {
298                         while ($notice->fetch()) {
299                                 $entry = $this->twitter_rss_entry_array($notice);
300                                 $this->show_twitter_rss_item($entry);
301                         }
302                 }
303
304                 common_element_end('channel');
305                 $this->end_twitter_rss();
306         }
307
308         function show_atom_timeline($notice, $title, $id, $link, $subtitle=NULL, $suplink=NULL) {
309
310                 $this->init_document('atom');
311
312                 common_element('title', NULL, $title);
313                 common_element('id', NULL, $id);
314                 common_element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), NULL);
315                 if (!is_null($suplink)) {
316                         # For FriendFeed's SUP protocol
317                         common_element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
318                                                                                  'href' => $suplink,
319                                                                                  'type' => 'application/json'));
320                 }
321                 common_element('subtitle', NULL, $subtitle);
322
323                 if (is_array($notice)) {
324                         foreach ($notice as $n) {
325                                 $entry = $this->twitter_rss_entry_array($n);
326                                 $this->show_twitter_atom_entry($entry);
327                         }
328                 } else {
329                         while ($notice->fetch()) {
330                                 $entry = $this->twitter_rss_entry_array($notice);
331                                 $this->show_twitter_atom_entry($entry);
332                         }
333                 }
334
335                 $this->end_document('atom');
336
337         }
338
339         function show_json_timeline($notice) {
340
341                 $this->init_document('json');
342
343                 $statuses = array();
344
345                 if (is_array($notice)) {
346                         foreach ($notice as $n) {
347                                 $twitter_status = $this->twitter_status_array($n);
348                                 array_push($statuses, $twitter_status);
349                         }
350                 } else {
351                         while ($notice->fetch()) {
352                                 $twitter_status = $this->twitter_status_array($notice);
353                                 array_push($statuses, $twitter_status);
354                         }
355                 }
356
357                 $this->show_json_objects($statuses);
358
359                 $this->end_document('json');
360         }
361
362         // Anyone know what date format this is?
363         // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
364         function date_twitter($dt) {
365                 $t = strtotime($dt);
366                 return date("D M d G:i:s O Y", $t);
367         }
368
369         function replier_by_reply($reply_id) {
370                 $notice = Notice::staticGet($reply_id);
371                 if ($notice) {
372                         $profile = $notice->getProfile();
373                         if ($profile) {
374                                 return intval($profile->id);
375                         } else {
376                                 common_debug('Can\'t find a profile for notice: ' . $notice->id, __FILE__);
377                         }
378                 } else {
379                         common_debug("Can't get notice: $reply_id", __FILE__);
380                 }
381                 return NULL;
382         }
383
384         // XXX: Candidate for a general utility method somewhere?
385         function count_subscriptions($profile) {
386
387                 $count = 0;
388                 $sub = new Subscription();
389                 $sub->subscribed = $profile->id;
390
391                 $count = $sub->find();
392
393                 if ($count > 0) {
394                         return $count - 1;
395                 } else {
396                         return 0;
397                 }
398         }
399
400         function init_document($type='xml') {
401                 switch ($type) {
402                  case 'xml':
403                         header('Content-Type: application/xml; charset=utf-8');
404                         common_start_xml();
405                         break;
406                  case 'json':
407                         header('Content-Type: application/json; charset=utf-8');
408
409                         // Check for JSONP callback
410                         $callback = $this->arg('callback');
411                         if ($callback) {
412                                 print $callback . '(';
413                         }
414                         break;
415                  case 'rss':
416                         header("Content-Type: application/rss+xml; charset=utf-8");
417                         $this->init_twitter_rss();
418                         break;
419                  case 'atom':
420                         header('Content-Type: application/atom+xml; charset=utf-8');
421                         $this->init_twitter_atom();
422                         break;
423                  default:
424                         $this->client_error(_('Not a supported data format.'));
425                         break;
426                 }
427
428                 return;
429         }
430
431         function end_document($type='xml') {
432                 switch ($type) {
433                  case 'xml':
434                         common_end_xml();
435                         break;
436                  case 'json':
437
438                         // Check for JSONP callback
439                         $callback = $this->arg('callback');
440                         if ($callback) {
441                                 print ')';
442                         }
443                         break;
444                  case 'rss':
445                         $this->end_twitter_rss();
446                         break;
447                  case 'atom':
448                         $this->end_twitter_rss();
449                         break;
450                  default:
451                         $this->client_error(_('Not a supported data format.'));
452                         break;
453                 }
454                 return;
455         }
456
457         function client_error($msg, $code = 400, $content_type = 'json') {
458
459                 static $status = array(400 => 'Bad Request',
460                                                            401 => 'Unauthorized',
461                                                            402 => 'Payment Required',
462                                                            403 => 'Forbidden',
463                                                            404 => 'Not Found',
464                                                            405 => 'Method Not Allowed',
465                                                            406 => 'Not Acceptable',
466                                                            407 => 'Proxy Authentication Required',
467                                                            408 => 'Request Timeout',
468                                                            409 => 'Conflict',
469                                                            410 => 'Gone',
470                                                            411 => 'Length Required',
471                                                            412 => 'Precondition Failed',
472                                                            413 => 'Request Entity Too Large',
473                                                            414 => 'Request-URI Too Long',
474                                                            415 => 'Unsupported Media Type',
475                                                            416 => 'Requested Range Not Satisfiable',
476                                                            417 => 'Expectation Failed');
477
478                 $action = $this->trimmed('action');
479
480                 common_debug("User error '$code' on '$action': $msg", __FILE__);
481
482                 if (!array_key_exists($code, $status)) {
483                         $code = 400;
484                 }
485
486                 $status_string = $status[$code];
487                 header('HTTP/1.1 '.$code.' '.$status_string);
488
489                 if ($content_type == 'xml') {
490                         $this->init_document('xml');
491                         common_element_start('hash');
492                         common_element('error', NULL, $msg);
493                         common_element('request', NULL, $_SERVER['REQUEST_URI']);
494                         common_element_end('hash');
495                         $this->end_document('xml');
496                 } else {
497                         $this->init_document('json');
498                         $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
499                         print(json_encode($error_array));
500                         $this->end_document('json');
501                 }
502
503         }
504
505         function init_twitter_rss() {
506                 common_start_xml();
507                 common_element_start('rss', array('version' => '2.0'));
508         }
509
510         function end_twitter_rss() {
511                 common_element_end('rss');
512                 common_end_xml();
513         }
514
515         function init_twitter_atom() {
516                 common_start_xml();
517                 common_element_start('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US'));
518         }
519
520         function end_twitter_atom() {
521                 common_end_xml();
522                 common_element_end('feed');
523         }
524
525         function show_profile($profile, $content_type='xml', $notice=NULL) {
526                 $profile_array = $this->twitter_user_array($profile, true);
527                 switch ($content_type) {
528                  case 'xml':
529                         $this->show_twitter_xml_user($profile_array);
530                         break;
531                  case 'json':
532                         $this->show_json_objects($profile_array);
533                         break;
534                  default:
535                         $this->client_error(_('Not a supported data format.'));
536                         return;
537                 }
538                 return;
539         }
540
541         function get_user($id, $apidata=NULL) {
542                 if (!$id) {
543                         return $apidata['user'];
544                 } else if (is_numeric($id)) {
545                         return User::staticGet($id);
546                 } else {
547                         $nickname = common_canonical_nickname($id);
548                         return User::staticGet('nickname', $nickname);
549                 }
550         }
551
552         function get_profile($id) {
553                 if (is_numeric($id)) {
554                         return Profile::staticGet($id);
555                 } else {
556                         $user = User::staticGet('nickname', $id);
557                         if ($user) {
558                                 return $user->getProfile();
559                         } else {
560                                 return NULL;
561                         }
562                 }
563         }
564
565         function source_link($source) {
566                 $source_name = _($source);
567                 switch ($source) {
568                  case 'web':
569                  case 'xmpp':
570                  case 'mail':
571                  case 'omb':
572                  case 'api':
573                         break;
574                  default:
575                         $ns = Notice_source::staticGet($source);
576                         if ($ns) {
577                                 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
578                         }
579                         break;
580                 }
581                 return $source_name;
582         }
583
584 }