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