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