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
58 // This should be protected/private in the future
59 public $args = array();
61 // Action properties, set per-class
62 protected $action = false;
63 protected $ajax = false;
64 protected $menus = true;
65 protected $needLogin = false;
67 // The currently scoped profile
68 protected $scoped = null;
70 // Messages to the front-end user
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 $this->args = common_copy_args($args);
139 $this->action = $this->trimmed('action');
141 if ($this->ajax || $this->boolean('ajax')) {
142 // check with StatusNet::isAjax()
143 StatusNet::setAjax(true);
146 if ($this->needLogin) {
147 $this->checkLogin(); // if not logged in, this redirs/excepts
150 $this->scoped = Profile::current();
156 * Show page, a template method.
162 if (Event::handle('StartShowHTML', array($this))) {
165 Event::handle('EndShowHTML', array($this));
167 if (Event::handle('StartShowHead', array($this))) {
170 Event::handle('EndShowHead', array($this));
172 if (Event::handle('StartShowBody', array($this))) {
174 Event::handle('EndShowBody', array($this));
176 if (Event::handle('StartEndHTML', array($this))) {
178 Event::handle('EndEndHTML', array($this));
186 if (isset($_startTime)) {
187 $endTime = microtime(true);
188 $diff = round(($endTime - $_startTime) * 1000);
189 $this->raw("<!-- ${diff}ms -->");
192 return parent::endHTML();
196 * Show head, a template method.
202 // XXX: attributes (profile?)
203 $this->elementStart('head');
204 if (Event::handle('StartShowHeadElements', array($this))) {
205 if (Event::handle('StartShowHeadTitle', array($this))) {
207 Event::handle('EndShowHeadTitle', array($this));
209 $this->showShortcutIcon();
210 $this->showStylesheets();
211 $this->showOpenSearch();
213 $this->showDescription();
215 Event::handle('EndShowHeadElements', array($this));
217 $this->elementEnd('head');
221 * Show title, a template method.
227 $this->element('title', null,
228 // TRANS: Page title. %1$s is the title, %2$s is the site name.
229 sprintf(_('%1$s - %2$s'),
231 common_config('site', 'name')));
235 * Returns the page title
239 * @return string page title
244 // TRANS: Page title for a page without a title set.
245 return _('Untitled page');
249 * Show themed shortcut icon
253 function showShortcutIcon()
255 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/favicon.ico')) {
256 $this->element('link', array('rel' => 'shortcut icon',
257 'href' => Theme::path('favicon.ico')));
259 // favicon.ico should be HTTPS if the rest of the page is
260 $this->element('link', array('rel' => 'shortcut icon',
261 'href' => common_path('favicon.ico', StatusNet::isHTTPS())));
264 if (common_config('site', 'mobile')) {
265 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/apple-touch-icon.png')) {
266 $this->element('link', array('rel' => 'apple-touch-icon',
267 'href' => Theme::path('apple-touch-icon.png')));
269 $this->element('link', array('rel' => 'apple-touch-icon',
270 'href' => common_path('apple-touch-icon.png')));
280 function showStylesheets()
282 if (Event::handle('StartShowStyles', array($this))) {
284 // Use old name for StatusNet for compatibility on events
286 if (Event::handle('StartShowStatusNetStyles', array($this)) &&
287 Event::handle('StartShowLaconicaStyles', array($this))) {
288 $this->primaryCssLink(null, 'screen, projection, tv, print');
289 Event::handle('EndShowStatusNetStyles', array($this));
290 Event::handle('EndShowLaconicaStyles', array($this));
293 $this->cssLink(common_path('js/css/smoothness/jquery-ui.css', StatusNet::isHTTPS()));
295 if (Event::handle('StartShowUAStyles', array($this))) {
296 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
297 'href="'.Theme::path('css/ie.css', 'base').'?version='.STATUSNET_VERSION.'" /><![endif]');
298 foreach (array(6,7) as $ver) {
299 if (file_exists(Theme::file('css/ie'.$ver.'.css', 'base'))) {
300 // Yes, IE people should be put in jail.
301 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
302 'href="'.Theme::path('css/ie'.$ver.'.css', 'base').'?version='.STATUSNET_VERSION.'" /><![endif]');
305 if (file_exists(Theme::file('css/ie.css'))) {
306 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
307 'href="'.Theme::path('css/ie.css', null).'?version='.STATUSNET_VERSION.'" /><![endif]');
309 Event::handle('EndShowUAStyles', array($this));
312 Event::handle('EndShowStyles', array($this));
314 if (common_config('custom_css', 'enabled')) {
315 $css = common_config('custom_css', 'css');
316 if (Event::handle('StartShowCustomCss', array($this, &$css))) {
317 if (trim($css) != '') {
320 Event::handle('EndShowCustomCss', array($this));
326 function primaryCssLink($mainTheme=null, $media=null)
328 $theme = new Theme($mainTheme);
330 // Some themes may have external stylesheets, such as using the
331 // Google Font APIs to load webfonts.
332 foreach ($theme->getExternals() as $url) {
333 $this->cssLink($url, $mainTheme, $media);
336 // If the currently-selected theme has dependencies on other themes,
337 // we'll need to load their display.css files as well in order.
338 $baseThemes = $theme->getDeps();
339 foreach ($baseThemes as $baseTheme) {
340 $this->cssLink('css/display.css', $baseTheme, $media);
342 $this->cssLink('css/display.css', $mainTheme, $media);
344 // Additional styles for RTL languages
345 if (is_rtl(common_language())) {
346 if (file_exists(Theme::file('css/rtl.css'))) {
347 $this->cssLink('css/rtl.css', $mainTheme, $media);
353 * Show javascript headers
357 function showScripts()
359 if (Event::handle('StartShowScripts', array($this))) {
360 if (Event::handle('StartShowJQueryScripts', array($this))) {
361 if (common_config('site', 'minify')) {
362 $this->script('extlib/jquery.min.js');
363 $this->script('jquery.form.min.js');
364 $this->script('jquery-ui.min.js');
365 $this->script('jquery.cookie.min.js');
366 $this->inlineScript('if (typeof window.JSON !== "object") { $.getScript("'.common_path('js/extlib/json2.min.js', StatusNet::isHTTPS()).'"); }');
367 $this->script('jquery.joverlay.min.js');
368 $this->script('jquery.infieldlabel.min.js');
370 $this->script('extlib/jquery.js');
371 $this->script('jquery.form.js');
372 $this->script('jquery-ui.min.js');
373 $this->script('jquery.cookie.js');
374 $this->inlineScript('if (typeof window.JSON !== "object") { $.getScript("'.common_path('js/extlib/json2.js', StatusNet::isHTTPS()).'"); }');
375 $this->script('jquery.joverlay.js');
376 $this->script('jquery.infieldlabel.js');
379 Event::handle('EndShowJQueryScripts', array($this));
381 if (Event::handle('StartShowStatusNetScripts', array($this)) &&
382 Event::handle('StartShowLaconicaScripts', array($this))) {
383 if (common_config('site', 'minify')) {
384 $this->script('util.min.js');
386 $this->script('util.js');
387 $this->script('xbImportNode.js');
388 $this->script('geometa.js');
390 // This route isn't available in single-user mode.
391 // Not sure why, but it causes errors here.
392 $this->inlineScript('var _peopletagAC = "' .
393 common_local_url('peopletagautocomplete') . '";');
394 $this->showScriptMessages();
395 // Anti-framing code to avoid clickjacking attacks in older browsers.
396 // This will show a blank page if the page is being framed, which is
397 // consistent with the behavior of the 'X-Frame-Options: SAMEORIGIN'
398 // header, which prevents framing in newer browser.
399 if (common_config('javascript', 'bustframes')) {
400 $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 = ""; }; }');
402 Event::handle('EndShowStatusNetScripts', array($this));
403 Event::handle('EndShowLaconicaScripts', array($this));
405 Event::handle('EndShowScripts', array($this));
410 * Exports a map of localized text strings to JavaScript code.
412 * Plugins can add to what's exported by hooking the StartScriptMessages or EndScriptMessages
413 * events and appending to the array. Try to avoid adding strings that won't be used, as
414 * they'll be added to HTML output.
416 function showScriptMessages()
420 if (Event::handle('StartScriptMessages', array($this, &$messages))) {
421 // Common messages needed for timeline views etc...
423 // TRANS: Localized tooltip for '...' expansion button on overlong remote messages.
424 $messages['showmore_tooltip'] = _m('TOOLTIP', 'Show more');
426 // TRANS: Inline reply form submit button: submits a reply comment.
427 $messages['reply_submit'] = _m('BUTTON', 'Reply');
429 // TRANS: Placeholder text for inline reply form. Clicking in this box will turn it into a mini notice form.
430 $messages['reply_placeholder'] = _m('Write a reply...');
432 $messages = array_merge($messages, $this->getScriptMessages());
434 Event::handle('EndScriptMessages', array($this, &$messages));
437 if (!empty($messages)) {
438 $this->inlineScript('SN.messages=' . json_encode($messages));
445 * If the action will need localizable text strings, export them here like so:
447 * return array('pool_deepend' => _('Deep end'),
448 * 'pool_shallow' => _('Shallow end'));
450 * The exported map will be available via SN.msg() to JS code:
452 * $('#pool').html('<div class="deepend"></div><div class="shallow"></div>');
453 * $('#pool .deepend').text(SN.msg('pool_deepend'));
454 * $('#pool .shallow').text(SN.msg('pool_shallow'));
456 * Exports a map of localized text strings to JavaScript code.
458 * Plugins can add to what's exported on any action by hooking the StartScriptMessages or
459 * EndScriptMessages events and appending to the array. Try to avoid adding strings that won't
460 * be used, as they'll be added to HTML output.
462 function getScriptMessages()
468 * Show OpenSearch headers
472 function showOpenSearch()
474 $this->element('link', array('rel' => 'search',
475 'type' => 'application/opensearchdescription+xml',
476 'href' => common_local_url('opensearch', array('type' => 'people')),
477 'title' => common_config('site', 'name').' People Search'));
478 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
479 'href' => common_local_url('opensearch', array('type' => 'notice')),
480 'title' => common_config('site', 'name').' Notice Search'));
492 $feeds = $this->getFeeds();
495 foreach ($feeds as $feed) {
496 $this->element('link', array('rel' => $feed->rel(),
497 'href' => $feed->url,
498 'type' => $feed->mimeType(),
499 'title' => $feed->title));
511 function showDescription()
513 // does nothing by default
517 * Show extra stuff in <head>.
525 // does nothing by default
531 * Calls template methods
537 $this->elementStart('body', (common_current_user()) ? array('id' => strtolower($this->trimmed('action')),
538 'class' => 'user_in')
539 : array('id' => strtolower($this->trimmed('action'))));
540 $this->elementStart('div', array('id' => 'wrap'));
541 if (Event::handle('StartShowHeader', array($this))) {
544 Event::handle('EndShowHeader', array($this));
548 if (Event::handle('StartShowFooter', array($this))) {
551 Event::handle('EndShowFooter', array($this));
553 $this->elementEnd('div');
554 $this->showScripts();
555 $this->elementEnd('body');
559 * Show header of the page.
561 * Calls template methods
565 function showHeader()
567 $this->elementStart('div', array('id' => 'header'));
569 $this->showPrimaryNav();
570 if (Event::handle('StartShowSiteNotice', array($this))) {
571 $this->showSiteNotice();
573 Event::handle('EndShowSiteNotice', array($this));
576 $this->elementEnd('div');
580 * Show configured logo.
586 $this->elementStart('address', array('id' => 'site_contact',
587 'class' => 'vcard'));
588 if (Event::handle('StartAddressData', array($this))) {
589 if (common_config('singleuser', 'enabled')) {
590 $user = User::singleUser();
591 $url = common_local_url('showstream',
592 array('nickname' => $user->nickname));
593 } else if (common_logged_in()) {
594 $cur = common_current_user();
595 $url = common_local_url('all', array('nickname' => $cur->nickname));
597 $url = common_local_url('public');
600 $this->elementStart('a', array('class' => 'url home bookmark',
603 if (StatusNet::isHTTPS()) {
604 $logoUrl = common_config('site', 'ssllogo');
605 if (empty($logoUrl)) {
606 // if logo is an uploaded file, try to fall back to HTTPS file URL
607 $httpUrl = common_config('site', 'logo');
608 if (!empty($httpUrl)) {
609 $f = File::getKV('url', $httpUrl);
610 if (!empty($f) && !empty($f->filename)) {
611 // this will handle the HTTPS case
612 $logoUrl = File::url($f->filename);
617 $logoUrl = common_config('site', 'logo');
620 if (empty($logoUrl) && file_exists(Theme::file('logo.png'))) {
621 // This should handle the HTTPS case internally
622 $logoUrl = Theme::path('logo.png');
625 if (!empty($logoUrl)) {
626 $this->element('img', array('class' => 'logo photo',
628 'alt' => common_config('site', 'name')));
632 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
633 $this->elementEnd('a');
635 Event::handle('EndAddressData', array($this));
637 $this->elementEnd('address');
641 * Show primary navigation.
645 function showPrimaryNav()
647 $this->elementStart('div', array('id' => 'site_nav_global_primary'));
649 $user = common_current_user();
651 if (!empty($user) || !common_config('site', 'private')) {
652 $form = new SearchForm($this);
656 $pn = new PrimaryNav($this);
658 $this->elementEnd('div');
666 function showSiteNotice()
668 // Revist. Should probably do an hAtom pattern here
669 $text = common_config('site', 'notice');
671 $this->elementStart('div', array('id' => 'site_notice',
672 'class' => 'system_notice'));
674 $this->elementEnd('div');
681 * MAY overload if no notice form needed... or direct message box????
685 function showNoticeForm()
687 // TRANS: Tab on the notice form.
688 $tabs = array('status' => _m('TAB','Status'));
690 $this->elementStart('div', 'input_forms');
692 if (Event::handle('StartShowEntryForms', array(&$tabs))) {
693 $this->elementStart('ul', array('class' => 'nav',
694 'id' => 'input_form_nav'));
696 foreach ($tabs as $tag => $title) {
697 $attrs = array('id' => 'input_form_nav_'.$tag,
698 'class' => 'input_form_nav_tab');
700 if ($tag == 'status') {
701 // We're actually showing the placeholder form,
702 // but we special-case the 'Status' tab as if
703 // it were a small version of it.
704 $attrs['class'] .= ' current';
706 $this->elementStart('li', $attrs);
709 array('href' => 'javascript:SN.U.switchInputFormTab("'.$tag.'")'),
711 $this->elementEnd('li');
714 $this->elementEnd('ul');
716 $attrs = array('class' => 'input_form current',
717 'id' => 'input_form_placeholder');
718 $this->elementStart('div', $attrs);
719 $form = new NoticePlaceholderForm($this);
721 $this->elementEnd('div');
723 foreach ($tabs as $tag => $title) {
724 $attrs = array('class' => 'input_form',
725 'id' => 'input_form_'.$tag);
727 $this->elementStart('div', $attrs);
731 if (Event::handle('StartMakeEntryForm', array($tag, $this, &$form))) {
732 if ($tag == 'status') {
733 $options = $this->noticeFormOptions();
734 $form = new NoticeForm($this, $options);
736 Event::handle('EndMakeEntryForm', array($tag, $this, $form));
743 $this->elementEnd('div');
747 $this->elementEnd('div');
750 function noticeFormOptions()
756 * Show anonymous message.
762 function showAnonymousMessage()
764 // needs to be defined by the class
770 * Shows local navigation, content block and aside.
776 $this->elementStart('div', array('id' => 'core'));
777 $this->elementStart('div', array('id' => 'aside_primary_wrapper'));
778 $this->elementStart('div', array('id' => 'content_wrapper'));
779 $this->elementStart('div', array('id' => 'site_nav_local_views_wrapper'));
780 if (Event::handle('StartShowLocalNavBlock', array($this))) {
781 $this->showLocalNavBlock();
783 Event::handle('EndShowLocalNavBlock', array($this));
785 if (Event::handle('StartShowContentBlock', array($this))) {
786 $this->showContentBlock();
788 Event::handle('EndShowContentBlock', array($this));
790 if (Event::handle('StartShowAside', array($this))) {
793 Event::handle('EndShowAside', array($this));
795 $this->elementEnd('div');
796 $this->elementEnd('div');
797 $this->elementEnd('div');
798 $this->elementEnd('div');
802 * Show local navigation block.
806 function showLocalNavBlock()
808 // Need to have this ID for CSS; I'm too lazy to add it to
810 $this->elementStart('div', array('id' => 'site_nav_local_views'));
811 // Cheat cheat cheat!
812 $this->showLocalNav();
813 $this->elementEnd('div');
817 * If there's a logged-in user, show a bit of login context
821 function showProfileBlock()
823 if (common_logged_in()) {
824 $block = new DefaultProfileBlock($this);
830 * Show local navigation.
836 function showLocalNav()
838 $nav = new DefaultLocalNav($this);
843 * Show menu for an object (group, profile)
845 * This block will only show if a subclass has overridden
846 * the showObjectNav() method.
850 function showObjectNavBlock()
852 $rmethod = new ReflectionMethod($this, 'showObjectNav');
853 $dclass = $rmethod->getDeclaringClass()->getName();
855 if ($dclass != 'Action') {
856 // Need to have this ID for CSS; I'm too lazy to add it to
858 $this->elementStart('div', array('id' => 'site_nav_object',
859 'class' => 'section'));
860 $this->showObjectNav();
861 $this->elementEnd('div');
866 * Show object navigation.
868 * If there are things to do with this object, show it here.
872 function showObjectNav()
878 * Show content block.
882 function showContentBlock()
884 $this->elementStart('div', array('id' => 'content'));
885 if (common_logged_in()) {
886 if (Event::handle('StartShowNoticeForm', array($this))) {
887 $this->showNoticeForm();
888 Event::handle('EndShowNoticeForm', array($this));
891 if (Event::handle('StartShowPageTitle', array($this))) {
892 $this->showPageTitle();
893 Event::handle('EndShowPageTitle', array($this));
895 $this->showPageNoticeBlock();
896 $this->elementStart('div', array('id' => 'content_inner'));
897 // show the actual content (forms, lists, whatever)
898 $this->showContent();
899 $this->elementEnd('div');
900 $this->elementEnd('div');
908 function showPageTitle()
910 $this->element('h1', null, $this->title());
914 * Show page notice block.
916 * Only show the block if a subclassed action has overrided
917 * Action::showPageNotice(), or an event handler is registered for
918 * the StartShowPageNotice event, in which case we assume the
919 * 'page_notice' definition list is desired. This is to prevent
920 * empty 'page_notice' definition lists from being output everywhere.
924 function showPageNoticeBlock()
926 $rmethod = new ReflectionMethod($this, 'showPageNotice');
927 $dclass = $rmethod->getDeclaringClass()->getName();
929 if ($dclass != 'Action' || Event::hasHandler('StartShowPageNotice')) {
931 $this->elementStart('div', array('id' => 'page_notice',
932 'class' => 'system_notice'));
933 if (Event::handle('StartShowPageNotice', array($this))) {
934 $this->showPageNotice();
935 Event::handle('EndShowPageNotice', array($this));
937 $this->elementEnd('div');
944 * SHOULD overload (unless there's not a notice)
948 function showPageNotice()
955 * MUST overload (unless there's not a notice)
959 function showContent()
970 $this->elementStart('div', array('id' => 'aside_primary',
971 'class' => 'aside'));
972 $this->showProfileBlock();
973 if (Event::handle('StartShowObjectNavBlock', array($this))) {
974 $this->showObjectNavBlock();
975 Event::handle('EndShowObjectNavBlock', array($this));
977 if (Event::handle('StartShowSections', array($this))) {
978 $this->showSections();
979 Event::handle('EndShowSections', array($this));
981 if (Event::handle('StartShowExportData', array($this))) {
982 $this->showExportData();
983 Event::handle('EndShowExportData', array($this));
985 $this->elementEnd('div');
989 * Show export data feeds.
993 function showExportData()
995 $feeds = $this->getFeeds();
997 $fl = new FeedList($this);
1009 function showSections()
1011 // for each section, show it
1019 function showFooter()
1021 $this->elementStart('div', array('id' => 'footer'));
1022 if (Event::handle('StartShowInsideFooter', array($this))) {
1023 $this->showSecondaryNav();
1024 $this->showLicenses();
1025 Event::handle('EndShowInsideFooter', array($this));
1027 $this->elementEnd('div');
1031 * Show secondary navigation.
1035 function showSecondaryNav()
1037 $sn = new SecondaryNav($this);
1046 function showLicenses()
1048 $this->showStatusNetLicense();
1049 $this->showContentLicense();
1053 * Show StatusNet license.
1057 function showStatusNetLicense()
1059 if (common_config('site', 'broughtby')) {
1060 // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is set.
1061 // TRANS: Text between [] is a link description, text between () is the link itself.
1062 // TRANS: Make sure there is no whitespace between "]" and "(".
1063 // TRANS: "%%site.broughtby%%" is the value of the variable site.broughtby
1064 $instr = _('**%%site.name%%** is a social network, courtesy of [%%site.broughtby%%](%%site.broughtbyurl%%).');
1066 // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is not set.
1067 $instr = _('**%%site.name%%** is a social network.');
1070 // TRANS: Second sentence of the StatusNet site license. Mentions the StatusNet source code license.
1071 // TRANS: Make sure there is no whitespace between "]" and "(".
1072 // TRANS: Text between [] is a link description, text between () is the link itself.
1073 // TRANS: %s is the version of StatusNet that is being used.
1074 $instr .= sprintf(_('It runs on [GNU social](http://www.gnu.org/software/social/), version %s, available under the [GNU Affero General Public License](http://www.fsf.org/licensing/licenses/agpl-3.0.html).'), STATUSNET_VERSION);
1075 $output = common_markup_to_html($instr);
1076 $this->raw($output);
1081 * Show content license.
1085 function showContentLicense()
1087 if (Event::handle('StartShowContentLicense', array($this))) {
1088 switch (common_config('license', 'type')) {
1090 // TRANS: Content license displayed when license is set to 'private'.
1091 // TRANS: %1$s is the site name.
1092 $this->element('p', null, sprintf(_('Content and data of %1$s are private and confidential.'),
1093 common_config('site', 'name')));
1095 case 'allrightsreserved':
1096 if (common_config('license', 'owner')) {
1097 // TRANS: Content license displayed when license is set to 'allrightsreserved'.
1098 // TRANS: %1$s is the copyright owner.
1099 $this->element('p', null, sprintf(_('Content and data copyright by %1$s. All rights reserved.'),
1100 common_config('license', 'owner')));
1102 // TRANS: Content license displayed when license is set to 'allrightsreserved' and no owner is set.
1103 $this->element('p', null, _('Content and data copyright by contributors. All rights reserved.'));
1106 case 'cc': // fall through
1108 $this->elementStart('p');
1110 $image = common_config('license', 'image');
1111 $sslimage = common_config('license', 'sslimage');
1113 if (StatusNet::isHTTPS()) {
1114 if (!empty($sslimage)) {
1116 } else if (preg_match('#^http://i.creativecommons.org/#', $image)) {
1117 // CC support HTTPS on their images
1118 $url = preg_replace('/^http/', 'https', $image);
1120 // Better to show mixed content than no content
1127 $this->element('img', array('id' => 'license_cc',
1129 'alt' => common_config('license', 'title'),
1133 // TRANS: license message in footer.
1134 // TRANS: %1$s is the site name, %2$s is a link to the license URL, with a licence name set in configuration.
1135 $notice = _('All %1$s content and data are available under the %2$s license.');
1136 $link = "<a class=\"license\" rel=\"external license\" href=\"" .
1137 htmlspecialchars(common_config('license', 'url')) .
1139 htmlspecialchars(common_config('license', 'title')) .
1141 $this->raw(sprintf(htmlspecialchars($notice),
1142 htmlspecialchars(common_config('site', 'name')),
1144 $this->elementEnd('p');
1148 Event::handle('EndShowContentLicense', array($this));
1153 * Return last modified, if applicable.
1157 * @return string last modified http header
1159 function lastModified()
1161 // For comparison with If-Last-Modified
1162 // If not applicable, return null
1167 * Return etag, if applicable.
1171 * @return string etag http header
1179 * Return true if read only.
1183 * @param array $args other arguments
1185 * @return boolean is read only action?
1187 function isReadOnly($args)
1193 * Returns query argument or default value if not found
1195 * @param string $key requested argument
1196 * @param string $def default value to return if $key is not provided
1198 * @return boolean is read only action?
1200 function arg($key, $def=null)
1202 if (array_key_exists($key, $this->args)) {
1203 return $this->args[$key];
1210 * Returns trimmed query argument or default value if not found
1212 * @param string $key requested argument
1213 * @param string $def default value to return if $key is not provided
1215 * @return boolean is read only action?
1217 function trimmed($key, $def=null)
1219 $arg = $this->arg($key, $def);
1220 return is_string($arg) ? trim($arg) : $arg;
1226 * @return boolean is read only action?
1228 protected function handle()
1230 header('Vary: Accept-Encoding,Cookie');
1232 $lm = $this->lastModified();
1233 $etag = $this->etag();
1236 header('ETag: ' . $etag);
1240 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
1241 if ($this->isCacheable()) {
1242 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1243 header( "Cache-Control: private, must-revalidate, max-age=0" );
1250 $if_none_match = (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) ?
1251 $_SERVER['HTTP_IF_NONE_MATCH'] : null;
1252 if ($if_none_match) {
1253 // If this check fails, ignore the if-modified-since below.
1255 if ($this->_hasEtag($etag, $if_none_match)) {
1256 header('HTTP/1.1 304 Not Modified');
1257 // Better way to do this?
1263 if (!$checked && $lm && array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
1264 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
1265 $ims = strtotime($if_modified_since);
1267 header('HTTP/1.1 304 Not Modified');
1268 // Better way to do this?
1275 * Is this action cacheable?
1277 * If the action returns a last-modified
1279 * @param array $argarray is ignored since it's now passed in in prepare()
1281 * @return boolean is read only action?
1283 function isCacheable()
1289 * Has etag? (private)
1291 * @param string $etag etag http header
1292 * @param string $if_none_match ifNoneMatch http header
1296 function _hasEtag($etag, $if_none_match)
1298 $etags = explode(',', $if_none_match);
1299 return in_array($etag, $etags) || in_array('*', $etags);
1303 * Boolean understands english (yes, no, true, false)
1305 * @param string $key query key we're interested in
1306 * @param string $def default value
1308 * @return boolean interprets yes/no strings as boolean
1310 function boolean($key, $def=false)
1312 $arg = strtolower($this->trimmed($key));
1314 if (is_null($arg)) {
1316 } else if (in_array($arg, array('true', 'yes', '1', 'on'))) {
1318 } else if (in_array($arg, array('false', 'no', '0'))) {
1326 * Integer value of an argument
1328 * @param string $key query key we're interested in
1329 * @param string $defValue optional default value (default null)
1330 * @param string $maxValue optional max value (default null)
1331 * @param string $minValue optional min value (default null)
1333 * @return integer integer value
1335 function int($key, $defValue=null, $maxValue=null, $minValue=null)
1337 $arg = strtolower($this->trimmed($key));
1339 if (is_null($arg) || !is_integer($arg)) {
1343 if (!is_null($maxValue)) {
1344 $arg = min($arg, $maxValue);
1347 if (!is_null($minValue)) {
1348 $arg = max($arg, $minValue);
1357 * @param string $msg error message to display
1358 * @param integer $code http error code, 500 by default
1362 function serverError($msg, $code=500)
1364 $action = $this->trimmed('action');
1365 common_debug("Server error '$code' on '$action': $msg", __FILE__);
1366 throw new ServerException($msg, $code);
1372 * @param string $msg error message to display
1373 * @param integer $code http error code, 400 by default
1377 function clientError($msg, $code=400)
1379 $action = $this->trimmed('action');
1380 common_debug("User error '$code' on '$action': $msg", __FILE__);
1381 throw new ClientException($msg, $code);
1385 * If not logged in, take appropriate action (redir or exception)
1387 * @param boolean $redir Redirect to login if not logged in
1389 * @return boolean true if logged in (never returns if not)
1391 public function checkLogin($redir=true)
1393 if (common_logged_in()) {
1398 common_set_returnto($_SERVER['REQUEST_URI']);
1399 common_redirect(common_local_url('login'));
1402 // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
1403 $this->clientError(_('Not logged in.'), 403);
1407 * Returns the current URL
1409 * @return string current URL
1413 list($action, $args) = $this->returnToArgs();
1414 return common_local_url($action, $args);
1418 * Returns arguments sufficient for re-constructing URL
1420 * @return array two elements: action, other args
1422 function returnToArgs()
1424 $action = $this->trimmed('action');
1425 $args = $this->args;
1426 unset($args['action']);
1427 if (common_config('site', 'fancy')) {
1430 if (array_key_exists('submit', $args)) {
1431 unset($args['submit']);
1433 foreach (array_keys($_COOKIE) as $cookie) {
1434 unset($args[$cookie]);
1436 return array($action, $args);
1440 * Generate a menu item
1442 * @param string $url menu URL
1443 * @param string $text menu name
1444 * @param string $title title attribute, null by default
1445 * @param boolean $is_selected current menu item, false by default
1446 * @param string $id element id, null by default
1450 function menuItem($url, $text, $title=null, $is_selected=false, $id=null, $class=null)
1452 // Added @id to li for some control.
1453 // XXX: We might want to move this to htmloutputter.php
1456 if ($class !== null) {
1457 $classes[] = trim($class);
1460 $classes[] = 'current';
1463 if (!empty($classes)) {
1464 $lattrs['class'] = implode(' ', $classes);
1467 if (!is_null($id)) {
1468 $lattrs['id'] = $id;
1471 $this->elementStart('li', $lattrs);
1472 $attrs['href'] = $url;
1474 $attrs['title'] = $title;
1476 $this->element('a', $attrs, $text);
1477 $this->elementEnd('li');
1481 * Generate pagination links
1483 * @param boolean $have_before is there something before?
1484 * @param boolean $have_after is there something after?
1485 * @param integer $page current page
1486 * @param string $action current action
1487 * @param array $args rest of query arguments
1491 // XXX: The messages in this pagination method only tailor to navigating
1492 // notices. In other lists, "Previous"/"Next" type navigation is
1493 // desirable, but not available.
1494 function pagination($have_before, $have_after, $page, $action, $args=null)
1496 // Does a little before-after block for next/prev page
1497 if ($have_before || $have_after) {
1498 $this->elementStart('ul', array('class' => 'nav',
1499 'id' => 'pagination'));
1502 $pargs = array('page' => $page-1);
1503 $this->elementStart('li', array('class' => 'nav_prev'));
1504 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1506 // TRANS: Pagination message to go to a page displaying information more in the
1507 // TRANS: present than the currently displayed information.
1509 $this->elementEnd('li');
1512 $pargs = array('page' => $page+1);
1513 $this->elementStart('li', array('class' => 'nav_next'));
1514 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1516 // TRANS: Pagination message to go to a page displaying information more in the
1517 // TRANS: past than the currently displayed information.
1519 $this->elementEnd('li');
1521 if ($have_before || $have_after) {
1522 $this->elementEnd('ul');
1527 * An array of feeds for this action.
1529 * Returns an array of potential feeds for this action.
1531 * @return array Feed object to show in head and links
1539 * Check the session token.
1541 * Checks that the current form has the correct session token,
1542 * and throw an exception if it does not.
1546 // XXX: Finding this type of check with the same message about 50 times.
1547 // Possible to refactor?
1548 function checkSessionToken()
1551 $token = $this->trimmed('token');
1552 if (empty($token) || $token != common_session_token()) {
1553 // TRANS: Client error text when there is a problem with the session token.
1554 $this->clientError(_('There was a problem with your session token.'));
1559 * Check if the current request is a POST
1561 * @return boolean true if POST; otherwise false.
1566 return ($_SERVER['REQUEST_METHOD'] == 'POST');