]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/action.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / lib / action.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Base class for all actions (~views)
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Action
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/noticeform.php';
36 require_once INSTALLDIR.'/lib/htmloutputter.php';
37
38 /**
39  * Base class for all actions
40  *
41  * This is the base class for all actions in the package. An action is
42  * more or less a "view" in an MVC framework.
43  *
44  * Actions are responsible for extracting and validating parameters; using
45  * model classes to read and write to the database; and doing ouput.
46  *
47  * @category Output
48  * @package  StatusNet
49  * @author   Evan Prodromou <evan@status.net>
50  * @author   Sarven Capadisli <csarven@status.net>
51  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
52  * @link     http://status.net/
53  *
54  * @see      HTMLOutputter
55  */
56 class Action extends HTMLOutputter // lawsuit
57 {
58     var $args;
59
60     /**
61      * Constructor
62      *
63      * Just wraps the HTMLOutputter constructor.
64      *
65      * @param string  $output URI to output to, default = stdout
66      * @param boolean $indent Whether to indent output, default true
67      *
68      * @see XMLOutputter::__construct
69      * @see HTMLOutputter::__construct
70      */
71     function __construct($output='php://output', $indent=null)
72     {
73         parent::__construct($output, $indent);
74     }
75
76     /**
77      * For initializing members of the class.
78      *
79      * @param array $argarray misc. arguments
80      *
81      * @return boolean true
82      */
83     function prepare($argarray)
84     {
85         $this->args =& common_copy_args($argarray);
86         return true;
87     }
88
89     /**
90      * Show page, a template method.
91      *
92      * @return nothing
93      */
94     function showPage()
95     {
96         if (Event::handle('StartShowHTML', array($this))) {
97             $this->startHTML();
98             Event::handle('EndShowHTML', array($this));
99         }
100         if (Event::handle('StartShowHead', array($this))) {
101             $this->showHead();
102             Event::handle('EndShowHead', array($this));
103         }
104         if (Event::handle('StartShowBody', array($this))) {
105             $this->showBody();
106             Event::handle('EndShowBody', array($this));
107         }
108         if (Event::handle('StartEndHTML', array($this))) {
109             $this->endHTML();
110             Event::handle('EndEndHTML', array($this));
111         }
112     }
113
114     /**
115      * Show head, a template method.
116      *
117      * @return nothing
118      */
119     function showHead()
120     {
121         // XXX: attributes (profile?)
122         $this->elementStart('head');
123         if (Event::handle('StartShowHeadElements', array($this))) {
124             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     function showFeeds()
318     {
319         $feeds = $this->getFeeds();
320
321         if ($feeds) {
322             foreach ($feeds as $feed) {
323                 $this->element('link', array('rel' => $feed->rel(),
324                                              'href' => $feed->url,
325                                              'type' => $feed->mimeType(),
326                                              'title' => $feed->title));
327             }
328         }
329     }
330
331     /**
332      * Show description.
333      *
334      * SHOULD overload
335      *
336      * @return nothing
337      */
338     function showDescription()
339     {
340         // does nothing by default
341     }
342
343     /**
344      * Show extra stuff in <head>.
345      *
346      * MAY overload
347      *
348      * @return nothing
349      */
350     function extraHead()
351     {
352         // does nothing by default
353     }
354
355     /**
356      * Show body.
357      *
358      * Calls template methods
359      *
360      * @return nothing
361      */
362     function showBody()
363     {
364         $this->elementStart('body', (common_current_user()) ? array('id' => $this->trimmed('action'),
365                                                                     'class' => 'user_in')
366                             : array('id' => $this->trimmed('action')));
367         $this->elementStart('div', array('id' => 'wrap'));
368         if (Event::handle('StartShowHeader', array($this))) {
369             $this->showHeader();
370             Event::handle('EndShowHeader', array($this));
371         }
372         $this->showCore();
373         if (Event::handle('StartShowFooter', array($this))) {
374             $this->showFooter();
375             Event::handle('EndShowFooter', array($this));
376         }
377         $this->elementEnd('div');
378         $this->showScripts();
379         $this->elementEnd('body');
380     }
381
382     /**
383      * Show header of the page.
384      *
385      * Calls template methods
386      *
387      * @return nothing
388      */
389     function showHeader()
390     {
391         $this->elementStart('div', array('id' => 'header'));
392         $this->showLogo();
393         $this->showPrimaryNav();
394         if (Event::handle('StartShowSiteNotice', array($this))) {
395             $this->showSiteNotice();
396
397             Event::handle('EndShowSiteNotice', array($this));
398         }
399         if (common_logged_in()) {
400             $this->showNoticeForm();
401         } else {
402             $this->showAnonymousMessage();
403         }
404         $this->elementEnd('div');
405     }
406
407     /**
408      * Show configured logo.
409      *
410      * @return nothing
411      */
412     function showLogo()
413     {
414         $this->elementStart('address', array('id' => 'site_contact',
415                                              'class' => 'vcard'));
416         if (Event::handle('StartAddressData', array($this))) {
417             if (common_config('singleuser', 'enabled')) {
418                 $url = common_local_url('showstream',
419                                         array('nickname' => common_config('singleuser', 'nickname')));
420             } else {
421                 $url = common_local_url('public');
422             }
423             $this->elementStart('a', array('class' => 'url home bookmark',
424                                            'href' => $url));
425             if (common_config('site', 'logo') || file_exists(Theme::file('logo.png'))) {
426                 $this->element('img', array('class' => 'logo photo',
427                                             'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : Theme::path('logo.png'),
428                                             'alt' => common_config('site', 'name')));
429             }
430             $this->text(' ');
431             $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
432             $this->elementEnd('a');
433             Event::handle('EndAddressData', array($this));
434         }
435         $this->elementEnd('address');
436     }
437
438     /**
439      * Show primary navigation.
440      *
441      * @return nothing
442      */
443     function showPrimaryNav()
444     {
445         $user = common_current_user();
446         $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
447         // TRANS: DT element for primary navigation menu. String is hidden in default CSS.
448         $this->element('dt', null, _('Primary site navigation'));
449         $this->elementStart('dd');
450         $this->elementStart('ul', array('class' => 'nav'));
451         if (Event::handle('StartPrimaryNav', array($this))) {
452             if ($user) {
453                 // TRANS: Tooltip for main menu option "Personal"
454                 $tooltip = _m('TOOLTIP', 'Personal profile and friends timeline');
455                 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
456                                 // TRANS: Main menu option when logged in for access to personal profile and friends timeline
457                                 _m('MENU', 'Personal'), $tooltip, false, 'nav_home');
458                 // TRANS: Tooltip for main menu option "Account"
459                 $tooltip = _m('TOOLTIP', 'Change your email, avatar, password, profile');
460                 $this->menuItem(common_local_url('profilesettings'),
461                                 // TRANS: Main menu option when logged in for access to user settings
462                                 _('Account'), $tooltip, false, 'nav_account');
463                 // TRANS: Tooltip for main menu option "Services"
464                 $tooltip = _m('TOOLTIP', 'Connect to services');
465                 $this->menuItem(common_local_url('oauthconnectionssettings'),
466                                 // TRANS: Main menu option when logged in and connection are possible for access to options to connect to other services
467                                 _('Connect'), $tooltip, false, 'nav_connect');
468                 if ($user->hasRight(Right::CONFIGURESITE)) {
469                     // TRANS: Tooltip for menu option "Admin"
470                     $tooltip = _m('TOOLTIP', 'Change site configuration');
471                     $this->menuItem(common_local_url('siteadminpanel'),
472                                     // TRANS: Main menu option when logged in and site admin for access to site configuration
473                                     _m('MENU', 'Admin'), $tooltip, false, 'nav_admin');
474                 }
475                 if (common_config('invite', 'enabled')) {
476                     // TRANS: Tooltip for main menu option "Invite"
477                     $tooltip = _m('TOOLTIP', 'Invite friends and colleagues to join you on %s');
478                     $this->menuItem(common_local_url('invite'),
479                                     // TRANS: Main menu option when logged in and invitations are allowed for inviting new users
480                                     _m('MENU', 'Invite'),
481                                     sprintf($tooltip,
482                                             common_config('site', 'name')),
483                                     false, 'nav_invitecontact');
484                 }
485                 // TRANS: Tooltip for main menu option "Logout"
486                 $tooltip = _m('TOOLTIP', 'Logout from the site');
487                 $this->menuItem(common_local_url('logout'),
488                                 // TRANS: Main menu option when logged in to log out the current user
489                                 _m('MENU', 'Logout'), $tooltip, false, 'nav_logout');
490             }
491             else {
492                 if (!common_config('site', 'closed') && !common_config('site', 'inviteonly')) {
493                     // TRANS: Tooltip for main menu option "Register"
494                     $tooltip = _m('TOOLTIP', 'Create an account');
495                     $this->menuItem(common_local_url('register'),
496                                     // TRANS: Main menu option when not logged in to register a new account
497                                     _m('MENU', 'Register'), $tooltip, false, 'nav_register');
498                 }
499                 // TRANS: Tooltip for main menu option "Login"
500                 $tooltip = _m('TOOLTIP', 'Login to the site');
501                 // TRANS: Main menu option when not logged in to log in
502                 $this->menuItem(common_local_url('login'),
503                                 _m('MENU', 'Login'), $tooltip, false, 'nav_login');
504             }
505             // TRANS: Tooltip for main menu option "Help"
506             $tooltip = _m('TOOLTIP', 'Help me!');
507             // TRANS: Main menu option for help on the StatusNet site
508             $this->menuItem(common_local_url('doc', array('title' => 'help')),
509                             _m('MENU', 'Help'), $tooltip, false, 'nav_help');
510             if ($user || !common_config('site', 'private')) {
511                 // TRANS: Tooltip for main menu option "Search"
512                 $tooltip = _m('TOOLTIP', 'Search for people or text');
513                 // TRANS: Main menu option when logged in or when the StatusNet instance is not private
514                 $this->menuItem(common_local_url('peoplesearch'),
515                                 _m('MENU', 'Search'), $tooltip, false, 'nav_search');
516             }
517             Event::handle('EndPrimaryNav', array($this));
518         }
519         $this->elementEnd('ul');
520         $this->elementEnd('dd');
521         $this->elementEnd('dl');
522     }
523
524     /**
525      * Show site notice.
526      *
527      * @return nothing
528      */
529     function showSiteNotice()
530     {
531         // Revist. Should probably do an hAtom pattern here
532         $text = common_config('site', 'notice');
533         if ($text) {
534             $this->elementStart('dl', array('id' => 'site_notice',
535                                             'class' => 'system_notice'));
536             // TRANS: DT element for site notice. String is hidden in default CSS.
537             $this->element('dt', null, _('Site notice'));
538             $this->elementStart('dd', null);
539             $this->raw($text);
540             $this->elementEnd('dd');
541             $this->elementEnd('dl');
542         }
543     }
544
545     /**
546      * Show notice form.
547      *
548      * MAY overload if no notice form needed... or direct message box????
549      *
550      * @return nothing
551      */
552     function showNoticeForm()
553     {
554         $notice_form = new NoticeForm($this);
555         $notice_form->show();
556     }
557
558     /**
559      * Show anonymous message.
560      *
561      * SHOULD overload
562      *
563      * @return nothing
564      */
565     function showAnonymousMessage()
566     {
567         // needs to be defined by the class
568     }
569
570     /**
571      * Show core.
572      *
573      * Shows local navigation, content block and aside.
574      *
575      * @return nothing
576      */
577     function showCore()
578     {
579         $this->elementStart('div', array('id' => 'core'));
580         if (Event::handle('StartShowLocalNavBlock', array($this))) {
581             $this->showLocalNavBlock();
582             Event::handle('EndShowLocalNavBlock', array($this));
583         }
584         if (Event::handle('StartShowContentBlock', array($this))) {
585             $this->showContentBlock();
586             Event::handle('EndShowContentBlock', array($this));
587         }
588         if (Event::handle('StartShowAside', array($this))) {
589             $this->showAside();
590             Event::handle('EndShowAside', array($this));
591         }
592         $this->elementEnd('div');
593     }
594
595     /**
596      * Show local navigation block.
597      *
598      * @return nothing
599      */
600     function showLocalNavBlock()
601     {
602         $this->elementStart('dl', array('id' => 'site_nav_local_views'));
603         // TRANS: DT element for local views block. String is hidden in default CSS.
604         $this->element('dt', null, _('Local views'));
605         $this->elementStart('dd');
606         $this->showLocalNav();
607         $this->elementEnd('dd');
608         $this->elementEnd('dl');
609     }
610
611     /**
612      * Show local navigation.
613      *
614      * SHOULD overload
615      *
616      * @return nothing
617      */
618     function showLocalNav()
619     {
620         // does nothing by default
621     }
622
623     /**
624      * Show content block.
625      *
626      * @return nothing
627      */
628     function showContentBlock()
629     {
630         $this->elementStart('div', array('id' => 'content'));
631         if (Event::handle('StartShowPageTitle', array($this))) {
632             $this->showPageTitle();
633             Event::handle('EndShowPageTitle', array($this));
634         }
635         $this->showPageNoticeBlock();
636         $this->elementStart('div', array('id' => 'content_inner'));
637         // show the actual content (forms, lists, whatever)
638         $this->showContent();
639         $this->elementEnd('div');
640         $this->elementEnd('div');
641     }
642
643     /**
644      * Show page title.
645      *
646      * @return nothing
647      */
648     function showPageTitle()
649     {
650         $this->element('h1', null, $this->title());
651     }
652
653     /**
654      * Show page notice block.
655      *
656      * Only show the block if a subclassed action has overrided
657      * Action::showPageNotice(), or an event handler is registered for
658      * the StartShowPageNotice event, in which case we assume the
659      * 'page_notice' definition list is desired.  This is to prevent
660      * empty 'page_notice' definition lists from being output everywhere.
661      *
662      * @return nothing
663      */
664     function showPageNoticeBlock()
665     {
666         $rmethod = new ReflectionMethod($this, 'showPageNotice');
667         $dclass = $rmethod->getDeclaringClass()->getName();
668
669         if ($dclass != 'Action' || Event::hasHandler('StartShowPageNotice')) {
670
671             $this->elementStart('dl', array('id' => 'page_notice',
672                                             'class' => 'system_notice'));
673             // TRANS: DT element for page notice. String is hidden in default CSS.
674             $this->element('dt', null, _('Page notice'));
675             $this->elementStart('dd');
676             if (Event::handle('StartShowPageNotice', array($this))) {
677                 $this->showPageNotice();
678                 Event::handle('EndShowPageNotice', array($this));
679             }
680             $this->elementEnd('dd');
681             $this->elementEnd('dl');
682         }
683     }
684
685     /**
686      * Show page notice.
687      *
688      * SHOULD overload (unless there's not a notice)
689      *
690      * @return nothing
691      */
692     function showPageNotice()
693     {
694     }
695
696     /**
697      * Show content.
698      *
699      * MUST overload (unless there's not a notice)
700      *
701      * @return nothing
702      */
703     function showContent()
704     {
705     }
706
707     /**
708      * Show Aside.
709      *
710      * @return nothing
711      */
712     function showAside()
713     {
714         $this->elementStart('div', array('id' => 'aside_primary',
715                                          'class' => 'aside'));
716         if (Event::handle('StartShowSections', array($this))) {
717             $this->showSections();
718             Event::handle('EndShowSections', array($this));
719         }
720         if (Event::handle('StartShowExportData', array($this))) {
721             $this->showExportData();
722             Event::handle('EndShowExportData', array($this));
723         }
724         $this->elementEnd('div');
725     }
726
727     /**
728      * Show export data feeds.
729      *
730      * @return void
731      */
732     function showExportData()
733     {
734         $feeds = $this->getFeeds();
735         if ($feeds) {
736             $fl = new FeedList($this);
737             $fl->show($feeds);
738         }
739     }
740
741     /**
742      * Show sections.
743      *
744      * SHOULD overload
745      *
746      * @return nothing
747      */
748     function showSections()
749     {
750         // for each section, show it
751     }
752
753     /**
754      * Show footer.
755      *
756      * @return nothing
757      */
758     function showFooter()
759     {
760         $this->elementStart('div', array('id' => 'footer'));
761         $this->showSecondaryNav();
762         $this->showLicenses();
763         $this->elementEnd('div');
764     }
765
766     /**
767      * Show secondary navigation.
768      *
769      * @return nothing
770      */
771     function showSecondaryNav()
772     {
773         $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
774         // TRANS: DT element for secondary navigation menu. String is hidden in default CSS.
775         $this->element('dt', null, _('Secondary site navigation'));
776         $this->elementStart('dd', null);
777         $this->elementStart('ul', array('class' => 'nav'));
778         if (Event::handle('StartSecondaryNav', array($this))) {
779             $this->menuItem(common_local_url('doc', array('title' => 'help')),
780                             // TRANS: Secondary navigation menu option leading to help on StatusNet.
781                             _('Help'));
782             $this->menuItem(common_local_url('doc', array('title' => 'about')),
783                             // TRANS: Secondary navigation menu option leading to text about StatusNet site.
784                             _('About'));
785             $this->menuItem(common_local_url('doc', array('title' => 'faq')),
786                             // TRANS: Secondary navigation menu option leading to Frequently Asked Questions.
787                             _('FAQ'));
788             $bb = common_config('site', 'broughtby');
789             if (!empty($bb)) {
790                 $this->menuItem(common_local_url('doc', array('title' => 'tos')),
791                                 // TRANS: Secondary navigation menu option leading to Terms of Service.
792                                 _('TOS'));
793             }
794             $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
795                             // TRANS: Secondary navigation menu option leading to privacy policy.
796                             _('Privacy'));
797             $this->menuItem(common_local_url('doc', array('title' => 'source')),
798                             // TRANS: Secondary navigation menu option.
799                             _('Source'));
800             $this->menuItem(common_local_url('version'),
801                             // TRANS: Secondary navigation menu option leading to version information on the StatusNet site.
802                             _('Version'));
803             $this->menuItem(common_local_url('doc', array('title' => 'contact')),
804                             // TRANS: Secondary navigation menu option leading to contact information on the StatusNet site.
805                             _('Contact'));
806             $this->menuItem(common_local_url('doc', array('title' => 'badge')),
807                             // TRANS: Secondary navigation menu option.
808                             _('Badge'));
809             Event::handle('EndSecondaryNav', array($this));
810         }
811         $this->elementEnd('ul');
812         $this->elementEnd('dd');
813         $this->elementEnd('dl');
814     }
815
816     /**
817      * Show licenses.
818      *
819      * @return nothing
820      */
821     function showLicenses()
822     {
823         $this->elementStart('dl', array('id' => 'licenses'));
824         $this->showStatusNetLicense();
825         $this->showContentLicense();
826         $this->elementEnd('dl');
827     }
828
829     /**
830      * Show StatusNet license.
831      *
832      * @return nothing
833      */
834     function showStatusNetLicense()
835     {
836         // TRANS: DT element for StatusNet software license.
837         $this->element('dt', array('id' => 'site_statusnet_license'), _('StatusNet software license'));
838         $this->elementStart('dd', null);
839         if (common_config('site', 'broughtby')) {
840             // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is set.
841             // TRANS: Text between [] is a link description, text between () is the link itself.
842             // TRANS: Make sure there is no whitespace between "]" and "(".
843             // TRANS: "%%site.broughtby%%" is the value of the variable site.broughtby
844             $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%).');
845         } else {
846             // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is not set.
847             $instr = _('**%%site.name%%** is a microblogging service.');
848         }
849         $instr .= ' ';
850         // TRANS: Second sentence of the StatusNet site license. Mentions the StatusNet source code license.
851         // TRANS: Make sure there is no whitespace between "]" and "(".
852         // TRANS: Text between [] is a link description, text between () is the link itself.
853         // TRANS: %s is the version of StatusNet that is being used.
854         $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);
855         $output = common_markup_to_html($instr);
856         $this->raw($output);
857         $this->elementEnd('dd');
858         // do it
859     }
860
861     /**
862      * Show content license.
863      *
864      * @return nothing
865      */
866     function showContentLicense()
867     {
868         if (Event::handle('StartShowContentLicense', array($this))) {
869             // TRANS: DT element for StatusNet site content license.
870             $this->element('dt', array('id' => 'site_content_license'), _('Site content license'));
871             $this->elementStart('dd', array('id' => 'site_content_license_cc'));
872
873             switch (common_config('license', 'type')) {
874             case 'private':
875                 // TRANS: Content license displayed when license is set to 'private'.
876                 // TRANS: %1$s is the site name.
877                 $this->element('p', null, sprintf(_('Content and data of %1$s are private and confidential.'),
878                                                   common_config('site', 'name')));
879                 // fall through
880             case 'allrightsreserved':
881                 if (common_config('license', 'owner')) {
882                     // TRANS: Content license displayed when license is set to 'allrightsreserved'.
883                     // TRANS: %1$s is the copyright owner.
884                     $this->element('p', null, sprintf(_('Content and data copyright by %1$s. All rights reserved.'),
885                                                       common_config('license', 'owner')));
886                 } else {
887                     // TRANS: Content license displayed when license is set to 'allrightsreserved' and no owner is set.
888                     $this->element('p', null, _('Content and data copyright by contributors. All rights reserved.'));
889                 }
890                 break;
891             case 'cc': // fall through
892             default:
893                 $this->elementStart('p');
894                 $this->element('img', array('id' => 'license_cc',
895                                             'src' => common_config('license', 'image'),
896                                             'alt' => common_config('license', 'title'),
897                                             'width' => '80',
898                                             'height' => '15'));
899                 $this->text(' ');
900                 // TRANS: license message in footer.
901                 // TRANS: %1$s is the site name, %2$s is a link to the license URL, with a licence name set in configuration.
902                 $notice = _('All %1$s content and data are available under the %2$s license.');
903                 $link = "<a class=\"license\" rel=\"external license\" href=\"" .
904                         htmlspecialchars(common_config('license', 'url')) .
905                         "\">" .
906                         htmlspecialchars(common_config('license', 'title')) .
907                         "</a>";
908                 $this->raw(sprintf(htmlspecialchars($notice),
909                                    htmlspecialchars(common_config('site', 'name')),
910                                    $link));
911                 $this->elementEnd('p');
912                 break;
913             }
914
915             $this->elementEnd('dd');
916             Event::handle('EndShowContentLicense', array($this));
917         }
918     }
919
920     /**
921      * Return last modified, if applicable.
922      *
923      * MAY override
924      *
925      * @return string last modified http header
926      */
927     function lastModified()
928     {
929         // For comparison with If-Last-Modified
930         // If not applicable, return null
931         return null;
932     }
933
934     /**
935      * Return etag, if applicable.
936      *
937      * MAY override
938      *
939      * @return string etag http header
940      */
941     function etag()
942     {
943         return null;
944     }
945
946     /**
947      * Return true if read only.
948      *
949      * MAY override
950      *
951      * @param array $args other arguments
952      *
953      * @return boolean is read only action?
954      */
955     function isReadOnly($args)
956     {
957         return false;
958     }
959
960     /**
961      * Returns query argument or default value if not found
962      *
963      * @param string $key requested argument
964      * @param string $def default value to return if $key is not provided
965      *
966      * @return boolean is read only action?
967      */
968     function arg($key, $def=null)
969     {
970         if (array_key_exists($key, $this->args)) {
971             return $this->args[$key];
972         } else {
973             return $def;
974         }
975     }
976
977     /**
978      * Returns trimmed query argument or default value if not found
979      *
980      * @param string $key requested argument
981      * @param string $def default value to return if $key is not provided
982      *
983      * @return boolean is read only action?
984      */
985     function trimmed($key, $def=null)
986     {
987         $arg = $this->arg($key, $def);
988         return is_string($arg) ? trim($arg) : $arg;
989     }
990
991     /**
992      * Handler method
993      *
994      * @param array $argarray is ignored since it's now passed in in prepare()
995      *
996      * @return boolean is read only action?
997      */
998     function handle($argarray=null)
999     {
1000         header('Vary: Accept-Encoding,Cookie');
1001
1002         $lm   = $this->lastModified();
1003         $etag = $this->etag();
1004
1005         if ($etag) {
1006             header('ETag: ' . $etag);
1007         }
1008
1009         if ($lm) {
1010             header('Last-Modified: ' . date(DATE_RFC1123, $lm));
1011             if ($this->isCacheable()) {
1012                 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1013                 header( "Cache-Control: private, must-revalidate, max-age=0" );
1014                 header( "Pragma:");
1015             }
1016         }
1017
1018         $checked = false;
1019         if ($etag) {
1020             $if_none_match = (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) ?
1021               $_SERVER['HTTP_IF_NONE_MATCH'] : null;
1022             if ($if_none_match) {
1023                 // If this check fails, ignore the if-modified-since below.
1024                 $checked = true;
1025                 if ($this->_hasEtag($etag, $if_none_match)) {
1026                     header('HTTP/1.1 304 Not Modified');
1027                     // Better way to do this?
1028                     exit(0);
1029                 }
1030             }
1031         }
1032
1033         if (!$checked && $lm && array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
1034             $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
1035             $ims = strtotime($if_modified_since);
1036             if ($lm <= $ims) {
1037                 header('HTTP/1.1 304 Not Modified');
1038                 // Better way to do this?
1039                 exit(0);
1040             }
1041         }
1042     }
1043
1044     /**
1045      * Is this action cacheable?
1046      *
1047      * If the action returns a last-modified
1048      *
1049      * @param array $argarray is ignored since it's now passed in in prepare()
1050      *
1051      * @return boolean is read only action?
1052      */
1053     function isCacheable()
1054     {
1055         return true;
1056     }
1057
1058     /**
1059      * Has etag? (private)
1060      *
1061      * @param string $etag          etag http header
1062      * @param string $if_none_match ifNoneMatch http header
1063      *
1064      * @return boolean
1065      */
1066     function _hasEtag($etag, $if_none_match)
1067     {
1068         $etags = explode(',', $if_none_match);
1069         return in_array($etag, $etags) || in_array('*', $etags);
1070     }
1071
1072     /**
1073      * Boolean understands english (yes, no, true, false)
1074      *
1075      * @param string $key query key we're interested in
1076      * @param string $def default value
1077      *
1078      * @return boolean interprets yes/no strings as boolean
1079      */
1080     function boolean($key, $def=false)
1081     {
1082         $arg = strtolower($this->trimmed($key));
1083
1084         if (is_null($arg)) {
1085             return $def;
1086         } else if (in_array($arg, array('true', 'yes', '1', 'on'))) {
1087             return true;
1088         } else if (in_array($arg, array('false', 'no', '0'))) {
1089             return false;
1090         } else {
1091             return $def;
1092         }
1093     }
1094
1095     /**
1096      * Integer value of an argument
1097      *
1098      * @param string $key      query key we're interested in
1099      * @param string $defValue optional default value (default null)
1100      * @param string $maxValue optional max value (default null)
1101      * @param string $minValue optional min value (default null)
1102      *
1103      * @return integer integer value
1104      */
1105     function int($key, $defValue=null, $maxValue=null, $minValue=null)
1106     {
1107         $arg = strtolower($this->trimmed($key));
1108
1109         if (is_null($arg) || !is_integer($arg)) {
1110             return $defValue;
1111         }
1112
1113         if (!is_null($maxValue)) {
1114             $arg = min($arg, $maxValue);
1115         }
1116
1117         if (!is_null($minValue)) {
1118             $arg = max($arg, $minValue);
1119         }
1120
1121         return $arg;
1122     }
1123
1124     /**
1125      * Server error
1126      *
1127      * @param string  $msg  error message to display
1128      * @param integer $code http error code, 500 by default
1129      *
1130      * @return nothing
1131      */
1132     function serverError($msg, $code=500)
1133     {
1134         $action = $this->trimmed('action');
1135         common_debug("Server error '$code' on '$action': $msg", __FILE__);
1136         throw new ServerException($msg, $code);
1137     }
1138
1139     /**
1140      * Client error
1141      *
1142      * @param string  $msg  error message to display
1143      * @param integer $code http error code, 400 by default
1144      *
1145      * @return nothing
1146      */
1147     function clientError($msg, $code=400)
1148     {
1149         $action = $this->trimmed('action');
1150         common_debug("User error '$code' on '$action': $msg", __FILE__);
1151         throw new ClientException($msg, $code);
1152     }
1153
1154     /**
1155      * Returns the current URL
1156      *
1157      * @return string current URL
1158      */
1159     function selfUrl()
1160     {
1161         list($action, $args) = $this->returnToArgs();
1162         return common_local_url($action, $args);
1163     }
1164
1165     /**
1166      * Returns arguments sufficient for re-constructing URL
1167      *
1168      * @return array two elements: action, other args
1169      */
1170     function returnToArgs()
1171     {
1172         $action = $this->trimmed('action');
1173         $args   = $this->args;
1174         unset($args['action']);
1175         if (common_config('site', 'fancy')) {
1176             unset($args['p']);
1177         }
1178         if (array_key_exists('submit', $args)) {
1179             unset($args['submit']);
1180         }
1181         foreach (array_keys($_COOKIE) as $cookie) {
1182             unset($args[$cookie]);
1183         }
1184         return array($action, $args);
1185     }
1186
1187     /**
1188      * Generate a menu item
1189      *
1190      * @param string  $url         menu URL
1191      * @param string  $text        menu name
1192      * @param string  $title       title attribute, null by default
1193      * @param boolean $is_selected current menu item, false by default
1194      * @param string  $id          element id, null by default
1195      *
1196      * @return nothing
1197      */
1198     function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
1199     {
1200         // Added @id to li for some control.
1201         // XXX: We might want to move this to htmloutputter.php
1202         $lattrs = array();
1203         if ($is_selected) {
1204             $lattrs['class'] = 'current';
1205         }
1206
1207         (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
1208
1209         $this->elementStart('li', $lattrs);
1210         $attrs['href'] = $url;
1211         if ($title) {
1212             $attrs['title'] = $title;
1213         }
1214         $this->element('a', $attrs, $text);
1215         $this->elementEnd('li');
1216     }
1217
1218     /**
1219      * Generate pagination links
1220      *
1221      * @param boolean $have_before is there something before?
1222      * @param boolean $have_after  is there something after?
1223      * @param integer $page        current page
1224      * @param string  $action      current action
1225      * @param array   $args        rest of query arguments
1226      *
1227      * @return nothing
1228      */
1229     // XXX: The messages in this pagination method only tailor to navigating
1230     //      notices. In other lists, "Previous"/"Next" type navigation is
1231     //      desirable, but not available.
1232     function pagination($have_before, $have_after, $page, $action, $args=null)
1233     {
1234         // Does a little before-after block for next/prev page
1235         if ($have_before || $have_after) {
1236             $this->elementStart('dl', 'pagination');
1237             // TRANS: DT element for pagination (previous/next, etc.).
1238             $this->element('dt', null, _('Pagination'));
1239             $this->elementStart('dd', null);
1240             $this->elementStart('ul', array('class' => 'nav'));
1241         }
1242         if ($have_before) {
1243             $pargs   = array('page' => $page-1);
1244             $this->elementStart('li', array('class' => 'nav_prev'));
1245             $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1246                                       'rel' => 'prev'),
1247                            // TRANS: Pagination message to go to a page displaying information more in the
1248                            // TRANS: present than the currently displayed information.
1249                            _('After'));
1250             $this->elementEnd('li');
1251         }
1252         if ($have_after) {
1253             $pargs   = array('page' => $page+1);
1254             $this->elementStart('li', array('class' => 'nav_next'));
1255             $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1256                                       'rel' => 'next'),
1257                            // TRANS: Pagination message to go to a page displaying information more in the
1258                            // TRANS: past than the currently displayed information.
1259                            _('Before'));
1260             $this->elementEnd('li');
1261         }
1262         if ($have_before || $have_after) {
1263             $this->elementEnd('ul');
1264             $this->elementEnd('dd');
1265             $this->elementEnd('dl');
1266         }
1267     }
1268
1269     /**
1270      * An array of feeds for this action.
1271      *
1272      * Returns an array of potential feeds for this action.
1273      *
1274      * @return array Feed object to show in head and links
1275      */
1276     function getFeeds()
1277     {
1278         return null;
1279     }
1280
1281     /**
1282      * A design for this action
1283      *
1284      * @return Design a design object to use
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     // XXX: Finding this type of check with the same message about 50 times.
1300     //      Possible to refactor?
1301     function checkSessionToken()
1302     {
1303         // CSRF protection
1304         $token = $this->trimmed('token');
1305         if (empty($token) || $token != common_session_token()) {
1306             // TRANS: Client error text when there is a problem with the session token.
1307             $this->clientError(_('There was a problem with your session token.'));
1308         }
1309     }
1310 }