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