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