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');
123 if (Event::handle('StartHeadChildren', array($this))) {
125 $this->showShortcutIcon();
126 $this->showStylesheets();
127 $this->showScripts();
128 $this->showOpenSearch();
130 $this->showDescription();
132 Event::handle('EndHeadChildren', array($this));
134 $this->elementEnd('head');
138 * Show title, a template method.
144 $this->element('title', null,
145 sprintf(_("%s - %s"),
147 common_config('site', 'name')));
151 * Returns the page title
155 * @return string page title
160 return _("Untitled page");
164 * Show themed shortcut icon
168 function showShortcutIcon()
170 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/favicon.ico')) {
171 $this->element('link', array('rel' => 'shortcut icon',
172 'href' => theme_path('favicon.ico')));
174 $this->element('link', array('rel' => 'shortcut icon',
175 'href' => common_path('favicon.ico')));
178 if (common_config('site', 'mobile')) {
179 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/apple-touch-icon.png')) {
180 $this->element('link', array('rel' => 'apple-touch-icon',
181 'href' => theme_path('apple-touch-icon.png')));
183 $this->element('link', array('rel' => 'apple-touch-icon',
184 'href' => common_path('apple-touch-icon.png')));
194 function showStylesheets()
196 if (Event::handle('StartShowStyles', array($this))) {
198 if (Event::handle('StartShowLaconicaStyles', array($this))) {
199 $this->cssLink('css/display.css',null,'screen, projection, tv');
200 if (common_config('site', 'mobile')) {
201 // TODO: "handheld" CSS for other mobile devices
202 $this->cssLink('css/mobile.css','base','only screen and (max-device-width: 480px)'); // Mobile WebKit
204 $this->cssLink('css/print.css','base','print');
205 Event::handle('EndShowLaconicaStyles', array($this));
208 if (Event::handle('StartShowUAStyles', array($this))) {
209 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
210 'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
211 foreach (array(6,7) as $ver) {
212 if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
213 // Yes, IE people should be put in jail.
214 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
215 'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
218 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
219 'href="'.theme_path('css/ie.css', null).'?version='.LACONICA_VERSION.'" /><![endif]');
220 Event::handle('EndShowUAStyles', array($this));
223 if (Event::handle('StartShowDesign', array($this))) {
225 $user = common_current_user();
227 if (empty($user) || $user->viewdesigns) {
228 $design = $this->getDesign();
230 if (!empty($design)) {
231 $design->showCSS($this);
235 Event::handle('EndShowDesign', array($this));
237 Event::handle('EndShowStyles', array($this));
242 * Show javascript headers
246 function showScripts()
248 if (Event::handle('StartShowScripts', array($this))) {
249 if (Event::handle('StartShowJQueryScripts', array($this))) {
250 $this->script('js/jquery.min.js');
251 $this->script('js/jquery.form.js');
252 $this->script('js/jquery.joverlay.min.js');
253 Event::handle('EndShowJQueryScripts', array($this));
255 if (Event::handle('StartShowLaconicaScripts', array($this))) {
256 $this->script('js/xbImportNode.js');
257 $this->script('js/util.js');
258 // Frame-busting code to avoid clickjacking attacks.
259 $this->element('script', array('type' => 'text/javascript'),
260 'if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
261 Event::handle('EndShowLaconicaScripts', array($this));
263 Event::handle('EndShowScripts', array($this));
268 * Show OpenSearch headers
272 function showOpenSearch()
274 $this->element('link', array('rel' => 'search',
275 'type' => 'application/opensearchdescription+xml',
276 'href' => common_local_url('opensearch', array('type' => 'people')),
277 'title' => common_config('site', 'name').' People Search'));
278 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
279 'href' => common_local_url('opensearch', array('type' => 'notice')),
280 'title' => common_config('site', 'name').' Notice Search'));
293 $feeds = $this->getFeeds();
296 foreach ($feeds as $feed) {
297 $this->element('link', array('rel' => $feed->rel(),
298 'href' => $feed->url,
299 'type' => $feed->mimeType(),
300 'title' => $feed->title));
312 function showDescription()
314 // does nothing by default
318 * Show extra stuff in <head>.
326 // does nothing by default
332 * Calls template methods
338 $this->elementStart('body', (common_current_user()) ? array('id' => $this->trimmed('action'),
339 'class' => 'user_in')
340 : array('id' => $this->trimmed('action')));
341 $this->elementStart('div', array('id' => 'wrap'));
342 if (Event::handle('StartShowHeader', array($this))) {
344 Event::handle('EndShowHeader', array($this));
347 if (Event::handle('StartShowFooter', array($this))) {
349 Event::handle('EndShowFooter', array($this));
351 $this->elementEnd('div');
352 $this->elementEnd('body');
356 * Show header of the page.
358 * Calls template methods
362 function showHeader()
364 $this->elementStart('div', array('id' => 'header'));
366 $this->showPrimaryNav();
367 $this->showSiteNotice();
368 if (common_logged_in()) {
369 $this->showNoticeForm();
371 $this->showAnonymousMessage();
373 $this->elementEnd('div');
377 * Show configured logo.
383 $this->elementStart('address', array('id' => 'site_contact',
384 'class' => 'vcard'));
385 if (Event::handle('StartAddressData', array($this))) {
386 $this->elementStart('a', array('class' => 'url home bookmark',
387 'href' => common_local_url('public')));
388 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
389 $this->element('img', array('class' => 'logo photo',
390 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
391 'alt' => common_config('site', 'name')));
393 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
394 $this->elementEnd('a');
395 Event::handle('EndAddressData', array($this));
397 $this->elementEnd('address');
401 * Show primary navigation.
405 function showPrimaryNav()
407 $user = common_current_user();
409 if (common_config('xmpp', 'enabled')) {
410 $connect = 'imsettings';
411 } else if (common_config('sms', 'enabled')) {
412 $connect = 'smssettings';
413 } else if (common_config('twitter', 'enabled')) {
414 $connect = 'twittersettings';
417 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
418 $this->element('dt', null, _('Primary site navigation'));
419 $this->elementStart('dd');
420 $this->elementStart('ul', array('class' => 'nav'));
421 if (Event::handle('StartPrimaryNav', array($this))) {
423 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
424 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
425 $this->menuItem(common_local_url('profilesettings'),
426 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
428 $this->menuItem(common_local_url($connect),
429 _('Connect'), _('Connect to services'), false, 'nav_connect');
431 if (common_config('invite', 'enabled')) {
432 $this->menuItem(common_local_url('invite'),
434 sprintf(_('Invite friends and colleagues to join you on %s'),
435 common_config('site', 'name')),
436 false, 'nav_invitecontact');
438 $this->menuItem(common_local_url('logout'),
439 _('Logout'), _('Logout from the site'), false, 'nav_logout');
442 if (!common_config('site', 'openidonly')) {
443 if (!common_config('site', 'closed')) {
444 $this->menuItem(common_local_url('register'),
445 _('Register'), _('Create an account'), false, 'nav_register');
447 $this->menuItem(common_local_url('login'),
448 _('Login'), _('Login to the site'), false, 'nav_login');
450 $this->menuItem(common_local_url('openidlogin'),
451 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
454 $this->menuItem(common_local_url('doc', array('title' => 'help')),
455 _('Help'), _('Help me!'), false, 'nav_help');
456 if ($user || !common_config('site', 'private')) {
457 $this->menuItem(common_local_url('peoplesearch'),
458 _('Search'), _('Search for people or text'), false, 'nav_search');
460 Event::handle('EndPrimaryNav', array($this));
462 $this->elementEnd('ul');
463 $this->elementEnd('dd');
464 $this->elementEnd('dl');
472 function showSiteNotice()
474 // Revist. Should probably do an hAtom pattern here
475 $text = common_config('site', 'notice');
477 $this->elementStart('dl', array('id' => 'site_notice',
478 'class' => 'system_notice'));
479 $this->element('dt', null, _('Site notice'));
480 $this->elementStart('dd', null);
482 $this->elementEnd('dd');
483 $this->elementEnd('dl');
490 * MAY overload if no notice form needed... or direct message box????
494 function showNoticeForm()
496 $notice_form = new NoticeForm($this);
497 $notice_form->show();
501 * Show anonymous message.
507 function showAnonymousMessage()
509 // needs to be defined by the class
515 * Shows local navigation, content block and aside.
521 $this->elementStart('div', array('id' => 'core'));
522 if (Event::handle('StartShowLocalNavBlock', array($this))) {
523 $this->showLocalNavBlock();
524 Event::handle('EndShowLocalNavBlock', array($this));
526 if (Event::handle('StartShowContentBlock', array($this))) {
527 $this->showContentBlock();
528 Event::handle('EndShowContentBlock', array($this));
531 $this->elementEnd('div');
535 * Show local navigation block.
539 function showLocalNavBlock()
541 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
542 $this->element('dt', null, _('Local views'));
543 $this->elementStart('dd');
544 $this->showLocalNav();
545 $this->elementEnd('dd');
546 $this->elementEnd('dl');
550 * Show local navigation.
556 function showLocalNav()
558 // does nothing by default
562 * Show content block.
566 function showContentBlock()
568 $this->elementStart('div', array('id' => 'content'));
569 $this->showPageTitle();
570 $this->showPageNoticeBlock();
571 $this->elementStart('div', array('id' => 'content_inner'));
572 // show the actual content (forms, lists, whatever)
573 $this->showContent();
574 $this->elementEnd('div');
575 $this->elementEnd('div');
583 function showPageTitle()
585 $this->element('h1', null, $this->title());
589 * Show page notice block.
591 * Only show the block if a subclassed action has overrided
592 * Action::showPageNotice(), or an event handler is registered for
593 * the StartShowPageNotice event, in which case we assume the
594 * 'page_notice' definition list is desired. This is to prevent
595 * empty 'page_notice' definition lists from being output everywhere.
599 function showPageNoticeBlock()
601 $rmethod = new ReflectionMethod($this, 'showPageNotice');
602 $dclass = $rmethod->getDeclaringClass()->getName();
604 if ($dclass != 'Action' || Event::hasHandler('StartShowPageNotice')) {
606 $this->elementStart('dl', array('id' => 'page_notice',
607 'class' => 'system_notice'));
608 $this->element('dt', null, _('Page notice'));
609 $this->elementStart('dd');
610 if (Event::handle('StartShowPageNotice', array($this))) {
611 $this->showPageNotice();
612 Event::handle('EndShowPageNotice', array($this));
614 $this->elementEnd('dd');
615 $this->elementEnd('dl');
622 * SHOULD overload (unless there's not a notice)
626 function showPageNotice()
633 * MUST overload (unless there's not a notice)
637 function showContent()
649 $this->elementStart('div', array('id' => 'aside_primary',
650 'class' => 'aside'));
651 if (Event::handle('StartShowExportData', array($this))) {
652 $this->showExportData();
653 Event::handle('EndShowExportData', array($this));
655 if (Event::handle('StartShowSections', array($this))) {
656 $this->showSections();
657 Event::handle('EndShowSections', array($this));
659 $this->elementEnd('div');
663 * Show export data feeds.
668 function showExportData()
670 $feeds = $this->getFeeds();
672 $fl = new FeedList($this);
684 function showSections()
686 // for each section, show it
694 function showFooter()
696 $this->elementStart('div', array('id' => 'footer'));
697 $this->showSecondaryNav();
698 $this->showLicenses();
699 $this->elementEnd('div');
703 * Show secondary navigation.
707 function showSecondaryNav()
709 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
710 $this->element('dt', null, _('Secondary site navigation'));
711 $this->elementStart('dd', null);
712 $this->elementStart('ul', array('class' => 'nav'));
713 if (Event::handle('StartSecondaryNav', array($this))) {
714 $this->menuItem(common_local_url('doc', array('title' => 'help')),
716 $this->menuItem(common_local_url('doc', array('title' => 'about')),
718 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
720 $bb = common_config('site', 'broughtby');
722 $this->menuItem(common_local_url('doc', array('title' => 'tos')),
725 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
727 $this->menuItem(common_local_url('doc', array('title' => 'source')),
729 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
731 $this->menuItem(common_local_url('doc', array('title' => 'badge')),
733 Event::handle('EndSecondaryNav', array($this));
735 $this->elementEnd('ul');
736 $this->elementEnd('dd');
737 $this->elementEnd('dl');
745 function showLicenses()
747 $this->elementStart('dl', array('id' => 'licenses'));
748 $this->showLaconicaLicense();
749 $this->showContentLicense();
750 $this->elementEnd('dl');
754 * Show Laconica license.
758 function showLaconicaLicense()
760 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
761 $this->elementStart('dd', null);
762 if (common_config('site', 'broughtby')) {
763 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
765 $instr = _('**%%site.name%%** is a microblogging service. ');
767 $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);
768 $output = common_markup_to_html($instr);
770 $this->elementEnd('dd');
775 * Show content license.
779 function showContentLicense()
781 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
782 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
783 $this->elementStart('p');
784 $this->element('img', array('id' => 'license_cc',
785 'src' => common_config('license', 'image'),
786 'alt' => common_config('license', 'title'),
789 //TODO: This is dirty: i18n
790 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
791 $this->element('a', array('class' => 'license',
792 'rel' => 'external license',
793 'href' => common_config('license', 'url')),
794 common_config('license', 'title'));
795 $this->text(_('license.'));
796 $this->elementEnd('p');
797 $this->elementEnd('dd');
801 * Return last modified, if applicable.
805 * @return string last modified http header
807 function lastModified()
809 // For comparison with If-Last-Modified
810 // If not applicable, return null
815 * Return etag, if applicable.
819 * @return string etag http header
827 * Return true if read only.
831 * @param array $args other arguments
833 * @return boolean is read only action?
836 function isReadOnly($args)
842 * Returns query argument or default value if not found
844 * @param string $key requested argument
845 * @param string $def default value to return if $key is not provided
847 * @return boolean is read only action?
849 function arg($key, $def=null)
851 if (array_key_exists($key, $this->args)) {
852 return $this->args[$key];
859 * Returns trimmed query argument or default value if not found
861 * @param string $key requested argument
862 * @param string $def default value to return if $key is not provided
864 * @return boolean is read only action?
866 function trimmed($key, $def=null)
868 $arg = $this->arg($key, $def);
869 return is_string($arg) ? trim($arg) : $arg;
875 * @param array $argarray is ignored since it's now passed in in prepare()
877 * @return boolean is read only action?
879 function handle($argarray=null)
881 $lm = $this->lastModified();
882 $etag = $this->etag();
884 header('ETag: ' . $etag);
887 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
888 if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
889 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
890 $ims = strtotime($if_modified_since);
892 $if_none_match = (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) ?
893 $_SERVER['HTTP_IF_NONE_MATCH'] : null;
894 if (!$if_none_match ||
896 $this->_hasEtag($etag, $if_none_match)) {
897 header('HTTP/1.1 304 Not Modified');
898 // Better way to do this?
907 * HasĀ etag? (private)
909 * @param string $etag etag http header
910 * @param string $if_none_match ifNoneMatch http header
915 function _hasEtag($etag, $if_none_match)
917 $etags = explode(',', $if_none_match);
918 return in_array($etag, $etags) || in_array('*', $etags);
922 * Boolean understands english (yes, no, true, false)
924 * @param string $key query key we're interested in
925 * @param string $def default value
927 * @return boolean interprets yes/no strings as boolean
929 function boolean($key, $def=false)
931 $arg = strtolower($this->trimmed($key));
935 } else if (in_array($arg, array('true', 'yes', '1'))) {
937 } else if (in_array($arg, array('false', 'no', '0'))) {
947 * @param string $msg error message to display
948 * @param integer $code http error code, 500 by default
953 function serverError($msg, $code=500)
955 $action = $this->trimmed('action');
956 common_debug("Server error '$code' on '$action': $msg", __FILE__);
957 throw new ServerException($msg, $code);
963 * @param string $msg error message to display
964 * @param integer $code http error code, 400 by default
969 function clientError($msg, $code=400)
971 $action = $this->trimmed('action');
972 common_debug("User error '$code' on '$action': $msg", __FILE__);
973 throw new ClientException($msg, $code);
977 * Returns the current URL
979 * @return string current URL
984 $action = $this->trimmed('action');
986 unset($args['action']);
987 if (common_config('site', 'fancy')) {
990 if (array_key_exists('submit', $args)) {
991 unset($args['submit']);
993 foreach (array_keys($_COOKIE) as $cookie) {
994 unset($args[$cookie]);
997 return common_local_url($action, $args);
1001 * Generate a menu item
1003 * @param string $url menu URL
1004 * @param string $text menu name
1005 * @param string $title title attribute, null by default
1006 * @param boolean $is_selected current menu item, false by default
1007 * @param string $id element id, null by default
1011 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
1013 // Added @id to li for some control.
1014 // XXX: We might want to move this to htmloutputter.php
1017 $lattrs['class'] = 'current';
1020 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
1022 $this->elementStart('li', $lattrs);
1023 $attrs['href'] = $url;
1025 $attrs['title'] = $title;
1027 $this->element('a', $attrs, $text);
1028 $this->elementEnd('li');
1032 * Generate pagination links
1034 * @param boolean $have_before is there something before?
1035 * @param boolean $have_after is there something after?
1036 * @param integer $page current page
1037 * @param string $action current action
1038 * @param array $args rest of query arguments
1042 function pagination($have_before, $have_after, $page, $action, $args=null)
1044 // Does a little before-after block for next/prev page
1045 if ($have_before || $have_after) {
1046 $this->elementStart('div', array('class' => 'pagination'));
1047 $this->elementStart('dl', null);
1048 $this->element('dt', null, _('Pagination'));
1049 $this->elementStart('dd', null);
1050 $this->elementStart('ul', array('class' => 'nav'));
1053 $pargs = array('page' => $page-1);
1054 $this->elementStart('li', array('class' => 'nav_prev'));
1055 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1058 $this->elementEnd('li');
1061 $pargs = array('page' => $page+1);
1062 $this->elementStart('li', array('class' => 'nav_next'));
1063 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1066 $this->elementEnd('li');
1068 if ($have_before || $have_after) {
1069 $this->elementEnd('ul');
1070 $this->elementEnd('dd');
1071 $this->elementEnd('dl');
1072 $this->elementEnd('div');
1077 * An array of feeds for this action.
1079 * Returns an array of potential feeds for this action.
1081 * @return array Feed object to show in head and links
1090 * A design for this action
1092 * @return Design a design object to use
1095 function getDesign()
1097 return Design::siteDesign();