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
30 * @param array $args Web and URL arguments
32 * @return boolean false if user doesn't exist
35 function prepare($args)
37 parent::prepare($args);
44 * @param array $args Arguments from $_REQUEST
49 function handle($args)
51 parent::handle($args);
55 * Overrides XMLOutputter::element to write booleans as strings (true|false).
56 * See that method's documentation for more info.
58 * @param string $tag Element type or tagname
59 * @param array $attrs Array of element attributes, as
61 * @param string $content string content of the element
65 function element($tag, $attrs=null, $content=null)
67 if (is_bool($content)) {
68 $content = ($content ? 'true' : 'false');
71 return parent::element($tag, $attrs, $content);
74 function twitter_user_array($profile, $get_notice=false)
77 $twitter_user = array();
79 $twitter_user['name'] = $profile->getBestName();
80 $twitter_user['followers_count'] = $this->count_subscriptions($profile);
81 $twitter_user['screen_name'] = $profile->nickname;
82 $twitter_user['description'] = ($profile->bio) ? $profile->bio : null;
83 $twitter_user['location'] = ($profile->location) ? $profile->location : null;
84 $twitter_user['id'] = intval($profile->id);
86 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
88 $twitter_user['profile_image_url'] = ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_STREAM_SIZE);
89 $twitter_user['protected'] = false; # not supported by Laconica yet
90 $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
93 $notice = $profile->getCurrentNotice();
96 $twitter_user['status'] = $this->twitter_status_array($notice, false);
100 return $twitter_user;
103 function twitter_status_array($notice, $include_user=true)
105 $profile = $notice->getProfile();
107 $twitter_status = array();
108 $twitter_status['text'] = $notice->content;
109 $twitter_status['truncated'] = false; # Not possible on Laconica
110 $twitter_status['created_at'] = $this->date_twitter($notice->created);
111 $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ?
112 intval($notice->reply_to) : null;
113 $twitter_status['source'] = $this->source_link($notice->source);
114 $twitter_status['id'] = intval($notice->id);
116 $replier_profile = null;
118 if ($notice->reply_to) {
119 $reply = Notice::staticGet(intval($notice->reply_to));
121 $replier_profile = $reply->getProfile();
125 $twitter_status['in_reply_to_user_id'] =
126 ($replier_profile) ? intval($replier_profile->id) : null;
127 $twitter_status['in_reply_to_screen_name'] =
128 ($replier_profile) ? $replier_profile->nickname : null;
130 if (isset($this->auth_user)) {
131 $twitter_status['favorited'] = $this->auth_user->hasFave($notice);
133 $twitter_status['favorited'] = false;
137 # Don't get notice (recursive!)
138 $twitter_user = $this->twitter_user_array($profile, false);
139 $twitter_status['user'] = $twitter_user;
142 return $twitter_status;
145 function twitter_rss_entry_array($notice)
148 $profile = $notice->getProfile();
151 # We trim() to avoid extraneous whitespace in the output
153 $entry['content'] = common_xml_safe_str(trim($notice->rendered));
154 $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
155 $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
156 $entry['published'] = common_date_iso8601($notice->created);
158 $taguribase = common_config('integration', 'taguri');
159 $entry['id'] = "tag:$taguribase:$entry[link]";
161 $entry['updated'] = $entry['published'];
162 $entry['author'] = $profile->getBestName();
165 $entry['description'] = $entry['content'];
166 $entry['pubDate'] = common_date_rfc2822($notice->created);
167 $entry['guid'] = $entry['link'];
172 function twitter_rss_dmsg_array($message)
177 $entry['title'] = sprintf('Message from %s to %s',
178 $message->getFrom()->nickname, $message->getTo()->nickname);
180 $entry['content'] = common_xml_safe_str(trim($message->content));
181 $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
182 $entry['published'] = common_date_iso8601($message->created);
184 $taguribase = common_config('integration', 'taguri');
186 $entry['id'] = "tag:$taguribase,:$entry[link]";
187 $entry['updated'] = $entry['published'];
188 $entry['author'] = $message->getFrom()->getBestName();
191 $entry['description'] = $entry['content'];
192 $entry['pubDate'] = common_date_rfc2822($message->created);
193 $entry['guid'] = $entry['link'];
198 function twitter_dmsg_array($message)
200 $twitter_dm = array();
202 $from_profile = $message->getFrom();
203 $to_profile = $message->getTo();
205 $twitter_dm['id'] = $message->id;
206 $twitter_dm['sender_id'] = $message->from_profile;
207 $twitter_dm['text'] = trim($message->content);
208 $twitter_dm['recipient_id'] = $message->to_profile;
209 $twitter_dm['created_at'] = $this->date_twitter($message->created);
210 $twitter_dm['sender_screen_name'] = $from_profile->nickname;
211 $twitter_dm['recipient_screen_name'] = $to_profile->nickname;
212 $twitter_dm['sender'] = $this->twitter_user_array($from_profile, false);
213 $twitter_dm['recipient'] = $this->twitter_user_array($to_profile, false);
218 function show_twitter_xml_status($twitter_status)
220 $this->elementStart('status');
221 foreach($twitter_status as $element => $value) {
224 $this->show_twitter_xml_user($twitter_status['user']);
227 $this->element($element, null, common_xml_safe_str($value));
230 $this->element($element, null, $value);
233 $this->elementEnd('status');
236 function show_twitter_xml_user($twitter_user, $role='user')
238 $this->elementStart($role);
239 foreach($twitter_user as $element => $value) {
240 if ($element == 'status') {
241 $this->show_twitter_xml_status($twitter_user['status']);
243 $this->element($element, null, $value);
246 $this->elementEnd($role);
249 function show_twitter_rss_item($entry)
251 $this->elementStart('item');
252 $this->element('title', null, $entry['title']);
253 $this->element('description', null, $entry['description']);
254 $this->element('pubDate', null, $entry['pubDate']);
255 $this->element('guid', null, $entry['guid']);
256 $this->element('link', null, $entry['link']);
257 $this->elementEnd('item');
260 function show_json_objects($objects)
262 print(json_encode($objects));
265 function show_single_xml_status($notice)
267 $this->init_document('xml');
268 $twitter_status = $this->twitter_status_array($notice);
269 $this->show_twitter_xml_status($twitter_status);
270 $this->end_document('xml');
273 function show_single_json_status($notice)
275 $this->init_document('json');
276 $status = $this->twitter_status_array($notice);
277 $this->show_json_objects($status);
278 $this->end_document('json');
281 function show_single_xml_dmsg($message)
283 $this->init_document('xml');
284 $dmsg = $this->twitter_dmsg_array($message);
285 $this->show_twitter_xml_dmsg($dmsg);
286 $this->end_document('xml');
289 function show_single_json_dmsg($message)
291 $this->init_document('json');
292 $dmsg = $this->twitter_dmsg_array($message);
293 $this->show_json_objects($dmsg);
294 $this->end_document('json');
297 function show_twitter_xml_dmsg($twitter_dm)
299 $this->elementStart('direct_message');
300 foreach($twitter_dm as $element => $value) {
304 $this->show_twitter_xml_user($value, $element);
307 $this->element($element, null, common_xml_safe_str($value));
310 $this->element($element, null, $value);
313 $this->elementEnd('direct_message');
316 function show_xml_timeline($notice)
319 $this->init_document('xml');
320 $this->elementStart('statuses', array('type' => 'array'));
322 if (is_array($notice)) {
323 foreach ($notice as $n) {
324 $twitter_status = $this->twitter_status_array($n);
325 $this->show_twitter_xml_status($twitter_status);
328 while ($notice->fetch()) {
329 $twitter_status = $this->twitter_status_array($notice);
330 $this->show_twitter_xml_status($twitter_status);
334 $this->elementEnd('statuses');
335 $this->end_document('xml');
338 function show_rss_timeline($notice, $title, $link, $subtitle, $suplink=null)
341 $this->init_document('rss');
343 $this->elementStart('channel');
344 $this->element('title', null, $title);
345 $this->element('link', null, $link);
346 if (!is_null($suplink)) {
347 # For FriendFeed's SUP protocol
348 $this->element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
349 'rel' => 'http://api.friendfeed.com/2008/03#sup',
351 'type' => 'application/json'));
353 $this->element('description', null, $subtitle);
354 $this->element('language', null, 'en-us');
355 $this->element('ttl', null, '40');
357 if (is_array($notice)) {
358 foreach ($notice as $n) {
359 $entry = $this->twitter_rss_entry_array($n);
360 $this->show_twitter_rss_item($entry);
363 while ($notice->fetch()) {
364 $entry = $this->twitter_rss_entry_array($notice);
365 $this->show_twitter_rss_item($entry);
369 $this->elementEnd('channel');
370 $this->end_twitter_rss();
373 function show_atom_timeline($notice, $title, $id, $link, $subtitle=null, $suplink=null, $selfuri=null)
376 $this->init_document('atom');
378 $this->element('title', null, $title);
379 $this->element('id', null, $id);
380 $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
382 if (!is_null($suplink)) {
383 # For FriendFeed's SUP protocol
384 $this->element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
386 'type' => 'application/json'));
389 if (!is_null($selfuri)) {
390 $this->element('link', array('href' => $selfuri,
391 'rel' => 'self', 'type' => 'application/atom+xml'), null);
394 $this->element('updated', null, common_date_iso8601('now'));
395 $this->element('subtitle', null, $subtitle);
397 if (is_array($notice)) {
398 foreach ($notice as $n) {
399 $this->raw($n->asAtomEntry());
402 while ($notice->fetch()) {
403 $this->raw($notice->asAtomEntry());
407 $this->end_document('atom');
411 function show_json_timeline($notice)
414 $this->init_document('json');
418 if (is_array($notice)) {
419 foreach ($notice as $n) {
420 $twitter_status = $this->twitter_status_array($n);
421 array_push($statuses, $twitter_status);
424 while ($notice->fetch()) {
425 $twitter_status = $this->twitter_status_array($notice);
426 array_push($statuses, $twitter_status);
430 $this->show_json_objects($statuses);
432 $this->end_document('json');
435 // Anyone know what date format this is?
436 // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
437 function date_twitter($dt)
440 return date("D M d H:i:s O Y", $t);
443 // XXX: Candidate for a general utility method somewhere?
444 function count_subscriptions($profile)
448 $sub = new Subscription();
449 $sub->subscribed = $profile->id;
451 $count = $sub->find();
460 function init_document($type='xml')
464 header('Content-Type: application/xml; charset=utf-8');
468 header('Content-Type: application/json; charset=utf-8');
470 // Check for JSONP callback
471 $callback = $this->arg('callback');
473 print $callback . '(';
477 header("Content-Type: application/rss+xml; charset=utf-8");
478 $this->init_twitter_rss();
481 header('Content-Type: application/atom+xml; charset=utf-8');
482 $this->init_twitter_atom();
485 $this->client_error(_('Not a supported data format.'));
492 function end_document($type='xml')
500 // Check for JSONP callback
501 $callback = $this->arg('callback');
507 $this->end_twitter_rss();
510 $this->end_twitter_rss();
513 $this->client_error(_('Not a supported data format.'));
519 function client_error($msg, $code = 400, $content_type = 'json')
522 static $status = array(400 => 'Bad Request',
523 401 => 'Unauthorized',
524 402 => 'Payment Required',
527 405 => 'Method Not Allowed',
528 406 => 'Not Acceptable',
529 407 => 'Proxy Authentication Required',
530 408 => 'Request Timeout',
533 411 => 'Length Required',
534 412 => 'Precondition Failed',
535 413 => 'Request Entity Too Large',
536 414 => 'Request-URI Too Long',
537 415 => 'Unsupported Media Type',
538 416 => 'Requested Range Not Satisfiable',
539 417 => 'Expectation Failed');
541 $action = $this->trimmed('action');
543 common_debug("User error '$code' on '$action': $msg", __FILE__);
545 if (!array_key_exists($code, $status)) {
549 $status_string = $status[$code];
550 header('HTTP/1.1 '.$code.' '.$status_string);
552 if ($content_type == 'xml') {
553 $this->init_document('xml');
554 $this->elementStart('hash');
555 $this->element('error', null, $msg);
556 $this->element('request', null, $_SERVER['REQUEST_URI']);
557 $this->elementEnd('hash');
558 $this->end_document('xml');
560 $this->init_document('json');
561 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
562 print(json_encode($error_array));
563 $this->end_document('json');
568 function init_twitter_rss()
571 $this->elementStart('rss', array('version' => '2.0'));
574 function end_twitter_rss()
576 $this->elementEnd('rss');
580 function init_twitter_atom()
583 // FIXME: don't hardcode the language here!
584 $this->elementStart('feed', array('xmlns' => 'http://www.w3.org/2005/Atom',
585 'xml:lang' => 'en-US',
586 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0'));
589 function end_twitter_atom()
591 $this->elementEnd('feed');
595 function show_profile($profile, $content_type='xml', $notice=null)
597 $profile_array = $this->twitter_user_array($profile, true);
598 switch ($content_type) {
600 $this->show_twitter_xml_user($profile_array);
603 $this->show_json_objects($profile_array);
606 $this->client_error(_('Not a supported data format.'));
612 function get_user($id, $apidata=null)
615 return $apidata['user'];
616 } else if (is_numeric($id)) {
617 return User::staticGet($id);
619 $nickname = common_canonical_nickname($id);
620 return User::staticGet('nickname', $nickname);
624 function get_profile($id)
626 if (is_numeric($id)) {
627 return Profile::staticGet($id);
629 $user = User::staticGet('nickname', $id);
631 return $user->getProfile();
638 function source_link($source)
640 $source_name = _($source);
649 $ns = Notice_source::staticGet($source);
651 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';