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