]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/twitterapi.php
Merge commit 'br3nda/0.8.x-hashtags' into 0.8.x
[quix0rs-gnu-social.git] / lib / twitterapi.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, Control Yourself, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) {
21     exit(1);
22 }
23
24 class TwitterapiAction extends Action
25 {
26
27     var $auth_user;
28
29     /**
30      * Initialization.
31      *
32      * @param array $args Web and URL arguments
33      *
34      * @return boolean false if user doesn't exist
35      */
36
37     function prepare($args)
38     {
39         parent::prepare($args);
40         return true;
41     }
42
43     /**
44      * Handle a request
45      *
46      * @param array $args Arguments from $_REQUEST
47      *
48      * @return void
49      */
50
51     function handle($args)
52     {
53         parent::handle($args);
54     }
55
56     /**
57      * Overrides XMLOutputter::element to write booleans as strings (true|false).
58      * See that method's documentation for more info.
59      *
60      * @param string $tag     Element type or tagname
61      * @param array  $attrs   Array of element attributes, as
62      *                        key-value pairs
63      * @param string $content string content of the element
64      *
65      * @return void
66      */
67     function element($tag, $attrs=null, $content=null)
68     {
69         if (is_bool($content)) {
70             $content = ($content ? 'true' : 'false');
71         }
72
73         return parent::element($tag, $attrs, $content);
74     }
75
76     function twitter_user_array($profile, $get_notice=false)
77     {
78         $twitter_user = array();
79
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;
85
86         $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
87         $twitter_user['profile_image_url'] = ($avatar) ? $avatar->displayUrl() :
88             Avatar::defaultImage(AVATAR_STREAM_SIZE);
89
90         $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
91         $twitter_user['protected'] = false; # not supported by Laconica yet
92         $twitter_user['followers_count'] = $profile->subscriberCount();
93
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'] = '';
100
101         $twitter_user['friends_count'] = $profile->subscriptionCount();
102
103         $twitter_user['created_at'] = $this->date_twitter($profile->created);
104
105         $twitter_user['favourites_count'] = $profile->faveCount(); // British spelling!
106
107         // Need to pull up the user for some of this
108         $user = User::staticGet($profile->id);
109
110         $timezone = 'UTC';
111
112         if ($user->timezone) {
113             $timezone = $user->timezone;
114         }
115
116         $t = new DateTime;
117         $t->setTimezone(new DateTimeZone($timezone));
118
119         $twitter_user['utc_offset'] = $t->format('Z');
120         $twitter_user['time_zone'] = $timezone;
121
122         // To be supported some day, perhaps
123         $twitter_user['profile_background_image_url'] = '';
124         $twitter_user['profile_background_tile'] = false;
125
126         $twitter_user['statuses_count'] = $profile->noticeCount();
127
128         // Is the requesting user following this user?
129         $twitter_user['following'] = false;
130         $twitter_user['notifications'] = false;
131
132         if (isset($apidata['user'])) {
133
134             $twitter_user['following'] = $apidata['user']->isSubscribed($profile);
135
136             // Notifications on?
137             $sub = Subscription::pkeyGet(array('subscriber' =>
138                 $apidata['user']->id, 'subscribed' => $profile->id));
139
140             if ($sub) {
141                 $twitter_user['notifications'] = ($sub->jabber || $sub->sms);
142             }
143         }
144
145         if ($get_notice) {
146             $notice = $profile->getCurrentNotice();
147             if ($notice) {
148                 # don't get user!
149                 $twitter_user['status'] = $this->twitter_status_array($notice, false);
150             }
151         }
152
153         return $twitter_user;
154     }
155
156     function twitter_status_array($notice, $include_user=true)
157     {
158         $profile = $notice->getProfile();
159
160         $twitter_status = array();
161         $twitter_status['text'] = $notice->content;
162         $twitter_status['truncated'] = false; # Not possible on Laconica
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);
168
169         $replier_profile = null;
170
171         if ($notice->reply_to) {
172             $reply = Notice::staticGet(intval($notice->reply_to));
173             if ($reply) {
174                 $replier_profile = $reply->getProfile();
175             }
176         }
177
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;
182
183         if (isset($this->auth_user)) {
184             $twitter_status['favorited'] = $this->auth_user->hasFave($notice);
185         } else {
186             $twitter_status['favorited'] = false;
187         }
188
189         // Enclosures
190         $attachments = $notice->attachments();
191         $enclosures = array();
192
193         foreach ($attachments as $attachment) {
194             if ($attachment->isEnclosure()) {
195                  $enclosure = array();
196                  $enclosure['url'] = $attachment->url;
197                  $enclosure['mimetype'] = $attachment->mimetype;
198                  $enclosure['size'] = $attachment->size;
199                  $enclosures[] = $enclosure;
200             }
201         }
202
203         if (!empty($enclosures)) {
204             $twitter_status['attachments'] = $enclosures;
205         }
206
207         if ($include_user) {
208             # Don't get notice (recursive!)
209             $twitter_user = $this->twitter_user_array($profile, false);
210             $twitter_status['user'] = $twitter_user;
211         }
212
213         return $twitter_status;
214     }
215
216     function twitter_rss_entry_array($notice)
217     {
218         $profile = $notice->getProfile();
219         $entry = array();
220
221         // We trim() to avoid extraneous whitespace in the output
222
223         $entry['content'] = common_xml_safe_str(trim($notice->rendered));
224         $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
225         $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
226         $entry['published'] = common_date_iso8601($notice->created);
227
228         $taguribase = common_config('integration', 'taguri');
229         $entry['id'] = "tag:$taguribase:$entry[link]";
230
231         $entry['updated'] = $entry['published'];
232         $entry['author'] = $profile->getBestName();
233
234         // Enclosures
235         $attachments = $notice->attachments();
236         $enclosures = array();
237
238         foreach ($attachments as $attachment) {
239             if ($attachment->isEnclosure()) {
240                  $enclosure = array();
241                  $enclosure['url'] = $attachment->url;
242                  $enclosure['mimetype'] = $attachment->mimetype;
243                  $enclosure['size'] = $attachment->size;
244                  $enclosures[] = $enclosure;
245             }
246         }
247
248         if (!empty($enclosures)) {
249             $entry['enclosures'] = $enclosures;
250         }
251
252 /*
253         // Enclosure
254         $attachments = $notice->attachments();
255         if($attachments){
256             $entry['enclosures']=array();
257             foreach($attachments as $attachment){
258                 if ($attachment->isEnclosure()) {
259                     $enclosure=array();
260                     $enclosure['url']=$attachment->url;
261                     $enclosure['mimetype']=$attachment->mimetype;
262                     $enclosure['size']=$attachment->size;
263                     $entry['enclosures'][]=$enclosure;
264                 }
265             }
266         }
267 */
268
269         // Tags/Categories
270         $tag = new Notice_tag();
271         $tag->notice_id = $notice->id;
272         if ($tag->find()) {
273             $entry['tags']=array();
274             while ($tag->fetch()) {
275                 $entry['tags'][]=$tag->tag;
276             }
277         }
278         $tag->free();
279
280         // RSS Item specific
281         $entry['description'] = $entry['content'];
282         $entry['pubDate'] = common_date_rfc2822($notice->created);
283         $entry['guid'] = $entry['link'];
284
285         return $entry;
286     }
287
288     function twitter_rss_dmsg_array($message)
289     {
290
291         $entry = array();
292
293         $entry['title'] = sprintf('Message from %s to %s',
294             $message->getFrom()->nickname, $message->getTo()->nickname);
295
296         $entry['content'] = common_xml_safe_str(trim($message->content));
297         $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
298         $entry['published'] = common_date_iso8601($message->created);
299
300         $taguribase = common_config('integration', 'taguri');
301
302         $entry['id'] = "tag:$taguribase,:$entry[link]";
303         $entry['updated'] = $entry['published'];
304         $entry['author'] = $message->getFrom()->getBestName();
305
306         # RSS Item specific
307         $entry['description'] = $entry['content'];
308         $entry['pubDate'] = common_date_rfc2822($message->created);
309         $entry['guid'] = $entry['link'];
310
311         return $entry;
312     }
313
314     function twitter_dmsg_array($message)
315     {
316         $twitter_dm = array();
317
318         $from_profile = $message->getFrom();
319         $to_profile = $message->getTo();
320
321         $twitter_dm['id'] = $message->id;
322         $twitter_dm['sender_id'] = $message->from_profile;
323         $twitter_dm['text'] = trim($message->content);
324         $twitter_dm['recipient_id'] = $message->to_profile;
325         $twitter_dm['created_at'] = $this->date_twitter($message->created);
326         $twitter_dm['sender_screen_name'] = $from_profile->nickname;
327         $twitter_dm['recipient_screen_name'] = $to_profile->nickname;
328         $twitter_dm['sender'] = $this->twitter_user_array($from_profile, false);
329         $twitter_dm['recipient'] = $this->twitter_user_array($to_profile, false);
330
331         return $twitter_dm;
332     }
333
334     function twitter_relationship_array($source, $target)
335     {
336         $relationship = array();
337
338         $relationship['source'] =
339             $this->relationship_details_array($source, $target);
340         $relationship['target'] =
341             $this->relationship_details_array($target, $source);
342
343         return array('relationship' => $relationship);
344     }
345
346     function relationship_details_array($source, $target)
347     {
348         $details = array();
349
350         $details['screen_name'] = $source->nickname;
351         $details['followed_by'] = $target->isSubscribed($source);
352         $details['following'] = $source->isSubscribed($target);
353
354         $notifications = false;
355
356         if ($source->isSubscribed($target)) {
357
358             $sub = Subscription::pkeyGet(array('subscriber' =>
359                 $source->id, 'subscribed' => $target->id));
360
361             if (!empty($sub)) {
362                 $notifications = ($sub->jabber || $sub->sms);
363             }
364         }
365
366         $details['notifications_enabled'] = $notifications;
367         $details['blocking'] = $source->hasBlocked($target);
368         $details['id'] = $source->id;
369
370         return $details;
371     }
372
373     function show_twitter_xml_relationship($relationship)
374     {
375         $this->elementStart('relationship');
376
377         foreach($relationship as $element => $value) {
378             if ($element == 'source' || $element == 'target') {
379                 $this->elementStart($element);
380                 $this->show_xml_relationship_details($value);
381                 $this->elementEnd($element);
382             }
383         }
384
385         $this->elementEnd('relationship');
386     }
387
388     function show_xml_relationship_details($details)
389     {
390         foreach($details as $element => $value) {
391             $this->element($element, null, $value);
392         }
393     }
394
395     function show_twitter_xml_status($twitter_status)
396     {
397         $this->elementStart('status');
398         foreach($twitter_status as $element => $value) {
399             switch ($element) {
400             case 'user':
401                 $this->show_twitter_xml_user($twitter_status['user']);
402                 break;
403             case 'text':
404                 $this->element($element, null, common_xml_safe_str($value));
405                 break;
406             case 'attachments':
407                 $this->show_xml_attachments($twitter_status['attachments']);
408                 break;
409             default:
410                 $this->element($element, null, $value);
411             }
412         }
413         $this->elementEnd('status');
414     }
415
416     function show_twitter_xml_user($twitter_user, $role='user')
417     {
418         $this->elementStart($role);
419         foreach($twitter_user as $element => $value) {
420             if ($element == 'status') {
421                 $this->show_twitter_xml_status($twitter_user['status']);
422             } else {
423                 $this->element($element, null, $value);
424             }
425         }
426         $this->elementEnd($role);
427     }
428
429     function show_xml_attachments($attachments) {
430         if (!empty($attachments)) {
431             $this->elementStart('attachments', array('type' => 'array'));
432             foreach ($attachments as $attachment) {
433                 $attrs = array();
434                 $attrs['url'] = $attachment['url'];
435                 $attrs['mimetype'] = $attachment['mimetype'];
436                 $attrs['size'] = $attachment['size'];
437                 $this->element('enclosure', $attrs, '');
438             }
439             $this->elementEnd('attachments');
440         }
441     }
442
443     function show_twitter_rss_item($entry)
444     {
445         $this->elementStart('item');
446         $this->element('title', null, $entry['title']);
447         $this->element('description', null, $entry['description']);
448         $this->element('pubDate', null, $entry['pubDate']);
449         $this->element('guid', null, $entry['guid']);
450         $this->element('link', null, $entry['link']);
451
452         # RSS only supports 1 enclosure per item
453         if($entry['enclosures']){
454             $enclosure = $entry['enclosures'][0];
455             $this->element('enclosure', array('url'=>$enclosure['url'],'type'=>$enclosure['mimetype'],'length'=>$enclosure['size']), null);
456         }
457         
458         if($entry['tags']){
459             foreach($entry['tags'] as $tag){
460                 $this->element('category', null,$tag);
461             }
462         }
463
464         $this->elementEnd('item');
465     }
466
467     function show_json_objects($objects)
468     {
469         print(json_encode($objects));
470     }
471
472     function show_single_xml_status($notice)
473     {
474         $this->init_document('xml');
475         $twitter_status = $this->twitter_status_array($notice);
476         $this->show_twitter_xml_status($twitter_status);
477         $this->end_document('xml');
478     }
479
480     function show_single_json_status($notice)
481     {
482         $this->init_document('json');
483         $status = $this->twitter_status_array($notice);
484         $this->show_json_objects($status);
485         $this->end_document('json');
486     }
487
488     function show_single_xml_dmsg($message)
489     {
490         $this->init_document('xml');
491         $dmsg = $this->twitter_dmsg_array($message);
492         $this->show_twitter_xml_dmsg($dmsg);
493         $this->end_document('xml');
494     }
495
496     function show_single_json_dmsg($message)
497     {
498         $this->init_document('json');
499         $dmsg = $this->twitter_dmsg_array($message);
500         $this->show_json_objects($dmsg);
501         $this->end_document('json');
502     }
503
504     function show_twitter_xml_dmsg($twitter_dm)
505     {
506         $this->elementStart('direct_message');
507         foreach($twitter_dm as $element => $value) {
508             switch ($element) {
509             case 'sender':
510             case 'recipient':
511                 $this->show_twitter_xml_user($value, $element);
512                 break;
513             case 'text':
514                 $this->element($element, null, common_xml_safe_str($value));
515                 break;
516             default:
517                 $this->element($element, null, $value);
518             }
519         }
520         $this->elementEnd('direct_message');
521     }
522
523     function show_xml_timeline($notice)
524     {
525
526         $this->init_document('xml');
527         $this->elementStart('statuses', array('type' => 'array'));
528
529         if (is_array($notice)) {
530             foreach ($notice as $n) {
531                 $twitter_status = $this->twitter_status_array($n);
532                 $this->show_twitter_xml_status($twitter_status);
533             }
534         } else {
535             while ($notice->fetch()) {
536                 $twitter_status = $this->twitter_status_array($notice);
537                 $this->show_twitter_xml_status($twitter_status);
538             }
539         }
540
541         $this->elementEnd('statuses');
542         $this->end_document('xml');
543     }
544
545     function show_rss_timeline($notice, $title, $link, $subtitle, $suplink=null)
546     {
547
548         $this->init_document('rss');
549
550         $this->elementStart('channel');
551         $this->element('title', null, $title);
552         $this->element('link', null, $link);
553         if (!is_null($suplink)) {
554             # For FriendFeed's SUP protocol
555             $this->element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
556                                          'rel' => 'http://api.friendfeed.com/2008/03#sup',
557                                          'href' => $suplink,
558                                          'type' => 'application/json'));
559         }
560         $this->element('description', null, $subtitle);
561         $this->element('language', null, 'en-us');
562         $this->element('ttl', null, '40');
563
564         if (is_array($notice)) {
565             foreach ($notice as $n) {
566                 $entry = $this->twitter_rss_entry_array($n);
567                 $this->show_twitter_rss_item($entry);
568             }
569         } else {
570             while ($notice->fetch()) {
571                 $entry = $this->twitter_rss_entry_array($notice);
572                 $this->show_twitter_rss_item($entry);
573             }
574         }
575
576         $this->elementEnd('channel');
577         $this->end_twitter_rss();
578     }
579
580     function show_atom_timeline($notice, $title, $id, $link, $subtitle=null, $suplink=null, $selfuri=null)
581     {
582
583         $this->init_document('atom');
584
585         $this->element('title', null, $title);
586         $this->element('id', null, $id);
587         $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
588
589         if (!is_null($suplink)) {
590             # For FriendFeed's SUP protocol
591             $this->element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
592                                          'href' => $suplink,
593                                          'type' => 'application/json'));
594         }
595
596         if (!is_null($selfuri)) {
597             $this->element('link', array('href' => $selfuri,
598                 'rel' => 'self', 'type' => 'application/atom+xml'), null);
599         }
600
601         $this->element('updated', null, common_date_iso8601('now'));
602         $this->element('subtitle', null, $subtitle);
603
604         if (is_array($notice)) {
605             foreach ($notice as $n) {
606                 $this->raw($n->asAtomEntry());
607             }
608         } else {
609             while ($notice->fetch()) {
610                 $this->raw($notice->asAtomEntry());
611             }
612         }
613
614         $this->end_document('atom');
615
616     }
617
618     function show_json_timeline($notice)
619     {
620
621         $this->init_document('json');
622
623         $statuses = array();
624
625         if (is_array($notice)) {
626             foreach ($notice as $n) {
627                 $twitter_status = $this->twitter_status_array($n);
628                 array_push($statuses, $twitter_status);
629             }
630         } else {
631             while ($notice->fetch()) {
632                 $twitter_status = $this->twitter_status_array($notice);
633                 array_push($statuses, $twitter_status);
634             }
635         }
636
637         $this->show_json_objects($statuses);
638
639         $this->end_document('json');
640     }
641
642     // Anyone know what date format this is?
643     // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
644     function date_twitter($dt)
645     {
646         $t = strtotime($dt);
647         return date("D M d H:i:s O Y", $t);
648     }
649
650     // XXX: Candidate for a general utility method somewhere?
651     function count_subscriptions($profile)
652     {
653
654         $count = 0;
655         $sub = new Subscription();
656         $sub->subscribed = $profile->id;
657
658         $count = $sub->find();
659
660         if ($count > 0) {
661             return $count - 1;
662         } else {
663             return 0;
664         }
665     }
666
667     function init_document($type='xml')
668     {
669         switch ($type) {
670         case 'xml':
671             header('Content-Type: application/xml; charset=utf-8');
672             $this->startXML();
673             break;
674         case 'json':
675             header('Content-Type: application/json; charset=utf-8');
676
677             // Check for JSONP callback
678             $callback = $this->arg('callback');
679             if ($callback) {
680                 print $callback . '(';
681             }
682             break;
683         case 'rss':
684             header("Content-Type: application/rss+xml; charset=utf-8");
685             $this->init_twitter_rss();
686             break;
687         case 'atom':
688             header('Content-Type: application/atom+xml; charset=utf-8');
689             $this->init_twitter_atom();
690             break;
691         default:
692             $this->clientError(_('Not a supported data format.'));
693             break;
694         }
695
696         return;
697     }
698
699     function end_document($type='xml')
700     {
701         switch ($type) {
702         case 'xml':
703             $this->endXML();
704             break;
705         case 'json':
706
707             // Check for JSONP callback
708             $callback = $this->arg('callback');
709             if ($callback) {
710                 print ')';
711             }
712             break;
713         case 'rss':
714             $this->end_twitter_rss();
715             break;
716         case 'atom':
717             $this->end_twitter_rss();
718             break;
719         default:
720             $this->clientError(_('Not a supported data format.'));
721             break;
722         }
723         return;
724     }
725
726     function clientError($msg, $code = 400, $content_type = 'json')
727     {
728
729         static $status = array(400 => 'Bad Request',
730                                401 => 'Unauthorized',
731                                402 => 'Payment Required',
732                                403 => 'Forbidden',
733                                404 => 'Not Found',
734                                405 => 'Method Not Allowed',
735                                406 => 'Not Acceptable',
736                                407 => 'Proxy Authentication Required',
737                                408 => 'Request Timeout',
738                                409 => 'Conflict',
739                                410 => 'Gone',
740                                411 => 'Length Required',
741                                412 => 'Precondition Failed',
742                                413 => 'Request Entity Too Large',
743                                414 => 'Request-URI Too Long',
744                                415 => 'Unsupported Media Type',
745                                416 => 'Requested Range Not Satisfiable',
746                                417 => 'Expectation Failed');
747
748         $action = $this->trimmed('action');
749
750         common_debug("User error '$code' on '$action': $msg", __FILE__);
751
752         if (!array_key_exists($code, $status)) {
753             $code = 400;
754         }
755
756         $status_string = $status[$code];
757         header('HTTP/1.1 '.$code.' '.$status_string);
758
759         if ($content_type == 'xml') {
760             $this->init_document('xml');
761             $this->elementStart('hash');
762             $this->element('error', null, $msg);
763             $this->element('request', null, $_SERVER['REQUEST_URI']);
764             $this->elementEnd('hash');
765             $this->end_document('xml');
766         } else {
767             $this->init_document('json');
768             $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
769             print(json_encode($error_array));
770             $this->end_document('json');
771         }
772
773     }
774
775     function init_twitter_rss()
776     {
777         $this->startXML();
778         $this->elementStart('rss', array('version' => '2.0'));
779     }
780
781     function end_twitter_rss()
782     {
783         $this->elementEnd('rss');
784         $this->endXML();
785     }
786
787     function init_twitter_atom()
788     {
789         $this->startXML();
790         // FIXME: don't hardcode the language here!
791         $this->elementStart('feed', array('xmlns' => 'http://www.w3.org/2005/Atom',
792                                           'xml:lang' => 'en-US',
793                                           'xmlns:thr' => 'http://purl.org/syndication/thread/1.0'));
794     }
795
796     function end_twitter_atom()
797     {
798         $this->elementEnd('feed');
799         $this->endXML();
800     }
801
802     function show_profile($profile, $content_type='xml', $notice=null)
803     {
804         $profile_array = $this->twitter_user_array($profile, true);
805         switch ($content_type) {
806         case 'xml':
807             $this->show_twitter_xml_user($profile_array);
808             break;
809         case 'json':
810             $this->show_json_objects($profile_array);
811             break;
812         default:
813             $this->clientError(_('Not a supported data format.'));
814             return;
815         }
816         return;
817     }
818
819     function get_user($id, $apidata=null)
820     {
821         if (empty($id)) {
822
823             // Twitter supports these other ways of passing the user ID
824             if (is_numeric($this->arg('id'))) {
825                 return User::staticGet($this->arg('id'));
826             } else if ($this->arg('id')) {
827                 $nickname = common_canonical_nickname($this->arg('id'));
828                 return User::staticGet('nickname', $nickname);
829             } else if ($this->arg('user_id')) {
830                 // This is to ensure that a non-numeric user_id still
831                 // overrides screen_name even if it doesn't get used
832                 if (is_numeric($this->arg('user_id'))) {
833                     return User::staticGet('id', $this->arg('user_id'));
834                 }
835             } else if ($this->arg('screen_name')) {
836                 $nickname = common_canonical_nickname($this->arg('screen_name'));
837                 return User::staticGet('nickname', $nickname);
838             } else {
839                 // Fall back to trying the currently authenticated user
840                 return $apidata['user'];
841             }
842
843         } else if (is_numeric($id)) {
844             return User::staticGet($id);
845         } else {
846             $nickname = common_canonical_nickname($id);
847             return User::staticGet('nickname', $nickname);
848         }
849     }
850
851     function get_group($id, $apidata=null)
852     {
853         if (empty($id)) {
854
855             if (is_numeric($this->arg('id'))) {
856                 return User_group::staticGet($this->arg('id'));
857             } else if ($this->arg('id')) {
858                 $nickname = common_canonical_nickname($this->arg('id'));
859                 return User_group::staticGet('nickname', $nickname);
860             } else if ($this->arg('group_id')) {
861                 // This is to ensure that a non-numeric user_id still
862                 // overrides screen_name even if it doesn't get used
863                 if (is_numeric($this->arg('group_id'))) {
864                     return User_group::staticGet('id', $this->arg('group_id'));
865                 }
866             } else if ($this->arg('group_name')) {
867                 $nickname = common_canonical_nickname($this->arg('group_name'));
868                 return User_group::staticGet('nickname', $nickname);
869             }
870
871         } else if (is_numeric($id)) {
872             return User_group::staticGet($id);
873         } else {
874             $nickname = common_canonical_nickname($id);
875             return User_group::staticGet('nickname', $nickname);
876         }
877     }
878
879     function get_profile($id)
880     {
881         if (is_numeric($id)) {
882             return Profile::staticGet($id);
883         } else {
884             $user = User::staticGet('nickname', $id);
885             if ($user) {
886                 return $user->getProfile();
887             } else {
888                 return null;
889             }
890         }
891     }
892
893     function source_link($source)
894     {
895         $source_name = _($source);
896         switch ($source) {
897         case 'web':
898         case 'xmpp':
899         case 'mail':
900         case 'omb':
901         case 'api':
902             break;
903         default:
904             $ns = Notice_source::staticGet($source);
905             if ($ns) {
906                 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
907             }
908             break;
909         }
910         return $source_name;
911     }
912
913     /**
914      * Returns query argument or default value if not found. Certain
915      * parameters used throughout the API are lightly scrubbed and
916      * bounds checked.  This overrides Action::arg().
917      *
918      * @param string $key requested argument
919      * @param string $def default value to return if $key is not provided
920      *
921      * @return var $var
922      */
923     function arg($key, $def=null)
924     {
925
926         // XXX: Do even more input validation/scrubbing?
927
928         if (array_key_exists($key, $this->args)) {
929             switch($key) {
930             case 'page':
931                 $page = (int)$this->args['page'];
932                 return ($page < 1) ? 1 : $page;
933             case 'count':
934                 $count = (int)$this->args['count'];
935                 if ($count < 1) {
936                     return 20;
937                 } elseif ($count > 200) {
938                     return 200;
939                 } else {
940                     return $count;
941                 }
942             case 'since_id':
943                 $since_id = (int)$this->args['since_id'];
944                 return ($since_id < 1) ? 0 : $since_id;
945             case 'max_id':
946                 $max_id = (int)$this->args['max_id'];
947                 return ($max_id < 1) ? 0 : $max_id;
948             case 'since':
949                 return strtotime($this->args['since']);
950             default:
951                 return parent::arg($key, $def);
952             }
953         } else {
954             return $def;
955         }
956     }
957
958 }