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