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