]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/action.php
Merge branch 'master' into mmn_fixes
[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, such as using the
398         // Google Font APIs to load webfonts.
399         foreach ($theme->getExternals() as $url) {
400             $this->cssLink($url, $mainTheme, $media);
401         }
402
403         // If the currently-selected theme has dependencies on other themes,
404         // we'll need to load their display.css files as well in order.
405         $baseThemes = $theme->getDeps();
406         foreach ($baseThemes as $baseTheme) {
407             $this->cssLink('css/display.css', $baseTheme, $media);
408         }
409         $this->cssLink('css/display.css', $mainTheme, $media);
410
411         // Additional styles for RTL languages
412         if (is_rtl(common_language())) {
413             if (file_exists(Theme::file('css/rtl.css'))) {
414                 $this->cssLink('css/rtl.css', $mainTheme, $media);
415             }
416         }
417     }
418
419     /**
420      * Show javascript headers
421      *
422      * @return nothing
423      */
424     function showScripts()
425     {
426         if (Event::handle('StartShowScripts', array($this))) {
427             if (Event::handle('StartShowJQueryScripts', array($this))) {
428                 $this->script('extlib/jquery.js');
429                 $this->script('extlib/jquery.form.js');
430                 $this->script('extlib/jquery-ui/jquery-ui.js');
431                 $this->script('extlib/jquery.cookie.js');
432                 $this->inlineScript('if (typeof window.JSON !== "object") { $.getScript("'.common_path('js/extlib/json2.js', GNUsocial::isHTTPS()).'"); }');
433
434                 Event::handle('EndShowJQueryScripts', array($this));
435             }
436             if (Event::handle('StartShowStatusNetScripts', array($this))) {
437                 $this->script('util.js');
438                 $this->script('xbImportNode.js');
439                 $this->script('geometa.js');
440
441                 // This route isn't available in single-user mode.
442                 // Not sure why, but it causes errors here.
443                 $this->inlineScript('var _peopletagAC = "' .
444                                     common_local_url('peopletagautocomplete') . '";');
445                 $this->showScriptMessages();
446                 $this->showScriptVariables();
447                 // Anti-framing code to avoid clickjacking attacks in older browsers.
448                 // This will show a blank page if the page is being framed, which is
449                 // consistent with the behavior of the 'X-Frame-Options: SAMEORIGIN'
450                 // header, which prevents framing in newer browser.
451                 if (common_config('javascript', 'bustframes')) {
452                     $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 = ""; }; }');
453                 }
454                 Event::handle('EndShowStatusNetScripts', array($this));
455             }
456             Event::handle('EndShowScripts', array($this));
457         }
458     }
459
460     /**
461      * Exports a map of localized text strings to JavaScript code.
462      *
463      * Plugins can add to what's exported by hooking the StartScriptMessages or EndScriptMessages
464      * events and appending to the array. Try to avoid adding strings that won't be used, as
465      * they'll be added to HTML output.
466      */
467     function showScriptMessages()
468     {
469         $messages = array();
470
471         if (Event::handle('StartScriptMessages', array($this, &$messages))) {
472             // Common messages needed for timeline views etc...
473
474             // TRANS: Localized tooltip for '...' expansion button on overlong remote messages.
475             $messages['showmore_tooltip'] = _m('TOOLTIP', 'Show more');
476             $messages['popup_close_button'] = _m('TOOLTIP', 'Close popup');
477
478             $messages = array_merge($messages, $this->getScriptMessages());
479
480             Event::handle('EndScriptMessages', array($this, &$messages));
481         }
482
483         if (!empty($messages)) {
484             $this->inlineScript('SN.messages=' . json_encode($messages));
485         }
486
487         return $messages;
488     }
489
490     protected function showScriptVariables()
491     {
492         $vars = array();
493
494         if (Event::handle('StartScriptVariables', array($this, &$vars))) {
495             $vars['urlNewNotice'] = common_local_url('newnotice');
496         }
497         if (!empty($vars)) {
498             $this->inlineScript('SN.V = ' . json_encode($vars));
499         }
500         return $vars;
501     }
502
503     /**
504      * If the action will need localizable text strings, export them here like so:
505      *
506      * return array('pool_deepend' => _('Deep end'),
507      *              'pool_shallow' => _('Shallow end'));
508      *
509      * The exported map will be available via SN.msg() to JS code:
510      *
511      *   $('#pool').html('<div class="deepend"></div><div class="shallow"></div>');
512      *   $('#pool .deepend').text(SN.msg('pool_deepend'));
513      *   $('#pool .shallow').text(SN.msg('pool_shallow'));
514      *
515      * Exports a map of localized text strings to JavaScript code.
516      *
517      * Plugins can add to what's exported on any action by hooking the StartScriptMessages or
518      * EndScriptMessages events and appending to the array. Try to avoid adding strings that won't
519      * be used, as they'll be added to HTML output.
520      */
521     function getScriptMessages()
522     {
523         return array();
524     }
525
526     /**
527      * Show OpenSearch headers
528      *
529      * @return nothing
530      */
531     function showOpenSearch()
532     {
533         $this->element('link', array('rel' => 'search',
534                                      'type' => 'application/opensearchdescription+xml',
535                                      'href' =>  common_local_url('opensearch', array('type' => 'people')),
536                                      'title' => common_config('site', 'name').' People Search'));
537         $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
538                                      'href' =>  common_local_url('opensearch', array('type' => 'notice')),
539                                      'title' => common_config('site', 'name').' Notice Search'));
540     }
541
542     /**
543      * Show feed headers
544      *
545      * MAY overload
546      *
547      * @return nothing
548      */
549     function showFeeds()
550     {
551         foreach ($this->getFeeds() as $feed) {
552             $this->element('link', array('rel' => $feed->rel(),
553                                          'href' => $feed->url,
554                                          'type' => $feed->mimeType(),
555                                          'title' => $feed->title));
556         }
557     }
558
559     /**
560      * Show description.
561      *
562      * SHOULD overload
563      *
564      * @return nothing
565      */
566     function showDescription()
567     {
568         // does nothing by default
569     }
570
571     /**
572      * Show extra stuff in <head>.
573      *
574      * MAY overload
575      *
576      * @return nothing
577      */
578     function extraHead()
579     {
580         // does nothing by default
581     }
582
583     /**
584      * Show body.
585      *
586      * Calls template methods
587      *
588      * @return nothing
589      */
590     function showBody()
591     {
592         $params = array('id' => $this->getActionName());
593         if ($this->scoped instanceof Profile) {
594             $params['class'] = 'user_in';
595         }
596         $this->elementStart('body', $params);
597         $this->elementStart('div', array('id' => 'wrap'));
598         if (Event::handle('StartShowHeader', array($this))) {
599             $this->showHeader();
600             $this->flush();
601             Event::handle('EndShowHeader', array($this));
602         }
603         $this->showCore();
604         $this->flush();
605         if (Event::handle('StartShowFooter', array($this))) {
606             $this->showFooter();
607             $this->flush();
608             Event::handle('EndShowFooter', array($this));
609         }
610         $this->elementEnd('div');
611         $this->showScripts();
612         $this->elementEnd('body');
613     }
614
615     /**
616      * Show header of the page.
617      *
618      * Calls template methods
619      *
620      * @return nothing
621      */
622     function showHeader()
623     {
624         $this->elementStart('div', array('id' => 'header'));
625         $this->showLogo();
626         $this->showPrimaryNav();
627         if (Event::handle('StartShowSiteNotice', array($this))) {
628             $this->showSiteNotice();
629
630             Event::handle('EndShowSiteNotice', array($this));
631         }
632
633         $this->elementEnd('div');
634     }
635
636     /**
637      * Show configured logo.
638      *
639      * @return nothing
640      */
641     function showLogo()
642     {
643         $this->elementStart('address', array('id' => 'site_contact', 'class' => 'h-card'));
644         if (Event::handle('StartAddressData', array($this))) {
645             if (common_config('singleuser', 'enabled')) {
646                 $user = User::singleUser();
647                 $url = common_local_url('showstream',
648                                         array('nickname' => $user->nickname));
649             } else if (common_logged_in()) {
650                 $cur = common_current_user();
651                 $url = common_local_url('all', array('nickname' => $cur->nickname));
652             } else {
653                 $url = common_local_url('public');
654             }
655
656             $this->elementStart('a', array('class' => 'home bookmark',
657                                            'href' => $url));
658
659             if (GNUsocial::isHTTPS()) {
660                 $logoUrl = common_config('site', 'ssllogo');
661                 if (empty($logoUrl)) {
662                     // if logo is an uploaded file, try to fall back to HTTPS file URL
663                     $httpUrl = common_config('site', 'logo');
664                     if (!empty($httpUrl)) {
665                         $f = File::getKV('url', $httpUrl);
666                         if (!empty($f) && !empty($f->filename)) {
667                             // this will handle the HTTPS case
668                             $logoUrl = File::url($f->filename);
669                         }
670                     }
671                 }
672             } else {
673                 $logoUrl = common_config('site', 'logo');
674             }
675
676             if (empty($logoUrl) && file_exists(Theme::file('logo.png'))) {
677                 // This should handle the HTTPS case internally
678                 $logoUrl = Theme::path('logo.png');
679             }
680
681             if (!empty($logoUrl)) {
682                 $this->element('img', array('class' => 'logo u-photo p-name',
683                                             'src' => $logoUrl,
684                                             'alt' => common_config('site', 'name')));
685             }
686
687             $this->elementEnd('a');
688
689             Event::handle('EndAddressData', array($this));
690         }
691         $this->elementEnd('address');
692     }
693
694     /**
695      * Show primary navigation.
696      *
697      * @return nothing
698      */
699     function showPrimaryNav()
700     {
701         $this->elementStart('div', array('id' => 'site_nav_global_primary'));
702
703         $user = common_current_user();
704
705         if (!empty($user) || !common_config('site', 'private')) {
706             $form = new SearchForm($this);
707             $form->show();
708         }
709
710         $pn = new PrimaryNav($this);
711         $pn->show();
712         $this->elementEnd('div');
713     }
714
715     /**
716      * Show site notice.
717      *
718      * @return nothing
719      */
720     function showSiteNotice()
721     {
722         // Revist. Should probably do an hAtom pattern here
723         $text = common_config('site', 'notice');
724         if ($text) {
725             $this->elementStart('div', array('id' => 'site_notice',
726                                             'class' => 'system_notice'));
727             $this->raw($text);
728             $this->elementEnd('div');
729         }
730     }
731
732     /**
733      * Show notice form.
734      *
735      * MAY overload if no notice form needed... or direct message box????
736      *
737      * @return nothing
738      */
739     function showNoticeForm()
740     {
741         // TRANS: Tab on the notice form.
742         $tabs = array('status' => array('title' => _m('TAB','Status'),
743                                         'href'  => common_local_url('newnotice')));
744
745         $this->elementStart('div', 'input_forms');
746
747         $this->element('label', array('for'=>'input_form_nav'), _m('TAB', 'Share your:'));
748
749         if (Event::handle('StartShowEntryForms', array(&$tabs))) {
750             $this->elementStart('ul', array('class' => 'nav',
751                                             'id' => 'input_form_nav'));
752
753             foreach ($tabs as $tag => $data) {
754                 $tag = htmlspecialchars($tag);
755                 $attrs = array('id' => 'input_form_nav_'.$tag,
756                                'class' => 'input_form_nav_tab');
757
758                 if ($tag == 'status') {
759                     $attrs['class'] .= ' current';
760                 }
761                 $this->elementStart('li', $attrs);
762
763                 $this->element('a',
764                                array('onclick' => 'return SN.U.switchInputFormTab("'.$tag.'");',
765                                      'href' => $data['href']),
766                                $data['title']);
767                 $this->elementEnd('li');
768             }
769
770             $this->elementEnd('ul');
771
772             foreach ($tabs as $tag => $data) {
773                 $attrs = array('class' => 'input_form',
774                                'id' => 'input_form_'.$tag);
775                 if ($tag == 'status') {
776                     $attrs['class'] .= ' current';
777                 }
778
779                 $this->elementStart('div', $attrs);
780
781                 $form = null;
782
783                 if (Event::handle('StartMakeEntryForm', array($tag, $this, &$form))) {
784                     if ($tag == 'status') {
785                         $options = $this->noticeFormOptions();
786                         $form = new NoticeForm($this, $options);
787                     }
788                     Event::handle('EndMakeEntryForm', array($tag, $this, $form));
789                 }
790
791                 if (!empty($form)) {
792                     $form->show();
793                 }
794
795                 $this->elementEnd('div');
796             }
797         }
798
799         $this->elementEnd('div');
800     }
801
802     function noticeFormOptions()
803     {
804         return array();
805     }
806
807     /**
808      * Show anonymous message.
809      *
810      * SHOULD overload
811      *
812      * @return nothing
813      */
814     function showAnonymousMessage()
815     {
816         // needs to be defined by the class
817     }
818
819     /**
820      * Show core.
821      *
822      * Shows local navigation, content block and aside.
823      *
824      * @return nothing
825      */
826     function showCore()
827     {
828         $this->elementStart('div', array('id' => 'core'));
829         $this->elementStart('div', array('id' => 'aside_primary_wrapper'));
830         $this->elementStart('div', array('id' => 'content_wrapper'));
831         $this->elementStart('div', array('id' => 'site_nav_local_views_wrapper'));
832         if (Event::handle('StartShowLocalNavBlock', array($this))) {
833             $this->showLocalNavBlock();
834             $this->flush();
835             Event::handle('EndShowLocalNavBlock', array($this));
836         }
837         if (Event::handle('StartShowContentBlock', array($this))) {
838             $this->showContentBlock();
839             $this->flush();
840             Event::handle('EndShowContentBlock', array($this));
841         }
842         if (Event::handle('StartShowAside', array($this))) {
843             $this->showAside();
844             $this->flush();
845             Event::handle('EndShowAside', array($this));
846         }
847         $this->elementEnd('div');
848         $this->elementEnd('div');
849         $this->elementEnd('div');
850         $this->elementEnd('div');
851     }
852
853     /**
854      * Show local navigation block.
855      *
856      * @return nothing
857      */
858     function showLocalNavBlock()
859     {
860         // Need to have this ID for CSS; I'm too lazy to add it to
861         // all menus
862         $this->elementStart('div', array('id' => 'site_nav_local_views'));
863         // Cheat cheat cheat!
864         $this->showLocalNav();
865         $this->elementEnd('div');
866     }
867
868     /**
869      * If there's a logged-in user, show a bit of login context
870      *
871      * @return nothing
872      */
873     function showProfileBlock()
874     {
875         if (common_logged_in()) {
876             $block = new DefaultProfileBlock($this);
877             $block->show();
878         }
879     }
880
881     /**
882      * Show local navigation.
883      *
884      * SHOULD overload
885      *
886      * @return nothing
887      */
888     function showLocalNav()
889     {
890         $nav = new DefaultLocalNav($this);
891         $nav->show();
892     }
893
894     /**
895      * Show menu for an object (group, profile)
896      *
897      * This block will only show if a subclass has overridden
898      * the showObjectNav() method.
899      *
900      * @return nothing
901      */
902     function showObjectNavBlock()
903     {
904         $rmethod = new ReflectionMethod($this, 'showObjectNav');
905         $dclass = $rmethod->getDeclaringClass()->getName();
906
907         if ($dclass != 'Action') {
908             // Need to have this ID for CSS; I'm too lazy to add it to
909             // all menus
910             $this->elementStart('div', array('id' => 'site_nav_object',
911                                              'class' => 'section'));
912             $this->showObjectNav();
913             $this->elementEnd('div');
914         }
915     }
916
917     /**
918      * Show object navigation.
919      *
920      * If there are things to do with this object, show it here.
921      *
922      * @return nothing
923      */
924     function showObjectNav()
925     {
926         /* Nothing here. */
927     }
928
929     /**
930      * Show content block.
931      *
932      * @return nothing
933      */
934     function showContentBlock()
935     {
936         $this->elementStart('div', array('id' => 'content'));
937         if (common_logged_in()) {
938             if (Event::handle('StartShowNoticeForm', array($this))) {
939                 $this->showNoticeForm();
940                 Event::handle('EndShowNoticeForm', array($this));
941             }
942         }
943         if (Event::handle('StartShowPageTitle', array($this))) {
944             $this->showPageTitle();
945             Event::handle('EndShowPageTitle', array($this));
946         }
947         $this->showPageNoticeBlock();
948         $this->elementStart('div', array('id' => 'content_inner'));
949         // show the actual content (forms, lists, whatever)
950         $this->showContent();
951         $this->elementEnd('div');
952         $this->elementEnd('div');
953     }
954
955     /**
956      * Show page title.
957      *
958      * @return nothing
959      */
960     function showPageTitle()
961     {
962         $this->element('h1', null, $this->title());
963     }
964
965     /**
966      * Show page notice block.
967      *
968      * Only show the block if a subclassed action has overrided
969      * Action::showPageNotice(), or an event handler is registered for
970      * the StartShowPageNotice event, in which case we assume the
971      * 'page_notice' definition list is desired.  This is to prevent
972      * empty 'page_notice' definition lists from being output everywhere.
973      *
974      * @return nothing
975      */
976     function showPageNoticeBlock()
977     {
978         $rmethod = new ReflectionMethod($this, 'showPageNotice');
979         $dclass = $rmethod->getDeclaringClass()->getName();
980
981         if ($dclass != 'Action' || Event::hasHandler('StartShowPageNotice')) {
982
983             $this->elementStart('div', array('id' => 'page_notice',
984                                             'class' => 'system_notice'));
985             if (Event::handle('StartShowPageNotice', array($this))) {
986                 $this->showPageNotice();
987                 Event::handle('EndShowPageNotice', array($this));
988             }
989             $this->elementEnd('div');
990         }
991     }
992
993     /**
994      * Show page notice.
995      *
996      * SHOULD overload (unless there's not a notice)
997      *
998      * @return nothing
999      */
1000     function showPageNotice()
1001     {
1002     }
1003
1004     /**
1005      * Show content.
1006      *
1007      * MUST overload (unless there's not a notice)
1008      *
1009      * @return nothing
1010      */
1011     protected function showContent()
1012     {
1013     }
1014
1015     /**
1016      * Show Aside.
1017      *
1018      * @return nothing
1019      */
1020     function showAside()
1021     {
1022         $this->elementStart('div', array('id' => 'aside_primary',
1023                                          'class' => 'aside'));
1024         $this->showProfileBlock();
1025         if (Event::handle('StartShowObjectNavBlock', array($this))) {
1026             $this->showObjectNavBlock();
1027             Event::handle('EndShowObjectNavBlock', array($this));
1028         }
1029         if (Event::handle('StartShowSections', array($this))) {
1030             $this->showSections();
1031             Event::handle('EndShowSections', array($this));
1032         }
1033         if (Event::handle('StartShowExportData', array($this))) {
1034             $this->showExportData();
1035             Event::handle('EndShowExportData', array($this));
1036         }
1037         $this->elementEnd('div');
1038     }
1039
1040     /**
1041      * Show export data feeds.
1042      *
1043      * @return void
1044      */
1045     function showExportData()
1046     {
1047         $feeds = $this->getFeeds();
1048         if (!empty($feeds)) {
1049             $fl = new FeedList($this, $feeds);
1050             $fl->show();
1051         }
1052     }
1053
1054     /**
1055      * Show sections.
1056      *
1057      * SHOULD overload
1058      *
1059      * @return nothing
1060      */
1061     function showSections()
1062     {
1063         // for each section, show it
1064     }
1065
1066     /**
1067      * Show footer.
1068      *
1069      * @return nothing
1070      */
1071     function showFooter()
1072     {
1073         $this->elementStart('div', array('id' => 'footer'));
1074         if (Event::handle('StartShowInsideFooter', array($this))) {
1075             $this->showSecondaryNav();
1076             $this->showLicenses();
1077             Event::handle('EndShowInsideFooter', array($this));
1078         }
1079         $this->elementEnd('div');
1080     }
1081
1082     /**
1083      * Show secondary navigation.
1084      *
1085      * @return nothing
1086      */
1087     function showSecondaryNav()
1088     {
1089         $sn = new SecondaryNav($this);
1090         $sn->show();
1091     }
1092
1093     /**
1094      * Show licenses.
1095      *
1096      * @return nothing
1097      */
1098     function showLicenses()
1099     {
1100         $this->showGNUsocialLicense();
1101         $this->showContentLicense();
1102     }
1103
1104     /**
1105      * Show GNU social license.
1106      *
1107      * @return nothing
1108      */
1109     function showGNUsocialLicense()
1110     {
1111         if (common_config('site', 'broughtby')) {
1112             // TRANS: First sentence of the GNU social site license. Used if 'broughtby' is set.
1113             // TRANS: Text between [] is a link description, text between () is the link itself.
1114             // TRANS: Make sure there is no whitespace between "]" and "(".
1115             // TRANS: "%%site.broughtby%%" is the value of the variable site.broughtby
1116             $instr = _('**%%site.name%%** is a social network, courtesy of [%%site.broughtby%%](%%site.broughtbyurl%%).');
1117         } else {
1118             // TRANS: First sentence of the GNU social site license. Used if 'broughtby' is not set.
1119             $instr = _('**%%site.name%%** is a social network.');
1120         }
1121         $instr .= ' ';
1122         // TRANS: Second sentence of the GNU social site license. Mentions the GNU social source code license.
1123         // TRANS: Make sure there is no whitespace between "]" and "(".
1124         // TRANS: [%1$s](%2$s) is a link description followed by the link itself
1125         // TRANS: %3$s is the version of GNU social that is being used.
1126         $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);
1127         $output = common_markup_to_html($instr);
1128         $this->raw($output);
1129         // do it
1130     }
1131
1132     /**
1133      * Show content license.
1134      *
1135      * @return nothing
1136      */
1137     function showContentLicense()
1138     {
1139         if (Event::handle('StartShowContentLicense', array($this))) {
1140             switch (common_config('license', 'type')) {
1141             case 'private':
1142                 // TRANS: Content license displayed when license is set to 'private'.
1143                 // TRANS: %1$s is the site name.
1144                 $this->element('p', null, sprintf(_('Content and data of %1$s are private and confidential.'),
1145                                                   common_config('site', 'name')));
1146                 // fall through
1147             case 'allrightsreserved':
1148                 if (common_config('license', 'owner')) {
1149                     // TRANS: Content license displayed when license is set to 'allrightsreserved'.
1150                     // TRANS: %1$s is the copyright owner.
1151                     $this->element('p', null, sprintf(_('Content and data copyright by %1$s. All rights reserved.'),
1152                                                       common_config('license', 'owner')));
1153                 } else {
1154                     // TRANS: Content license displayed when license is set to 'allrightsreserved' and no owner is set.
1155                     $this->element('p', null, _('Content and data copyright by contributors. All rights reserved.'));
1156                 }
1157                 break;
1158             case 'cc': // fall through
1159             default:
1160                 $this->elementStart('p');
1161
1162                 $image    = common_config('license', 'image');
1163                 $sslimage = common_config('license', 'sslimage');
1164
1165                 if (GNUsocial::isHTTPS()) {
1166                     if (!empty($sslimage)) {
1167                         $url = $sslimage;
1168                     } else if (preg_match('#^http://i.creativecommons.org/#', $image)) {
1169                         // CC support HTTPS on their images
1170                         $url = preg_replace('/^http/', 'https', $image, 1);
1171                     } else {
1172                         // Better to show mixed content than no content
1173                         $url = $image;
1174                     }
1175                 } else {
1176                     $url = $image;
1177                 }
1178
1179                 $this->element('img', array('id' => 'license_cc',
1180                                             'src' => $url,
1181                                             'alt' => common_config('license', 'title'),
1182                                             'width' => '80',
1183                                             'height' => '15'));
1184                 $this->text(' ');
1185                 // TRANS: license message in footer.
1186                 // TRANS: %1$s is the site name, %2$s is a link to the license URL, with a licence name set in configuration.
1187                 $notice = _('All %1$s content and data are available under the %2$s license.');
1188                 $link = sprintf('<a class="license" rel="external license" href="%1$s">%2$s</a>',
1189                                 htmlspecialchars(common_config('license', 'url')),
1190                                 htmlspecialchars(common_config('license', 'title')));
1191                 $this->raw(@sprintf(htmlspecialchars($notice),
1192                                    htmlspecialchars(common_config('site', 'name')),
1193                                    $link));
1194                 $this->elementEnd('p');
1195                 break;
1196             }
1197
1198             Event::handle('EndShowContentLicense', array($this));
1199         }
1200     }
1201
1202     /**
1203      * Return last modified, if applicable.
1204      *
1205      * MAY override
1206      *
1207      * @return string last modified http header
1208      */
1209     function lastModified()
1210     {
1211         // For comparison with If-Last-Modified
1212         // If not applicable, return null
1213         return null;
1214     }
1215
1216     /**
1217      * Return etag, if applicable.
1218      *
1219      * MAY override
1220      *
1221      * @return string etag http header
1222      */
1223     function etag()
1224     {
1225         return null;
1226     }
1227
1228     /**
1229      * Return true if read only.
1230      *
1231      * MAY override
1232      *
1233      * @param array $args other arguments
1234      *
1235      * @return boolean is read only action?
1236      */
1237     function isReadOnly($args)
1238     {
1239         return false;
1240     }
1241
1242     /**
1243      * Returns query argument or default value if not found
1244      *
1245      * @param string $key requested argument
1246      * @param string $def default value to return if $key is not provided
1247      *
1248      * @return boolean is read only action?
1249      */
1250     function arg($key, $def=null)
1251     {
1252         if (array_key_exists($key, $this->args)) {
1253             return $this->args[$key];
1254         } else {
1255             return $def;
1256         }
1257     }
1258
1259     /**
1260      * Returns trimmed query argument or default value if not found
1261      *
1262      * @param string $key requested argument
1263      * @param string $def default value to return if $key is not provided
1264      *
1265      * @return boolean is read only action?
1266      */
1267     function trimmed($key, $def=null)
1268     {
1269         $arg = $this->arg($key, $def);
1270         return is_string($arg) ? trim($arg) : $arg;
1271     }
1272
1273     /**
1274      * Handler method
1275      *
1276      * @return boolean is read only action?
1277      */
1278     protected function handle()
1279     {
1280         header('Vary: Accept-Encoding,Cookie');
1281
1282         $lm   = $this->lastModified();
1283         $etag = $this->etag();
1284
1285         if ($etag) {
1286             header('ETag: ' . $etag);
1287         }
1288
1289         if ($lm) {
1290             header('Last-Modified: ' . date(DATE_RFC1123, $lm));
1291             if ($this->isCacheable()) {
1292                 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1293                 header( "Cache-Control: private, must-revalidate, max-age=0" );
1294                 header( "Pragma:");
1295             }
1296         }
1297
1298         $checked = false;
1299         if ($etag) {
1300             $if_none_match = (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) ?
1301               $_SERVER['HTTP_IF_NONE_MATCH'] : null;
1302             if ($if_none_match) {
1303                 // If this check fails, ignore the if-modified-since below.
1304                 $checked = true;
1305                 if ($this->_hasEtag($etag, $if_none_match)) {
1306                     header('HTTP/1.1 304 Not Modified');
1307                     // Better way to do this?
1308                     exit(0);
1309                 }
1310             }
1311         }
1312
1313         if (!$checked && $lm && array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
1314             $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
1315             $ims = strtotime($if_modified_since);
1316             if ($lm <= $ims) {
1317                 header('HTTP/1.1 304 Not Modified');
1318                 // Better way to do this?
1319                 exit(0);
1320             }
1321         }
1322     }
1323
1324     /**
1325      * Is this action cacheable?
1326      *
1327      * If the action returns a last-modified
1328      *
1329      * @param array $argarray is ignored since it's now passed in in prepare()
1330      *
1331      * @return boolean is read only action?
1332      */
1333     function isCacheable()
1334     {
1335         return true;
1336     }
1337
1338     /**
1339      * Has etag? (private)
1340      *
1341      * @param string $etag          etag http header
1342      * @param string $if_none_match ifNoneMatch http header
1343      *
1344      * @return boolean
1345      */
1346     function _hasEtag($etag, $if_none_match)
1347     {
1348         $etags = explode(',', $if_none_match);
1349         return in_array($etag, $etags) || in_array('*', $etags);
1350     }
1351
1352     /**
1353      * Boolean understands english (yes, no, true, false)
1354      *
1355      * @param string $key query key we're interested in
1356      * @param string $def default value
1357      *
1358      * @return boolean interprets yes/no strings as boolean
1359      */
1360     function boolean($key, $def=false)
1361     {
1362         $arg = strtolower($this->trimmed($key));
1363
1364         if (is_null($arg)) {
1365             return $def;
1366         } else if (in_array($arg, array('true', 'yes', '1', 'on'))) {
1367             return true;
1368         } else if (in_array($arg, array('false', 'no', '0'))) {
1369             return false;
1370         } else {
1371             return $def;
1372         }
1373     }
1374
1375     /**
1376      * This is a cheap hack to avoid a bug in DB_DataObject
1377      * where '' is non-type-aware compared to 0, which means it
1378      * will always be true for values like false and 0 too...
1379      *
1380      * Upstream bug is::
1381      * https://pear.php.net/bugs/bug.php?id=20291
1382      */
1383     function booleanintstring($key, $def=false)
1384     {
1385         return $this->boolean($key, $def) ? '1' : '0';
1386     }
1387
1388     /**
1389      * Integer value of an argument
1390      *
1391      * @param string $key      query key we're interested in
1392      * @param string $defValue optional default value (default null)
1393      * @param string $maxValue optional max value (default null)
1394      * @param string $minValue optional min value (default null)
1395      *
1396      * @return integer integer value
1397      */
1398     function int($key, $defValue=null, $maxValue=null, $minValue=null)
1399     {
1400         $arg = intval($this->arg($key));
1401
1402         if (!is_numeric($this->arg($key)) || $arg != $this->arg($key)) {
1403             return $defValue;
1404         }
1405
1406         if (!is_null($maxValue)) {
1407             $arg = min($arg, $maxValue);
1408         }
1409
1410         if (!is_null($minValue)) {
1411             $arg = max($arg, $minValue);
1412         }
1413
1414         return $arg;
1415     }
1416
1417     /**
1418      * Server error
1419      *
1420      * @param string  $msg  error message to display
1421      * @param integer $code http error code, 500 by default
1422      *
1423      * @return nothing
1424      */
1425     function serverError($msg, $code=500, $format=null)
1426     {
1427         if ($format === null) {
1428             $format = $this->format;
1429         }
1430
1431         common_debug("Server error '{$code}' on '{$this->action}': {$msg}", __FILE__);
1432
1433         if (!array_key_exists($code, ServerErrorAction::$status)) {
1434             $code = 500;
1435         }
1436
1437         $status_string = ServerErrorAction::$status[$code];
1438
1439         switch ($format) {
1440         case 'xml':
1441             header("HTTP/1.1 {$code} {$status_string}");
1442             $this->initDocument('xml');
1443             $this->elementStart('hash');
1444             $this->element('error', null, $msg);
1445             $this->element('request', null, $_SERVER['REQUEST_URI']);
1446             $this->elementEnd('hash');
1447             $this->endDocument('xml');
1448             break;
1449         case 'json':
1450             if (!isset($this->callback)) {
1451                 header("HTTP/1.1 {$code} {$status_string}");
1452             }
1453             $this->initDocument('json');
1454             $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
1455             print(json_encode($error_array));
1456             $this->endDocument('json');
1457             break;
1458         default:
1459             common_log(LOG_ERR, 'Handled serverError ('._ve($code).') but cannot output into desired format ('._ve($this->format).'): '._ve($msg));
1460             $action = new ServerErrorAction($msg, $code);
1461             $action->execute();
1462         }
1463
1464         exit((int)$code);
1465     }
1466
1467     /**
1468      * Client error
1469      *
1470      * @param string  $msg    error message to display
1471      * @param integer $code   http error code, 400 by default
1472      * @param string  $format error format (json, xml, text) for ApiAction
1473      *
1474      * @return nothing
1475      * @throws ClientException always
1476      */
1477     function clientError($msg, $code=400, $format=null)
1478     {
1479         // $format is currently only relevant for an ApiAction anyway
1480         if ($format === null) {
1481             $format = $this->format;
1482         }
1483
1484         common_debug("User error '{$code}' on '{$this->action}': {$msg}", __FILE__);
1485
1486         if (!array_key_exists($code, ClientErrorAction::$status)) {
1487             $code = 400;
1488         }
1489
1490         $status_string = ClientErrorAction::$status[$code];
1491
1492         switch ($format) {
1493         case 'xml':
1494             header("HTTP/1.1 {$code} {$status_string}");
1495             $this->initDocument('xml');
1496             $this->elementStart('hash');
1497             $this->element('error', null, $msg);
1498             $this->element('request', null, $_SERVER['REQUEST_URI']);
1499             $this->elementEnd('hash');
1500             $this->endDocument('xml');
1501             break;
1502         case 'json':
1503             if (!isset($this->callback)) {
1504                 header("HTTP/1.1 {$code} {$status_string}");
1505             }
1506             $this->initDocument('json');
1507             $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
1508             print(json_encode($error_array));
1509             $this->endDocument('json');
1510             break;
1511         case 'text':
1512             header("HTTP/1.1 {$code} {$status_string}");
1513             header('Content-Type: text/plain; charset=utf-8');
1514             echo $msg;
1515             break;
1516         default:
1517             common_log(LOG_ERR, 'Handled clientError ('._ve($code).') but cannot output into desired format ('._ve($this->format).'): '._ve($msg));
1518             $action = new ClientErrorAction($msg, $code);
1519             $action->execute();
1520         }
1521         exit((int)$code);
1522     }
1523
1524     /**
1525      * If not logged in, take appropriate action (redir or exception)
1526      *
1527      * @param boolean $redir Redirect to login if not logged in
1528      *
1529      * @return boolean true if logged in (never returns if not)
1530      */
1531     public function checkLogin($redir=true)
1532     {
1533         if (common_logged_in()) {
1534             return true;
1535         }
1536
1537         if ($redir==true) {
1538             common_set_returnto($_SERVER['REQUEST_URI']);
1539             common_redirect(common_local_url('login'));
1540         }
1541
1542         // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
1543         $this->clientError(_('Not logged in.'), 403);
1544     }
1545
1546     /**
1547      * Returns the current URL
1548      *
1549      * @return string current URL
1550      */
1551     function selfUrl()
1552     {
1553         list($action, $args) = $this->returnToArgs();
1554         return common_local_url($action, $args);
1555     }
1556
1557     /**
1558      * Returns arguments sufficient for re-constructing URL
1559      *
1560      * @return array two elements: action, other args
1561      */
1562     function returnToArgs()
1563     {
1564         $action = $this->getActionName();
1565         $args   = $this->args;
1566         unset($args['action']);
1567         if (common_config('site', 'fancy')) {
1568             unset($args['p']);
1569         }
1570         if (array_key_exists('submit', $args)) {
1571             unset($args['submit']);
1572         }
1573         foreach (array_keys($_COOKIE) as $cookie) {
1574             unset($args[$cookie]);
1575         }
1576         return array($action, $args);
1577     }
1578
1579     /**
1580      * Generate a menu item
1581      *
1582      * @param string  $url         menu URL
1583      * @param string  $text        menu name
1584      * @param string  $title       title attribute, null by default
1585      * @param boolean $is_selected current menu item, false by default
1586      * @param string  $id          element id, null by default
1587      *
1588      * @return nothing
1589      */
1590     function menuItem($url, $text, $title=null, $is_selected=false, $id=null, $class=null)
1591     {
1592         // Added @id to li for some control.
1593         // XXX: We might want to move this to htmloutputter.php
1594         $lattrs  = array();
1595         $classes = array();
1596         if ($class !== null) {
1597             $classes[] = trim($class);
1598         }
1599         if ($is_selected) {
1600             $classes[] = 'current';
1601         }
1602
1603         if (!empty($classes)) {
1604             $lattrs['class'] = implode(' ', $classes);
1605         }
1606
1607         if (!is_null($id)) {
1608             $lattrs['id'] = $id;
1609         }
1610
1611         $this->elementStart('li', $lattrs);
1612         $attrs['href'] = $url;
1613         if ($title) {
1614             $attrs['title'] = $title;
1615         }
1616         $this->element('a', $attrs, $text);
1617         $this->elementEnd('li');
1618     }
1619
1620     /**
1621      * Generate pagination links
1622      *
1623      * @param boolean $have_before is there something before?
1624      * @param boolean $have_after  is there something after?
1625      * @param integer $page        current page
1626      * @param string  $action      current action
1627      * @param array   $args        rest of query arguments
1628      *
1629      * @return nothing
1630      */
1631     // XXX: The messages in this pagination method only tailor to navigating
1632     //      notices. In other lists, "Previous"/"Next" type navigation is
1633     //      desirable, but not available.
1634     function pagination($have_before, $have_after, $page, $action, $args=null)
1635     {
1636         // Does a little before-after block for next/prev page
1637         if ($have_before || $have_after) {
1638             $this->elementStart('ul', array('class' => 'nav',
1639                                             'id' => 'pagination'));
1640         }
1641         if ($have_before) {
1642             $pargs   = array('page' => $page-1);
1643             $this->elementStart('li', array('class' => 'nav_prev'));
1644             $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1645                                       'rel' => 'prev'),
1646                            // TRANS: Pagination message to go to a page displaying information more in the
1647                            // TRANS: present than the currently displayed information.
1648                            _('After'));
1649             $this->elementEnd('li');
1650         }
1651         if ($have_after) {
1652             $pargs   = array('page' => $page+1);
1653             $this->elementStart('li', array('class' => 'nav_next'));
1654             $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1655                                       'rel' => 'next'),
1656                            // TRANS: Pagination message to go to a page displaying information more in the
1657                            // TRANS: past than the currently displayed information.
1658                            _('Before'));
1659             $this->elementEnd('li');
1660         }
1661         if ($have_before || $have_after) {
1662             $this->elementEnd('ul');
1663         }
1664     }
1665
1666     /**
1667      * An array of feeds for this action.
1668      *
1669      * Returns an array of potential feeds for this action.
1670      *
1671      * @return array Feed object to show in head and links
1672      */
1673     function getFeeds()
1674     {
1675         return array();
1676     }
1677
1678     /**
1679      * Check the session token.
1680      *
1681      * Checks that the current form has the correct session token,
1682      * and throw an exception if it does not.
1683      *
1684      * @return void
1685      */
1686     // XXX: Finding this type of check with the same message about 50 times.
1687     //      Possible to refactor?
1688     function checkSessionToken()
1689     {
1690         // CSRF protection
1691         $token = $this->trimmed('token');
1692         if (empty($token) || $token != common_session_token()) {
1693             // TRANS: Client error text when there is a problem with the session token.
1694             $this->clientError(_('There was a problem with your session token.'));
1695         }
1696     }
1697
1698     /**
1699      * Check if the current request is a POST
1700      *
1701      * @return boolean true if POST; otherwise false.
1702      */
1703
1704     function isPost()
1705     {
1706         return ($_SERVER['REQUEST_METHOD'] == 'POST');
1707     }
1708 }