]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/action.php
Merge branch '0.7.x' into 0.8.x
[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', null) . '?version=' . LACONICA_VERSION,
198                                              'media' => 'screen, projection, tv'));
199                 if (common_config('site', 'mobile')) {
200                     $this->element('link', array('rel' => 'stylesheet',
201                                                  'type' => 'text/css',
202                                                  'href' => theme_path('css/mobile.css', 'base') . '?version=' . LACONICA_VERSION,
203                                                  // TODO: "handheld" CSS for other mobile devices
204                                                  'media' => 'only screen and (max-device-width: 480px)')); // Mobile WebKit
205                 }
206                 $this->element('link', array('rel' => 'stylesheet',
207                                              'type' => 'text/css',
208                                              'href' => theme_path('css/print.css', 'base') . '?version=' . LACONICA_VERSION,
209                                              'media' => 'print'));
210                 Event::handle('EndShowLaconicaStyles', array($this));
211             }
212             if (Event::handle('StartShowUAStyles', array($this))) {
213                 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
214                                'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
215                 foreach (array(6,7) as $ver) {
216                     if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
217                         // Yes, IE people should be put in jail.
218                         $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
219                                        'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
220                     }
221                 }
222                 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
223                                'href="'.theme_path('css/ie.css', null).'?version='.LACONICA_VERSION.'" /><![endif]');
224                 Event::handle('EndShowUAStyles', array($this));
225             }
226             Event::handle('EndShowStyles', array($this));
227         }
228     }
229
230     /**
231      * Show javascript headers
232      *
233      * @return nothing
234      */
235     function showScripts()
236     {
237         if (Event::handle('StartShowScripts', array($this))) {
238             if (Event::handle('StartShowJQueryScripts', array($this))) {
239                 $this->element('script', array('type' => 'text/javascript',
240                                                'src' => common_path('js/jquery.min.js')),
241                                ' ');
242                 $this->element('script', array('type' => 'text/javascript',
243                                                'src' => common_path('js/jquery.form.js')),
244                                ' ');
245                 Event::handle('EndShowJQueryScripts', array($this));
246             }
247             if (Event::handle('StartShowLaconicaScripts', array($this))) {
248                 $this->element('script', array('type' => 'text/javascript',
249                                                'src' => common_path('js/xbImportNode.js')),
250                                ' ');
251                 $this->element('script', array('type' => 'text/javascript',
252                                                'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
253                                ' ');
254                 // Frame-busting code to avoid clickjacking attacks.
255                 $this->element('script', array('type' => 'text/javascript'),
256                                'if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
257                 Event::handle('EndShowLaconicaScripts', array($this));
258             }
259             Event::handle('EndShowScripts', array($this));
260         }
261     }
262
263     /**
264      * Show OpenSearch headers
265      *
266      * @return nothing
267      */
268     function showOpenSearch()
269     {
270         $this->element('link', array('rel' => 'search',
271                                      'type' => 'application/opensearchdescription+xml',
272                                      'href' =>  common_local_url('opensearch', array('type' => 'people')),
273                                      'title' => common_config('site', 'name').' People Search'));
274         $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
275                                      'href' =>  common_local_url('opensearch', array('type' => 'notice')),
276                                      'title' => common_config('site', 'name').' Notice Search'));
277     }
278
279     /**
280      * Show feed headers
281      *
282      * MAY overload
283      *
284      * @return nothing
285      */
286
287     function showFeeds()
288     {
289         $feeds = $this->getFeeds();
290
291         if ($feeds) {
292             foreach ($feeds as $feed) {
293                 $this->element('link', array('rel' => $feed->rel(),
294                                              'href' => $feed->url,
295                                              'type' => $feed->mimeType(),
296                                              'title' => $feed->title));
297             }
298         }
299     }
300
301     /**
302      * Show description.
303      *
304      * SHOULD overload
305      *
306      * @return nothing
307      */
308     function showDescription()
309     {
310         // does nothing by default
311     }
312
313     /**
314      * Show extra stuff in <head>.
315      *
316      * MAY overload
317      *
318      * @return nothing
319      */
320     function extraHead()
321     {
322         // does nothing by default
323     }
324
325     /**
326      * Show body.
327      *
328      * Calls template methods
329      *
330      * @return nothing
331      */
332     function showBody()
333     {
334         $this->elementStart('body', (common_current_user()) ? array('id' => $this->trimmed('action'),
335                                                                     'class' => 'user_in')
336                                                             : array('id' => $this->trimmed('action')));
337         $this->elementStart('div', array('id' => 'wrap'));
338         if (Event::handle('StartShowHeader', array($this))) {
339             $this->showHeader();
340             Event::handle('EndShowHeader', array($this));
341         }
342         $this->showCore();
343         if (Event::handle('StartShowFooter', array($this))) {
344             $this->showFooter();
345             Event::handle('EndShowFooter', array($this));
346         }
347         $this->elementEnd('div');
348         $this->elementEnd('body');
349     }
350
351     /**
352      * Show header of the page.
353      *
354      * Calls template methods
355      *
356      * @return nothing
357      */
358     function showHeader()
359     {
360         $this->elementStart('div', array('id' => 'header'));
361         $this->showLogo();
362         $this->showPrimaryNav();
363         $this->showSiteNotice();
364         if (common_logged_in()) {
365             $this->showNoticeForm();
366         } else {
367             $this->showAnonymousMessage();
368         }
369         $this->elementEnd('div');
370     }
371
372     /**
373      * Show configured logo.
374      *
375      * @return nothing
376      */
377     function showLogo()
378     {
379         $this->elementStart('address', array('id' => 'site_contact',
380                                              'class' => 'vcard'));
381         $this->elementStart('a', array('class' => 'url home bookmark',
382                                        'href' => common_local_url('public')));
383         if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
384             $this->element('img', array('class' => 'logo photo',
385                                         'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
386                                         'alt' => common_config('site', 'name')));
387         }
388         $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
389         $this->elementEnd('a');
390         $this->elementEnd('address');
391     }
392
393     /**
394      * Show primary navigation.
395      *
396      * @return nothing
397      */
398     function showPrimaryNav()
399     {
400         $user = common_current_user();
401
402         $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
403         $this->element('dt', null, _('Primary site navigation'));
404         $this->elementStart('dd');
405         $this->elementStart('ul', array('class' => 'nav'));
406         if (Event::handle('StartPrimaryNav', array($this))) {
407             if ($user) {
408                 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
409                                 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
410                 $this->menuItem(common_local_url('profilesettings'),
411                                 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
412                 if (common_config('xmpp', 'enabled')) {
413                     $this->menuItem(common_local_url('imsettings'),
414                                     _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
415                 } else {
416                     $this->menuItem(common_local_url('smssettings'),
417                                     _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
418                 }
419                 $this->menuItem(common_local_url('invite'),
420                                  _('Invite'),
421                                  sprintf(_('Invite friends and colleagues to join you on %s'),
422                                  common_config('site', 'name')),
423                                  false, 'nav_invitecontact');
424                 $this->menuItem(common_local_url('logout'),
425                                 _('Logout'), _('Logout from the site'), false, 'nav_logout');
426             }
427             else {
428                 if (!common_config('site', 'closed')) {
429                     $this->menuItem(common_local_url('register'),
430                                     _('Register'), _('Create an account'), false, 'nav_register');
431                 }
432                 $this->menuItem(common_local_url('openidlogin'),
433                                 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
434                 $this->menuItem(common_local_url('login'),
435                                 _('Login'), _('Login to the site'), false, 'nav_login');
436             }
437             $this->menuItem(common_local_url('doc', array('title' => 'help')),
438                             _('Help'), _('Help me!'), false, 'nav_help');
439             $this->menuItem(common_local_url('peoplesearch'),
440                             _('Search'), _('Search for people or text'), false, 'nav_search');
441             Event::handle('EndPrimaryNav', array($this));
442         }
443         $this->elementEnd('ul');
444         $this->elementEnd('dd');
445         $this->elementEnd('dl');
446     }
447
448     /**
449      * Show site notice.
450      *
451      * @return nothing
452      */
453     function showSiteNotice()
454     {
455         // Revist. Should probably do an hAtom pattern here
456         $text = common_config('site', 'notice');
457         if ($text) {
458             $this->elementStart('dl', array('id' => 'site_notice',
459                                             'class' => 'system_notice'));
460             $this->element('dt', null, _('Site notice'));
461             $this->elementStart('dd', null);
462             $this->raw($text);
463             $this->elementEnd('dd');
464             $this->elementEnd('dl');
465         }
466     }
467
468     /**
469      * Show notice form.
470      *
471      * MAY overload if no notice form needed... or direct message box????
472      *
473      * @return nothing
474      */
475     function showNoticeForm()
476     {
477         $notice_form = new NoticeForm($this);
478         $notice_form->show();
479     }
480
481     /**
482      * Show anonymous message.
483      *
484      * SHOULD overload
485      *
486      * @return nothing
487      */
488     function showAnonymousMessage()
489     {
490         // needs to be defined by the class
491     }
492
493     /**
494      * Show core.
495      *
496      * Shows local navigation, content block and aside.
497      *
498      * @return nothing
499      */
500     function showCore()
501     {
502         $this->elementStart('div', array('id' => 'core'));
503         if (Event::handle('StartShowLocalNavBlock', array($this))) {
504             $this->showLocalNavBlock();
505             Event::handle('EndShowLocalNavBlock', array($this));
506         }
507         if (Event::handle('StartShowContentBlock', array($this))) {
508             $this->showContentBlock();
509             Event::handle('EndShowContentBlock', array($this));
510         }
511         $this->showAside();
512         $this->elementEnd('div');
513     }
514
515     /**
516      * Show local navigation block.
517      *
518      * @return nothing
519      */
520     function showLocalNavBlock()
521     {
522         $this->elementStart('dl', array('id' => 'site_nav_local_views'));
523         $this->element('dt', null, _('Local views'));
524         $this->elementStart('dd');
525         $this->showLocalNav();
526         $this->elementEnd('dd');
527         $this->elementEnd('dl');
528     }
529
530     /**
531      * Show local navigation.
532      *
533      * SHOULD overload
534      *
535      * @return nothing
536      */
537     function showLocalNav()
538     {
539         // does nothing by default
540     }
541
542     /**
543      * Show content block.
544      *
545      * @return nothing
546      */
547     function showContentBlock()
548     {
549         $this->elementStart('div', array('id' => 'content'));
550         $this->showPageTitle();
551         $this->showPageNoticeBlock();
552         $this->elementStart('div', array('id' => 'content_inner'));
553         // show the actual content (forms, lists, whatever)
554         $this->showContent();
555         $this->elementEnd('div');
556         $this->elementEnd('div');
557     }
558
559     /**
560      * Show page title.
561      *
562      * @return nothing
563      */
564     function showPageTitle()
565     {
566         $this->element('h1', null, $this->title());
567     }
568
569     /**
570      * Show page notice block.
571      *
572      * @return nothing
573      */
574     function showPageNoticeBlock()
575     {
576         $this->elementStart('dl', array('id' => 'page_notice',
577                                         'class' => 'system_notice'));
578         $this->element('dt', null, _('Page notice'));
579         $this->elementStart('dd');
580         $this->showPageNotice();
581         $this->elementEnd('dd');
582         $this->elementEnd('dl');
583     }
584
585     /**
586      * Show page notice.
587      *
588      * SHOULD overload (unless there's not a notice)
589      *
590      * @return nothing
591      */
592     function showPageNotice()
593     {
594     }
595
596     /**
597      * Show content.
598      *
599      * MUST overload (unless there's not a notice)
600      *
601      * @return nothing
602      */
603     function showContent()
604     {
605     }
606
607     /**
608      * Show Aside.
609      *
610      * @return nothing
611      */
612
613     function showAside()
614     {
615         $this->elementStart('div', array('id' => 'aside_primary',
616                                          'class' => 'aside'));
617         if (Event::handle('StartShowExportData', array($this))) {
618         $this->showExportData();
619             Event::handle('EndShowExportData', array($this));
620         }
621         if (Event::handle('StartShowSections', array($this))) {
622             $this->showSections();
623             Event::handle('EndShowSections', array($this));
624         }
625         $this->elementEnd('div');
626     }
627
628     /**
629      * Show export data feeds.
630      *
631      * @return void
632      */
633
634     function showExportData()
635     {
636         $feeds = $this->getFeeds();
637         if ($feeds) {
638             $fl = new FeedList($this);
639             $fl->show($feeds);
640         }
641     }
642
643     /**
644      * Show sections.
645      *
646      * SHOULD overload
647      *
648      * @return nothing
649      */
650     function showSections()
651     {
652         // for each section, show it
653     }
654
655     /**
656      * Show footer.
657      *
658      * @return nothing
659      */
660     function showFooter()
661     {
662         $this->elementStart('div', array('id' => 'footer'));
663         $this->showSecondaryNav();
664         $this->showLicenses();
665         $this->elementEnd('div');
666     }
667
668     /**
669      * Show secondary navigation.
670      *
671      * @return nothing
672      */
673     function showSecondaryNav()
674     {
675         $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
676         $this->element('dt', null, _('Secondary site navigation'));
677         $this->elementStart('dd', null);
678         $this->elementStart('ul', array('class' => 'nav'));
679         if (Event::handle('StartSecondaryNav', array($this))) {
680             $this->menuItem(common_local_url('doc', array('title' => 'help')),
681                             _('Help'));
682             $this->menuItem(common_local_url('doc', array('title' => 'about')),
683                             _('About'));
684             $this->menuItem(common_local_url('doc', array('title' => 'faq')),
685                             _('FAQ'));
686             $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
687                             _('Privacy'));
688             $this->menuItem(common_local_url('doc', array('title' => 'source')),
689                             _('Source'));
690             $this->menuItem(common_local_url('doc', array('title' => 'contact')),
691                             _('Contact'));
692             $this->menuItem(common_local_url('doc', array('title' => 'badge')),
693                             _('Badge'));
694             Event::handle('EndSecondaryNav', array($this));
695         }
696         $this->elementEnd('ul');
697         $this->elementEnd('dd');
698         $this->elementEnd('dl');
699     }
700
701     /**
702      * Show licenses.
703      *
704      * @return nothing
705      */
706     function showLicenses()
707     {
708         $this->elementStart('dl', array('id' => 'licenses'));
709         $this->showLaconicaLicense();
710         $this->showContentLicense();
711         $this->elementEnd('dl');
712     }
713
714     /**
715      * Show Laconica license.
716      *
717      * @return nothing
718      */
719     function showLaconicaLicense()
720     {
721         $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
722         $this->elementStart('dd', null);
723         if (common_config('site', 'broughtby')) {
724             $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
725         } else {
726             $instr = _('**%%site.name%%** is a microblogging service. ');
727         }
728         $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);
729         $output = common_markup_to_html($instr);
730         $this->raw($output);
731         $this->elementEnd('dd');
732         // do it
733     }
734
735     /**
736      * Show content license.
737      *
738      * @return nothing
739      */
740     function showContentLicense()
741     {
742         $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
743         $this->elementStart('dd', array('id' => 'site_content_license_cc'));
744         $this->elementStart('p');
745         $this->element('img', array('id' => 'license_cc',
746                                     'src' => common_config('license', 'image'),
747                                     'alt' => common_config('license', 'title')));
748         //TODO: This is dirty: i18n
749         $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
750         $this->element('a', array('class' => 'license',
751                                   'rel' => 'external license',
752                                   'href' => common_config('license', 'url')),
753                        common_config('license', 'title'));
754         $this->text(_('license.'));
755         $this->elementEnd('p');
756         $this->elementEnd('dd');
757     }
758
759     /**
760      * Return last modified, if applicable.
761      *
762      * MAY override
763      *
764      * @return string last modified http header
765      */
766     function lastModified()
767     {
768         // For comparison with If-Last-Modified
769         // If not applicable, return null
770         return null;
771     }
772
773     /**
774      * Return etag, if applicable.
775      *
776      * MAY override
777      *
778      * @return string etag http header
779      */
780     function etag()
781     {
782         return null;
783     }
784
785     /**
786      * Return true if read only.
787      *
788      * MAY override
789      *
790      * @param array $args other arguments
791      *
792      * @return boolean is read only action?
793      */
794
795     function isReadOnly($args)
796     {
797         return false;
798     }
799
800     /**
801      * Returns query argument or default value if not found
802      *
803      * @param string $key requested argument
804      * @param string $def default value to return if $key is not provided
805      *
806      * @return boolean is read only action?
807      */
808     function arg($key, $def=null)
809     {
810         if (array_key_exists($key, $this->args)) {
811             return $this->args[$key];
812         } else {
813             return $def;
814         }
815     }
816
817     /**
818      * Returns trimmed query argument or default value if not found
819      *
820      * @param string $key requested argument
821      * @param string $def default value to return if $key is not provided
822      *
823      * @return boolean is read only action?
824      */
825     function trimmed($key, $def=null)
826     {
827         $arg = $this->arg($key, $def);
828         return is_string($arg) ? trim($arg) : $arg;
829     }
830
831     /**
832      * Handler method
833      *
834      * @param array $argarray is ignored since it's now passed in in prepare()
835      *
836      * @return boolean is read only action?
837      */
838     function handle($argarray=null)
839     {
840         $lm   = $this->lastModified();
841         $etag = $this->etag();
842         if ($etag) {
843             header('ETag: ' . $etag);
844         }
845         if ($lm) {
846             header('Last-Modified: ' . date(DATE_RFC1123, $lm));
847             if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
848                 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
849                 $ims = strtotime($if_modified_since);
850                 if ($lm <= $ims) {
851                     $if_none_match = (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) ?
852                       $_SERVER['HTTP_IF_NONE_MATCH'] : null;
853                     if (!$if_none_match ||
854                         !$etag ||
855                         $this->_hasEtag($etag, $if_none_match)) {
856                         header('HTTP/1.1 304 Not Modified');
857                         // Better way to do this?
858                         exit(0);
859                     }
860                 }
861             }
862         }
863     }
864
865     /**
866      * HasĀ etag? (private)
867      *
868      * @param string $etag          etag http header
869      * @param string $if_none_match ifNoneMatch http header
870      *
871      * @return boolean
872      */
873
874     function _hasEtag($etag, $if_none_match)
875     {
876         $etags = explode(',', $if_none_match);
877         return in_array($etag, $etags) || in_array('*', $etags);
878     }
879
880     /**
881      * Boolean understands english (yes, no, true, false)
882      *
883      * @param string $key query key we're interested in
884      * @param string $def default value
885      *
886      * @return boolean interprets yes/no strings as boolean
887      */
888     function boolean($key, $def=false)
889     {
890         $arg = strtolower($this->trimmed($key));
891
892         if (is_null($arg)) {
893             return $def;
894         } else if (in_array($arg, array('true', 'yes', '1'))) {
895             return true;
896         } else if (in_array($arg, array('false', 'no', '0'))) {
897             return false;
898         } else {
899             return $def;
900         }
901     }
902
903     /**
904      * Server error
905      *
906      * @param string  $msg  error message to display
907      * @param integer $code http error code, 500 by default
908      *
909      * @return nothing
910      */
911
912     function serverError($msg, $code=500)
913     {
914         $action = $this->trimmed('action');
915         common_debug("Server error '$code' on '$action': $msg", __FILE__);
916         throw new ServerException($msg, $code);
917     }
918
919     /**
920      * Client error
921      *
922      * @param string  $msg  error message to display
923      * @param integer $code http error code, 400 by default
924      *
925      * @return nothing
926      */
927
928     function clientError($msg, $code=400)
929     {
930         $action = $this->trimmed('action');
931         common_debug("User error '$code' on '$action': $msg", __FILE__);
932         throw new ClientException($msg, $code);
933     }
934
935     /**
936      * Returns the current URL
937      *
938      * @return string current URL
939      */
940
941     function selfUrl()
942     {
943         $action = $this->trimmed('action');
944         $args   = $this->args;
945         unset($args['action']);
946         if (array_key_exists('submit', $args)) {
947             unset($args['submit']);
948         }
949         foreach (array_keys($_COOKIE) as $cookie) {
950             unset($args[$cookie]);
951         }
952         return common_local_url($action, $args);
953     }
954
955     /**
956      * Generate a menu item
957      *
958      * @param string  $url         menu URL
959      * @param string  $text        menu name
960      * @param string  $title       title attribute, null by default
961      * @param boolean $is_selected current menu item, false by default
962      * @param string  $id          element id, null by default
963      *
964      * @return nothing
965      */
966     function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
967     {
968         // Added @id to li for some control.
969         // XXX: We might want to move this to htmloutputter.php
970         $lattrs = array();
971         if ($is_selected) {
972             $lattrs['class'] = 'current';
973         }
974
975         (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
976
977         $this->elementStart('li', $lattrs);
978         $attrs['href'] = $url;
979         if ($title) {
980             $attrs['title'] = $title;
981         }
982         $this->element('a', $attrs, $text);
983         $this->elementEnd('li');
984     }
985
986     /**
987      * Generate pagination links
988      *
989      * @param boolean $have_before is there something before?
990      * @param boolean $have_after  is there something after?
991      * @param integer $page        current page
992      * @param string  $action      current action
993      * @param array   $args        rest of query arguments
994      *
995      * @return nothing
996      */
997     function pagination($have_before, $have_after, $page, $action, $args=null)
998     {
999         // Does a little before-after block for next/prev page
1000         if ($have_before || $have_after) {
1001             $this->elementStart('div', array('class' => 'pagination'));
1002             $this->elementStart('dl', null);
1003             $this->element('dt', null, _('Pagination'));
1004             $this->elementStart('dd', null);
1005             $this->elementStart('ul', array('class' => 'nav'));
1006         }
1007         if ($have_before) {
1008             $pargs   = array('page' => $page-1);
1009             $this->elementStart('li', array('class' => 'nav_prev'));
1010             $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1011                                       'rel' => 'prev'),
1012                            _('After'));
1013             $this->elementEnd('li');
1014         }
1015         if ($have_after) {
1016             $pargs   = array('page' => $page+1);
1017             $this->elementStart('li', array('class' => 'nav_next'));
1018             $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1019                                       'rel' => 'next'),
1020                            _('Before'));
1021             $this->elementEnd('li');
1022         }
1023         if ($have_before || $have_after) {
1024             $this->elementEnd('ul');
1025             $this->elementEnd('dd');
1026             $this->elementEnd('dl');
1027             $this->elementEnd('div');
1028         }
1029     }
1030
1031     /**
1032      * An array of feeds for this action.
1033      *
1034      * Returns an array of potential feeds for this action.
1035      *
1036      * @return array Feed object to show in head and links
1037      */
1038
1039     function getFeeds()
1040     {
1041         return null;
1042     }
1043 }