3 * StatusNet, the distributed open-source microblogging tool
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Craig Andrews <candrews@integralblue.com>
25 * @author Dan Moore <dan@moore.cx>
26 * @author Evan Prodromou <evan@status.net>
27 * @author Jeffery To <jeffery.to@gmail.com>
28 * @author Toby Inkster <mail@tobyinkster.co.uk>
29 * @author Zach Copley <zach@status.net>
30 * @copyright 2009-2010 StatusNet, Inc.
31 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
32 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
33 * @link http://status.net/
36 /* External API usage documentation. Please update when you change how the API works. */
38 /*! @mainpage StatusNet REST API
42 Some explanatory text about the API would be nice.
46 @subsection timelinesmethods_sec Timeline Methods
48 @li @ref publictimeline
49 @li @ref friendstimeline
51 @subsection statusmethods_sec Status Methods
53 @li @ref statusesupdate
55 @subsection usermethods_sec User Methods
57 @subsection directmessagemethods_sec Direct Message Methods (now a plugin)
59 @subsection friendshipmethods_sec Friendship Methods
61 @subsection socialgraphmethods_sec Social Graph Methods
63 @subsection accountmethods_sec Account Methods
65 @subsection favoritesmethods_sec Favorites Methods
67 @subsection blockmethods_sec Block Methods
69 @subsection oauthmethods_sec OAuth Methods
71 @subsection helpmethods_sec Help Methods
73 @subsection groupmethods_sec Group Methods
75 @page apiroot API Root
77 The URLs for methods referred to in this API documentation are
78 relative to the StatusNet API root. The API root is determined by the
79 site's @b server and @b path variables, which are generally specified
80 in config.php. For example:
83 $config['site']['server'] = 'example.org';
84 $config['site']['path'] = 'statusnet'
87 The pattern for a site's API root is: @c protocol://server/path/api E.g:
89 @c http://example.org/statusnet/api
91 The @b path can be empty. In that case the API root would simply be:
93 @c http://example.org/api
97 if (!defined('STATUSNET')) {
101 class ApiValidationException extends Exception { }
104 * Contains most of the Twitter-compatible API output functions.
108 * @author Craig Andrews <candrews@integralblue.com>
109 * @author Dan Moore <dan@moore.cx>
110 * @author Evan Prodromou <evan@status.net>
111 * @author Jeffery To <jeffery.to@gmail.com>
112 * @author Toby Inkster <mail@tobyinkster.co.uk>
113 * @author Zach Copley <zach@status.net>
114 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
115 * @link http://status.net/
117 class ApiAction extends Action
120 const READ_WRITE = 2;
123 var $auth_user = null;
129 var $since_id = null;
131 var $callback = null;
134 var $access = self::READ_ONLY; // read (default) or read-write
136 static $reserved_sources = array('web', 'omb', 'ostatus', 'mail', 'xmpp', 'api');
141 * @param array $args Web and URL arguments
143 * @return boolean false if user doesn't exist
145 protected function prepare(array $args=array())
147 GNUsocial::setApi(true); // reduce exception reports to aid in debugging
148 parent::prepare($args);
150 $this->format = $this->arg('format');
151 $this->callback = $this->arg('callback');
152 $this->page = (int)$this->arg('page', 1);
153 $this->count = (int)$this->arg('count', 20);
154 $this->max_id = (int)$this->arg('max_id', 0);
155 $this->since_id = (int)$this->arg('since_id', 0);
157 // These two are not used everywhere, mainly just AtompubAction extensions
158 $this->offset = ($this->page-1) * $this->count;
159 $this->limit = $this->count + 1;
161 if ($this->arg('since')) {
162 header('X-GNUsocial-Warning: since parameter is disabled; use since_id');
165 $this->source = $this->trimmed('source');
167 if (empty($this->source) || in_array($this->source, self::$reserved_sources)) {
168 $this->source = 'api';
177 * @param array $args Arguments from $_REQUEST
181 protected function handle()
183 header('Access-Control-Allow-Origin: *');
188 * Overrides XMLOutputter::element to write booleans as strings (true|false).
189 * See that method's documentation for more info.
191 * @param string $tag Element type or tagname
192 * @param array $attrs Array of element attributes, as
194 * @param string $content string content of the element
198 function element($tag, $attrs=null, $content=null)
200 if (is_bool($content)) {
201 $content = ($content ? 'true' : 'false');
204 return parent::element($tag, $attrs, $content);
207 function twitterUserArray($profile, $get_notice=false)
209 $twitter_user = array();
212 $user = $profile->getUser();
213 } catch (NoSuchUserException $e) {
217 $twitter_user['id'] = intval($profile->id);
218 $twitter_user['name'] = $profile->getBestName();
219 $twitter_user['screen_name'] = $profile->nickname;
220 $twitter_user['location'] = ($profile->location) ? $profile->location : null;
221 $twitter_user['description'] = ($profile->bio) ? $profile->bio : null;
223 // TODO: avatar url template (example.com/user/avatar?size={x}x{y})
224 $twitter_user['profile_image_url'] = Avatar::urlByProfile($profile, AVATAR_STREAM_SIZE);
225 $twitter_user['profile_image_url_https'] = $twitter_user['profile_image_url'];
227 // START introduced by qvitter API, not necessary for StatusNet API
228 $twitter_user['profile_image_url_profile_size'] = Avatar::urlByProfile($profile, AVATAR_PROFILE_SIZE);
230 $avatar = Avatar::getUploaded($profile);
231 $origurl = $avatar->displayUrl();
232 } catch (Exception $e) {
233 $origurl = $twitter_user['profile_image_url_profile_size'];
235 $twitter_user['profile_image_url_original'] = $origurl;
237 $twitter_user['groups_count'] = $profile->getGroupCount();
238 foreach (array('linkcolor', 'backgroundcolor') as $key) {
239 $twitter_user[$key] = Profile_prefs::getConfigData($profile, 'theme', $key);
241 // END introduced by qvitter API, not necessary for StatusNet API
243 $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
244 $twitter_user['protected'] = (!empty($user) && $user->private_stream) ? true : false;
245 $twitter_user['followers_count'] = $profile->subscriberCount();
247 // Note: some profiles don't have an associated user
249 $twitter_user['friends_count'] = $profile->subscriptionCount();
251 $twitter_user['created_at'] = self::dateTwitter($profile->created);
255 if (!empty($user) && $user->timezone) {
256 $timezone = $user->timezone;
260 $t->setTimezone(new DateTimeZone($timezone));
262 $twitter_user['utc_offset'] = $t->format('Z');
263 $twitter_user['time_zone'] = $timezone;
264 $twitter_user['statuses_count'] = $profile->noticeCount();
266 // Is the requesting user following this user?
267 // These values might actually also mean "unknown". Ambiguity issues?
268 $twitter_user['following'] = false;
269 $twitter_user['statusnet_blocking'] = false;
270 $twitter_user['notifications'] = false;
272 if ($this->scoped instanceof Profile) {
274 $sub = Subscription::getSubscription($this->scoped, $profile);
276 $twitter_user['following'] = true;
277 $twitter_user['statusnet_blocking'] = $this->scoped->hasBlocked($profile);
278 $twitter_user['notifications'] = ($sub->jabber || $sub->sms);
279 } catch (NoResultException $e) {
280 // well, the values are already false...
285 $notice = $profile->getCurrentNotice();
286 if ($notice instanceof Notice) {
288 $twitter_user['status'] = $this->twitterStatusArray($notice, false);
292 // StatusNet-specific
294 $twitter_user['statusnet_profile_url'] = $profile->profileurl;
296 // The event call to handle NoticeSimpleStatusArray lets plugins add data to the output array
297 Event::handle('TwitterUserArray', array($profile, &$twitter_user, $this->scoped, array()));
299 return $twitter_user;
302 function twitterStatusArray($notice, $include_user=true)
304 $base = $this->twitterSimpleStatusArray($notice, $include_user);
306 // FIXME: MOVE TO SHARE PLUGIN
307 if (!empty($notice->repeat_of)) {
308 $original = Notice::getKV('id', $notice->repeat_of);
309 if ($original instanceof Notice) {
310 $orig_array = $this->twitterSimpleStatusArray($original, $include_user);
311 $base['retweeted_status'] = $orig_array;
318 function twitterSimpleStatusArray($notice, $include_user=true)
320 $profile = $notice->getProfile();
322 $twitter_status = array();
323 $twitter_status['text'] = $notice->content;
324 $twitter_status['truncated'] = false; # Not possible on StatusNet
325 $twitter_status['created_at'] = self::dateTwitter($notice->created);
327 // We could just do $notice->reply_to but maybe the future holds a
328 // different story for parenting.
329 $parent = $notice->getParent();
330 $in_reply_to = $parent->id;
331 } catch (NoParentNoticeException $e) {
334 $twitter_status['in_reply_to_status_id'] = $in_reply_to;
338 $ns = $notice->getSource();
339 if ($ns instanceof Notice_source) {
340 if (!empty($ns->name) && !empty($ns->url)) {
341 $source = '<a href="'
342 . htmlspecialchars($ns->url)
343 . '" rel="nofollow">'
344 . htmlspecialchars($ns->name)
351 $twitter_status['uri'] = $notice->getUri();
352 $twitter_status['source'] = $source;
353 $twitter_status['id'] = intval($notice->id);
355 $replier_profile = null;
357 if ($notice->reply_to) {
358 $reply = Notice::getKV(intval($notice->reply_to));
360 $replier_profile = $reply->getProfile();
364 $twitter_status['in_reply_to_user_id'] =
365 ($replier_profile) ? intval($replier_profile->id) : null;
366 $twitter_status['in_reply_to_screen_name'] =
367 ($replier_profile) ? $replier_profile->nickname : null;
370 $notloc = Notice_location::locFromStored($notice);
371 // This is the format that GeoJSON expects stuff to be in
372 $twitter_status['geo'] = array('type' => 'Point',
373 'coordinates' => array((float) $notloc->lat,
374 (float) $notloc->lon));
375 } catch (ServerException $e) {
376 $twitter_status['geo'] = null;
380 $attachments = $notice->attachments();
382 if (!empty($attachments)) {
384 $twitter_status['attachments'] = array();
386 foreach ($attachments as $attachment) {
388 $enclosure_o = $attachment->getEnclosure();
389 $enclosure = array();
390 $enclosure['url'] = $enclosure_o->url;
391 $enclosure['mimetype'] = $enclosure_o->mimetype;
392 $enclosure['size'] = $enclosure_o->size;
393 $twitter_status['attachments'][] = $enclosure;
394 } catch (ServerException $e) {
395 // There was not enough metadata available
400 if ($include_user && $profile) {
401 // Don't get notice (recursive!)
402 $twitter_user = $this->twitterUserArray($profile, false);
403 $twitter_status['user'] = $twitter_user;
406 // StatusNet-specific
408 $twitter_status['statusnet_html'] = $notice->rendered;
409 $twitter_status['statusnet_conversation_id'] = intval($notice->conversation);
411 // The event call to handle NoticeSimpleStatusArray lets plugins add data to the output array
412 Event::handle('NoticeSimpleStatusArray', array($notice, &$twitter_status, $this->scoped,
413 array('include_user'=>$include_user)));
415 return $twitter_status;
418 function twitterGroupArray($group)
420 $twitter_group = array();
422 $twitter_group['id'] = intval($group->id);
423 $twitter_group['url'] = $group->permalink();
424 $twitter_group['nickname'] = $group->nickname;
425 $twitter_group['fullname'] = $group->fullname;
427 if ($this->scoped instanceof Profile) {
428 $twitter_group['member'] = $this->scoped->isMember($group);
429 $twitter_group['blocked'] = Group_block::isBlocked(
435 $twitter_group['admin_count'] = $group->getAdminCount();
436 $twitter_group['member_count'] = $group->getMemberCount();
437 $twitter_group['original_logo'] = $group->original_logo;
438 $twitter_group['homepage_logo'] = $group->homepage_logo;
439 $twitter_group['stream_logo'] = $group->stream_logo;
440 $twitter_group['mini_logo'] = $group->mini_logo;
441 $twitter_group['homepage'] = $group->homepage;
442 $twitter_group['description'] = $group->description;
443 $twitter_group['location'] = $group->location;
444 $twitter_group['created'] = self::dateTwitter($group->created);
445 $twitter_group['modified'] = self::dateTwitter($group->modified);
447 return $twitter_group;
450 function twitterRssGroupArray($group)
453 $entry['content']=$group->description;
454 $entry['title']=$group->nickname;
455 $entry['link']=$group->permalink();
456 $entry['published']=common_date_iso8601($group->created);
457 $entry['updated']==common_date_iso8601($group->modified);
458 $taguribase = common_config('integration', 'groupuri');
459 $entry['id'] = "group:$groupuribase:$entry[link]";
461 $entry['description'] = $entry['content'];
462 $entry['pubDate'] = common_date_rfc2822($group->created);
463 $entry['guid'] = $entry['link'];
468 function twitterListArray($list)
470 $profile = Profile::getKV('id', $list->tagger);
472 $twitter_list = array();
473 $twitter_list['id'] = $list->id;
474 $twitter_list['name'] = $list->tag;
475 $twitter_list['full_name'] = '@'.$profile->nickname.'/'.$list->tag;;
476 $twitter_list['slug'] = $list->tag;
477 $twitter_list['description'] = $list->description;
478 $twitter_list['subscriber_count'] = $list->subscriberCount();
479 $twitter_list['member_count'] = $list->taggedCount();
480 $twitter_list['uri'] = $list->getUri();
482 if ($this->scoped instanceof Profile) {
483 $twitter_list['following'] = $list->hasSubscriber($this->scoped);
485 $twitter_list['following'] = false;
488 $twitter_list['mode'] = ($list->private) ? 'private' : 'public';
489 $twitter_list['user'] = $this->twitterUserArray($profile, false);
491 return $twitter_list;
494 function twitterRssEntryArray($notice)
498 if (Event::handle('StartRssEntryArray', array($notice, &$entry))) {
499 $profile = $notice->getProfile();
501 // We trim() to avoid extraneous whitespace in the output
503 $entry['content'] = common_xml_safe_str(trim($notice->rendered));
504 $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
505 $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
506 $entry['published'] = common_date_iso8601($notice->created);
508 $taguribase = TagURI::base();
509 $entry['id'] = "tag:$taguribase:$entry[link]";
511 $entry['updated'] = $entry['published'];
512 $entry['author'] = $profile->getBestName();
515 $attachments = $notice->attachments();
516 $enclosures = array();
518 foreach ($attachments as $attachment) {
520 $enclosure_o = $attachment->getEnclosure();
521 $enclosure = array();
522 $enclosure['url'] = $enclosure_o->url;
523 $enclosure['mimetype'] = $enclosure_o->mimetype;
524 $enclosure['size'] = $enclosure_o->size;
525 $enclosures[] = $enclosure;
526 } catch (ServerException $e) {
527 // There was not enough metadata available
531 if (!empty($enclosures)) {
532 $entry['enclosures'] = $enclosures;
536 $tag = new Notice_tag();
537 $tag->notice_id = $notice->id;
539 $entry['tags']=array();
540 while ($tag->fetch()) {
541 $entry['tags'][]=$tag->tag;
547 $entry['description'] = $entry['content'];
548 $entry['pubDate'] = common_date_rfc2822($notice->created);
549 $entry['guid'] = $entry['link'];
552 $notloc = Notice_location::locFromStored($notice);
553 // This is the format that GeoJSON expects stuff to be in.
554 // showGeoRSS() below uses it for XML output, so we reuse it
555 $entry['geo'] = array('type' => 'Point',
556 'coordinates' => array((float) $notloc->lat,
557 (float) $notloc->lon));
558 } catch (ServerException $e) {
559 $entry['geo'] = null;
562 Event::handle('EndRssEntryArray', array($notice, &$entry));
568 function twitterRelationshipArray($source, $target)
570 $relationship = array();
572 $relationship['source'] =
573 $this->relationshipDetailsArray($source->getProfile(), $target->getProfile());
574 $relationship['target'] =
575 $this->relationshipDetailsArray($target->getProfile(), $source->getProfile());
577 return array('relationship' => $relationship);
580 function relationshipDetailsArray(Profile $source, Profile $target)
584 $details['screen_name'] = $source->getNickname();
585 $details['followed_by'] = $target->isSubscribed($source);
588 $sub = Subscription::getSubscription($source, $target);
589 $details['following'] = true;
590 $details['notifications_enabled'] = ($sub->jabber || $sub->sms);
591 } catch (NoResultException $e) {
592 $details['following'] = false;
593 $details['notifications_enabled'] = false;
596 $details['blocking'] = $source->hasBlocked($target);
597 $details['id'] = intval($source->id);
602 function showTwitterXmlRelationship($relationship)
604 $this->elementStart('relationship');
606 foreach($relationship as $element => $value) {
607 if ($element == 'source' || $element == 'target') {
608 $this->elementStart($element);
609 $this->showXmlRelationshipDetails($value);
610 $this->elementEnd($element);
614 $this->elementEnd('relationship');
617 function showXmlRelationshipDetails($details)
619 foreach($details as $element => $value) {
620 $this->element($element, null, $value);
624 function showTwitterXmlStatus($twitter_status, $tag='status', $namespaces=false)
628 $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
630 $this->elementStart($tag, $attrs);
631 foreach($twitter_status as $element => $value) {
634 $this->showTwitterXmlUser($twitter_status['user']);
637 $this->element($element, null, common_xml_safe_str($value));
640 $this->showXmlAttachments($twitter_status['attachments']);
643 $this->showGeoXML($value);
645 case 'retweeted_status':
646 // FIXME: MOVE TO SHARE PLUGIN
647 $this->showTwitterXmlStatus($value, 'retweeted_status');
650 if (strncmp($element, 'statusnet_', 10) == 0) {
651 $this->element('statusnet:'.substr($element, 10), null, $value);
653 $this->element($element, null, $value);
657 $this->elementEnd($tag);
660 function showTwitterXmlGroup($twitter_group)
662 $this->elementStart('group');
663 foreach($twitter_group as $element => $value) {
664 $this->element($element, null, $value);
666 $this->elementEnd('group');
669 function showTwitterXmlList($twitter_list)
671 $this->elementStart('list');
672 foreach($twitter_list as $element => $value) {
673 if($element == 'user') {
674 $this->showTwitterXmlUser($value, 'user');
677 $this->element($element, null, $value);
680 $this->elementEnd('list');
683 function showTwitterXmlUser($twitter_user, $role='user', $namespaces=false)
687 $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
689 $this->elementStart($role, $attrs);
690 foreach($twitter_user as $element => $value) {
691 if ($element == 'status') {
692 $this->showTwitterXmlStatus($twitter_user['status']);
693 } else if (strncmp($element, 'statusnet_', 10) == 0) {
694 $this->element('statusnet:'.substr($element, 10), null, $value);
696 $this->element($element, null, $value);
699 $this->elementEnd($role);
702 function showXmlAttachments($attachments) {
703 if (!empty($attachments)) {
704 $this->elementStart('attachments', array('type' => 'array'));
705 foreach ($attachments as $attachment) {
707 $attrs['url'] = $attachment['url'];
708 $attrs['mimetype'] = $attachment['mimetype'];
709 $attrs['size'] = $attachment['size'];
710 $this->element('enclosure', $attrs, '');
712 $this->elementEnd('attachments');
716 function showGeoXML($geo)
720 $this->element('geo');
722 $this->elementStart('geo', array('xmlns:georss' => 'http://www.georss.org/georss'));
723 $this->element('georss:point', null, $geo['coordinates'][0] . ' ' . $geo['coordinates'][1]);
724 $this->elementEnd('geo');
728 function showGeoRSS($geo)
734 $geo['coordinates'][0] . ' ' . $geo['coordinates'][1]
739 function showTwitterRssItem($entry)
741 $this->elementStart('item');
742 $this->element('title', null, $entry['title']);
743 $this->element('description', null, $entry['description']);
744 $this->element('pubDate', null, $entry['pubDate']);
745 $this->element('guid', null, $entry['guid']);
746 $this->element('link', null, $entry['link']);
748 // RSS only supports 1 enclosure per item
749 if(array_key_exists('enclosures', $entry) and !empty($entry['enclosures'])){
750 $enclosure = $entry['enclosures'][0];
751 $this->element('enclosure', array('url'=>$enclosure['url'],'type'=>$enclosure['mimetype'],'length'=>$enclosure['size']), null);
754 if(array_key_exists('tags', $entry)){
755 foreach($entry['tags'] as $tag){
756 $this->element('category', null,$tag);
760 $this->showGeoRSS($entry['geo']);
761 $this->elementEnd('item');
764 function showJsonObjects($objects)
766 print(json_encode($objects));
769 function showSingleXmlStatus($notice)
771 $this->initDocument('xml');
772 $twitter_status = $this->twitterStatusArray($notice);
773 $this->showTwitterXmlStatus($twitter_status, 'status', true);
774 $this->endDocument('xml');
777 function showSingleAtomStatus($notice)
779 header('Content-Type: application/atom+xml; charset=utf-8');
780 print $notice->asAtomEntry(true, true, true, $this->scoped);
783 function show_single_json_status($notice)
785 $this->initDocument('json');
786 $status = $this->twitterStatusArray($notice);
787 $this->showJsonObjects($status);
788 $this->endDocument('json');
791 function showXmlTimeline($notice)
793 $this->initDocument('xml');
794 $this->elementStart('statuses', array('type' => 'array',
795 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
797 if (is_array($notice)) {
798 //FIXME: make everything calling showJsonTimeline use only Notice objects
800 foreach ($notice as $n) {
801 $ids[] = $n->getID();
803 $notice = Notice::multiGet('id', $ids);
806 while ($notice->fetch()) {
808 $twitter_status = $this->twitterStatusArray($notice);
809 $this->showTwitterXmlStatus($twitter_status);
810 } catch (Exception $e) {
811 common_log(LOG_ERR, $e->getMessage());
816 $this->elementEnd('statuses');
817 $this->endDocument('xml');
820 function showRssTimeline($notice, $title, $link, $subtitle, $suplink = null, $logo = null, $self = null)
822 $this->initDocument('rss');
824 $this->element('title', null, $title);
825 $this->element('link', null, $link);
827 if (!is_null($self)) {
831 'type' => 'application/rss+xml',
838 if (!is_null($suplink)) {
839 // For FriendFeed's SUP protocol
840 $this->element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
841 'rel' => 'http://api.friendfeed.com/2008/03#sup',
843 'type' => 'application/json'));
846 if (!is_null($logo)) {
847 $this->elementStart('image');
848 $this->element('link', null, $link);
849 $this->element('title', null, $title);
850 $this->element('url', null, $logo);
851 $this->elementEnd('image');
854 $this->element('description', null, $subtitle);
855 $this->element('language', null, 'en-us');
856 $this->element('ttl', null, '40');
858 if (is_array($notice)) {
859 //FIXME: make everything calling showJsonTimeline use only Notice objects
861 foreach ($notice as $n) {
862 $ids[] = $n->getID();
864 $notice = Notice::multiGet('id', $ids);
867 while ($notice->fetch()) {
869 $entry = $this->twitterRssEntryArray($notice);
870 $this->showTwitterRssItem($entry);
871 } catch (Exception $e) {
872 common_log(LOG_ERR, $e->getMessage());
873 // continue on exceptions
877 $this->endTwitterRss();
880 function showAtomTimeline($notice, $title, $id, $link, $subtitle=null, $suplink=null, $selfuri=null, $logo=null)
882 $this->initDocument('atom');
884 $this->element('title', null, $title);
885 $this->element('id', null, $id);
886 $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
888 if (!is_null($logo)) {
889 $this->element('logo',null,$logo);
892 if (!is_null($suplink)) {
893 // For FriendFeed's SUP protocol
894 $this->element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
896 'type' => 'application/json'));
899 if (!is_null($selfuri)) {
900 $this->element('link', array('href' => $selfuri,
901 'rel' => 'self', 'type' => 'application/atom+xml'), null);
904 $this->element('updated', null, common_date_iso8601('now'));
905 $this->element('subtitle', null, $subtitle);
907 if (is_array($notice)) {
908 //FIXME: make everything calling showJsonTimeline use only Notice objects
910 foreach ($notice as $n) {
911 $ids[] = $n->getID();
913 $notice = Notice::multiGet('id', $ids);
916 while ($notice->fetch()) {
918 $this->raw($notice->asAtomEntry());
919 } catch (Exception $e) {
920 common_log(LOG_ERR, $e->getMessage());
925 $this->endDocument('atom');
928 function showRssGroups($group, $title, $link, $subtitle)
930 $this->initDocument('rss');
932 $this->element('title', null, $title);
933 $this->element('link', null, $link);
934 $this->element('description', null, $subtitle);
935 $this->element('language', null, 'en-us');
936 $this->element('ttl', null, '40');
938 if (is_array($group)) {
939 foreach ($group as $g) {
940 $twitter_group = $this->twitterRssGroupArray($g);
941 $this->showTwitterRssItem($twitter_group);
944 while ($group->fetch()) {
945 $twitter_group = $this->twitterRssGroupArray($group);
946 $this->showTwitterRssItem($twitter_group);
950 $this->endTwitterRss();
953 function showTwitterAtomEntry($entry)
955 $this->elementStart('entry');
956 $this->element('title', null, common_xml_safe_str($entry['title']));
959 array('type' => 'html'),
960 common_xml_safe_str($entry['content'])
962 $this->element('id', null, $entry['id']);
963 $this->element('published', null, $entry['published']);
964 $this->element('updated', null, $entry['updated']);
965 $this->element('link', array('type' => 'text/html',
966 'href' => $entry['link'],
967 'rel' => 'alternate'));
968 $this->element('link', array('type' => $entry['avatar-type'],
969 'href' => $entry['avatar'],
971 $this->elementStart('author');
973 $this->element('name', null, $entry['author-name']);
974 $this->element('uri', null, $entry['author-uri']);
976 $this->elementEnd('author');
977 $this->elementEnd('entry');
980 function showAtomGroups($group, $title, $id, $link, $subtitle=null, $selfuri=null)
982 $this->initDocument('atom');
984 $this->element('title', null, common_xml_safe_str($title));
985 $this->element('id', null, $id);
986 $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
988 if (!is_null($selfuri)) {
989 $this->element('link', array('href' => $selfuri,
990 'rel' => 'self', 'type' => 'application/atom+xml'), null);
993 $this->element('updated', null, common_date_iso8601('now'));
994 $this->element('subtitle', null, common_xml_safe_str($subtitle));
996 if (is_array($group)) {
997 foreach ($group as $g) {
998 $this->raw($g->asAtomEntry());
1001 while ($group->fetch()) {
1002 $this->raw($group->asAtomEntry());
1006 $this->endDocument('atom');
1010 function showJsonTimeline($notice)
1012 $this->initDocument('json');
1014 $statuses = array();
1016 if (is_array($notice)) {
1017 //FIXME: make everything calling showJsonTimeline use only Notice objects
1019 foreach ($notice as $n) {
1020 $ids[] = $n->getID();
1022 $notice = Notice::multiGet('id', $ids);
1025 while ($notice->fetch()) {
1027 $twitter_status = $this->twitterStatusArray($notice);
1028 array_push($statuses, $twitter_status);
1029 } catch (Exception $e) {
1030 common_log(LOG_ERR, $e->getMessage());
1035 $this->showJsonObjects($statuses);
1037 $this->endDocument('json');
1040 function showJsonGroups($group)
1042 $this->initDocument('json');
1046 if (is_array($group)) {
1047 foreach ($group as $g) {
1048 $twitter_group = $this->twitterGroupArray($g);
1049 array_push($groups, $twitter_group);
1052 while ($group->fetch()) {
1053 $twitter_group = $this->twitterGroupArray($group);
1054 array_push($groups, $twitter_group);
1058 $this->showJsonObjects($groups);
1060 $this->endDocument('json');
1063 function showXmlGroups($group)
1066 $this->initDocument('xml');
1067 $this->elementStart('groups', array('type' => 'array'));
1069 if (is_array($group)) {
1070 foreach ($group as $g) {
1071 $twitter_group = $this->twitterGroupArray($g);
1072 $this->showTwitterXmlGroup($twitter_group);
1075 while ($group->fetch()) {
1076 $twitter_group = $this->twitterGroupArray($group);
1077 $this->showTwitterXmlGroup($twitter_group);
1081 $this->elementEnd('groups');
1082 $this->endDocument('xml');
1085 function showXmlLists($list, $next_cursor=0, $prev_cursor=0)
1088 $this->initDocument('xml');
1089 $this->elementStart('lists_list');
1090 $this->elementStart('lists', array('type' => 'array'));
1092 if (is_array($list)) {
1093 foreach ($list as $l) {
1094 $twitter_list = $this->twitterListArray($l);
1095 $this->showTwitterXmlList($twitter_list);
1098 while ($list->fetch()) {
1099 $twitter_list = $this->twitterListArray($list);
1100 $this->showTwitterXmlList($twitter_list);
1104 $this->elementEnd('lists');
1106 $this->element('next_cursor', null, $next_cursor);
1107 $this->element('previous_cursor', null, $prev_cursor);
1109 $this->elementEnd('lists_list');
1110 $this->endDocument('xml');
1113 function showJsonLists($list, $next_cursor=0, $prev_cursor=0)
1115 $this->initDocument('json');
1119 if (is_array($list)) {
1120 foreach ($list as $l) {
1121 $twitter_list = $this->twitterListArray($l);
1122 array_push($lists, $twitter_list);
1125 while ($list->fetch()) {
1126 $twitter_list = $this->twitterListArray($list);
1127 array_push($lists, $twitter_list);
1131 $lists_list = array(
1133 'next_cursor' => $next_cursor,
1134 'next_cursor_str' => strval($next_cursor),
1135 'previous_cursor' => $prev_cursor,
1136 'previous_cursor_str' => strval($prev_cursor)
1139 $this->showJsonObjects($lists_list);
1141 $this->endDocument('json');
1144 function showTwitterXmlUsers($user)
1146 $this->initDocument('xml');
1147 $this->elementStart('users', array('type' => 'array',
1148 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
1150 if (is_array($user)) {
1151 foreach ($user as $u) {
1152 $twitter_user = $this->twitterUserArray($u);
1153 $this->showTwitterXmlUser($twitter_user);
1156 while ($user->fetch()) {
1157 $twitter_user = $this->twitterUserArray($user);
1158 $this->showTwitterXmlUser($twitter_user);
1162 $this->elementEnd('users');
1163 $this->endDocument('xml');
1166 function showJsonUsers($user)
1168 $this->initDocument('json');
1172 if (is_array($user)) {
1173 foreach ($user as $u) {
1174 $twitter_user = $this->twitterUserArray($u);
1175 array_push($users, $twitter_user);
1178 while ($user->fetch()) {
1179 $twitter_user = $this->twitterUserArray($user);
1180 array_push($users, $twitter_user);
1184 $this->showJsonObjects($users);
1186 $this->endDocument('json');
1189 function showSingleJsonGroup($group)
1191 $this->initDocument('json');
1192 $twitter_group = $this->twitterGroupArray($group);
1193 $this->showJsonObjects($twitter_group);
1194 $this->endDocument('json');
1197 function showSingleXmlGroup($group)
1199 $this->initDocument('xml');
1200 $twitter_group = $this->twitterGroupArray($group);
1201 $this->showTwitterXmlGroup($twitter_group);
1202 $this->endDocument('xml');
1205 function showSingleJsonList($list)
1207 $this->initDocument('json');
1208 $twitter_list = $this->twitterListArray($list);
1209 $this->showJsonObjects($twitter_list);
1210 $this->endDocument('json');
1213 function showSingleXmlList($list)
1215 $this->initDocument('xml');
1216 $twitter_list = $this->twitterListArray($list);
1217 $this->showTwitterXmlList($twitter_list);
1218 $this->endDocument('xml');
1221 static function dateTwitter($dt)
1223 $dateStr = date('d F Y H:i:s', strtotime($dt));
1224 $d = new DateTime($dateStr, new DateTimeZone('UTC'));
1225 $d->setTimezone(new DateTimeZone(common_timezone()));
1226 return $d->format('D M d H:i:s O Y');
1229 function initDocument($type='xml')
1233 header('Content-Type: application/xml; charset=utf-8');
1237 header('Content-Type: application/json; charset=utf-8');
1239 // Check for JSONP callback
1240 if (isset($this->callback)) {
1241 print $this->callback . '(';
1245 header("Content-Type: application/rss+xml; charset=utf-8");
1246 $this->initTwitterRss();
1249 header('Content-Type: application/atom+xml; charset=utf-8');
1250 $this->initTwitterAtom();
1253 // TRANS: Client error on an API request with an unsupported data format.
1254 $this->clientError(_('Not a supported data format.'));
1260 function endDocument($type='xml')
1267 // Check for JSONP callback
1268 if (isset($this->callback)) {
1273 $this->endTwitterRss();
1276 $this->endTwitterRss();
1279 // TRANS: Client error on an API request with an unsupported data format.
1280 $this->clientError(_('Not a supported data format.'));
1285 function initTwitterRss()
1288 $this->elementStart(
1292 'xmlns:atom' => 'http://www.w3.org/2005/Atom',
1293 'xmlns:georss' => 'http://www.georss.org/georss'
1296 $this->elementStart('channel');
1297 Event::handle('StartApiRss', array($this));
1300 function endTwitterRss()
1302 $this->elementEnd('channel');
1303 $this->elementEnd('rss');
1307 function initTwitterAtom()
1310 // FIXME: don't hardcode the language here!
1311 $this->elementStart('feed', array('xmlns' => 'http://www.w3.org/2005/Atom',
1312 'xml:lang' => 'en-US',
1313 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0'));
1316 function endTwitterAtom()
1318 $this->elementEnd('feed');
1322 function showProfile($profile, $content_type='xml', $notice=null, $includeStatuses=true)
1324 $profile_array = $this->twitterUserArray($profile, $includeStatuses);
1325 switch ($content_type) {
1327 $this->showTwitterXmlUser($profile_array);
1330 $this->showJsonObjects($profile_array);
1333 // TRANS: Client error on an API request with an unsupported data format.
1334 $this->clientError(_('Not a supported data format.'));
1339 private static function is_decimal($str)
1341 return preg_match('/^[0-9]+$/', $str);
1344 function getTargetUser($id)
1347 // Twitter supports these other ways of passing the user ID
1348 if (self::is_decimal($this->arg('id'))) {
1349 return User::getKV($this->arg('id'));
1350 } else if ($this->arg('id')) {
1351 $nickname = common_canonical_nickname($this->arg('id'));
1352 return User::getKV('nickname', $nickname);
1353 } else if ($this->arg('user_id')) {
1354 // This is to ensure that a non-numeric user_id still
1355 // overrides screen_name even if it doesn't get used
1356 if (self::is_decimal($this->arg('user_id'))) {
1357 return User::getKV('id', $this->arg('user_id'));
1359 } else if ($this->arg('screen_name')) {
1360 $nickname = common_canonical_nickname($this->arg('screen_name'));
1361 return User::getKV('nickname', $nickname);
1363 // Fall back to trying the currently authenticated user
1364 return $this->scoped->getUser();
1367 } else if (self::is_decimal($id)) {
1368 return User::getKV($id);
1370 $nickname = common_canonical_nickname($id);
1371 return User::getKV('nickname', $nickname);
1375 function getTargetProfile($id)
1379 // Twitter supports these other ways of passing the user ID
1380 if (self::is_decimal($this->arg('id'))) {
1381 return Profile::getKV($this->arg('id'));
1382 } else if ($this->arg('id')) {
1383 // Screen names currently can only uniquely identify a local user.
1384 $nickname = common_canonical_nickname($this->arg('id'));
1385 $user = User::getKV('nickname', $nickname);
1386 return $user ? $user->getProfile() : null;
1387 } else if ($this->arg('user_id')) {
1388 // This is to ensure that a non-numeric user_id still
1389 // overrides screen_name even if it doesn't get used
1390 if (self::is_decimal($this->arg('user_id'))) {
1391 return Profile::getKV('id', $this->arg('user_id'));
1393 } else if ($this->arg('screen_name')) {
1394 $nickname = common_canonical_nickname($this->arg('screen_name'));
1395 $user = User::getKV('nickname', $nickname);
1396 return $user instanceof User ? $user->getProfile() : null;
1398 // Fall back to trying the currently authenticated user
1399 return $this->scoped;
1401 } else if (self::is_decimal($id)) {
1402 return Profile::getKV($id);
1404 $nickname = common_canonical_nickname($id);
1405 $user = User::getKV('nickname', $nickname);
1406 return $user ? $user->getProfile() : null;
1410 function getTargetGroup($id)
1413 if (self::is_decimal($this->arg('id'))) {
1414 return User_group::getKV('id', $this->arg('id'));
1415 } else if ($this->arg('id')) {
1416 return User_group::getForNickname($this->arg('id'));
1417 } else if ($this->arg('group_id')) {
1418 // This is to ensure that a non-numeric group_id still
1419 // overrides group_name even if it doesn't get used
1420 if (self::is_decimal($this->arg('group_id'))) {
1421 return User_group::getKV('id', $this->arg('group_id'));
1423 } else if ($this->arg('group_name')) {
1424 return User_group::getForNickname($this->arg('group_name'));
1427 } else if (self::is_decimal($id)) {
1428 return User_group::getKV('id', $id);
1429 } else if ($this->arg('uri')) { // FIXME: move this into empty($id) check?
1430 return User_group::getKV('uri', urldecode($this->arg('uri')));
1432 return User_group::getForNickname($id);
1436 function getTargetList($user=null, $id=null)
1438 $tagger = $this->getTargetUser($user);
1442 $id = $this->arg('id');
1446 if (is_numeric($id)) {
1447 $list = Profile_list::getKV('id', $id);
1449 // only if the list with the id belongs to the tagger
1450 if(empty($list) || $list->tagger != $tagger->id) {
1455 $tag = common_canonical_tag($id);
1456 $list = Profile_list::getByTaggerAndTag($tagger->id, $tag);
1459 if (!empty($list) && $list->private) {
1460 if ($this->scoped->id == $list->tagger) {
1471 * Returns query argument or default value if not found. Certain
1472 * parameters used throughout the API are lightly scrubbed and
1473 * bounds checked. This overrides Action::arg().
1475 * @param string $key requested argument
1476 * @param string $def default value to return if $key is not provided
1480 function arg($key, $def=null)
1482 // XXX: Do even more input validation/scrubbing?
1484 if (array_key_exists($key, $this->args)) {
1487 $page = (int)$this->args['page'];
1488 return ($page < 1) ? 1 : $page;
1490 $count = (int)$this->args['count'];
1493 } elseif ($count > 200) {
1499 $since_id = (int)$this->args['since_id'];
1500 return ($since_id < 1) ? 0 : $since_id;
1502 $max_id = (int)$this->args['max_id'];
1503 return ($max_id < 1) ? 0 : $max_id;
1505 return parent::arg($key, $def);
1513 * Calculate the complete URI that called up this action. Used for
1514 * Atom rel="self" links. Warning: this is funky.
1516 * @return string URL a URL suitable for rel="self" Atom links
1518 function getSelfUri()
1520 $action = mb_substr(get_class($this), 0, -6); // remove 'Action'
1522 $id = $this->arg('id');
1523 $aargs = array('format' => $this->format);
1528 $user = $this->arg('user');
1529 if (!empty($user)) {
1530 $aargs['user'] = $user;
1533 $tag = $this->arg('tag');
1535 $aargs['tag'] = $tag;
1538 parse_str($_SERVER['QUERY_STRING'], $params);
1540 if (!empty($params)) {
1541 unset($params['p']);
1542 $pstring = http_build_query($params);
1545 $uri = common_local_url($action, $aargs);
1547 if (!empty($pstring)) {
1548 $uri .= '?' . $pstring;