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