]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/twitterapi.php
Merge branch 'master' of git@gitorious.org:laconica/mainline
[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
192         if (!empty($attachments)) {
193
194             $twitter_status['attachments'] = array();
195
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;
203                 }
204             }
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         # Enclosure
235         $attachments = $notice->attachments();
236         if($attachments){
237             $entry['enclosures']=array();
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                     $entry['enclosures'][]=$enclosure;
245                 }
246             }
247         }
248
249         # RSS Item specific
250         $entry['description'] = $entry['content'];
251         $entry['pubDate'] = common_date_rfc2822($notice->created);
252         $entry['guid'] = $entry['link'];
253
254         return $entry;
255     }
256
257     function twitter_rss_dmsg_array($message)
258     {
259
260         $entry = array();
261
262         $entry['title'] = sprintf('Message from %s to %s',
263             $message->getFrom()->nickname, $message->getTo()->nickname);
264
265         $entry['content'] = common_xml_safe_str(trim($message->content));
266         $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
267         $entry['published'] = common_date_iso8601($message->created);
268
269         $taguribase = common_config('integration', 'taguri');
270
271         $entry['id'] = "tag:$taguribase,:$entry[link]";
272         $entry['updated'] = $entry['published'];
273         $entry['author'] = $message->getFrom()->getBestName();
274
275         # RSS Item specific
276         $entry['description'] = $entry['content'];
277         $entry['pubDate'] = common_date_rfc2822($message->created);
278         $entry['guid'] = $entry['link'];
279
280         return $entry;
281     }
282
283     function twitter_dmsg_array($message)
284     {
285         $twitter_dm = array();
286
287         $from_profile = $message->getFrom();
288         $to_profile = $message->getTo();
289
290         $twitter_dm['id'] = $message->id;
291         $twitter_dm['sender_id'] = $message->from_profile;
292         $twitter_dm['text'] = trim($message->content);
293         $twitter_dm['recipient_id'] = $message->to_profile;
294         $twitter_dm['created_at'] = $this->date_twitter($message->created);
295         $twitter_dm['sender_screen_name'] = $from_profile->nickname;
296         $twitter_dm['recipient_screen_name'] = $to_profile->nickname;
297         $twitter_dm['sender'] = $this->twitter_user_array($from_profile, false);
298         $twitter_dm['recipient'] = $this->twitter_user_array($to_profile, false);
299
300         return $twitter_dm;
301     }
302
303     function twitter_relationship_array($source, $target)
304     {
305         $relationship = array();
306
307         $relationship['source'] =
308             $this->relationship_details_array($source, $target);
309         $relationship['target'] =
310             $this->relationship_details_array($target, $source);
311
312         return array('relationship' => $relationship);
313     }
314
315     function relationship_details_array($source, $target)
316     {
317         $details = array();
318
319         $details['screen_name'] = $source->nickname;
320         $details['followed_by'] = $target->isSubscribed($source);
321         $details['following'] = $source->isSubscribed($target);
322
323         $notifications = false;
324
325         if ($source->isSubscribed($target)) {
326
327             $sub = Subscription::pkeyGet(array('subscriber' =>
328                 $source->id, 'subscribed' => $target->id));
329
330             if (!empty($sub)) {
331                 $notifications = ($sub->jabber || $sub->sms);
332             }
333         }
334
335         $details['notifications_enabled'] = $notifications;
336         $details['blocking'] = $source->hasBlocked($target);
337         $details['id'] = $source->id;
338
339         return $details;
340     }
341
342     function show_twitter_xml_relationship($relationship)
343     {
344         $this->elementStart('relationship');
345
346         foreach($relationship as $element => $value) {
347             if ($element == 'source' || $element == 'target') {
348                 $this->elementStart($element);
349                 $this->show_xml_relationship_details($value);
350                 $this->elementEnd($element);
351             }
352         }
353
354         $this->elementEnd('relationship');
355     }
356
357     function show_xml_relationship_details($details)
358     {
359         foreach($details as $element => $value) {
360             $this->element($element, null, $value);
361         }
362     }
363
364     function show_twitter_xml_status($twitter_status)
365     {
366         $this->elementStart('status');
367         foreach($twitter_status as $element => $value) {
368             switch ($element) {
369             case 'user':
370                 $this->show_twitter_xml_user($twitter_status['user']);
371                 break;
372             case 'text':
373                 $this->element($element, null, common_xml_safe_str($value));
374                 break;
375             case 'attachments':
376                 $this->show_xml_attachments($twitter_status['attachments']);
377                 break;
378             default:
379                 $this->element($element, null, $value);
380             }
381         }
382         $this->elementEnd('status');
383     }
384
385     function show_twitter_xml_user($twitter_user, $role='user')
386     {
387         $this->elementStart($role);
388         foreach($twitter_user as $element => $value) {
389             if ($element == 'status') {
390                 $this->show_twitter_xml_status($twitter_user['status']);
391             } else {
392                 $this->element($element, null, $value);
393             }
394         }
395         $this->elementEnd($role);
396     }
397
398     function show_xml_attachments($attachments) {
399         if (!empty($attachments)) {
400             $this->elementStart('attachments', array('type' => 'array'));
401             foreach ($attachments as $attachment) {
402                 $attrs = array();
403                 $attrs['url'] = $attachment['url'];
404                 $attrs['mimetype'] = $attachment['mimetype'];
405                 $attrs['size'] = $attachment['size'];
406                 $this->element('enclosure', $attrs, '');
407             }
408             $this->elementEnd('attachments');
409         }
410     }
411
412     function show_twitter_rss_item($entry)
413     {
414         $this->elementStart('item');
415         $this->element('title', null, $entry['title']);
416         $this->element('description', null, $entry['description']);
417         $this->element('pubDate', null, $entry['pubDate']);
418         $this->element('guid', null, $entry['guid']);
419         $this->element('link', null, $entry['link']);
420
421         # RSS only supports 1 enclosure per item
422         if($entry['enclosures']){
423             $enclosure = $entry['enclosures'][0];
424             $this->element('enclosure', array('url'=>$enclosure['url'],'type'=>$enclosure['mimetype'],'length'=>$enclosure['size']), null);
425         }
426
427         $this->elementEnd('item');
428     }
429
430     function show_json_objects($objects)
431     {
432         print(json_encode($objects));
433     }
434
435     function show_single_xml_status($notice)
436     {
437         $this->init_document('xml');
438         $twitter_status = $this->twitter_status_array($notice);
439         $this->show_twitter_xml_status($twitter_status);
440         $this->end_document('xml');
441     }
442
443     function show_single_json_status($notice)
444     {
445         $this->init_document('json');
446         $status = $this->twitter_status_array($notice);
447         $this->show_json_objects($status);
448         $this->end_document('json');
449     }
450
451     function show_single_xml_dmsg($message)
452     {
453         $this->init_document('xml');
454         $dmsg = $this->twitter_dmsg_array($message);
455         $this->show_twitter_xml_dmsg($dmsg);
456         $this->end_document('xml');
457     }
458
459     function show_single_json_dmsg($message)
460     {
461         $this->init_document('json');
462         $dmsg = $this->twitter_dmsg_array($message);
463         $this->show_json_objects($dmsg);
464         $this->end_document('json');
465     }
466
467     function show_twitter_xml_dmsg($twitter_dm)
468     {
469         $this->elementStart('direct_message');
470         foreach($twitter_dm as $element => $value) {
471             switch ($element) {
472             case 'sender':
473             case 'recipient':
474                 $this->show_twitter_xml_user($value, $element);
475                 break;
476             case 'text':
477                 $this->element($element, null, common_xml_safe_str($value));
478                 break;
479             default:
480                 $this->element($element, null, $value);
481             }
482         }
483         $this->elementEnd('direct_message');
484     }
485
486     function show_xml_timeline($notice)
487     {
488
489         $this->init_document('xml');
490         $this->elementStart('statuses', array('type' => 'array'));
491
492         if (is_array($notice)) {
493             foreach ($notice as $n) {
494                 $twitter_status = $this->twitter_status_array($n);
495                 $this->show_twitter_xml_status($twitter_status);
496             }
497         } else {
498             while ($notice->fetch()) {
499                 $twitter_status = $this->twitter_status_array($notice);
500                 $this->show_twitter_xml_status($twitter_status);
501             }
502         }
503
504         $this->elementEnd('statuses');
505         $this->end_document('xml');
506     }
507
508     function show_rss_timeline($notice, $title, $link, $subtitle, $suplink=null)
509     {
510
511         $this->init_document('rss');
512
513         $this->elementStart('channel');
514         $this->element('title', null, $title);
515         $this->element('link', null, $link);
516         if (!is_null($suplink)) {
517             # For FriendFeed's SUP protocol
518             $this->element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
519                                          'rel' => 'http://api.friendfeed.com/2008/03#sup',
520                                          'href' => $suplink,
521                                          'type' => 'application/json'));
522         }
523         $this->element('description', null, $subtitle);
524         $this->element('language', null, 'en-us');
525         $this->element('ttl', null, '40');
526
527         if (is_array($notice)) {
528             foreach ($notice as $n) {
529                 $entry = $this->twitter_rss_entry_array($n);
530                 $this->show_twitter_rss_item($entry);
531             }
532         } else {
533             while ($notice->fetch()) {
534                 $entry = $this->twitter_rss_entry_array($notice);
535                 $this->show_twitter_rss_item($entry);
536             }
537         }
538
539         $this->elementEnd('channel');
540         $this->end_twitter_rss();
541     }
542
543     function show_atom_timeline($notice, $title, $id, $link, $subtitle=null, $suplink=null, $selfuri=null)
544     {
545
546         $this->init_document('atom');
547
548         $this->element('title', null, $title);
549         $this->element('id', null, $id);
550         $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
551
552         if (!is_null($suplink)) {
553             # For FriendFeed's SUP protocol
554             $this->element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
555                                          'href' => $suplink,
556                                          'type' => 'application/json'));
557         }
558
559         if (!is_null($selfuri)) {
560             $this->element('link', array('href' => $selfuri,
561                 'rel' => 'self', 'type' => 'application/atom+xml'), null);
562         }
563
564         $this->element('updated', null, common_date_iso8601('now'));
565         $this->element('subtitle', null, $subtitle);
566
567         if (is_array($notice)) {
568             foreach ($notice as $n) {
569                 $this->raw($n->asAtomEntry());
570             }
571         } else {
572             while ($notice->fetch()) {
573                 $this->raw($notice->asAtomEntry());
574             }
575         }
576
577         $this->end_document('atom');
578
579     }
580
581     function show_json_timeline($notice)
582     {
583
584         $this->init_document('json');
585
586         $statuses = array();
587
588         if (is_array($notice)) {
589             foreach ($notice as $n) {
590                 $twitter_status = $this->twitter_status_array($n);
591                 array_push($statuses, $twitter_status);
592             }
593         } else {
594             while ($notice->fetch()) {
595                 $twitter_status = $this->twitter_status_array($notice);
596                 array_push($statuses, $twitter_status);
597             }
598         }
599
600         $this->show_json_objects($statuses);
601
602         $this->end_document('json');
603     }
604
605     // Anyone know what date format this is?
606     // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
607     function date_twitter($dt)
608     {
609         $t = strtotime($dt);
610         return date("D M d H:i:s O Y", $t);
611     }
612
613     // XXX: Candidate for a general utility method somewhere?
614     function count_subscriptions($profile)
615     {
616
617         $count = 0;
618         $sub = new Subscription();
619         $sub->subscribed = $profile->id;
620
621         $count = $sub->find();
622
623         if ($count > 0) {
624             return $count - 1;
625         } else {
626             return 0;
627         }
628     }
629
630     function init_document($type='xml')
631     {
632         switch ($type) {
633         case 'xml':
634             header('Content-Type: application/xml; charset=utf-8');
635             $this->startXML();
636             break;
637         case 'json':
638             header('Content-Type: application/json; charset=utf-8');
639
640             // Check for JSONP callback
641             $callback = $this->arg('callback');
642             if ($callback) {
643                 print $callback . '(';
644             }
645             break;
646         case 'rss':
647             header("Content-Type: application/rss+xml; charset=utf-8");
648             $this->init_twitter_rss();
649             break;
650         case 'atom':
651             header('Content-Type: application/atom+xml; charset=utf-8');
652             $this->init_twitter_atom();
653             break;
654         default:
655             $this->clientError(_('Not a supported data format.'));
656             break;
657         }
658
659         return;
660     }
661
662     function end_document($type='xml')
663     {
664         switch ($type) {
665         case 'xml':
666             $this->endXML();
667             break;
668         case 'json':
669
670             // Check for JSONP callback
671             $callback = $this->arg('callback');
672             if ($callback) {
673                 print ')';
674             }
675             break;
676         case 'rss':
677             $this->end_twitter_rss();
678             break;
679         case 'atom':
680             $this->end_twitter_rss();
681             break;
682         default:
683             $this->clientError(_('Not a supported data format.'));
684             break;
685         }
686         return;
687     }
688
689     function clientError($msg, $code = 400, $content_type = 'json')
690     {
691
692         static $status = array(400 => 'Bad Request',
693                                401 => 'Unauthorized',
694                                402 => 'Payment Required',
695                                403 => 'Forbidden',
696                                404 => 'Not Found',
697                                405 => 'Method Not Allowed',
698                                406 => 'Not Acceptable',
699                                407 => 'Proxy Authentication Required',
700                                408 => 'Request Timeout',
701                                409 => 'Conflict',
702                                410 => 'Gone',
703                                411 => 'Length Required',
704                                412 => 'Precondition Failed',
705                                413 => 'Request Entity Too Large',
706                                414 => 'Request-URI Too Long',
707                                415 => 'Unsupported Media Type',
708                                416 => 'Requested Range Not Satisfiable',
709                                417 => 'Expectation Failed');
710
711         $action = $this->trimmed('action');
712
713         common_debug("User error '$code' on '$action': $msg", __FILE__);
714
715         if (!array_key_exists($code, $status)) {
716             $code = 400;
717         }
718
719         $status_string = $status[$code];
720         header('HTTP/1.1 '.$code.' '.$status_string);
721
722         if ($content_type == 'xml') {
723             $this->init_document('xml');
724             $this->elementStart('hash');
725             $this->element('error', null, $msg);
726             $this->element('request', null, $_SERVER['REQUEST_URI']);
727             $this->elementEnd('hash');
728             $this->end_document('xml');
729         } else {
730             $this->init_document('json');
731             $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
732             print(json_encode($error_array));
733             $this->end_document('json');
734         }
735
736     }
737
738     function init_twitter_rss()
739     {
740         $this->startXML();
741         $this->elementStart('rss', array('version' => '2.0'));
742     }
743
744     function end_twitter_rss()
745     {
746         $this->elementEnd('rss');
747         $this->endXML();
748     }
749
750     function init_twitter_atom()
751     {
752         $this->startXML();
753         // FIXME: don't hardcode the language here!
754         $this->elementStart('feed', array('xmlns' => 'http://www.w3.org/2005/Atom',
755                                           'xml:lang' => 'en-US',
756                                           'xmlns:thr' => 'http://purl.org/syndication/thread/1.0'));
757     }
758
759     function end_twitter_atom()
760     {
761         $this->elementEnd('feed');
762         $this->endXML();
763     }
764
765     function show_profile($profile, $content_type='xml', $notice=null)
766     {
767         $profile_array = $this->twitter_user_array($profile, true);
768         switch ($content_type) {
769         case 'xml':
770             $this->show_twitter_xml_user($profile_array);
771             break;
772         case 'json':
773             $this->show_json_objects($profile_array);
774             break;
775         default:
776             $this->clientError(_('Not a supported data format.'));
777             return;
778         }
779         return;
780     }
781
782     function get_user($id, $apidata=null)
783     {
784         if (empty($id)) {
785
786             // Twitter supports these other ways of passing the user ID
787             if (is_numeric($this->arg('id'))) {
788                 return User::staticGet($this->arg('id'));
789             } else if ($this->arg('id')) {
790                 $nickname = common_canonical_nickname($this->arg('id'));
791                 return User::staticGet('nickname', $nickname);
792             } else if ($this->arg('user_id')) {
793                 // This is to ensure that a non-numeric user_id still
794                 // overrides screen_name even if it doesn't get used
795                 if (is_numeric($this->arg('user_id'))) {
796                     return User::staticGet('id', $this->arg('user_id'));
797                 }
798             } else if ($this->arg('screen_name')) {
799                 $nickname = common_canonical_nickname($this->arg('screen_name'));
800                 return User::staticGet('nickname', $nickname);
801             } else {
802                 // Fall back to trying the currently authenticated user
803                 return $apidata['user'];
804             }
805
806         } else if (is_numeric($id)) {
807             return User::staticGet($id);
808         } else {
809             $nickname = common_canonical_nickname($id);
810             return User::staticGet('nickname', $nickname);
811         }
812     }
813
814     function get_group($id, $apidata=null)
815     {
816         if (empty($id)) {
817
818             if (is_numeric($this->arg('id'))) {
819                 return User_group::staticGet($this->arg('id'));
820             } else if ($this->arg('id')) {
821                 $nickname = common_canonical_nickname($this->arg('id'));
822                 return User_group::staticGet('nickname', $nickname);
823             } else if ($this->arg('group_id')) {
824                 // This is to ensure that a non-numeric user_id still
825                 // overrides screen_name even if it doesn't get used
826                 if (is_numeric($this->arg('group_id'))) {
827                     return User_group::staticGet('id', $this->arg('group_id'));
828                 }
829             } else if ($this->arg('group_name')) {
830                 $nickname = common_canonical_nickname($this->arg('group_name'));
831                 return User_group::staticGet('nickname', $nickname);
832             }
833
834         } else if (is_numeric($id)) {
835             return User_group::staticGet($id);
836         } else {
837             $nickname = common_canonical_nickname($id);
838             return User_group::staticGet('nickname', $nickname);
839         }
840     }
841
842     function get_profile($id)
843     {
844         if (is_numeric($id)) {
845             return Profile::staticGet($id);
846         } else {
847             $user = User::staticGet('nickname', $id);
848             if ($user) {
849                 return $user->getProfile();
850             } else {
851                 return null;
852             }
853         }
854     }
855
856     function source_link($source)
857     {
858         $source_name = _($source);
859         switch ($source) {
860         case 'web':
861         case 'xmpp':
862         case 'mail':
863         case 'omb':
864         case 'api':
865             break;
866         default:
867             $ns = Notice_source::staticGet($source);
868             if ($ns) {
869                 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
870             }
871             break;
872         }
873         return $source_name;
874     }
875
876     /**
877      * Returns query argument or default value if not found. Certain
878      * parameters used throughout the API are lightly scrubbed and
879      * bounds checked.  This overrides Action::arg().
880      *
881      * @param string $key requested argument
882      * @param string $def default value to return if $key is not provided
883      *
884      * @return var $var
885      */
886     function arg($key, $def=null)
887     {
888
889         // XXX: Do even more input validation/scrubbing?
890
891         if (array_key_exists($key, $this->args)) {
892             switch($key) {
893             case 'page':
894                 $page = (int)$this->args['page'];
895                 return ($page < 1) ? 1 : $page;
896             case 'count':
897                 $count = (int)$this->args['count'];
898                 if ($count < 1) {
899                     return 20;
900                 } elseif ($count > 200) {
901                     return 200;
902                 } else {
903                     return $count;
904                 }
905             case 'since_id':
906                 $since_id = (int)$this->args['since_id'];
907                 return ($since_id < 1) ? 0 : $since_id;
908             case 'max_id':
909                 $max_id = (int)$this->args['max_id'];
910                 return ($max_id < 1) ? 0 : $max_id;
911             case 'since':
912                 return strtotime($this->args['since']);
913             default:
914                 return parent::arg($key, $def);
915             }
916         } else {
917             return $def;
918         }
919     }
920
921 }