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