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