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