]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/action.php
Domain name regular expression into lib/framework.php
[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('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * Base class for all actions
35  *
36  * This is the base class for all actions in the package. An action is
37  * more or less a "view" in an MVC framework.
38  *
39  * Actions are responsible for extracting and validating parameters; using
40  * model classes to read and write to the database; and doing ouput.
41  *
42  * @category Output
43  * @package  StatusNet
44  * @author   Evan Prodromou <evan@status.net>
45  * @author   Sarven Capadisli <csarven@status.net>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://status.net/
48  *
49  * @see      HTMLOutputter
50  */
51 class Action extends HTMLOutputter // lawsuit
52 {
53     // This should be protected/private in the future
54     public $args = array();
55
56     // Action properties, set per-class
57     protected $action = false;
58     protected $ajax   = false;
59     protected $menus  = true;
60     protected $needLogin = false;
61     protected $needPost = false;    // implies canPost if true
62     protected $canPost = false;     // can this action handle POST method?
63
64     // The currently scoped profile (normally Profile::current; from $this->auth_user for API)
65     protected $scoped = null;
66
67     // Related to front-end user representation
68     protected $format = null;
69     protected $error  = null;
70     protected $msg    = null;
71
72     /**
73      * Constructor
74      *
75      * Just wraps the HTMLOutputter constructor.
76      *
77      * @param string  $output URI to output to, default = stdout
78      * @param boolean $indent Whether to indent output, default true
79      *
80      * @see XMLOutputter::__construct
81      * @see HTMLOutputter::__construct
82      */
83     function __construct($output='php://output', $indent=null)
84     {
85         parent::__construct($output, $indent);
86     }
87
88     function getError()
89     {
90         return $this->error;
91     }
92
93     function getInfo()
94     {
95         return $this->msg;
96     }
97
98     public function handleError($e)
99     {
100         if ($e instanceof ClientException) {
101             $this->clientError($e->getMessage(), $e->getCode());
102         } elseif ($e instanceof ServerException) {
103             $this->serverError($e->getMessage(), $e->getCode());
104         } else {
105             // If it wasn't specified more closely which kind of exception it was
106             $this->serverError($e->getMessage(), 500);
107         }
108     }
109
110     static public function run(array $args=array(), $output='php://output', $indent=null) {
111         $class = get_called_class();
112         $action = new $class($output, $indent);
113         set_exception_handler(array($action, 'handleError'));
114         $action->execute($args);
115         return $action;
116     }
117
118     public function execute(array $args=array()) {
119         // checkMirror stuff
120         if (common_config('db', 'mirror') && $this->isReadOnly($args)) {
121             if (is_array(common_config('db', 'mirror'))) {
122                 // "load balancing", ha ha
123                 $arr = common_config('db', 'mirror');
124                 $k = array_rand($arr);
125                 $mirror = $arr[$k];
126             } else {
127                 $mirror = common_config('db', 'mirror');
128             }
129
130             // everyone else uses the mirror
131             common_config_set('db', 'database', $mirror);
132         }
133
134         if (Event::handle('StartActionExecute', array($this, &$args))) {
135             $prepared = $this->prepare($args);
136             if ($prepared) {
137                 $this->handle($args);
138             } else {
139                 common_debug('Prepare failed for Action.');
140             }
141
142             $this->flush();
143             Event::handle('EndActionExecute', array($this));
144         }
145     }
146
147     /**
148      * For initializing members of the class.
149      *
150      * @param array $argarray misc. arguments
151      *
152      * @return boolean true
153      */
154     protected function prepare(array $args=array())
155     {
156         if ($this->needPost && !$this->isPost()) {
157             // TRANS: Client error. POST is a HTTP command. It should not be translated.
158             $this->clientError(_('This method requires a POST.'), 405);
159         }
160
161         // needPost, of course, overrides canPost if true
162         if (!$this->canPost) {
163             $this->canPost = $this->needPost;
164         }
165
166         $this->args = common_copy_args($args);
167
168         // This could be set with get_called_action and then
169         // chop off 'Action' from the class name. In lower case.
170         $this->action = strtolower($this->trimmed('action'));
171
172         if ($this->ajax || $this->boolean('ajax')) {
173             // check with GNUsocial::isAjax()
174             GNUsocial::setAjax(true);
175         }
176
177         if ($this->needLogin) {
178             $this->checkLogin(); // if not logged in, this redirs/excepts
179         }
180
181         $this->updateScopedProfile();
182
183         return true;
184     }
185
186     public function updateScopedProfile()
187     {
188         $this->scoped = Profile::current();
189         return $this->scoped;
190     }
191
192     public function getScoped()
193     {
194         return ($this->scoped instanceof Profile) ? $this->scoped : null;
195     }
196
197     // Must be run _after_ prepare
198     public function getActionName()
199     {
200         return $this->action;
201     }
202
203     public function isAction(array $names)
204     {
205         foreach ($names as $class) {
206             // PHP is case insensitive, and we have stuff like ApiUpperCaseAction,
207             // but we at least make a point out of wanting to do stuff case-sensitive.
208             $class = ucfirst($class) . 'Action';
209             if ($this instanceof $class) {
210                 return true;
211             }
212         }
213         return false;
214     }
215
216     /**
217      * Show page, a template method.
218      *
219      * @return nothing
220      */
221     public function showPage()
222     {
223         if (GNUsocial::isAjax()) {
224             self::showAjax();
225             return;
226         }
227         if (Event::handle('StartShowHTML', array($this))) {
228             $this->startHTML();
229             $this->flush();
230             Event::handle('EndShowHTML', array($this));
231         }
232         if (Event::handle('StartShowHead', array($this))) {
233             $this->showHead();
234             $this->flush();
235             Event::handle('EndShowHead', array($this));
236         }
237         if (Event::handle('StartShowBody', array($this))) {
238             $this->showBody();
239             Event::handle('EndShowBody', array($this));
240         }
241         if (Event::handle('StartEndHTML', array($this))) {
242             $this->endHTML();
243             Event::handle('EndEndHTML', array($this));
244         }
245     }
246
247     public function showAjax()
248     {
249         $this->startHTML('text/xml;charset=utf-8');
250         $this->elementStart('head');
251         // TRANS: Title for conversation page.
252         $this->element('title', null, $this->title());
253         $this->elementEnd('head');
254         $this->elementStart('body');
255         if ($this->getError()) {
256             $this->element('p', array('id'=>'error'), $this->getError());
257         } else {
258             $this->showContent();
259         }
260         $this->elementEnd('body');
261         $this->endHTML();
262     }
263
264     function endHTML()
265     {
266         global $_startTime;
267
268         if (isset($_startTime)) {
269             $endTime = microtime(true);
270             $diff = round(($endTime - $_startTime) * 1000);
271             $this->raw("<!-- ${diff}ms -->");
272         }
273
274         return parent::endHTML();
275     }
276
277     /**
278      * Show head, a template method.
279      *
280      * @return nothing
281      */
282     function showHead()
283     {
284         // XXX: attributes (profile?)
285         $this->elementStart('head');
286         if (Event::handle('StartShowHeadElements', array($this))) {
287             if (Event::handle('StartShowHeadTitle', array($this))) {
288                 $this->showTitle();
289                 Event::handle('EndShowHeadTitle', array($this));
290             }
291             $this->showShortcutIcon();
292             $this->showStylesheets();
293             $this->showOpenSearch();
294             $this->showFeeds();
295             $this->showDescription();
296             $this->extraHead();
297             Event::handle('EndShowHeadElements', array($this));
298         }
299         $this->elementEnd('head');
300     }
301
302     /**
303      * Show title, a template method.
304      *
305      * @return nothing
306      */
307     function showTitle()
308     {
309         $this->element('title', null,
310                        // TRANS: Page title. %1$s is the title, %2$s is the site name.
311                        sprintf(_('%1$s - %2$s'),
312                                $this->title(),
313                                common_config('site', 'name')));
314     }
315
316     /**
317      * Returns the page title
318      *
319      * SHOULD overload
320      *
321      * @return string page title
322      */
323
324     function title()
325     {
326         // TRANS: Page title for a page without a title set.
327         return _('Untitled page');
328     }
329
330     /**
331      * Show themed shortcut icon
332      *
333      * @return nothing
334      */
335     function showShortcutIcon()
336     {
337         if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/favicon.ico')) {
338             $this->element('link', array('rel' => 'shortcut icon',
339                                          'href' => Theme::path('favicon.ico')));
340         } else {
341             // favicon.ico should be HTTPS if the rest of the page is
342             $this->element('link', array('rel' => 'shortcut icon',
343                                          'href' => common_path('favicon.ico', GNUsocial::isHTTPS())));
344         }
345
346         if (common_config('site', 'mobile')) {
347             if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/apple-touch-icon.png')) {
348                 $this->element('link', array('rel' => 'apple-touch-icon',
349                                              'href' => Theme::path('apple-touch-icon.png')));
350             } else {
351                 $this->element('link', array('rel' => 'apple-touch-icon',
352                                              'href' => common_path('apple-touch-icon.png')));
353             }
354         }
355     }
356
357     /**
358      * Show stylesheets
359      *
360      * @return nothing
361      */
362     function showStylesheets()
363     {
364         if (Event::handle('StartShowStyles', array($this))) {
365
366             // Use old name for StatusNet for compatibility on events
367
368             if (Event::handle('StartShowStylesheets', array($this))) {
369                 $this->primaryCssLink(null, 'screen, projection, tv, print');
370                 Event::handle('EndShowStylesheets', array($this));
371             }
372
373             $this->cssLink('js/extlib/jquery-ui/css/smoothness/jquery-ui.css');
374
375             if (Event::handle('StartShowUAStyles', array($this))) {
376                 Event::handle('EndShowUAStyles', array($this));
377             }
378
379             Event::handle('EndShowStyles', array($this));
380
381             if (common_config('custom_css', 'enabled')) {
382                 $css = common_config('custom_css', 'css');
383                 if (Event::handle('StartShowCustomCss', array($this, &$css))) {
384                     if (trim($css) != '') {
385                         $this->style($css);
386                     }
387                     Event::handle('EndShowCustomCss', array($this));
388                 }
389             }
390         }
391     }
392
393     function primaryCssLink($mainTheme=null, $media=null)
394     {
395         $theme = new Theme($mainTheme);
396
397         // Some themes may have external stylesheets
398         foreach ($theme->getExternals() as $url) {
399             $this->cssLink($url, $mainTheme, $media);
400         }
401
402         // If the currently-selected theme has dependencies on other themes,
403         // we'll need to load their display.css files as well in order.
404         $baseThemes = $theme->getDeps();
405         foreach ($baseThemes as $baseTheme) {
406             $this->cssLink('css/display.css', $baseTheme, $media);
407         }
408         $this->cssLink('css/display.css', $mainTheme, $media);
409
410         // Additional styles for RTL languages
411         if (is_rtl(common_language())) {
412             if (file_exists(Theme::file('css/rtl.css'))) {
413                 $this->cssLink('css/rtl.css', $mainTheme, $media);
414             }
415         }
416     }
417
418     /**
419      * Show javascript headers
420      *
421      * @return nothing
422      */
423     function showScripts()
424     {
425         if (Event::handle('StartShowScripts', array($this))) {
426             if (Event::handle('StartShowJQueryScripts', array($this))) {
427                 $this->script('extlib/jquery.js');
428                 $this->script('extlib/jquery.form.js');
429                 $this->script('extlib/jquery-ui/jquery-ui.js');
430                 $this->script('extlib/jquery.cookie.js');
431
432                 Event::handle('EndShowJQueryScripts', array($this));
433             }
434             if (Event::handle('StartShowStatusNetScripts', array($this))) {
435                 $this->script('util.js');
436                 $this->script('xbImportNode.js');
437
438                 // This route isn't available in single-user mode.
439                 // Not sure why, but it causes errors here.
440                 $this->inlineScript('var _peopletagAC = "' .
441                                     common_local_url('peopletagautocomplete') . '";');
442                 $this->showScriptMessages();
443                 $this->showScriptVariables();
444                 // Anti-framing code to avoid clickjacking attacks in older browsers.
445                 // This will show a blank page if the page is being framed, which is
446                 // consistent with the behavior of the 'X-Frame-Options: SAMEORIGIN'
447                 // header, which prevents framing in newer browser.
448                 if (common_config('javascript', 'bustframes')) {
449                     $this->inlineScript('if (window.top !== window.self) { document.write = ""; window.top.location = window.self.location; setTimeout(function () { document.body.innerHTML = ""; }, 1); window.self.onload = function () { document.body.innerHTML = ""; }; }');
450                 }
451                 Event::handle('EndShowStatusNetScripts', array($this));
452             }
453             Event::handle('EndShowScripts', array($this));
454         }
455     }
456
457     /**
458      * Exports a map of localized text strings to JavaScript code.
459      *
460      * Plugins can add to what's exported by hooking the StartScriptMessages or EndScriptMessages
461      * events and appending to the array. Try to avoid adding strings that won't be used, as
462      * they'll be added to HTML output.
463      */
464     function showScriptMessages()
465     {
466         $messages = array();
467
468         if (Event::handle('StartScriptMessages', array($this, &$messages))) {
469             // Common messages needed for timeline views etc...
470
471             // TRANS: Localized tooltip for '...' expansion button on overlong remote messages.
472             $messages['showmore_tooltip'] = _m('TOOLTIP', 'Show more');
473             $messages['popup_close_button'] = _m('TOOLTIP', 'Close popup');
474
475             $messages = array_merge($messages, $this->getScriptMessages());
476
477             Event::handle('EndScriptMessages', array($this, &$messages));
478         }
479
480         if (!empty($messages)) {
481             $this->inlineScript('SN.messages=' . json_encode($messages));
482         }
483
484         return $messages;
485     }
486
487     protected function showScriptVariables()
488     {
489         $vars = array();
490
491         if (Event::handle('StartScriptVariables', array($this, &$vars))) {
492             $vars['urlNewNotice'] = common_local_url('newnotice');
493         }
494         if (!empty($vars)) {
495             $this->inlineScript('SN.V = ' . json_encode($vars));
496         }
497         return $vars;
498     }
499
500     /**
501      * If the action will need localizable text strings, export them here like so:
502      *
503      * return array('pool_deepend' => _('Deep end'),
504      *              'pool_shallow' => _('Shallow end'));
505      *
506      * The exported map will be available via SN.msg() to JS code:
507      *
508      *   $('#pool').html('<div class="deepend"></div><div class="shallow"></div>');
509      *   $('#pool .deepend').text(SN.msg('pool_deepend'));
510      *   $('#pool .shallow').text(SN.msg('pool_shallow'));
511      *
512      * Exports a map of localized text strings to JavaScript code.
513      *
514      * Plugins can add to what's exported on any action by hooking the StartScriptMessages or
515      * EndScriptMessages events and appending to the array. Try to avoid adding strings that won't
516      * be used, as they'll be added to HTML output.
517      */
518     function getScriptMessages()
519     {
520         return array();
521     }
522
523     /**
524      * Show OpenSearch headers
525      *
526      * @return nothing
527      */
528     function showOpenSearch()
529     {
530         $this->element('link', array('rel' => 'search',
531                                      'type' => 'application/opensearchdescription+xml',
532                                      'href' =>  common_local_url('opensearch', array('type' => 'people')),
533                                      'title' => common_config('site', 'name').' People Search'));
534         $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
535                                      'href' =>  common_local_url('opensearch', array('type' => 'notice')),
536                                      'title' => common_config('site', 'name').' Notice Search'));
537     }
538
539     /**
540      * Show feed headers
541      *
542      * MAY overload
543      *
544      * @return nothing
545      */
546     function showFeeds()
547     {
548         foreach ($this->getFeeds() as $feed) {
549             $this->element('link', array('rel' => $feed->rel(),
550                                          'href' => $feed->url,
551                                          'type' => $feed->mimeType(),
552                                          'title' => $feed->title));
553         }
554     }
555
556     /**
557      * Show description.
558      *
559      * SHOULD overload
560      *
561      * @return nothing
562      */
563     function showDescription()
564     {
565         // does nothing by default
566     }
567
568     /**
569      * Show extra stuff in <head>.
570      *
571      * MAY overload
572      *
573      * @return nothing
574      */
575     function extraHead()
576     {
577         // does nothing by default
578     }
579
580     /**
581      * Show body.
582      *
583      * Calls template methods
584      *
585      * @return nothing
586      */
587     function showBody()
588     {
589         $params = array('id' => $this->getActionName());
590         if ($this->scoped instanceof Profile) {
591             $params['class'] = 'user_in';
592         }
593         $this->elementStart('body', $params);
594         $this->elementStart('div', array('id' => 'wrap'));
595         if (Event::handle('StartShowHeader', array($this))) {
596             $this->showHeader();
597             $this->flush();
598             Event::handle('EndShowHeader', array($this));
599         }
600         $this->showCore();
601         $this->flush();
602         if (Event::handle('StartShowFooter', array($this))) {
603             $this->showFooter();
604             $this->flush();
605             Event::handle('EndShowFooter', array($this));
606         }
607         $this->elementEnd('div');
608         $this->showScripts();
609         $this->elementEnd('body');
610     }
611
612     /**
613      * Show header of the page.
614      *
615      * Calls template methods
616      *
617      * @return nothing
618      */
619     function showHeader()
620     {
621         $this->elementStart('div', array('id' => 'header'));
622         $this->showLogo();
623         $this->showPrimaryNav();
624         if (Event::handle('StartShowSiteNotice', array($this))) {
625             $this->showSiteNotice();
626
627             Event::handle('EndShowSiteNotice', array($this));
628         }
629
630         $this->elementEnd('div');
631     }
632
633     /**
634      * Show configured logo.
635      *
636      * @return nothing
637      */
638     function showLogo()
639     {
640         $this->elementStart('address', array('id' => 'site_contact', 'class' => 'h-card'));
641         if (Event::handle('StartAddressData', array($this))) {
642             if (common_config('singleuser', 'enabled')) {
643                 $user = User::singleUser();
644                 $url = common_local_url('showstream',
645                                         array('nickname' => $user->nickname));
646             } else if (common_logged_in()) {
647                 $cur = common_current_user();
648                 $url = common_local_url('all', array('nickname' => $cur->nickname));
649             } else {
650                 $url = common_local_url('public');
651             }
652
653             $this->elementStart('a', array('class' => 'home bookmark',
654                                            'href' => $url));
655
656             if (GNUsocial::isHTTPS()) {
657                 $logoUrl = common_config('site', 'ssllogo');
658                 if (empty($logoUrl)) {
659                     // if logo is an uploaded file, try to fall back to HTTPS file URL
660                     $httpUrl = common_config('site', 'logo');
661                     if (!empty($httpUrl)) {
662                         $f = File::getKV('url', $httpUrl);
663                         if (!empty($f) && !empty($f->filename)) {
664                             // this will handle the HTTPS case
665                             $logoUrl = File::url($f->filename);
666                         }
667                     }
668                 }
669             } else {
670                 $logoUrl = common_config('site', 'logo');
671             }
672
673             if (empty($logoUrl) && file_exists(Theme::file('logo.png'))) {
674                 // This should handle the HTTPS case internally
675                 $logoUrl = Theme::path('logo.png');
676             }
677
678             if (!empty($logoUrl)) {
679                 $this->element('img', array('class' => 'logo u-photo p-name',
680                                             'src' => $logoUrl,
681                                             'alt' => common_config('site', 'name')));
682             }
683
684             $this->elementEnd('a');
685
686             Event::handle('EndAddressData', array($this));
687         }
688         $this->elementEnd('address');
689     }
690
691     /**
692      * Show primary navigation.
693      *
694      * @return nothing
695      */
696     function showPrimaryNav()
697     {
698         $this->elementStart('div', array('id' => 'site_nav_global_primary'));
699
700         $user = common_current_user();
701
702         if (!empty($user) || !common_config('site', 'private')) {
703             $form = new SearchForm($this);
704             $form->show();
705         }
706
707         $pn = new PrimaryNav($this);
708         $pn->show();
709         $this->elementEnd('div');
710     }
711
712     /**
713      * Show site notice.
714      *
715      * @return nothing
716      */
717     function showSiteNotice()
718     {
719         // Revist. Should probably do an hAtom pattern here
720         $text = common_config('site', 'notice');
721         if ($text) {
722             $this->elementStart('div', array('id' => 'site_notice',
723                                             'class' => 'system_notice'));
724             $this->raw($text);
725             $this->elementEnd('div');
726         }
727     }
728
729     /**
730      * Show notice form.
731      *
732      * MAY overload if no notice form needed... or direct message box????
733      *
734      * @return nothing
735      */
736     function showNoticeForm()
737     {
738         // TRANS: Tab on the notice form.
739         $tabs = array('status' => array('title' => _m('TAB','Status'),
740                                         'href'  => common_local_url('newnotice')));
741
742         $this->elementStart('div', 'input_forms');
743
744         $this->element('label', array('for'=>'input_form_nav'), _m('TAB', 'Share your:'));
745
746         if (Event::handle('StartShowEntryForms', array(&$tabs))) {
747             $this->elementStart('ul', array('class' => 'nav',
748                                             'id' => 'input_form_nav'));
749
750             foreach ($tabs as $tag => $data) {
751                 $tag = htmlspecialchars($tag);
752                 $attrs = array('id' => 'input_form_nav_'.$tag,
753                                'class' => 'input_form_nav_tab');
754
755                 if ($tag == 'status') {
756                     $attrs['class'] .= ' current';
757                 }
758                 $this->elementStart('li', $attrs);
759
760                 $this->element('a',
761                                array('onclick' => 'return SN.U.switchInputFormTab("'.$tag.'");',
762                                      'href' => $data['href']),
763                                $data['title']);
764                 $this->elementEnd('li');
765             }
766
767             $this->elementEnd('ul');
768
769             foreach ($tabs as $tag => $data) {
770                 $attrs = array('class' => 'input_form',
771                                'id' => 'input_form_'.$tag);
772                 if ($tag == 'status') {
773                     $attrs['class'] .= ' current';
774                 }
775
776                 $this->elementStart('div', $attrs);
777
778                 $form = null;
779
780                 if (Event::handle('StartMakeEntryForm', array($tag, $this, &$form))) {
781                     if ($tag == 'status') {
782                         $options = $this->noticeFormOptions();
783                         $form = new NoticeForm($this, $options);
784                     }
785                     Event::handle('EndMakeEntryForm', array($tag, $this, $form));
786                 }
787
788                 if (!empty($form)) {
789                     $form->show();
790                 }
791
792                 $this->elementEnd('div');
793             }
794         }
795
796         $this->elementEnd('div');
797     }
798
799     function noticeFormOptions()
800     {
801         return array();
802     }
803
804     /**
805      * Show anonymous message.
806      *
807      * SHOULD overload
808      *
809      * @return nothing
810      */
811     function showAnonymousMessage()
812     {
813         // needs to be defined by the class
814     }
815
816     /**
817      * Show core.
818      *
819      * Shows local navigation, content block and aside.
820      *
821      * @return nothing
822      */
823     function showCore()
824     {
825         $this->elementStart('div', array('id' => 'core'));
826         $this->elementStart('div', array('id' => 'aside_primary_wrapper'));
827         $this->elementStart('div', array('id' => 'content_wrapper'));
828         $this->elementStart('div', array('id' => 'site_nav_local_views_wrapper'));
829         if (Event::handle('StartShowLocalNavBlock', array($this))) {
830             $this->showLocalNavBlock();
831             $this->flush();
832             Event::handle('EndShowLocalNavBlock', array($this));
833         }
834         if (Event::handle('StartShowContentBlock', array($this))) {
835             $this->showContentBlock();
836             $this->flush();
837             Event::handle('EndShowContentBlock', array($this));
838         }
839         if (Event::handle('StartShowAside', array($this))) {
840             $this->showAside();
841             $this->flush();
842             Event::handle('EndShowAside', array($this));
843         }
844         $this->elementEnd('div');
845         $this->elementEnd('div');
846         $this->elementEnd('div');
847         $this->elementEnd('div');
848     }
849
850     /**
851      * Show local navigation block.
852      *
853      * @return nothing
854      */
855     function showLocalNavBlock()
856     {
857         // Need to have this ID for CSS; I'm too lazy to add it to
858         // all menus
859         $this->elementStart('div', array('id' => 'site_nav_local_views'));
860         // Cheat cheat cheat!
861         $this->showLocalNav();
862         $this->elementEnd('div');
863     }
864
865     /**
866      * If there's a logged-in user, show a bit of login context
867      *
868      * @return nothing
869      */
870     function showProfileBlock()
871     {
872         if (common_logged_in()) {
873             $block = new DefaultProfileBlock($this);
874             $block->show();
875         }
876     }
877
878     /**
879      * Show local navigation.
880      *
881      * SHOULD overload
882      *
883      * @return nothing
884      */
885     function showLocalNav()
886     {
887         $nav = new DefaultLocalNav($this);
888         $nav->show();
889     }
890
891     /**
892      * Show menu for an object (group, profile)
893      *
894      * This block will only show if a subclass has overridden
895      * the showObjectNav() method.
896      *
897      * @return nothing
898      */
899     function showObjectNavBlock()
900     {
901         $rmethod = new ReflectionMethod($this, 'showObjectNav');
902         $dclass = $rmethod->getDeclaringClass()->getName();
903
904         if ($dclass != 'Action') {
905             // Need to have this ID for CSS; I'm too lazy to add it to
906             // all menus
907             $this->elementStart('div', array('id' => 'site_nav_object',
908                                              'class' => 'section'));
909             $this->showObjectNav();
910             $this->elementEnd('div');
911         }
912     }
913
914     /**
915      * Show object navigation.
916      *
917      * If there are things to do with this object, show it here.
918      *
919      * @return nothing
920      */
921     function showObjectNav()
922     {
923         /* Nothing here. */
924     }
925
926     /**
927      * Show content block.
928      *
929      * @return nothing
930      */
931     function showContentBlock()
932     {
933         $this->elementStart('div', array('id' => 'content'));
934         if (common_logged_in()) {
935             if (Event::handle('StartShowNoticeForm', array($this))) {
936                 $this->showNoticeForm();
937                 Event::handle('EndShowNoticeForm', array($this));
938             }
939         }
940         if (Event::handle('StartShowPageTitle', array($this))) {
941             $this->showPageTitle();
942             Event::handle('EndShowPageTitle', array($this));
943         }
944         $this->showPageNoticeBlock();
945         $this->elementStart('div', array('id' => 'content_inner'));
946         // show the actual content (forms, lists, whatever)
947         $this->showContent();
948         $this->elementEnd('div');
949         $this->elementEnd('div');
950     }
951
952     /**
953      * Show page title.
954      *
955      * @return nothing
956      */
957     function showPageTitle()
958     {
959         $this->element('h1', null, $this->title());
960     }
961
962     /**
963      * Show page notice block.
964      *
965      * Only show the block if a subclassed action has overrided
966      * Action::showPageNotice(), or an event handler is registered for
967      * the StartShowPageNotice event, in which case we assume the
968      * 'page_notice' definition list is desired.  This is to prevent
969      * empty 'page_notice' definition lists from being output everywhere.
970      *
971      * @return nothing
972      */
973     function showPageNoticeBlock()
974     {
975         $rmethod = new ReflectionMethod($this, 'showPageNotice');
976         $dclass = $rmethod->getDeclaringClass()->getName();
977
978         if ($dclass != 'Action' || Event::hasHandler('StartShowPageNotice')) {
979
980             $this->elementStart('div', array('id' => 'page_notice',
981                                             'class' => 'system_notice'));
982             if (Event::handle('StartShowPageNotice', array($this))) {
983                 $this->showPageNotice();
984                 Event::handle('EndShowPageNotice', array($this));
985             }
986             $this->elementEnd('div');
987         }
988     }
989
990     /**
991      * Show page notice.
992      *
993      * SHOULD overload (unless there's not a notice)
994      *
995      * @return nothing
996      */
997     function showPageNotice()
998     {
999     }
1000
1001     /**
1002      * Show content.
1003      *
1004      * MUST overload (unless there's not a notice)
1005      *
1006      * @return nothing
1007      */
1008     protected function showContent()
1009     {
1010     }
1011
1012     /**
1013      * Show Aside.
1014      *
1015      * @return nothing
1016      */
1017     function showAside()
1018     {
1019         $this->elementStart('div', array('id' => 'aside_primary',
1020                                          'class' => 'aside'));
1021         $this->showProfileBlock();
1022         if (Event::handle('StartShowObjectNavBlock', array($this))) {
1023             $this->showObjectNavBlock();
1024             Event::handle('EndShowObjectNavBlock', array($this));
1025         }
1026         if (Event::handle('StartShowSections', array($this))) {
1027             $this->showSections();
1028             Event::handle('EndShowSections', array($this));
1029         }
1030         if (Event::handle('StartShowExportData', array($this))) {
1031             $this->showExportData();
1032             Event::handle('EndShowExportData', array($this));
1033         }
1034         $this->elementEnd('div');
1035     }
1036
1037     /**
1038      * Show export data feeds.
1039      *
1040      * @return void
1041      */
1042     function showExportData()
1043     {
1044         $feeds = $this->getFeeds();
1045         if (!empty($feeds)) {
1046             $fl = new FeedList($this, $feeds);
1047             $fl->show();
1048         }
1049     }
1050
1051     /**
1052      * Show sections.
1053      *
1054      * SHOULD overload
1055      *
1056      * @return nothing
1057      */
1058     function showSections()
1059     {
1060         // for each section, show it
1061     }
1062
1063     /**
1064      * Show footer.
1065      *
1066      * @return nothing
1067      */
1068     function showFooter()
1069     {
1070         $this->elementStart('div', array('id' => 'footer'));
1071         if (Event::handle('StartShowInsideFooter', array($this))) {
1072             $this->showSecondaryNav();
1073             $this->showLicenses();
1074             Event::handle('EndShowInsideFooter', array($this));
1075         }
1076         $this->elementEnd('div');
1077     }
1078
1079     /**
1080      * Show secondary navigation.
1081      *
1082      * @return nothing
1083      */
1084     function showSecondaryNav()
1085     {
1086         $sn = new SecondaryNav($this);
1087         $sn->show();
1088     }
1089
1090     /**
1091      * Show licenses.
1092      *
1093      * @return nothing
1094      */
1095     function showLicenses()
1096     {
1097         $this->showGNUsocialLicense();
1098         $this->showContentLicense();
1099     }
1100
1101     /**
1102      * Show GNU social license.
1103      *
1104      * @return nothing
1105      */
1106     function showGNUsocialLicense()
1107     {
1108         if (common_config('site', 'broughtby')) {
1109             // TRANS: First sentence of the GNU social site license. Used if 'broughtby' is set.
1110             // TRANS: Text between [] is a link description, text between () is the link itself.
1111             // TRANS: Make sure there is no whitespace between "]" and "(".
1112             // TRANS: "%%site.broughtby%%" is the value of the variable site.broughtby
1113             $instr = _('**%%site.name%%** is a social network, courtesy of [%%site.broughtby%%](%%site.broughtbyurl%%).');
1114         } else {
1115             // TRANS: First sentence of the GNU social site license. Used if 'broughtby' is not set.
1116             $instr = _('**%%site.name%%** is a social network.');
1117         }
1118         $instr .= ' ';
1119         // TRANS: Second sentence of the GNU social site license. Mentions the GNU social source code license.
1120         // TRANS: Make sure there is no whitespace between "]" and "(".
1121         // TRANS: [%1$s](%2$s) is a link description followed by the link itself
1122         // TRANS: %3$s is the version of GNU social that is being used.
1123         $instr .= sprintf(_('It runs on [%1$s](%2$s), version %3$s, available under the [GNU Affero General Public License](http://www.fsf.org/licensing/licenses/agpl-3.0.html).'), GNUSOCIAL_ENGINE, GNUSOCIAL_ENGINE_URL, GNUSOCIAL_VERSION);
1124         $output = common_markup_to_html($instr);
1125         $this->raw($output);
1126         // do it
1127     }
1128
1129     /**
1130      * Show content license.
1131      *
1132      * @return nothing
1133      */
1134     function showContentLicense()
1135     {
1136         if (Event::handle('StartShowContentLicense', array($this))) {
1137             switch (common_config('license', 'type')) {
1138             case 'private':
1139                 // TRANS: Content license displayed when license is set to 'private'.
1140                 // TRANS: %1$s is the site name.
1141                 $this->element('p', null, sprintf(_('Content and data of %1$s are private and confidential.'),
1142                                                   common_config('site', 'name')));
1143                 // fall through
1144             case 'allrightsreserved':
1145                 if (common_config('license', 'owner')) {
1146                     // TRANS: Content license displayed when license is set to 'allrightsreserved'.
1147                     // TRANS: %1$s is the copyright owner.
1148                     $this->element('p', null, sprintf(_('Content and data copyright by %1$s. All rights reserved.'),
1149                                                       common_config('license', 'owner')));
1150                 } else {
1151                     // TRANS: Content license displayed when license is set to 'allrightsreserved' and no owner is set.
1152                     $this->element('p', null, _('Content and data copyright by contributors. All rights reserved.'));
1153                 }
1154                 break;
1155             case 'cc': // fall through
1156             default:
1157                 $this->elementStart('p');
1158
1159                 $image    = common_config('license', 'image');
1160                 $sslimage = common_config('license', 'sslimage');
1161
1162                 if (GNUsocial::isHTTPS()) {
1163                     if (!empty($sslimage)) {
1164                         $url = $sslimage;
1165                     } else if (preg_match('#^http://i.creativecommons.org/#', $image)) {
1166                         // CC support HTTPS on their images
1167                         $url = preg_replace('/^http/', 'https', $image, 1);
1168                     } else {
1169                         // Better to show mixed content than no content
1170                         $url = $image;
1171                     }
1172                 } else {
1173                     $url = $image;
1174                 }
1175
1176                 $this->element('img', array('id' => 'license_cc',
1177                                             'src' => $url,
1178                                             'alt' => common_config('license', 'title'),
1179                                             'width' => '80',
1180                                             'height' => '15'));
1181                 $this->text(' ');
1182                 // TRANS: license message in footer.
1183                 // TRANS: %1$s is the site name, %2$s is a link to the license URL, with a licence name set in configuration.
1184                 $notice = _('All %1$s content and data are available under the %2$s license.');
1185                 $link = sprintf('<a class="license" rel="external license" href="%1$s">%2$s</a>',
1186                                 htmlspecialchars(common_config('license', 'url')),
1187                                 htmlspecialchars(common_config('license', 'title')));
1188                 $this->raw(@sprintf(htmlspecialchars($notice),
1189                                    htmlspecialchars(common_config('site', 'name')),
1190                                    $link));
1191                 $this->elementEnd('p');
1192                 break;
1193             }
1194
1195             Event::handle('EndShowContentLicense', array($this));
1196         }
1197     }
1198
1199     /**
1200      * Return last modified, if applicable.
1201      *
1202      * MAY override
1203      *
1204      * @return string last modified http header
1205      */
1206     function lastModified()
1207     {
1208         // For comparison with If-Last-Modified
1209         // If not applicable, return null
1210         return null;
1211     }
1212
1213     /**
1214      * Return etag, if applicable.
1215      *
1216      * MAY override
1217      *
1218      * @return string etag http header
1219      */
1220     function etag()
1221     {
1222         return null;
1223     }
1224
1225     /**
1226      * Return true if read only.
1227      *
1228      * MAY override
1229      *
1230      * @param array $args other arguments
1231      *
1232      * @return boolean is read only action?
1233      */
1234     function isReadOnly($args)
1235     {
1236         return false;
1237     }
1238
1239     /**
1240      * Returns query argument or default value if not found
1241      *
1242      * @param string $key requested argument
1243      * @param string $def default value to return if $key is not provided
1244      *
1245      * @return boolean is read only action?
1246      */
1247     function arg($key, $def=null)
1248     {
1249         if (array_key_exists($key, $this->args)) {
1250             return $this->args[$key];
1251         } else {
1252             return $def;
1253         }
1254     }
1255
1256     /**
1257      * Returns trimmed query argument or default value if not found
1258      *
1259      * @param string $key requested argument
1260      * @param string $def default value to return if $key is not provided
1261      *
1262      * @return boolean is read only action?
1263      */
1264     function trimmed($key, $def=null)
1265     {
1266         $arg = $this->arg($key, $def);
1267         return is_string($arg) ? trim($arg) : $arg;
1268     }
1269
1270     /**
1271      * Handler method
1272      *
1273      * @return boolean is read only action?
1274      */
1275     protected function handle()
1276     {
1277         header('Vary: Accept-Encoding,Cookie');
1278
1279         $lm   = $this->lastModified();
1280         $etag = $this->etag();
1281
1282         if ($etag) {
1283             header('ETag: ' . $etag);
1284         }
1285
1286         if ($lm) {
1287             header('Last-Modified: ' . date(DATE_RFC1123, $lm));
1288             if ($this->isCacheable()) {
1289                 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1290                 header( "Cache-Control: private, must-revalidate, max-age=0" );
1291                 header( "Pragma:");
1292             }
1293         }
1294
1295         $checked = false;
1296         if ($etag) {
1297             $if_none_match = (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) ?
1298               $_SERVER['HTTP_IF_NONE_MATCH'] : null;
1299             if ($if_none_match) {
1300                 // If this check fails, ignore the if-modified-since below.
1301                 $checked = true;
1302                 if ($this->_hasEtag($etag, $if_none_match)) {
1303                     header('HTTP/1.1 304 Not Modified');
1304                     // Better way to do this?
1305                     exit(0);
1306                 }
1307             }
1308         }
1309
1310         if (!$checked && $lm && array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
1311             $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
1312             $ims = strtotime($if_modified_since);
1313             if ($lm <= $ims) {
1314                 header('HTTP/1.1 304 Not Modified');
1315                 // Better way to do this?
1316                 exit(0);
1317             }
1318         }
1319     }
1320
1321     /**
1322      * Is this action cacheable?
1323      *
1324      * If the action returns a last-modified
1325      *
1326      * @param array $argarray is ignored since it's now passed in in prepare()
1327      *
1328      * @return boolean is read only action?
1329      */
1330     function isCacheable()
1331     {
1332         return true;
1333     }
1334
1335     /**
1336      * Has etag? (private)
1337      *
1338      * @param string $etag          etag http header
1339      * @param string $if_none_match ifNoneMatch http header
1340      *
1341      * @return boolean
1342      */
1343     function _hasEtag($etag, $if_none_match)
1344     {
1345         $etags = explode(',', $if_none_match);
1346         return in_array($etag, $etags) || in_array('*', $etags);
1347     }
1348
1349     /**
1350      * Boolean understands english (yes, no, true, false)
1351      *
1352      * @param string $key query key we're interested in
1353      * @param string $def default value
1354      *
1355      * @return boolean interprets yes/no strings as boolean
1356      */
1357     function boolean($key, $def=false)
1358     {
1359         $arg = strtolower($this->trimmed($key));
1360
1361         if (is_null($arg)) {
1362             return $def;
1363         } else if (in_array($arg, array('true', 'yes', '1', 'on'))) {
1364             return true;
1365         } else if (in_array($arg, array('false', 'no', '0'))) {
1366             return false;
1367         } else {
1368             return $def;
1369         }
1370     }
1371
1372     /**
1373      * This is a cheap hack to avoid a bug in DB_DataObject
1374      * where '' is non-type-aware compared to 0, which means it
1375      * will always be true for values like false and 0 too...
1376      *
1377      * Upstream bug is::
1378      * https://pear.php.net/bugs/bug.php?id=20291
1379      */
1380     function booleanintstring($key, $def=false)
1381     {
1382         return $this->boolean($key, $def) ? '1' : '0';
1383     }
1384
1385     /**
1386      * Integer value of an argument
1387      *
1388      * @param string $key      query key we're interested in
1389      * @param string $defValue optional default value (default null)
1390      * @param string $maxValue optional max value (default null)
1391      * @param string $minValue optional min value (default null)
1392      *
1393      * @return integer integer value
1394      */
1395     function int($key, $defValue=null, $maxValue=null, $minValue=null)
1396     {
1397         $arg = intval($this->arg($key));
1398
1399         if (!is_numeric($this->arg($key)) || $arg != $this->arg($key)) {
1400             return $defValue;
1401         }
1402
1403         if (!is_null($maxValue)) {
1404             $arg = min($arg, $maxValue);
1405         }
1406
1407         if (!is_null($minValue)) {
1408             $arg = max($arg, $minValue);
1409         }
1410
1411         return $arg;
1412     }
1413
1414     /**
1415      * Server error
1416      *
1417      * @param string  $msg  error message to display
1418      * @param integer $code http error code, 500 by default
1419      *
1420      * @return nothing
1421      */
1422     function serverError($msg, $code=500, $format=null)
1423     {
1424         if ($format === null) {
1425             $format = $this->format;
1426         }
1427
1428         common_debug("Server error '{$code}' on '{$this->action}': {$msg}", __FILE__);
1429
1430         if (!array_key_exists($code, ServerErrorAction::$status)) {
1431             $code = 500;
1432         }
1433
1434         $status_string = ServerErrorAction::$status[$code];
1435
1436         switch ($format) {
1437         case 'xml':
1438             header("HTTP/1.1 {$code} {$status_string}");
1439             $this->initDocument('xml');
1440             $this->elementStart('hash');
1441             $this->element('error', null, $msg);
1442             $this->element('request', null, $_SERVER['REQUEST_URI']);
1443             $this->elementEnd('hash');
1444             $this->endDocument('xml');
1445             break;
1446         case 'json':
1447             if (!isset($this->callback)) {
1448                 header("HTTP/1.1 {$code} {$status_string}");
1449             }
1450             $this->initDocument('json');
1451             $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
1452             print(json_encode($error_array));
1453             $this->endDocument('json');
1454             break;
1455         default:
1456             common_log(LOG_ERR, 'Handled serverError ('._ve($code).') but cannot output into desired format ('._ve($this->format).'): '._ve($msg));
1457             $action = new ServerErrorAction($msg, $code);
1458             $action->execute();
1459         }
1460
1461         exit((int)$code);
1462     }
1463
1464     /**
1465      * Client error
1466      *
1467      * @param string  $msg    error message to display
1468      * @param integer $code   http error code, 400 by default
1469      * @param string  $format error format (json, xml, text) for ApiAction
1470      *
1471      * @return nothing
1472      * @throws ClientException always
1473      */
1474     function clientError($msg, $code=400, $format=null)
1475     {
1476         // $format is currently only relevant for an ApiAction anyway
1477         if ($format === null) {
1478             $format = $this->format;
1479         }
1480
1481         common_debug("User error '{$code}' on '{$this->action}': {$msg}", __FILE__);
1482
1483         if (!array_key_exists($code, ClientErrorAction::$status)) {
1484             $code = 400;
1485         }
1486
1487         $status_string = ClientErrorAction::$status[$code];
1488
1489         switch ($format) {
1490         case 'xml':
1491             header("HTTP/1.1 {$code} {$status_string}");
1492             $this->initDocument('xml');
1493             $this->elementStart('hash');
1494             $this->element('error', null, $msg);
1495             $this->element('request', null, $_SERVER['REQUEST_URI']);
1496             $this->elementEnd('hash');
1497             $this->endDocument('xml');
1498             break;
1499         case 'json':
1500             if (!isset($this->callback)) {
1501                 header("HTTP/1.1 {$code} {$status_string}");
1502             }
1503             $this->initDocument('json');
1504             $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
1505             print(json_encode($error_array));
1506             $this->endDocument('json');
1507             break;
1508         case 'text':
1509             header("HTTP/1.1 {$code} {$status_string}");
1510             header('Content-Type: text/plain; charset=utf-8');
1511             echo $msg;
1512             break;
1513         default:
1514             common_log(LOG_ERR, 'Handled clientError ('._ve($code).') but cannot output into desired format ('._ve($this->format).'): '._ve($msg));
1515             $action = new ClientErrorAction($msg, $code);
1516             $action->execute();
1517         }
1518         exit((int)$code);
1519     }
1520
1521     /**
1522      * If not logged in, take appropriate action (redir or exception)
1523      *
1524      * @param boolean $redir Redirect to login if not logged in
1525      *
1526      * @return boolean true if logged in (never returns if not)
1527      */
1528     public function checkLogin($redir=true)
1529     {
1530         if (common_logged_in()) {
1531             return true;
1532         }
1533
1534         if ($redir==true) {
1535             common_set_returnto($_SERVER['REQUEST_URI']);
1536             common_redirect(common_local_url('login'));
1537         }
1538
1539         // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
1540         $this->clientError(_('Not logged in.'), 403);
1541     }
1542
1543     /**
1544      * Returns the current URL
1545      *
1546      * @return string current URL
1547      */
1548     function selfUrl()
1549     {
1550         list($action, $args) = $this->returnToArgs();
1551         return common_local_url($action, $args);
1552     }
1553
1554     /**
1555      * Returns arguments sufficient for re-constructing URL
1556      *
1557      * @return array two elements: action, other args
1558      */
1559     function returnToArgs()
1560     {
1561         $action = $this->getActionName();
1562         $args   = $this->args;
1563         unset($args['action']);
1564         if (common_config('site', 'fancy')) {
1565             unset($args['p']);
1566         }
1567         if (array_key_exists('submit', $args)) {
1568             unset($args['submit']);
1569         }
1570         foreach (array_keys($_COOKIE) as $cookie) {
1571             unset($args[$cookie]);
1572         }
1573         return array($action, $args);
1574     }
1575
1576     /**
1577      * Generate a menu item
1578      *
1579      * @param string  $url         menu URL
1580      * @param string  $text        menu name
1581      * @param string  $title       title attribute, null by default
1582      * @param boolean $is_selected current menu item, false by default
1583      * @param string  $id          element id, null by default
1584      *
1585      * @return nothing
1586      */
1587     function menuItem($url, $text, $title=null, $is_selected=false, $id=null, $class=null)
1588     {
1589         // Added @id to li for some control.
1590         // XXX: We might want to move this to htmloutputter.php
1591         $lattrs  = array();
1592         $classes = array();
1593         if ($class !== null) {
1594             $classes[] = trim($class);
1595         }
1596         if ($is_selected) {
1597             $classes[] = 'current';
1598         }
1599
1600         if (!empty($classes)) {
1601             $lattrs['class'] = implode(' ', $classes);
1602         }
1603
1604         if (!is_null($id)) {
1605             $lattrs['id'] = $id;
1606         }
1607
1608         $this->elementStart('li', $lattrs);
1609         $attrs['href'] = $url;
1610         if ($title) {
1611             $attrs['title'] = $title;
1612         }
1613         $this->element('a', $attrs, $text);
1614         $this->elementEnd('li');
1615     }
1616
1617     /**
1618      * Generate pagination links
1619      *
1620      * @param boolean $have_before is there something before?
1621      * @param boolean $have_after  is there something after?
1622      * @param integer $page        current page
1623      * @param string  $action      current action
1624      * @param array   $args        rest of query arguments
1625      *
1626      * @return nothing
1627      */
1628     // XXX: The messages in this pagination method only tailor to navigating
1629     //      notices. In other lists, "Previous"/"Next" type navigation is
1630     //      desirable, but not available.
1631     function pagination($have_before, $have_after, $page, $action, $args=null)
1632     {
1633         // Does a little before-after block for next/prev page
1634         if ($have_before || $have_after) {
1635             $this->elementStart('ul', array('class' => 'nav',
1636                                             'id' => 'pagination'));
1637         }
1638         if ($have_before) {
1639             $pargs   = array('page' => $page-1);
1640             $this->elementStart('li', array('class' => 'nav_prev'));
1641             $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1642                                       'rel' => 'prev'),
1643                            // TRANS: Pagination message to go to a page displaying information more in the
1644                            // TRANS: present than the currently displayed information.
1645                            _('After'));
1646             $this->elementEnd('li');
1647         }
1648         if ($have_after) {
1649             $pargs   = array('page' => $page+1);
1650             $this->elementStart('li', array('class' => 'nav_next'));
1651             $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1652                                       'rel' => 'next'),
1653                            // TRANS: Pagination message to go to a page displaying information more in the
1654                            // TRANS: past than the currently displayed information.
1655                            _('Before'));
1656             $this->elementEnd('li');
1657         }
1658         if ($have_before || $have_after) {
1659             $this->elementEnd('ul');
1660         }
1661     }
1662
1663     /**
1664      * An array of feeds for this action.
1665      *
1666      * Returns an array of potential feeds for this action.
1667      *
1668      * @return array Feed object to show in head and links
1669      */
1670     function getFeeds()
1671     {
1672         return array();
1673     }
1674
1675     /**
1676      * Check the session token.
1677      *
1678      * Checks that the current form has the correct session token,
1679      * and throw an exception if it does not.
1680      *
1681      * @return void
1682      */
1683     // XXX: Finding this type of check with the same message about 50 times.
1684     //      Possible to refactor?
1685     function checkSessionToken()
1686     {
1687         // CSRF protection
1688         $token = $this->trimmed('token');
1689         if (empty($token) || $token != common_session_token()) {
1690             // TRANS: Client error text when there is a problem with the session token.
1691             $this->clientError(_('There was a problem with your session token.'));
1692         }
1693     }
1694
1695     /**
1696      * Check if the current request is a POST
1697      *
1698      * @return boolean true if POST; otherwise false.
1699      */
1700
1701     function isPost()
1702     {
1703         return ($_SERVER['REQUEST_METHOD'] == 'POST');
1704     }
1705 }