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