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