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 Zach Copley <zach@status.net>
25 * @copyright 2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET')) {
35 * Contains most of the Twitter-compatible API output functions.
39 * @author Zach Copley <zach@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://status.net/
44 class ApiAction extends Action
50 * @param array $args Web and URL arguments
52 * @return boolean false if user doesn't exist
55 function prepare($args)
57 parent::prepare($args);
64 * @param array $args Arguments from $_REQUEST
69 function handle($args)
71 parent::handle($args);
75 * Overrides XMLOutputter::element to write booleans as strings (true|false).
76 * See that method's documentation for more info.
78 * @param string $tag Element type or tagname
79 * @param array $attrs Array of element attributes, as
81 * @param string $content string content of the element
85 function element($tag, $attrs=null, $content=null)
87 if (is_bool($content)) {
88 $content = ($content ? 'true' : 'false');
91 return parent::element($tag, $attrs, $content);
94 function twitter_user_array($profile, $get_notice=false)
96 $twitter_user = array();
98 $twitter_user['id'] = intval($profile->id);
99 $twitter_user['name'] = $profile->getBestName();
100 $twitter_user['screen_name'] = $profile->nickname;
101 $twitter_user['location'] = ($profile->location) ? $profile->location : null;
102 $twitter_user['description'] = ($profile->bio) ? $profile->bio : null;
104 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
105 $twitter_user['profile_image_url'] = ($avatar) ? $avatar->displayUrl() :
106 Avatar::defaultImage(AVATAR_STREAM_SIZE);
108 $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
109 $twitter_user['protected'] = false; # not supported by StatusNet yet
110 $twitter_user['followers_count'] = $profile->subscriberCount();
112 // To be supported soon...
113 $twitter_user['profile_background_color'] = '';
114 $twitter_user['profile_text_color'] = '';
115 $twitter_user['profile_link_color'] = '';
116 $twitter_user['profile_sidebar_fill_color'] = '';
117 $twitter_user['profile_sidebar_border_color'] = '';
119 $twitter_user['friends_count'] = $profile->subscriptionCount();
121 $twitter_user['created_at'] = $this->date_twitter($profile->created);
123 $twitter_user['favourites_count'] = $profile->faveCount(); // British spelling!
125 // Need to pull up the user for some of this
126 $user = User::staticGet($profile->id);
130 if ($user->timezone) {
131 $timezone = $user->timezone;
135 $t->setTimezone(new DateTimeZone($timezone));
137 $twitter_user['utc_offset'] = $t->format('Z');
138 $twitter_user['time_zone'] = $timezone;
140 // To be supported some day, perhaps
141 $twitter_user['profile_background_image_url'] = '';
142 $twitter_user['profile_background_tile'] = false;
144 $twitter_user['statuses_count'] = $profile->noticeCount();
146 // Is the requesting user following this user?
147 $twitter_user['following'] = false;
148 $twitter_user['notifications'] = false;
150 if (isset($apidata['user'])) {
152 $twitter_user['following'] = $apidata['user']->isSubscribed($profile);
155 $sub = Subscription::pkeyGet(array('subscriber' =>
156 $apidata['user']->id, 'subscribed' => $profile->id));
159 $twitter_user['notifications'] = ($sub->jabber || $sub->sms);
164 $notice = $profile->getCurrentNotice();
167 $twitter_user['status'] = $this->twitter_status_array($notice, false);
171 return $twitter_user;
174 function twitter_status_array($notice, $include_user=true)
176 $profile = $notice->getProfile();
178 $twitter_status = array();
179 $twitter_status['text'] = $notice->content;
180 $twitter_status['truncated'] = false; # Not possible on StatusNet
181 $twitter_status['created_at'] = $this->date_twitter($notice->created);
182 $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ?
183 intval($notice->reply_to) : null;
184 $twitter_status['source'] = $this->source_link($notice->source);
185 $twitter_status['id'] = intval($notice->id);
187 $replier_profile = null;
189 if ($notice->reply_to) {
190 $reply = Notice::staticGet(intval($notice->reply_to));
192 $replier_profile = $reply->getProfile();
196 $twitter_status['in_reply_to_user_id'] =
197 ($replier_profile) ? intval($replier_profile->id) : null;
198 $twitter_status['in_reply_to_screen_name'] =
199 ($replier_profile) ? $replier_profile->nickname : null;
201 if (isset($this->auth_user)) {
202 $twitter_status['favorited'] = $this->auth_user->hasFave($notice);
204 $twitter_status['favorited'] = false;
208 $attachments = $notice->attachments();
210 if (!empty($attachments)) {
212 $twitter_status['attachments'] = array();
214 foreach ($attachments as $attachment) {
215 if ($attachment->isEnclosure()) {
216 $enclosure = array();
217 $enclosure['url'] = $attachment->url;
218 $enclosure['mimetype'] = $attachment->mimetype;
219 $enclosure['size'] = $attachment->size;
220 $twitter_status['attachments'][] = $enclosure;
226 # Don't get notice (recursive!)
227 $twitter_user = $this->twitter_user_array($profile, false);
228 $twitter_status['user'] = $twitter_user;
231 return $twitter_status;
234 function twitter_group_array($group)
236 $twitter_group=array();
237 $twitter_group['id']=$group->id;
238 $twitter_group['url']=$group->permalink();
239 $twitter_group['nickname']=$group->nickname;
240 $twitter_group['fullname']=$group->fullname;
241 $twitter_group['homepage_url']=$group->homepage_url;
242 $twitter_group['original_logo']=$group->original_logo;
243 $twitter_group['homepage_logo']=$group->homepage_logo;
244 $twitter_group['stream_logo']=$group->stream_logo;
245 $twitter_group['mini_logo']=$group->mini_logo;
246 $twitter_group['homepage']=$group->homepage;
247 $twitter_group['description']=$group->description;
248 $twitter_group['location']=$group->location;
249 $twitter_group['created']=$this->date_twitter($group->created);
250 $twitter_group['modified']=$this->date_twitter($group->modified);
251 return $twitter_group;
254 function twitter_rss_group_array($group)
257 $entry['content']=$group->description;
258 $entry['title']=$group->nickname;
259 $entry['link']=$group->permalink();
260 $entry['published']=common_date_iso8601($group->created);
261 $entry['updated']==common_date_iso8601($group->modified);
262 $taguribase = common_config('integration', 'groupuri');
263 $entry['id'] = "group:$groupuribase:$entry[link]";
265 $entry['description'] = $entry['content'];
266 $entry['pubDate'] = common_date_rfc2822($group->created);
267 $entry['guid'] = $entry['link'];
272 function twitter_rss_entry_array($notice)
274 $profile = $notice->getProfile();
277 // We trim() to avoid extraneous whitespace in the output
279 $entry['content'] = common_xml_safe_str(trim($notice->rendered));
280 $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
281 $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
282 $entry['published'] = common_date_iso8601($notice->created);
284 $taguribase = common_config('integration', 'taguri');
285 $entry['id'] = "tag:$taguribase:$entry[link]";
287 $entry['updated'] = $entry['published'];
288 $entry['author'] = $profile->getBestName();
291 $attachments = $notice->attachments();
292 $enclosures = array();
294 foreach ($attachments as $attachment) {
295 $enclosure_o=$attachment->getEnclosure();
297 $enclosure = array();
298 $enclosure['url'] = $enclosure_o->url;
299 $enclosure['mimetype'] = $enclosure_o->mimetype;
300 $enclosure['size'] = $enclosure_o->size;
301 $enclosures[] = $enclosure;
305 if (!empty($enclosures)) {
306 $entry['enclosures'] = $enclosures;
311 $attachments = $notice->attachments();
313 $entry['enclosures']=array();
314 foreach($attachments as $attachment){
315 if ($attachment->isEnclosure()) {
317 $enclosure['url']=$attachment->url;
318 $enclosure['mimetype']=$attachment->mimetype;
319 $enclosure['size']=$attachment->size;
320 $entry['enclosures'][]=$enclosure;
327 $tag = new Notice_tag();
328 $tag->notice_id = $notice->id;
330 $entry['tags']=array();
331 while ($tag->fetch()) {
332 $entry['tags'][]=$tag->tag;
338 $entry['description'] = $entry['content'];
339 $entry['pubDate'] = common_date_rfc2822($notice->created);
340 $entry['guid'] = $entry['link'];
346 function twitter_relationship_array($source, $target)
348 $relationship = array();
350 $relationship['source'] =
351 $this->relationship_details_array($source, $target);
352 $relationship['target'] =
353 $this->relationship_details_array($target, $source);
355 return array('relationship' => $relationship);
358 function relationship_details_array($source, $target)
362 $details['screen_name'] = $source->nickname;
363 $details['followed_by'] = $target->isSubscribed($source);
364 $details['following'] = $source->isSubscribed($target);
366 $notifications = false;
368 if ($source->isSubscribed($target)) {
370 $sub = Subscription::pkeyGet(array('subscriber' =>
371 $source->id, 'subscribed' => $target->id));
374 $notifications = ($sub->jabber || $sub->sms);
378 $details['notifications_enabled'] = $notifications;
379 $details['blocking'] = $source->hasBlocked($target);
380 $details['id'] = $source->id;
385 function show_twitter_xml_relationship($relationship)
387 $this->elementStart('relationship');
389 foreach($relationship as $element => $value) {
390 if ($element == 'source' || $element == 'target') {
391 $this->elementStart($element);
392 $this->show_xml_relationship_details($value);
393 $this->elementEnd($element);
397 $this->elementEnd('relationship');
400 function show_xml_relationship_details($details)
402 foreach($details as $element => $value) {
403 $this->element($element, null, $value);
407 function show_twitter_xml_status($twitter_status)
409 $this->elementStart('status');
410 foreach($twitter_status as $element => $value) {
413 $this->show_twitter_xml_user($twitter_status['user']);
416 $this->element($element, null, common_xml_safe_str($value));
419 $this->show_xml_attachments($twitter_status['attachments']);
422 $this->element($element, null, $value);
425 $this->elementEnd('status');
428 function show_twitter_xml_group($twitter_group)
430 $this->elementStart('group');
431 foreach($twitter_group as $element => $value) {
432 $this->element($element, null, $value);
434 $this->elementEnd('group');
437 function show_twitter_xml_user($twitter_user, $role='user')
439 $this->elementStart($role);
440 foreach($twitter_user as $element => $value) {
441 if ($element == 'status') {
442 $this->show_twitter_xml_status($twitter_user['status']);
444 $this->element($element, null, $value);
447 $this->elementEnd($role);
450 function show_xml_attachments($attachments) {
451 if (!empty($attachments)) {
452 $this->elementStart('attachments', array('type' => 'array'));
453 foreach ($attachments as $attachment) {
455 $attrs['url'] = $attachment['url'];
456 $attrs['mimetype'] = $attachment['mimetype'];
457 $attrs['size'] = $attachment['size'];
458 $this->element('enclosure', $attrs, '');
460 $this->elementEnd('attachments');
464 function show_twitter_rss_item($entry)
466 $this->elementStart('item');
467 $this->element('title', null, $entry['title']);
468 $this->element('description', null, $entry['description']);
469 $this->element('pubDate', null, $entry['pubDate']);
470 $this->element('guid', null, $entry['guid']);
471 $this->element('link', null, $entry['link']);
473 # RSS only supports 1 enclosure per item
474 if(array_key_exists('enclosures', $entry) and !empty($entry['enclosures'])){
475 $enclosure = $entry['enclosures'][0];
476 $this->element('enclosure', array('url'=>$enclosure['url'],'type'=>$enclosure['mimetype'],'length'=>$enclosure['size']), null);
479 if(array_key_exists('tags', $entry)){
480 foreach($entry['tags'] as $tag){
481 $this->element('category', null,$tag);
485 $this->elementEnd('item');
488 function show_json_objects($objects)
490 print(json_encode($objects));
493 function show_single_xml_status($notice)
495 $this->init_document('xml');
496 $twitter_status = $this->twitter_status_array($notice);
497 $this->show_twitter_xml_status($twitter_status);
498 $this->end_document('xml');
501 function show_single_json_status($notice)
503 $this->init_document('json');
504 $status = $this->twitter_status_array($notice);
505 $this->show_json_objects($status);
506 $this->end_document('json');
510 function show_xml_timeline($notice)
513 $this->init_document('xml');
514 $this->elementStart('statuses', array('type' => 'array'));
516 if (is_array($notice)) {
517 foreach ($notice as $n) {
518 $twitter_status = $this->twitter_status_array($n);
519 $this->show_twitter_xml_status($twitter_status);
522 while ($notice->fetch()) {
523 $twitter_status = $this->twitter_status_array($notice);
524 $this->show_twitter_xml_status($twitter_status);
528 $this->elementEnd('statuses');
529 $this->end_document('xml');
532 function show_rss_timeline($notice, $title, $link, $subtitle, $suplink=null)
535 $this->init_document('rss');
537 $this->element('title', null, $title);
538 $this->element('link', null, $link);
539 if (!is_null($suplink)) {
540 # For FriendFeed's SUP protocol
541 $this->element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
542 'rel' => 'http://api.friendfeed.com/2008/03#sup',
544 'type' => 'application/json'));
546 $this->element('description', null, $subtitle);
547 $this->element('language', null, 'en-us');
548 $this->element('ttl', null, '40');
550 if (is_array($notice)) {
551 foreach ($notice as $n) {
552 $entry = $this->twitter_rss_entry_array($n);
553 $this->show_twitter_rss_item($entry);
556 while ($notice->fetch()) {
557 $entry = $this->twitter_rss_entry_array($notice);
558 $this->show_twitter_rss_item($entry);
562 $this->end_twitter_rss();
565 function show_atom_timeline($notice, $title, $id, $link, $subtitle=null, $suplink=null, $selfuri=null)
568 $this->init_document('atom');
570 $this->element('title', null, $title);
571 $this->element('id', null, $id);
572 $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
574 if (!is_null($suplink)) {
575 # For FriendFeed's SUP protocol
576 $this->element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
578 'type' => 'application/json'));
581 if (!is_null($selfuri)) {
582 $this->element('link', array('href' => $selfuri,
583 'rel' => 'self', 'type' => 'application/atom+xml'), null);
586 $this->element('updated', null, common_date_iso8601('now'));
587 $this->element('subtitle', null, $subtitle);
589 if (is_array($notice)) {
590 foreach ($notice as $n) {
591 $this->raw($n->asAtomEntry());
594 while ($notice->fetch()) {
595 $this->raw($notice->asAtomEntry());
599 $this->end_document('atom');
603 function show_rss_groups($group, $title, $link, $subtitle)
606 $this->init_document('rss');
608 $this->element('title', null, $title);
609 $this->element('link', null, $link);
610 $this->element('description', null, $subtitle);
611 $this->element('language', null, 'en-us');
612 $this->element('ttl', null, '40');
614 if (is_array($group)) {
615 foreach ($group as $g) {
616 $twitter_group = $this->twitter_rss_group_array($g);
617 $this->show_twitter_rss_item($twitter_group);
620 while ($group->fetch()) {
621 $twitter_group = $this->twitter_rss_group_array($group);
622 $this->show_twitter_rss_item($twitter_group);
626 $this->end_twitter_rss();
630 function showTwitterAtomEntry($entry)
632 $this->elementStart('entry');
633 $this->element('title', null, $entry['title']);
634 $this->element('content', array('type' => 'html'), $entry['content']);
635 $this->element('id', null, $entry['id']);
636 $this->element('published', null, $entry['published']);
637 $this->element('updated', null, $entry['updated']);
638 $this->element('link', array('type' => 'text/html',
639 'href' => $entry['link'],
640 'rel' => 'alternate'));
641 $this->element('link', array('type' => $entry['avatar-type'],
642 'href' => $entry['avatar'],
644 $this->elementStart('author');
646 $this->element('name', null, $entry['author-name']);
647 $this->element('uri', null, $entry['author-uri']);
649 $this->elementEnd('author');
650 $this->elementEnd('entry');
653 function showXmlDirectMessage($dm)
655 $this->elementStart('direct_message');
656 foreach($dm as $element => $value) {
660 $this->show_twitter_xml_user($value, $element);
663 $this->element($element, null, common_xml_safe_str($value));
666 $this->element($element, null, $value);
670 $this->elementEnd('direct_message');
673 function directMessageArray($message)
677 $from_profile = $message->getFrom();
678 $to_profile = $message->getTo();
680 $dmsg['id'] = $message->id;
681 $dmsg['sender_id'] = $message->from_profile;
682 $dmsg['text'] = trim($message->content);
683 $dmsg['recipient_id'] = $message->to_profile;
684 $dmsg['created_at'] = $this->date_twitter($message->created);
685 $dmsg['sender_screen_name'] = $from_profile->nickname;
686 $dmsg['recipient_screen_name'] = $to_profile->nickname;
687 $dmsg['sender'] = $this->twitter_user_array($from_profile, false);
688 $dmsg['recipient'] = $this->twitter_user_array($to_profile, false);
693 function rssDirectMessageArray($message)
697 $from = $message->getFrom();
699 $entry['title'] = sprintf('Message from %s to %s',
700 $from->nickname, $message->getTo()->nickname);
702 $entry['content'] = common_xml_safe_str($message->rendered);
703 $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
704 $entry['published'] = common_date_iso8601($message->created);
706 $taguribase = common_config('integration', 'taguri');
708 $entry['id'] = "tag:$taguribase:$entry[link]";
709 $entry['updated'] = $entry['published'];
711 $entry['author-name'] = $from->getBestName();
712 $entry['author-uri'] = $from->homepage;
714 $avatar = $from->getAvatar(AVATAR_STREAM_SIZE);
716 $entry['avatar'] = (!empty($avatar)) ? $avatar->url : Avatar::defaultImage(AVATAR_STREAM_SIZE);
717 $entry['avatar-type'] = (!empty($avatar)) ? $avatar->mediatype : 'image/png';
721 $entry['description'] = $entry['content'];
722 $entry['pubDate'] = common_date_rfc2822($message->created);
723 $entry['guid'] = $entry['link'];
728 function showSingleXmlDirectMessage($message)
730 $this->init_document('xml');
731 $dmsg = $this->directMessageArray($message);
732 $this->showXmlDirectMessage($dmsg);
733 $this->end_document('xml');
736 function showSingleJsonDirectMessage($message)
738 $this->init_document('json');
739 $dmsg = $this->directMessageArray($message);
740 $this->show_json_objects($dmsg);
741 $this->end_document('json');
744 function show_atom_groups($group, $title, $id, $link, $subtitle=null, $selfuri=null)
747 $this->init_document('atom');
749 $this->element('title', null, $title);
750 $this->element('id', null, $id);
751 $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
753 if (!is_null($selfuri)) {
754 $this->element('link', array('href' => $selfuri,
755 'rel' => 'self', 'type' => 'application/atom+xml'), null);
758 $this->element('updated', null, common_date_iso8601('now'));
759 $this->element('subtitle', null, $subtitle);
761 if (is_array($group)) {
762 foreach ($group as $g) {
763 $this->raw($g->asAtomEntry());
766 while ($group->fetch()) {
767 $this->raw($group->asAtomEntry());
771 $this->end_document('atom');
775 function show_json_timeline($notice)
778 $this->init_document('json');
782 if (is_array($notice)) {
783 foreach ($notice as $n) {
784 $twitter_status = $this->twitter_status_array($n);
785 array_push($statuses, $twitter_status);
788 while ($notice->fetch()) {
789 $twitter_status = $this->twitter_status_array($notice);
790 array_push($statuses, $twitter_status);
794 $this->show_json_objects($statuses);
796 $this->end_document('json');
799 function show_json_groups($group)
802 $this->init_document('json');
806 if (is_array($group)) {
807 foreach ($group as $g) {
808 $twitter_group = $this->twitter_group_array($g);
809 array_push($groups, $twitter_group);
812 while ($group->fetch()) {
813 $twitter_group = $this->twitter_group_array($group);
814 array_push($groups, $twitter_group);
818 $this->show_json_objects($groups);
820 $this->end_document('json');
823 function show_xml_groups($group)
826 $this->init_document('xml');
827 $this->elementStart('groups', array('type' => 'array'));
829 if (is_array($group)) {
830 foreach ($group as $g) {
831 $twitter_group = $this->twitter_group_array($g);
832 $this->show_twitter_xml_group($twitter_group);
835 while ($group->fetch()) {
836 $twitter_group = $this->twitter_group_array($group);
837 $this->show_twitter_xml_group($twitter_group);
841 $this->elementEnd('groups');
842 $this->end_document('xml');
845 function show_twitter_xml_users($user)
848 $this->init_document('xml');
849 $this->elementStart('users', array('type' => 'array'));
851 if (is_array($user)) {
852 foreach ($user as $u) {
853 $twitter_user = $this->twitter_user_array($u);
854 $this->show_twitter_xml_user($twitter_user);
857 while ($user->fetch()) {
858 $twitter_user = $this->twitter_user_array($user);
859 $this->show_twitter_xml_user($twitter_user);
863 $this->elementEnd('users');
864 $this->end_document('xml');
867 function show_json_users($user)
870 $this->init_document('json');
874 if (is_array($user)) {
875 foreach ($user as $u) {
876 $twitter_user = $this->twitter_user_array($u);
877 array_push($users, $twitter_user);
880 while ($user->fetch()) {
881 $twitter_user = $this->twitter_user_array($user);
882 array_push($users, $twitter_user);
886 $this->show_json_objects($users);
888 $this->end_document('json');
891 function show_single_json_group($group)
893 $this->init_document('json');
894 $twitter_group = $this->twitter_group_array($group);
895 $this->show_json_objects($twitter_group);
896 $this->end_document('json');
899 function show_single_xml_group($group)
901 $this->init_document('xml');
902 $twitter_group = $this->twitter_group_array($group);
903 $this->show_twitter_xml_group($twitter_group);
904 $this->end_document('xml');
907 function date_twitter($dt)
909 $dateStr = date('d F Y H:i:s', strtotime($dt));
910 $d = new DateTime($dateStr, new DateTimeZone('UTC'));
911 $d->setTimezone(new DateTimeZone(common_timezone()));
912 return $d->format('D M d H:i:s O Y');
915 // XXX: Candidate for a general utility method somewhere?
916 function count_subscriptions($profile)
920 $sub = new Subscription();
921 $sub->subscribed = $profile->id;
923 $count = $sub->find();
932 function init_document($type='xml')
936 header('Content-Type: application/xml; charset=utf-8');
940 header('Content-Type: application/json; charset=utf-8');
942 // Check for JSONP callback
943 $callback = $this->arg('callback');
945 print $callback . '(';
949 header("Content-Type: application/rss+xml; charset=utf-8");
950 $this->init_twitter_rss();
953 header('Content-Type: application/atom+xml; charset=utf-8');
954 $this->init_twitter_atom();
957 $this->clientError(_('Not a supported data format.'));
964 function end_document($type='xml')
972 // Check for JSONP callback
973 $callback = $this->arg('callback');
979 $this->end_twitter_rss();
982 $this->end_twitter_rss();
985 $this->clientError(_('Not a supported data format.'));
991 function clientError($msg, $code = 400, $format = 'xml')
993 $action = $this->trimmed('action');
995 common_debug("User error '$code' on '$action': $msg", __FILE__);
997 if (!array_key_exists($code, ClientErrorAction::$status)) {
1001 $status_string = ClientErrorAction::$status[$code];
1003 header('HTTP/1.1 '.$code.' '.$status_string);
1005 if ($format == 'xml') {
1006 $this->init_document('xml');
1007 $this->elementStart('hash');
1008 $this->element('error', null, $msg);
1009 $this->element('request', null, $_SERVER['REQUEST_URI']);
1010 $this->elementEnd('hash');
1011 $this->end_document('xml');
1012 } elseif ($format == 'json'){
1013 $this->init_document('json');
1014 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
1015 print(json_encode($error_array));
1016 $this->end_document('json');
1019 // If user didn't request a useful format, throw a regular client error
1020 throw new ClientException($msg, $code);
1024 function serverError($msg, $code = 500, $content_type = 'json')
1026 $action = $this->trimmed('action');
1028 common_debug("Server error '$code' on '$action': $msg", __FILE__);
1030 if (!array_key_exists($code, ServerErrorAction::$status)) {
1034 $status_string = ServerErrorAction::$status[$code];
1036 header('HTTP/1.1 '.$code.' '.$status_string);
1038 if ($content_type == 'xml') {
1039 $this->init_document('xml');
1040 $this->elementStart('hash');
1041 $this->element('error', null, $msg);
1042 $this->element('request', null, $_SERVER['REQUEST_URI']);
1043 $this->elementEnd('hash');
1044 $this->end_document('xml');
1046 $this->init_document('json');
1047 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
1048 print(json_encode($error_array));
1049 $this->end_document('json');
1053 function init_twitter_rss()
1056 $this->elementStart('rss', array('version' => '2.0', 'xmlns:atom'=>'http://www.w3.org/2005/Atom'));
1057 $this->elementStart('channel');
1058 Event::handle('StartApiRss', array($this));
1061 function end_twitter_rss()
1063 $this->elementEnd('channel');
1064 $this->elementEnd('rss');
1068 function init_twitter_atom()
1071 // FIXME: don't hardcode the language here!
1072 $this->elementStart('feed', array('xmlns' => 'http://www.w3.org/2005/Atom',
1073 'xml:lang' => 'en-US',
1074 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0'));
1075 Event::handle('StartApiAtom', array($this));
1078 function end_twitter_atom()
1080 $this->elementEnd('feed');
1084 function show_profile($profile, $content_type='xml', $notice=null, $includeStatuses=true)
1086 $profile_array = $this->twitter_user_array($profile, $includeStatuses);
1087 switch ($content_type) {
1089 $this->show_twitter_xml_user($profile_array);
1092 $this->show_json_objects($profile_array);
1095 $this->clientError(_('Not a supported data format.'));
1101 function get_user($id, $apidata=null)
1105 // Twitter supports these other ways of passing the user ID
1106 if (is_numeric($this->arg('id'))) {
1107 return User::staticGet($this->arg('id'));
1108 } else if ($this->arg('id')) {
1109 $nickname = common_canonical_nickname($this->arg('id'));
1110 return User::staticGet('nickname', $nickname);
1111 } else if ($this->arg('user_id')) {
1112 // This is to ensure that a non-numeric user_id still
1113 // overrides screen_name even if it doesn't get used
1114 if (is_numeric($this->arg('user_id'))) {
1115 return User::staticGet('id', $this->arg('user_id'));
1117 } else if ($this->arg('screen_name')) {
1118 $nickname = common_canonical_nickname($this->arg('screen_name'));
1119 return User::staticGet('nickname', $nickname);
1121 // Fall back to trying the currently authenticated user
1122 return $apidata['user'];
1125 } else if (is_numeric($id)) {
1126 return User::staticGet($id);
1128 $nickname = common_canonical_nickname($id);
1129 return User::staticGet('nickname', $nickname);
1133 function getTargetUser($id)
1137 // Twitter supports these other ways of passing the user ID
1138 if (is_numeric($this->arg('id'))) {
1139 return User::staticGet($this->arg('id'));
1140 } else if ($this->arg('id')) {
1141 $nickname = common_canonical_nickname($this->arg('id'));
1142 return User::staticGet('nickname', $nickname);
1143 } else if ($this->arg('user_id')) {
1144 // This is to ensure that a non-numeric user_id still
1145 // overrides screen_name even if it doesn't get used
1146 if (is_numeric($this->arg('user_id'))) {
1147 return User::staticGet('id', $this->arg('user_id'));
1149 } else if ($this->arg('screen_name')) {
1150 $nickname = common_canonical_nickname($this->arg('screen_name'));
1151 return User::staticGet('nickname', $nickname);
1153 // Fall back to trying the currently authenticated user
1154 return $this->auth_user;
1157 } else if (is_numeric($id)) {
1158 return User::staticGet($id);
1160 $nickname = common_canonical_nickname($id);
1161 return User::staticGet('nickname', $nickname);
1165 function getTargetGroup($id)
1168 if (is_numeric($this->arg('id'))) {
1169 return User_group::staticGet($this->arg('id'));
1170 } else if ($this->arg('id')) {
1171 $nickname = common_canonical_nickname($this->arg('id'));
1172 return User_group::staticGet('nickname', $nickname);
1173 } else if ($this->arg('group_id')) {
1174 // This is to ensure that a non-numeric user_id still
1175 // overrides screen_name even if it doesn't get used
1176 if (is_numeric($this->arg('group_id'))) {
1177 return User_group::staticGet('id', $this->arg('group_id'));
1179 } else if ($this->arg('group_name')) {
1180 $nickname = common_canonical_nickname($this->arg('group_name'));
1181 return User_group::staticGet('nickname', $nickname);
1184 } else if (is_numeric($id)) {
1185 return User_group::staticGet($id);
1187 $nickname = common_canonical_nickname($id);
1188 return User_group::staticGet('nickname', $nickname);
1192 function get_profile($id)
1194 if (is_numeric($id)) {
1195 return Profile::staticGet($id);
1197 $user = User::staticGet('nickname', $id);
1199 return $user->getProfile();
1206 function source_link($source)
1208 $source_name = _($source);
1217 $ns = Notice_source::staticGet($source);
1219 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
1223 return $source_name;
1227 * Returns query argument or default value if not found. Certain
1228 * parameters used throughout the API are lightly scrubbed and
1229 * bounds checked. This overrides Action::arg().
1231 * @param string $key requested argument
1232 * @param string $def default value to return if $key is not provided
1236 function arg($key, $def=null)
1239 // XXX: Do even more input validation/scrubbing?
1241 if (array_key_exists($key, $this->args)) {
1244 $page = (int)$this->args['page'];
1245 return ($page < 1) ? 1 : $page;
1247 $count = (int)$this->args['count'];
1250 } elseif ($count > 200) {
1256 $since_id = (int)$this->args['since_id'];
1257 return ($since_id < 1) ? 0 : $since_id;
1259 $max_id = (int)$this->args['max_id'];
1260 return ($max_id < 1) ? 0 : $max_id;
1262 return strtotime($this->args['since']);
1264 return parent::arg($key, $def);