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