]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/action.php
isReadOnly() now takes arguments
[quix0rs-gnu-social.git] / lib / action.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Base class for all actions (~views)
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Action
23  * @package   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @author    Sarven Capadisli <csarven@controlyourself.ca>
26  * @copyright 2008 Control Yourself, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://laconi.ca/
29  */
30
31 if (!defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/noticeform.php';
36 require_once INSTALLDIR.'/lib/htmloutputter.php';
37
38 /**
39  * Base class for all actions
40  *
41  * This is the base class for all actions in the package. An action is
42  * more or less a "view" in an MVC framework.
43  *
44  * Actions are responsible for extracting and validating parameters; using
45  * model classes to read and write to the database; and doing ouput.
46  *
47  * @category Output
48  * @package  Laconica
49  * @author   Evan Prodromou <evan@controlyourself.ca>
50  * @author   Sarven Capadisli <csarven@controlyourself.ca>
51  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
52  * @link     http://laconi.ca/
53  *
54  * @see      HTMLOutputter
55  */
56 class Action extends HTMLOutputter // lawsuit
57 {
58     var $args;
59
60     /**
61      * Constructor
62      *
63      * Just wraps the HTMLOutputter constructor.
64      *
65      * @param string  $output URI to output to, default = stdout
66      * @param boolean $indent Whether to indent output, default true
67      *
68      * @see XMLOutputter::__construct
69      * @see HTMLOutputter::__construct
70      */
71     function __construct($output='php://output', $indent=true)
72     {
73         parent::__construct($output, $indent);
74     }
75
76     /**
77      * For initializing members of the class.
78      *
79      * @param array $argarray misc. arguments
80      *
81      * @return boolean true
82      */
83     function prepare($argarray)
84     {
85         $this->args =& common_copy_args($argarray);
86         return true;
87     }
88
89     /**
90      * Show page, a template method.
91      *
92      * @return nothing
93      */
94     function showPage()
95     {
96         if (Event::handle('StartShowHTML', array($this))) {
97             $this->startHTML();
98             Event::handle('EndShowHTML', array($this));
99         }
100         if (Event::handle('StartShowHead', array($this))) {
101         $this->showHead();
102             Event::handle('EndShowHead', array($this));
103         }
104         if (Event::handle('StartShowBody', array($this))) {
105         $this->showBody();
106             Event::handle('EndShowBody', array($this));
107         }
108         if (Event::handle('StartEndHTML', array($this))) {
109         $this->endHTML();
110             Event::handle('EndEndHTML', array($this));
111         }
112     }
113
114     /**
115      * Show head, a template method.
116      *
117      * @return nothing
118      */
119     function showHead()
120     {
121         // XXX: attributes (profile?)
122         $this->elementStart('head');
123         $this->showTitle();
124         $this->showShortcutIcon();
125         $this->showStylesheets();
126         $this->showScripts();
127         $this->showOpenSearch();
128         $this->showFeeds();
129         $this->showDescription();
130         $this->extraHead();
131         $this->elementEnd('head');
132     }
133
134     /**
135      * Show title, a template method.
136      *
137      * @return nothing
138      */
139     function showTitle()
140     {
141         $this->element('title', null,
142                        sprintf(_("%s - %s"),
143                                $this->title(),
144                                common_config('site', 'name')));
145     }
146
147     /**
148      * Returns the page title
149      *
150      * SHOULD overload
151      *
152      * @return string page title
153      */
154
155     function title()
156     {
157         return _("Untitled page");
158     }
159
160     /**
161      * Show themed shortcut icon
162      *
163      * @return nothing
164      */
165     function showShortcutIcon()
166     {
167         if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/favicon.ico')) {
168             $this->element('link', array('rel' => 'shortcut icon',
169                                          'href' => theme_path('favicon.ico')));
170         } else {
171             $this->element('link', array('rel' => 'shortcut icon',
172                                          'href' => common_path('favicon.ico')));
173         }
174
175         if (common_config('site', 'mobile')) {
176             if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/apple-touch-icon.png')) {
177                 $this->element('link', array('rel' => 'apple-touch-icon',
178                                              'href' => theme_path('apple-touch-icon.png')));
179             } else {
180                 $this->element('link', array('rel' => 'apple-touch-icon',
181                                              'href' => common_path('apple-touch-icon.png')));
182             }
183         }
184     }
185
186     /**
187      * Show stylesheets
188      *
189      * @return nothing
190      */
191     function showStylesheets()
192     {
193         if (Event::handle('StartShowStyles', array($this))) {
194             if (Event::handle('StartShowLaconicaStyles', array($this))) {
195                 $this->element('link', array('rel' => 'stylesheet',
196                                              'type' => 'text/css',
197                                              'href' => theme_path('css/display.css', 'base') . '?version=' . LACONICA_VERSION,
198                                              'media' => 'screen, projection, tv'));
199                 $this->element('link', array('rel' => 'stylesheet',
200                                              'type' => 'text/css',
201                                              'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
202                                              'media' => 'screen, projection, tv'));
203                 if (common_config('site', 'mobile')) {
204                     $this->element('link', array('rel' => 'stylesheet',
205                                                  'type' => 'text/css',
206                                                  'href' => theme_path('css/mobile.css', 'base') . '?version=' . LACONICA_VERSION,
207                                                  // TODO: "handheld" CSS for other mobile devices
208                                                  'media' => 'only screen and (max-device-width: 480px)')); // Mobile WebKit
209                 }
210                 $this->element('link', array('rel' => 'stylesheet',
211                                              'type' => 'text/css',
212                                              'href' => theme_path('css/print.css', 'base') . '?version=' . LACONICA_VERSION,
213                                              'media' => 'print'));
214                 Event::handle('EndShowLaconicaStyles', array($this));
215             }
216             if (Event::handle('StartShowUAStyles', array($this))) {
217                 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
218                                'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
219                 foreach (array(6,7) as $ver) {
220                     if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
221                         // Yes, IE people should be put in jail.
222                         $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
223                                        'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
224                     }
225                 }
226                 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
227                                'href="'.theme_path('css/ie.css', null).'?version='.LACONICA_VERSION.'" /><![endif]');
228                 Event::handle('EndShowUAStyles', array($this));
229             }
230             Event::handle('EndShowStyles', array($this));
231         }
232     }
233
234     /**
235      * Show javascript headers
236      *
237      * @return nothing
238      */
239     function showScripts()
240     {
241         if (Event::handle('StartShowScripts', array($this))) {
242             if (Event::handle('StartShowJQueryScripts', array($this))) {
243                 $this->element('script', array('type' => 'text/javascript',
244                                                'src' => common_path('js/jquery.min.js')),
245                                ' ');
246                 $this->element('script', array('type' => 'text/javascript',
247                                                'src' => common_path('js/jquery.form.js')),
248                                ' ');
249                 Event::handle('EndShowJQueryScripts', array($this));
250             }
251             if (Event::handle('StartShowLaconicaScripts', array($this))) {
252                 $this->element('script', array('type' => 'text/javascript',
253                                                'src' => common_path('js/xbImportNode.js')),
254                                ' ');
255                 $this->element('script', array('type' => 'text/javascript',
256                                                'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
257                                ' ');
258                 // Frame-busting code to avoid clickjacking attacks.
259                 $this->element('script', array('type' => 'text/javascript'),
260                                'if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
261                 Event::handle('EndShowLaconicaScripts', array($this));
262             }
263             Event::handle('EndShowScripts', array($this));
264         }
265     }
266
267     /**
268      * Show OpenSearch headers
269      *
270      * @return nothing
271      */
272     function showOpenSearch()
273     {
274         $this->element('link', array('rel' => 'search',
275                                      'type' => 'application/opensearchdescription+xml',
276                                      'href' =>  common_local_url('opensearch', array('type' => 'people')),
277                                      'title' => common_config('site', 'name').' People Search'));
278         $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
279                                      'href' =>  common_local_url('opensearch', array('type' => 'notice')),
280                                      'title' => common_config('site', 'name').' Notice Search'));
281     }
282
283     /**
284      * Show feed headers
285      *
286      * MAY overload
287      *
288      * @return nothing
289      */
290
291     function showFeeds()
292     {
293         $feeds = $this->getFeeds();
294
295         if ($feeds) {
296             foreach ($feeds as $feed) {
297                 $this->element('link', array('rel' => $feed->rel(),
298                                              'href' => $feed->url,
299                                              'type' => $feed->mimeType(),
300                                              'title' => $feed->title));
301             }
302         }
303     }
304
305     /**
306      * Show description.
307      *
308      * SHOULD overload
309      *
310      * @return nothing
311      */
312     function showDescription()
313     {
314         // does nothing by default
315     }
316
317     /**
318      * Show extra stuff in <head>.
319      *
320      * MAY overload
321      *
322      * @return nothing
323      */
324     function extraHead()
325     {
326         // does nothing by default
327     }
328
329     /**
330      * Show body.
331      *
332      * Calls template methods
333      *
334      * @return nothing
335      */
336     function showBody()
337     {
338         $this->elementStart('body', (common_current_user()) ? array('id' => $this->trimmed('action'),
339                                                                     'class' => 'user_in')
340                                                             : array('id' => $this->trimmed('action')));
341         $this->elementStart('div', array('id' => 'wrap'));
342         if (Event::handle('StartShowHeader', array($this))) {
343             $this->showHeader();
344             Event::handle('EndShowHeader', array($this));
345         }
346         $this->showCore();
347         if (Event::handle('StartShowFooter', array($this))) {
348             $this->showFooter();
349             Event::handle('EndShowFooter', array($this));
350         }
351         $this->elementEnd('div');
352         $this->elementEnd('body');
353     }
354
355     /**
356      * Show header of the page.
357      *
358      * Calls template methods
359      *
360      * @return nothing
361      */
362     function showHeader()
363     {
364         $this->elementStart('div', array('id' => 'header'));
365         $this->showLogo();
366         $this->showPrimaryNav();
367         $this->showSiteNotice();
368         if (common_logged_in()) {
369             $this->showNoticeForm();
370         } else {
371             $this->showAnonymousMessage();
372         }
373         $this->elementEnd('div');
374     }
375
376     /**
377      * Show configured logo.
378      *
379      * @return nothing
380      */
381     function showLogo()
382     {
383         $this->elementStart('address', array('id' => 'site_contact',
384                                              'class' => 'vcard'));
385         $this->elementStart('a', array('class' => 'url home bookmark',
386                                        'href' => common_local_url('public')));
387         if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
388             $this->element('img', array('class' => 'logo photo',
389                                         'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
390                                         'alt' => common_config('site', 'name')));
391         }
392         $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
393         $this->elementEnd('a');
394         $this->elementEnd('address');
395     }
396
397     /**
398      * Show primary navigation.
399      *
400      * @return nothing
401      */
402     function showPrimaryNav()
403     {
404         $user = common_current_user();
405
406         $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
407         $this->element('dt', null, _('Primary site navigation'));
408         $this->elementStart('dd');
409         $this->elementStart('ul', array('class' => 'nav'));
410         if (Event::handle('StartPrimaryNav', array($this))) {
411             if ($user) {
412                 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
413                                 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
414                 $this->menuItem(common_local_url('profilesettings'),
415                                 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
416                 if (common_config('xmpp', 'enabled')) {
417                     $this->menuItem(common_local_url('imsettings'),
418                                     _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
419                 } else {
420                     $this->menuItem(common_local_url('smssettings'),
421                                     _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
422                 }
423                 $this->menuItem(common_local_url('invite'),
424                                  _('Invite'),
425                                  sprintf(_('Invite friends and colleagues to join you on %s'),
426                                  common_config('site', 'name')),
427                                  false, 'nav_invitecontact');
428                 $this->menuItem(common_local_url('logout'),
429                                 _('Logout'), _('Logout from the site'), false, 'nav_logout');
430             }
431             else {
432                 if (!common_config('site', 'closed')) {
433                     $this->menuItem(common_local_url('register'),
434                                     _('Register'), _('Create an account'), false, 'nav_register');
435                 }
436                 $this->menuItem(common_local_url('openidlogin'),
437                                 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
438                 $this->menuItem(common_local_url('login'),
439                                 _('Login'), _('Login to the site'), false, 'nav_login');
440             }
441             $this->menuItem(common_local_url('doc', array('title' => 'help')),
442                             _('Help'), _('Help me!'), false, 'nav_help');
443             $this->menuItem(common_local_url('peoplesearch'),
444                             _('Search'), _('Search for people or text'), false, 'nav_search');
445             Event::handle('EndPrimaryNav', array($this));
446         }
447         $this->elementEnd('ul');
448         $this->elementEnd('dd');
449         $this->elementEnd('dl');
450     }
451
452     /**
453      * Show site notice.
454      *
455      * @return nothing
456      */
457     function showSiteNotice()
458     {
459         // Revist. Should probably do an hAtom pattern here
460         $text = common_config('site', 'notice');
461         if ($text) {
462             $this->elementStart('dl', array('id' => 'site_notice',
463                                             'class' => 'system_notice'));
464             $this->element('dt', null, _('Site notice'));
465             $this->elementStart('dd', null);
466             $this->raw($text);
467             $this->elementEnd('dd');
468             $this->elementEnd('dl');
469         }
470     }
471
472     /**
473      * Show notice form.
474      *
475      * MAY overload if no notice form needed... or direct message box????
476      *
477      * @return nothing
478      */
479     function showNoticeForm()
480     {
481         $notice_form = new NoticeForm($this);
482         $notice_form->show();
483     }
484
485     /**
486      * Show anonymous message.
487      *
488      * SHOULD overload
489      *
490      * @return nothing
491      */
492     function showAnonymousMessage()
493     {
494         // needs to be defined by the class
495     }
496
497     /**
498      * Show core.
499      *
500      * Shows local navigation, content block and aside.
501      *
502      * @return nothing
503      */
504     function showCore()
505     {
506         $this->elementStart('div', array('id' => 'core'));
507         if (Event::handle('StartShowLocalNavBlock', array($this))) {
508             $this->showLocalNavBlock();
509             Event::handle('EndShowLocalNavBlock', array($this));
510         }
511         if (Event::handle('StartShowContentBlock', array($this))) {
512             $this->showContentBlock();
513             Event::handle('EndShowContentBlock', array($this));
514         }
515         $this->showAside();
516         $this->elementEnd('div');
517     }
518
519     /**
520      * Show local navigation block.
521      *
522      * @return nothing
523      */
524     function showLocalNavBlock()
525     {
526         $this->elementStart('dl', array('id' => 'site_nav_local_views'));
527         $this->element('dt', null, _('Local views'));
528         $this->elementStart('dd');
529         $this->showLocalNav();
530         $this->elementEnd('dd');
531         $this->elementEnd('dl');
532     }
533
534     /**
535      * Show local navigation.
536      *
537      * SHOULD overload
538      *
539      * @return nothing
540      */
541     function showLocalNav()
542     {
543         // does nothing by default
544     }
545
546     /**
547      * Show content block.
548      *
549      * @return nothing
550      */
551     function showContentBlock()
552     {
553         $this->elementStart('div', array('id' => 'content'));
554         $this->showPageTitle();
555         $this->showPageNoticeBlock();
556         $this->elementStart('div', array('id' => 'content_inner'));
557         // show the actual content (forms, lists, whatever)
558         $this->showContent();
559         $this->elementEnd('div');
560         $this->elementEnd('div');
561     }
562
563     /**
564      * Show page title.
565      *
566      * @return nothing
567      */
568     function showPageTitle()
569     {
570         $this->element('h1', null, $this->title());
571     }
572
573     /**
574      * Show page notice block.
575      *
576      * @return nothing
577      */
578     function showPageNoticeBlock()
579     {
580         $this->elementStart('dl', array('id' => 'page_notice',
581                                         'class' => 'system_notice'));
582         $this->element('dt', null, _('Page notice'));
583         $this->elementStart('dd');
584         $this->showPageNotice();
585         $this->elementEnd('dd');
586         $this->elementEnd('dl');
587     }
588
589     /**
590      * Show page notice.
591      *
592      * SHOULD overload (unless there's not a notice)
593      *
594      * @return nothing
595      */
596     function showPageNotice()
597     {
598     }
599
600     /**
601      * Show content.
602      *
603      * MUST overload (unless there's not a notice)
604      *
605      * @return nothing
606      */
607     function showContent()
608     {
609     }
610
611     /**
612      * Show Aside.
613      *
614      * @return nothing
615      */
616
617     function showAside()
618     {
619         $this->elementStart('div', array('id' => 'aside_primary',
620                                          'class' => 'aside'));
621         if (Event::handle('StartShowExportData', array($this))) {
622         $this->showExportData();
623             Event::handle('EndShowExportData', array($this));
624         }
625         if (Event::handle('StartShowSections', array($this))) {
626             $this->showSections();
627             Event::handle('EndShowSections', array($this));
628         }
629         $this->elementEnd('div');
630     }
631
632     /**
633      * Show export data feeds.
634      *
635      * @return void
636      */
637
638     function showExportData()
639     {
640         $feeds = $this->getFeeds();
641         if ($feeds) {
642             $fl = new FeedList($this);
643             $fl->show($feeds);
644         }
645     }
646
647     /**
648      * Show sections.
649      *
650      * SHOULD overload
651      *
652      * @return nothing
653      */
654     function showSections()
655     {
656         // for each section, show it
657     }
658
659     /**
660      * Show footer.
661      *
662      * @return nothing
663      */
664     function showFooter()
665     {
666         $this->elementStart('div', array('id' => 'footer'));
667         $this->showSecondaryNav();
668         $this->showLicenses();
669         $this->elementEnd('div');
670     }
671
672     /**
673      * Show secondary navigation.
674      *
675      * @return nothing
676      */
677     function showSecondaryNav()
678     {
679         $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
680         $this->element('dt', null, _('Secondary site navigation'));
681         $this->elementStart('dd', null);
682         $this->elementStart('ul', array('class' => 'nav'));
683         if (Event::handle('StartSecondaryNav', array($this))) {
684             $this->menuItem(common_local_url('doc', array('title' => 'help')),
685                             _('Help'));
686             $this->menuItem(common_local_url('doc', array('title' => 'about')),
687                             _('About'));
688             $this->menuItem(common_local_url('doc', array('title' => 'faq')),
689                             _('FAQ'));
690             $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
691                             _('Privacy'));
692             $this->menuItem(common_local_url('doc', array('title' => 'source')),
693                             _('Source'));
694             $this->menuItem(common_local_url('doc', array('title' => 'contact')),
695                             _('Contact'));
696             $this->menuItem(common_local_url('doc', array('title' => 'badge')),
697                             _('Badge'));
698             Event::handle('EndSecondaryNav', array($this));
699         }
700         $this->elementEnd('ul');
701         $this->elementEnd('dd');
702         $this->elementEnd('dl');
703     }
704
705     /**
706      * Show licenses.
707      *
708      * @return nothing
709      */
710     function showLicenses()
711     {
712         $this->elementStart('dl', array('id' => 'licenses'));
713         $this->showLaconicaLicense();
714         $this->showContentLicense();
715         $this->elementEnd('dl');
716     }
717
718     /**
719      * Show Laconica license.
720      *
721      * @return nothing
722      */
723     function showLaconicaLicense()
724     {
725         $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
726         $this->elementStart('dd', null);
727         if (common_config('site', 'broughtby')) {
728             $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
729         } else {
730             $instr = _('**%%site.name%%** is a microblogging service. ');
731         }
732         $instr .= sprintf(_('It runs the [Laconica](http://laconi.ca/) microblogging software, version %s, available under the [GNU Affero General Public License](http://www.fsf.org/licensing/licenses/agpl-3.0.html).'), LACONICA_VERSION);
733         $output = common_markup_to_html($instr);
734         $this->raw($output);
735         $this->elementEnd('dd');
736         // do it
737     }
738
739     /**
740      * Show content license.
741      *
742      * @return nothing
743      */
744     function showContentLicense()
745     {
746         $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
747         $this->elementStart('dd', array('id' => 'site_content_license_cc'));
748         $this->elementStart('p');
749         $this->element('img', array('id' => 'license_cc',
750                                     'src' => common_config('license', 'image'),
751                                     'alt' => common_config('license', 'title')));
752         //TODO: This is dirty: i18n
753         $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
754         $this->element('a', array('class' => 'license',
755                                   'rel' => 'external license',
756                                   'href' => common_config('license', 'url')),
757                        common_config('license', 'title'));
758         $this->text(_('license.'));
759         $this->elementEnd('p');
760         $this->elementEnd('dd');
761     }
762
763     /**
764      * Return last modified, if applicable.
765      *
766      * MAY override
767      *
768      * @return string last modified http header
769      */
770     function lastModified()
771     {
772         // For comparison with If-Last-Modified
773         // If not applicable, return null
774         return null;
775     }
776
777     /**
778      * Return etag, if applicable.
779      *
780      * MAY override
781      *
782      * @return string etag http header
783      */
784     function etag()
785     {
786         return null;
787     }
788
789     /**
790      * Return true if read only.
791      *
792      * MAY override
793      *
794      * @param array $args other arguments
795      *
796      * @return boolean is read only action?
797      */
798
799     function isReadOnly($args)($args)
800     {
801         return false;
802     }
803
804     /**
805      * Returns query argument or default value if not found
806      *
807      * @param string $key requested argument
808      * @param string $def default value to return if $key is not provided
809      *
810      * @return boolean is read only action?
811      */
812     function arg($key, $def=null)
813     {
814         if (array_key_exists($key, $this->args)) {
815             return $this->args[$key];
816         } else {
817             return $def;
818         }
819     }
820
821     /**
822      * Returns trimmed query argument or default value if not found
823      *
824      * @param string $key requested argument
825      * @param string $def default value to return if $key is not provided
826      *
827      * @return boolean is read only action?
828      */
829     function trimmed($key, $def=null)
830     {
831         $arg = $this->arg($key, $def);
832         return is_string($arg) ? trim($arg) : $arg;
833     }
834
835     /**
836      * Handler method
837      *
838      * @param array $argarray is ignored since it's now passed in in prepare()
839      *
840      * @return boolean is read only action?
841      */
842     function handle($argarray=null)
843     {
844         $lm   = $this->lastModified();
845         $etag = $this->etag();
846         if ($etag) {
847             header('ETag: ' . $etag);
848         }
849         if ($lm) {
850             header('Last-Modified: ' . date(DATE_RFC1123, $lm));
851             if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
852                 $ims = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
853                 if ($lm <= $ims) {
854                     $if_none_match = $_SERVER['HTTP_IF_NONE_MATCH'];
855                     if (!$if_none_match ||
856                         !$etag ||
857                         $this->_hasEtag($etag, $if_none_match)) {
858                         header('HTTP/1.1 304 Not Modified');
859                         // Better way to do this?
860                         exit(0);
861                     }
862                 }
863             }
864         }
865     }
866
867     /**
868      * HasĀ etag? (private)
869      *
870      * @param string $etag          etag http header
871      * @param string $if_none_match ifNoneMatch http header
872      *
873      * @return boolean
874      */
875
876     function _hasEtag($etag, $if_none_match)
877     {
878         $etags = explode(',', $if_none_match);
879         return in_array($etag, $etags) || in_array('*', $etags);
880     }
881
882     /**
883      * Boolean understands english (yes, no, true, false)
884      *
885      * @param string $key query key we're interested in
886      * @param string $def default value
887      *
888      * @return boolean interprets yes/no strings as boolean
889      */
890     function boolean($key, $def=false)
891     {
892         $arg = strtolower($this->trimmed($key));
893
894         if (is_null($arg)) {
895             return $def;
896         } else if (in_array($arg, array('true', 'yes', '1'))) {
897             return true;
898         } else if (in_array($arg, array('false', 'no', '0'))) {
899             return false;
900         } else {
901             return $def;
902         }
903     }
904
905     /**
906      * Server error
907      *
908      * @param string  $msg  error message to display
909      * @param integer $code http error code, 500 by default
910      *
911      * @return nothing
912      */
913
914     function serverError($msg, $code=500)
915     {
916         $action = $this->trimmed('action');
917         common_debug("Server error '$code' on '$action': $msg", __FILE__);
918         throw new ServerException($msg, $code);
919     }
920
921     /**
922      * Client error
923      *
924      * @param string  $msg  error message to display
925      * @param integer $code http error code, 400 by default
926      *
927      * @return nothing
928      */
929
930     function clientError($msg, $code=400)
931     {
932         $action = $this->trimmed('action');
933         common_debug("User error '$code' on '$action': $msg", __FILE__);
934         throw new ClientException($msg, $code);
935     }
936
937     /**
938      * Returns the current URL
939      *
940      * @return string current URL
941      */
942
943     function selfUrl()
944     {
945         $action = $this->trimmed('action');
946         $args   = $this->args;
947         unset($args['action']);
948         if (array_key_exists('submit', $args)) {
949             unset($args['submit']);
950         }
951         foreach (array_keys($_COOKIE) as $cookie) {
952             unset($args[$cookie]);
953         }
954         return common_local_url($action, $args);
955     }
956
957     /**
958      * Generate a menu item
959      *
960      * @param string  $url         menu URL
961      * @param string  $text        menu name
962      * @param string  $title       title attribute, null by default
963      * @param boolean $is_selected current menu item, false by default
964      * @param string  $id          element id, null by default
965      *
966      * @return nothing
967      */
968     function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
969     {
970         // Added @id to li for some control.
971         // XXX: We might want to move this to htmloutputter.php
972         $lattrs = array();
973         if ($is_selected) {
974             $lattrs['class'] = 'current';
975         }
976
977         (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
978
979         $this->elementStart('li', $lattrs);
980         $attrs['href'] = $url;
981         if ($title) {
982             $attrs['title'] = $title;
983         }
984         $this->element('a', $attrs, $text);
985         $this->elementEnd('li');
986     }
987
988     /**
989      * Generate pagination links
990      *
991      * @param boolean $have_before is there something before?
992      * @param boolean $have_after  is there something after?
993      * @param integer $page        current page
994      * @param string  $action      current action
995      * @param array   $args        rest of query arguments
996      *
997      * @return nothing
998      */
999     function pagination($have_before, $have_after, $page, $action, $args=null)
1000     {
1001         // Does a little before-after block for next/prev page
1002         if ($have_before || $have_after) {
1003             $this->elementStart('div', array('class' => 'pagination'));
1004             $this->elementStart('dl', null);
1005             $this->element('dt', null, _('Pagination'));
1006             $this->elementStart('dd', null);
1007             $this->elementStart('ul', array('class' => 'nav'));
1008         }
1009         if ($have_before) {
1010             $pargs   = array('page' => $page-1);
1011             $this->elementStart('li', array('class' => 'nav_prev'));
1012             $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1013                                       'rel' => 'prev'),
1014                            _('After'));
1015             $this->elementEnd('li');
1016         }
1017         if ($have_after) {
1018             $pargs   = array('page' => $page+1);
1019             $this->elementStart('li', array('class' => 'nav_next'));
1020             $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1021                                       'rel' => 'next'),
1022                            _('Before'));
1023             $this->elementEnd('li');
1024         }
1025         if ($have_before || $have_after) {
1026             $this->elementEnd('ul');
1027             $this->elementEnd('dd');
1028             $this->elementEnd('dl');
1029             $this->elementEnd('div');
1030         }
1031     }
1032
1033     /**
1034      * An array of feeds for this action.
1035      *
1036      * Returns an array of potential feeds for this action.
1037      *
1038      * @return array Feed object to show in head and links
1039      */
1040
1041     function getFeeds()
1042     {
1043         return null;
1044     }
1045 }