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