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