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
32 * @param array $args Web and URL arguments
34 * @return boolean false if user doesn't exist
37 function prepare($args)
39 parent::prepare($args);
46 * @param array $args Arguments from $_REQUEST
51 function handle($args)
53 parent::handle($args);
57 * Overrides XMLOutputter::element to write booleans as strings (true|false).
58 * See that method's documentation for more info.
60 * @param string $tag Element type or tagname
61 * @param array $attrs Array of element attributes, as
63 * @param string $content string content of the element
67 function element($tag, $attrs=null, $content=null)
69 if (is_bool($content)) {
70 $content = ($content ? 'true' : 'false');
73 return parent::element($tag, $attrs, $content);
76 function twitter_user_array($profile, $get_notice=false)
78 $twitter_user = array();
80 $twitter_user['id'] = intval($profile->id);
81 $twitter_user['name'] = $profile->getBestName();
82 $twitter_user['screen_name'] = $profile->nickname;
83 $twitter_user['location'] = ($profile->location) ? $profile->location : null;
84 $twitter_user['description'] = ($profile->bio) ? $profile->bio : null;
86 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
87 $twitter_user['profile_image_url'] = ($avatar) ? $avatar->displayUrl() :
88 Avatar::defaultImage(AVATAR_STREAM_SIZE);
90 $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
91 $twitter_user['protected'] = false; # not supported by StatusNet yet
92 $twitter_user['followers_count'] = $profile->subscriberCount();
94 // To be supported soon...
95 $twitter_user['profile_background_color'] = '';
96 $twitter_user['profile_text_color'] = '';
97 $twitter_user['profile_link_color'] = '';
98 $twitter_user['profile_sidebar_fill_color'] = '';
99 $twitter_user['profile_sidebar_border_color'] = '';
101 $twitter_user['friends_count'] = $profile->subscriptionCount();
103 $twitter_user['created_at'] = $this->date_twitter($profile->created);
105 $twitter_user['favourites_count'] = $profile->faveCount(); // British spelling!
107 // Need to pull up the user for some of this
108 $user = User::staticGet($profile->id);
112 if ($user->timezone) {
113 $timezone = $user->timezone;
117 $t->setTimezone(new DateTimeZone($timezone));
119 $twitter_user['utc_offset'] = $t->format('Z');
120 $twitter_user['time_zone'] = $timezone;
122 // To be supported some day, perhaps
123 $twitter_user['profile_background_image_url'] = '';
124 $twitter_user['profile_background_tile'] = false;
126 $twitter_user['statuses_count'] = $profile->noticeCount();
128 // Is the requesting user following this user?
129 $twitter_user['following'] = false;
130 $twitter_user['notifications'] = false;
132 if (isset($apidata['user'])) {
134 $twitter_user['following'] = $apidata['user']->isSubscribed($profile);
137 $sub = Subscription::pkeyGet(array('subscriber' =>
138 $apidata['user']->id, 'subscribed' => $profile->id));
141 $twitter_user['notifications'] = ($sub->jabber || $sub->sms);
146 $notice = $profile->getCurrentNotice();
149 $twitter_user['status'] = $this->twitter_status_array($notice, false);
153 return $twitter_user;
156 function twitter_status_array($notice, $include_user=true)
158 $profile = $notice->getProfile();
160 $twitter_status = array();
161 $twitter_status['text'] = $notice->content;
162 $twitter_status['truncated'] = false; # Not possible on StatusNet
163 $twitter_status['created_at'] = $this->date_twitter($notice->created);
164 $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ?
165 intval($notice->reply_to) : null;
166 $twitter_status['source'] = $this->source_link($notice->source);
167 $twitter_status['id'] = intval($notice->id);
169 $replier_profile = null;
171 if ($notice->reply_to) {
172 $reply = Notice::staticGet(intval($notice->reply_to));
174 $replier_profile = $reply->getProfile();
178 $twitter_status['in_reply_to_user_id'] =
179 ($replier_profile) ? intval($replier_profile->id) : null;
180 $twitter_status['in_reply_to_screen_name'] =
181 ($replier_profile) ? $replier_profile->nickname : null;
183 if (isset($this->auth_user)) {
184 $twitter_status['favorited'] = $this->auth_user->hasFave($notice);
186 $twitter_status['favorited'] = false;
190 $attachments = $notice->attachments();
192 if (!empty($attachments)) {
194 $twitter_status['attachments'] = array();
196 foreach ($attachments as $attachment) {
197 if ($attachment->isEnclosure()) {
198 $enclosure = array();
199 $enclosure['url'] = $attachment->url;
200 $enclosure['mimetype'] = $attachment->mimetype;
201 $enclosure['size'] = $attachment->size;
202 $twitter_status['attachments'][] = $enclosure;
208 # Don't get notice (recursive!)
209 $twitter_user = $this->twitter_user_array($profile, false);
210 $twitter_status['user'] = $twitter_user;
213 return $twitter_status;
216 function twitter_group_array($group)
218 $twitter_group=array();
219 $twitter_group['id']=$group->id;
220 $twitter_group['url']=$group->permalink();
221 $twitter_group['nickname']=$group->nickname;
222 $twitter_group['fullname']=$group->fullname;
223 $twitter_group['homepage_url']=$group->homepage_url;
224 $twitter_group['original_logo']=$group->original_logo;
225 $twitter_group['homepage_logo']=$group->homepage_logo;
226 $twitter_group['stream_logo']=$group->stream_logo;
227 $twitter_group['mini_logo']=$group->mini_logo;
228 $twitter_group['homepage']=$group->homepage;
229 $twitter_group['description']=$group->description;
230 $twitter_group['location']=$group->location;
231 $twitter_group['created']=$this->date_twitter($group->created);
232 $twitter_group['modified']=$this->date_twitter($group->modified);
233 return $twitter_group;
236 function twitter_rss_group_array($group)
239 $entry['content']=$group->description;
240 $entry['title']=$group->nickname;
241 $entry['link']=$group->permalink();
242 $entry['published']=common_date_iso8601($group->created);
243 $entry['updated']==common_date_iso8601($group->modified);
244 $taguribase = common_config('integration', 'groupuri');
245 $entry['id'] = "group:$groupuribase:$entry[link]";
247 $entry['description'] = $entry['content'];
248 $entry['pubDate'] = common_date_rfc2822($group->created);
249 $entry['guid'] = $entry['link'];
254 function twitter_rss_entry_array($notice)
256 $profile = $notice->getProfile();
259 // We trim() to avoid extraneous whitespace in the output
261 $entry['content'] = common_xml_safe_str(trim($notice->rendered));
262 $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
263 $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
264 $entry['published'] = common_date_iso8601($notice->created);
266 $taguribase = common_config('integration', 'taguri');
267 $entry['id'] = "tag:$taguribase:$entry[link]";
269 $entry['updated'] = $entry['published'];
270 $entry['author'] = $profile->getBestName();
273 $attachments = $notice->attachments();
274 $enclosures = array();
276 foreach ($attachments as $attachment) {
277 $enclosure_o=$attachment->getEnclosure();
279 $enclosure = array();
280 $enclosure['url'] = $enclosure_o->url;
281 $enclosure['mimetype'] = $enclosure_o->mimetype;
282 $enclosure['size'] = $enclosure_o->size;
283 $enclosures[] = $enclosure;
287 if (!empty($enclosures)) {
288 $entry['enclosures'] = $enclosures;
293 $attachments = $notice->attachments();
295 $entry['enclosures']=array();
296 foreach($attachments as $attachment){
297 if ($attachment->isEnclosure()) {
299 $enclosure['url']=$attachment->url;
300 $enclosure['mimetype']=$attachment->mimetype;
301 $enclosure['size']=$attachment->size;
302 $entry['enclosures'][]=$enclosure;
309 $tag = new Notice_tag();
310 $tag->notice_id = $notice->id;
312 $entry['tags']=array();
313 while ($tag->fetch()) {
314 $entry['tags'][]=$tag->tag;
320 $entry['description'] = $entry['content'];
321 $entry['pubDate'] = common_date_rfc2822($notice->created);
322 $entry['guid'] = $entry['link'];
327 function twitter_rss_dmsg_array($message)
332 $entry['title'] = sprintf('Message from %s to %s',
333 $message->getFrom()->nickname, $message->getTo()->nickname);
335 $entry['content'] = common_xml_safe_str(trim($message->content));
336 $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
337 $entry['published'] = common_date_iso8601($message->created);
339 $taguribase = common_config('integration', 'taguri');
341 $entry['id'] = "tag:$taguribase,:$entry[link]";
342 $entry['updated'] = $entry['published'];
343 $entry['author'] = $message->getFrom()->getBestName();
346 $entry['description'] = $entry['content'];
347 $entry['pubDate'] = common_date_rfc2822($message->created);
348 $entry['guid'] = $entry['link'];
353 function twitter_dmsg_array($message)
355 $twitter_dm = array();
357 $from_profile = $message->getFrom();
358 $to_profile = $message->getTo();
360 $twitter_dm['id'] = $message->id;
361 $twitter_dm['sender_id'] = $message->from_profile;
362 $twitter_dm['text'] = trim($message->content);
363 $twitter_dm['recipient_id'] = $message->to_profile;
364 $twitter_dm['created_at'] = $this->date_twitter($message->created);
365 $twitter_dm['sender_screen_name'] = $from_profile->nickname;
366 $twitter_dm['recipient_screen_name'] = $to_profile->nickname;
367 $twitter_dm['sender'] = $this->twitter_user_array($from_profile, false);
368 $twitter_dm['recipient'] = $this->twitter_user_array($to_profile, false);
373 function twitter_relationship_array($source, $target)
375 $relationship = array();
377 $relationship['source'] =
378 $this->relationship_details_array($source, $target);
379 $relationship['target'] =
380 $this->relationship_details_array($target, $source);
382 return array('relationship' => $relationship);
385 function relationship_details_array($source, $target)
389 $details['screen_name'] = $source->nickname;
390 $details['followed_by'] = $target->isSubscribed($source);
391 $details['following'] = $source->isSubscribed($target);
393 $notifications = false;
395 if ($source->isSubscribed($target)) {
397 $sub = Subscription::pkeyGet(array('subscriber' =>
398 $source->id, 'subscribed' => $target->id));
401 $notifications = ($sub->jabber || $sub->sms);
405 $details['notifications_enabled'] = $notifications;
406 $details['blocking'] = $source->hasBlocked($target);
407 $details['id'] = $source->id;
412 function show_twitter_xml_relationship($relationship)
414 $this->elementStart('relationship');
416 foreach($relationship as $element => $value) {
417 if ($element == 'source' || $element == 'target') {
418 $this->elementStart($element);
419 $this->show_xml_relationship_details($value);
420 $this->elementEnd($element);
424 $this->elementEnd('relationship');
427 function show_xml_relationship_details($details)
429 foreach($details as $element => $value) {
430 $this->element($element, null, $value);
434 function show_twitter_xml_status($twitter_status)
436 $this->elementStart('status');
437 foreach($twitter_status as $element => $value) {
440 $this->show_twitter_xml_user($twitter_status['user']);
443 $this->element($element, null, common_xml_safe_str($value));
446 $this->show_xml_attachments($twitter_status['attachments']);
449 $this->element($element, null, $value);
452 $this->elementEnd('status');
455 function show_twitter_xml_group($twitter_group)
457 $this->elementStart('group');
458 foreach($twitter_group as $element => $value) {
459 $this->element($element, null, $value);
461 $this->elementEnd('group');
464 function show_twitter_xml_user($twitter_user, $role='user')
466 $this->elementStart($role);
467 foreach($twitter_user as $element => $value) {
468 if ($element == 'status') {
469 $this->show_twitter_xml_status($twitter_user['status']);
471 $this->element($element, null, $value);
474 $this->elementEnd($role);
477 function show_xml_attachments($attachments) {
478 if (!empty($attachments)) {
479 $this->elementStart('attachments', array('type' => 'array'));
480 foreach ($attachments as $attachment) {
482 $attrs['url'] = $attachment['url'];
483 $attrs['mimetype'] = $attachment['mimetype'];
484 $attrs['size'] = $attachment['size'];
485 $this->element('enclosure', $attrs, '');
487 $this->elementEnd('attachments');
491 function show_twitter_rss_item($entry)
493 $this->elementStart('item');
494 $this->element('title', null, $entry['title']);
495 $this->element('description', null, $entry['description']);
496 $this->element('pubDate', null, $entry['pubDate']);
497 $this->element('guid', null, $entry['guid']);
498 $this->element('link', null, $entry['link']);
500 # RSS only supports 1 enclosure per item
501 if(array_key_exists('enclosures', $entry) and !empty($entry['enclosures'])){
502 $enclosure = $entry['enclosures'][0];
503 $this->element('enclosure', array('url'=>$enclosure['url'],'type'=>$enclosure['mimetype'],'length'=>$enclosure['size']), null);
506 if(array_key_exists('tags', $entry)){
507 foreach($entry['tags'] as $tag){
508 $this->element('category', null,$tag);
512 $this->elementEnd('item');
515 function show_json_objects($objects)
517 print(json_encode($objects));
520 function show_single_xml_status($notice)
522 $this->init_document('xml');
523 $twitter_status = $this->twitter_status_array($notice);
524 $this->show_twitter_xml_status($twitter_status);
525 $this->end_document('xml');
528 function show_single_json_status($notice)
530 $this->init_document('json');
531 $status = $this->twitter_status_array($notice);
532 $this->show_json_objects($status);
533 $this->end_document('json');
536 function show_single_xml_dmsg($message)
538 $this->init_document('xml');
539 $dmsg = $this->twitter_dmsg_array($message);
540 $this->show_twitter_xml_dmsg($dmsg);
541 $this->end_document('xml');
544 function show_single_json_dmsg($message)
546 $this->init_document('json');
547 $dmsg = $this->twitter_dmsg_array($message);
548 $this->show_json_objects($dmsg);
549 $this->end_document('json');
552 function show_twitter_xml_dmsg($twitter_dm)
554 $this->elementStart('direct_message');
555 foreach($twitter_dm as $element => $value) {
559 $this->show_twitter_xml_user($value, $element);
562 $this->element($element, null, common_xml_safe_str($value));
565 $this->element($element, null, $value);
568 $this->elementEnd('direct_message');
571 function show_xml_timeline($notice)
574 $this->init_document('xml');
575 $this->elementStart('statuses', array('type' => 'array'));
577 if (is_array($notice)) {
578 foreach ($notice as $n) {
579 $twitter_status = $this->twitter_status_array($n);
580 $this->show_twitter_xml_status($twitter_status);
583 while ($notice->fetch()) {
584 $twitter_status = $this->twitter_status_array($notice);
585 $this->show_twitter_xml_status($twitter_status);
589 $this->elementEnd('statuses');
590 $this->end_document('xml');
593 function show_rss_timeline($notice, $title, $link, $subtitle, $suplink=null)
596 $this->init_document('rss');
598 $this->element('title', null, $title);
599 $this->element('link', null, $link);
600 if (!is_null($suplink)) {
601 # For FriendFeed's SUP protocol
602 $this->element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
603 'rel' => 'http://api.friendfeed.com/2008/03#sup',
605 'type' => 'application/json'));
607 $this->element('description', null, $subtitle);
608 $this->element('language', null, 'en-us');
609 $this->element('ttl', null, '40');
611 if (is_array($notice)) {
612 foreach ($notice as $n) {
613 $entry = $this->twitter_rss_entry_array($n);
614 $this->show_twitter_rss_item($entry);
617 while ($notice->fetch()) {
618 $entry = $this->twitter_rss_entry_array($notice);
619 $this->show_twitter_rss_item($entry);
623 $this->end_twitter_rss();
626 function show_atom_timeline($notice, $title, $id, $link, $subtitle=null, $suplink=null, $selfuri=null)
629 $this->init_document('atom');
631 $this->element('title', null, $title);
632 $this->element('id', null, $id);
633 $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
635 if (!is_null($suplink)) {
636 # For FriendFeed's SUP protocol
637 $this->element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
639 'type' => 'application/json'));
642 if (!is_null($selfuri)) {
643 $this->element('link', array('href' => $selfuri,
644 'rel' => 'self', 'type' => 'application/atom+xml'), null);
647 $this->element('updated', null, common_date_iso8601('now'));
648 $this->element('subtitle', null, $subtitle);
650 if (is_array($notice)) {
651 foreach ($notice as $n) {
652 $this->raw($n->asAtomEntry());
655 while ($notice->fetch()) {
656 $this->raw($notice->asAtomEntry());
660 $this->end_document('atom');
664 function show_rss_groups($group, $title, $link, $subtitle)
667 $this->init_document('rss');
669 $this->element('title', null, $title);
670 $this->element('link', null, $link);
671 $this->element('description', null, $subtitle);
672 $this->element('language', null, 'en-us');
673 $this->element('ttl', null, '40');
675 if (is_array($group)) {
676 foreach ($group as $g) {
677 $twitter_group = $this->twitter_rss_group_array($g);
678 $this->show_twitter_rss_item($twitter_group);
681 while ($group->fetch()) {
682 $twitter_group = $this->twitter_rss_group_array($group);
683 $this->show_twitter_rss_item($twitter_group);
687 $this->end_twitter_rss();
690 function show_atom_groups($group, $title, $id, $link, $subtitle=null, $selfuri=null)
693 $this->init_document('atom');
695 $this->element('title', null, $title);
696 $this->element('id', null, $id);
697 $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
699 if (!is_null($selfuri)) {
700 $this->element('link', array('href' => $selfuri,
701 'rel' => 'self', 'type' => 'application/atom+xml'), null);
704 $this->element('updated', null, common_date_iso8601('now'));
705 $this->element('subtitle', null, $subtitle);
707 if (is_array($group)) {
708 foreach ($group as $g) {
709 $this->raw($g->asAtomEntry());
712 while ($group->fetch()) {
713 $this->raw($group->asAtomEntry());
717 $this->end_document('atom');
721 function show_json_timeline($notice)
724 $this->init_document('json');
728 if (is_array($notice)) {
729 foreach ($notice as $n) {
730 $twitter_status = $this->twitter_status_array($n);
731 array_push($statuses, $twitter_status);
734 while ($notice->fetch()) {
735 $twitter_status = $this->twitter_status_array($notice);
736 array_push($statuses, $twitter_status);
740 $this->show_json_objects($statuses);
742 $this->end_document('json');
745 function show_json_groups($group)
748 $this->init_document('json');
752 if (is_array($group)) {
753 foreach ($group as $g) {
754 $twitter_group = $this->twitter_group_array($g);
755 array_push($groups, $twitter_group);
758 while ($group->fetch()) {
759 $twitter_group = $this->twitter_group_array($group);
760 array_push($groups, $twitter_group);
764 $this->show_json_objects($groups);
766 $this->end_document('json');
769 function show_xml_groups($group)
772 $this->init_document('xml');
773 $this->elementStart('groups', array('type' => 'array'));
775 if (is_array($group)) {
776 foreach ($group as $g) {
777 $twitter_group = $this->twitter_group_array($g);
778 $this->show_twitter_xml_group($twitter_group);
781 while ($group->fetch()) {
782 $twitter_group = $this->twitter_group_array($group);
783 $this->show_twitter_xml_group($twitter_group);
787 $this->elementEnd('groups');
788 $this->end_document('xml');
791 function show_twitter_xml_users($user)
794 $this->init_document('xml');
795 $this->elementStart('users', array('type' => 'array'));
797 if (is_array($user)) {
798 foreach ($group as $g) {
799 $twitter_user = $this->twitter_user_array($g);
800 $this->show_twitter_xml_user($twitter_user,'user');
803 while ($user->fetch()) {
804 $twitter_user = $this->twitter_user_array($user);
805 $this->show_twitter_xml_user($twitter_user);
809 $this->elementEnd('users');
810 $this->end_document('xml');
813 function show_json_users($user)
816 $this->init_document('json');
820 if (is_array($user)) {
821 foreach ($user as $u) {
822 $twitter_user = $this->twitter_user_array($u);
823 array_push($users, $twitter_user);
826 while ($user->fetch()) {
827 $twitter_user = $this->twitter_user_array($user);
828 array_push($users, $twitter_user);
832 $this->show_json_objects($users);
834 $this->end_document('json');
837 function show_single_json_group($group)
839 $this->init_document('json');
840 $twitter_group = $this->twitter_group_array($group);
841 $this->show_json_objects($twitter_group);
842 $this->end_document('json');
845 function show_single_xml_group($group)
847 $this->init_document('xml');
848 $twitter_group = $this->twitter_group_array($group);
849 $this->show_twitter_xml_group($twitter_group);
850 $this->end_document('xml');
853 // Anyone know what date format this is?
854 // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
855 function date_twitter($dt)
858 return date("D M d H:i:s O Y", $t);
861 // XXX: Candidate for a general utility method somewhere?
862 function count_subscriptions($profile)
866 $sub = new Subscription();
867 $sub->subscribed = $profile->id;
869 $count = $sub->find();
878 function init_document($type='xml')
882 header('Content-Type: application/xml; charset=utf-8');
886 header('Content-Type: application/json; charset=utf-8');
888 // Check for JSONP callback
889 $callback = $this->arg('callback');
891 print $callback . '(';
895 header("Content-Type: application/rss+xml; charset=utf-8");
896 $this->init_twitter_rss();
899 header('Content-Type: application/atom+xml; charset=utf-8');
900 $this->init_twitter_atom();
903 $this->clientError(_('Not a supported data format.'));
910 function end_document($type='xml')
918 // Check for JSONP callback
919 $callback = $this->arg('callback');
925 $this->end_twitter_rss();
928 $this->end_twitter_rss();
931 $this->clientError(_('Not a supported data format.'));
937 function clientError($msg, $code = 400, $format = 'xml')
940 static $status = array(400 => 'Bad Request',
941 401 => 'Unauthorized',
942 402 => 'Payment Required',
945 405 => 'Method Not Allowed',
946 406 => 'Not Acceptable',
947 407 => 'Proxy Authentication Required',
948 408 => 'Request Timeout',
951 411 => 'Length Required',
952 412 => 'Precondition Failed',
953 413 => 'Request Entity Too Large',
954 414 => 'Request-URI Too Long',
955 415 => 'Unsupported Media Type',
956 416 => 'Requested Range Not Satisfiable',
957 417 => 'Expectation Failed');
959 $action = $this->trimmed('action');
961 common_debug("User error '$code' on '$action': $msg", __FILE__);
963 if (!array_key_exists($code, $status)) {
967 $status_string = $status[$code];
968 header('HTTP/1.1 '.$code.' '.$status_string);
970 if ($format == 'xml') {
971 $this->init_document('xml');
972 $this->elementStart('hash');
973 $this->element('error', null, $msg);
974 $this->element('request', null, $_SERVER['REQUEST_URI']);
975 $this->elementEnd('hash');
976 $this->end_document('xml');
977 } elseif ($format == 'json'){
978 $this->init_document('json');
979 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
980 print(json_encode($error_array));
981 $this->end_document('json');
984 // If user didn't request a useful format, throw a regular client error
985 throw new ClientException($msg, $code);
989 function init_twitter_rss()
992 $this->elementStart('rss', array('version' => '2.0', 'xmlns:atom'=>'http://www.w3.org/2005/Atom'));
993 $this->elementStart('channel');
994 Event::handle('StartApiRss', array($this));
997 function end_twitter_rss()
999 $this->elementEnd('channel');
1000 $this->elementEnd('rss');
1004 function init_twitter_atom()
1007 // FIXME: don't hardcode the language here!
1008 $this->elementStart('feed', array('xmlns' => 'http://www.w3.org/2005/Atom',
1009 'xml:lang' => 'en-US',
1010 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0'));
1011 Event::handle('StartApiAtom', array($this));
1014 function end_twitter_atom()
1016 $this->elementEnd('feed');
1020 function show_profile($profile, $content_type='xml', $notice=null, $includeStatuses=true)
1022 $profile_array = $this->twitter_user_array($profile, $includeStatuses);
1023 switch ($content_type) {
1025 $this->show_twitter_xml_user($profile_array);
1028 $this->show_json_objects($profile_array);
1031 $this->clientError(_('Not a supported data format.'));
1037 function get_user($id, $apidata=null)
1041 // Twitter supports these other ways of passing the user ID
1042 if (is_numeric($this->arg('id'))) {
1043 return User::staticGet($this->arg('id'));
1044 } else if ($this->arg('id')) {
1045 $nickname = common_canonical_nickname($this->arg('id'));
1046 return User::staticGet('nickname', $nickname);
1047 } else if ($this->arg('user_id')) {
1048 // This is to ensure that a non-numeric user_id still
1049 // overrides screen_name even if it doesn't get used
1050 if (is_numeric($this->arg('user_id'))) {
1051 return User::staticGet('id', $this->arg('user_id'));
1053 } else if ($this->arg('screen_name')) {
1054 $nickname = common_canonical_nickname($this->arg('screen_name'));
1055 return User::staticGet('nickname', $nickname);
1057 // Fall back to trying the currently authenticated user
1058 return $apidata['user'];
1061 } else if (is_numeric($id)) {
1062 return User::staticGet($id);
1064 $nickname = common_canonical_nickname($id);
1065 return User::staticGet('nickname', $nickname);
1069 function getTargetUser($id)
1073 // Twitter supports these other ways of passing the user ID
1074 if (is_numeric($this->arg('id'))) {
1075 return User::staticGet($this->arg('id'));
1076 } else if ($this->arg('id')) {
1077 $nickname = common_canonical_nickname($this->arg('id'));
1078 return User::staticGet('nickname', $nickname);
1079 } else if ($this->arg('user_id')) {
1080 // This is to ensure that a non-numeric user_id still
1081 // overrides screen_name even if it doesn't get used
1082 if (is_numeric($this->arg('user_id'))) {
1083 return User::staticGet('id', $this->arg('user_id'));
1085 } else if ($this->arg('screen_name')) {
1086 $nickname = common_canonical_nickname($this->arg('screen_name'));
1087 return User::staticGet('nickname', $nickname);
1089 // Fall back to trying the currently authenticated user
1090 return $this->auth_user;
1093 } else if (is_numeric($id)) {
1094 return User::staticGet($id);
1096 $nickname = common_canonical_nickname($id);
1097 return User::staticGet('nickname', $nickname);
1101 function get_group($id, $apidata=null)
1105 if (is_numeric($this->arg('id'))) {
1106 return User_group::staticGet($this->arg('id'));
1107 } else if ($this->arg('id')) {
1108 $nickname = common_canonical_nickname($this->arg('id'));
1109 return User_group::staticGet('nickname', $nickname);
1110 } else if ($this->arg('group_id')) {
1111 // This is to ensure that a non-numeric user_id still
1112 // overrides screen_name even if it doesn't get used
1113 if (is_numeric($this->arg('group_id'))) {
1114 return User_group::staticGet('id', $this->arg('group_id'));
1116 } else if ($this->arg('group_name')) {
1117 $nickname = common_canonical_nickname($this->arg('group_name'));
1118 return User_group::staticGet('nickname', $nickname);
1121 } else if (is_numeric($id)) {
1122 return User_group::staticGet($id);
1124 $nickname = common_canonical_nickname($id);
1125 return User_group::staticGet('nickname', $nickname);
1129 function get_profile($id)
1131 if (is_numeric($id)) {
1132 return Profile::staticGet($id);
1134 $user = User::staticGet('nickname', $id);
1136 return $user->getProfile();
1143 function source_link($source)
1145 $source_name = _($source);
1154 $ns = Notice_source::staticGet($source);
1156 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
1160 return $source_name;
1164 * Returns query argument or default value if not found. Certain
1165 * parameters used throughout the API are lightly scrubbed and
1166 * bounds checked. This overrides Action::arg().
1168 * @param string $key requested argument
1169 * @param string $def default value to return if $key is not provided
1173 function arg($key, $def=null)
1176 // XXX: Do even more input validation/scrubbing?
1178 if (array_key_exists($key, $this->args)) {
1181 $page = (int)$this->args['page'];
1182 return ($page < 1) ? 1 : $page;
1184 $count = (int)$this->args['count'];
1187 } elseif ($count > 200) {
1193 $since_id = (int)$this->args['since_id'];
1194 return ($since_id < 1) ? 0 : $since_id;
1196 $max_id = (int)$this->args['max_id'];
1197 return ($max_id < 1) ? 0 : $max_id;
1199 return strtotime($this->args['since']);
1201 return parent::arg($key, $def);
1208 function checkBasicAuthUser()
1210 $this->basicAuthProcessHeader();
1212 if (!isset($this->auth_user)) {
1213 header('WWW-Authenticate: Basic realm="StatusNet API"');
1215 // show error if the user clicks 'cancel'
1217 $this->showBasicAuthError();
1221 $nickname = $this->auth_user;
1222 $password = $this->auth_pw;
1223 $this->auth_user = common_check_user($nickname, $password);
1225 if (empty($this->auth_user)) {
1227 // basic authentication failed
1229 list($proxy, $ip) = common_client_ip();
1230 common_log(LOG_WARNING,
1231 "Failed API auth attempt, nickname = $nickname, proxy = $proxy, ip = $ip.");
1232 $this->showBasicAuthError();
1239 function basicAuthProcessHeader()
1241 if (isset($_SERVER['AUTHORIZATION']) || isset($_SERVER['HTTP_AUTHORIZATION'])) {
1242 $authorization_header = isset($_SERVER['HTTP_AUTHORIZATION'])? $_SERVER['HTTP_AUTHORIZATION'] : $_SERVER['AUTHORIZATION'];
1245 if (isset($_SERVER['PHP_AUTH_USER'])) {
1246 $this->auth_user = $_SERVER['PHP_AUTH_USER'];
1247 $this->auth_pw = $_SERVER['PHP_AUTH_PW'];
1248 } elseif (isset($authorization_header) && strstr(substr($authorization_header, 0, 5), 'Basic')) {
1249 // decode the HTTP_AUTHORIZATION header on php-cgi server self
1250 // on fcgid server the header name is AUTHORIZATION
1252 $auth_hash = base64_decode(substr($authorization_header, 6));
1253 list($this->auth_user, $this->auth_pw) = explode(':', $auth_hash);
1255 // set all to null on a empty basic auth request
1256 if ($this->auth_user == "") {
1257 $this->auth_user = null;
1258 $this->auth_pw = null;
1261 $this->auth_user = null;
1262 $this->auth_pw = null;
1266 function showBasicAuthError()
1268 header('HTTP/1.1 401 Unauthorized');
1269 $msg = 'Could not authenticate you.';
1271 if ($this->arg('format') == 'xml') {
1272 header('Content-Type: application/xml; charset=utf-8');
1274 $this->elementStart('hash');
1275 $this->element('error', null, $msg);
1276 $this->element('request', null, $_SERVER['REQUEST_URI']);
1277 $this->elementEnd('hash');
1279 } elseif ($this->arg('format') == 'json') {
1280 header('Content-Type: application/json; charset=utf-8');
1281 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
1282 print(json_encode($error_array));
1284 header('Content-type: text/plain');