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