3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) {
24 class TwitterapiAction extends Action
30 * @param array $args Web and URL arguments
32 * @return boolean false if user doesn't exist
35 function prepare($args)
37 parent::prepare($args);
44 * @param array $args Arguments from $_REQUEST
49 function handle($args)
51 parent::handle($args);
55 * Overrides XMLOutputter::element to write booleans as strings (true|false).
56 * See that method's documentation for more info.
58 * @param string $tag Element type or tagname
59 * @param array $attrs Array of element attributes, as
61 * @param string $content string content of the element
65 function element($tag, $attrs=null, $content=null)
67 if (is_bool($content)) {
68 $content = ($content ? 'true' : 'false');
71 return parent::element($tag, $attrs, $content);
74 function twitter_user_array($profile, $get_notice=false)
76 $twitter_user = array();
78 $twitter_user['id'] = intval($profile->id);
79 $twitter_user['name'] = $profile->getBestName();
80 $twitter_user['screen_name'] = $profile->nickname;
81 $twitter_user['location'] = ($profile->location) ? $profile->location : null;
82 $twitter_user['description'] = ($profile->bio) ? $profile->bio : null;
84 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
85 $twitter_user['profile_image_url'] = ($avatar) ? $avatar->displayUrl() :
86 Avatar::defaultImage(AVATAR_STREAM_SIZE);
88 $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
89 $twitter_user['protected'] = false; # not supported by StatusNet yet
90 $twitter_user['followers_count'] = $profile->subscriberCount();
92 // To be supported soon...
93 $twitter_user['profile_background_color'] = '';
94 $twitter_user['profile_text_color'] = '';
95 $twitter_user['profile_link_color'] = '';
96 $twitter_user['profile_sidebar_fill_color'] = '';
97 $twitter_user['profile_sidebar_border_color'] = '';
99 $twitter_user['friends_count'] = $profile->subscriptionCount();
101 $twitter_user['created_at'] = $this->date_twitter($profile->created);
103 $twitter_user['favourites_count'] = $profile->faveCount(); // British spelling!
105 // Need to pull up the user for some of this
106 $user = User::staticGet($profile->id);
110 if ($user->timezone) {
111 $timezone = $user->timezone;
115 $t->setTimezone(new DateTimeZone($timezone));
117 $twitter_user['utc_offset'] = $t->format('Z');
118 $twitter_user['time_zone'] = $timezone;
120 // To be supported some day, perhaps
121 $twitter_user['profile_background_image_url'] = '';
122 $twitter_user['profile_background_tile'] = false;
124 $twitter_user['statuses_count'] = $profile->noticeCount();
126 // Is the requesting user following this user?
127 $twitter_user['following'] = false;
128 $twitter_user['notifications'] = false;
130 if (isset($apidata['user'])) {
132 $twitter_user['following'] = $apidata['user']->isSubscribed($profile);
135 $sub = Subscription::pkeyGet(array('subscriber' =>
136 $apidata['user']->id, 'subscribed' => $profile->id));
139 $twitter_user['notifications'] = ($sub->jabber || $sub->sms);
144 $notice = $profile->getCurrentNotice();
147 $twitter_user['status'] = $this->twitter_status_array($notice, false);
151 return $twitter_user;
154 function twitter_status_array($notice, $include_user=true)
156 $profile = $notice->getProfile();
158 $twitter_status = array();
159 $twitter_status['text'] = $notice->content;
160 $twitter_status['truncated'] = false; # Not possible on StatusNet
161 $twitter_status['created_at'] = $this->date_twitter($notice->created);
162 $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ?
163 intval($notice->reply_to) : null;
164 $twitter_status['source'] = $this->source_link($notice->source);
165 $twitter_status['id'] = intval($notice->id);
167 $replier_profile = null;
169 if ($notice->reply_to) {
170 $reply = Notice::staticGet(intval($notice->reply_to));
172 $replier_profile = $reply->getProfile();
176 $twitter_status['in_reply_to_user_id'] =
177 ($replier_profile) ? intval($replier_profile->id) : null;
178 $twitter_status['in_reply_to_screen_name'] =
179 ($replier_profile) ? $replier_profile->nickname : null;
181 if (isset($this->auth_user)) {
182 $twitter_status['favorited'] = $this->auth_user->hasFave($notice);
184 $twitter_status['favorited'] = false;
188 $attachments = $notice->attachments();
190 if (!empty($attachments)) {
192 $twitter_status['attachments'] = array();
194 foreach ($attachments as $attachment) {
195 if ($attachment->isEnclosure()) {
196 $enclosure = array();
197 $enclosure['url'] = $attachment->url;
198 $enclosure['mimetype'] = $attachment->mimetype;
199 $enclosure['size'] = $attachment->size;
200 $twitter_status['attachments'][] = $enclosure;
206 # Don't get notice (recursive!)
207 $twitter_user = $this->twitter_user_array($profile, false);
208 $twitter_status['user'] = $twitter_user;
211 return $twitter_status;
214 function twitter_group_array($group)
216 $twitter_group=array();
217 $twitter_group['id']=$group->id;
218 $twitter_group['url']=$group->permalink();
219 $twitter_group['nickname']=$group->nickname;
220 $twitter_group['fullname']=$group->fullname;
221 $twitter_group['homepage_url']=$group->homepage_url;
222 $twitter_group['original_logo']=$group->original_logo;
223 $twitter_group['homepage_logo']=$group->homepage_logo;
224 $twitter_group['stream_logo']=$group->stream_logo;
225 $twitter_group['mini_logo']=$group->mini_logo;
226 $twitter_group['homepage']=$group->homepage;
227 $twitter_group['description']=$group->description;
228 $twitter_group['location']=$group->location;
229 $twitter_group['created']=$this->date_twitter($group->created);
230 $twitter_group['modified']=$this->date_twitter($group->modified);
231 return $twitter_group;
234 function twitter_rss_group_array($group)
237 $entry['content']=$group->description;
238 $entry['title']=$group->nickname;
239 $entry['link']=$group->permalink();
240 $entry['published']=common_date_iso8601($group->created);
241 $entry['updated']==common_date_iso8601($group->modified);
242 $taguribase = common_config('integration', 'groupuri');
243 $entry['id'] = "group:$groupuribase:$entry[link]";
245 $entry['description'] = $entry['content'];
246 $entry['pubDate'] = common_date_rfc2822($group->created);
247 $entry['guid'] = $entry['link'];
252 function twitter_rss_entry_array($notice)
254 $profile = $notice->getProfile();
257 // We trim() to avoid extraneous whitespace in the output
259 $entry['content'] = common_xml_safe_str(trim($notice->rendered));
260 $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
261 $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
262 $entry['published'] = common_date_iso8601($notice->created);
264 $taguribase = common_config('integration', 'taguri');
265 $entry['id'] = "tag:$taguribase:$entry[link]";
267 $entry['updated'] = $entry['published'];
268 $entry['author'] = $profile->getBestName();
271 $attachments = $notice->attachments();
272 $enclosures = array();
274 foreach ($attachments as $attachment) {
275 $enclosure_o=$attachment->getEnclosure();
277 $enclosure = array();
278 $enclosure['url'] = $enclosure_o->url;
279 $enclosure['mimetype'] = $enclosure_o->mimetype;
280 $enclosure['size'] = $enclosure_o->size;
281 $enclosures[] = $enclosure;
285 if (!empty($enclosures)) {
286 $entry['enclosures'] = $enclosures;
291 $attachments = $notice->attachments();
293 $entry['enclosures']=array();
294 foreach($attachments as $attachment){
295 if ($attachment->isEnclosure()) {
297 $enclosure['url']=$attachment->url;
298 $enclosure['mimetype']=$attachment->mimetype;
299 $enclosure['size']=$attachment->size;
300 $entry['enclosures'][]=$enclosure;
307 $tag = new Notice_tag();
308 $tag->notice_id = $notice->id;
310 $entry['tags']=array();
311 while ($tag->fetch()) {
312 $entry['tags'][]=$tag->tag;
318 $entry['description'] = $entry['content'];
319 $entry['pubDate'] = common_date_rfc2822($notice->created);
320 $entry['guid'] = $entry['link'];
326 function twitter_relationship_array($source, $target)
328 $relationship = array();
330 $relationship['source'] =
331 $this->relationship_details_array($source, $target);
332 $relationship['target'] =
333 $this->relationship_details_array($target, $source);
335 return array('relationship' => $relationship);
338 function relationship_details_array($source, $target)
342 $details['screen_name'] = $source->nickname;
343 $details['followed_by'] = $target->isSubscribed($source);
344 $details['following'] = $source->isSubscribed($target);
346 $notifications = false;
348 if ($source->isSubscribed($target)) {
350 $sub = Subscription::pkeyGet(array('subscriber' =>
351 $source->id, 'subscribed' => $target->id));
354 $notifications = ($sub->jabber || $sub->sms);
358 $details['notifications_enabled'] = $notifications;
359 $details['blocking'] = $source->hasBlocked($target);
360 $details['id'] = $source->id;
365 function show_twitter_xml_relationship($relationship)
367 $this->elementStart('relationship');
369 foreach($relationship as $element => $value) {
370 if ($element == 'source' || $element == 'target') {
371 $this->elementStart($element);
372 $this->show_xml_relationship_details($value);
373 $this->elementEnd($element);
377 $this->elementEnd('relationship');
380 function show_xml_relationship_details($details)
382 foreach($details as $element => $value) {
383 $this->element($element, null, $value);
387 function show_twitter_xml_status($twitter_status)
389 $this->elementStart('status');
390 foreach($twitter_status as $element => $value) {
393 $this->show_twitter_xml_user($twitter_status['user']);
396 $this->element($element, null, common_xml_safe_str($value));
399 $this->show_xml_attachments($twitter_status['attachments']);
402 $this->element($element, null, $value);
405 $this->elementEnd('status');
408 function show_twitter_xml_group($twitter_group)
410 $this->elementStart('group');
411 foreach($twitter_group as $element => $value) {
412 $this->element($element, null, $value);
414 $this->elementEnd('group');
417 function show_twitter_xml_user($twitter_user, $role='user')
419 $this->elementStart($role);
420 foreach($twitter_user as $element => $value) {
421 if ($element == 'status') {
422 $this->show_twitter_xml_status($twitter_user['status']);
424 $this->element($element, null, $value);
427 $this->elementEnd($role);
430 function show_xml_attachments($attachments) {
431 if (!empty($attachments)) {
432 $this->elementStart('attachments', array('type' => 'array'));
433 foreach ($attachments as $attachment) {
435 $attrs['url'] = $attachment['url'];
436 $attrs['mimetype'] = $attachment['mimetype'];
437 $attrs['size'] = $attachment['size'];
438 $this->element('enclosure', $attrs, '');
440 $this->elementEnd('attachments');
444 function show_twitter_rss_item($entry)
446 $this->elementStart('item');
447 $this->element('title', null, $entry['title']);
448 $this->element('description', null, $entry['description']);
449 $this->element('pubDate', null, $entry['pubDate']);
450 $this->element('guid', null, $entry['guid']);
451 $this->element('link', null, $entry['link']);
453 # RSS only supports 1 enclosure per item
454 if(array_key_exists('enclosures', $entry) and !empty($entry['enclosures'])){
455 $enclosure = $entry['enclosures'][0];
456 $this->element('enclosure', array('url'=>$enclosure['url'],'type'=>$enclosure['mimetype'],'length'=>$enclosure['size']), null);
459 if(array_key_exists('tags', $entry)){
460 foreach($entry['tags'] as $tag){
461 $this->element('category', null,$tag);
465 $this->elementEnd('item');
468 function show_json_objects($objects)
470 print(json_encode($objects));
473 function show_single_xml_status($notice)
475 $this->init_document('xml');
476 $twitter_status = $this->twitter_status_array($notice);
477 $this->show_twitter_xml_status($twitter_status);
478 $this->end_document('xml');
481 function show_single_json_status($notice)
483 $this->init_document('json');
484 $status = $this->twitter_status_array($notice);
485 $this->show_json_objects($status);
486 $this->end_document('json');
490 function show_xml_timeline($notice)
493 $this->init_document('xml');
494 $this->elementStart('statuses', array('type' => 'array'));
496 if (is_array($notice)) {
497 foreach ($notice as $n) {
498 $twitter_status = $this->twitter_status_array($n);
499 $this->show_twitter_xml_status($twitter_status);
502 while ($notice->fetch()) {
503 $twitter_status = $this->twitter_status_array($notice);
504 $this->show_twitter_xml_status($twitter_status);
508 $this->elementEnd('statuses');
509 $this->end_document('xml');
512 function show_rss_timeline($notice, $title, $link, $subtitle, $suplink=null)
515 $this->init_document('rss');
517 $this->element('title', null, $title);
518 $this->element('link', null, $link);
519 if (!is_null($suplink)) {
520 # For FriendFeed's SUP protocol
521 $this->element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
522 'rel' => 'http://api.friendfeed.com/2008/03#sup',
524 'type' => 'application/json'));
526 $this->element('description', null, $subtitle);
527 $this->element('language', null, 'en-us');
528 $this->element('ttl', null, '40');
530 if (is_array($notice)) {
531 foreach ($notice as $n) {
532 $entry = $this->twitter_rss_entry_array($n);
533 $this->show_twitter_rss_item($entry);
536 while ($notice->fetch()) {
537 $entry = $this->twitter_rss_entry_array($notice);
538 $this->show_twitter_rss_item($entry);
542 $this->end_twitter_rss();
545 function show_atom_timeline($notice, $title, $id, $link, $subtitle=null, $suplink=null, $selfuri=null)
548 $this->init_document('atom');
550 $this->element('title', null, $title);
551 $this->element('id', null, $id);
552 $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
554 if (!is_null($suplink)) {
555 # For FriendFeed's SUP protocol
556 $this->element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
558 'type' => 'application/json'));
561 if (!is_null($selfuri)) {
562 $this->element('link', array('href' => $selfuri,
563 'rel' => 'self', 'type' => 'application/atom+xml'), null);
566 $this->element('updated', null, common_date_iso8601('now'));
567 $this->element('subtitle', null, $subtitle);
569 if (is_array($notice)) {
570 foreach ($notice as $n) {
571 $this->raw($n->asAtomEntry());
574 while ($notice->fetch()) {
575 $this->raw($notice->asAtomEntry());
579 $this->end_document('atom');
583 function show_rss_groups($group, $title, $link, $subtitle)
586 $this->init_document('rss');
588 $this->element('title', null, $title);
589 $this->element('link', null, $link);
590 $this->element('description', null, $subtitle);
591 $this->element('language', null, 'en-us');
592 $this->element('ttl', null, '40');
594 if (is_array($group)) {
595 foreach ($group as $g) {
596 $twitter_group = $this->twitter_rss_group_array($g);
597 $this->show_twitter_rss_item($twitter_group);
600 while ($group->fetch()) {
601 $twitter_group = $this->twitter_rss_group_array($group);
602 $this->show_twitter_rss_item($twitter_group);
606 $this->end_twitter_rss();
610 function showTwitterAtomEntry($entry)
612 $this->elementStart('entry');
613 $this->element('title', null, $entry['title']);
614 $this->element('content', array('type' => 'html'), $entry['content']);
615 $this->element('id', null, $entry['id']);
616 $this->element('published', null, $entry['published']);
617 $this->element('updated', null, $entry['updated']);
618 $this->element('link', array('type' => 'text/html',
619 'href' => $entry['link'],
620 'rel' => 'alternate'));
621 $this->element('link', array('type' => $entry['avatar-type'],
622 'href' => $entry['avatar'],
624 $this->elementStart('author');
626 $this->element('name', null, $entry['author-name']);
627 $this->element('uri', null, $entry['author-uri']);
629 $this->elementEnd('author');
630 $this->elementEnd('entry');
633 function showXmlDirectMessage($dm)
635 $this->elementStart('direct_message');
636 foreach($dm as $element => $value) {
640 $this->show_twitter_xml_user($value, $element);
643 $this->element($element, null, common_xml_safe_str($value));
646 $this->element($element, null, $value);
650 $this->elementEnd('direct_message');
653 function directMessageArray($message)
657 $from_profile = $message->getFrom();
658 $to_profile = $message->getTo();
660 $dmsg['id'] = $message->id;
661 $dmsg['sender_id'] = $message->from_profile;
662 $dmsg['text'] = trim($message->content);
663 $dmsg['recipient_id'] = $message->to_profile;
664 $dmsg['created_at'] = $this->date_twitter($message->created);
665 $dmsg['sender_screen_name'] = $from_profile->nickname;
666 $dmsg['recipient_screen_name'] = $to_profile->nickname;
667 $dmsg['sender'] = $this->twitter_user_array($from_profile, false);
668 $dmsg['recipient'] = $this->twitter_user_array($to_profile, false);
673 function rssDirectMessageArray($message)
677 $from = $message->getFrom();
679 $entry['title'] = sprintf('Message from %s to %s',
680 $from->nickname, $message->getTo()->nickname);
682 $entry['content'] = common_xml_safe_str($message->rendered);
683 $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
684 $entry['published'] = common_date_iso8601($message->created);
686 $taguribase = common_config('integration', 'taguri');
688 $entry['id'] = "tag:$taguribase:$entry[link]";
689 $entry['updated'] = $entry['published'];
691 $entry['author-name'] = $from->getBestName();
692 $entry['author-uri'] = $from->homepage;
694 $avatar = $from->getAvatar(AVATAR_STREAM_SIZE);
696 $entry['avatar'] = (!empty($avatar)) ? $avatar->url : Avatar::defaultImage(AVATAR_STREAM_SIZE);
697 $entry['avatar-type'] = (!empty($avatar)) ? $avatar->mediatype : 'image/png';
701 $entry['description'] = $entry['content'];
702 $entry['pubDate'] = common_date_rfc2822($message->created);
703 $entry['guid'] = $entry['link'];
708 function showSingleXmlDirectMessage($message)
710 $this->init_document('xml');
711 $dmsg = $this->directMessageArray($message);
712 $this->showXmlDirectMessage($dmsg);
713 $this->end_document('xml');
716 function showSingleJsonDirectMessage($message)
718 $this->init_document('json');
719 $dmsg = $this->directMessageArray($message);
720 $this->show_json_objects($dmsg);
721 $this->end_document('json');
724 function show_atom_groups($group, $title, $id, $link, $subtitle=null, $selfuri=null)
727 $this->init_document('atom');
729 $this->element('title', null, $title);
730 $this->element('id', null, $id);
731 $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
733 if (!is_null($selfuri)) {
734 $this->element('link', array('href' => $selfuri,
735 'rel' => 'self', 'type' => 'application/atom+xml'), null);
738 $this->element('updated', null, common_date_iso8601('now'));
739 $this->element('subtitle', null, $subtitle);
741 if (is_array($group)) {
742 foreach ($group as $g) {
743 $this->raw($g->asAtomEntry());
746 while ($group->fetch()) {
747 $this->raw($group->asAtomEntry());
751 $this->end_document('atom');
755 function show_json_timeline($notice)
758 $this->init_document('json');
762 if (is_array($notice)) {
763 foreach ($notice as $n) {
764 $twitter_status = $this->twitter_status_array($n);
765 array_push($statuses, $twitter_status);
768 while ($notice->fetch()) {
769 $twitter_status = $this->twitter_status_array($notice);
770 array_push($statuses, $twitter_status);
774 $this->show_json_objects($statuses);
776 $this->end_document('json');
779 function show_json_groups($group)
782 $this->init_document('json');
786 if (is_array($group)) {
787 foreach ($group as $g) {
788 $twitter_group = $this->twitter_group_array($g);
789 array_push($groups, $twitter_group);
792 while ($group->fetch()) {
793 $twitter_group = $this->twitter_group_array($group);
794 array_push($groups, $twitter_group);
798 $this->show_json_objects($groups);
800 $this->end_document('json');
803 function show_xml_groups($group)
806 $this->init_document('xml');
807 $this->elementStart('groups', array('type' => 'array'));
809 if (is_array($group)) {
810 foreach ($group as $g) {
811 $twitter_group = $this->twitter_group_array($g);
812 $this->show_twitter_xml_group($twitter_group);
815 while ($group->fetch()) {
816 $twitter_group = $this->twitter_group_array($group);
817 $this->show_twitter_xml_group($twitter_group);
821 $this->elementEnd('groups');
822 $this->end_document('xml');
825 function show_twitter_xml_users($user)
828 $this->init_document('xml');
829 $this->elementStart('users', array('type' => 'array'));
831 if (is_array($user)) {
832 foreach ($user as $u) {
833 $twitter_user = $this->twitter_user_array($u);
834 $this->show_twitter_xml_user($twitter_user);
837 while ($user->fetch()) {
838 $twitter_user = $this->twitter_user_array($user);
839 $this->show_twitter_xml_user($twitter_user);
843 $this->elementEnd('users');
844 $this->end_document('xml');
847 function show_json_users($user)
850 $this->init_document('json');
854 if (is_array($user)) {
855 foreach ($user as $u) {
856 $twitter_user = $this->twitter_user_array($u);
857 array_push($users, $twitter_user);
860 while ($user->fetch()) {
861 $twitter_user = $this->twitter_user_array($user);
862 array_push($users, $twitter_user);
866 $this->show_json_objects($users);
868 $this->end_document('json');
871 function show_single_json_group($group)
873 $this->init_document('json');
874 $twitter_group = $this->twitter_group_array($group);
875 $this->show_json_objects($twitter_group);
876 $this->end_document('json');
879 function show_single_xml_group($group)
881 $this->init_document('xml');
882 $twitter_group = $this->twitter_group_array($group);
883 $this->show_twitter_xml_group($twitter_group);
884 $this->end_document('xml');
887 function date_twitter($dt)
889 $dateStr = date('d F Y H:i:s', strtotime($dt));
890 $d = new DateTime($dateStr, new DateTimeZone('UTC'));
891 $d->setTimezone(new DateTimeZone(common_timezone()));
892 return $d->format('D M d H:i:s O Y');
895 // XXX: Candidate for a general utility method somewhere?
896 function count_subscriptions($profile)
900 $sub = new Subscription();
901 $sub->subscribed = $profile->id;
903 $count = $sub->find();
912 function init_document($type='xml')
916 header('Content-Type: application/xml; charset=utf-8');
920 header('Content-Type: application/json; charset=utf-8');
922 // Check for JSONP callback
923 $callback = $this->arg('callback');
925 print $callback . '(';
929 header("Content-Type: application/rss+xml; charset=utf-8");
930 $this->init_twitter_rss();
933 header('Content-Type: application/atom+xml; charset=utf-8');
934 $this->init_twitter_atom();
937 $this->clientError(_('Not a supported data format.'));
944 function end_document($type='xml')
952 // Check for JSONP callback
953 $callback = $this->arg('callback');
959 $this->end_twitter_rss();
962 $this->end_twitter_rss();
965 $this->clientError(_('Not a supported data format.'));
971 function clientError($msg, $code = 400, $format = 'xml')
973 $action = $this->trimmed('action');
975 common_debug("User error '$code' on '$action': $msg", __FILE__);
977 if (!array_key_exists($code, ClientErrorAction::$status)) {
981 $status_string = ClientErrorAction::$status[$code];
983 header('HTTP/1.1 '.$code.' '.$status_string);
985 if ($format == 'xml') {
986 $this->init_document('xml');
987 $this->elementStart('hash');
988 $this->element('error', null, $msg);
989 $this->element('request', null, $_SERVER['REQUEST_URI']);
990 $this->elementEnd('hash');
991 $this->end_document('xml');
992 } elseif ($format == 'json'){
993 $this->init_document('json');
994 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
995 print(json_encode($error_array));
996 $this->end_document('json');
999 // If user didn't request a useful format, throw a regular client error
1000 throw new ClientException($msg, $code);
1004 function serverError($msg, $code = 500, $content_type = 'json')
1006 $action = $this->trimmed('action');
1008 common_debug("Server error '$code' on '$action': $msg", __FILE__);
1010 if (!array_key_exists($code, ServerErrorAction::$status)) {
1014 $status_string = ServerErrorAction::$status[$code];
1016 header('HTTP/1.1 '.$code.' '.$status_string);
1018 if ($content_type == 'xml') {
1019 $this->init_document('xml');
1020 $this->elementStart('hash');
1021 $this->element('error', null, $msg);
1022 $this->element('request', null, $_SERVER['REQUEST_URI']);
1023 $this->elementEnd('hash');
1024 $this->end_document('xml');
1026 $this->init_document('json');
1027 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
1028 print(json_encode($error_array));
1029 $this->end_document('json');
1033 function init_twitter_rss()
1036 $this->elementStart('rss', array('version' => '2.0', 'xmlns:atom'=>'http://www.w3.org/2005/Atom'));
1037 $this->elementStart('channel');
1038 Event::handle('StartApiRss', array($this));
1041 function end_twitter_rss()
1043 $this->elementEnd('channel');
1044 $this->elementEnd('rss');
1048 function init_twitter_atom()
1051 // FIXME: don't hardcode the language here!
1052 $this->elementStart('feed', array('xmlns' => 'http://www.w3.org/2005/Atom',
1053 'xml:lang' => 'en-US',
1054 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0'));
1055 Event::handle('StartApiAtom', array($this));
1058 function end_twitter_atom()
1060 $this->elementEnd('feed');
1064 function show_profile($profile, $content_type='xml', $notice=null, $includeStatuses=true)
1066 $profile_array = $this->twitter_user_array($profile, $includeStatuses);
1067 switch ($content_type) {
1069 $this->show_twitter_xml_user($profile_array);
1072 $this->show_json_objects($profile_array);
1075 $this->clientError(_('Not a supported data format.'));
1081 function get_user($id, $apidata=null)
1085 // Twitter supports these other ways of passing the user ID
1086 if (is_numeric($this->arg('id'))) {
1087 return User::staticGet($this->arg('id'));
1088 } else if ($this->arg('id')) {
1089 $nickname = common_canonical_nickname($this->arg('id'));
1090 return User::staticGet('nickname', $nickname);
1091 } else if ($this->arg('user_id')) {
1092 // This is to ensure that a non-numeric user_id still
1093 // overrides screen_name even if it doesn't get used
1094 if (is_numeric($this->arg('user_id'))) {
1095 return User::staticGet('id', $this->arg('user_id'));
1097 } else if ($this->arg('screen_name')) {
1098 $nickname = common_canonical_nickname($this->arg('screen_name'));
1099 return User::staticGet('nickname', $nickname);
1101 // Fall back to trying the currently authenticated user
1102 return $apidata['user'];
1105 } else if (is_numeric($id)) {
1106 return User::staticGet($id);
1108 $nickname = common_canonical_nickname($id);
1109 return User::staticGet('nickname', $nickname);
1113 function getTargetUser($id)
1117 // Twitter supports these other ways of passing the user ID
1118 if (is_numeric($this->arg('id'))) {
1119 return User::staticGet($this->arg('id'));
1120 } else if ($this->arg('id')) {
1121 $nickname = common_canonical_nickname($this->arg('id'));
1122 return User::staticGet('nickname', $nickname);
1123 } else if ($this->arg('user_id')) {
1124 // This is to ensure that a non-numeric user_id still
1125 // overrides screen_name even if it doesn't get used
1126 if (is_numeric($this->arg('user_id'))) {
1127 return User::staticGet('id', $this->arg('user_id'));
1129 } else if ($this->arg('screen_name')) {
1130 $nickname = common_canonical_nickname($this->arg('screen_name'));
1131 return User::staticGet('nickname', $nickname);
1133 // Fall back to trying the currently authenticated user
1134 return $this->auth_user;
1137 } else if (is_numeric($id)) {
1138 return User::staticGet($id);
1140 $nickname = common_canonical_nickname($id);
1141 return User::staticGet('nickname', $nickname);
1145 function getTargetGroup($id)
1148 if (is_numeric($this->arg('id'))) {
1149 return User_group::staticGet($this->arg('id'));
1150 } else if ($this->arg('id')) {
1151 $nickname = common_canonical_nickname($this->arg('id'));
1152 return User_group::staticGet('nickname', $nickname);
1153 } else if ($this->arg('group_id')) {
1154 // This is to ensure that a non-numeric user_id still
1155 // overrides screen_name even if it doesn't get used
1156 if (is_numeric($this->arg('group_id'))) {
1157 return User_group::staticGet('id', $this->arg('group_id'));
1159 } else if ($this->arg('group_name')) {
1160 $nickname = common_canonical_nickname($this->arg('group_name'));
1161 return User_group::staticGet('nickname', $nickname);
1164 } else if (is_numeric($id)) {
1165 return User_group::staticGet($id);
1167 $nickname = common_canonical_nickname($id);
1168 return User_group::staticGet('nickname', $nickname);
1172 function get_profile($id)
1174 if (is_numeric($id)) {
1175 return Profile::staticGet($id);
1177 $user = User::staticGet('nickname', $id);
1179 return $user->getProfile();
1186 function source_link($source)
1188 $source_name = _($source);
1197 $ns = Notice_source::staticGet($source);
1199 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
1203 return $source_name;
1207 * Returns query argument or default value if not found. Certain
1208 * parameters used throughout the API are lightly scrubbed and
1209 * bounds checked. This overrides Action::arg().
1211 * @param string $key requested argument
1212 * @param string $def default value to return if $key is not provided
1216 function arg($key, $def=null)
1219 // XXX: Do even more input validation/scrubbing?
1221 if (array_key_exists($key, $this->args)) {
1224 $page = (int)$this->args['page'];
1225 return ($page < 1) ? 1 : $page;
1227 $count = (int)$this->args['count'];
1230 } elseif ($count > 200) {
1236 $since_id = (int)$this->args['since_id'];
1237 return ($since_id < 1) ? 0 : $since_id;
1239 $max_id = (int)$this->args['max_id'];
1240 return ($max_id < 1) ? 0 : $max_id;
1242 return strtotime($this->args['since']);
1244 return parent::arg($key, $def);