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