3 * Laconica, 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@controlyourself.ca>
25 * @author Sarven Capadisli <csarven@controlyourself.ca>
26 * @copyright 2008 Control Yourself, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://laconi.ca/
31 if (!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@controlyourself.ca>
50 * @author Sarven Capadisli <csarven@controlyourself.ca>
51 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
52 * @link http://laconi.ca/
56 class Action extends HTMLOutputter // lawsuit
63 * Just wraps the HTMLOutputter constructor.
65 * @param string $output URI to output to, default = stdout
66 * @param boolean $indent Whether to indent output, default true
68 * @see XMLOutputter::__construct
69 * @see HTMLOutputter::__construct
71 function __construct($output='php://output', $indent=true)
73 parent::__construct($output, $indent);
77 * For initializing members of the class.
79 * @param array $argarray misc. arguments
81 * @return boolean true
83 function prepare($argarray)
85 $this->args =& common_copy_args($argarray);
90 * Show page, a template method.
96 if (Event::handle('StartShowHTML', array($this))) {
98 Event::handle('EndShowHTML', array($this));
100 if (Event::handle('StartShowHead', array($this))) {
102 Event::handle('EndShowHead', array($this));
104 if (Event::handle('StartShowBody', array($this))) {
106 Event::handle('EndShowBody', array($this));
108 if (Event::handle('StartEndHTML', array($this))) {
110 Event::handle('EndEndHTML', array($this));
115 * Show head, a template method.
121 // XXX: attributes (profile?)
122 $this->elementStart('head');
124 $this->showShortcutIcon();
125 $this->showStylesheets();
126 $this->showScripts();
127 $this->showOpenSearch();
129 $this->showDescription();
131 $this->elementEnd('head');
135 * Show title, a template method.
141 $this->element('title', null,
142 sprintf(_("%s - %s"),
144 common_config('site', 'name')));
148 * Returns the page title
152 * @return string page title
157 return _("Untitled page");
161 * Show themed shortcut icon
165 function showShortcutIcon()
167 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/favicon.ico')) {
168 $this->element('link', array('rel' => 'shortcut icon',
169 'href' => theme_path('favicon.ico')));
171 $this->element('link', array('rel' => 'shortcut icon',
172 'href' => common_path('favicon.ico')));
175 if (common_config('site', 'mobile')) {
176 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/apple-touch-icon.png')) {
177 $this->element('link', array('rel' => 'apple-touch-icon',
178 'href' => theme_path('apple-touch-icon.png')));
180 $this->element('link', array('rel' => 'apple-touch-icon',
181 'href' => common_path('apple-touch-icon.png')));
191 function showStylesheets()
193 if (Event::handle('StartShowStyles', array($this))) {
194 if (Event::handle('StartShowLaconicaStyles', array($this))) {
195 $this->element('link', array('rel' => 'stylesheet',
196 'type' => 'text/css',
197 'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
198 'media' => 'screen, projection, tv'));
199 if (common_config('site', 'mobile')) {
200 $this->element('link', array('rel' => 'stylesheet',
201 'type' => 'text/css',
202 'href' => theme_path('css/mobile.css', 'base') . '?version=' . LACONICA_VERSION,
203 // TODO: "handheld" CSS for other mobile devices
204 'media' => 'only screen and (max-device-width: 480px)')); // Mobile WebKit
206 $this->element('link', array('rel' => 'stylesheet',
207 'type' => 'text/css',
208 'href' => theme_path('css/print.css', 'base') . '?version=' . LACONICA_VERSION,
209 'media' => 'print'));
210 Event::handle('EndShowLaconicaStyles', array($this));
212 if (Event::handle('StartShowUAStyles', array($this))) {
213 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
214 'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
215 foreach (array(6,7) as $ver) {
216 if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
217 // Yes, IE people should be put in jail.
218 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
219 'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
222 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
223 'href="'.theme_path('css/ie.css', null).'?version='.LACONICA_VERSION.'" /><![endif]');
224 Event::handle('EndShowUAStyles', array($this));
226 Event::handle('EndShowStyles', array($this));
231 * Show javascript headers
235 function showScripts()
237 if (Event::handle('StartShowScripts', array($this))) {
238 if (Event::handle('StartShowJQueryScripts', array($this))) {
239 $this->element('script', array('type' => 'text/javascript',
240 'src' => common_path('js/jquery.min.js')),
242 $this->element('script', array('type' => 'text/javascript',
243 'src' => common_path('js/jquery.form.js')),
246 $this->element('script', array('type' => 'text/javascript',
247 'src' => common_path('js/jquery.joverlay.min.js')),
251 Event::handle('EndShowJQueryScripts', array($this));
253 if (Event::handle('StartShowLaconicaScripts', array($this))) {
254 $this->element('script', array('type' => 'text/javascript',
255 'src' => common_path('js/xbImportNode.js')),
257 $this->element('script', array('type' => 'text/javascript',
258 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
260 // Frame-busting code to avoid clickjacking attacks.
261 $this->element('script', array('type' => 'text/javascript'),
262 'if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
263 Event::handle('EndShowLaconicaScripts', array($this));
265 Event::handle('EndShowScripts', array($this));
270 * Show OpenSearch headers
274 function showOpenSearch()
276 $this->element('link', array('rel' => 'search',
277 'type' => 'application/opensearchdescription+xml',
278 'href' => common_local_url('opensearch', array('type' => 'people')),
279 'title' => common_config('site', 'name').' People Search'));
280 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
281 'href' => common_local_url('opensearch', array('type' => 'notice')),
282 'title' => common_config('site', 'name').' Notice Search'));
295 $feeds = $this->getFeeds();
298 foreach ($feeds as $feed) {
299 $this->element('link', array('rel' => $feed->rel(),
300 'href' => $feed->url,
301 'type' => $feed->mimeType(),
302 'title' => $feed->title));
314 function showDescription()
316 // does nothing by default
320 * Show extra stuff in <head>.
328 // does nothing by default
334 * Calls template methods
340 $this->elementStart('body', (common_current_user()) ? array('id' => $this->trimmed('action'),
341 'class' => 'user_in')
342 : array('id' => $this->trimmed('action')));
343 $this->elementStart('div', array('id' => 'wrap'));
344 if (Event::handle('StartShowHeader', array($this))) {
346 Event::handle('EndShowHeader', array($this));
349 if (Event::handle('StartShowFooter', array($this))) {
351 Event::handle('EndShowFooter', array($this));
353 $this->elementEnd('div');
354 $this->elementEnd('body');
358 * Show header of the page.
360 * Calls template methods
364 function showHeader()
366 $this->elementStart('div', array('id' => 'header'));
368 $this->showPrimaryNav();
369 $this->showSiteNotice();
370 if (common_logged_in()) {
371 $this->showNoticeForm();
373 $this->showAnonymousMessage();
375 $this->elementEnd('div');
379 * Show configured logo.
385 $this->elementStart('address', array('id' => 'site_contact',
386 'class' => 'vcard'));
387 $this->elementStart('a', array('class' => 'url home bookmark',
388 'href' => common_local_url('public')));
389 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
390 $this->element('img', array('class' => 'logo photo',
391 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
392 'alt' => common_config('site', 'name')));
394 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
395 $this->elementEnd('a');
396 $this->elementEnd('address');
400 * Show primary navigation.
404 function showPrimaryNav()
406 $user = common_current_user();
408 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
409 $this->element('dt', null, _('Primary site navigation'));
410 $this->elementStart('dd');
411 $this->elementStart('ul', array('class' => 'nav'));
412 if (Event::handle('StartPrimaryNav', array($this))) {
414 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
415 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
416 $this->menuItem(common_local_url('profilesettings'),
417 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
418 if (common_config('xmpp', 'enabled')) {
419 $this->menuItem(common_local_url('imsettings'),
420 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
422 $this->menuItem(common_local_url('smssettings'),
423 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
425 $this->menuItem(common_local_url('invite'),
427 sprintf(_('Invite friends and colleagues to join you on %s'),
428 common_config('site', 'name')),
429 false, 'nav_invitecontact');
430 $this->menuItem(common_local_url('logout'),
431 _('Logout'), _('Logout from the site'), false, 'nav_logout');
434 if (!common_config('site', 'closed')) {
435 $this->menuItem(common_local_url('register'),
436 _('Register'), _('Create an account'), false, 'nav_register');
438 $this->menuItem(common_local_url('openidlogin'),
439 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
440 $this->menuItem(common_local_url('login'),
441 _('Login'), _('Login to the site'), false, 'nav_login');
443 $this->menuItem(common_local_url('doc', array('title' => 'help')),
444 _('Help'), _('Help me!'), false, 'nav_help');
445 $this->menuItem(common_local_url('peoplesearch'),
446 _('Search'), _('Search for people or text'), false, 'nav_search');
447 Event::handle('EndPrimaryNav', array($this));
449 $this->elementEnd('ul');
450 $this->elementEnd('dd');
451 $this->elementEnd('dl');
459 function showSiteNotice()
461 // Revist. Should probably do an hAtom pattern here
462 $text = common_config('site', 'notice');
464 $this->elementStart('dl', array('id' => 'site_notice',
465 'class' => 'system_notice'));
466 $this->element('dt', null, _('Site notice'));
467 $this->elementStart('dd', null);
469 $this->elementEnd('dd');
470 $this->elementEnd('dl');
477 * MAY overload if no notice form needed... or direct message box????
481 function showNoticeForm()
483 $notice_form = new NoticeForm($this);
484 $notice_form->show();
488 * Show anonymous message.
494 function showAnonymousMessage()
496 // needs to be defined by the class
502 * Shows local navigation, content block and aside.
508 $this->elementStart('div', array('id' => 'core'));
509 if (Event::handle('StartShowLocalNavBlock', array($this))) {
510 $this->showLocalNavBlock();
511 Event::handle('EndShowLocalNavBlock', array($this));
513 if (Event::handle('StartShowContentBlock', array($this))) {
514 $this->showContentBlock();
515 Event::handle('EndShowContentBlock', array($this));
518 $this->elementEnd('div');
522 * Show local navigation block.
526 function showLocalNavBlock()
528 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
529 $this->element('dt', null, _('Local views'));
530 $this->elementStart('dd');
531 $this->showLocalNav();
532 $this->elementEnd('dd');
533 $this->elementEnd('dl');
537 * Show local navigation.
543 function showLocalNav()
545 // does nothing by default
549 * Show content block.
553 function showContentBlock()
555 $this->elementStart('div', array('id' => 'content'));
556 $this->showPageTitle();
557 $this->showPageNoticeBlock();
558 $this->elementStart('div', array('id' => 'content_inner'));
559 // show the actual content (forms, lists, whatever)
560 $this->showContent();
561 $this->elementEnd('div');
562 $this->elementEnd('div');
570 function showPageTitle()
572 $this->element('h1', null, $this->title());
576 * Show page notice block.
578 * Only show the block if a subclassed action has overrided
579 * Action::showPageNotice(), or an event handler is registered for
580 * the StartShowPageNotice event, in which case we assume the
581 * 'page_notice' definition list is desired. This is to prevent
582 * empty 'page_notice' definition lists from being output everywhere.
586 function showPageNoticeBlock()
588 $rmethod = new ReflectionMethod($this, 'showPageNotice');
589 $dclass = $rmethod->getDeclaringClass()->getName();
591 if ($dclass != 'Action' || Event::hasHandler('StartShowPageNotice')) {
593 $this->elementStart('dl', array('id' => 'page_notice',
594 'class' => 'system_notice'));
595 $this->element('dt', null, _('Page notice'));
596 $this->elementStart('dd');
597 if (Event::handle('StartShowPageNotice', array($this))) {
598 $this->showPageNotice();
599 Event::handle('EndShowPageNotice', array($this));
601 $this->elementEnd('dd');
602 $this->elementEnd('dl');
609 * SHOULD overload (unless there's not a notice)
613 function showPageNotice()
620 * MUST overload (unless there's not a notice)
624 function showContent()
636 $this->elementStart('div', array('id' => 'aside_primary',
637 'class' => 'aside'));
638 if (Event::handle('StartShowExportData', array($this))) {
639 $this->showExportData();
640 Event::handle('EndShowExportData', array($this));
642 if (Event::handle('StartShowSections', array($this))) {
643 $this->showSections();
644 Event::handle('EndShowSections', array($this));
646 $this->elementEnd('div');
650 * Show export data feeds.
655 function showExportData()
657 $feeds = $this->getFeeds();
659 $fl = new FeedList($this);
671 function showSections()
673 // for each section, show it
681 function showFooter()
683 $this->elementStart('div', array('id' => 'footer'));
684 $this->showSecondaryNav();
685 $this->showLicenses();
686 $this->elementEnd('div');
690 * Show secondary navigation.
694 function showSecondaryNav()
696 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
697 $this->element('dt', null, _('Secondary site navigation'));
698 $this->elementStart('dd', null);
699 $this->elementStart('ul', array('class' => 'nav'));
700 if (Event::handle('StartSecondaryNav', array($this))) {
701 $this->menuItem(common_local_url('doc', array('title' => 'help')),
703 $this->menuItem(common_local_url('doc', array('title' => 'about')),
705 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
707 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
709 $this->menuItem(common_local_url('doc', array('title' => 'source')),
711 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
713 $this->menuItem(common_local_url('doc', array('title' => 'badge')),
715 Event::handle('EndSecondaryNav', array($this));
717 $this->elementEnd('ul');
718 $this->elementEnd('dd');
719 $this->elementEnd('dl');
727 function showLicenses()
729 $this->elementStart('dl', array('id' => 'licenses'));
730 $this->showLaconicaLicense();
731 $this->showContentLicense();
732 $this->elementEnd('dl');
736 * Show Laconica license.
740 function showLaconicaLicense()
742 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
743 $this->elementStart('dd', null);
744 if (common_config('site', 'broughtby')) {
745 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
747 $instr = _('**%%site.name%%** is a microblogging service. ');
749 $instr .= sprintf(_('It runs the [Laconica](http://laconi.ca/) microblogging software, version %s, available under the [GNU Affero General Public License](http://www.fsf.org/licensing/licenses/agpl-3.0.html).'), LACONICA_VERSION);
750 $output = common_markup_to_html($instr);
752 $this->elementEnd('dd');
757 * Show content license.
761 function showContentLicense()
763 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
764 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
765 $this->elementStart('p');
766 $this->element('img', array('id' => 'license_cc',
767 'src' => common_config('license', 'image'),
768 'alt' => common_config('license', 'title')));
769 //TODO: This is dirty: i18n
770 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
771 $this->element('a', array('class' => 'license',
772 'rel' => 'external license',
773 'href' => common_config('license', 'url')),
774 common_config('license', 'title'));
775 $this->text(_('license.'));
776 $this->elementEnd('p');
777 $this->elementEnd('dd');
781 * Return last modified, if applicable.
785 * @return string last modified http header
787 function lastModified()
789 // For comparison with If-Last-Modified
790 // If not applicable, return null
795 * Return etag, if applicable.
799 * @return string etag http header
807 * Return true if read only.
811 * @param array $args other arguments
813 * @return boolean is read only action?
816 function isReadOnly($args)
822 * Returns query argument or default value if not found
824 * @param string $key requested argument
825 * @param string $def default value to return if $key is not provided
827 * @return boolean is read only action?
829 function arg($key, $def=null)
831 if (array_key_exists($key, $this->args)) {
832 return $this->args[$key];
839 * Returns trimmed query argument or default value if not found
841 * @param string $key requested argument
842 * @param string $def default value to return if $key is not provided
844 * @return boolean is read only action?
846 function trimmed($key, $def=null)
848 $arg = $this->arg($key, $def);
849 return is_string($arg) ? trim($arg) : $arg;
855 * @param array $argarray is ignored since it's now passed in in prepare()
857 * @return boolean is read only action?
859 function handle($argarray=null)
861 $lm = $this->lastModified();
862 $etag = $this->etag();
864 header('ETag: ' . $etag);
867 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
868 if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
869 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
870 $ims = strtotime($if_modified_since);
872 $if_none_match = (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) ?
873 $_SERVER['HTTP_IF_NONE_MATCH'] : null;
874 if (!$if_none_match ||
876 $this->_hasEtag($etag, $if_none_match)) {
877 header('HTTP/1.1 304 Not Modified');
878 // Better way to do this?
887 * HasĀ etag? (private)
889 * @param string $etag etag http header
890 * @param string $if_none_match ifNoneMatch http header
895 function _hasEtag($etag, $if_none_match)
897 $etags = explode(',', $if_none_match);
898 return in_array($etag, $etags) || in_array('*', $etags);
902 * Boolean understands english (yes, no, true, false)
904 * @param string $key query key we're interested in
905 * @param string $def default value
907 * @return boolean interprets yes/no strings as boolean
909 function boolean($key, $def=false)
911 $arg = strtolower($this->trimmed($key));
915 } else if (in_array($arg, array('true', 'yes', '1'))) {
917 } else if (in_array($arg, array('false', 'no', '0'))) {
927 * @param string $msg error message to display
928 * @param integer $code http error code, 500 by default
933 function serverError($msg, $code=500)
935 $action = $this->trimmed('action');
936 common_debug("Server error '$code' on '$action': $msg", __FILE__);
937 throw new ServerException($msg, $code);
943 * @param string $msg error message to display
944 * @param integer $code http error code, 400 by default
949 function clientError($msg, $code=400)
951 $action = $this->trimmed('action');
952 common_debug("User error '$code' on '$action': $msg", __FILE__);
953 throw new ClientException($msg, $code);
957 * Returns the current URL
959 * @return string current URL
964 $action = $this->trimmed('action');
966 unset($args['action']);
967 if (array_key_exists('submit', $args)) {
968 unset($args['submit']);
970 foreach (array_keys($_COOKIE) as $cookie) {
971 unset($args[$cookie]);
973 return common_local_url($action, $args);
977 * Generate a menu item
979 * @param string $url menu URL
980 * @param string $text menu name
981 * @param string $title title attribute, null by default
982 * @param boolean $is_selected current menu item, false by default
983 * @param string $id element id, null by default
987 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
989 // Added @id to li for some control.
990 // XXX: We might want to move this to htmloutputter.php
993 $lattrs['class'] = 'current';
996 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
998 $this->elementStart('li', $lattrs);
999 $attrs['href'] = $url;
1001 $attrs['title'] = $title;
1003 $this->element('a', $attrs, $text);
1004 $this->elementEnd('li');
1008 * Generate pagination links
1010 * @param boolean $have_before is there something before?
1011 * @param boolean $have_after is there something after?
1012 * @param integer $page current page
1013 * @param string $action current action
1014 * @param array $args rest of query arguments
1018 function pagination($have_before, $have_after, $page, $action, $args=null)
1020 // Does a little before-after block for next/prev page
1021 if ($have_before || $have_after) {
1022 $this->elementStart('div', array('class' => 'pagination'));
1023 $this->elementStart('dl', null);
1024 $this->element('dt', null, _('Pagination'));
1025 $this->elementStart('dd', null);
1026 $this->elementStart('ul', array('class' => 'nav'));
1029 $pargs = array('page' => $page-1);
1030 $this->elementStart('li', array('class' => 'nav_prev'));
1031 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1034 $this->elementEnd('li');
1037 $pargs = array('page' => $page+1);
1038 $this->elementStart('li', array('class' => 'nav_next'));
1039 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1042 $this->elementEnd('li');
1044 if ($have_before || $have_after) {
1045 $this->elementEnd('ul');
1046 $this->elementEnd('dd');
1047 $this->elementEnd('dl');
1048 $this->elementEnd('div');
1053 * An array of feeds for this action.
1055 * Returns an array of potential feeds for this action.
1057 * @return array Feed object to show in head and links