]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/action.php
Add framebusting JavaScript to help avoid clickjacking attacks.
[quix0rs-gnu-social.git] / lib / action.php
1 <?php
2 /**
3  * Laconica, 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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @author    Sarven Capadisli <csarven@controlyourself.ca>
26  * @copyright 2008 Control Yourself, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://laconi.ca/
29  */
30
31 if (!defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/noticeform.php';
36 require_once INSTALLDIR.'/lib/htmloutputter.php';
37
38 /**
39  * Base class for all actions
40  *
41  * This is the base class for all actions in the package. An action is
42  * more or less a "view" in an MVC framework.
43  *
44  * Actions are responsible for extracting and validating parameters; using
45  * model classes to read and write to the database; and doing ouput.
46  *
47  * @category Output
48  * @package  Laconica
49  * @author   Evan Prodromou <evan@controlyourself.ca>
50  * @author   Sarven Capadisli <csarven@controlyourself.ca>
51  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
52  * @link     http://laconi.ca/
53  *
54  * @see      HTMLOutputter
55  */
56 class Action extends HTMLOutputter // lawsuit
57 {
58     var $args;
59
60     /**
61      * Constructor
62      *
63      * Just wraps the HTMLOutputter constructor.
64      *
65      * @param string  $output URI to output to, default = stdout
66      * @param boolean $indent Whether to indent output, default true
67      *
68      * @see XMLOutputter::__construct
69      * @see HTMLOutputter::__construct
70      */
71     function __construct($output='php://output', $indent=true)
72     {
73         parent::__construct($output, $indent);
74     }
75
76     /**
77      * For initializing members of the class.
78      *
79      * @param array $argarray misc. arguments
80      *
81      * @return boolean true
82      */
83     function prepare($argarray)
84     {
85         $this->args =& common_copy_args($argarray);
86         return true;
87     }
88
89     /**
90      * Show page, a template method.
91      *
92      * @return nothing
93      */
94     function showPage()
95     {
96         $this->startHTML();
97         $this->showHead();
98         $this->showBody();
99         $this->endHTML();
100     }
101
102     /**
103      * Show head, a template method.
104      *
105      * @return nothing
106      */
107     function showHead()
108     {
109         // XXX: attributes (profile?)
110         $this->elementStart('head');
111         $this->showTitle();
112         $this->showStylesheets();
113         $this->showScripts();
114         $this->showOpenSearch();
115         $this->showFeeds();
116         $this->showDescription();
117         $this->extraHead();
118         $this->elementEnd('head');
119     }
120
121     /**
122      * Show title, a template method.
123      *
124      * @return nothing
125      */
126     function showTitle()
127     {
128         $this->element('title', null,
129                        sprintf(_("%s - %s"),
130                                $this->title(),
131                                common_config('site', 'name')));
132     }
133
134     /**
135      * Returns the page title
136      *
137      * SHOULD overload
138      *
139      * @return string page title
140      */
141
142     function title()
143     {
144         return _("Untitled page");
145     }
146
147     /**
148      * Show stylesheets
149      *
150      * @return nothing
151      */
152     function showStylesheets()
153     {
154         if (Event::handle('StartShowStyles', array($this))) {
155             if (Event::handle('StartShowLaconicaStyles', array($this))) {
156                 $this->element('link', array('rel' => 'stylesheet',
157                                              'type' => 'text/css',
158                                              'href' => theme_path('css/display.css', 'base') . '?version=' . LACONICA_VERSION,
159                                              'media' => 'screen, projection, tv'));
160                 $this->element('link', array('rel' => 'stylesheet',
161                                              'type' => 'text/css',
162                                              'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
163                                              'media' => 'screen, projection, tv'));
164                 Event::handle('EndShowLaconicaStyles', array($this));
165             }
166             if (Event::handle('StartShowUAStyles', array($this))) {
167                 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
168                                'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
169                 foreach (array(6,7) as $ver) {
170                     if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
171                         // Yes, IE people should be put in jail.
172                         $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
173                                        'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
174                     }
175                 }
176                 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
177                                'href="'.theme_path('css/ie.css', null).'?version='.LACONICA_VERSION.'" /><![endif]');
178                 Event::handle('EndShowUAStyles', array($this));
179             }
180             Event::handle('EndShowStyles', array($this));
181         }
182     }
183
184     /**
185      * Show javascript headers
186      *
187      * @return nothing
188      */
189     function showScripts()
190     {
191         if (Event::handle('StartShowScripts', array($this))) {
192             if (Event::handle('StartShowJQueryScripts', array($this))) {
193                 $this->element('script', array('type' => 'text/javascript',
194                                                'src' => common_path('js/jquery.min.js')),
195                                ' ');
196                 $this->element('script', array('type' => 'text/javascript',
197                                                'src' => common_path('js/jquery.form.js')),
198                                ' ');
199                 Event::handle('EndShowJQueryScripts', array($this));
200             }
201             if (Event::handle('StartShowLaconicaScripts', array($this))) {
202                 $this->element('script', array('type' => 'text/javascript',
203                                                'src' => common_path('js/xbImportNode.js')),
204                                ' ');
205                 $this->element('script', array('type' => 'text/javascript',
206                                                'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
207                                ' ');
208                 // Frame-busting code to avoid clickjacking attacks.
209                 $this->element('script', array('type' => 'text/javascript'),
210                                'if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
211                 Event::handle('EndShowLaconicaScripts', array($this));
212             }
213             Event::handle('EndShowScripts', array($this));
214         }
215     }
216
217     /**
218      * Show OpenSearch headers
219      *
220      * @return nothing
221      */
222     function showOpenSearch()
223     {
224         $this->element('link', array('rel' => 'search',
225                                      'type' => 'application/opensearchdescription+xml',
226                                      'href' =>  common_local_url('opensearch', array('type' => 'people')),
227                                      'title' => common_config('site', 'name').' People Search'));
228         $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
229                                      'href' =>  common_local_url('opensearch', array('type' => 'notice')),
230                                      'title' => common_config('site', 'name').' Notice Search'));
231     }
232
233     /**
234      * Show feed headers
235      *
236      * MAY overload
237      *
238      * @return nothing
239      */
240
241     function showFeeds()
242     {
243         $feeds = $this->getFeeds();
244
245         if ($feeds) {
246             foreach ($feeds as $feed) {
247                 $this->element('link', array('rel' => $feed->rel(),
248                                              'href' => $feed->url,
249                                              'type' => $feed->mimeType(),
250                                              'title' => $feed->title));
251             }
252         }
253     }
254
255     /**
256      * Show description.
257      *
258      * SHOULD overload
259      *
260      * @return nothing
261      */
262     function showDescription()
263     {
264         // does nothing by default
265     }
266
267     /**
268      * Show extra stuff in <head>.
269      *
270      * MAY overload
271      *
272      * @return nothing
273      */
274     function extraHead()
275     {
276         // does nothing by default
277     }
278
279     /**
280      * Show body.
281      *
282      * Calls template methods
283      *
284      * @return nothing
285      */
286     function showBody()
287     {
288         $this->elementStart('body', array('id' => $this->trimmed('action')));
289         $this->elementStart('div', array('id' => 'wrap'));
290         if (Event::handle('StartShowHeader', array($this))) {
291             $this->showHeader();
292             Event::handle('EndShowHeader', array($this));
293         }
294         $this->showCore();
295         if (Event::handle('StartShowFooter', array($this))) {
296             $this->showFooter();
297             Event::handle('EndShowFooter', array($this));
298         }
299         $this->elementEnd('div');
300         $this->elementEnd('body');
301     }
302
303     /**
304      * Show header of the page.
305      *
306      * Calls template methods
307      *
308      * @return nothing
309      */
310     function showHeader()
311     {
312         $this->elementStart('div', array('id' => 'header'));
313         $this->showLogo();
314         $this->showPrimaryNav();
315         $this->showSiteNotice();
316         if (common_logged_in()) {
317             $this->showNoticeForm();
318         } else {
319             $this->showAnonymousMessage();
320         }
321         $this->elementEnd('div');
322     }
323
324     /**
325      * Show configured logo.
326      *
327      * @return nothing
328      */
329     function showLogo()
330     {
331         $this->elementStart('address', array('id' => 'site_contact',
332                                              'class' => 'vcard'));
333         $this->elementStart('a', array('class' => 'url home bookmark',
334                                        'href' => common_local_url('public')));
335         if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
336             $this->element('img', array('class' => 'logo photo',
337                                         'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
338                                         'alt' => common_config('site', 'name')));
339         }
340         $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
341         $this->elementEnd('a');
342         $this->elementEnd('address');
343     }
344
345     /**
346      * Show primary navigation.
347      *
348      * @return nothing
349      */
350     function showPrimaryNav()
351     {
352         $user = common_current_user();
353
354         $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
355         $this->element('dt', null, _('Primary site navigation'));
356         $this->elementStart('dd');
357         $this->elementStart('ul', array('class' => 'nav'));
358         if (Event::handle('StartPrimaryNav', array($this))) {
359             if ($user) {
360                 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
361                                 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
362             }
363             $this->menuItem(common_local_url('peoplesearch'),
364                             _('Search'), _('Search for people or text'), false, 'nav_search');
365             if ($user) {
366                 $this->menuItem(common_local_url('profilesettings'),
367                                 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
368
369                 if (common_config('xmpp', 'enabled')) {
370                     $this->menuItem(common_local_url('imsettings'),
371                                     _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
372                 } else {
373                     $this->menuItem(common_local_url('smssettings'),
374                                     _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
375                 }
376                 $this->menuItem(common_local_url('logout'),
377                                 _('Logout'), _('Logout from the site'), false, 'nav_logout');
378             } else {
379                 $this->menuItem(common_local_url('login'),
380                                 _('Login'), _('Login to the site'), false, 'nav_login');
381                 if (!common_config('site', 'closed')) {
382                     $this->menuItem(common_local_url('register'),
383                                     _('Register'), _('Create an account'), false, 'nav_register');
384                 }
385                 $this->menuItem(common_local_url('openidlogin'),
386                                 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
387             }
388             $this->menuItem(common_local_url('doc', array('title' => 'help')),
389                             _('Help'), _('Help me!'), false, 'nav_help');
390             Event::handle('EndPrimaryNav', array($this));
391         }
392         $this->elementEnd('ul');
393         $this->elementEnd('dd');
394         $this->elementEnd('dl');
395     }
396
397     /**
398      * Show site notice.
399      *
400      * @return nothing
401      */
402     function showSiteNotice()
403     {
404         // Revist. Should probably do an hAtom pattern here
405         $text = common_config('site', 'notice');
406         if ($text) {
407             $this->elementStart('dl', array('id' => 'site_notice',
408                                             'class' => 'system_notice'));
409             $this->element('dt', null, _('Site notice'));
410             $this->elementStart('dd', null);
411             $this->raw($text);
412             $this->elementEnd('dd');
413             $this->elementEnd('dl');
414         }
415     }
416
417     /**
418      * Show notice form.
419      *
420      * MAY overload if no notice form needed... or direct message box????
421      *
422      * @return nothing
423      */
424     function showNoticeForm()
425     {
426         $notice_form = new NoticeForm($this);
427         $notice_form->show();
428     }
429
430     /**
431      * Show anonymous message.
432      *
433      * SHOULD overload
434      *
435      * @return nothing
436      */
437     function showAnonymousMessage()
438     {
439         // needs to be defined by the class
440     }
441
442     /**
443      * Show core.
444      *
445      * Shows local navigation, content block and aside.
446      *
447      * @return nothing
448      */
449     function showCore()
450     {
451         $this->elementStart('div', array('id' => 'core'));
452         $this->showLocalNavBlock();
453         if (Event::handle('StartShowContentBlock', array($this))) {
454             $this->showContentBlock();
455             Event::handle('EndShowContentBlock', array($this));
456         }
457         $this->showAside();
458         $this->elementEnd('div');
459     }
460
461     /**
462      * Show local navigation block.
463      *
464      * @return nothing
465      */
466     function showLocalNavBlock()
467     {
468         $this->elementStart('dl', array('id' => 'site_nav_local_views'));
469         $this->element('dt', null, _('Local views'));
470         $this->elementStart('dd');
471         $this->showLocalNav();
472         $this->elementEnd('dd');
473         $this->elementEnd('dl');
474     }
475
476     /**
477      * Show local navigation.
478      *
479      * SHOULD overload
480      *
481      * @return nothing
482      */
483     function showLocalNav()
484     {
485         // does nothing by default
486     }
487
488     /**
489      * Show content block.
490      *
491      * @return nothing
492      */
493     function showContentBlock()
494     {
495         $this->elementStart('div', array('id' => 'content'));
496         $this->showPageTitle();
497         $this->showPageNoticeBlock();
498         $this->elementStart('div', array('id' => 'content_inner'));
499         // show the actual content (forms, lists, whatever)
500         $this->showContent();
501         $this->elementEnd('div');
502         $this->elementEnd('div');
503     }
504
505     /**
506      * Show page title.
507      *
508      * @return nothing
509      */
510     function showPageTitle()
511     {
512         $this->element('h1', null, $this->title());
513     }
514
515     /**
516      * Show page notice block.
517      *
518      * @return nothing
519      */
520     function showPageNoticeBlock()
521     {
522         $this->elementStart('dl', array('id' => 'page_notice',
523                                         'class' => 'system_notice'));
524         $this->element('dt', null, _('Page notice'));
525         $this->elementStart('dd');
526         $this->showPageNotice();
527         $this->elementEnd('dd');
528         $this->elementEnd('dl');
529     }
530
531     /**
532      * Show page notice.
533      *
534      * SHOULD overload (unless there's not a notice)
535      *
536      * @return nothing
537      */
538     function showPageNotice()
539     {
540     }
541
542     /**
543      * Show content.
544      *
545      * MUST overload (unless there's not a notice)
546      *
547      * @return nothing
548      */
549     function showContent()
550     {
551     }
552
553     /**
554      * Show Aside.
555      *
556      * @return nothing
557      */
558
559     function showAside()
560     {
561         $this->elementStart('div', array('id' => 'aside_primary',
562                                          'class' => 'aside'));
563         $this->showExportData();
564         if (Event::handle('StartShowSections', array($this))) {
565             $this->showSections();
566             Event::handle('EndShowSections', array($this));
567         }
568         $this->elementEnd('div');
569     }
570
571     /**
572      * Show export data feeds.
573      *
574      * @return void
575      */
576
577     function showExportData()
578     {
579         $feeds = $this->getFeeds();
580         if ($feeds) {
581             $fl = new FeedList($this);
582             $fl->show($feeds);
583         }
584     }
585
586     /**
587      * Show sections.
588      *
589      * SHOULD overload
590      *
591      * @return nothing
592      */
593     function showSections()
594     {
595         // for each section, show it
596     }
597
598     /**
599      * Show footer.
600      *
601      * @return nothing
602      */
603     function showFooter()
604     {
605         $this->elementStart('div', array('id' => 'footer'));
606         $this->showSecondaryNav();
607         $this->showLicenses();
608         $this->elementEnd('div');
609     }
610
611     /**
612      * Show secondary navigation.
613      *
614      * @return nothing
615      */
616     function showSecondaryNav()
617     {
618         $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
619         $this->element('dt', null, _('Secondary site navigation'));
620         $this->elementStart('dd', null);
621         $this->elementStart('ul', array('class' => 'nav'));
622         if (Event::handle('StartSecondaryNav', array($this))) {
623             $this->menuItem(common_local_url('doc', array('title' => 'help')),
624                             _('Help'));
625             $this->menuItem(common_local_url('doc', array('title' => 'about')),
626                             _('About'));
627             $this->menuItem(common_local_url('doc', array('title' => 'faq')),
628                             _('FAQ'));
629             $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
630                             _('Privacy'));
631             $this->menuItem(common_local_url('doc', array('title' => 'source')),
632                             _('Source'));
633             $this->menuItem(common_local_url('doc', array('title' => 'contact')),
634                             _('Contact'));
635             Event::handle('EndSecondaryNav', array($this));
636         }
637         $this->elementEnd('ul');
638         $this->elementEnd('dd');
639         $this->elementEnd('dl');
640     }
641
642     /**
643      * Show licenses.
644      *
645      * @return nothing
646      */
647     function showLicenses()
648     {
649         $this->elementStart('dl', array('id' => 'licenses'));
650         $this->showLaconicaLicense();
651         $this->showContentLicense();
652         $this->elementEnd('dl');
653     }
654
655     /**
656      * Show Laconica license.
657      *
658      * @return nothing
659      */
660     function showLaconicaLicense()
661     {
662         $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
663         $this->elementStart('dd', null);
664         if (common_config('site', 'broughtby')) {
665             $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
666         } else {
667             $instr = _('**%%site.name%%** is a microblogging service. ');
668         }
669         $instr .= sprintf(_('It runs the [Laconica](http://laconi.ca/) microblogging software, version %s, available under the [GNU Affero General Public License](http://www.fsf.org/licensing/licenses/agpl-3.0.html).'), LACONICA_VERSION);
670         $output = common_markup_to_html($instr);
671         $this->raw($output);
672         $this->elementEnd('dd');
673         // do it
674     }
675
676     /**
677      * Show content license.
678      *
679      * @return nothing
680      */
681     function showContentLicense()
682     {
683         $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
684         $this->elementStart('dd', array('id' => 'site_content_license_cc'));
685         $this->elementStart('p');
686         $this->element('img', array('id' => 'license_cc',
687                                     'src' => common_config('license', 'image'),
688                                     'alt' => common_config('license', 'title')));
689         //TODO: This is dirty: i18n
690         $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
691         $this->element('a', array('class' => 'license',
692                                   'rel' => 'external license',
693                                   'href' => common_config('license', 'url')),
694                        common_config('license', 'title'));
695         $this->text(_('license.'));
696         $this->elementEnd('p');
697         $this->elementEnd('dd');
698     }
699
700     /**
701      * Return last modified, if applicable.
702      *
703      * MAY override
704      *
705      * @return string last modified http header
706      */
707     function lastModified()
708     {
709         // For comparison with If-Last-Modified
710         // If not applicable, return null
711         return null;
712     }
713
714     /**
715      * Return etag, if applicable.
716      *
717      * MAY override
718      *
719      * @return string etag http header
720      */
721     function etag()
722     {
723         return null;
724     }
725
726     /**
727      * Return true if read only.
728      *
729      * MAY override
730      *
731      * @return boolean is read only action?
732      */
733     function isReadOnly()
734     {
735         return false;
736     }
737
738     /**
739      * Returns query argument or default value if not found
740      *
741      * @param string $key requested argument
742      * @param string $def default value to return if $key is not provided
743      *
744      * @return boolean is read only action?
745      */
746     function arg($key, $def=null)
747     {
748         if (array_key_exists($key, $this->args)) {
749             return $this->args[$key];
750         } else {
751             return $def;
752         }
753     }
754
755     /**
756      * Returns trimmed query argument or default value if not found
757      *
758      * @param string $key requested argument
759      * @param string $def default value to return if $key is not provided
760      *
761      * @return boolean is read only action?
762      */
763     function trimmed($key, $def=null)
764     {
765         $arg = $this->arg($key, $def);
766         return is_string($arg) ? trim($arg) : $arg;
767     }
768
769     /**
770      * Handler method
771      *
772      * @param array $argarray is ignored since it's now passed in in prepare()
773      *
774      * @return boolean is read only action?
775      */
776     function handle($argarray=null)
777     {
778         $lm   = $this->lastModified();
779         $etag = $this->etag();
780         if ($etag) {
781             header('ETag: ' . $etag);
782         }
783         if ($lm) {
784             header('Last-Modified: ' . date(DATE_RFC1123, $lm));
785             $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
786             if ($if_modified_since) {
787                 $ims = strtotime($if_modified_since);
788                 if ($lm <= $ims) {
789                     if (!$etag ||
790                         $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
791                         header('HTTP/1.1 304 Not Modified');
792                         // Better way to do this?
793                         exit(0);
794                     }
795                 }
796             }
797         }
798     }
799
800     /**
801      * HasĀ etag? (private)
802      *
803      * @param string $etag          etag http header
804      * @param string $if_none_match ifNoneMatch http header
805      *
806      * @return boolean
807      */
808     function _hasEtag($etag, $if_none_match)
809     {
810         return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
811     }
812
813     /**
814      * Boolean understands english (yes, no, true, false)
815      *
816      * @param string $key query key we're interested in
817      * @param string $def default value
818      *
819      * @return boolean interprets yes/no strings as boolean
820      */
821     function boolean($key, $def=false)
822     {
823         $arg = strtolower($this->trimmed($key));
824
825         if (is_null($arg)) {
826             return $def;
827         } else if (in_array($arg, array('true', 'yes', '1'))) {
828             return true;
829         } else if (in_array($arg, array('false', 'no', '0'))) {
830             return false;
831         } else {
832             return $def;
833         }
834     }
835
836     /**
837      * Server error
838      *
839      * @param string  $msg  error message to display
840      * @param integer $code http error code, 500 by default
841      *
842      * @return nothing
843      */
844
845     function serverError($msg, $code=500)
846     {
847         $action = $this->trimmed('action');
848         common_debug("Server error '$code' on '$action': $msg", __FILE__);
849         throw new ServerException($msg, $code);
850     }
851
852     /**
853      * Client error
854      *
855      * @param string  $msg  error message to display
856      * @param integer $code http error code, 400 by default
857      *
858      * @return nothing
859      */
860
861     function clientError($msg, $code=400)
862     {
863         $action = $this->trimmed('action');
864         common_debug("User error '$code' on '$action': $msg", __FILE__);
865         throw new ClientException($msg, $code);
866     }
867
868     /**
869      * Returns the current URL
870      *
871      * @return string current URL
872      */
873     function selfUrl()
874     {
875         $action = $this->trimmed('action');
876         $args   = $this->args;
877         unset($args['action']);
878         foreach (array_keys($_COOKIE) as $cookie) {
879             unset($args[$cookie]);
880         }
881         return common_local_url($action, $args);
882     }
883
884     /**
885      * Generate a menu item
886      *
887      * @param string  $url         menu URL
888      * @param string  $text        menu name
889      * @param string  $title       title attribute, null by default
890      * @param boolean $is_selected current menu item, false by default
891      * @param string  $id          element id, null by default
892      *
893      * @return nothing
894      */
895     function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
896     {
897         // Added @id to li for some control.
898         // XXX: We might want to move this to htmloutputter.php
899         $lattrs = array();
900         if ($is_selected) {
901             $lattrs['class'] = 'current';
902         }
903
904         (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
905
906         $this->elementStart('li', $lattrs);
907         $attrs['href'] = $url;
908         if ($title) {
909             $attrs['title'] = $title;
910         }
911         $this->element('a', $attrs, $text);
912         $this->elementEnd('li');
913     }
914
915     /**
916      * Generate pagination links
917      *
918      * @param boolean $have_before is there something before?
919      * @param boolean $have_after  is there something after?
920      * @param integer $page        current page
921      * @param string  $action      current action
922      * @param array   $args        rest of query arguments
923      *
924      * @return nothing
925      */
926     function pagination($have_before, $have_after, $page, $action, $args=null)
927     {
928         // Does a little before-after block for next/prev page
929         if ($have_before || $have_after) {
930             $this->elementStart('div', array('class' => 'pagination'));
931             $this->elementStart('dl', null);
932             $this->element('dt', null, _('Pagination'));
933             $this->elementStart('dd', null);
934             $this->elementStart('ul', array('class' => 'nav'));
935         }
936         if ($have_before) {
937             $pargs   = array('page' => $page-1);
938             $newargs = $args ? array_merge($args, $pargs) : $pargs;
939             $this->elementStart('li', array('class' => 'nav_prev'));
940             $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
941                            _('After'));
942             $this->elementEnd('li');
943         }
944         if ($have_after) {
945             $pargs   = array('page' => $page+1);
946             $newargs = $args ? array_merge($args, $pargs) : $pargs;
947             $this->elementStart('li', array('class' => 'nav_next'));
948             $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
949                            _('Before'));
950             $this->elementEnd('li');
951         }
952         if ($have_before || $have_after) {
953             $this->elementEnd('ul');
954             $this->elementEnd('dd');
955             $this->elementEnd('dl');
956             $this->elementEnd('div');
957         }
958     }
959
960     /**
961      * An array of feeds for this action.
962      *
963      * Returns an array of potential feeds for this action.
964      *
965      * @return array Feed object to show in head and links
966      */
967
968     function getFeeds()
969     {
970         return null;
971     }
972 }