3 * StatusNet, the distributed open-source microblogging tool
5 * Base class for all actions (~views)
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.
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.
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/>.
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/
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 require_once INSTALLDIR.'/lib/noticeform.php';
36 require_once INSTALLDIR.'/lib/htmloutputter.php';
39 * Base class for all actions
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.
44 * Actions are responsible for extracting and validating parameters; using
45 * model classes to read and write to the database; and doing ouput.
49 * @author Evan Prodromou <evan@status.net>
50 * @author Sarven Capadisli <csarven@status.net>
51 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
52 * @link http://status.net/
56 class Action extends HTMLOutputter // lawsuit
63 * Just wraps the HTMLOutputter constructor.
65 * @param string $output URI to output to, default = stdout
66 * @param boolean $indent Whether to indent output, default true
68 * @see XMLOutputter::__construct
69 * @see HTMLOutputter::__construct
71 function __construct($output='php://output', $indent=null)
73 parent::__construct($output, $indent);
77 * For initializing members of the class.
79 * @param array $argarray misc. arguments
81 * @return boolean true
83 function prepare($argarray)
85 $this->args =& common_copy_args($argarray);
90 * Show page, a template method.
96 if (Event::handle('StartShowHTML', array($this))) {
98 Event::handle('EndShowHTML', array($this));
100 if (Event::handle('StartShowHead', array($this))) {
102 Event::handle('EndShowHead', array($this));
104 if (Event::handle('StartShowBody', array($this))) {
106 Event::handle('EndShowBody', array($this));
108 if (Event::handle('StartEndHTML', array($this))) {
110 Event::handle('EndEndHTML', array($this));
115 * Show head, a template method.
121 // XXX: attributes (profile?)
122 $this->elementStart('head');
123 if (Event::handle('StartShowHeadElements', array($this))) {
124 if (Event::handle('StartShowHeadTitle', array($this))) {
126 Event::handle('EndShowHeadTitle', array($this));
128 $this->showShortcutIcon();
129 $this->showStylesheets();
130 $this->showOpenSearch();
132 $this->showDescription();
134 Event::handle('EndShowHeadElements', array($this));
136 $this->elementEnd('head');
140 * Show title, a template method.
146 $this->element('title', null,
147 // TRANS: Page title. %1$s is the title, %2$s is the site name.
148 sprintf(_("%1\$s - %2\$s"),
150 common_config('site', 'name')));
154 * Returns the page title
158 * @return string page title
163 // TRANS: Page title for a page without a title set.
164 return _("Untitled page");
168 * Show themed shortcut icon
172 function showShortcutIcon()
174 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/favicon.ico')) {
175 $this->element('link', array('rel' => 'shortcut icon',
176 'href' => Theme::path('favicon.ico')));
178 $this->element('link', array('rel' => 'shortcut icon',
179 'href' => common_path('favicon.ico')));
182 if (common_config('site', 'mobile')) {
183 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/apple-touch-icon.png')) {
184 $this->element('link', array('rel' => 'apple-touch-icon',
185 'href' => Theme::path('apple-touch-icon.png')));
187 $this->element('link', array('rel' => 'apple-touch-icon',
188 'href' => common_path('apple-touch-icon.png')));
198 function showStylesheets()
200 if (Event::handle('StartShowStyles', array($this))) {
202 // Use old name for StatusNet for compatibility on events
204 if (Event::handle('StartShowStatusNetStyles', array($this)) &&
205 Event::handle('StartShowLaconicaStyles', array($this))) {
206 $this->primaryCssLink(null, 'screen, projection, tv, print');
207 Event::handle('EndShowStatusNetStyles', array($this));
208 Event::handle('EndShowLaconicaStyles', array($this));
211 if (Event::handle('StartShowUAStyles', array($this))) {
212 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
213 'href="'.Theme::path('css/ie.css', 'base').'?version='.STATUSNET_VERSION.'" /><![endif]');
214 foreach (array(6,7) as $ver) {
215 if (file_exists(Theme::file('css/ie'.$ver.'.css', 'base'))) {
216 // Yes, IE people should be put in jail.
217 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
218 'href="'.Theme::path('css/ie'.$ver.'.css', 'base').'?version='.STATUSNET_VERSION.'" /><![endif]');
221 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
222 'href="'.Theme::path('css/ie.css', null).'?version='.STATUSNET_VERSION.'" /><![endif]');
223 Event::handle('EndShowUAStyles', array($this));
226 if (Event::handle('StartShowDesign', array($this))) {
228 $user = common_current_user();
230 if (empty($user) || $user->viewdesigns) {
231 $design = $this->getDesign();
233 if (!empty($design)) {
234 $design->showCSS($this);
238 Event::handle('EndShowDesign', array($this));
240 Event::handle('EndShowStyles', array($this));
242 if (common_config('custom_css', 'enabled')) {
243 $css = common_config('custom_css', 'css');
244 if (Event::handle('StartShowCustomCss', array($this, &$css))) {
245 if (trim($css) != '') {
248 Event::handle('EndShowCustomCss', array($this));
254 function primaryCssLink($mainTheme=null, $media=null)
256 // If the currently-selected theme has dependencies on other themes,
257 // we'll need to load their display.css files as well in order.
258 $theme = new Theme($mainTheme);
259 $baseThemes = $theme->getDeps();
260 foreach ($baseThemes as $baseTheme) {
261 $this->cssLink('css/display.css', $baseTheme, $media);
263 $this->cssLink('css/display.css', $mainTheme, $media);
267 * Show javascript headers
271 function showScripts()
273 if (Event::handle('StartShowScripts', array($this))) {
274 if (Event::handle('StartShowJQueryScripts', array($this))) {
275 $this->script('jquery.min.js');
276 $this->script('jquery.form.js');
277 $this->script('jquery.cookie.js');
278 $this->inlineScript('if (typeof window.JSON !== "object") { $.getScript("'.common_path('js/json2.js').'"); }');
279 $this->script('jquery.joverlay.min.js');
280 Event::handle('EndShowJQueryScripts', array($this));
282 if (Event::handle('StartShowStatusNetScripts', array($this)) &&
283 Event::handle('StartShowLaconicaScripts', array($this))) {
284 $this->script('util.js');
285 // Frame-busting code to avoid clickjacking attacks.
286 $this->inlineScript('if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
287 Event::handle('EndShowStatusNetScripts', array($this));
288 Event::handle('EndShowLaconicaScripts', array($this));
290 Event::handle('EndShowScripts', array($this));
295 * Show OpenSearch headers
299 function showOpenSearch()
301 $this->element('link', array('rel' => 'search',
302 'type' => 'application/opensearchdescription+xml',
303 'href' => common_local_url('opensearch', array('type' => 'people')),
304 'title' => common_config('site', 'name').' People Search'));
305 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
306 'href' => common_local_url('opensearch', array('type' => 'notice')),
307 'title' => common_config('site', 'name').' Notice Search'));
320 $feeds = $this->getFeeds();
323 foreach ($feeds as $feed) {
324 $this->element('link', array('rel' => $feed->rel(),
325 'href' => $feed->url,
326 'type' => $feed->mimeType(),
327 'title' => $feed->title));
339 function showDescription()
341 // does nothing by default
345 * Show extra stuff in <head>.
353 // does nothing by default
359 * Calls template methods
365 $this->elementStart('body', (common_current_user()) ? array('id' => $this->trimmed('action'),
366 'class' => 'user_in')
367 : array('id' => $this->trimmed('action')));
368 $this->elementStart('div', array('id' => 'wrap'));
369 if (Event::handle('StartShowHeader', array($this))) {
371 Event::handle('EndShowHeader', array($this));
374 if (Event::handle('StartShowFooter', array($this))) {
376 Event::handle('EndShowFooter', array($this));
378 $this->elementEnd('div');
379 $this->showScripts();
380 $this->elementEnd('body');
384 * Show header of the page.
386 * Calls template methods
390 function showHeader()
392 $this->elementStart('div', array('id' => 'header'));
394 $this->showPrimaryNav();
395 if (Event::handle('StartShowSiteNotice', array($this))) {
396 $this->showSiteNotice();
398 Event::handle('EndShowSiteNotice', array($this));
400 if (common_logged_in()) {
401 $this->showNoticeForm();
403 $this->showAnonymousMessage();
405 $this->elementEnd('div');
409 * Show configured logo.
415 $this->elementStart('address', array('id' => 'site_contact',
416 'class' => 'vcard'));
417 if (Event::handle('StartAddressData', array($this))) {
418 if (common_config('singleuser', 'enabled')) {
419 $url = common_local_url('showstream',
420 array('nickname' => common_config('singleuser', 'nickname')));
422 $url = common_local_url('public');
424 $this->elementStart('a', array('class' => 'url home bookmark',
426 if (common_config('site', 'logo') || file_exists(Theme::file('logo.png'))) {
427 $this->element('img', array('class' => 'logo photo',
428 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : Theme::path('logo.png'),
429 'alt' => common_config('site', 'name')));
432 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
433 $this->elementEnd('a');
434 Event::handle('EndAddressData', array($this));
436 $this->elementEnd('address');
440 * Show primary navigation.
444 function showPrimaryNav()
446 $user = common_current_user();
447 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
448 // TRANS: DT element for primary navigation menu. String is hidden in default CSS.
449 $this->element('dt', null, _('Primary site navigation'));
450 $this->elementStart('dd');
451 $this->elementStart('ul', array('class' => 'nav'));
452 if (Event::handle('StartPrimaryNav', array($this))) {
454 // TRANS: Tooltip for main menu option "Personal"
455 $tooltip = _m('TOOLTIP', 'Personal profile and friends timeline');
456 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
457 // TRANS: Main menu option when logged in for access to personal profile and friends timeline
458 _m('MENU', 'Personal'), $tooltip, false, 'nav_home');
459 // TRANS: Tooltip for main menu option "Account"
460 $tooltip = _m('TOOLTIP', 'Change your email, avatar, password, profile');
461 $this->menuItem(common_local_url('profilesettings'),
462 // TRANS: Main menu option when logged in for access to user settings
463 _('Account'), $tooltip, false, 'nav_account');
464 // TRANS: Tooltip for main menu option "Services"
465 $tooltip = _m('TOOLTIP', 'Connect to services');
466 $this->menuItem(common_local_url('oauthconnectionssettings'),
467 // TRANS: Main menu option when logged in and connection are possible for access to options to connect to other services
468 _('Connect'), $tooltip, false, 'nav_connect');
469 if ($user->hasRight(Right::CONFIGURESITE)) {
470 // TRANS: Tooltip for menu option "Admin"
471 $tooltip = _m('TOOLTIP', 'Change site configuration');
472 $this->menuItem(common_local_url('siteadminpanel'),
473 // TRANS: Main menu option when logged in and site admin for access to site configuration
474 _m('MENU', 'Admin'), $tooltip, false, 'nav_admin');
476 if (common_config('invite', 'enabled')) {
477 // TRANS: Tooltip for main menu option "Invite"
478 $tooltip = _m('TOOLTIP', 'Invite friends and colleagues to join you on %s');
479 $this->menuItem(common_local_url('invite'),
480 // TRANS: Main menu option when logged in and invitations are allowed for inviting new users
481 _m('MENU', 'Invite'),
483 common_config('site', 'name')),
484 false, 'nav_invitecontact');
486 // TRANS: Tooltip for main menu option "Logout"
487 $tooltip = _m('TOOLTIP', 'Logout from the site');
488 $this->menuItem(common_local_url('logout'),
489 // TRANS: Main menu option when logged in to log out the current user
490 _m('MENU', 'Logout'), $tooltip, false, 'nav_logout');
493 if (!common_config('site', 'closed') && !common_config('site', 'inviteonly')) {
494 // TRANS: Tooltip for main menu option "Register"
495 $tooltip = _m('TOOLTIP', 'Create an account');
496 $this->menuItem(common_local_url('register'),
497 // TRANS: Main menu option when not logged in to register a new account
498 _m('MENU', 'Register'), $tooltip, false, 'nav_register');
500 // TRANS: Tooltip for main menu option "Login"
501 $tooltip = _m('TOOLTIP', 'Login to the site');
502 // TRANS: Main menu option when not logged in to log in
503 $this->menuItem(common_local_url('login'),
504 _m('MENU', 'Login'), $tooltip, false, 'nav_login');
506 // TRANS: Tooltip for main menu option "Help"
507 $tooltip = _m('TOOLTIP', 'Help me!');
508 // TRANS: Main menu option for help on the StatusNet site
509 $this->menuItem(common_local_url('doc', array('title' => 'help')),
510 _m('MENU', 'Help'), $tooltip, false, 'nav_help');
511 if ($user || !common_config('site', 'private')) {
512 // TRANS: Tooltip for main menu option "Search"
513 $tooltip = _m('TOOLTIP', 'Search for people or text');
514 // TRANS: Main menu option when logged in or when the StatusNet instance is not private
515 $this->menuItem(common_local_url('peoplesearch'),
516 _m('MENU', 'Search'), $tooltip, false, 'nav_search');
518 Event::handle('EndPrimaryNav', array($this));
520 $this->elementEnd('ul');
521 $this->elementEnd('dd');
522 $this->elementEnd('dl');
530 function showSiteNotice()
532 // Revist. Should probably do an hAtom pattern here
533 $text = common_config('site', 'notice');
535 $this->elementStart('dl', array('id' => 'site_notice',
536 'class' => 'system_notice'));
537 // TRANS: DT element for site notice. String is hidden in default CSS.
538 $this->element('dt', null, _('Site notice'));
539 $this->elementStart('dd', null);
541 $this->elementEnd('dd');
542 $this->elementEnd('dl');
549 * MAY overload if no notice form needed... or direct message box????
553 function showNoticeForm()
555 $notice_form = new NoticeForm($this);
556 $notice_form->show();
560 * Show anonymous message.
566 function showAnonymousMessage()
568 // needs to be defined by the class
574 * Shows local navigation, content block and aside.
580 $this->elementStart('div', array('id' => 'core'));
581 if (Event::handle('StartShowLocalNavBlock', array($this))) {
582 $this->showLocalNavBlock();
583 Event::handle('EndShowLocalNavBlock', array($this));
585 if (Event::handle('StartShowContentBlock', array($this))) {
586 $this->showContentBlock();
587 Event::handle('EndShowContentBlock', array($this));
589 if (Event::handle('StartShowAside', array($this))) {
591 Event::handle('EndShowAside', array($this));
593 $this->elementEnd('div');
597 * Show local navigation block.
601 function showLocalNavBlock()
603 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
604 // TRANS: DT element for local views block. String is hidden in default CSS.
605 $this->element('dt', null, _('Local views'));
606 $this->elementStart('dd');
607 $this->showLocalNav();
608 $this->elementEnd('dd');
609 $this->elementEnd('dl');
613 * Show local navigation.
619 function showLocalNav()
621 // does nothing by default
625 * Show content block.
629 function showContentBlock()
631 $this->elementStart('div', array('id' => 'content'));
632 if (Event::handle('StartShowPageTitle', array($this))) {
633 $this->showPageTitle();
634 Event::handle('EndShowPageTitle', array($this));
636 $this->showPageNoticeBlock();
637 $this->elementStart('div', array('id' => 'content_inner'));
638 // show the actual content (forms, lists, whatever)
639 $this->showContent();
640 $this->elementEnd('div');
641 $this->elementEnd('div');
649 function showPageTitle()
651 $this->element('h1', null, $this->title());
655 * Show page notice block.
657 * Only show the block if a subclassed action has overrided
658 * Action::showPageNotice(), or an event handler is registered for
659 * the StartShowPageNotice event, in which case we assume the
660 * 'page_notice' definition list is desired. This is to prevent
661 * empty 'page_notice' definition lists from being output everywhere.
665 function showPageNoticeBlock()
667 $rmethod = new ReflectionMethod($this, 'showPageNotice');
668 $dclass = $rmethod->getDeclaringClass()->getName();
670 if ($dclass != 'Action' || Event::hasHandler('StartShowPageNotice')) {
672 $this->elementStart('dl', array('id' => 'page_notice',
673 'class' => 'system_notice'));
674 // TRANS: DT element for page notice. String is hidden in default CSS.
675 $this->element('dt', null, _('Page notice'));
676 $this->elementStart('dd');
677 if (Event::handle('StartShowPageNotice', array($this))) {
678 $this->showPageNotice();
679 Event::handle('EndShowPageNotice', array($this));
681 $this->elementEnd('dd');
682 $this->elementEnd('dl');
689 * SHOULD overload (unless there's not a notice)
693 function showPageNotice()
700 * MUST overload (unless there's not a notice)
704 function showContent()
716 $this->elementStart('div', array('id' => 'aside_primary',
717 'class' => 'aside'));
718 if (Event::handle('StartShowSections', array($this))) {
719 $this->showSections();
720 Event::handle('EndShowSections', array($this));
722 if (Event::handle('StartShowExportData', array($this))) {
723 $this->showExportData();
724 Event::handle('EndShowExportData', array($this));
726 $this->elementEnd('div');
730 * Show export data feeds.
735 function showExportData()
737 $feeds = $this->getFeeds();
739 $fl = new FeedList($this);
751 function showSections()
753 // for each section, show it
761 function showFooter()
763 $this->elementStart('div', array('id' => 'footer'));
764 $this->showSecondaryNav();
765 $this->showLicenses();
766 $this->elementEnd('div');
770 * Show secondary navigation.
774 function showSecondaryNav()
776 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
777 // TRANS: DT element for secondary navigation menu. String is hidden in default CSS.
778 $this->element('dt', null, _('Secondary site navigation'));
779 $this->elementStart('dd', null);
780 $this->elementStart('ul', array('class' => 'nav'));
781 if (Event::handle('StartSecondaryNav', array($this))) {
782 $this->menuItem(common_local_url('doc', array('title' => 'help')),
783 // TRANS: Secondary navigation menu option leading to help on StatusNet.
785 $this->menuItem(common_local_url('doc', array('title' => 'about')),
786 // TRANS: Secondary navigation menu option leading to text about StatusNet site.
788 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
789 // TRANS: Secondary navigation menu option leading to Frequently Asked Questions.
791 $bb = common_config('site', 'broughtby');
793 $this->menuItem(common_local_url('doc', array('title' => 'tos')),
794 // TRANS: Secondary navigation menu option leading to Terms of Service.
797 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
798 // TRANS: Secondary navigation menu option leading to privacy policy.
800 $this->menuItem(common_local_url('doc', array('title' => 'source')),
801 // TRANS: Secondary navigation menu option.
803 $this->menuItem(common_local_url('version'),
804 // TRANS: Secondary navigation menu option leading to version information on the StatusNet site.
806 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
807 // TRANS: Secondary navigation menu option leading to contact information on the StatusNet site.
809 $this->menuItem(common_local_url('doc', array('title' => 'badge')),
811 Event::handle('EndSecondaryNav', array($this));
813 $this->elementEnd('ul');
814 $this->elementEnd('dd');
815 $this->elementEnd('dl');
823 function showLicenses()
825 $this->elementStart('dl', array('id' => 'licenses'));
826 $this->showStatusNetLicense();
827 $this->showContentLicense();
828 $this->elementEnd('dl');
832 * Show StatusNet license.
836 function showStatusNetLicense()
838 // TRANS: DT element for StatusNet software license.
839 $this->element('dt', array('id' => 'site_statusnet_license'), _('StatusNet software license'));
840 $this->elementStart('dd', null);
841 if (common_config('site', 'broughtby')) {
842 // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is set.
843 // TRANS: Text between [] is a link description, text between () is the link itself.
844 // TRANS: Make sure there is no whitespace between "]" and "(".
845 // TRANS: "%%site.broughtby%%" is the value of the variable site.broughtby
846 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%).');
848 // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is not set.
849 $instr = _('**%%site.name%%** is a microblogging service.');
852 // TRANS: Second sentence of the StatusNet site license. Mentions the StatusNet source code license.
853 // TRANS: Make sure there is no whitespace between "]" and "(".
854 // TRANS: Text between [] is a link description, text between () is the link itself.
855 // TRANS: %s is the version of StatusNet that is being used.
856 $instr .= sprintf(_('It runs the [StatusNet](http://status.net/) microblogging software, version %s, available under the [GNU Affero General Public License](http://www.fsf.org/licensing/licenses/agpl-3.0.html).'), STATUSNET_VERSION);
857 $output = common_markup_to_html($instr);
859 $this->elementEnd('dd');
864 * Show content license.
868 function showContentLicense()
870 if (Event::handle('StartShowContentLicense', array($this))) {
871 // TRANS: DT element for StatusNet site content license.
872 $this->element('dt', array('id' => 'site_content_license'), _('Site content license'));
873 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
875 switch (common_config('license', 'type')) {
877 // TRANS: Content license displayed when license is set to 'private'.
878 // TRANS: %1$s is the site name.
879 $this->element('p', null, sprintf(_('Content and data of %1$s are private and confidential.'),
880 common_config('site', 'name')));
882 case 'allrightsreserved':
883 if (common_config('license', 'owner')) {
884 // TRANS: Content license displayed when license is set to 'allrightsreserved'.
885 // TRANS: %1$s is the copyright owner.
886 $this->element('p', null, sprintf(_('Content and data copyright by %1$s. All rights reserved.'),
887 common_config('license', 'owner')));
889 // TRANS: Content license displayed when license is set to 'allrightsreserved' and no owner is set.
890 $this->element('p', null, _('Content and data copyright by contributors. All rights reserved.'));
893 case 'cc': // fall through
895 $this->elementStart('p');
896 $this->element('img', array('id' => 'license_cc',
897 'src' => common_config('license', 'image'),
898 'alt' => common_config('license', 'title'),
902 // TRANS: license message in footer.
903 // TRANS: %1$s is the site name, %2$s is a link to the license URL, with a licence name set in configuration.
904 $notice = _('All %1$s content and data are available under the %2$s license.');
905 $link = "<a class=\"license\" rel=\"external license\" href=\"" .
906 htmlspecialchars(common_config('license', 'url')) .
908 htmlspecialchars(common_config('license', 'title')) .
910 $this->raw(sprintf(htmlspecialchars($notice),
911 htmlspecialchars(common_config('site', 'name')),
913 $this->elementEnd('p');
917 $this->elementEnd('dd');
918 Event::handle('EndShowContentLicense', array($this));
923 * Return last modified, if applicable.
927 * @return string last modified http header
929 function lastModified()
931 // For comparison with If-Last-Modified
932 // If not applicable, return null
937 * Return etag, if applicable.
941 * @return string etag http header
949 * Return true if read only.
953 * @param array $args other arguments
955 * @return boolean is read only action?
958 function isReadOnly($args)
964 * Returns query argument or default value if not found
966 * @param string $key requested argument
967 * @param string $def default value to return if $key is not provided
969 * @return boolean is read only action?
971 function arg($key, $def=null)
973 if (array_key_exists($key, $this->args)) {
974 return $this->args[$key];
981 * Returns trimmed query argument or default value if not found
983 * @param string $key requested argument
984 * @param string $def default value to return if $key is not provided
986 * @return boolean is read only action?
988 function trimmed($key, $def=null)
990 $arg = $this->arg($key, $def);
991 return is_string($arg) ? trim($arg) : $arg;
997 * @param array $argarray is ignored since it's now passed in in prepare()
999 * @return boolean is read only action?
1001 function handle($argarray=null)
1003 header('Vary: Accept-Encoding,Cookie');
1005 $lm = $this->lastModified();
1006 $etag = $this->etag();
1009 header('ETag: ' . $etag);
1013 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
1014 if ($this->isCacheable()) {
1015 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1016 header( "Cache-Control: private, must-revalidate, max-age=0" );
1023 $if_none_match = (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) ?
1024 $_SERVER['HTTP_IF_NONE_MATCH'] : null;
1025 if ($if_none_match) {
1026 // If this check fails, ignore the if-modified-since below.
1028 if ($this->_hasEtag($etag, $if_none_match)) {
1029 header('HTTP/1.1 304 Not Modified');
1030 // Better way to do this?
1036 if (!$checked && $lm && array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
1037 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
1038 $ims = strtotime($if_modified_since);
1040 header('HTTP/1.1 304 Not Modified');
1041 // Better way to do this?
1048 * Is this action cacheable?
1050 * If the action returns a last-modified
1052 * @param array $argarray is ignored since it's now passed in in prepare()
1054 * @return boolean is read only action?
1057 function isCacheable()
1063 * Has etag? (private)
1065 * @param string $etag etag http header
1066 * @param string $if_none_match ifNoneMatch http header
1071 function _hasEtag($etag, $if_none_match)
1073 $etags = explode(',', $if_none_match);
1074 return in_array($etag, $etags) || in_array('*', $etags);
1078 * Boolean understands english (yes, no, true, false)
1080 * @param string $key query key we're interested in
1081 * @param string $def default value
1083 * @return boolean interprets yes/no strings as boolean
1085 function boolean($key, $def=false)
1087 $arg = strtolower($this->trimmed($key));
1089 if (is_null($arg)) {
1091 } else if (in_array($arg, array('true', 'yes', '1', 'on'))) {
1093 } else if (in_array($arg, array('false', 'no', '0'))) {
1101 * Integer value of an argument
1103 * @param string $key query key we're interested in
1104 * @param string $defValue optional default value (default null)
1105 * @param string $maxValue optional max value (default null)
1106 * @param string $minValue optional min value (default null)
1108 * @return integer integer value
1111 function int($key, $defValue=null, $maxValue=null, $minValue=null)
1113 $arg = strtolower($this->trimmed($key));
1115 if (is_null($arg) || !is_integer($arg)) {
1119 if (!is_null($maxValue)) {
1120 $arg = min($arg, $maxValue);
1123 if (!is_null($minValue)) {
1124 $arg = max($arg, $minValue);
1133 * @param string $msg error message to display
1134 * @param integer $code http error code, 500 by default
1139 function serverError($msg, $code=500)
1141 $action = $this->trimmed('action');
1142 common_debug("Server error '$code' on '$action': $msg", __FILE__);
1143 throw new ServerException($msg, $code);
1149 * @param string $msg error message to display
1150 * @param integer $code http error code, 400 by default
1155 function clientError($msg, $code=400)
1157 $action = $this->trimmed('action');
1158 common_debug("User error '$code' on '$action': $msg", __FILE__);
1159 throw new ClientException($msg, $code);
1163 * Returns the current URL
1165 * @return string current URL
1170 list($action, $args) = $this->returnToArgs();
1171 return common_local_url($action, $args);
1175 * Returns arguments sufficient for re-constructing URL
1177 * @return array two elements: action, other args
1180 function returnToArgs()
1182 $action = $this->trimmed('action');
1183 $args = $this->args;
1184 unset($args['action']);
1185 if (common_config('site', 'fancy')) {
1188 if (array_key_exists('submit', $args)) {
1189 unset($args['submit']);
1191 foreach (array_keys($_COOKIE) as $cookie) {
1192 unset($args[$cookie]);
1194 return array($action, $args);
1198 * Generate a menu item
1200 * @param string $url menu URL
1201 * @param string $text menu name
1202 * @param string $title title attribute, null by default
1203 * @param boolean $is_selected current menu item, false by default
1204 * @param string $id element id, null by default
1208 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
1210 // Added @id to li for some control.
1211 // XXX: We might want to move this to htmloutputter.php
1214 $lattrs['class'] = 'current';
1217 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
1219 $this->elementStart('li', $lattrs);
1220 $attrs['href'] = $url;
1222 $attrs['title'] = $title;
1224 $this->element('a', $attrs, $text);
1225 $this->elementEnd('li');
1229 * Generate pagination links
1231 * @param boolean $have_before is there something before?
1232 * @param boolean $have_after is there something after?
1233 * @param integer $page current page
1234 * @param string $action current action
1235 * @param array $args rest of query arguments
1239 // XXX: The messages in this pagination method only tailor to navigating
1240 // notices. In other lists, "Previous"/"Next" type navigation is
1241 // desirable, but not available.
1242 function pagination($have_before, $have_after, $page, $action, $args=null)
1244 // Does a little before-after block for next/prev page
1245 if ($have_before || $have_after) {
1246 $this->elementStart('dl', 'pagination');
1247 // TRANS: DT element for pagination (previous/next, etc.).
1248 $this->element('dt', null, _('Pagination'));
1249 $this->elementStart('dd', null);
1250 $this->elementStart('ul', array('class' => 'nav'));
1253 $pargs = array('page' => $page-1);
1254 $this->elementStart('li', array('class' => 'nav_prev'));
1255 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1257 // TRANS: Pagination message to go to a page displaying information more in the
1258 // TRANS: present than the currently displayed information.
1260 $this->elementEnd('li');
1263 $pargs = array('page' => $page+1);
1264 $this->elementStart('li', array('class' => 'nav_next'));
1265 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1267 // TRANS: Pagination message to go to a page displaying information more in the
1268 // TRANS: past than the currently displayed information.
1270 $this->elementEnd('li');
1272 if ($have_before || $have_after) {
1273 $this->elementEnd('ul');
1274 $this->elementEnd('dd');
1275 $this->elementEnd('dl');
1280 * An array of feeds for this action.
1282 * Returns an array of potential feeds for this action.
1284 * @return array Feed object to show in head and links
1293 * A design for this action
1295 * @return Design a design object to use
1298 function getDesign()
1300 return Design::siteDesign();
1304 * Check the session token.
1306 * Checks that the current form has the correct session token,
1307 * and throw an exception if it does not.
1312 // XXX: Finding this type of check with the same message about 50 times.
1313 // Possible to refactor?
1314 function checkSessionToken()
1317 $token = $this->trimmed('token');
1318 if (empty($token) || $token != common_session_token()) {
1319 // TRANS: Client error text when there is a problem with the session token.
1320 $this->clientError(_('There was a problem with your session token.'));