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