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'];
325 function twitter_rss_dmsg_array($message)
330 $entry['title'] = sprintf('Message from %s to %s',
331 $message->getFrom()->nickname, $message->getTo()->nickname);
333 $entry['content'] = common_xml_safe_str(trim($message->content));
334 $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
335 $entry['published'] = common_date_iso8601($message->created);
337 $taguribase = common_config('integration', 'taguri');
339 $entry['id'] = "tag:$taguribase,:$entry[link]";
340 $entry['updated'] = $entry['published'];
341 $entry['author'] = $message->getFrom()->getBestName();
344 $entry['description'] = $entry['content'];
345 $entry['pubDate'] = common_date_rfc2822($message->created);
346 $entry['guid'] = $entry['link'];
351 function twitter_dmsg_array($message)
353 $twitter_dm = array();
355 $from_profile = $message->getFrom();
356 $to_profile = $message->getTo();
358 $twitter_dm['id'] = $message->id;
359 $twitter_dm['sender_id'] = $message->from_profile;
360 $twitter_dm['text'] = trim($message->content);
361 $twitter_dm['recipient_id'] = $message->to_profile;
362 $twitter_dm['created_at'] = $this->date_twitter($message->created);
363 $twitter_dm['sender_screen_name'] = $from_profile->nickname;
364 $twitter_dm['recipient_screen_name'] = $to_profile->nickname;
365 $twitter_dm['sender'] = $this->twitter_user_array($from_profile, false);
366 $twitter_dm['recipient'] = $this->twitter_user_array($to_profile, false);
371 function twitter_relationship_array($source, $target)
373 $relationship = array();
375 $relationship['source'] =
376 $this->relationship_details_array($source, $target);
377 $relationship['target'] =
378 $this->relationship_details_array($target, $source);
380 return array('relationship' => $relationship);
383 function relationship_details_array($source, $target)
387 $details['screen_name'] = $source->nickname;
388 $details['followed_by'] = $target->isSubscribed($source);
389 $details['following'] = $source->isSubscribed($target);
391 $notifications = false;
393 if ($source->isSubscribed($target)) {
395 $sub = Subscription::pkeyGet(array('subscriber' =>
396 $source->id, 'subscribed' => $target->id));
399 $notifications = ($sub->jabber || $sub->sms);
403 $details['notifications_enabled'] = $notifications;
404 $details['blocking'] = $source->hasBlocked($target);
405 $details['id'] = $source->id;
410 function show_twitter_xml_relationship($relationship)
412 $this->elementStart('relationship');
414 foreach($relationship as $element => $value) {
415 if ($element == 'source' || $element == 'target') {
416 $this->elementStart($element);
417 $this->show_xml_relationship_details($value);
418 $this->elementEnd($element);
422 $this->elementEnd('relationship');
425 function show_xml_relationship_details($details)
427 foreach($details as $element => $value) {
428 $this->element($element, null, $value);
432 function show_twitter_xml_status($twitter_status)
434 $this->elementStart('status');
435 foreach($twitter_status as $element => $value) {
438 $this->show_twitter_xml_user($twitter_status['user']);
441 $this->element($element, null, common_xml_safe_str($value));
444 $this->show_xml_attachments($twitter_status['attachments']);
447 $this->element($element, null, $value);
450 $this->elementEnd('status');
453 function show_twitter_xml_group($twitter_group)
455 $this->elementStart('group');
456 foreach($twitter_group as $element => $value) {
457 $this->element($element, null, $value);
459 $this->elementEnd('group');
462 function show_twitter_xml_user($twitter_user, $role='user')
464 $this->elementStart($role);
465 foreach($twitter_user as $element => $value) {
466 if ($element == 'status') {
467 $this->show_twitter_xml_status($twitter_user['status']);
469 $this->element($element, null, $value);
472 $this->elementEnd($role);
475 function show_xml_attachments($attachments) {
476 if (!empty($attachments)) {
477 $this->elementStart('attachments', array('type' => 'array'));
478 foreach ($attachments as $attachment) {
480 $attrs['url'] = $attachment['url'];
481 $attrs['mimetype'] = $attachment['mimetype'];
482 $attrs['size'] = $attachment['size'];
483 $this->element('enclosure', $attrs, '');
485 $this->elementEnd('attachments');
489 function show_twitter_rss_item($entry)
491 $this->elementStart('item');
492 $this->element('title', null, $entry['title']);
493 $this->element('description', null, $entry['description']);
494 $this->element('pubDate', null, $entry['pubDate']);
495 $this->element('guid', null, $entry['guid']);
496 $this->element('link', null, $entry['link']);
498 # RSS only supports 1 enclosure per item
499 if(array_key_exists('enclosures', $entry) and !empty($entry['enclosures'])){
500 $enclosure = $entry['enclosures'][0];
501 $this->element('enclosure', array('url'=>$enclosure['url'],'type'=>$enclosure['mimetype'],'length'=>$enclosure['size']), null);
504 if(array_key_exists('tags', $entry)){
505 foreach($entry['tags'] as $tag){
506 $this->element('category', null,$tag);
510 $this->elementEnd('item');
513 function show_json_objects($objects)
515 print(json_encode($objects));
518 function show_single_xml_status($notice)
520 $this->init_document('xml');
521 $twitter_status = $this->twitter_status_array($notice);
522 $this->show_twitter_xml_status($twitter_status);
523 $this->end_document('xml');
526 function show_single_json_status($notice)
528 $this->init_document('json');
529 $status = $this->twitter_status_array($notice);
530 $this->show_json_objects($status);
531 $this->end_document('json');
534 function show_single_xml_dmsg($message)
536 $this->init_document('xml');
537 $dmsg = $this->twitter_dmsg_array($message);
538 $this->show_twitter_xml_dmsg($dmsg);
539 $this->end_document('xml');
542 function show_single_json_dmsg($message)
544 $this->init_document('json');
545 $dmsg = $this->twitter_dmsg_array($message);
546 $this->show_json_objects($dmsg);
547 $this->end_document('json');
550 function show_twitter_xml_dmsg($twitter_dm)
552 $this->elementStart('direct_message');
553 foreach($twitter_dm as $element => $value) {
557 $this->show_twitter_xml_user($value, $element);
560 $this->element($element, null, common_xml_safe_str($value));
563 $this->element($element, null, $value);
566 $this->elementEnd('direct_message');
569 function show_xml_timeline($notice)
572 $this->init_document('xml');
573 $this->elementStart('statuses', array('type' => 'array'));
575 if (is_array($notice)) {
576 foreach ($notice as $n) {
577 $twitter_status = $this->twitter_status_array($n);
578 $this->show_twitter_xml_status($twitter_status);
581 while ($notice->fetch()) {
582 $twitter_status = $this->twitter_status_array($notice);
583 $this->show_twitter_xml_status($twitter_status);
587 $this->elementEnd('statuses');
588 $this->end_document('xml');
591 function show_rss_timeline($notice, $title, $link, $subtitle, $suplink=null)
594 $this->init_document('rss');
596 $this->element('title', null, $title);
597 $this->element('link', null, $link);
598 if (!is_null($suplink)) {
599 # For FriendFeed's SUP protocol
600 $this->element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
601 'rel' => 'http://api.friendfeed.com/2008/03#sup',
603 'type' => 'application/json'));
605 $this->element('description', null, $subtitle);
606 $this->element('language', null, 'en-us');
607 $this->element('ttl', null, '40');
609 if (is_array($notice)) {
610 foreach ($notice as $n) {
611 $entry = $this->twitter_rss_entry_array($n);
612 $this->show_twitter_rss_item($entry);
615 while ($notice->fetch()) {
616 $entry = $this->twitter_rss_entry_array($notice);
617 $this->show_twitter_rss_item($entry);
621 $this->end_twitter_rss();
624 function show_atom_timeline($notice, $title, $id, $link, $subtitle=null, $suplink=null, $selfuri=null)
627 $this->init_document('atom');
629 $this->element('title', null, $title);
630 $this->element('id', null, $id);
631 $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
633 if (!is_null($suplink)) {
634 # For FriendFeed's SUP protocol
635 $this->element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
637 'type' => 'application/json'));
640 if (!is_null($selfuri)) {
641 $this->element('link', array('href' => $selfuri,
642 'rel' => 'self', 'type' => 'application/atom+xml'), null);
645 $this->element('updated', null, common_date_iso8601('now'));
646 $this->element('subtitle', null, $subtitle);
648 if (is_array($notice)) {
649 foreach ($notice as $n) {
650 $this->raw($n->asAtomEntry());
653 while ($notice->fetch()) {
654 $this->raw($notice->asAtomEntry());
658 $this->end_document('atom');
662 function show_rss_groups($group, $title, $link, $subtitle)
665 $this->init_document('rss');
667 $this->element('title', null, $title);
668 $this->element('link', null, $link);
669 $this->element('description', null, $subtitle);
670 $this->element('language', null, 'en-us');
671 $this->element('ttl', null, '40');
673 if (is_array($group)) {
674 foreach ($group as $g) {
675 $twitter_group = $this->twitter_rss_group_array($g);
676 $this->show_twitter_rss_item($twitter_group);
679 while ($group->fetch()) {
680 $twitter_group = $this->twitter_rss_group_array($group);
681 $this->show_twitter_rss_item($twitter_group);
685 $this->end_twitter_rss();
688 function show_atom_groups($group, $title, $id, $link, $subtitle=null, $selfuri=null)
691 $this->init_document('atom');
693 $this->element('title', null, $title);
694 $this->element('id', null, $id);
695 $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
697 if (!is_null($selfuri)) {
698 $this->element('link', array('href' => $selfuri,
699 'rel' => 'self', 'type' => 'application/atom+xml'), null);
702 $this->element('updated', null, common_date_iso8601('now'));
703 $this->element('subtitle', null, $subtitle);
705 if (is_array($group)) {
706 foreach ($group as $g) {
707 $this->raw($g->asAtomEntry());
710 while ($group->fetch()) {
711 $this->raw($group->asAtomEntry());
715 $this->end_document('atom');
719 function show_json_timeline($notice)
722 $this->init_document('json');
726 if (is_array($notice)) {
727 foreach ($notice as $n) {
728 $twitter_status = $this->twitter_status_array($n);
729 array_push($statuses, $twitter_status);
732 while ($notice->fetch()) {
733 $twitter_status = $this->twitter_status_array($notice);
734 array_push($statuses, $twitter_status);
738 $this->show_json_objects($statuses);
740 $this->end_document('json');
743 function show_json_groups($group)
746 $this->init_document('json');
750 if (is_array($group)) {
751 foreach ($group as $g) {
752 $twitter_group = $this->twitter_group_array($g);
753 array_push($groups, $twitter_group);
756 while ($group->fetch()) {
757 $twitter_group = $this->twitter_group_array($group);
758 array_push($groups, $twitter_group);
762 $this->show_json_objects($groups);
764 $this->end_document('json');
767 function show_xml_groups($group)
770 $this->init_document('xml');
771 $this->elementStart('groups', array('type' => 'array'));
773 if (is_array($group)) {
774 foreach ($group as $g) {
775 $twitter_group = $this->twitter_group_array($g);
776 $this->show_twitter_xml_group($twitter_group);
779 while ($group->fetch()) {
780 $twitter_group = $this->twitter_group_array($group);
781 $this->show_twitter_xml_group($twitter_group);
785 $this->elementEnd('groups');
786 $this->end_document('xml');
789 function show_twitter_xml_users($user)
792 $this->init_document('xml');
793 $this->elementStart('users', array('type' => 'array'));
795 if (is_array($user)) {
796 foreach ($group as $g) {
797 $twitter_user = $this->twitter_user_array($g);
798 $this->show_twitter_xml_user($twitter_user,'user');
801 while ($user->fetch()) {
802 $twitter_user = $this->twitter_user_array($user);
803 $this->show_twitter_xml_user($twitter_user);
807 $this->elementEnd('users');
808 $this->end_document('xml');
811 function show_json_users($user)
814 $this->init_document('json');
818 if (is_array($user)) {
819 foreach ($user as $u) {
820 $twitter_user = $this->twitter_user_array($u);
821 array_push($users, $twitter_user);
824 while ($user->fetch()) {
825 $twitter_user = $this->twitter_user_array($user);
826 array_push($users, $twitter_user);
830 $this->show_json_objects($users);
832 $this->end_document('json');
835 function show_single_json_group($group)
837 $this->init_document('json');
838 $twitter_group = $this->twitter_group_array($group);
839 $this->show_json_objects($twitter_group);
840 $this->end_document('json');
843 function show_single_xml_group($group)
845 $this->init_document('xml');
846 $twitter_group = $this->twitter_group_array($group);
847 $this->show_twitter_xml_group($twitter_group);
848 $this->end_document('xml');
851 // Anyone know what date format this is?
852 // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
853 function date_twitter($dt)
856 return date("D M d H:i:s O Y", $t);
859 // XXX: Candidate for a general utility method somewhere?
860 function count_subscriptions($profile)
864 $sub = new Subscription();
865 $sub->subscribed = $profile->id;
867 $count = $sub->find();
876 function init_document($type='xml')
880 header('Content-Type: application/xml; charset=utf-8');
884 header('Content-Type: application/json; charset=utf-8');
886 // Check for JSONP callback
887 $callback = $this->arg('callback');
889 print $callback . '(';
893 header("Content-Type: application/rss+xml; charset=utf-8");
894 $this->init_twitter_rss();
897 header('Content-Type: application/atom+xml; charset=utf-8');
898 $this->init_twitter_atom();
901 $this->clientError(_('Not a supported data format.'));
908 function end_document($type='xml')
916 // Check for JSONP callback
917 $callback = $this->arg('callback');
923 $this->end_twitter_rss();
926 $this->end_twitter_rss();
929 $this->clientError(_('Not a supported data format.'));
935 function clientError($msg, $code = 400, $format = 'xml')
937 $action = $this->trimmed('action');
939 common_debug("User error '$code' on '$action': $msg", __FILE__);
941 if (!array_key_exists($code, ClientErrorAction::$status)) {
945 $status_string = ClientErrorAction::$status[$code];
947 header('HTTP/1.1 '.$code.' '.$status_string);
949 if ($format == 'xml') {
950 $this->init_document('xml');
951 $this->elementStart('hash');
952 $this->element('error', null, $msg);
953 $this->element('request', null, $_SERVER['REQUEST_URI']);
954 $this->elementEnd('hash');
955 $this->end_document('xml');
956 } elseif ($format == 'json'){
957 $this->init_document('json');
958 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
959 print(json_encode($error_array));
960 $this->end_document('json');
963 // If user didn't request a useful format, throw a regular client error
964 throw new ClientException($msg, $code);
968 function serverError($msg, $code = 500, $content_type = 'json')
970 $action = $this->trimmed('action');
972 common_debug("Server error '$code' on '$action': $msg", __FILE__);
974 if (!array_key_exists($code, ServerErrorAction::$status)) {
978 $status_string = ServerErrorAction::$status[$code];
980 header('HTTP/1.1 '.$code.' '.$status_string);
982 if ($content_type == 'xml') {
983 $this->init_document('xml');
984 $this->elementStart('hash');
985 $this->element('error', null, $msg);
986 $this->element('request', null, $_SERVER['REQUEST_URI']);
987 $this->elementEnd('hash');
988 $this->end_document('xml');
990 $this->init_document('json');
991 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
992 print(json_encode($error_array));
993 $this->end_document('json');
997 function init_twitter_rss()
1000 $this->elementStart('rss', array('version' => '2.0', 'xmlns:atom'=>'http://www.w3.org/2005/Atom'));
1001 $this->elementStart('channel');
1002 Event::handle('StartApiRss', array($this));
1005 function end_twitter_rss()
1007 $this->elementEnd('channel');
1008 $this->elementEnd('rss');
1012 function init_twitter_atom()
1015 // FIXME: don't hardcode the language here!
1016 $this->elementStart('feed', array('xmlns' => 'http://www.w3.org/2005/Atom',
1017 'xml:lang' => 'en-US',
1018 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0'));
1019 Event::handle('StartApiAtom', array($this));
1022 function end_twitter_atom()
1024 $this->elementEnd('feed');
1028 function show_profile($profile, $content_type='xml', $notice=null, $includeStatuses=true)
1030 $profile_array = $this->twitter_user_array($profile, $includeStatuses);
1031 switch ($content_type) {
1033 $this->show_twitter_xml_user($profile_array);
1036 $this->show_json_objects($profile_array);
1039 $this->clientError(_('Not a supported data format.'));
1045 function get_user($id, $apidata=null)
1049 // Twitter supports these other ways of passing the user ID
1050 if (is_numeric($this->arg('id'))) {
1051 return User::staticGet($this->arg('id'));
1052 } else if ($this->arg('id')) {
1053 $nickname = common_canonical_nickname($this->arg('id'));
1054 return User::staticGet('nickname', $nickname);
1055 } else if ($this->arg('user_id')) {
1056 // This is to ensure that a non-numeric user_id still
1057 // overrides screen_name even if it doesn't get used
1058 if (is_numeric($this->arg('user_id'))) {
1059 return User::staticGet('id', $this->arg('user_id'));
1061 } else if ($this->arg('screen_name')) {
1062 $nickname = common_canonical_nickname($this->arg('screen_name'));
1063 return User::staticGet('nickname', $nickname);
1065 // Fall back to trying the currently authenticated user
1066 return $apidata['user'];
1069 } else if (is_numeric($id)) {
1070 return User::staticGet($id);
1072 $nickname = common_canonical_nickname($id);
1073 return User::staticGet('nickname', $nickname);
1077 function getTargetUser($id)
1081 // Twitter supports these other ways of passing the user ID
1082 if (is_numeric($this->arg('id'))) {
1083 return User::staticGet($this->arg('id'));
1084 } else if ($this->arg('id')) {
1085 $nickname = common_canonical_nickname($this->arg('id'));
1086 return User::staticGet('nickname', $nickname);
1087 } else if ($this->arg('user_id')) {
1088 // This is to ensure that a non-numeric user_id still
1089 // overrides screen_name even if it doesn't get used
1090 if (is_numeric($this->arg('user_id'))) {
1091 return User::staticGet('id', $this->arg('user_id'));
1093 } else if ($this->arg('screen_name')) {
1094 $nickname = common_canonical_nickname($this->arg('screen_name'));
1095 return User::staticGet('nickname', $nickname);
1097 // Fall back to trying the currently authenticated user
1098 return $this->auth_user;
1101 } else if (is_numeric($id)) {
1102 return User::staticGet($id);
1104 $nickname = common_canonical_nickname($id);
1105 return User::staticGet('nickname', $nickname);
1109 function get_group($id, $apidata=null)
1113 if (is_numeric($this->arg('id'))) {
1114 return User_group::staticGet($this->arg('id'));
1115 } else if ($this->arg('id')) {
1116 $nickname = common_canonical_nickname($this->arg('id'));
1117 return User_group::staticGet('nickname', $nickname);
1118 } else if ($this->arg('group_id')) {
1119 // This is to ensure that a non-numeric user_id still
1120 // overrides screen_name even if it doesn't get used
1121 if (is_numeric($this->arg('group_id'))) {
1122 return User_group::staticGet('id', $this->arg('group_id'));
1124 } else if ($this->arg('group_name')) {
1125 $nickname = common_canonical_nickname($this->arg('group_name'));
1126 return User_group::staticGet('nickname', $nickname);
1129 } else if (is_numeric($id)) {
1130 return User_group::staticGet($id);
1132 $nickname = common_canonical_nickname($id);
1133 return User_group::staticGet('nickname', $nickname);
1137 function get_profile($id)
1139 if (is_numeric($id)) {
1140 return Profile::staticGet($id);
1142 $user = User::staticGet('nickname', $id);
1144 return $user->getProfile();
1151 function source_link($source)
1153 $source_name = _($source);
1162 $ns = Notice_source::staticGet($source);
1164 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
1168 return $source_name;
1172 * Returns query argument or default value if not found. Certain
1173 * parameters used throughout the API are lightly scrubbed and
1174 * bounds checked. This overrides Action::arg().
1176 * @param string $key requested argument
1177 * @param string $def default value to return if $key is not provided
1181 function arg($key, $def=null)
1184 // XXX: Do even more input validation/scrubbing?
1186 if (array_key_exists($key, $this->args)) {
1189 $page = (int)$this->args['page'];
1190 return ($page < 1) ? 1 : $page;
1192 $count = (int)$this->args['count'];
1195 } elseif ($count > 200) {
1201 $since_id = (int)$this->args['since_id'];
1202 return ($since_id < 1) ? 0 : $since_id;
1204 $max_id = (int)$this->args['max_id'];
1205 return ($max_id < 1) ? 0 : $max_id;
1207 return strtotime($this->args['since']);
1209 return parent::arg($key, $def);