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('GNUSOCIAL')) { exit(1); }
34 * Base class for all actions
36 * This is the base class for all actions in the package. An action is
37 * more or less a "view" in an MVC framework.
39 * Actions are responsible for extracting and validating parameters; using
40 * model classes to read and write to the database; and doing ouput.
44 * @author Evan Prodromou <evan@status.net>
45 * @author Sarven Capadisli <csarven@status.net>
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://status.net/
51 class Action extends HTMLOutputter // lawsuit
53 // This should be protected/private in the future
54 public $args = array();
56 // Action properties, set per-class
57 protected $action = false;
58 protected $ajax = false;
59 protected $menus = true;
60 protected $needLogin = false;
61 protected $needPost = false;
63 // The currently scoped profile (normally Profile::current; from $this->auth_user for API)
64 protected $scoped = null;
66 // Related to front-end user representation
67 protected $format = null;
68 protected $error = null;
69 protected $msg = null;
74 * Just wraps the HTMLOutputter constructor.
76 * @param string $output URI to output to, default = stdout
77 * @param boolean $indent Whether to indent output, default true
79 * @see XMLOutputter::__construct
80 * @see HTMLOutputter::__construct
82 function __construct($output='php://output', $indent=null)
84 parent::__construct($output, $indent);
97 static public function run(array $args=array(), $output='php://output', $indent=null) {
98 $class = get_called_class();
99 $action = new $class($output, $indent);
100 $action->execute($args);
104 public function execute(array $args=array()) {
106 if (common_config('db', 'mirror') && $this->isReadOnly($args)) {
107 if (is_array(common_config('db', 'mirror'))) {
108 // "load balancing", ha ha
109 $arr = common_config('db', 'mirror');
110 $k = array_rand($arr);
113 $mirror = common_config('db', 'mirror');
116 // everyone else uses the mirror
117 common_config_set('db', 'database', $mirror);
120 $status = $this->prepare($args);
122 $this->handle($args);
124 common_debug('Prepare failed for Action.');
129 Event::handle('EndActionExecute', array($status, $this));
133 * For initializing members of the class.
135 * @param array $argarray misc. arguments
137 * @return boolean true
139 protected function prepare(array $args=array())
141 if ($this->needPost && !$this->isPost()) {
142 // TRANS: Client error. POST is a HTTP command. It should not be translated.
143 $this->clientError(_('This method requires a POST.'), 405);
146 $this->args = common_copy_args($args);
148 $this->action = $this->trimmed('action');
150 if ($this->ajax || $this->boolean('ajax')) {
151 // check with StatusNet::isAjax()
152 StatusNet::setAjax(true);
155 if ($this->needLogin) {
156 $this->checkLogin(); // if not logged in, this redirs/excepts
159 $this->updateScopedProfile();
164 function updateScopedProfile() {
165 $this->scoped = Profile::current();
166 return $this->scoped;
169 public function getActionName()
171 return $this->action;
175 * Show page, a template method.
181 if (Event::handle('StartShowHTML', array($this))) {
184 Event::handle('EndShowHTML', array($this));
186 if (Event::handle('StartShowHead', array($this))) {
189 Event::handle('EndShowHead', array($this));
191 if (Event::handle('StartShowBody', array($this))) {
193 Event::handle('EndShowBody', array($this));
195 if (Event::handle('StartEndHTML', array($this))) {
197 Event::handle('EndEndHTML', array($this));
205 if (isset($_startTime)) {
206 $endTime = microtime(true);
207 $diff = round(($endTime - $_startTime) * 1000);
208 $this->raw("<!-- ${diff}ms -->");
211 return parent::endHTML();
215 * Show head, a template method.
221 // XXX: attributes (profile?)
222 $this->elementStart('head');
223 if (Event::handle('StartShowHeadElements', array($this))) {
224 if (Event::handle('StartShowHeadTitle', array($this))) {
226 Event::handle('EndShowHeadTitle', array($this));
228 $this->showShortcutIcon();
229 $this->showStylesheets();
230 $this->showOpenSearch();
232 $this->showDescription();
234 Event::handle('EndShowHeadElements', array($this));
236 $this->elementEnd('head');
240 * Show title, a template method.
246 $this->element('title', null,
247 // TRANS: Page title. %1$s is the title, %2$s is the site name.
248 sprintf(_('%1$s - %2$s'),
250 common_config('site', 'name')));
254 * Returns the page title
258 * @return string page title
263 // TRANS: Page title for a page without a title set.
264 return _('Untitled page');
268 * Show themed shortcut icon
272 function showShortcutIcon()
274 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/favicon.ico')) {
275 $this->element('link', array('rel' => 'shortcut icon',
276 'href' => Theme::path('favicon.ico')));
278 // favicon.ico should be HTTPS if the rest of the page is
279 $this->element('link', array('rel' => 'shortcut icon',
280 'href' => common_path('favicon.ico', StatusNet::isHTTPS())));
283 if (common_config('site', 'mobile')) {
284 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/apple-touch-icon.png')) {
285 $this->element('link', array('rel' => 'apple-touch-icon',
286 'href' => Theme::path('apple-touch-icon.png')));
288 $this->element('link', array('rel' => 'apple-touch-icon',
289 'href' => common_path('apple-touch-icon.png')));
299 function showStylesheets()
301 if (Event::handle('StartShowStyles', array($this))) {
303 // Use old name for StatusNet for compatibility on events
305 if (Event::handle('StartShowStylesheets', array($this))) {
306 $this->primaryCssLink(null, 'screen, projection, tv, print');
307 Event::handle('EndShowStylesheets', array($this));
310 $this->cssLink('js/extlib/jquery-ui/css/smoothness/jquery-ui.css');
312 if (Event::handle('StartShowUAStyles', array($this))) {
313 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
314 'href="'.Theme::path('css/ie.css', 'base').'?version='.GNUSOCIAL_VERSION.'" /><![endif]');
315 foreach (array(6,7) as $ver) {
316 if (file_exists(Theme::file('css/ie'.$ver.'.css', 'base'))) {
317 // Yes, IE people should be put in jail.
318 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
319 'href="'.Theme::path('css/ie'.$ver.'.css', 'base').'?version='.GNUSOCIAL_VERSION.'" /><![endif]');
322 if (file_exists(Theme::file('css/ie.css'))) {
323 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
324 'href="'.Theme::path('css/ie.css', null).'?version='.GNUSOCIAL_VERSION.'" /><![endif]');
326 Event::handle('EndShowUAStyles', array($this));
329 Event::handle('EndShowStyles', array($this));
331 if (common_config('custom_css', 'enabled')) {
332 $css = common_config('custom_css', 'css');
333 if (Event::handle('StartShowCustomCss', array($this, &$css))) {
334 if (trim($css) != '') {
337 Event::handle('EndShowCustomCss', array($this));
343 function primaryCssLink($mainTheme=null, $media=null)
345 $theme = new Theme($mainTheme);
347 // Some themes may have external stylesheets, such as using the
348 // Google Font APIs to load webfonts.
349 foreach ($theme->getExternals() as $url) {
350 $this->cssLink($url, $mainTheme, $media);
353 // If the currently-selected theme has dependencies on other themes,
354 // we'll need to load their display.css files as well in order.
355 $baseThemes = $theme->getDeps();
356 foreach ($baseThemes as $baseTheme) {
357 $this->cssLink('css/display.css', $baseTheme, $media);
359 $this->cssLink('css/display.css', $mainTheme, $media);
361 // Additional styles for RTL languages
362 if (is_rtl(common_language())) {
363 if (file_exists(Theme::file('css/rtl.css'))) {
364 $this->cssLink('css/rtl.css', $mainTheme, $media);
370 * Show javascript headers
374 function showScripts()
376 if (Event::handle('StartShowScripts', array($this))) {
377 if (Event::handle('StartShowJQueryScripts', array($this))) {
378 $this->script('extlib/jquery.js');
379 $this->script('extlib/jquery.form.js');
380 $this->script('extlib/jquery-ui/jquery-ui.js');
381 $this->script('extlib/jquery.cookie.js');
382 $this->inlineScript('if (typeof window.JSON !== "object") { $.getScript("'.common_path('js/extlib/json2.js', StatusNet::isHTTPS()).'"); }');
383 $this->script('extlib/jquery.infieldlabel.js');
385 Event::handle('EndShowJQueryScripts', array($this));
387 if (Event::handle('StartShowStatusNetScripts', array($this))) {
388 $this->script('util.js');
389 $this->script('xbImportNode.js');
390 $this->script('geometa.js');
392 // This route isn't available in single-user mode.
393 // Not sure why, but it causes errors here.
394 $this->inlineScript('var _peopletagAC = "' .
395 common_local_url('peopletagautocomplete') . '";');
396 $this->showScriptMessages();
397 // Anti-framing code to avoid clickjacking attacks in older browsers.
398 // This will show a blank page if the page is being framed, which is
399 // consistent with the behavior of the 'X-Frame-Options: SAMEORIGIN'
400 // header, which prevents framing in newer browser.
401 if (common_config('javascript', 'bustframes')) {
402 $this->inlineScript('if (window.top !== window.self) { document.write = ""; window.top.location = window.self.location; setTimeout(function () { document.body.innerHTML = ""; }, 1); window.self.onload = function () { document.body.innerHTML = ""; }; }');
404 Event::handle('EndShowStatusNetScripts', array($this));
406 Event::handle('EndShowScripts', array($this));
411 * Exports a map of localized text strings to JavaScript code.
413 * Plugins can add to what's exported by hooking the StartScriptMessages or EndScriptMessages
414 * events and appending to the array. Try to avoid adding strings that won't be used, as
415 * they'll be added to HTML output.
417 function showScriptMessages()
421 if (Event::handle('StartScriptMessages', array($this, &$messages))) {
422 // Common messages needed for timeline views etc...
424 // TRANS: Localized tooltip for '...' expansion button on overlong remote messages.
425 $messages['showmore_tooltip'] = _m('TOOLTIP', 'Show more');
427 // TRANS: Inline reply form submit button: submits a reply comment.
428 $messages['reply_submit'] = _m('BUTTON', 'Reply');
430 // TRANS: Placeholder text for inline reply form. Clicking in this box will turn it into a mini notice form.
431 $messages['reply_placeholder'] = _m('Write a reply...');
433 $messages = array_merge($messages, $this->getScriptMessages());
435 Event::handle('EndScriptMessages', array($this, &$messages));
438 if (!empty($messages)) {
439 $this->inlineScript('SN.messages=' . json_encode($messages));
446 * If the action will need localizable text strings, export them here like so:
448 * return array('pool_deepend' => _('Deep end'),
449 * 'pool_shallow' => _('Shallow end'));
451 * The exported map will be available via SN.msg() to JS code:
453 * $('#pool').html('<div class="deepend"></div><div class="shallow"></div>');
454 * $('#pool .deepend').text(SN.msg('pool_deepend'));
455 * $('#pool .shallow').text(SN.msg('pool_shallow'));
457 * Exports a map of localized text strings to JavaScript code.
459 * Plugins can add to what's exported on any action by hooking the StartScriptMessages or
460 * EndScriptMessages events and appending to the array. Try to avoid adding strings that won't
461 * be used, as they'll be added to HTML output.
463 function getScriptMessages()
469 * Show OpenSearch headers
473 function showOpenSearch()
475 $this->element('link', array('rel' => 'search',
476 'type' => 'application/opensearchdescription+xml',
477 'href' => common_local_url('opensearch', array('type' => 'people')),
478 'title' => common_config('site', 'name').' People Search'));
479 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
480 'href' => common_local_url('opensearch', array('type' => 'notice')),
481 'title' => common_config('site', 'name').' Notice Search'));
493 $feeds = $this->getFeeds();
496 foreach ($feeds as $feed) {
497 $this->element('link', array('rel' => $feed->rel(),
498 'href' => $feed->url,
499 'type' => $feed->mimeType(),
500 'title' => $feed->title));
512 function showDescription()
514 // does nothing by default
518 * Show extra stuff in <head>.
526 // does nothing by default
532 * Calls template methods
538 $this->elementStart('body', (common_current_user()) ? array('id' => strtolower($this->trimmed('action')),
539 'class' => 'user_in')
540 : array('id' => strtolower($this->trimmed('action'))));
541 $this->elementStart('div', array('id' => 'wrap'));
542 if (Event::handle('StartShowHeader', array($this))) {
545 Event::handle('EndShowHeader', array($this));
549 if (Event::handle('StartShowFooter', array($this))) {
552 Event::handle('EndShowFooter', array($this));
554 $this->elementEnd('div');
555 $this->showScripts();
556 $this->elementEnd('body');
560 * Show header of the page.
562 * Calls template methods
566 function showHeader()
568 $this->elementStart('div', array('id' => 'header'));
570 $this->showPrimaryNav();
571 if (Event::handle('StartShowSiteNotice', array($this))) {
572 $this->showSiteNotice();
574 Event::handle('EndShowSiteNotice', array($this));
577 $this->elementEnd('div');
581 * Show configured logo.
587 $this->elementStart('address', array('id' => 'site_contact',
588 'class' => 'vcard'));
589 if (Event::handle('StartAddressData', array($this))) {
590 if (common_config('singleuser', 'enabled')) {
591 $user = User::singleUser();
592 $url = common_local_url('showstream',
593 array('nickname' => $user->nickname));
594 } else if (common_logged_in()) {
595 $cur = common_current_user();
596 $url = common_local_url('all', array('nickname' => $cur->nickname));
598 $url = common_local_url('public');
601 $this->elementStart('a', array('class' => 'url home bookmark',
604 if (StatusNet::isHTTPS()) {
605 $logoUrl = common_config('site', 'ssllogo');
606 if (empty($logoUrl)) {
607 // if logo is an uploaded file, try to fall back to HTTPS file URL
608 $httpUrl = common_config('site', 'logo');
609 if (!empty($httpUrl)) {
610 $f = File::getKV('url', $httpUrl);
611 if (!empty($f) && !empty($f->filename)) {
612 // this will handle the HTTPS case
613 $logoUrl = File::url($f->filename);
618 $logoUrl = common_config('site', 'logo');
621 if (empty($logoUrl) && file_exists(Theme::file('logo.png'))) {
622 // This should handle the HTTPS case internally
623 $logoUrl = Theme::path('logo.png');
626 if (!empty($logoUrl)) {
627 $this->element('img', array('class' => 'logo photo',
629 'alt' => common_config('site', 'name')));
633 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
634 $this->elementEnd('a');
636 Event::handle('EndAddressData', array($this));
638 $this->elementEnd('address');
642 * Show primary navigation.
646 function showPrimaryNav()
648 $this->elementStart('div', array('id' => 'site_nav_global_primary'));
650 $user = common_current_user();
652 if (!empty($user) || !common_config('site', 'private')) {
653 $form = new SearchForm($this);
657 $pn = new PrimaryNav($this);
659 $this->elementEnd('div');
667 function showSiteNotice()
669 // Revist. Should probably do an hAtom pattern here
670 $text = common_config('site', 'notice');
672 $this->elementStart('div', array('id' => 'site_notice',
673 'class' => 'system_notice'));
675 $this->elementEnd('div');
682 * MAY overload if no notice form needed... or direct message box????
686 function showNoticeForm()
688 // TRANS: Tab on the notice form.
689 $tabs = array('status' => array('title' => _m('TAB','Status'),
690 'href' => common_local_url('newnotice')));
692 $this->elementStart('div', 'input_forms');
694 if (Event::handle('StartShowEntryForms', array(&$tabs))) {
695 $this->elementStart('ul', array('class' => 'nav',
696 'id' => 'input_form_nav'));
698 foreach ($tabs as $tag => $data) {
699 $tag = htmlspecialchars($tag);
700 $attrs = array('id' => 'input_form_nav_'.$tag,
701 'class' => 'input_form_nav_tab');
703 if ($tag == 'status') {
704 // We're actually showing the placeholder form,
705 // but we special-case the 'Status' tab as if
706 // it were a small version of it.
707 $attrs['class'] .= ' current';
709 $this->elementStart('li', $attrs);
712 array('onclick' => 'return SN.U.switchInputFormTab("'.$tag.'");',
713 'href' => $data['href']),
715 $this->elementEnd('li');
718 $this->elementEnd('ul');
720 $attrs = array('class' => 'input_form current',
721 'id' => 'input_form_placeholder');
722 $this->elementStart('div', $attrs);
723 $form = new NoticePlaceholderForm($this);
725 $this->elementEnd('div');
727 foreach ($tabs as $tag => $data) {
728 $attrs = array('class' => 'input_form',
729 'id' => 'input_form_'.$tag);
731 $this->elementStart('div', $attrs);
735 if (Event::handle('StartMakeEntryForm', array($tag, $this, &$form))) {
736 if ($tag == 'status') {
737 $options = $this->noticeFormOptions();
738 $form = new NoticeForm($this, $options);
740 Event::handle('EndMakeEntryForm', array($tag, $this, $form));
747 $this->elementEnd('div');
751 $this->elementEnd('div');
754 function noticeFormOptions()
760 * Show anonymous message.
766 function showAnonymousMessage()
768 // needs to be defined by the class
774 * Shows local navigation, content block and aside.
780 $this->elementStart('div', array('id' => 'core'));
781 $this->elementStart('div', array('id' => 'aside_primary_wrapper'));
782 $this->elementStart('div', array('id' => 'content_wrapper'));
783 $this->elementStart('div', array('id' => 'site_nav_local_views_wrapper'));
784 if (Event::handle('StartShowLocalNavBlock', array($this))) {
785 $this->showLocalNavBlock();
787 Event::handle('EndShowLocalNavBlock', array($this));
789 if (Event::handle('StartShowContentBlock', array($this))) {
790 $this->showContentBlock();
792 Event::handle('EndShowContentBlock', array($this));
794 if (Event::handle('StartShowAside', array($this))) {
797 Event::handle('EndShowAside', array($this));
799 $this->elementEnd('div');
800 $this->elementEnd('div');
801 $this->elementEnd('div');
802 $this->elementEnd('div');
806 * Show local navigation block.
810 function showLocalNavBlock()
812 // Need to have this ID for CSS; I'm too lazy to add it to
814 $this->elementStart('div', array('id' => 'site_nav_local_views'));
815 // Cheat cheat cheat!
816 $this->showLocalNav();
817 $this->elementEnd('div');
821 * If there's a logged-in user, show a bit of login context
825 function showProfileBlock()
827 if (common_logged_in()) {
828 $block = new DefaultProfileBlock($this);
834 * Show local navigation.
840 function showLocalNav()
842 $nav = new DefaultLocalNav($this);
847 * Show menu for an object (group, profile)
849 * This block will only show if a subclass has overridden
850 * the showObjectNav() method.
854 function showObjectNavBlock()
856 $rmethod = new ReflectionMethod($this, 'showObjectNav');
857 $dclass = $rmethod->getDeclaringClass()->getName();
859 if ($dclass != 'Action') {
860 // Need to have this ID for CSS; I'm too lazy to add it to
862 $this->elementStart('div', array('id' => 'site_nav_object',
863 'class' => 'section'));
864 $this->showObjectNav();
865 $this->elementEnd('div');
870 * Show object navigation.
872 * If there are things to do with this object, show it here.
876 function showObjectNav()
882 * Show content block.
886 function showContentBlock()
888 $this->elementStart('div', array('id' => 'content'));
889 if (common_logged_in()) {
890 if (Event::handle('StartShowNoticeForm', array($this))) {
891 $this->showNoticeForm();
892 Event::handle('EndShowNoticeForm', array($this));
895 if (Event::handle('StartShowPageTitle', array($this))) {
896 $this->showPageTitle();
897 Event::handle('EndShowPageTitle', array($this));
899 $this->showPageNoticeBlock();
900 $this->elementStart('div', array('id' => 'content_inner'));
901 // show the actual content (forms, lists, whatever)
902 $this->showContent();
903 $this->elementEnd('div');
904 $this->elementEnd('div');
912 function showPageTitle()
914 $this->element('h1', null, $this->title());
918 * Show page notice block.
920 * Only show the block if a subclassed action has overrided
921 * Action::showPageNotice(), or an event handler is registered for
922 * the StartShowPageNotice event, in which case we assume the
923 * 'page_notice' definition list is desired. This is to prevent
924 * empty 'page_notice' definition lists from being output everywhere.
928 function showPageNoticeBlock()
930 $rmethod = new ReflectionMethod($this, 'showPageNotice');
931 $dclass = $rmethod->getDeclaringClass()->getName();
933 if ($dclass != 'Action' || Event::hasHandler('StartShowPageNotice')) {
935 $this->elementStart('div', array('id' => 'page_notice',
936 'class' => 'system_notice'));
937 if (Event::handle('StartShowPageNotice', array($this))) {
938 $this->showPageNotice();
939 Event::handle('EndShowPageNotice', array($this));
941 $this->elementEnd('div');
948 * SHOULD overload (unless there's not a notice)
952 function showPageNotice()
959 * MUST overload (unless there's not a notice)
963 function showContent()
974 $this->elementStart('div', array('id' => 'aside_primary',
975 'class' => 'aside'));
976 $this->showProfileBlock();
977 if (Event::handle('StartShowObjectNavBlock', array($this))) {
978 $this->showObjectNavBlock();
979 Event::handle('EndShowObjectNavBlock', array($this));
981 if (Event::handle('StartShowSections', array($this))) {
982 $this->showSections();
983 Event::handle('EndShowSections', array($this));
985 if (Event::handle('StartShowExportData', array($this))) {
986 $this->showExportData();
987 Event::handle('EndShowExportData', array($this));
989 $this->elementEnd('div');
993 * Show export data feeds.
997 function showExportData()
999 $feeds = $this->getFeeds();
1001 $fl = new FeedList($this);
1013 function showSections()
1015 // for each section, show it
1023 function showFooter()
1025 $this->elementStart('div', array('id' => 'footer'));
1026 if (Event::handle('StartShowInsideFooter', array($this))) {
1027 $this->showSecondaryNav();
1028 $this->showLicenses();
1029 Event::handle('EndShowInsideFooter', array($this));
1031 $this->elementEnd('div');
1035 * Show secondary navigation.
1039 function showSecondaryNav()
1041 $sn = new SecondaryNav($this);
1050 function showLicenses()
1052 $this->showGNUsocialLicense();
1053 $this->showContentLicense();
1057 * Show GNU social license.
1061 function showGNUsocialLicense()
1063 if (common_config('site', 'broughtby')) {
1064 // TRANS: First sentence of the GNU social site license. Used if 'broughtby' is set.
1065 // TRANS: Text between [] is a link description, text between () is the link itself.
1066 // TRANS: Make sure there is no whitespace between "]" and "(".
1067 // TRANS: "%%site.broughtby%%" is the value of the variable site.broughtby
1068 $instr = _('**%%site.name%%** is a social network, courtesy of [%%site.broughtby%%](%%site.broughtbyurl%%).');
1070 // TRANS: First sentence of the GNU social site license. Used if 'broughtby' is not set.
1071 $instr = _('**%%site.name%%** is a social network.');
1074 // TRANS: Second sentence of the GNU social site license. Mentions the GNU social source code license.
1075 // TRANS: Make sure there is no whitespace between "]" and "(".
1076 // TRANS: [%1$s](%2$s) is a link description followed by the link itself
1077 // TRANS: %3$s is the version of GNU social that is being used.
1078 $instr .= sprintf(_('It runs on [%1$s](%2$s), version %3$s, available under the [GNU Affero General Public License](http://www.fsf.org/licensing/licenses/agpl-3.0.html).'), GNUSOCIAL_ENGINE, GNUSOCIAL_ENGINE_URL, GNUSOCIAL_VERSION);
1079 $output = common_markup_to_html($instr);
1080 $this->raw($output);
1085 * Show content license.
1089 function showContentLicense()
1091 if (Event::handle('StartShowContentLicense', array($this))) {
1092 switch (common_config('license', 'type')) {
1094 // TRANS: Content license displayed when license is set to 'private'.
1095 // TRANS: %1$s is the site name.
1096 $this->element('p', null, sprintf(_('Content and data of %1$s are private and confidential.'),
1097 common_config('site', 'name')));
1099 case 'allrightsreserved':
1100 if (common_config('license', 'owner')) {
1101 // TRANS: Content license displayed when license is set to 'allrightsreserved'.
1102 // TRANS: %1$s is the copyright owner.
1103 $this->element('p', null, sprintf(_('Content and data copyright by %1$s. All rights reserved.'),
1104 common_config('license', 'owner')));
1106 // TRANS: Content license displayed when license is set to 'allrightsreserved' and no owner is set.
1107 $this->element('p', null, _('Content and data copyright by contributors. All rights reserved.'));
1110 case 'cc': // fall through
1112 $this->elementStart('p');
1114 $image = common_config('license', 'image');
1115 $sslimage = common_config('license', 'sslimage');
1117 if (StatusNet::isHTTPS()) {
1118 if (!empty($sslimage)) {
1120 } else if (preg_match('#^http://i.creativecommons.org/#', $image)) {
1121 // CC support HTTPS on their images
1122 $url = preg_replace('/^http/', 'https', $image);
1124 // Better to show mixed content than no content
1131 $this->element('img', array('id' => 'license_cc',
1133 'alt' => common_config('license', 'title'),
1137 // TRANS: license message in footer.
1138 // TRANS: %1$s is the site name, %2$s is a link to the license URL, with a licence name set in configuration.
1139 $notice = _('All %1$s content and data are available under the %2$s license.');
1140 $link = "<a class=\"license\" rel=\"external license\" href=\"" .
1141 htmlspecialchars(common_config('license', 'url')) .
1143 htmlspecialchars(common_config('license', 'title')) .
1145 $this->raw(sprintf(htmlspecialchars($notice),
1146 htmlspecialchars(common_config('site', 'name')),
1148 $this->elementEnd('p');
1152 Event::handle('EndShowContentLicense', array($this));
1157 * Return last modified, if applicable.
1161 * @return string last modified http header
1163 function lastModified()
1165 // For comparison with If-Last-Modified
1166 // If not applicable, return null
1171 * Return etag, if applicable.
1175 * @return string etag http header
1183 * Return true if read only.
1187 * @param array $args other arguments
1189 * @return boolean is read only action?
1191 function isReadOnly($args)
1197 * Returns query argument or default value if not found
1199 * @param string $key requested argument
1200 * @param string $def default value to return if $key is not provided
1202 * @return boolean is read only action?
1204 function arg($key, $def=null)
1206 if (array_key_exists($key, $this->args)) {
1207 return $this->args[$key];
1214 * Returns trimmed query argument or default value if not found
1216 * @param string $key requested argument
1217 * @param string $def default value to return if $key is not provided
1219 * @return boolean is read only action?
1221 function trimmed($key, $def=null)
1223 $arg = $this->arg($key, $def);
1224 return is_string($arg) ? trim($arg) : $arg;
1230 * @return boolean is read only action?
1232 protected function handle()
1234 header('Vary: Accept-Encoding,Cookie');
1236 $lm = $this->lastModified();
1237 $etag = $this->etag();
1240 header('ETag: ' . $etag);
1244 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
1245 if ($this->isCacheable()) {
1246 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1247 header( "Cache-Control: private, must-revalidate, max-age=0" );
1254 $if_none_match = (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) ?
1255 $_SERVER['HTTP_IF_NONE_MATCH'] : null;
1256 if ($if_none_match) {
1257 // If this check fails, ignore the if-modified-since below.
1259 if ($this->_hasEtag($etag, $if_none_match)) {
1260 header('HTTP/1.1 304 Not Modified');
1261 // Better way to do this?
1267 if (!$checked && $lm && array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
1268 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
1269 $ims = strtotime($if_modified_since);
1271 header('HTTP/1.1 304 Not Modified');
1272 // Better way to do this?
1279 * Is this action cacheable?
1281 * If the action returns a last-modified
1283 * @param array $argarray is ignored since it's now passed in in prepare()
1285 * @return boolean is read only action?
1287 function isCacheable()
1293 * Has etag? (private)
1295 * @param string $etag etag http header
1296 * @param string $if_none_match ifNoneMatch http header
1300 function _hasEtag($etag, $if_none_match)
1302 $etags = explode(',', $if_none_match);
1303 return in_array($etag, $etags) || in_array('*', $etags);
1307 * Boolean understands english (yes, no, true, false)
1309 * @param string $key query key we're interested in
1310 * @param string $def default value
1312 * @return boolean interprets yes/no strings as boolean
1314 function boolean($key, $def=false)
1316 $arg = strtolower($this->trimmed($key));
1318 if (is_null($arg)) {
1320 } else if (in_array($arg, array('true', 'yes', '1', 'on'))) {
1322 } else if (in_array($arg, array('false', 'no', '0'))) {
1330 * Integer value of an argument
1332 * @param string $key query key we're interested in
1333 * @param string $defValue optional default value (default null)
1334 * @param string $maxValue optional max value (default null)
1335 * @param string $minValue optional min value (default null)
1337 * @return integer integer value
1339 function int($key, $defValue=null, $maxValue=null, $minValue=null)
1341 $arg = intval($this->arg($key));
1343 if (!is_numeric($this->arg($key)) || $arg != $this->arg($key)) {
1347 if (!is_null($maxValue)) {
1348 $arg = min($arg, $maxValue);
1351 if (!is_null($minValue)) {
1352 $arg = max($arg, $minValue);
1361 * @param string $msg error message to display
1362 * @param integer $code http error code, 500 by default
1366 function serverError($msg, $code=500, $format=null)
1368 if ($format === null) {
1369 $format = $this->format;
1372 common_debug("Server error '{$code}' on '{$this->action}': {$msg}", __FILE__);
1374 if (!array_key_exists($code, ServerErrorAction::$status)) {
1378 $status_string = ServerErrorAction::$status[$code];
1382 header("HTTP/1.1 {$code} {$status_string}");
1383 $this->initDocument('xml');
1384 $this->elementStart('hash');
1385 $this->element('error', null, $msg);
1386 $this->element('request', null, $_SERVER['REQUEST_URI']);
1387 $this->elementEnd('hash');
1388 $this->endDocument('xml');
1391 if (!isset($this->callback)) {
1392 header("HTTP/1.1 {$code} {$status_string}");
1394 $this->initDocument('json');
1395 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
1396 print(json_encode($error_array));
1397 $this->endDocument('json');
1400 throw new ServerException($msg, $code);
1409 * @param string $msg error message to display
1410 * @param integer $code http error code, 400 by default
1411 * @param string $format error format (json, xml, text) for ApiAction
1414 * @throws ClientException always
1416 function clientError($msg, $code=400, $format=null)
1418 // $format is currently only relevant for an ApiAction anyway
1419 if ($format === null) {
1420 $format = $this->format;
1423 common_debug("User error '{$code}' on '{$this->action}': {$msg}", __FILE__);
1425 if (!array_key_exists($code, ClientErrorAction::$status)) {
1429 $status_string = ClientErrorAction::$status[$code];
1433 header("HTTP/1.1 {$code} {$status_string}");
1434 $this->initDocument('xml');
1435 $this->elementStart('hash');
1436 $this->element('error', null, $msg);
1437 $this->element('request', null, $_SERVER['REQUEST_URI']);
1438 $this->elementEnd('hash');
1439 $this->endDocument('xml');
1442 if (!isset($this->callback)) {
1443 header("HTTP/1.1 {$code} {$status_string}");
1445 $this->initDocument('json');
1446 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
1447 $this->text(json_encode($error_array));
1448 $this->endDocument('json');
1451 header("HTTP/1.1 {$code} {$status_string}");
1452 header('Content-Type: text/plain; charset=utf-8');
1456 throw new ClientException($msg, $code);
1462 * If not logged in, take appropriate action (redir or exception)
1464 * @param boolean $redir Redirect to login if not logged in
1466 * @return boolean true if logged in (never returns if not)
1468 public function checkLogin($redir=true)
1470 if (common_logged_in()) {
1475 common_set_returnto($_SERVER['REQUEST_URI']);
1476 common_redirect(common_local_url('login'));
1479 // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
1480 $this->clientError(_('Not logged in.'), 403);
1484 * Returns the current URL
1486 * @return string current URL
1490 list($action, $args) = $this->returnToArgs();
1491 return common_local_url($action, $args);
1495 * Returns arguments sufficient for re-constructing URL
1497 * @return array two elements: action, other args
1499 function returnToArgs()
1501 $action = $this->trimmed('action');
1502 $args = $this->args;
1503 unset($args['action']);
1504 if (common_config('site', 'fancy')) {
1507 if (array_key_exists('submit', $args)) {
1508 unset($args['submit']);
1510 foreach (array_keys($_COOKIE) as $cookie) {
1511 unset($args[$cookie]);
1513 return array($action, $args);
1517 * Generate a menu item
1519 * @param string $url menu URL
1520 * @param string $text menu name
1521 * @param string $title title attribute, null by default
1522 * @param boolean $is_selected current menu item, false by default
1523 * @param string $id element id, null by default
1527 function menuItem($url, $text, $title=null, $is_selected=false, $id=null, $class=null)
1529 // Added @id to li for some control.
1530 // XXX: We might want to move this to htmloutputter.php
1533 if ($class !== null) {
1534 $classes[] = trim($class);
1537 $classes[] = 'current';
1540 if (!empty($classes)) {
1541 $lattrs['class'] = implode(' ', $classes);
1544 if (!is_null($id)) {
1545 $lattrs['id'] = $id;
1548 $this->elementStart('li', $lattrs);
1549 $attrs['href'] = $url;
1551 $attrs['title'] = $title;
1553 $this->element('a', $attrs, $text);
1554 $this->elementEnd('li');
1558 * Generate pagination links
1560 * @param boolean $have_before is there something before?
1561 * @param boolean $have_after is there something after?
1562 * @param integer $page current page
1563 * @param string $action current action
1564 * @param array $args rest of query arguments
1568 // XXX: The messages in this pagination method only tailor to navigating
1569 // notices. In other lists, "Previous"/"Next" type navigation is
1570 // desirable, but not available.
1571 function pagination($have_before, $have_after, $page, $action, $args=null)
1573 // Does a little before-after block for next/prev page
1574 if ($have_before || $have_after) {
1575 $this->elementStart('ul', array('class' => 'nav',
1576 'id' => 'pagination'));
1579 $pargs = array('page' => $page-1);
1580 $this->elementStart('li', array('class' => 'nav_prev'));
1581 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1583 // TRANS: Pagination message to go to a page displaying information more in the
1584 // TRANS: present than the currently displayed information.
1586 $this->elementEnd('li');
1589 $pargs = array('page' => $page+1);
1590 $this->elementStart('li', array('class' => 'nav_next'));
1591 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1593 // TRANS: Pagination message to go to a page displaying information more in the
1594 // TRANS: past than the currently displayed information.
1596 $this->elementEnd('li');
1598 if ($have_before || $have_after) {
1599 $this->elementEnd('ul');
1604 * An array of feeds for this action.
1606 * Returns an array of potential feeds for this action.
1608 * @return array Feed object to show in head and links
1616 * Check the session token.
1618 * Checks that the current form has the correct session token,
1619 * and throw an exception if it does not.
1623 // XXX: Finding this type of check with the same message about 50 times.
1624 // Possible to refactor?
1625 function checkSessionToken()
1628 $token = $this->trimmed('token');
1629 if (empty($token) || $token != common_session_token()) {
1630 // TRANS: Client error text when there is a problem with the session token.
1631 $this->clientError(_('There was a problem with your session token.'));
1636 * Check if the current request is a POST
1638 * @return boolean true if POST; otherwise false.
1643 return ($_SERVER['REQUEST_METHOD'] == 'POST');