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