3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 class TwitterapiAction extends Action {
26 function handle($args)
28 parent::handle($args);
31 function twitter_user_array($profile, $get_notice=false)
34 $twitter_user = array();
36 $twitter_user['name'] = $profile->getBestName();
37 $twitter_user['followers_count'] = $this->count_subscriptions($profile);
38 $twitter_user['screen_name'] = $profile->nickname;
39 $twitter_user['description'] = ($profile->bio) ? $profile->bio : null;
40 $twitter_user['location'] = ($profile->location) ? $profile->location : null;
41 $twitter_user['id'] = intval($profile->id);
43 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
45 $twitter_user['profile_image_url'] = ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE);
46 $twitter_user['protected'] = 'false'; # not supported by Laconica yet
47 $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
50 $notice = $profile->getCurrentNotice();
53 $twitter_user['status'] = $this->twitter_status_array($notice, false);
60 function twitter_status_array($notice, $include_user=true)
63 $profile = $notice->getProfile();
65 $twitter_status = array();
66 $twitter_status['text'] = $notice->content;
67 $twitter_status['truncated'] = 'false'; # Not possible on Laconica
68 $twitter_status['created_at'] = $this->date_twitter($notice->created);
69 $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ? intval($notice->reply_to) : null;
70 $twitter_status['source'] = $this->source_link($notice->source);
71 $twitter_status['id'] = intval($notice->id);
72 $twitter_status['in_reply_to_user_id'] = ($notice->reply_to) ? $this->replier_by_reply(intval($notice->reply_to)) : null;
74 if (isset($this->auth_user)) {
75 $twitter_status['favorited'] = ($this->auth_user->hasFave($notice)) ? 'true' : 'false';
77 $twitter_status['favorited'] = 'false';
81 # Don't get notice (recursive!)
82 $twitter_user = $this->twitter_user_array($profile, false);
83 $twitter_status['user'] = $twitter_user;
86 return $twitter_status;
89 function twitter_rss_entry_array($notice)
92 $profile = $notice->getProfile();
94 $server = common_config('site', 'server');
97 # We trim() to avoid extraneous whitespace in the output
99 $entry['content'] = common_xml_safe_str(trim($notice->rendered));
100 $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
101 $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
102 $entry['published'] = common_date_iso8601($notice->created);
103 $entry['id'] = "tag:$server,2008:$entry[link]";
104 $entry['updated'] = $entry['published'];
107 $entry['description'] = $entry['content'];
108 $entry['pubDate'] = common_date_rfc2822($notice->created);
109 $entry['guid'] = $entry['link'];
114 function twitter_rss_dmsg_array($message)
117 $server = common_config('site', 'server');
120 $entry['title'] = sprintf('Message from %s to %s',
121 $message->getFrom()->nickname, $message->getTo()->nickname);
123 $entry['content'] = common_xml_safe_str(trim($message->content));
124 $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
125 $entry['published'] = common_date_iso8601($message->created);
126 $entry['id'] = "tag:$server,2008:$entry[link]";
127 $entry['updated'] = $entry['published'];
130 $entry['description'] = $entry['content'];
131 $entry['pubDate'] = common_date_rfc2822($message->created);
132 $entry['guid'] = $entry['link'];
137 function twitter_dmsg_array($message)
140 $twitter_dm = array();
142 $from_profile = $message->getFrom();
143 $to_profile = $message->getTo();
145 $twitter_dm['id'] = $message->id;
146 $twitter_dm['sender_id'] = $message->from_profile;
147 $twitter_dm['text'] = trim($message->content);
148 $twitter_dm['recipient_id'] = $message->to_profile;
149 $twitter_dm['created_at'] = $this->date_twitter($message->created);
150 $twitter_dm['sender_screen_name'] = $from_profile->nickname;
151 $twitter_dm['recipient_screen_name'] = $to_profile->nickname;
152 $twitter_dm['sender'] = $this->twitter_user_array($from_profile, false);
153 $twitter_dm['recipient'] = $this->twitter_user_array($to_profile, false);
158 function show_twitter_xml_status($twitter_status)
160 common_element_start('status');
161 foreach($twitter_status as $element => $value) {
164 $this->show_twitter_xml_user($twitter_status['user']);
167 common_element($element, null, common_xml_safe_str($value));
170 common_element($element, null, $value);
173 common_element_end('status');
176 function show_twitter_xml_user($twitter_user, $role='user')
178 common_element_start($role);
179 foreach($twitter_user as $element => $value) {
180 if ($element == 'status') {
181 $this->show_twitter_xml_status($twitter_user['status']);
183 common_element($element, null, $value);
186 common_element_end($role);
189 function show_twitter_rss_item($entry)
191 common_element_start('item');
192 common_element('title', null, $entry['title']);
193 common_element('description', null, $entry['description']);
194 common_element('pubDate', null, $entry['pubDate']);
195 common_element('guid', null, $entry['guid']);
196 common_element('link', null, $entry['link']);
197 common_element_end('item');
200 function show_twitter_atom_entry($entry)
202 common_element_start('entry');
203 common_element('title', null, $entry['title']);
204 common_element('content', array('type' => 'html'), $entry['content']);
205 common_element('id', null, $entry['id']);
206 common_element('published', null, $entry['published']);
207 common_element('updated', null, $entry['updated']);
208 common_element('link', array('href' => $entry['link'], 'rel' => 'alternate', 'type' => 'text/html'), null);
209 common_element_end('entry');
212 function show_json_objects($objects)
214 print(json_encode($objects));
217 function show_single_xml_status($notice)
219 $this->init_document('xml');
220 $twitter_status = $this->twitter_status_array($notice);
221 $this->show_twitter_xml_status($twitter_status);
222 $this->end_document('xml');
225 function show_single_json_status($notice)
227 $this->init_document('json');
228 $status = $this->twitter_status_array($notice);
229 $this->show_json_objects($status);
230 $this->end_document('json');
233 function show_single_xml_dmsg($message)
235 $this->init_document('xml');
236 $dmsg = $this->twitter_dmsg_array($message);
237 $this->show_twitter_xml_dmsg($dmsg);
238 $this->end_document('xml');
241 function show_single_json_dmsg($message)
243 $this->init_document('json');
244 $dmsg = $this->twitter_dmsg_array($message);
245 $this->show_json_objects($dmsg);
246 $this->end_document('json');
249 function show_twitter_xml_dmsg($twitter_dm)
251 common_element_start('direct_message');
252 foreach($twitter_dm as $element => $value) {
256 $this->show_twitter_xml_user($value, $element);
259 common_element($element, null, common_xml_safe_str($value));
262 common_element($element, null, $value);
265 common_element_end('direct_message');
268 function show_xml_timeline($notice)
271 $this->init_document('xml');
272 common_element_start('statuses', array('type' => 'array'));
274 if (is_array($notice)) {
275 foreach ($notice as $n) {
276 $twitter_status = $this->twitter_status_array($n);
277 $this->show_twitter_xml_status($twitter_status);
280 while ($notice->fetch()) {
281 $twitter_status = $this->twitter_status_array($notice);
282 $this->show_twitter_xml_status($twitter_status);
286 common_element_end('statuses');
287 $this->end_document('xml');
290 function show_rss_timeline($notice, $title, $link, $subtitle, $suplink=null)
293 $this->init_document('rss');
295 common_element_start('channel');
296 common_element('title', null, $title);
297 common_element('link', null, $link);
298 if (!is_null($suplink)) {
299 # For FriendFeed's SUP protocol
300 common_element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
301 'rel' => 'http://api.friendfeed.com/2008/03#sup',
303 'type' => 'application/json'));
305 common_element('description', null, $subtitle);
306 common_element('language', null, 'en-us');
307 common_element('ttl', null, '40');
309 if (is_array($notice)) {
310 foreach ($notice as $n) {
311 $entry = $this->twitter_rss_entry_array($n);
312 $this->show_twitter_rss_item($entry);
315 while ($notice->fetch()) {
316 $entry = $this->twitter_rss_entry_array($notice);
317 $this->show_twitter_rss_item($entry);
321 common_element_end('channel');
322 $this->end_twitter_rss();
325 function show_atom_timeline($notice, $title, $id, $link, $subtitle=null, $suplink=null)
328 $this->init_document('atom');
330 common_element('title', null, $title);
331 common_element('id', null, $id);
332 common_element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
333 if (!is_null($suplink)) {
334 # For FriendFeed's SUP protocol
335 common_element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
337 'type' => 'application/json'));
339 common_element('subtitle', null, $subtitle);
341 if (is_array($notice)) {
342 foreach ($notice as $n) {
343 $entry = $this->twitter_rss_entry_array($n);
344 $this->show_twitter_atom_entry($entry);
347 while ($notice->fetch()) {
348 $entry = $this->twitter_rss_entry_array($notice);
349 $this->show_twitter_atom_entry($entry);
353 $this->end_document('atom');
357 function show_json_timeline($notice)
360 $this->init_document('json');
364 if (is_array($notice)) {
365 foreach ($notice as $n) {
366 $twitter_status = $this->twitter_status_array($n);
367 array_push($statuses, $twitter_status);
370 while ($notice->fetch()) {
371 $twitter_status = $this->twitter_status_array($notice);
372 array_push($statuses, $twitter_status);
376 $this->show_json_objects($statuses);
378 $this->end_document('json');
381 // Anyone know what date format this is?
382 // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
383 function date_twitter($dt)
386 return date("D M d G:i:s O Y", $t);
389 function replier_by_reply($reply_id)
391 $notice = Notice::staticGet($reply_id);
393 $profile = $notice->getProfile();
395 return intval($profile->id);
397 common_debug('Can\'t find a profile for notice: ' . $notice->id, __FILE__);
400 common_debug("Can't get notice: $reply_id", __FILE__);
405 // XXX: Candidate for a general utility method somewhere?
406 function count_subscriptions($profile)
410 $sub = new Subscription();
411 $sub->subscribed = $profile->id;
413 $count = $sub->find();
422 function init_document($type='xml')
426 header('Content-Type: application/xml; charset=utf-8');
430 header('Content-Type: application/json; charset=utf-8');
432 // Check for JSONP callback
433 $callback = $this->arg('callback');
435 print $callback . '(';
439 header("Content-Type: application/rss+xml; charset=utf-8");
440 $this->init_twitter_rss();
443 header('Content-Type: application/atom+xml; charset=utf-8');
444 $this->init_twitter_atom();
447 $this->client_error(_('Not a supported data format.'));
454 function end_document($type='xml')
462 // Check for JSONP callback
463 $callback = $this->arg('callback');
469 $this->end_twitter_rss();
472 $this->end_twitter_rss();
475 $this->client_error(_('Not a supported data format.'));
481 function client_error($msg, $code = 400, $content_type = 'json')
484 static $status = array(400 => 'Bad Request',
485 401 => 'Unauthorized',
486 402 => 'Payment Required',
489 405 => 'Method Not Allowed',
490 406 => 'Not Acceptable',
491 407 => 'Proxy Authentication Required',
492 408 => 'Request Timeout',
495 411 => 'Length Required',
496 412 => 'Precondition Failed',
497 413 => 'Request Entity Too Large',
498 414 => 'Request-URI Too Long',
499 415 => 'Unsupported Media Type',
500 416 => 'Requested Range Not Satisfiable',
501 417 => 'Expectation Failed');
503 $action = $this->trimmed('action');
505 common_debug("User error '$code' on '$action': $msg", __FILE__);
507 if (!array_key_exists($code, $status)) {
511 $status_string = $status[$code];
512 header('HTTP/1.1 '.$code.' '.$status_string);
514 if ($content_type == 'xml') {
515 $this->init_document('xml');
516 common_element_start('hash');
517 common_element('error', null, $msg);
518 common_element('request', null, $_SERVER['REQUEST_URI']);
519 common_element_end('hash');
520 $this->end_document('xml');
522 $this->init_document('json');
523 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
524 print(json_encode($error_array));
525 $this->end_document('json');
530 function init_twitter_rss()
533 common_element_start('rss', array('version' => '2.0'));
536 function end_twitter_rss()
538 common_element_end('rss');
542 function init_twitter_atom()
545 common_element_start('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US'));
548 function end_twitter_atom()
551 common_element_end('feed');
554 function show_profile($profile, $content_type='xml', $notice=null)
556 $profile_array = $this->twitter_user_array($profile, true);
557 switch ($content_type) {
559 $this->show_twitter_xml_user($profile_array);
562 $this->show_json_objects($profile_array);
565 $this->client_error(_('Not a supported data format.'));
571 function get_user($id, $apidata=null)
574 return $apidata['user'];
575 } else if (is_numeric($id)) {
576 return User::staticGet($id);
578 $nickname = common_canonical_nickname($id);
579 return User::staticGet('nickname', $nickname);
583 function get_profile($id)
585 if (is_numeric($id)) {
586 return Profile::staticGet($id);
588 $user = User::staticGet('nickname', $id);
590 return $user->getProfile();
597 function source_link($source)
599 $source_name = _($source);
608 $ns = Notice_source::staticGet($source);
610 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
617 function show_extended_profile($user, $apidata)
620 $this->auth_user = $apidata['user'];
622 $profile = $user->getProfile();
625 common_server_error(_('User has no profile.'));
629 $twitter_user = $this->twitter_user_array($profile, true);
631 // Add in extended user fields offered up by this method
632 $twitter_user['created_at'] = $this->date_twitter($profile->created);
634 $subbed = DB_DataObject::factory('subscription');
635 $subbed->subscriber = $profile->id;
636 $subbed_count = (int) $subbed->count() - 1;
638 $notices = DB_DataObject::factory('notice');
639 $notices->profile_id = $profile->id;
640 $notice_count = (int) $notices->count();
642 $twitter_user['friends_count'] = (is_int($subbed_count)) ? $subbed_count : 0;
643 $twitter_user['statuses_count'] = (is_int($notice_count)) ? $notice_count : 0;
645 // Other fields Twitter sends...
646 $twitter_user['profile_background_color'] = '';
647 $twitter_user['profile_text_color'] = '';
648 $twitter_user['profile_link_color'] = '';
649 $twitter_user['profile_sidebar_fill_color'] = '';
651 $faves = DB_DataObject::factory('fave');
652 $faves->user_id = $user->id;
653 $faves_count = (int) $faves->count();
654 $twitter_user['favourites_count'] = $faves_count;
658 if ($user->timezone) {
659 $timezone = $user->timezone;
663 $t->setTimezone(new DateTimeZone($timezone));
664 $twitter_user['utc_offset'] = $t->format('Z');
665 $twitter_user['time_zone'] = $timezone;
667 $following = 'false';
669 if (isset($this->auth_user)) {
670 if ($this->auth_user->isSubscribed($profile)) {
674 // Not implemented yet
675 $twitter_user['notifications'] = 'false';
678 $twitter_user['following'] = $following;
680 if ($apidata['content-type'] == 'xml') {
681 $this->init_document('xml');
682 $this->show_twitter_xml_user($twitter_user);
683 $this->end_document('xml');
684 } elseif ($apidata['content-type'] == 'json') {
685 $this->init_document('json');
686 $this->show_json_objects($twitter_user);
687 $this->end_document('json');