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