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