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