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