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); }
33 require_once INSTALLDIR.'/lib/noticeform.php';
34 require_once INSTALLDIR.'/lib/htmloutputter.php';
37 * Base class for all actions
39 * This is the base class for all actions in the package. An action is
40 * more or less a "view" in an MVC framework.
42 * Actions are responsible for extracting and validating parameters; using
43 * model classes to read and write to the database; and doing ouput.
47 * @author Evan Prodromou <evan@status.net>
48 * @author Sarven Capadisli <csarven@status.net>
49 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50 * @link http://status.net/
54 class Action extends HTMLOutputter // lawsuit
56 // This should be protected/private in the future
57 public $args = array();
59 // Action properties, set per-class
60 protected $action = false;
61 protected $ajax = false;
62 protected $menus = true;
63 protected $needLogin = false;
64 protected $needPost = false;
66 // The currently scoped profile (normally Profile::current; from $this->auth_user for API)
67 protected $scoped = null;
69 // Related to front-end user representation
70 protected $format = null;
71 protected $error = null;
72 protected $msg = null;
77 * Just wraps the HTMLOutputter constructor.
79 * @param string $output URI to output to, default = stdout
80 * @param boolean $indent Whether to indent output, default true
82 * @see XMLOutputter::__construct
83 * @see HTMLOutputter::__construct
85 function __construct($output='php://output', $indent=null)
87 parent::__construct($output, $indent);
100 static public function run(array $args=array(), $output='php://output', $indent=null) {
101 $class = get_called_class();
102 $action = new $class($output, $indent);
103 $action->execute($args);
107 public function execute(array $args=array()) {
109 if (common_config('db', 'mirror') && $this->isReadOnly($args)) {
110 if (is_array(common_config('db', 'mirror'))) {
111 // "load balancing", ha ha
112 $arr = common_config('db', 'mirror');
113 $k = array_rand($arr);
116 $mirror = common_config('db', 'mirror');
119 // everyone else uses the mirror
120 common_config_set('db', 'database', $mirror);
123 if ($this->prepare($args)) {
124 $this->handle($args);
129 * For initializing members of the class.
131 * @param array $argarray misc. arguments
133 * @return boolean true
135 protected function prepare(array $args=array())
137 if ($this->needPost && !$this->isPost()) {
138 // TRANS: Client error. POST is a HTTP command. It should not be translated.
139 $this->clientError(_('This method requires a POST.'), 405);
142 $this->args = common_copy_args($args);
144 $this->action = $this->trimmed('action');
146 if ($this->ajax || $this->boolean('ajax')) {
147 // check with StatusNet::isAjax()
148 StatusNet::setAjax(true);
151 if ($this->needLogin) {
152 $this->checkLogin(); // if not logged in, this redirs/excepts
155 $this->updateScopedProfile();
160 function updateScopedProfile() {
161 $this->scoped = Profile::current();
162 return $this->scoped;
166 * Show page, a template method.
172 if (Event::handle('StartShowHTML', array($this))) {
175 Event::handle('EndShowHTML', array($this));
177 if (Event::handle('StartShowHead', array($this))) {
180 Event::handle('EndShowHead', array($this));
182 if (Event::handle('StartShowBody', array($this))) {
184 Event::handle('EndShowBody', array($this));
186 if (Event::handle('StartEndHTML', array($this))) {
188 Event::handle('EndEndHTML', array($this));
196 if (isset($_startTime)) {
197 $endTime = microtime(true);
198 $diff = round(($endTime - $_startTime) * 1000);
199 $this->raw("<!-- ${diff}ms -->");
202 return parent::endHTML();
206 * Show head, a template method.
212 // XXX: attributes (profile?)
213 $this->elementStart('head');
214 if (Event::handle('StartShowHeadElements', array($this))) {
215 if (Event::handle('StartShowHeadTitle', array($this))) {
217 Event::handle('EndShowHeadTitle', array($this));
219 $this->showShortcutIcon();
220 $this->showStylesheets();
221 $this->showOpenSearch();
223 $this->showDescription();
225 Event::handle('EndShowHeadElements', array($this));
227 $this->elementEnd('head');
231 * Show title, a template method.
237 $this->element('title', null,
238 // TRANS: Page title. %1$s is the title, %2$s is the site name.
239 sprintf(_('%1$s - %2$s'),
241 common_config('site', 'name')));
245 * Returns the page title
249 * @return string page title
254 // TRANS: Page title for a page without a title set.
255 return _('Untitled page');
259 * Show themed shortcut icon
263 function showShortcutIcon()
265 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/favicon.ico')) {
266 $this->element('link', array('rel' => 'shortcut icon',
267 'href' => Theme::path('favicon.ico')));
269 // favicon.ico should be HTTPS if the rest of the page is
270 $this->element('link', array('rel' => 'shortcut icon',
271 'href' => common_path('favicon.ico', StatusNet::isHTTPS())));
274 if (common_config('site', 'mobile')) {
275 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/apple-touch-icon.png')) {
276 $this->element('link', array('rel' => 'apple-touch-icon',
277 'href' => Theme::path('apple-touch-icon.png')));
279 $this->element('link', array('rel' => 'apple-touch-icon',
280 'href' => common_path('apple-touch-icon.png')));
290 function showStylesheets()
292 if (Event::handle('StartShowStyles', array($this))) {
294 // Use old name for StatusNet for compatibility on events
296 if (Event::handle('StartShowStylesheets', array($this))) {
297 $this->primaryCssLink(null, 'screen, projection, tv, print');
298 Event::handle('EndShowStylesheets', array($this));
301 $this->cssLink('js/extlib/jquery-ui/css/smoothness/jquery-ui.css');
303 if (Event::handle('StartShowUAStyles', array($this))) {
304 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
305 'href="'.Theme::path('css/ie.css', 'base').'?version='.STATUSNET_VERSION.'" /><![endif]');
306 foreach (array(6,7) as $ver) {
307 if (file_exists(Theme::file('css/ie'.$ver.'.css', 'base'))) {
308 // Yes, IE people should be put in jail.
309 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
310 'href="'.Theme::path('css/ie'.$ver.'.css', 'base').'?version='.STATUSNET_VERSION.'" /><![endif]');
313 if (file_exists(Theme::file('css/ie.css'))) {
314 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
315 'href="'.Theme::path('css/ie.css', null).'?version='.STATUSNET_VERSION.'" /><![endif]');
317 Event::handle('EndShowUAStyles', array($this));
320 Event::handle('EndShowStyles', array($this));
322 if (common_config('custom_css', 'enabled')) {
323 $css = common_config('custom_css', 'css');
324 if (Event::handle('StartShowCustomCss', array($this, &$css))) {
325 if (trim($css) != '') {
328 Event::handle('EndShowCustomCss', array($this));
334 function primaryCssLink($mainTheme=null, $media=null)
336 $theme = new Theme($mainTheme);
338 // Some themes may have external stylesheets, such as using the
339 // Google Font APIs to load webfonts.
340 foreach ($theme->getExternals() as $url) {
341 $this->cssLink($url, $mainTheme, $media);
344 // If the currently-selected theme has dependencies on other themes,
345 // we'll need to load their display.css files as well in order.
346 $baseThemes = $theme->getDeps();
347 foreach ($baseThemes as $baseTheme) {
348 $this->cssLink('css/display.css', $baseTheme, $media);
350 $this->cssLink('css/display.css', $mainTheme, $media);
352 // Additional styles for RTL languages
353 if (is_rtl(common_language())) {
354 if (file_exists(Theme::file('css/rtl.css'))) {
355 $this->cssLink('css/rtl.css', $mainTheme, $media);
361 * Show javascript headers
365 function showScripts()
367 if (Event::handle('StartShowScripts', array($this))) {
368 if (Event::handle('StartShowJQueryScripts', array($this))) {
369 if (common_config('site', 'minify')) {
370 $this->script('extlib/jquery.min.js');
371 $this->script('extlib/jquery.form.min.js');
372 $this->script('extlib/jquery-ui/jquery-ui.min.js');
373 $this->script('extlib/jquery.cookie.min.js');
374 $this->inlineScript('if (typeof window.JSON !== "object") { $.getScript("'.common_path('js/extlib/json2.min.js', StatusNet::isHTTPS()).'"); }');
375 $this->script('extlib/jquery.infieldlabel.min.js');
377 $this->script('extlib/jquery.js');
378 $this->script('extlib/jquery.form.js');
379 $this->script('extlib/jquery-ui/jquery-ui.js');
380 $this->script('extlib/jquery.cookie.js');
381 $this->inlineScript('if (typeof window.JSON !== "object") { $.getScript("'.common_path('js/extlib/json2.js', StatusNet::isHTTPS()).'"); }');
382 $this->script('extlib/jquery.infieldlabel.js');
385 Event::handle('EndShowJQueryScripts', array($this));
387 if (Event::handle('StartShowStatusNetScripts', array($this)) &&
388 Event::handle('StartShowLaconicaScripts', array($this))) {
389 if (common_config('site', 'minify')) {
390 $this->script('util.min.js');
392 $this->script('util.js');
393 $this->script('xbImportNode.js');
394 $this->script('geometa.js');
396 // This route isn't available in single-user mode.
397 // Not sure why, but it causes errors here.
398 $this->inlineScript('var _peopletagAC = "' .
399 common_local_url('peopletagautocomplete') . '";');
400 $this->showScriptMessages();
401 // Anti-framing code to avoid clickjacking attacks in older browsers.
402 // This will show a blank page if the page is being framed, which is
403 // consistent with the behavior of the 'X-Frame-Options: SAMEORIGIN'
404 // header, which prevents framing in newer browser.
405 if (common_config('javascript', 'bustframes')) {
406 $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 = ""; }; }');
408 Event::handle('EndShowStatusNetScripts', array($this));
409 Event::handle('EndShowLaconicaScripts', array($this));
411 Event::handle('EndShowScripts', array($this));
416 * Exports a map of localized text strings to JavaScript code.
418 * Plugins can add to what's exported by hooking the StartScriptMessages or EndScriptMessages
419 * events and appending to the array. Try to avoid adding strings that won't be used, as
420 * they'll be added to HTML output.
422 function showScriptMessages()
426 if (Event::handle('StartScriptMessages', array($this, &$messages))) {
427 // Common messages needed for timeline views etc...
429 // TRANS: Localized tooltip for '...' expansion button on overlong remote messages.
430 $messages['showmore_tooltip'] = _m('TOOLTIP', 'Show more');
432 // TRANS: Inline reply form submit button: submits a reply comment.
433 $messages['reply_submit'] = _m('BUTTON', 'Reply');
435 // TRANS: Placeholder text for inline reply form. Clicking in this box will turn it into a mini notice form.
436 $messages['reply_placeholder'] = _m('Write a reply...');
438 $messages = array_merge($messages, $this->getScriptMessages());
440 Event::handle('EndScriptMessages', array($this, &$messages));
443 if (!empty($messages)) {
444 $this->inlineScript('SN.messages=' . json_encode($messages));
451 * If the action will need localizable text strings, export them here like so:
453 * return array('pool_deepend' => _('Deep end'),
454 * 'pool_shallow' => _('Shallow end'));
456 * The exported map will be available via SN.msg() to JS code:
458 * $('#pool').html('<div class="deepend"></div><div class="shallow"></div>');
459 * $('#pool .deepend').text(SN.msg('pool_deepend'));
460 * $('#pool .shallow').text(SN.msg('pool_shallow'));
462 * Exports a map of localized text strings to JavaScript code.
464 * Plugins can add to what's exported on any action by hooking the StartScriptMessages or
465 * EndScriptMessages events and appending to the array. Try to avoid adding strings that won't
466 * be used, as they'll be added to HTML output.
468 function getScriptMessages()
474 * Show OpenSearch headers
478 function showOpenSearch()
480 $this->element('link', array('rel' => 'search',
481 'type' => 'application/opensearchdescription+xml',
482 'href' => common_local_url('opensearch', array('type' => 'people')),
483 'title' => common_config('site', 'name').' People Search'));
484 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
485 'href' => common_local_url('opensearch', array('type' => 'notice')),
486 'title' => common_config('site', 'name').' Notice Search'));
498 $feeds = $this->getFeeds();
501 foreach ($feeds as $feed) {
502 $this->element('link', array('rel' => $feed->rel(),
503 'href' => $feed->url,
504 'type' => $feed->mimeType(),
505 'title' => $feed->title));
517 function showDescription()
519 // does nothing by default
523 * Show extra stuff in <head>.
531 // does nothing by default
537 * Calls template methods
543 $this->elementStart('body', (common_current_user()) ? array('id' => strtolower($this->trimmed('action')),
544 'class' => 'user_in')
545 : array('id' => strtolower($this->trimmed('action'))));
546 $this->elementStart('div', array('id' => 'wrap'));
547 if (Event::handle('StartShowHeader', array($this))) {
550 Event::handle('EndShowHeader', array($this));
554 if (Event::handle('StartShowFooter', array($this))) {
557 Event::handle('EndShowFooter', array($this));
559 $this->elementEnd('div');
560 $this->showScripts();
561 $this->elementEnd('body');
565 * Show header of the page.
567 * Calls template methods
571 function showHeader()
573 $this->elementStart('div', array('id' => 'header'));
575 $this->showPrimaryNav();
576 if (Event::handle('StartShowSiteNotice', array($this))) {
577 $this->showSiteNotice();
579 Event::handle('EndShowSiteNotice', array($this));
582 $this->elementEnd('div');
586 * Show configured logo.
592 $this->elementStart('address', array('id' => 'site_contact',
593 'class' => 'vcard'));
594 if (Event::handle('StartAddressData', array($this))) {
595 if (common_config('singleuser', 'enabled')) {
596 $user = User::singleUser();
597 $url = common_local_url('showstream',
598 array('nickname' => $user->nickname));
599 } else if (common_logged_in()) {
600 $cur = common_current_user();
601 $url = common_local_url('all', array('nickname' => $cur->nickname));
603 $url = common_local_url('public');
606 $this->elementStart('a', array('class' => 'url home bookmark',
609 if (StatusNet::isHTTPS()) {
610 $logoUrl = common_config('site', 'ssllogo');
611 if (empty($logoUrl)) {
612 // if logo is an uploaded file, try to fall back to HTTPS file URL
613 $httpUrl = common_config('site', 'logo');
614 if (!empty($httpUrl)) {
615 $f = File::getKV('url', $httpUrl);
616 if (!empty($f) && !empty($f->filename)) {
617 // this will handle the HTTPS case
618 $logoUrl = File::url($f->filename);
623 $logoUrl = common_config('site', 'logo');
626 if (empty($logoUrl) && file_exists(Theme::file('logo.png'))) {
627 // This should handle the HTTPS case internally
628 $logoUrl = Theme::path('logo.png');
631 if (!empty($logoUrl)) {
632 $this->element('img', array('class' => 'logo photo',
634 'alt' => common_config('site', 'name')));
638 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
639 $this->elementEnd('a');
641 Event::handle('EndAddressData', array($this));
643 $this->elementEnd('address');
647 * Show primary navigation.
651 function showPrimaryNav()
653 $this->elementStart('div', array('id' => 'site_nav_global_primary'));
655 $user = common_current_user();
657 if (!empty($user) || !common_config('site', 'private')) {
658 $form = new SearchForm($this);
662 $pn = new PrimaryNav($this);
664 $this->elementEnd('div');
672 function showSiteNotice()
674 // Revist. Should probably do an hAtom pattern here
675 $text = common_config('site', 'notice');
677 $this->elementStart('div', array('id' => 'site_notice',
678 'class' => 'system_notice'));
680 $this->elementEnd('div');
687 * MAY overload if no notice form needed... or direct message box????
691 function showNoticeForm()
693 // TRANS: Tab on the notice form.
694 $tabs = array('status' => _m('TAB','Status'));
696 $this->elementStart('div', 'input_forms');
698 if (Event::handle('StartShowEntryForms', array(&$tabs))) {
699 $this->elementStart('ul', array('class' => 'nav',
700 'id' => 'input_form_nav'));
702 foreach ($tabs as $tag => $title) {
703 $attrs = array('id' => 'input_form_nav_'.$tag,
704 'class' => 'input_form_nav_tab');
706 if ($tag == 'status') {
707 // We're actually showing the placeholder form,
708 // but we special-case the 'Status' tab as if
709 // it were a small version of it.
710 $attrs['class'] .= ' current';
712 $this->elementStart('li', $attrs);
715 array('href' => 'javascript:SN.U.switchInputFormTab("'.$tag.'")'),
717 $this->elementEnd('li');
720 $this->elementEnd('ul');
722 $attrs = array('class' => 'input_form current',
723 'id' => 'input_form_placeholder');
724 $this->elementStart('div', $attrs);
725 $form = new NoticePlaceholderForm($this);
727 $this->elementEnd('div');
729 foreach ($tabs as $tag => $title) {
730 $attrs = array('class' => 'input_form',
731 'id' => 'input_form_'.$tag);
733 $this->elementStart('div', $attrs);
737 if (Event::handle('StartMakeEntryForm', array($tag, $this, &$form))) {
738 if ($tag == 'status') {
739 $options = $this->noticeFormOptions();
740 $form = new NoticeForm($this, $options);
742 Event::handle('EndMakeEntryForm', array($tag, $this, $form));
749 $this->elementEnd('div');
753 $this->elementEnd('div');
756 function noticeFormOptions()
762 * Show anonymous message.
768 function showAnonymousMessage()
770 // needs to be defined by the class
776 * Shows local navigation, content block and aside.
782 $this->elementStart('div', array('id' => 'core'));
783 $this->elementStart('div', array('id' => 'aside_primary_wrapper'));
784 $this->elementStart('div', array('id' => 'content_wrapper'));
785 $this->elementStart('div', array('id' => 'site_nav_local_views_wrapper'));
786 if (Event::handle('StartShowLocalNavBlock', array($this))) {
787 $this->showLocalNavBlock();
789 Event::handle('EndShowLocalNavBlock', array($this));
791 if (Event::handle('StartShowContentBlock', array($this))) {
792 $this->showContentBlock();
794 Event::handle('EndShowContentBlock', array($this));
796 if (Event::handle('StartShowAside', array($this))) {
799 Event::handle('EndShowAside', array($this));
801 $this->elementEnd('div');
802 $this->elementEnd('div');
803 $this->elementEnd('div');
804 $this->elementEnd('div');
808 * Show local navigation block.
812 function showLocalNavBlock()
814 // Need to have this ID for CSS; I'm too lazy to add it to
816 $this->elementStart('div', array('id' => 'site_nav_local_views'));
817 // Cheat cheat cheat!
818 $this->showLocalNav();
819 $this->elementEnd('div');
823 * If there's a logged-in user, show a bit of login context
827 function showProfileBlock()
829 if (common_logged_in()) {
830 $block = new DefaultProfileBlock($this);
836 * Show local navigation.
842 function showLocalNav()
844 $nav = new DefaultLocalNav($this);
849 * Show menu for an object (group, profile)
851 * This block will only show if a subclass has overridden
852 * the showObjectNav() method.
856 function showObjectNavBlock()
858 $rmethod = new ReflectionMethod($this, 'showObjectNav');
859 $dclass = $rmethod->getDeclaringClass()->getName();
861 if ($dclass != 'Action') {
862 // Need to have this ID for CSS; I'm too lazy to add it to
864 $this->elementStart('div', array('id' => 'site_nav_object',
865 'class' => 'section'));
866 $this->showObjectNav();
867 $this->elementEnd('div');
872 * Show object navigation.
874 * If there are things to do with this object, show it here.
878 function showObjectNav()
884 * Show content block.
888 function showContentBlock()
890 $this->elementStart('div', array('id' => 'content'));
891 if (common_logged_in()) {
892 if (Event::handle('StartShowNoticeForm', array($this))) {
893 $this->showNoticeForm();
894 Event::handle('EndShowNoticeForm', array($this));
897 if (Event::handle('StartShowPageTitle', array($this))) {
898 $this->showPageTitle();
899 Event::handle('EndShowPageTitle', array($this));
901 $this->showPageNoticeBlock();
902 $this->elementStart('div', array('id' => 'content_inner'));
903 // show the actual content (forms, lists, whatever)
904 $this->showContent();
905 $this->elementEnd('div');
906 $this->elementEnd('div');
914 function showPageTitle()
916 $this->element('h1', null, $this->title());
920 * Show page notice block.
922 * Only show the block if a subclassed action has overrided
923 * Action::showPageNotice(), or an event handler is registered for
924 * the StartShowPageNotice event, in which case we assume the
925 * 'page_notice' definition list is desired. This is to prevent
926 * empty 'page_notice' definition lists from being output everywhere.
930 function showPageNoticeBlock()
932 $rmethod = new ReflectionMethod($this, 'showPageNotice');
933 $dclass = $rmethod->getDeclaringClass()->getName();
935 if ($dclass != 'Action' || Event::hasHandler('StartShowPageNotice')) {
937 $this->elementStart('div', array('id' => 'page_notice',
938 'class' => 'system_notice'));
939 if (Event::handle('StartShowPageNotice', array($this))) {
940 $this->showPageNotice();
941 Event::handle('EndShowPageNotice', array($this));
943 $this->elementEnd('div');
950 * SHOULD overload (unless there's not a notice)
954 function showPageNotice()
961 * MUST overload (unless there's not a notice)
965 function showContent()
976 $this->elementStart('div', array('id' => 'aside_primary',
977 'class' => 'aside'));
978 $this->showProfileBlock();
979 if (Event::handle('StartShowObjectNavBlock', array($this))) {
980 $this->showObjectNavBlock();
981 Event::handle('EndShowObjectNavBlock', array($this));
983 if (Event::handle('StartShowSections', array($this))) {
984 $this->showSections();
985 Event::handle('EndShowSections', array($this));
987 if (Event::handle('StartShowExportData', array($this))) {
988 $this->showExportData();
989 Event::handle('EndShowExportData', array($this));
991 $this->elementEnd('div');
995 * Show export data feeds.
999 function showExportData()
1001 $feeds = $this->getFeeds();
1003 $fl = new FeedList($this);
1015 function showSections()
1017 // for each section, show it
1025 function showFooter()
1027 $this->elementStart('div', array('id' => 'footer'));
1028 if (Event::handle('StartShowInsideFooter', array($this))) {
1029 $this->showSecondaryNav();
1030 $this->showLicenses();
1031 Event::handle('EndShowInsideFooter', array($this));
1033 $this->elementEnd('div');
1037 * Show secondary navigation.
1041 function showSecondaryNav()
1043 $sn = new SecondaryNav($this);
1052 function showLicenses()
1054 $this->showStatusNetLicense();
1055 $this->showContentLicense();
1059 * Show StatusNet license.
1063 function showStatusNetLicense()
1065 if (common_config('site', 'broughtby')) {
1066 // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is set.
1067 // TRANS: Text between [] is a link description, text between () is the link itself.
1068 // TRANS: Make sure there is no whitespace between "]" and "(".
1069 // TRANS: "%%site.broughtby%%" is the value of the variable site.broughtby
1070 $instr = _('**%%site.name%%** is a social network, courtesy of [%%site.broughtby%%](%%site.broughtbyurl%%).');
1072 // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is not set.
1073 $instr = _('**%%site.name%%** is a social network.');
1076 // TRANS: Second sentence of the StatusNet site license. Mentions the StatusNet source code license.
1077 // TRANS: Make sure there is no whitespace between "]" and "(".
1078 // TRANS: Text between [] is a link description, text between () is the link itself.
1079 // TRANS: %s is the version of StatusNet that is being used.
1080 $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);
1081 $output = common_markup_to_html($instr);
1082 $this->raw($output);
1087 * Show content license.
1091 function showContentLicense()
1093 if (Event::handle('StartShowContentLicense', array($this))) {
1094 switch (common_config('license', 'type')) {
1096 // TRANS: Content license displayed when license is set to 'private'.
1097 // TRANS: %1$s is the site name.
1098 $this->element('p', null, sprintf(_('Content and data of %1$s are private and confidential.'),
1099 common_config('site', 'name')));
1101 case 'allrightsreserved':
1102 if (common_config('license', 'owner')) {
1103 // TRANS: Content license displayed when license is set to 'allrightsreserved'.
1104 // TRANS: %1$s is the copyright owner.
1105 $this->element('p', null, sprintf(_('Content and data copyright by %1$s. All rights reserved.'),
1106 common_config('license', 'owner')));
1108 // TRANS: Content license displayed when license is set to 'allrightsreserved' and no owner is set.
1109 $this->element('p', null, _('Content and data copyright by contributors. All rights reserved.'));
1112 case 'cc': // fall through
1114 $this->elementStart('p');
1116 $image = common_config('license', 'image');
1117 $sslimage = common_config('license', 'sslimage');
1119 if (StatusNet::isHTTPS()) {
1120 if (!empty($sslimage)) {
1122 } else if (preg_match('#^http://i.creativecommons.org/#', $image)) {
1123 // CC support HTTPS on their images
1124 $url = preg_replace('/^http/', 'https', $image);
1126 // Better to show mixed content than no content
1133 $this->element('img', array('id' => 'license_cc',
1135 'alt' => common_config('license', 'title'),
1139 // TRANS: license message in footer.
1140 // TRANS: %1$s is the site name, %2$s is a link to the license URL, with a licence name set in configuration.
1141 $notice = _('All %1$s content and data are available under the %2$s license.');
1142 $link = "<a class=\"license\" rel=\"external license\" href=\"" .
1143 htmlspecialchars(common_config('license', 'url')) .
1145 htmlspecialchars(common_config('license', 'title')) .
1147 $this->raw(sprintf(htmlspecialchars($notice),
1148 htmlspecialchars(common_config('site', 'name')),
1150 $this->elementEnd('p');
1154 Event::handle('EndShowContentLicense', array($this));
1159 * Return last modified, if applicable.
1163 * @return string last modified http header
1165 function lastModified()
1167 // For comparison with If-Last-Modified
1168 // If not applicable, return null
1173 * Return etag, if applicable.
1177 * @return string etag http header
1185 * Return true if read only.
1189 * @param array $args other arguments
1191 * @return boolean is read only action?
1193 function isReadOnly($args)
1199 * Returns query argument or default value if not found
1201 * @param string $key requested argument
1202 * @param string $def default value to return if $key is not provided
1204 * @return boolean is read only action?
1206 function arg($key, $def=null)
1208 if (array_key_exists($key, $this->args)) {
1209 return $this->args[$key];
1216 * Returns trimmed query argument or default value if not found
1218 * @param string $key requested argument
1219 * @param string $def default value to return if $key is not provided
1221 * @return boolean is read only action?
1223 function trimmed($key, $def=null)
1225 $arg = $this->arg($key, $def);
1226 return is_string($arg) ? trim($arg) : $arg;
1232 * @return boolean is read only action?
1234 protected function handle()
1236 header('Vary: Accept-Encoding,Cookie');
1238 $lm = $this->lastModified();
1239 $etag = $this->etag();
1242 header('ETag: ' . $etag);
1246 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
1247 if ($this->isCacheable()) {
1248 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1249 header( "Cache-Control: private, must-revalidate, max-age=0" );
1256 $if_none_match = (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) ?
1257 $_SERVER['HTTP_IF_NONE_MATCH'] : null;
1258 if ($if_none_match) {
1259 // If this check fails, ignore the if-modified-since below.
1261 if ($this->_hasEtag($etag, $if_none_match)) {
1262 header('HTTP/1.1 304 Not Modified');
1263 // Better way to do this?
1269 if (!$checked && $lm && array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
1270 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
1271 $ims = strtotime($if_modified_since);
1273 header('HTTP/1.1 304 Not Modified');
1274 // Better way to do this?
1281 * Is this action cacheable?
1283 * If the action returns a last-modified
1285 * @param array $argarray is ignored since it's now passed in in prepare()
1287 * @return boolean is read only action?
1289 function isCacheable()
1295 * Has etag? (private)
1297 * @param string $etag etag http header
1298 * @param string $if_none_match ifNoneMatch http header
1302 function _hasEtag($etag, $if_none_match)
1304 $etags = explode(',', $if_none_match);
1305 return in_array($etag, $etags) || in_array('*', $etags);
1309 * Boolean understands english (yes, no, true, false)
1311 * @param string $key query key we're interested in
1312 * @param string $def default value
1314 * @return boolean interprets yes/no strings as boolean
1316 function boolean($key, $def=false)
1318 $arg = strtolower($this->trimmed($key));
1320 if (is_null($arg)) {
1322 } else if (in_array($arg, array('true', 'yes', '1', 'on'))) {
1324 } else if (in_array($arg, array('false', 'no', '0'))) {
1332 * Integer value of an argument
1334 * @param string $key query key we're interested in
1335 * @param string $defValue optional default value (default null)
1336 * @param string $maxValue optional max value (default null)
1337 * @param string $minValue optional min value (default null)
1339 * @return integer integer value
1341 function int($key, $defValue=null, $maxValue=null, $minValue=null)
1343 $arg = strtolower($this->trimmed($key));
1345 if (is_null($arg) || !is_integer($arg)) {
1349 if (!is_null($maxValue)) {
1350 $arg = min($arg, $maxValue);
1353 if (!is_null($minValue)) {
1354 $arg = max($arg, $minValue);
1363 * @param string $msg error message to display
1364 * @param integer $code http error code, 500 by default
1368 function serverError($msg, $code=500, $format=null)
1370 if ($format === null) {
1371 $format = $this->format;
1374 common_debug("Server error '{$code}' on '{$this->action}': {$msg}", __FILE__);
1376 if (!array_key_exists($code, ServerErrorAction::$status)) {
1380 $status_string = ServerErrorAction::$status[$code];
1384 header("HTTP/1.1 {$code} {$status_string}");
1385 $this->initDocument('xml');
1386 $this->elementStart('hash');
1387 $this->element('error', null, $msg);
1388 $this->element('request', null, $_SERVER['REQUEST_URI']);
1389 $this->elementEnd('hash');
1390 $this->endDocument('xml');
1393 if (!isset($this->callback)) {
1394 header("HTTP/1.1 {$code} {$status_string}");
1396 $this->initDocument('json');
1397 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
1398 print(json_encode($error_array));
1399 $this->endDocument('json');
1402 throw new ServerException($msg, $code);
1411 * @param string $msg error message to display
1412 * @param integer $code http error code, 400 by default
1413 * @param string $format error format (json, xml, text) for ApiAction
1416 * @throws ClientException always
1418 function clientError($msg, $code=400, $format=null)
1420 // $format is currently only relevant for an ApiAction anyway
1421 if ($format === null) {
1422 $format = $this->format;
1425 common_debug("User error '{$code}' on '{$this->action}': {$msg}", __FILE__);
1427 if (!array_key_exists($code, ClientErrorAction::$status)) {
1431 $status_string = ClientErrorAction::$status[$code];
1435 header("HTTP/1.1 {$code} {$status_string}");
1436 $this->initDocument('xml');
1437 $this->elementStart('hash');
1438 $this->element('error', null, $msg);
1439 $this->element('request', null, $_SERVER['REQUEST_URI']);
1440 $this->elementEnd('hash');
1441 $this->endDocument('xml');
1444 if (!isset($this->callback)) {
1445 header("HTTP/1.1 {$code} {$status_string}");
1447 $this->initDocument('json');
1448 $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
1449 $this->text(json_encode($error_array));
1450 $this->endDocument('json');
1453 header("HTTP/1.1 {$code} {$status_string}");
1454 header('Content-Type: text/plain; charset=utf-8');
1458 throw new ClientException($msg, $code);
1464 * If not logged in, take appropriate action (redir or exception)
1466 * @param boolean $redir Redirect to login if not logged in
1468 * @return boolean true if logged in (never returns if not)
1470 public function checkLogin($redir=true)
1472 if (common_logged_in()) {
1477 common_set_returnto($_SERVER['REQUEST_URI']);
1478 common_redirect(common_local_url('login'));
1481 // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
1482 $this->clientError(_('Not logged in.'), 403);
1486 * Returns the current URL
1488 * @return string current URL
1492 list($action, $args) = $this->returnToArgs();
1493 return common_local_url($action, $args);
1497 * Returns arguments sufficient for re-constructing URL
1499 * @return array two elements: action, other args
1501 function returnToArgs()
1503 $action = $this->trimmed('action');
1504 $args = $this->args;
1505 unset($args['action']);
1506 if (common_config('site', 'fancy')) {
1509 if (array_key_exists('submit', $args)) {
1510 unset($args['submit']);
1512 foreach (array_keys($_COOKIE) as $cookie) {
1513 unset($args[$cookie]);
1515 return array($action, $args);
1519 * Generate a menu item
1521 * @param string $url menu URL
1522 * @param string $text menu name
1523 * @param string $title title attribute, null by default
1524 * @param boolean $is_selected current menu item, false by default
1525 * @param string $id element id, null by default
1529 function menuItem($url, $text, $title=null, $is_selected=false, $id=null, $class=null)
1531 // Added @id to li for some control.
1532 // XXX: We might want to move this to htmloutputter.php
1535 if ($class !== null) {
1536 $classes[] = trim($class);
1539 $classes[] = 'current';
1542 if (!empty($classes)) {
1543 $lattrs['class'] = implode(' ', $classes);
1546 if (!is_null($id)) {
1547 $lattrs['id'] = $id;
1550 $this->elementStart('li', $lattrs);
1551 $attrs['href'] = $url;
1553 $attrs['title'] = $title;
1555 $this->element('a', $attrs, $text);
1556 $this->elementEnd('li');
1560 * Generate pagination links
1562 * @param boolean $have_before is there something before?
1563 * @param boolean $have_after is there something after?
1564 * @param integer $page current page
1565 * @param string $action current action
1566 * @param array $args rest of query arguments
1570 // XXX: The messages in this pagination method only tailor to navigating
1571 // notices. In other lists, "Previous"/"Next" type navigation is
1572 // desirable, but not available.
1573 function pagination($have_before, $have_after, $page, $action, $args=null)
1575 // Does a little before-after block for next/prev page
1576 if ($have_before || $have_after) {
1577 $this->elementStart('ul', array('class' => 'nav',
1578 'id' => 'pagination'));
1581 $pargs = array('page' => $page-1);
1582 $this->elementStart('li', array('class' => 'nav_prev'));
1583 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1585 // TRANS: Pagination message to go to a page displaying information more in the
1586 // TRANS: present than the currently displayed information.
1588 $this->elementEnd('li');
1591 $pargs = array('page' => $page+1);
1592 $this->elementStart('li', array('class' => 'nav_next'));
1593 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1595 // TRANS: Pagination message to go to a page displaying information more in the
1596 // TRANS: past than the currently displayed information.
1598 $this->elementEnd('li');
1600 if ($have_before || $have_after) {
1601 $this->elementEnd('ul');
1606 * An array of feeds for this action.
1608 * Returns an array of potential feeds for this action.
1610 * @return array Feed object to show in head and links
1618 * Check the session token.
1620 * Checks that the current form has the correct session token,
1621 * and throw an exception if it does not.
1625 // XXX: Finding this type of check with the same message about 50 times.
1626 // Possible to refactor?
1627 function checkSessionToken()
1630 $token = $this->trimmed('token');
1631 if (empty($token) || $token != common_session_token()) {
1632 // TRANS: Client error text when there is a problem with the session token.
1633 $this->clientError(_('There was a problem with your session token.'));
1638 * Check if the current request is a POST
1640 * @return boolean true if POST; otherwise false.
1645 return ($_SERVER['REQUEST_METHOD'] == 'POST');