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))) {
125 $this->showShortcutIcon();
126 $this->showStylesheets();
127 $this->showOpenSearch();
129 $this->showDescription();
131 Event::handle('EndShowHeadElements', array($this));
133 $this->elementEnd('head');
137 * Show title, a template method.
143 $this->element('title', null,
144 // TRANS: Page title. %1$s is the title, %2$s is the site name.
145 sprintf(_("%1\$s - %2\$s"),
147 common_config('site', 'name')));
151 * Returns the page title
155 * @return string page title
160 // TRANS: Page title for a page without a title set.
161 return _("Untitled page");
165 * Show themed shortcut icon
169 function showShortcutIcon()
171 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/favicon.ico')) {
172 $this->element('link', array('rel' => 'shortcut icon',
173 'href' => Theme::path('favicon.ico')));
175 $this->element('link', array('rel' => 'shortcut icon',
176 'href' => common_path('favicon.ico')));
179 if (common_config('site', 'mobile')) {
180 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/apple-touch-icon.png')) {
181 $this->element('link', array('rel' => 'apple-touch-icon',
182 'href' => Theme::path('apple-touch-icon.png')));
184 $this->element('link', array('rel' => 'apple-touch-icon',
185 'href' => common_path('apple-touch-icon.png')));
195 function showStylesheets()
197 if (Event::handle('StartShowStyles', array($this))) {
199 // Use old name for StatusNet for compatibility on events
201 if (Event::handle('StartShowStatusNetStyles', array($this)) &&
202 Event::handle('StartShowLaconicaStyles', array($this))) {
203 $this->primaryCssLink(null, 'screen, projection, tv, print');
204 Event::handle('EndShowStatusNetStyles', array($this));
205 Event::handle('EndShowLaconicaStyles', array($this));
208 if (Event::handle('StartShowUAStyles', array($this))) {
209 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
210 'href="'.Theme::path('css/ie.css', 'base').'?version='.STATUSNET_VERSION.'" /><![endif]');
211 foreach (array(6,7) as $ver) {
212 if (file_exists(Theme::file('css/ie'.$ver.'.css', 'base'))) {
213 // Yes, IE people should be put in jail.
214 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
215 'href="'.Theme::path('css/ie'.$ver.'.css', 'base').'?version='.STATUSNET_VERSION.'" /><![endif]');
218 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
219 'href="'.Theme::path('css/ie.css', null).'?version='.STATUSNET_VERSION.'" /><![endif]');
220 Event::handle('EndShowUAStyles', array($this));
223 if (Event::handle('StartShowDesign', array($this))) {
225 $user = common_current_user();
227 if (empty($user) || $user->viewdesigns) {
228 $design = $this->getDesign();
230 if (!empty($design)) {
231 $design->showCSS($this);
235 Event::handle('EndShowDesign', array($this));
237 Event::handle('EndShowStyles', array($this));
239 if (common_config('custom_css', 'enabled')) {
240 $css = common_config('custom_css', 'css');
241 if (Event::handle('StartShowCustomCss', array($this, &$css))) {
242 if (trim($css) != '') {
245 Event::handle('EndShowCustomCss', array($this));
251 function primaryCssLink($mainTheme=null, $media=null)
253 // If the currently-selected theme has dependencies on other themes,
254 // we'll need to load their display.css files as well in order.
255 $theme = new Theme($mainTheme);
256 $baseThemes = $theme->getDeps();
257 foreach ($baseThemes as $baseTheme) {
258 $this->cssLink('css/display.css', $baseTheme, $media);
260 $this->cssLink('css/display.css', $mainTheme, $media);
264 * Show javascript headers
268 function showScripts()
270 if (Event::handle('StartShowScripts', array($this))) {
271 if (Event::handle('StartShowJQueryScripts', array($this))) {
272 $this->script('jquery.min.js');
273 $this->script('jquery.form.js');
274 $this->script('jquery.cookie.js');
275 $this->inlineScript('if (typeof window.JSON !== "object") { $.getScript("'.common_path('js/json2.js').'"); }');
276 $this->script('jquery.joverlay.min.js');
277 Event::handle('EndShowJQueryScripts', array($this));
279 if (Event::handle('StartShowStatusNetScripts', array($this)) &&
280 Event::handle('StartShowLaconicaScripts', array($this))) {
281 $this->script('xbImportNode.js');
282 $this->script('util.js');
283 $this->script('geometa.js');
284 // Frame-busting code to avoid clickjacking attacks.
285 $this->inlineScript('if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
286 Event::handle('EndShowStatusNetScripts', array($this));
287 Event::handle('EndShowLaconicaScripts', array($this));
289 Event::handle('EndShowScripts', array($this));
294 * Show OpenSearch headers
298 function showOpenSearch()
300 $this->element('link', array('rel' => 'search',
301 'type' => 'application/opensearchdescription+xml',
302 'href' => common_local_url('opensearch', array('type' => 'people')),
303 'title' => common_config('site', 'name').' People Search'));
304 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
305 'href' => common_local_url('opensearch', array('type' => 'notice')),
306 'title' => common_config('site', 'name').' Notice Search'));
319 $feeds = $this->getFeeds();
322 foreach ($feeds as $feed) {
323 $this->element('link', array('rel' => $feed->rel(),
324 'href' => $feed->url,
325 'type' => $feed->mimeType(),
326 'title' => $feed->title));
338 function showDescription()
340 // does nothing by default
344 * Show extra stuff in <head>.
352 // does nothing by default
358 * Calls template methods
364 $this->elementStart('body', (common_current_user()) ? array('id' => $this->trimmed('action'),
365 'class' => 'user_in')
366 : array('id' => $this->trimmed('action')));
367 $this->elementStart('div', array('id' => 'wrap'));
368 if (Event::handle('StartShowHeader', array($this))) {
370 Event::handle('EndShowHeader', array($this));
373 if (Event::handle('StartShowFooter', array($this))) {
375 Event::handle('EndShowFooter', array($this));
377 $this->elementEnd('div');
378 $this->showScripts();
379 $this->elementEnd('body');
383 * Show header of the page.
385 * Calls template methods
389 function showHeader()
391 $this->elementStart('div', array('id' => 'header'));
393 $this->showPrimaryNav();
394 if (Event::handle('StartShowSiteNotice', array($this))) {
395 $this->showSiteNotice();
397 Event::handle('EndShowSiteNotice', array($this));
399 if (common_logged_in()) {
400 $this->showNoticeForm();
402 $this->showAnonymousMessage();
404 $this->elementEnd('div');
408 * Show configured logo.
414 $this->elementStart('address', array('id' => 'site_contact',
415 'class' => 'vcard'));
416 if (Event::handle('StartAddressData', array($this))) {
417 if (common_config('singleuser', 'enabled')) {
418 $url = common_local_url('showstream',
419 array('nickname' => common_config('singleuser', 'nickname')));
421 $url = common_local_url('public');
423 $this->elementStart('a', array('class' => 'url home bookmark',
425 if (common_config('site', 'logo') || file_exists(Theme::file('logo.png'))) {
426 $this->element('img', array('class' => 'logo photo',
427 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : Theme::path('logo.png'),
428 'alt' => common_config('site', 'name')));
431 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
432 $this->elementEnd('a');
433 Event::handle('EndAddressData', array($this));
435 $this->elementEnd('address');
439 * Show primary navigation.
443 function showPrimaryNav()
445 $user = common_current_user();
446 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
447 // TRANS: DT element for primary navigation menu. String is hidden in default CSS.
448 $this->element('dt', null, _('Primary site navigation'));
449 $this->elementStart('dd');
450 $this->elementStart('ul', array('class' => 'nav'));
451 if (Event::handle('StartPrimaryNav', array($this))) {
453 // TRANS: Tooltip for main menu option "Personal"
454 $tooltip = _m('TOOLTIP', 'Personal profile and friends timeline');
455 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
456 // TRANS: Main menu option when logged in for access to personal profile and friends timeline
457 _m('MENU', 'Personal'), $tooltip, false, 'nav_home');
458 // TRANS: Tooltip for main menu option "Account"
459 $tooltip = _m('TOOLTIP', 'Change your email, avatar, password, profile');
460 $this->menuItem(common_local_url('profilesettings'),
461 // TRANS: Main menu option when logged in for access to user settings
462 _('Account'), $tooltip, false, 'nav_account');
463 // TRANS: Tooltip for main menu option "Services"
464 $tooltip = _m('TOOLTIP', 'Connect to services');
465 $this->menuItem(common_local_url('oauthconnectionssettings'),
466 // TRANS: Main menu option when logged in and connection are possible for access to options to connect to other services
467 _('Connect'), $tooltip, false, 'nav_connect');
468 if ($user->hasRight(Right::CONFIGURESITE)) {
469 // TRANS: Tooltip for menu option "Admin"
470 $tooltip = _m('TOOLTIP', 'Change site configuration');
471 $this->menuItem(common_local_url('siteadminpanel'),
472 // TRANS: Main menu option when logged in and site admin for access to site configuration
473 _m('MENU', 'Admin'), $tooltip, false, 'nav_admin');
475 if (common_config('invite', 'enabled')) {
476 // TRANS: Tooltip for main menu option "Invite"
477 $tooltip = _m('TOOLTIP', 'Invite friends and colleagues to join you on %s');
478 $this->menuItem(common_local_url('invite'),
479 // TRANS: Main menu option when logged in and invitations are allowed for inviting new users
480 _m('MENU', 'Invite'),
482 common_config('site', 'name')),
483 false, 'nav_invitecontact');
485 // TRANS: Tooltip for main menu option "Logout"
486 $tooltip = _m('TOOLTIP', 'Logout from the site');
487 $this->menuItem(common_local_url('logout'),
488 // TRANS: Main menu option when logged in to log out the current user
489 _m('MENU', 'Logout'), $tooltip, false, 'nav_logout');
492 if (!common_config('site', 'closed') && !common_config('site', 'inviteonly')) {
493 // TRANS: Tooltip for main menu option "Register"
494 $tooltip = _m('TOOLTIP', 'Create an account');
495 $this->menuItem(common_local_url('register'),
496 // TRANS: Main menu option when not logged in to register a new account
497 _m('MENU', 'Register'), $tooltip, false, 'nav_register');
499 // TRANS: Tooltip for main menu option "Login"
500 $tooltip = _m('TOOLTIP', 'Login to the site');
501 // TRANS: Main menu option when not logged in to log in
502 $this->menuItem(common_local_url('login'),
503 _m('MENU', 'Login'), $tooltip, false, 'nav_login');
505 // TRANS: Tooltip for main menu option "Help"
506 $tooltip = _m('TOOLTIP', 'Help me!');
507 // TRANS: Main menu option for help on the StatusNet site
508 $this->menuItem(common_local_url('doc', array('title' => 'help')),
509 _m('MENU', 'Help'), $tooltip, false, 'nav_help');
510 if ($user || !common_config('site', 'private')) {
511 // TRANS: Tooltip for main menu option "Search"
512 $tooltip = _m('TOOLTIP', 'Search for people or text');
513 // TRANS: Main menu option when logged in or when the StatusNet instance is not private
514 $this->menuItem(common_local_url('peoplesearch'),
515 _m('MENU', 'Search'), $tooltip, false, 'nav_search');
517 Event::handle('EndPrimaryNav', array($this));
519 $this->elementEnd('ul');
520 $this->elementEnd('dd');
521 $this->elementEnd('dl');
529 function showSiteNotice()
531 // Revist. Should probably do an hAtom pattern here
532 $text = common_config('site', 'notice');
534 $this->elementStart('dl', array('id' => 'site_notice',
535 'class' => 'system_notice'));
536 // TRANS: DT element for site notice. String is hidden in default CSS.
537 $this->element('dt', null, _('Site notice'));
538 $this->elementStart('dd', null);
540 $this->elementEnd('dd');
541 $this->elementEnd('dl');
548 * MAY overload if no notice form needed... or direct message box????
552 function showNoticeForm()
554 $notice_form = new NoticeForm($this);
555 $notice_form->show();
559 * Show anonymous message.
565 function showAnonymousMessage()
567 // needs to be defined by the class
573 * Shows local navigation, content block and aside.
579 $this->elementStart('div', array('id' => 'core'));
580 if (Event::handle('StartShowLocalNavBlock', array($this))) {
581 $this->showLocalNavBlock();
582 Event::handle('EndShowLocalNavBlock', array($this));
584 if (Event::handle('StartShowContentBlock', array($this))) {
585 $this->showContentBlock();
586 Event::handle('EndShowContentBlock', array($this));
588 if (Event::handle('StartShowAside', array($this))) {
590 Event::handle('EndShowAside', array($this));
592 $this->elementEnd('div');
596 * Show local navigation block.
600 function showLocalNavBlock()
602 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
603 // TRANS: DT element for local views block. String is hidden in default CSS.
604 $this->element('dt', null, _('Local views'));
605 $this->elementStart('dd');
606 $this->showLocalNav();
607 $this->elementEnd('dd');
608 $this->elementEnd('dl');
612 * Show local navigation.
618 function showLocalNav()
620 // does nothing by default
624 * Show content block.
628 function showContentBlock()
630 $this->elementStart('div', array('id' => 'content'));
631 $this->showPageTitle();
632 $this->showPageNoticeBlock();
633 $this->elementStart('div', array('id' => 'content_inner'));
634 // show the actual content (forms, lists, whatever)
635 $this->showContent();
636 $this->elementEnd('div');
637 $this->elementEnd('div');
645 function showPageTitle()
647 $this->element('h1', null, $this->title());
651 * Show page notice block.
653 * Only show the block if a subclassed action has overrided
654 * Action::showPageNotice(), or an event handler is registered for
655 * the StartShowPageNotice event, in which case we assume the
656 * 'page_notice' definition list is desired. This is to prevent
657 * empty 'page_notice' definition lists from being output everywhere.
661 function showPageNoticeBlock()
663 $rmethod = new ReflectionMethod($this, 'showPageNotice');
664 $dclass = $rmethod->getDeclaringClass()->getName();
666 if ($dclass != 'Action' || Event::hasHandler('StartShowPageNotice')) {
668 $this->elementStart('dl', array('id' => 'page_notice',
669 'class' => 'system_notice'));
670 // TRANS: DT element for page notice. String is hidden in default CSS.
671 $this->element('dt', null, _('Page notice'));
672 $this->elementStart('dd');
673 if (Event::handle('StartShowPageNotice', array($this))) {
674 $this->showPageNotice();
675 Event::handle('EndShowPageNotice', array($this));
677 $this->elementEnd('dd');
678 $this->elementEnd('dl');
685 * SHOULD overload (unless there's not a notice)
689 function showPageNotice()
696 * MUST overload (unless there's not a notice)
700 function showContent()
712 $this->elementStart('div', array('id' => 'aside_primary',
713 'class' => 'aside'));
714 if (Event::handle('StartShowExportData', array($this))) {
715 $this->showExportData();
716 Event::handle('EndShowExportData', array($this));
718 if (Event::handle('StartShowSections', array($this))) {
719 $this->showSections();
720 Event::handle('EndShowSections', array($this));
722 $this->elementEnd('div');
726 * Show export data feeds.
731 function showExportData()
733 $feeds = $this->getFeeds();
735 $fl = new FeedList($this);
747 function showSections()
749 // for each section, show it
757 function showFooter()
759 $this->elementStart('div', array('id' => 'footer'));
760 $this->showSecondaryNav();
761 $this->showLicenses();
762 $this->elementEnd('div');
766 * Show secondary navigation.
770 function showSecondaryNav()
772 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
773 // TRANS: DT element for secondary navigation menu. String is hidden in default CSS.
774 $this->element('dt', null, _('Secondary site navigation'));
775 $this->elementStart('dd', null);
776 $this->elementStart('ul', array('class' => 'nav'));
777 if (Event::handle('StartSecondaryNav', array($this))) {
778 $this->menuItem(common_local_url('doc', array('title' => 'help')),
779 // TRANS: Secondary navigation menu option leading to help on StatusNet.
781 $this->menuItem(common_local_url('doc', array('title' => 'about')),
782 // TRANS: Secondary navigation menu option leading to text about StatusNet site.
784 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
785 // TRANS: Secondary navigation menu option leading to Frequently Asked Questions.
787 $bb = common_config('site', 'broughtby');
789 $this->menuItem(common_local_url('doc', array('title' => 'tos')),
790 // TRANS: Secondary navigation menu option leading to Terms of Service.
793 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
794 // TRANS: Secondary navigation menu option leading to privacy policy.
796 $this->menuItem(common_local_url('doc', array('title' => 'source')),
797 // TRANS: Secondary navigation menu option.
799 $this->menuItem(common_local_url('version'),
800 // TRANS: Secondary navigation menu option leading to version information on the StatusNet site.
802 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
803 // TRANS: Secondary navigation menu option leading to contact information on the StatusNet site.
805 $this->menuItem(common_local_url('doc', array('title' => 'badge')),
807 Event::handle('EndSecondaryNav', array($this));
809 $this->elementEnd('ul');
810 $this->elementEnd('dd');
811 $this->elementEnd('dl');
819 function showLicenses()
821 $this->elementStart('dl', array('id' => 'licenses'));
822 $this->showStatusNetLicense();
823 $this->showContentLicense();
824 $this->elementEnd('dl');
828 * Show StatusNet license.
832 function showStatusNetLicense()
834 // TRANS: DT element for StatusNet software license.
835 $this->element('dt', array('id' => 'site_statusnet_license'), _('StatusNet software license'));
836 $this->elementStart('dd', null);
837 if (common_config('site', 'broughtby')) {
838 // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is set.
839 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%).');
841 // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is not set.
842 $instr = _('**%%site.name%%** is a microblogging service.');
845 // TRANS: Second sentence of the StatusNet site license. Mentions the StatusNet source code license.
846 $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);
847 $output = common_markup_to_html($instr);
849 $this->elementEnd('dd');
854 * Show content license.
858 function showContentLicense()
860 if (Event::handle('StartShowContentLicense', array($this))) {
861 // TRANS: DT element for StatusNet site content license.
862 $this->element('dt', array('id' => 'site_content_license'), _('Site content license'));
863 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
865 switch (common_config('license', 'type')) {
867 // TRANS: Content license displayed when license is set to 'private'.
868 // TRANS: %1$s is the site name.
869 $this->element('p', null, sprintf(_('Content and data of %1$s are private and confidential.'),
870 common_config('site', 'name')));
872 case 'allrightsreserved':
873 if (common_config('license', 'owner')) {
874 // TRANS: Content license displayed when license is set to 'allrightsreserved'.
875 // TRANS: %1$s is the copyright owner.
876 $this->element('p', null, sprintf(_('Content and data copyright by %1$s. All rights reserved.'),
877 common_config('license', 'owner')));
879 // TRANS: Content license displayed when license is set to 'allrightsreserved' and no owner is set.
880 $this->element('p', null, _('Content and data copyright by contributors. All rights reserved.'));
883 case 'cc': // fall through
885 $this->elementStart('p');
886 $this->element('img', array('id' => 'license_cc',
887 'src' => common_config('license', 'image'),
888 'alt' => common_config('license', 'title'),
892 // TRANS: license message in footer. %1$s is the site name, %2$s is a link to the license URL, with a licence name set in configuration.
893 $notice = _('All %1$s content and data are available under the %2$s license.');
894 $link = "<a class=\"license\" rel=\"external license\" href=\"" .
895 htmlspecialchars(common_config('license', 'url')) .
897 htmlspecialchars(common_config('license', 'title')) .
899 $this->raw(sprintf(htmlspecialchars($notice),
900 htmlspecialchars(common_config('site', 'name')),
902 $this->elementEnd('p');
906 $this->elementEnd('dd');
907 Event::handle('EndShowContentLicense', array($this));
912 * Return last modified, if applicable.
916 * @return string last modified http header
918 function lastModified()
920 // For comparison with If-Last-Modified
921 // If not applicable, return null
926 * Return etag, if applicable.
930 * @return string etag http header
938 * Return true if read only.
942 * @param array $args other arguments
944 * @return boolean is read only action?
947 function isReadOnly($args)
953 * Returns query argument or default value if not found
955 * @param string $key requested argument
956 * @param string $def default value to return if $key is not provided
958 * @return boolean is read only action?
960 function arg($key, $def=null)
962 if (array_key_exists($key, $this->args)) {
963 return $this->args[$key];
970 * Returns trimmed query argument or default value if not found
972 * @param string $key requested argument
973 * @param string $def default value to return if $key is not provided
975 * @return boolean is read only action?
977 function trimmed($key, $def=null)
979 $arg = $this->arg($key, $def);
980 return is_string($arg) ? trim($arg) : $arg;
986 * @param array $argarray is ignored since it's now passed in in prepare()
988 * @return boolean is read only action?
990 function handle($argarray=null)
992 header('Vary: Accept-Encoding,Cookie');
993 $lm = $this->lastModified();
994 $etag = $this->etag();
996 header('ETag: ' . $etag);
999 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
1000 if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
1001 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
1002 $ims = strtotime($if_modified_since);
1004 $if_none_match = (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) ?
1005 $_SERVER['HTTP_IF_NONE_MATCH'] : null;
1006 if (!$if_none_match ||
1008 $this->_hasEtag($etag, $if_none_match)) {
1009 header('HTTP/1.1 304 Not Modified');
1010 // Better way to do this?
1019 * Has etag? (private)
1021 * @param string $etag etag http header
1022 * @param string $if_none_match ifNoneMatch http header
1027 function _hasEtag($etag, $if_none_match)
1029 $etags = explode(',', $if_none_match);
1030 return in_array($etag, $etags) || in_array('*', $etags);
1034 * Boolean understands english (yes, no, true, false)
1036 * @param string $key query key we're interested in
1037 * @param string $def default value
1039 * @return boolean interprets yes/no strings as boolean
1041 function boolean($key, $def=false)
1043 $arg = strtolower($this->trimmed($key));
1045 if (is_null($arg)) {
1047 } else if (in_array($arg, array('true', 'yes', '1', 'on'))) {
1049 } else if (in_array($arg, array('false', 'no', '0'))) {
1057 * Integer value of an argument
1059 * @param string $key query key we're interested in
1060 * @param string $defValue optional default value (default null)
1061 * @param string $maxValue optional max value (default null)
1062 * @param string $minValue optional min value (default null)
1064 * @return integer integer value
1067 function int($key, $defValue=null, $maxValue=null, $minValue=null)
1069 $arg = strtolower($this->trimmed($key));
1071 if (is_null($arg) || !is_integer($arg)) {
1075 if (!is_null($maxValue)) {
1076 $arg = min($arg, $maxValue);
1079 if (!is_null($minValue)) {
1080 $arg = max($arg, $minValue);
1089 * @param string $msg error message to display
1090 * @param integer $code http error code, 500 by default
1095 function serverError($msg, $code=500)
1097 $action = $this->trimmed('action');
1098 common_debug("Server error '$code' on '$action': $msg", __FILE__);
1099 throw new ServerException($msg, $code);
1105 * @param string $msg error message to display
1106 * @param integer $code http error code, 400 by default
1111 function clientError($msg, $code=400)
1113 $action = $this->trimmed('action');
1114 common_debug("User error '$code' on '$action': $msg", __FILE__);
1115 throw new ClientException($msg, $code);
1119 * Returns the current URL
1121 * @return string current URL
1126 list($action, $args) = $this->returnToArgs();
1127 return common_local_url($action, $args);
1131 * Returns arguments sufficient for re-constructing URL
1133 * @return array two elements: action, other args
1136 function returnToArgs()
1138 $action = $this->trimmed('action');
1139 $args = $this->args;
1140 unset($args['action']);
1141 if (common_config('site', 'fancy')) {
1144 if (array_key_exists('submit', $args)) {
1145 unset($args['submit']);
1147 foreach (array_keys($_COOKIE) as $cookie) {
1148 unset($args[$cookie]);
1150 return array($action, $args);
1154 * Generate a menu item
1156 * @param string $url menu URL
1157 * @param string $text menu name
1158 * @param string $title title attribute, null by default
1159 * @param boolean $is_selected current menu item, false by default
1160 * @param string $id element id, null by default
1164 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
1166 // Added @id to li for some control.
1167 // XXX: We might want to move this to htmloutputter.php
1170 $lattrs['class'] = 'current';
1173 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
1175 $this->elementStart('li', $lattrs);
1176 $attrs['href'] = $url;
1178 $attrs['title'] = $title;
1180 $this->element('a', $attrs, $text);
1181 $this->elementEnd('li');
1185 * Generate pagination links
1187 * @param boolean $have_before is there something before?
1188 * @param boolean $have_after is there something after?
1189 * @param integer $page current page
1190 * @param string $action current action
1191 * @param array $args rest of query arguments
1195 // XXX: The messages in this pagination method only tailor to navigating
1196 // notices. In other lists, "Previous"/"Next" type navigation is
1197 // desirable, but not available.
1198 function pagination($have_before, $have_after, $page, $action, $args=null)
1200 // Does a little before-after block for next/prev page
1201 if ($have_before || $have_after) {
1202 $this->elementStart('dl', 'pagination');
1203 // TRANS: DT element for pagination (previous/next, etc.).
1204 $this->element('dt', null, _('Pagination'));
1205 $this->elementStart('dd', null);
1206 $this->elementStart('ul', array('class' => 'nav'));
1209 $pargs = array('page' => $page-1);
1210 $this->elementStart('li', array('class' => 'nav_prev'));
1211 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1213 // TRANS: Pagination message to go to a page displaying information more in the
1214 // TRANS: present than the currently displayed information.
1216 $this->elementEnd('li');
1219 $pargs = array('page' => $page+1);
1220 $this->elementStart('li', array('class' => 'nav_next'));
1221 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1223 // TRANS: Pagination message to go to a page displaying information more in the
1224 // TRANS: past than the currently displayed information.
1226 $this->elementEnd('li');
1228 if ($have_before || $have_after) {
1229 $this->elementEnd('ul');
1230 $this->elementEnd('dd');
1231 $this->elementEnd('dl');
1236 * An array of feeds for this action.
1238 * Returns an array of potential feeds for this action.
1240 * @return array Feed object to show in head and links
1249 * A design for this action
1251 * @return Design a design object to use
1254 function getDesign()
1256 return Design::siteDesign();
1260 * Check the session token.
1262 * Checks that the current form has the correct session token,
1263 * and throw an exception if it does not.
1268 // XXX: Finding this type of check with the same message about 50 times.
1269 // Possible to refactor?
1270 function checkSessionToken()
1273 $token = $this->trimmed('token');
1274 if (empty($token) || $token != common_session_token()) {
1275 $this->clientError(_('There was a problem with your session token.'));