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('xbImportNode.js');
285 $this->script('util.js');
286 $this->script('geometa.js');
287 // Frame-busting code to avoid clickjacking attacks.
288 $this->inlineScript('if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
289 Event::handle('EndShowStatusNetScripts', array($this));
290 Event::handle('EndShowLaconicaScripts', array($this));
292 Event::handle('EndShowScripts', array($this));
297 * Show OpenSearch headers
301 function showOpenSearch()
303 $this->element('link', array('rel' => 'search',
304 'type' => 'application/opensearchdescription+xml',
305 'href' => common_local_url('opensearch', array('type' => 'people')),
306 'title' => common_config('site', 'name').' People Search'));
307 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
308 'href' => common_local_url('opensearch', array('type' => 'notice')),
309 'title' => common_config('site', 'name').' Notice Search'));
322 $feeds = $this->getFeeds();
325 foreach ($feeds as $feed) {
326 $this->element('link', array('rel' => $feed->rel(),
327 'href' => $feed->url,
328 'type' => $feed->mimeType(),
329 'title' => $feed->title));
341 function showDescription()
343 // does nothing by default
347 * Show extra stuff in <head>.
355 // does nothing by default
361 * Calls template methods
367 $this->elementStart('body', (common_current_user()) ? array('id' => $this->trimmed('action'),
368 'class' => 'user_in')
369 : array('id' => $this->trimmed('action')));
370 $this->elementStart('div', array('id' => 'wrap'));
371 if (Event::handle('StartShowHeader', array($this))) {
373 Event::handle('EndShowHeader', array($this));
376 if (Event::handle('StartShowFooter', array($this))) {
378 Event::handle('EndShowFooter', array($this));
380 $this->elementEnd('div');
381 $this->showScripts();
382 $this->elementEnd('body');
386 * Show header of the page.
388 * Calls template methods
392 function showHeader()
394 $this->elementStart('div', array('id' => 'header'));
396 $this->showPrimaryNav();
397 if (Event::handle('StartShowSiteNotice', array($this))) {
398 $this->showSiteNotice();
400 Event::handle('EndShowSiteNotice', array($this));
402 if (common_logged_in()) {
403 $this->showNoticeForm();
405 $this->showAnonymousMessage();
407 $this->elementEnd('div');
411 * Show configured logo.
417 $this->elementStart('address', array('id' => 'site_contact',
418 'class' => 'vcard'));
419 if (Event::handle('StartAddressData', array($this))) {
420 if (common_config('singleuser', 'enabled')) {
421 $url = common_local_url('showstream',
422 array('nickname' => common_config('singleuser', 'nickname')));
424 $url = common_local_url('public');
426 $this->elementStart('a', array('class' => 'url home bookmark',
428 if (common_config('site', 'logo') || file_exists(Theme::file('logo.png'))) {
429 $this->element('img', array('class' => 'logo photo',
430 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : Theme::path('logo.png'),
431 'alt' => common_config('site', 'name')));
434 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
435 $this->elementEnd('a');
436 Event::handle('EndAddressData', array($this));
438 $this->elementEnd('address');
442 * Show primary navigation.
446 function showPrimaryNav()
448 $user = common_current_user();
449 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
450 // TRANS: DT element for primary navigation menu. String is hidden in default CSS.
451 $this->element('dt', null, _('Primary site navigation'));
452 $this->elementStart('dd');
453 $this->elementStart('ul', array('class' => 'nav'));
454 if (Event::handle('StartPrimaryNav', array($this))) {
456 // TRANS: Tooltip for main menu option "Personal"
457 $tooltip = _m('TOOLTIP', 'Personal profile and friends timeline');
458 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
459 // TRANS: Main menu option when logged in for access to personal profile and friends timeline
460 _m('MENU', 'Personal'), $tooltip, false, 'nav_home');
461 // TRANS: Tooltip for main menu option "Account"
462 $tooltip = _m('TOOLTIP', 'Change your email, avatar, password, profile');
463 $this->menuItem(common_local_url('profilesettings'),
464 // TRANS: Main menu option when logged in for access to user settings
465 _('Account'), $tooltip, false, 'nav_account');
466 // TRANS: Tooltip for main menu option "Services"
467 $tooltip = _m('TOOLTIP', 'Connect to services');
468 $this->menuItem(common_local_url('oauthconnectionssettings'),
469 // TRANS: Main menu option when logged in and connection are possible for access to options to connect to other services
470 _('Connect'), $tooltip, false, 'nav_connect');
471 if ($user->hasRight(Right::CONFIGURESITE)) {
472 // TRANS: Tooltip for menu option "Admin"
473 $tooltip = _m('TOOLTIP', 'Change site configuration');
474 $this->menuItem(common_local_url('siteadminpanel'),
475 // TRANS: Main menu option when logged in and site admin for access to site configuration
476 _m('MENU', 'Admin'), $tooltip, false, 'nav_admin');
478 if (common_config('invite', 'enabled')) {
479 // TRANS: Tooltip for main menu option "Invite"
480 $tooltip = _m('TOOLTIP', 'Invite friends and colleagues to join you on %s');
481 $this->menuItem(common_local_url('invite'),
482 // TRANS: Main menu option when logged in and invitations are allowed for inviting new users
483 _m('MENU', 'Invite'),
485 common_config('site', 'name')),
486 false, 'nav_invitecontact');
488 // TRANS: Tooltip for main menu option "Logout"
489 $tooltip = _m('TOOLTIP', 'Logout from the site');
490 $this->menuItem(common_local_url('logout'),
491 // TRANS: Main menu option when logged in to log out the current user
492 _m('MENU', 'Logout'), $tooltip, false, 'nav_logout');
495 if (!common_config('site', 'closed') && !common_config('site', 'inviteonly')) {
496 // TRANS: Tooltip for main menu option "Register"
497 $tooltip = _m('TOOLTIP', 'Create an account');
498 $this->menuItem(common_local_url('register'),
499 // TRANS: Main menu option when not logged in to register a new account
500 _m('MENU', 'Register'), $tooltip, false, 'nav_register');
502 // TRANS: Tooltip for main menu option "Login"
503 $tooltip = _m('TOOLTIP', 'Login to the site');
504 // TRANS: Main menu option when not logged in to log in
505 $this->menuItem(common_local_url('login'),
506 _m('MENU', 'Login'), $tooltip, false, 'nav_login');
508 // TRANS: Tooltip for main menu option "Help"
509 $tooltip = _m('TOOLTIP', 'Help me!');
510 // TRANS: Main menu option for help on the StatusNet site
511 $this->menuItem(common_local_url('doc', array('title' => 'help')),
512 _m('MENU', 'Help'), $tooltip, false, 'nav_help');
513 if ($user || !common_config('site', 'private')) {
514 // TRANS: Tooltip for main menu option "Search"
515 $tooltip = _m('TOOLTIP', 'Search for people or text');
516 // TRANS: Main menu option when logged in or when the StatusNet instance is not private
517 $this->menuItem(common_local_url('peoplesearch'),
518 _m('MENU', 'Search'), $tooltip, false, 'nav_search');
520 Event::handle('EndPrimaryNav', array($this));
522 $this->elementEnd('ul');
523 $this->elementEnd('dd');
524 $this->elementEnd('dl');
532 function showSiteNotice()
534 // Revist. Should probably do an hAtom pattern here
535 $text = common_config('site', 'notice');
537 $this->elementStart('dl', array('id' => 'site_notice',
538 'class' => 'system_notice'));
539 // TRANS: DT element for site notice. String is hidden in default CSS.
540 $this->element('dt', null, _('Site notice'));
541 $this->elementStart('dd', null);
543 $this->elementEnd('dd');
544 $this->elementEnd('dl');
551 * MAY overload if no notice form needed... or direct message box????
555 function showNoticeForm()
557 $notice_form = new NoticeForm($this);
558 $notice_form->show();
562 * Show anonymous message.
568 function showAnonymousMessage()
570 // needs to be defined by the class
576 * Shows local navigation, content block and aside.
582 $this->elementStart('div', array('id' => 'core'));
583 if (Event::handle('StartShowLocalNavBlock', array($this))) {
584 $this->showLocalNavBlock();
585 Event::handle('EndShowLocalNavBlock', array($this));
587 if (Event::handle('StartShowContentBlock', array($this))) {
588 $this->showContentBlock();
589 Event::handle('EndShowContentBlock', array($this));
591 if (Event::handle('StartShowAside', array($this))) {
593 Event::handle('EndShowAside', array($this));
595 $this->elementEnd('div');
599 * Show local navigation block.
603 function showLocalNavBlock()
605 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
606 // TRANS: DT element for local views block. String is hidden in default CSS.
607 $this->element('dt', null, _('Local views'));
608 $this->elementStart('dd');
609 $this->showLocalNav();
610 $this->elementEnd('dd');
611 $this->elementEnd('dl');
615 * Show local navigation.
621 function showLocalNav()
623 // does nothing by default
627 * Show content block.
631 function showContentBlock()
633 $this->elementStart('div', array('id' => 'content'));
634 if (Event::handle('StartShowPageTitle', array($this))) {
635 $this->showPageTitle();
636 Event::handle('EndShowPageTitle', array($this));
638 $this->showPageNoticeBlock();
639 $this->elementStart('div', array('id' => 'content_inner'));
640 // show the actual content (forms, lists, whatever)
641 $this->showContent();
642 $this->elementEnd('div');
643 $this->elementEnd('div');
651 function showPageTitle()
653 $this->element('h1', null, $this->title());
657 * Show page notice block.
659 * Only show the block if a subclassed action has overrided
660 * Action::showPageNotice(), or an event handler is registered for
661 * the StartShowPageNotice event, in which case we assume the
662 * 'page_notice' definition list is desired. This is to prevent
663 * empty 'page_notice' definition lists from being output everywhere.
667 function showPageNoticeBlock()
669 $rmethod = new ReflectionMethod($this, 'showPageNotice');
670 $dclass = $rmethod->getDeclaringClass()->getName();
672 if ($dclass != 'Action' || Event::hasHandler('StartShowPageNotice')) {
674 $this->elementStart('dl', array('id' => 'page_notice',
675 'class' => 'system_notice'));
676 // TRANS: DT element for page notice. String is hidden in default CSS.
677 $this->element('dt', null, _('Page notice'));
678 $this->elementStart('dd');
679 if (Event::handle('StartShowPageNotice', array($this))) {
680 $this->showPageNotice();
681 Event::handle('EndShowPageNotice', array($this));
683 $this->elementEnd('dd');
684 $this->elementEnd('dl');
691 * SHOULD overload (unless there's not a notice)
695 function showPageNotice()
702 * MUST overload (unless there's not a notice)
706 function showContent()
718 $this->elementStart('div', array('id' => 'aside_primary',
719 'class' => 'aside'));
720 if (Event::handle('StartShowExportData', array($this))) {
721 $this->showExportData();
722 Event::handle('EndShowExportData', array($this));
724 if (Event::handle('StartShowSections', array($this))) {
725 $this->showSections();
726 Event::handle('EndShowSections', array($this));
728 $this->elementEnd('div');
732 * Show export data feeds.
737 function showExportData()
739 $feeds = $this->getFeeds();
741 $fl = new FeedList($this);
753 function showSections()
755 // for each section, show it
763 function showFooter()
765 $this->elementStart('div', array('id' => 'footer'));
766 $this->showSecondaryNav();
767 $this->showLicenses();
768 $this->elementEnd('div');
772 * Show secondary navigation.
776 function showSecondaryNav()
778 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
779 // TRANS: DT element for secondary navigation menu. String is hidden in default CSS.
780 $this->element('dt', null, _('Secondary site navigation'));
781 $this->elementStart('dd', null);
782 $this->elementStart('ul', array('class' => 'nav'));
783 if (Event::handle('StartSecondaryNav', array($this))) {
784 $this->menuItem(common_local_url('doc', array('title' => 'help')),
785 // TRANS: Secondary navigation menu option leading to help on StatusNet.
787 $this->menuItem(common_local_url('doc', array('title' => 'about')),
788 // TRANS: Secondary navigation menu option leading to text about StatusNet site.
790 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
791 // TRANS: Secondary navigation menu option leading to Frequently Asked Questions.
793 $bb = common_config('site', 'broughtby');
795 $this->menuItem(common_local_url('doc', array('title' => 'tos')),
796 // TRANS: Secondary navigation menu option leading to Terms of Service.
799 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
800 // TRANS: Secondary navigation menu option leading to privacy policy.
802 $this->menuItem(common_local_url('doc', array('title' => 'source')),
803 // TRANS: Secondary navigation menu option.
805 $this->menuItem(common_local_url('version'),
806 // TRANS: Secondary navigation menu option leading to version information on the StatusNet site.
808 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
809 // TRANS: Secondary navigation menu option leading to contact information on the StatusNet site.
811 $this->menuItem(common_local_url('doc', array('title' => 'badge')),
813 Event::handle('EndSecondaryNav', array($this));
815 $this->elementEnd('ul');
816 $this->elementEnd('dd');
817 $this->elementEnd('dl');
825 function showLicenses()
827 $this->elementStart('dl', array('id' => 'licenses'));
828 $this->showStatusNetLicense();
829 $this->showContentLicense();
830 $this->elementEnd('dl');
834 * Show StatusNet license.
838 function showStatusNetLicense()
840 // TRANS: DT element for StatusNet software license.
841 $this->element('dt', array('id' => 'site_statusnet_license'), _('StatusNet software license'));
842 $this->elementStart('dd', null);
843 if (common_config('site', 'broughtby')) {
844 // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is set.
845 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%).');
847 // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is not set.
848 $instr = _('**%%site.name%%** is a microblogging service.');
851 // TRANS: Second sentence of the StatusNet site license. Mentions the StatusNet source code license.
852 $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);
853 $output = common_markup_to_html($instr);
855 $this->elementEnd('dd');
860 * Show content license.
864 function showContentLicense()
866 if (Event::handle('StartShowContentLicense', array($this))) {
867 // TRANS: DT element for StatusNet site content license.
868 $this->element('dt', array('id' => 'site_content_license'), _('Site content license'));
869 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
871 switch (common_config('license', 'type')) {
873 // TRANS: Content license displayed when license is set to 'private'.
874 // TRANS: %1$s is the site name.
875 $this->element('p', null, sprintf(_('Content and data of %1$s are private and confidential.'),
876 common_config('site', 'name')));
878 case 'allrightsreserved':
879 if (common_config('license', 'owner')) {
880 // TRANS: Content license displayed when license is set to 'allrightsreserved'.
881 // TRANS: %1$s is the copyright owner.
882 $this->element('p', null, sprintf(_('Content and data copyright by %1$s. All rights reserved.'),
883 common_config('license', 'owner')));
885 // TRANS: Content license displayed when license is set to 'allrightsreserved' and no owner is set.
886 $this->element('p', null, _('Content and data copyright by contributors. All rights reserved.'));
889 case 'cc': // fall through
891 $this->elementStart('p');
892 $this->element('img', array('id' => 'license_cc',
893 'src' => common_config('license', 'image'),
894 'alt' => common_config('license', 'title'),
898 // 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.
899 $notice = _('All %1$s content and data are available under the %2$s license.');
900 $link = "<a class=\"license\" rel=\"external license\" href=\"" .
901 htmlspecialchars(common_config('license', 'url')) .
903 htmlspecialchars(common_config('license', 'title')) .
905 $this->raw(sprintf(htmlspecialchars($notice),
906 htmlspecialchars(common_config('site', 'name')),
908 $this->elementEnd('p');
912 $this->elementEnd('dd');
913 Event::handle('EndShowContentLicense', array($this));
918 * Return last modified, if applicable.
922 * @return string last modified http header
924 function lastModified()
926 // For comparison with If-Last-Modified
927 // If not applicable, return null
932 * Return etag, if applicable.
936 * @return string etag http header
944 * Return true if read only.
948 * @param array $args other arguments
950 * @return boolean is read only action?
953 function isReadOnly($args)
959 * Returns query argument or default value if not found
961 * @param string $key requested argument
962 * @param string $def default value to return if $key is not provided
964 * @return boolean is read only action?
966 function arg($key, $def=null)
968 if (array_key_exists($key, $this->args)) {
969 return $this->args[$key];
976 * Returns trimmed query argument or default value if not found
978 * @param string $key requested argument
979 * @param string $def default value to return if $key is not provided
981 * @return boolean is read only action?
983 function trimmed($key, $def=null)
985 $arg = $this->arg($key, $def);
986 return is_string($arg) ? trim($arg) : $arg;
992 * @param array $argarray is ignored since it's now passed in in prepare()
994 * @return boolean is read only action?
996 function handle($argarray=null)
998 header('Vary: Accept-Encoding,Cookie');
999 $lm = $this->lastModified();
1000 $etag = $this->etag();
1002 header('ETag: ' . $etag);
1005 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
1006 if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
1007 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
1008 $ims = strtotime($if_modified_since);
1010 $if_none_match = (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) ?
1011 $_SERVER['HTTP_IF_NONE_MATCH'] : null;
1012 if (!$if_none_match ||
1014 $this->_hasEtag($etag, $if_none_match)) {
1015 header('HTTP/1.1 304 Not Modified');
1016 // Better way to do this?
1025 * HasĀ etag? (private)
1027 * @param string $etag etag http header
1028 * @param string $if_none_match ifNoneMatch http header
1033 function _hasEtag($etag, $if_none_match)
1035 $etags = explode(',', $if_none_match);
1036 return in_array($etag, $etags) || in_array('*', $etags);
1040 * Boolean understands english (yes, no, true, false)
1042 * @param string $key query key we're interested in
1043 * @param string $def default value
1045 * @return boolean interprets yes/no strings as boolean
1047 function boolean($key, $def=false)
1049 $arg = strtolower($this->trimmed($key));
1051 if (is_null($arg)) {
1053 } else if (in_array($arg, array('true', 'yes', '1', 'on'))) {
1055 } else if (in_array($arg, array('false', 'no', '0'))) {
1063 * Integer value of an argument
1065 * @param string $key query key we're interested in
1066 * @param string $defValue optional default value (default null)
1067 * @param string $maxValue optional max value (default null)
1068 * @param string $minValue optional min value (default null)
1070 * @return integer integer value
1073 function int($key, $defValue=null, $maxValue=null, $minValue=null)
1075 $arg = strtolower($this->trimmed($key));
1077 if (is_null($arg) || !is_integer($arg)) {
1081 if (!is_null($maxValue)) {
1082 $arg = min($arg, $maxValue);
1085 if (!is_null($minValue)) {
1086 $arg = max($arg, $minValue);
1095 * @param string $msg error message to display
1096 * @param integer $code http error code, 500 by default
1101 function serverError($msg, $code=500)
1103 $action = $this->trimmed('action');
1104 common_debug("Server error '$code' on '$action': $msg", __FILE__);
1105 throw new ServerException($msg, $code);
1111 * @param string $msg error message to display
1112 * @param integer $code http error code, 400 by default
1117 function clientError($msg, $code=400)
1119 $action = $this->trimmed('action');
1120 common_debug("User error '$code' on '$action': $msg", __FILE__);
1121 throw new ClientException($msg, $code);
1125 * Returns the current URL
1127 * @return string current URL
1132 list($action, $args) = $this->returnToArgs();
1133 return common_local_url($action, $args);
1137 * Returns arguments sufficient for re-constructing URL
1139 * @return array two elements: action, other args
1142 function returnToArgs()
1144 $action = $this->trimmed('action');
1145 $args = $this->args;
1146 unset($args['action']);
1147 if (common_config('site', 'fancy')) {
1150 if (array_key_exists('submit', $args)) {
1151 unset($args['submit']);
1153 foreach (array_keys($_COOKIE) as $cookie) {
1154 unset($args[$cookie]);
1156 return array($action, $args);
1160 * Generate a menu item
1162 * @param string $url menu URL
1163 * @param string $text menu name
1164 * @param string $title title attribute, null by default
1165 * @param boolean $is_selected current menu item, false by default
1166 * @param string $id element id, null by default
1170 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
1172 // Added @id to li for some control.
1173 // XXX: We might want to move this to htmloutputter.php
1176 $lattrs['class'] = 'current';
1179 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
1181 $this->elementStart('li', $lattrs);
1182 $attrs['href'] = $url;
1184 $attrs['title'] = $title;
1186 $this->element('a', $attrs, $text);
1187 $this->elementEnd('li');
1191 * Generate pagination links
1193 * @param boolean $have_before is there something before?
1194 * @param boolean $have_after is there something after?
1195 * @param integer $page current page
1196 * @param string $action current action
1197 * @param array $args rest of query arguments
1201 // XXX: The messages in this pagination method only tailor to navigating
1202 // notices. In other lists, "Previous"/"Next" type navigation is
1203 // desirable, but not available.
1204 function pagination($have_before, $have_after, $page, $action, $args=null)
1206 // Does a little before-after block for next/prev page
1207 if ($have_before || $have_after) {
1208 $this->elementStart('dl', 'pagination');
1209 // TRANS: DT element for pagination (previous/next, etc.).
1210 $this->element('dt', null, _('Pagination'));
1211 $this->elementStart('dd', null);
1212 $this->elementStart('ul', array('class' => 'nav'));
1215 $pargs = array('page' => $page-1);
1216 $this->elementStart('li', array('class' => 'nav_prev'));
1217 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1219 // TRANS: Pagination message to go to a page displaying information more in the
1220 // TRANS: present than the currently displayed information.
1222 $this->elementEnd('li');
1225 $pargs = array('page' => $page+1);
1226 $this->elementStart('li', array('class' => 'nav_next'));
1227 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1229 // TRANS: Pagination message to go to a page displaying information more in the
1230 // TRANS: past than the currently displayed information.
1232 $this->elementEnd('li');
1234 if ($have_before || $have_after) {
1235 $this->elementEnd('ul');
1236 $this->elementEnd('dd');
1237 $this->elementEnd('dl');
1242 * An array of feeds for this action.
1244 * Returns an array of potential feeds for this action.
1246 * @return array Feed object to show in head and links
1255 * A design for this action
1257 * @return Design a design object to use
1260 function getDesign()
1262 return Design::siteDesign();
1266 * Check the session token.
1268 * Checks that the current form has the correct session token,
1269 * and throw an exception if it does not.
1274 // XXX: Finding this type of check with the same message about 50 times.
1275 // Possible to refactor?
1276 function checkSessionToken()
1279 $token = $this->trimmed('token');
1280 if (empty($token) || $token != common_session_token()) {
1281 $this->clientError(_('There was a problem with your session token.'));