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
27 function handle($args)
29 parent::handle($args);
32 function twitter_user_array($profile, $get_notice=false)
35 $twitter_user = array();
37 $twitter_user['name'] = $profile->getBestName();
38 $twitter_user['followers_count'] = $this->count_subscriptions($profile);
39 $twitter_user['screen_name'] = $profile->nickname;
40 $twitter_user['description'] = ($profile->bio) ? $profile->bio : null;
41 $twitter_user['location'] = ($profile->location) ? $profile->location : null;
42 $twitter_user['id'] = intval($profile->id);
44 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
46 $twitter_user['profile_image_url'] = ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE);
47 $twitter_user['protected'] = 'false'; # not supported by Laconica yet
48 $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
51 $notice = $profile->getCurrentNotice();
54 $twitter_user['status'] = $this->twitter_status_array($notice, false);
61 function twitter_status_array($notice, $include_user=true)
64 $profile = $notice->getProfile();
66 $twitter_status = array();
67 $twitter_status['text'] = $notice->content;
68 $twitter_status['truncated'] = 'false'; # Not possible on Laconica
69 $twitter_status['created_at'] = $this->date_twitter($notice->created);
70 $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ? intval($notice->reply_to) : null;
71 $twitter_status['source'] = $this->source_link($notice->source);
72 $twitter_status['id'] = intval($notice->id);
73 $twitter_status['in_reply_to_user_id'] = ($notice->reply_to) ? $this->replier_by_reply(intval($notice->reply_to)) : null;
75 if (isset($this->auth_user)) {
76 $twitter_status['favorited'] = ($this->auth_user->hasFave($notice)) ? 'true' : 'false';
78 $twitter_status['favorited'] = 'false';
82 # Don't get notice (recursive!)
83 $twitter_user = $this->twitter_user_array($profile, false);
84 $twitter_status['user'] = $twitter_user;
87 return $twitter_status;
90 function twitter_rss_entry_array($notice)
93 $profile = $notice->getProfile();
95 $server = common_config('site', 'server');
98 # We trim() to avoid extraneous whitespace in the output
100 $entry['content'] = common_xml_safe_str(trim($notice->rendered));
101 $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
102 $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
103 $entry['published'] = common_date_iso8601($notice->created);
104 $entry['id'] = "tag:$server,2008:$entry[link]";
105 $entry['updated'] = $entry['published'];
108 $entry['description'] = $entry['content'];
109 $entry['pubDate'] = common_date_rfc2822($notice->created);
110 $entry['guid'] = $entry['link'];
115 function twitter_rss_dmsg_array($message)
118 $server = common_config('site', 'server');
121 $entry['title'] = sprintf('Message from %s to %s',
122 $message->getFrom()->nickname, $message->getTo()->nickname);
124 $entry['content'] = common_xml_safe_str(trim($message->content));
125 $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
126 $entry['published'] = common_date_iso8601($message->created);
127 $entry['id'] = "tag:$server,2008:$entry[link]";
128 $entry['updated'] = $entry['published'];
131 $entry['description'] = $entry['content'];
132 $entry['pubDate'] = common_date_rfc2822($message->created);
133 $entry['guid'] = $entry['link'];
138 function twitter_dmsg_array($message)
141 $twitter_dm = array();
143 $from_profile = $message->getFrom();
144 $to_profile = $message->getTo();
146 $twitter_dm['id'] = $message->id;
147 $twitter_dm['sender_id'] = $message->from_profile;
148 $twitter_dm['text'] = trim($message->content);
149 $twitter_dm['recipient_id'] = $message->to_profile;
150 $twitter_dm['created_at'] = $this->date_twitter($message->created);
151 $twitter_dm['sender_screen_name'] = $from_profile->nickname;
152 $twitter_dm['recipient_screen_name'] = $to_profile->nickname;
153 $twitter_dm['sender'] = $this->twitter_user_array($from_profile, false);
154 $twitter_dm['recipient'] = $this->twitter_user_array($to_profile, false);
159 function show_twitter_xml_status($twitter_status)
161 common_element_start('status');
162 foreach($twitter_status as $element => $value) {
165 $this->show_twitter_xml_user($twitter_status['user']);
168 common_element($element, null, common_xml_safe_str($value));
171 common_element($element, null, $value);
174 common_element_end('status');
177 function show_twitter_xml_user($twitter_user, $role='user')
179 common_element_start($role);
180 foreach($twitter_user as $element => $value) {
181 if ($element == 'status') {
182 $this->show_twitter_xml_status($twitter_user['status']);
184 common_element($element, null, $value);
187 common_element_end($role);
190 function show_twitter_rss_item($entry)
192 common_element_start('item');
193 common_element('title', null, $entry['title']);
194 common_element('description', null, $entry['description']);
195 common_element('pubDate', null, $entry['pubDate']);
196 common_element('guid', null, $entry['guid']);
197 common_element('link', null, $entry['link']);
198 common_element_end('item');
201 function show_twitter_atom_entry($entry)
203 common_element_start('entry');
204 common_element('title', null, $entry['title']);
205 common_element('content', array('type' => 'html'), $entry['content']);
206 common_element('id', null, $entry['id']);
207 common_element('published', null, $entry['published']);
208 common_element('updated', null, $entry['updated']);
209 common_element('link', array('href' => $entry['link'], 'rel' => 'alternate', 'type' => 'text/html'), null);
210 common_element_end('entry');
213 function show_json_objects($objects)
215 print(json_encode($objects));
218 function show_single_xml_status($notice)
220 $this->init_document('xml');
221 $twitter_status = $this->twitter_status_array($notice);
222 $this->show_twitter_xml_status($twitter_status);
223 $this->end_document('xml');
226 function show_single_json_status($notice)
228 $this->init_document('json');
229 $status = $this->twitter_status_array($notice);
230 $this->show_json_objects($status);
231 $this->end_document('json');
234 function show_single_xml_dmsg($message)
236 $this->init_document('xml');
237 $dmsg = $this->twitter_dmsg_array($message);
238 $this->show_twitter_xml_dmsg($dmsg);
239 $this->end_document('xml');
242 function show_single_json_dmsg($message)
244 $this->init_document('json');
245 $dmsg = $this->twitter_dmsg_array($message);
246 $this->show_json_objects($dmsg);
247 $this->end_document('json');
250 function show_twitter_xml_dmsg($twitter_dm)
252 common_element_start('direct_message');
253 foreach($twitter_dm as $element => $value) {
257 $this->show_twitter_xml_user($value, $element);
260 common_element($element, null, common_xml_safe_str($value));
263 common_element($element, null, $value);
266 common_element_end('direct_message');
269 function show_xml_timeline($notice)
272 $this->init_document('xml');
273 common_element_start('statuses', array('type' => 'array'));
275 if (is_array($notice)) {
276 foreach ($notice as $n) {
277 $twitter_status = $this->twitter_status_array($n);
278 $this->show_twitter_xml_status($twitter_status);
281 while ($notice->fetch()) {
282 $twitter_status = $this->twitter_status_array($notice);
283 $this->show_twitter_xml_status($twitter_status);
287 common_element_end('statuses');
288 $this->end_document('xml');
291 function show_rss_timeline($notice, $title, $link, $subtitle, $suplink=null)
294 $this->init_document('rss');
296 common_element_start('channel');
297 common_element('title', null, $title);
298 common_element('link', null, $link);
299 if (!is_null($suplink)) {
300 # For FriendFeed's SUP protocol
301 common_element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
302 'rel' => 'http://api.friendfeed.com/2008/03#sup',
304 'type' => 'application/json'));
306 common_element('description', null, $subtitle);
307 common_element('language', null, 'en-us');
308 common_element('ttl', null, '40');
310 if (is_array($notice)) {
311 foreach ($notice as $n) {
312 $entry = $this->twitter_rss_entry_array($n);
313 $this->show_twitter_rss_item($entry);
316 while ($notice->fetch()) {
317 $entry = $this->twitter_rss_entry_array($notice);
318 $this->show_twitter_rss_item($entry);
322 common_element_end('channel');
323 $this->end_twitter_rss();
326 function show_atom_timeline($notice, $title, $id, $link, $subtitle=null, $suplink=null)
329 $this->init_document('atom');
331 common_element('title', null, $title);
332 common_element('id', null, $id);
333 common_element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
334 if (!is_null($suplink)) {
335 # For FriendFeed's SUP protocol
336 common_element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
338 'type' => 'application/json'));
340 common_element('subtitle', null, $subtitle);
342 if (is_array($notice)) {
343 foreach ($notice as $n) {
344 $entry = $this->twitter_rss_entry_array($n);
345 $this->show_twitter_atom_entry($entry);
348 while ($notice->fetch()) {
349 $entry = $this->twitter_rss_entry_array($notice);
350 $this->show_twitter_atom_entry($entry);
354 $this->end_document('atom');
358 function show_json_timeline($notice)
361 $this->init_document('json');
365 if (is_array($notice)) {
366 foreach ($notice as $n) {
367 $twitter_status = $this->twitter_status_array($n);
368 array_push($statuses, $twitter_status);
371 while ($notice->fetch()) {
372 $twitter_status = $this->twitter_status_array($notice);
373 array_push($statuses, $twitter_status);
377 $this->show_json_objects($statuses);
379 $this->end_document('json');
382 // Anyone know what date format this is?
383 // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
384 function date_twitter($dt)
387 return date("D M d G:i:s O Y", $t);
390 function replier_by_reply($reply_id)
392 $notice = Notice::staticGet($reply_id);
394 $profile = $notice->getProfile();
396 return intval($profile->id);
398 common_debug('Can\'t find a profile for notice: ' . $notice->id, __FILE__);
401 common_debug("Can't get notice: $reply_id", __FILE__);
406 // XXX: Candidate for a general utility method somewhere?
407 function count_subscriptions($profile)
411 $sub = new Subscription();
412 $sub->subscribed = $profile->id;
414 $count = $sub->find();
423 function init_document($type='xml')
427 header('Content-Type: application/xml; charset=utf-8');
431 header('Content-Type: application/json; charset=utf-8');
433 // Check for JSONP callback
434 $callback = $this->arg('callback');
436 print $callback . '(';
440 header("Content-Type: application/rss+xml; charset=utf-8");
441 $this->init_twitter_rss();
444 header('Content-Type: application/atom+xml; charset=utf-8');
445 $this->init_twitter_atom();
448 $this->client_error(_('Not a supported data format.'));
455 function end_document($type='xml')
463 // Check for JSONP callback
464 $callback = $this->arg('callback');
470 $this->end_twitter_rss();
473 $this->end_twitter_rss();
476 $this->client_error(_('Not a supported data format.'));
482 function client_error($msg, $code = 400, $content_type = 'json')
485 static $status = array(400 => 'Bad Request',
486 401 => 'Unauthorized',
487 402 => 'Payment Required',
490 405 => 'Method Not Allowed',
491 406 => 'Not Acceptable',
492 407 => 'Proxy Authentication Required',
493 408 => 'Request Timeout',
496 411 => 'Length Required',
497 412 => 'Precondition Failed',
498 413 => 'Request Entity Too Large',
499 414 => 'Request-URI Too Long',
500 415 => 'Unsupported Media Type',
501 416 => 'Requested Range Not Satisfiable',
502 417 => 'Expectation Failed');
504 $action = $this->trimmed('action');
506 common_debug("User error '$code' on '$action': $msg", __FILE__);
508 if (!array_key_exists($code, $status)) {
512 $status_string = $status[$code];
513 header('HTTP/1.1 '.$code.' '.$status_string);
515 if ($content_type == 'xml') {
516 $this->init_document('xml');
517 common_element_start('hash');
518 common_element('error', null, $msg);
519 common_element('request', null, $_SERVER['REQUEST_URI']);
520 common_element_end('hash');
521 $this->end_document('xml');
523 $this->init_document('json');
524 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
525 print(json_encode($error_array));
526 $this->end_document('json');
531 function init_twitter_rss()
534 common_element_start('rss', array('version' => '2.0'));
537 function end_twitter_rss()
539 common_element_end('rss');
543 function init_twitter_atom()
546 common_element_start('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US'));
549 function end_twitter_atom()
552 common_element_end('feed');
555 function show_profile($profile, $content_type='xml', $notice=null)
557 $profile_array = $this->twitter_user_array($profile, true);
558 switch ($content_type) {
560 $this->show_twitter_xml_user($profile_array);
563 $this->show_json_objects($profile_array);
566 $this->client_error(_('Not a supported data format.'));
572 function get_user($id, $apidata=null)
575 return $apidata['user'];
576 } else if (is_numeric($id)) {
577 return User::staticGet($id);
579 $nickname = common_canonical_nickname($id);
580 return User::staticGet('nickname', $nickname);
584 function get_profile($id)
586 if (is_numeric($id)) {
587 return Profile::staticGet($id);
589 $user = User::staticGet('nickname', $id);
591 return $user->getProfile();
598 function source_link($source)
600 $source_name = _($source);
609 $ns = Notice_source::staticGet($source);
611 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
618 function show_extended_profile($user, $apidata)
621 $this->auth_user = $apidata['user'];
623 $profile = $user->getProfile();
626 common_server_error(_('User has no profile.'));
630 $twitter_user = $this->twitter_user_array($profile, true);
632 // Add in extended user fields offered up by this method
633 $twitter_user['created_at'] = $this->date_twitter($profile->created);
635 $subbed = DB_DataObject::factory('subscription');
636 $subbed->subscriber = $profile->id;
637 $subbed_count = (int) $subbed->count() - 1;
639 $notices = DB_DataObject::factory('notice');
640 $notices->profile_id = $profile->id;
641 $notice_count = (int) $notices->count();
643 $twitter_user['friends_count'] = (is_int($subbed_count)) ? $subbed_count : 0;
644 $twitter_user['statuses_count'] = (is_int($notice_count)) ? $notice_count : 0;
646 // Other fields Twitter sends...
647 $twitter_user['profile_background_color'] = '';
648 $twitter_user['profile_text_color'] = '';
649 $twitter_user['profile_link_color'] = '';
650 $twitter_user['profile_sidebar_fill_color'] = '';
652 $faves = DB_DataObject::factory('fave');
653 $faves->user_id = $user->id;
654 $faves_count = (int) $faves->count();
655 $twitter_user['favourites_count'] = $faves_count;
659 if ($user->timezone) {
660 $timezone = $user->timezone;
664 $t->setTimezone(new DateTimeZone($timezone));
665 $twitter_user['utc_offset'] = $t->format('Z');
666 $twitter_user['time_zone'] = $timezone;
668 $following = 'false';
670 if (isset($this->auth_user)) {
671 if ($this->auth_user->isSubscribed($profile)) {
675 // Not implemented yet
676 $twitter_user['notifications'] = 'false';
679 $twitter_user['following'] = $following;
681 if ($apidata['content-type'] == 'xml') {
682 $this->init_document('xml');
683 $this->show_twitter_xml_user($twitter_user);
684 $this->end_document('xml');
685 } elseif ($apidata['content-type'] == 'json') {
686 $this->init_document('json');
687 $this->show_json_objects($twitter_user);
688 $this->end_document('json');