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
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;
124 var $auth_user = null;
128 var $since_id = null;
130 var $callback = null;
132 var $access = self::READ_ONLY; // read (default) or read-write
134 static $reserved_sources = array('web', 'omb', 'ostatus', 'mail', 'xmpp', 'api');
139 * @param array $args Web and URL arguments
141 * @return boolean false if user doesn't exist
143 function prepare($args)
145 StatusNet::setApi(true); // reduce exception reports to aid in debugging
146 parent::prepare($args);
148 $this->format = $this->arg('format');
149 $this->callback = $this->arg('callback');
150 $this->page = (int)$this->arg('page', 1);
151 $this->count = (int)$this->arg('count', 20);
152 $this->max_id = (int)$this->arg('max_id', 0);
153 $this->since_id = (int)$this->arg('since_id', 0);
155 if ($this->arg('since')) {
156 header('X-StatusNet-Warning: since parameter is disabled; use since_id');
159 $this->source = $this->trimmed('source');
161 if (empty($this->source) || in_array($this->source, self::$reserved_sources)) {
162 $this->source = 'api';
171 * @param array $args Arguments from $_REQUEST
175 function handle($args)
177 header('Access-Control-Allow-Origin: *');
178 parent::handle($args);
182 * Overrides XMLOutputter::element to write booleans as strings (true|false).
183 * See that method's documentation for more info.
185 * @param string $tag Element type or tagname
186 * @param array $attrs Array of element attributes, as
188 * @param string $content string content of the element
192 function element($tag, $attrs=null, $content=null)
194 if (is_bool($content)) {
195 $content = ($content ? 'true' : 'false');
198 return parent::element($tag, $attrs, $content);
201 function twitterUserArray($profile, $get_notice=false)
203 $twitter_user = array();
205 $twitter_user['id'] = intval($profile->id);
206 $twitter_user['name'] = $profile->getBestName();
207 $twitter_user['screen_name'] = $profile->nickname;
208 $twitter_user['location'] = ($profile->location) ? $profile->location : null;
209 $twitter_user['description'] = ($profile->bio) ? $profile->bio : null;
211 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
212 $twitter_user['profile_image_url'] = ($avatar) ? $avatar->displayUrl() :
213 Avatar::defaultImage(AVATAR_STREAM_SIZE);
215 $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
216 $twitter_user['protected'] = false; # not supported by StatusNet yet
217 $twitter_user['followers_count'] = $profile->subscriberCount();
220 $user = $profile->getUser();
222 // Note: some profiles don't have an associated user
224 $defaultDesign = Design::siteDesign();
227 $design = $user->getDesign();
230 if (empty($design)) {
231 $design = $defaultDesign;
234 $color = Design::toWebColor(empty($design->backgroundcolor) ? $defaultDesign->backgroundcolor : $design->backgroundcolor);
235 $twitter_user['profile_background_color'] = ($color == null) ? '' : '#'.$color->hexValue();
236 $color = Design::toWebColor(empty($design->textcolor) ? $defaultDesign->textcolor : $design->textcolor);
237 $twitter_user['profile_text_color'] = ($color == null) ? '' : '#'.$color->hexValue();
238 $color = Design::toWebColor(empty($design->linkcolor) ? $defaultDesign->linkcolor : $design->linkcolor);
239 $twitter_user['profile_link_color'] = ($color == null) ? '' : '#'.$color->hexValue();
240 $color = Design::toWebColor(empty($design->sidebarcolor) ? $defaultDesign->sidebarcolor : $design->sidebarcolor);
241 $twitter_user['profile_sidebar_fill_color'] = ($color == null) ? '' : '#'.$color->hexValue();
242 $twitter_user['profile_sidebar_border_color'] = '';
244 $twitter_user['friends_count'] = $profile->subscriptionCount();
246 $twitter_user['created_at'] = $this->dateTwitter($profile->created);
248 $twitter_user['favourites_count'] = $profile->faveCount(); // British spelling!
252 if (!empty($user) && $user->timezone) {
253 $timezone = $user->timezone;
257 $t->setTimezone(new DateTimeZone($timezone));
259 $twitter_user['utc_offset'] = $t->format('Z');
260 $twitter_user['time_zone'] = $timezone;
262 $twitter_user['profile_background_image_url']
263 = empty($design->backgroundimage)
264 ? '' : ($design->disposition & BACKGROUND_ON)
265 ? Design::url($design->backgroundimage) : '';
267 $twitter_user['profile_background_tile']
268 = (bool)($design->disposition & BACKGROUND_TILE);
270 $twitter_user['statuses_count'] = $profile->noticeCount();
272 // Is the requesting user following this user?
273 $twitter_user['following'] = false;
274 $twitter_user['statusnet:blocking'] = false;
275 $twitter_user['notifications'] = false;
277 if (isset($this->auth_user)) {
279 $twitter_user['following'] = $this->auth_user->isSubscribed($profile);
280 $twitter_user['statusnet:blocking'] = $this->auth_user->hasBlocked($profile);
283 $sub = Subscription::pkeyGet(array('subscriber' =>
284 $this->auth_user->id,
285 'subscribed' => $profile->id));
288 $twitter_user['notifications'] = ($sub->jabber || $sub->sms);
293 $notice = $profile->getCurrentNotice();
296 $twitter_user['status'] = $this->twitterStatusArray($notice, false);
300 // StatusNet-specific
302 $twitter_user['statusnet_profile_url'] = $profile->profileurl;
304 return $twitter_user;
307 function twitterStatusArray($notice, $include_user=true)
309 $base = $this->twitterSimpleStatusArray($notice, $include_user);
311 if (!empty($notice->repeat_of)) {
312 $original = Notice::staticGet('id', $notice->repeat_of);
313 if (!empty($original)) {
314 $original_array = $this->twitterSimpleStatusArray($original, $include_user);
315 $base['retweeted_status'] = $original_array;
322 function twitterSimpleStatusArray($notice, $include_user=true)
324 $profile = $notice->getProfile();
326 $twitter_status = array();
327 $twitter_status['text'] = $notice->content;
328 $twitter_status['truncated'] = false; # Not possible on StatusNet
329 $twitter_status['created_at'] = $this->dateTwitter($notice->created);
330 $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ?
331 intval($notice->reply_to) : null;
335 $ns = $notice->getSource();
337 if (!empty($ns->name) && !empty($ns->url)) {
338 $source = '<a href="'
339 . htmlspecialchars($ns->url)
340 . '" rel="nofollow">'
341 . htmlspecialchars($ns->name)
348 $twitter_status['source'] = $source;
349 $twitter_status['id'] = intval($notice->id);
351 $replier_profile = null;
353 if ($notice->reply_to) {
354 $reply = Notice::staticGet(intval($notice->reply_to));
356 $replier_profile = $reply->getProfile();
360 $twitter_status['in_reply_to_user_id'] =
361 ($replier_profile) ? intval($replier_profile->id) : null;
362 $twitter_status['in_reply_to_screen_name'] =
363 ($replier_profile) ? $replier_profile->nickname : null;
365 if (isset($notice->lat) && isset($notice->lon)) {
366 // This is the format that GeoJSON expects stuff to be in
367 $twitter_status['geo'] = array('type' => 'Point',
368 'coordinates' => array((float) $notice->lat,
369 (float) $notice->lon));
371 $twitter_status['geo'] = null;
374 if (isset($this->auth_user)) {
375 $twitter_status['favorited'] = $this->auth_user->hasFave($notice);
377 $twitter_status['favorited'] = false;
381 $attachments = $notice->attachments();
383 if (!empty($attachments)) {
385 $twitter_status['attachments'] = array();
387 foreach ($attachments as $attachment) {
388 $enclosure_o=$attachment->getEnclosure();
390 $enclosure = array();
391 $enclosure['url'] = $enclosure_o->url;
392 $enclosure['mimetype'] = $enclosure_o->mimetype;
393 $enclosure['size'] = $enclosure_o->size;
394 $twitter_status['attachments'][] = $enclosure;
399 if ($include_user && $profile) {
400 // Don't get notice (recursive!)
401 $twitter_user = $this->twitterUserArray($profile, false);
402 $twitter_status['user'] = $twitter_user;
405 // StatusNet-specific
407 $twitter_status['statusnet_html'] = $notice->rendered;
409 return $twitter_status;
412 function twitterGroupArray($group)
414 $twitter_group = array();
416 $twitter_group['id'] = intval($group->id);
417 $twitter_group['url'] = $group->permalink();
418 $twitter_group['nickname'] = $group->nickname;
419 $twitter_group['fullname'] = $group->fullname;
421 if (isset($this->auth_user)) {
422 $twitter_group['member'] = $this->auth_user->isMember($group);
423 $twitter_group['blocked'] = Group_block::isBlocked(
425 $this->auth_user->getProfile()
429 $twitter_group['member_count'] = $group->getMemberCount();
430 $twitter_group['original_logo'] = $group->original_logo;
431 $twitter_group['homepage_logo'] = $group->homepage_logo;
432 $twitter_group['stream_logo'] = $group->stream_logo;
433 $twitter_group['mini_logo'] = $group->mini_logo;
434 $twitter_group['homepage'] = $group->homepage;
435 $twitter_group['description'] = $group->description;
436 $twitter_group['location'] = $group->location;
437 $twitter_group['created'] = $this->dateTwitter($group->created);
438 $twitter_group['modified'] = $this->dateTwitter($group->modified);
440 return $twitter_group;
443 function twitterRssGroupArray($group)
446 $entry['content']=$group->description;
447 $entry['title']=$group->nickname;
448 $entry['link']=$group->permalink();
449 $entry['published']=common_date_iso8601($group->created);
450 $entry['updated']==common_date_iso8601($group->modified);
451 $taguribase = common_config('integration', 'groupuri');
452 $entry['id'] = "group:$groupuribase:$entry[link]";
454 $entry['description'] = $entry['content'];
455 $entry['pubDate'] = common_date_rfc2822($group->created);
456 $entry['guid'] = $entry['link'];
461 function twitterListArray($list)
463 $profile = Profile::staticGet('id', $list->tagger);
465 $twitter_list = array();
466 $twitter_list['id'] = $list->id;
467 $twitter_list['name'] = $list->tag;
468 $twitter_list['full_name'] = '@'.$profile->nickname.'/'.$list->tag;;
469 $twitter_list['slug'] = $list->tag;
470 $twitter_list['description'] = $list->description;
471 $twitter_list['subscriber_count'] = $list->subscriberCount();
472 $twitter_list['member_count'] = $list->taggedCount();
473 $twitter_list['uri'] = $list->getUri();
475 if (isset($this->auth_user)) {
476 $twitter_list['following'] = $list->hasSubscriber($this->auth_user);
478 $twitter_list['following'] = false;
481 $twitter_list['mode'] = ($list->private) ? 'private' : 'public';
482 $twitter_list['user'] = $this->twitterUserArray($profile, false);
484 return $twitter_list;
487 function twitterRssEntryArray($notice)
491 if (Event::handle('StartRssEntryArray', array($notice, &$entry))) {
492 $profile = $notice->getProfile();
494 // We trim() to avoid extraneous whitespace in the output
496 $entry['content'] = common_xml_safe_str(trim($notice->rendered));
497 $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
498 $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
499 $entry['published'] = common_date_iso8601($notice->created);
501 $taguribase = TagURI::base();
502 $entry['id'] = "tag:$taguribase:$entry[link]";
504 $entry['updated'] = $entry['published'];
505 $entry['author'] = $profile->getBestName();
508 $attachments = $notice->attachments();
509 $enclosures = array();
511 foreach ($attachments as $attachment) {
512 $enclosure_o=$attachment->getEnclosure();
514 $enclosure = array();
515 $enclosure['url'] = $enclosure_o->url;
516 $enclosure['mimetype'] = $enclosure_o->mimetype;
517 $enclosure['size'] = $enclosure_o->size;
518 $enclosures[] = $enclosure;
522 if (!empty($enclosures)) {
523 $entry['enclosures'] = $enclosures;
527 $tag = new Notice_tag();
528 $tag->notice_id = $notice->id;
530 $entry['tags']=array();
531 while ($tag->fetch()) {
532 $entry['tags'][]=$tag->tag;
538 $entry['description'] = $entry['content'];
539 $entry['pubDate'] = common_date_rfc2822($notice->created);
540 $entry['guid'] = $entry['link'];
542 if (isset($notice->lat) && isset($notice->lon)) {
543 // This is the format that GeoJSON expects stuff to be in.
544 // showGeoRSS() below uses it for XML output, so we reuse it
545 $entry['geo'] = array('type' => 'Point',
546 'coordinates' => array((float) $notice->lat,
547 (float) $notice->lon));
549 $entry['geo'] = null;
552 Event::handle('EndRssEntryArray', array($notice, &$entry));
558 function twitterRelationshipArray($source, $target)
560 $relationship = array();
562 $relationship['source'] =
563 $this->relationshipDetailsArray($source, $target);
564 $relationship['target'] =
565 $this->relationshipDetailsArray($target, $source);
567 return array('relationship' => $relationship);
570 function relationshipDetailsArray($source, $target)
574 $details['screen_name'] = $source->nickname;
575 $details['followed_by'] = $target->isSubscribed($source);
576 $details['following'] = $source->isSubscribed($target);
578 $notifications = false;
580 if ($source->isSubscribed($target)) {
581 $sub = Subscription::pkeyGet(array('subscriber' =>
582 $source->id, 'subscribed' => $target->id));
585 $notifications = ($sub->jabber || $sub->sms);
589 $details['notifications_enabled'] = $notifications;
590 $details['blocking'] = $source->hasBlocked($target);
591 $details['id'] = intval($source->id);
596 function showTwitterXmlRelationship($relationship)
598 $this->elementStart('relationship');
600 foreach($relationship as $element => $value) {
601 if ($element == 'source' || $element == 'target') {
602 $this->elementStart($element);
603 $this->showXmlRelationshipDetails($value);
604 $this->elementEnd($element);
608 $this->elementEnd('relationship');
611 function showXmlRelationshipDetails($details)
613 foreach($details as $element => $value) {
614 $this->element($element, null, $value);
618 function showTwitterXmlStatus($twitter_status, $tag='status', $namespaces=false)
622 $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
624 $this->elementStart($tag, $attrs);
625 foreach($twitter_status as $element => $value) {
628 $this->showTwitterXmlUser($twitter_status['user']);
631 $this->element($element, null, common_xml_safe_str($value));
634 $this->showXmlAttachments($twitter_status['attachments']);
637 $this->showGeoXML($value);
639 case 'retweeted_status':
640 $this->showTwitterXmlStatus($value, 'retweeted_status');
643 if (strncmp($element, 'statusnet_', 10) == 0) {
644 $this->element('statusnet:'.substr($element, 10), null, $value);
646 $this->element($element, null, $value);
650 $this->elementEnd($tag);
653 function showTwitterXmlGroup($twitter_group)
655 $this->elementStart('group');
656 foreach($twitter_group as $element => $value) {
657 $this->element($element, null, $value);
659 $this->elementEnd('group');
662 function showTwitterXmlList($twitter_list)
664 $this->elementStart('list');
665 foreach($twitter_list as $element => $value) {
666 if($element == 'user') {
667 $this->showTwitterXmlUser($value, 'user');
670 $this->element($element, null, $value);
673 $this->elementEnd('list');
676 function showTwitterXmlUser($twitter_user, $role='user', $namespaces=false)
680 $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
682 $this->elementStart($role, $attrs);
683 foreach($twitter_user as $element => $value) {
684 if ($element == 'status') {
685 $this->showTwitterXmlStatus($twitter_user['status']);
686 } else if (strncmp($element, 'statusnet_', 10) == 0) {
687 $this->element('statusnet:'.substr($element, 10), null, $value);
689 $this->element($element, null, $value);
692 $this->elementEnd($role);
695 function showXmlAttachments($attachments) {
696 if (!empty($attachments)) {
697 $this->elementStart('attachments', array('type' => 'array'));
698 foreach ($attachments as $attachment) {
700 $attrs['url'] = $attachment['url'];
701 $attrs['mimetype'] = $attachment['mimetype'];
702 $attrs['size'] = $attachment['size'];
703 $this->element('enclosure', $attrs, '');
705 $this->elementEnd('attachments');
709 function showGeoXML($geo)
713 $this->element('geo');
715 $this->elementStart('geo', array('xmlns:georss' => 'http://www.georss.org/georss'));
716 $this->element('georss:point', null, $geo['coordinates'][0] . ' ' . $geo['coordinates'][1]);
717 $this->elementEnd('geo');
721 function showGeoRSS($geo)
727 $geo['coordinates'][0] . ' ' . $geo['coordinates'][1]
732 function showTwitterRssItem($entry)
734 $this->elementStart('item');
735 $this->element('title', null, $entry['title']);
736 $this->element('description', null, $entry['description']);
737 $this->element('pubDate', null, $entry['pubDate']);
738 $this->element('guid', null, $entry['guid']);
739 $this->element('link', null, $entry['link']);
741 // RSS only supports 1 enclosure per item
742 if(array_key_exists('enclosures', $entry) and !empty($entry['enclosures'])){
743 $enclosure = $entry['enclosures'][0];
744 $this->element('enclosure', array('url'=>$enclosure['url'],'type'=>$enclosure['mimetype'],'length'=>$enclosure['size']), null);
747 if(array_key_exists('tags', $entry)){
748 foreach($entry['tags'] as $tag){
749 $this->element('category', null,$tag);
753 $this->showGeoRSS($entry['geo']);
754 $this->elementEnd('item');
757 function showJsonObjects($objects)
759 print(json_encode($objects));
762 function showSingleXmlStatus($notice)
764 $this->initDocument('xml');
765 $twitter_status = $this->twitterStatusArray($notice);
766 $this->showTwitterXmlStatus($twitter_status, 'status', true);
767 $this->endDocument('xml');
770 function showSingleAtomStatus($notice)
772 header('Content-Type: application/atom+xml; charset=utf-8');
773 print $notice->asAtomEntry(true, true, true, $this->auth_user);
776 function show_single_json_status($notice)
778 $this->initDocument('json');
779 $status = $this->twitterStatusArray($notice);
780 $this->showJsonObjects($status);
781 $this->endDocument('json');
784 function showXmlTimeline($notice)
786 $this->initDocument('xml');
787 $this->elementStart('statuses', array('type' => 'array',
788 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
790 if (is_array($notice)) {
791 $notice = new ArrayWrapper($notice);
794 while ($notice->fetch()) {
796 $twitter_status = $this->twitterStatusArray($notice);
797 $this->showTwitterXmlStatus($twitter_status);
798 } catch (Exception $e) {
799 common_log(LOG_ERR, $e->getMessage());
804 $this->elementEnd('statuses');
805 $this->endDocument('xml');
808 function showRssTimeline($notice, $title, $link, $subtitle, $suplink = null, $logo = null, $self = null)
810 $this->initDocument('rss');
812 $this->element('title', null, $title);
813 $this->element('link', null, $link);
815 if (!is_null($self)) {
819 'type' => 'application/rss+xml',
826 if (!is_null($suplink)) {
827 // For FriendFeed's SUP protocol
828 $this->element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
829 'rel' => 'http://api.friendfeed.com/2008/03#sup',
831 'type' => 'application/json'));
834 if (!is_null($logo)) {
835 $this->elementStart('image');
836 $this->element('link', null, $link);
837 $this->element('title', null, $title);
838 $this->element('url', null, $logo);
839 $this->elementEnd('image');
842 $this->element('description', null, $subtitle);
843 $this->element('language', null, 'en-us');
844 $this->element('ttl', null, '40');
846 if (is_array($notice)) {
847 $notice = new ArrayWrapper($notice);
850 while ($notice->fetch()) {
852 $entry = $this->twitterRssEntryArray($notice);
853 $this->showTwitterRssItem($entry);
854 } catch (Exception $e) {
855 common_log(LOG_ERR, $e->getMessage());
856 // continue on exceptions
860 $this->endTwitterRss();
863 function showAtomTimeline($notice, $title, $id, $link, $subtitle=null, $suplink=null, $selfuri=null, $logo=null)
865 $this->initDocument('atom');
867 $this->element('title', null, $title);
868 $this->element('id', null, $id);
869 $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
871 if (!is_null($logo)) {
872 $this->element('logo',null,$logo);
875 if (!is_null($suplink)) {
876 // For FriendFeed's SUP protocol
877 $this->element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
879 'type' => 'application/json'));
882 if (!is_null($selfuri)) {
883 $this->element('link', array('href' => $selfuri,
884 'rel' => 'self', 'type' => 'application/atom+xml'), null);
887 $this->element('updated', null, common_date_iso8601('now'));
888 $this->element('subtitle', null, $subtitle);
890 if (is_array($notice)) {
891 $notice = new ArrayWrapper($notice);
894 while ($notice->fetch()) {
896 $this->raw($notice->asAtomEntry());
897 } catch (Exception $e) {
898 common_log(LOG_ERR, $e->getMessage());
903 $this->endDocument('atom');
906 function showRssGroups($group, $title, $link, $subtitle)
908 $this->initDocument('rss');
910 $this->element('title', null, $title);
911 $this->element('link', null, $link);
912 $this->element('description', null, $subtitle);
913 $this->element('language', null, 'en-us');
914 $this->element('ttl', null, '40');
916 if (is_array($group)) {
917 foreach ($group as $g) {
918 $twitter_group = $this->twitterRssGroupArray($g);
919 $this->showTwitterRssItem($twitter_group);
922 while ($group->fetch()) {
923 $twitter_group = $this->twitterRssGroupArray($group);
924 $this->showTwitterRssItem($twitter_group);
928 $this->endTwitterRss();
931 function showTwitterAtomEntry($entry)
933 $this->elementStart('entry');
934 $this->element('title', null, common_xml_safe_str($entry['title']));
937 array('type' => 'html'),
938 common_xml_safe_str($entry['content'])
940 $this->element('id', null, $entry['id']);
941 $this->element('published', null, $entry['published']);
942 $this->element('updated', null, $entry['updated']);
943 $this->element('link', array('type' => 'text/html',
944 'href' => $entry['link'],
945 'rel' => 'alternate'));
946 $this->element('link', array('type' => $entry['avatar-type'],
947 'href' => $entry['avatar'],
949 $this->elementStart('author');
951 $this->element('name', null, $entry['author-name']);
952 $this->element('uri', null, $entry['author-uri']);
954 $this->elementEnd('author');
955 $this->elementEnd('entry');
958 function showXmlDirectMessage($dm, $namespaces=false)
962 $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
964 $this->elementStart('direct_message', $attrs);
965 foreach($dm as $element => $value) {
969 $this->showTwitterXmlUser($value, $element);
972 $this->element($element, null, common_xml_safe_str($value));
975 $this->element($element, null, $value);
979 $this->elementEnd('direct_message');
982 function directMessageArray($message)
986 $from_profile = $message->getFrom();
987 $to_profile = $message->getTo();
989 $dmsg['id'] = intval($message->id);
990 $dmsg['sender_id'] = intval($from_profile);
991 $dmsg['text'] = trim($message->content);
992 $dmsg['recipient_id'] = intval($to_profile);
993 $dmsg['created_at'] = $this->dateTwitter($message->created);
994 $dmsg['sender_screen_name'] = $from_profile->nickname;
995 $dmsg['recipient_screen_name'] = $to_profile->nickname;
996 $dmsg['sender'] = $this->twitterUserArray($from_profile, false);
997 $dmsg['recipient'] = $this->twitterUserArray($to_profile, false);
1002 function rssDirectMessageArray($message)
1006 $from = $message->getFrom();
1008 $entry['title'] = sprintf('Message from %1$s to %2$s',
1009 $from->nickname, $message->getTo()->nickname);
1011 $entry['content'] = common_xml_safe_str($message->rendered);
1012 $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
1013 $entry['published'] = common_date_iso8601($message->created);
1015 $taguribase = TagURI::base();
1017 $entry['id'] = "tag:$taguribase:$entry[link]";
1018 $entry['updated'] = $entry['published'];
1020 $entry['author-name'] = $from->getBestName();
1021 $entry['author-uri'] = $from->homepage;
1023 $avatar = $from->getAvatar(AVATAR_STREAM_SIZE);
1025 $entry['avatar'] = (!empty($avatar)) ? $avatar->url : Avatar::defaultImage(AVATAR_STREAM_SIZE);
1026 $entry['avatar-type'] = (!empty($avatar)) ? $avatar->mediatype : 'image/png';
1028 // RSS item specific
1030 $entry['description'] = $entry['content'];
1031 $entry['pubDate'] = common_date_rfc2822($message->created);
1032 $entry['guid'] = $entry['link'];
1037 function showSingleXmlDirectMessage($message)
1039 $this->initDocument('xml');
1040 $dmsg = $this->directMessageArray($message);
1041 $this->showXmlDirectMessage($dmsg, true);
1042 $this->endDocument('xml');
1045 function showSingleJsonDirectMessage($message)
1047 $this->initDocument('json');
1048 $dmsg = $this->directMessageArray($message);
1049 $this->showJsonObjects($dmsg);
1050 $this->endDocument('json');
1053 function showAtomGroups($group, $title, $id, $link, $subtitle=null, $selfuri=null)
1055 $this->initDocument('atom');
1057 $this->element('title', null, common_xml_safe_str($title));
1058 $this->element('id', null, $id);
1059 $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
1061 if (!is_null($selfuri)) {
1062 $this->element('link', array('href' => $selfuri,
1063 'rel' => 'self', 'type' => 'application/atom+xml'), null);
1066 $this->element('updated', null, common_date_iso8601('now'));
1067 $this->element('subtitle', null, common_xml_safe_str($subtitle));
1069 if (is_array($group)) {
1070 foreach ($group as $g) {
1071 $this->raw($g->asAtomEntry());
1074 while ($group->fetch()) {
1075 $this->raw($group->asAtomEntry());
1079 $this->endDocument('atom');
1083 function showJsonTimeline($notice)
1085 $this->initDocument('json');
1087 $statuses = array();
1089 if (is_array($notice)) {
1090 $notice = new ArrayWrapper($notice);
1093 while ($notice->fetch()) {
1095 $twitter_status = $this->twitterStatusArray($notice);
1096 array_push($statuses, $twitter_status);
1097 } catch (Exception $e) {
1098 common_log(LOG_ERR, $e->getMessage());
1103 $this->showJsonObjects($statuses);
1105 $this->endDocument('json');
1108 function showJsonGroups($group)
1110 $this->initDocument('json');
1114 if (is_array($group)) {
1115 foreach ($group as $g) {
1116 $twitter_group = $this->twitterGroupArray($g);
1117 array_push($groups, $twitter_group);
1120 while ($group->fetch()) {
1121 $twitter_group = $this->twitterGroupArray($group);
1122 array_push($groups, $twitter_group);
1126 $this->showJsonObjects($groups);
1128 $this->endDocument('json');
1131 function showXmlGroups($group)
1134 $this->initDocument('xml');
1135 $this->elementStart('groups', array('type' => 'array'));
1137 if (is_array($group)) {
1138 foreach ($group as $g) {
1139 $twitter_group = $this->twitterGroupArray($g);
1140 $this->showTwitterXmlGroup($twitter_group);
1143 while ($group->fetch()) {
1144 $twitter_group = $this->twitterGroupArray($group);
1145 $this->showTwitterXmlGroup($twitter_group);
1149 $this->elementEnd('groups');
1150 $this->endDocument('xml');
1153 function showXmlLists($list, $next_cursor=0, $prev_cursor=0)
1156 $this->initDocument('xml');
1157 $this->elementStart('lists_list');
1158 $this->elementStart('lists', array('type' => 'array'));
1160 if (is_array($list)) {
1161 foreach ($list as $l) {
1162 $twitter_list = $this->twitterListArray($l);
1163 $this->showTwitterXmlList($twitter_list);
1166 while ($list->fetch()) {
1167 $twitter_list = $this->twitterListArray($list);
1168 $this->showTwitterXmlList($twitter_list);
1172 $this->elementEnd('lists');
1174 $this->element('next_cursor', null, $next_cursor);
1175 $this->element('previous_cursor', null, $prev_cursor);
1177 $this->elementEnd('lists_list');
1178 $this->endDocument('xml');
1181 function showJsonLists($list, $next_cursor=0, $prev_cursor=0)
1183 $this->initDocument('json');
1187 if (is_array($list)) {
1188 foreach ($list as $l) {
1189 $twitter_list = $this->twitterListArray($l);
1190 array_push($lists, $twitter_list);
1193 while ($list->fetch()) {
1194 $twitter_list = $this->twitterListArray($list);
1195 array_push($lists, $twitter_list);
1199 $lists_list = array(
1201 'next_cursor' => $next_cursor,
1202 'next_cursor_str' => strval($next_cursor),
1203 'previous_cursor' => $prev_cursor,
1204 'previous_cursor_str' => strval($prev_cursor)
1207 $this->showJsonObjects($lists_list);
1209 $this->endDocument('json');
1212 function showTwitterXmlUsers($user)
1214 $this->initDocument('xml');
1215 $this->elementStart('users', array('type' => 'array',
1216 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
1218 if (is_array($user)) {
1219 foreach ($user as $u) {
1220 $twitter_user = $this->twitterUserArray($u);
1221 $this->showTwitterXmlUser($twitter_user);
1224 while ($user->fetch()) {
1225 $twitter_user = $this->twitterUserArray($user);
1226 $this->showTwitterXmlUser($twitter_user);
1230 $this->elementEnd('users');
1231 $this->endDocument('xml');
1234 function showJsonUsers($user)
1236 $this->initDocument('json');
1240 if (is_array($user)) {
1241 foreach ($user as $u) {
1242 $twitter_user = $this->twitterUserArray($u);
1243 array_push($users, $twitter_user);
1246 while ($user->fetch()) {
1247 $twitter_user = $this->twitterUserArray($user);
1248 array_push($users, $twitter_user);
1252 $this->showJsonObjects($users);
1254 $this->endDocument('json');
1257 function showSingleJsonGroup($group)
1259 $this->initDocument('json');
1260 $twitter_group = $this->twitterGroupArray($group);
1261 $this->showJsonObjects($twitter_group);
1262 $this->endDocument('json');
1265 function showSingleXmlGroup($group)
1267 $this->initDocument('xml');
1268 $twitter_group = $this->twitterGroupArray($group);
1269 $this->showTwitterXmlGroup($twitter_group);
1270 $this->endDocument('xml');
1273 function showSingleJsonList($list)
1275 $this->initDocument('json');
1276 $twitter_list = $this->twitterListArray($list);
1277 $this->showJsonObjects($twitter_list);
1278 $this->endDocument('json');
1281 function showSingleXmlList($list)
1283 $this->initDocument('xml');
1284 $twitter_list = $this->twitterListArray($list);
1285 $this->showTwitterXmlList($twitter_list);
1286 $this->endDocument('xml');
1289 function dateTwitter($dt)
1291 $dateStr = date('d F Y H:i:s', strtotime($dt));
1292 $d = new DateTime($dateStr, new DateTimeZone('UTC'));
1293 $d->setTimezone(new DateTimeZone(common_timezone()));
1294 return $d->format('D M d H:i:s O Y');
1297 function initDocument($type='xml')
1301 header('Content-Type: application/xml; charset=utf-8');
1305 header('Content-Type: application/json; charset=utf-8');
1307 // Check for JSONP callback
1308 if (isset($this->callback)) {
1309 print $this->callback . '(';
1313 header("Content-Type: application/rss+xml; charset=utf-8");
1314 $this->initTwitterRss();
1317 header('Content-Type: application/atom+xml; charset=utf-8');
1318 $this->initTwitterAtom();
1321 // TRANS: Client error on an API request with an unsupported data format.
1322 $this->clientError(_('Not a supported data format.'));
1329 function endDocument($type='xml')
1336 // Check for JSONP callback
1337 if (isset($this->callback)) {
1342 $this->endTwitterRss();
1345 $this->endTwitterRss();
1348 // TRANS: Client error on an API request with an unsupported data format.
1349 $this->clientError(_('Not a supported data format.'));
1355 function clientError($msg, $code = 400, $format = null)
1357 $action = $this->trimmed('action');
1358 if ($format === null) {
1359 $format = $this->format;
1362 common_debug("User error '$code' on '$action': $msg", __FILE__);
1364 if (!array_key_exists($code, ClientErrorAction::$status)) {
1368 $status_string = ClientErrorAction::$status[$code];
1370 // Do not emit error header for JSONP
1371 if (!isset($this->callback)) {
1372 header('HTTP/1.1 ' . $code . ' ' . $status_string);
1377 $this->initDocument('xml');
1378 $this->elementStart('hash');
1379 $this->element('error', null, $msg);
1380 $this->element('request', null, $_SERVER['REQUEST_URI']);
1381 $this->elementEnd('hash');
1382 $this->endDocument('xml');
1385 $this->initDocument('json');
1386 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
1387 print(json_encode($error_array));
1388 $this->endDocument('json');
1391 header('Content-Type: text/plain; charset=utf-8');
1395 // If user didn't request a useful format, throw a regular client error
1396 throw new ClientException($msg, $code);
1400 function serverError($msg, $code = 500, $content_type = null)
1402 $action = $this->trimmed('action');
1403 if ($content_type === null) {
1404 $content_type = $this->format;
1407 common_debug("Server error '$code' on '$action': $msg", __FILE__);
1409 if (!array_key_exists($code, ServerErrorAction::$status)) {
1413 $status_string = ServerErrorAction::$status[$code];
1415 // Do not emit error header for JSONP
1416 if (!isset($this->callback)) {
1417 header('HTTP/1.1 '.$code.' '.$status_string);
1420 if ($content_type == 'xml') {
1421 $this->initDocument('xml');
1422 $this->elementStart('hash');
1423 $this->element('error', null, $msg);
1424 $this->element('request', null, $_SERVER['REQUEST_URI']);
1425 $this->elementEnd('hash');
1426 $this->endDocument('xml');
1428 $this->initDocument('json');
1429 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
1430 print(json_encode($error_array));
1431 $this->endDocument('json');
1435 function initTwitterRss()
1438 $this->elementStart(
1442 'xmlns:atom' => 'http://www.w3.org/2005/Atom',
1443 'xmlns:georss' => 'http://www.georss.org/georss'
1446 $this->elementStart('channel');
1447 Event::handle('StartApiRss', array($this));
1450 function endTwitterRss()
1452 $this->elementEnd('channel');
1453 $this->elementEnd('rss');
1457 function initTwitterAtom()
1460 // FIXME: don't hardcode the language here!
1461 $this->elementStart('feed', array('xmlns' => 'http://www.w3.org/2005/Atom',
1462 'xml:lang' => 'en-US',
1463 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0'));
1466 function endTwitterAtom()
1468 $this->elementEnd('feed');
1472 function showProfile($profile, $content_type='xml', $notice=null, $includeStatuses=true)
1474 $profile_array = $this->twitterUserArray($profile, $includeStatuses);
1475 switch ($content_type) {
1477 $this->showTwitterXmlUser($profile_array);
1480 $this->showJsonObjects($profile_array);
1483 // TRANS: Client error on an API request with an unsupported data format.
1484 $this->clientError(_('Not a supported data format.'));
1490 private static function is_decimal($str)
1492 return preg_match('/^[0-9]+$/', $str);
1495 function getTargetUser($id)
1498 // Twitter supports these other ways of passing the user ID
1499 if (self::is_decimal($this->arg('id'))) {
1500 return User::staticGet($this->arg('id'));
1501 } else if ($this->arg('id')) {
1502 $nickname = common_canonical_nickname($this->arg('id'));
1503 return User::staticGet('nickname', $nickname);
1504 } else if ($this->arg('user_id')) {
1505 // This is to ensure that a non-numeric user_id still
1506 // overrides screen_name even if it doesn't get used
1507 if (self::is_decimal($this->arg('user_id'))) {
1508 return User::staticGet('id', $this->arg('user_id'));
1510 } else if ($this->arg('screen_name')) {
1511 $nickname = common_canonical_nickname($this->arg('screen_name'));
1512 return User::staticGet('nickname', $nickname);
1514 // Fall back to trying the currently authenticated user
1515 return $this->auth_user;
1518 } else if (self::is_decimal($id)) {
1519 return User::staticGet($id);
1521 $nickname = common_canonical_nickname($id);
1522 return User::staticGet('nickname', $nickname);
1526 function getTargetProfile($id)
1530 // Twitter supports these other ways of passing the user ID
1531 if (self::is_decimal($this->arg('id'))) {
1532 return Profile::staticGet($this->arg('id'));
1533 } else if ($this->arg('id')) {
1534 // Screen names currently can only uniquely identify a local user.
1535 $nickname = common_canonical_nickname($this->arg('id'));
1536 $user = User::staticGet('nickname', $nickname);
1537 return $user ? $user->getProfile() : null;
1538 } else if ($this->arg('user_id')) {
1539 // This is to ensure that a non-numeric user_id still
1540 // overrides screen_name even if it doesn't get used
1541 if (self::is_decimal($this->arg('user_id'))) {
1542 return Profile::staticGet('id', $this->arg('user_id'));
1544 } else if ($this->arg('screen_name')) {
1545 $nickname = common_canonical_nickname($this->arg('screen_name'));
1546 $user = User::staticGet('nickname', $nickname);
1547 return $user ? $user->getProfile() : null;
1549 } else if (self::is_decimal($id)) {
1550 return Profile::staticGet($id);
1552 $nickname = common_canonical_nickname($id);
1553 $user = User::staticGet('nickname', $nickname);
1554 return $user ? $user->getProfile() : null;
1558 function getTargetGroup($id)
1561 if (self::is_decimal($this->arg('id'))) {
1562 return User_group::staticGet('id', $this->arg('id'));
1563 } else if ($this->arg('id')) {
1564 return User_group::getForNickname($this->arg('id'));
1565 } else if ($this->arg('group_id')) {
1566 // This is to ensure that a non-numeric group_id still
1567 // overrides group_name even if it doesn't get used
1568 if (self::is_decimal($this->arg('group_id'))) {
1569 return User_group::staticGet('id', $this->arg('group_id'));
1571 } else if ($this->arg('group_name')) {
1572 return User_group::getForNickname($this->arg('group_name'));
1575 } else if (self::is_decimal($id)) {
1576 return User_group::staticGet('id', $id);
1578 return User_group::getForNickname($id);
1582 function getTargetList($user=null, $id=null)
1584 $tagger = $this->getTargetUser($user);
1588 $id = $this->arg('id');
1592 if (is_numeric($id)) {
1593 $list = Profile_list::staticGet('id', $id);
1595 // only if the list with the id belongs to the tagger
1596 if(empty($list) || $list->tagger != $tagger->id) {
1601 $tag = common_canonical_tag($id);
1602 $list = Profile_list::getByTaggerAndTag($tagger->id, $tag);
1605 if (!empty($list) && $list->private) {
1606 if ($this->auth_user->id == $list->tagger) {
1617 * Returns query argument or default value if not found. Certain
1618 * parameters used throughout the API are lightly scrubbed and
1619 * bounds checked. This overrides Action::arg().
1621 * @param string $key requested argument
1622 * @param string $def default value to return if $key is not provided
1626 function arg($key, $def=null)
1628 // XXX: Do even more input validation/scrubbing?
1630 if (array_key_exists($key, $this->args)) {
1633 $page = (int)$this->args['page'];
1634 return ($page < 1) ? 1 : $page;
1636 $count = (int)$this->args['count'];
1639 } elseif ($count > 200) {
1645 $since_id = (int)$this->args['since_id'];
1646 return ($since_id < 1) ? 0 : $since_id;
1648 $max_id = (int)$this->args['max_id'];
1649 return ($max_id < 1) ? 0 : $max_id;
1651 return parent::arg($key, $def);
1659 * Calculate the complete URI that called up this action. Used for
1660 * Atom rel="self" links. Warning: this is funky.
1662 * @return string URL a URL suitable for rel="self" Atom links
1664 function getSelfUri()
1666 $action = mb_substr(get_class($this), 0, -6); // remove 'Action'
1668 $id = $this->arg('id');
1669 $aargs = array('format' => $this->format);
1674 $tag = $this->arg('tag');
1676 $aargs['tag'] = $tag;
1679 parse_str($_SERVER['QUERY_STRING'], $params);
1681 if (!empty($params)) {
1682 unset($params['p']);
1683 $pstring = http_build_query($params);
1686 $uri = common_local_url($action, $aargs);
1688 if (!empty($pstring)) {
1689 $uri .= '?' . $pstring;