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->showRelationshipLinks();
128 $this->showOpenSearch();
130 $this->showDescription();
132 $this->elementEnd('head');
136 * Show title, a template method.
142 $this->element('title', null,
143 sprintf(_("%s - %s"),
145 common_config('site', 'name')));
149 * Returns the page title
153 * @return string page title
158 return _("Untitled page");
162 * Show themed shortcut icon
166 function showShortcutIcon()
168 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/favicon.ico')) {
169 $this->element('link', array('rel' => 'shortcut icon',
170 'href' => theme_path('favicon.ico')));
172 $this->element('link', array('rel' => 'shortcut icon',
173 'href' => common_path('favicon.ico')));
176 if (common_config('site', 'mobile')) {
177 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/apple-touch-icon.png')) {
178 $this->element('link', array('rel' => 'apple-touch-icon',
179 'href' => theme_path('apple-touch-icon.png')));
181 $this->element('link', array('rel' => 'apple-touch-icon',
182 'href' => common_path('apple-touch-icon.png')));
192 function showStylesheets()
194 if (Event::handle('StartShowStyles', array($this))) {
195 if (Event::handle('StartShowLaconicaStyles', array($this))) {
196 $this->element('link', array('rel' => 'stylesheet',
197 'type' => 'text/css',
198 'href' => theme_path('css/display.css', 'base') . '?version=' . LACONICA_VERSION,
199 'media' => 'screen, projection, tv'));
200 $this->element('link', array('rel' => 'stylesheet',
201 'type' => 'text/css',
202 'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
203 'media' => 'screen, projection, tv'));
204 if (common_config('site', 'mobile')) {
205 $this->element('link', array('rel' => 'stylesheet',
206 'type' => 'text/css',
207 'href' => theme_path('css/mobile.css', 'base') . '?version=' . LACONICA_VERSION,
208 // TODO: "handheld" CSS for other mobile devices
209 'media' => 'only screen and (max-device-width: 480px)')); // Mobile WebKit
211 $this->element('link', array('rel' => 'stylesheet',
212 'type' => 'text/css',
213 'href' => theme_path('css/print.css', 'base') . '?version=' . LACONICA_VERSION,
214 'media' => 'print'));
215 Event::handle('EndShowLaconicaStyles', array($this));
217 if (Event::handle('StartShowUAStyles', array($this))) {
218 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
219 'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
220 foreach (array(6,7) as $ver) {
221 if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
222 // Yes, IE people should be put in jail.
223 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
224 'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
227 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
228 'href="'.theme_path('css/ie.css', null).'?version='.LACONICA_VERSION.'" /><![endif]');
229 Event::handle('EndShowUAStyles', array($this));
231 Event::handle('EndShowStyles', array($this));
236 * Show javascript headers
240 function showScripts()
242 if (Event::handle('StartShowScripts', array($this))) {
243 if (Event::handle('StartShowJQueryScripts', array($this))) {
244 $this->element('script', array('type' => 'text/javascript',
245 'src' => common_path('js/jquery.min.js')),
247 $this->element('script', array('type' => 'text/javascript',
248 'src' => common_path('js/jquery.form.js')),
250 Event::handle('EndShowJQueryScripts', array($this));
252 if (Event::handle('StartShowLaconicaScripts', array($this))) {
253 $this->element('script', array('type' => 'text/javascript',
254 'src' => common_path('js/xbImportNode.js')),
256 $this->element('script', array('type' => 'text/javascript',
257 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
259 // Frame-busting code to avoid clickjacking attacks.
260 $this->element('script', array('type' => 'text/javascript'),
261 'if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
262 Event::handle('EndShowLaconicaScripts', array($this));
264 Event::handle('EndShowScripts', array($this));
269 * Show document relationship links
275 function showRelationshipLinks()
277 // output <link> elements with appropriate HTML4.01 link types:
278 // http://www.w3.org/TR/html401/types.html#type-links
282 * Show OpenSearch headers
286 function showOpenSearch()
288 $this->element('link', array('rel' => 'search',
289 'type' => 'application/opensearchdescription+xml',
290 'href' => common_local_url('opensearch', array('type' => 'people')),
291 'title' => common_config('site', 'name').' People Search'));
292 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
293 'href' => common_local_url('opensearch', array('type' => 'notice')),
294 'title' => common_config('site', 'name').' Notice Search'));
307 $feeds = $this->getFeeds();
310 foreach ($feeds as $feed) {
311 $this->element('link', array('rel' => $feed->rel(),
312 'href' => $feed->url,
313 'type' => $feed->mimeType(),
314 'title' => $feed->title));
326 function showDescription()
328 // does nothing by default
332 * Show extra stuff in <head>.
340 // does nothing by default
346 * Calls template methods
352 $this->elementStart('body', (common_current_user()) ? array('id' => $this->trimmed('action'),
353 'class' => 'user_in')
354 : array('id' => $this->trimmed('action')));
355 $this->elementStart('div', array('id' => 'wrap'));
356 if (Event::handle('StartShowHeader', array($this))) {
358 Event::handle('EndShowHeader', array($this));
361 if (Event::handle('StartShowFooter', array($this))) {
363 Event::handle('EndShowFooter', array($this));
365 $this->elementEnd('div');
366 $this->elementEnd('body');
370 * Show header of the page.
372 * Calls template methods
376 function showHeader()
378 $this->elementStart('div', array('id' => 'header'));
380 $this->showPrimaryNav();
381 $this->showSiteNotice();
382 if (common_logged_in()) {
383 $this->showNoticeForm();
385 $this->showAnonymousMessage();
387 $this->elementEnd('div');
391 * Show configured logo.
397 $this->elementStart('address', array('id' => 'site_contact',
398 'class' => 'vcard'));
399 $this->elementStart('a', array('class' => 'url home bookmark',
400 'href' => common_local_url('public')));
401 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
402 $this->element('img', array('class' => 'logo photo',
403 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
404 'alt' => common_config('site', 'name')));
406 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
407 $this->elementEnd('a');
408 $this->elementEnd('address');
412 * Show primary navigation.
416 function showPrimaryNav()
418 $user = common_current_user();
420 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
421 $this->element('dt', null, _('Primary site navigation'));
422 $this->elementStart('dd');
423 $this->elementStart('ul', array('class' => 'nav'));
424 if (Event::handle('StartPrimaryNav', array($this))) {
426 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
427 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
428 $this->menuItem(common_local_url('profilesettings'),
429 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
430 if (common_config('xmpp', 'enabled')) {
431 $this->menuItem(common_local_url('imsettings'),
432 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
434 $this->menuItem(common_local_url('smssettings'),
435 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
437 $this->menuItem(common_local_url('invite'),
439 sprintf(_('Invite friends and colleagues to join you on %s'),
440 common_config('site', 'name')),
441 false, 'nav_invitecontact');
442 $this->menuItem(common_local_url('logout'),
443 _('Logout'), _('Logout from the site'), false, 'nav_logout');
446 if (!common_config('site', 'closed')) {
447 $this->menuItem(common_local_url('register'),
448 _('Register'), _('Create an account'), false, 'nav_register');
450 $this->menuItem(common_local_url('openidlogin'),
451 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
452 $this->menuItem(common_local_url('login'),
453 _('Login'), _('Login to the site'), false, 'nav_login');
455 $this->menuItem(common_local_url('doc', array('title' => 'help')),
456 _('Help'), _('Help me!'), false, 'nav_help');
457 $this->menuItem(common_local_url('peoplesearch'),
458 _('Search'), _('Search for people or text'), false, 'nav_search');
459 Event::handle('EndPrimaryNav', array($this));
461 $this->elementEnd('ul');
462 $this->elementEnd('dd');
463 $this->elementEnd('dl');
471 function showSiteNotice()
473 // Revist. Should probably do an hAtom pattern here
474 $text = common_config('site', 'notice');
476 $this->elementStart('dl', array('id' => 'site_notice',
477 'class' => 'system_notice'));
478 $this->element('dt', null, _('Site notice'));
479 $this->elementStart('dd', null);
481 $this->elementEnd('dd');
482 $this->elementEnd('dl');
489 * MAY overload if no notice form needed... or direct message box????
493 function showNoticeForm()
495 $notice_form = new NoticeForm($this);
496 $notice_form->show();
500 * Show anonymous message.
506 function showAnonymousMessage()
508 // needs to be defined by the class
514 * Shows local navigation, content block and aside.
520 $this->elementStart('div', array('id' => 'core'));
521 if (Event::handle('StartShowLocalNavBlock', array($this))) {
522 $this->showLocalNavBlock();
523 Event::handle('EndShowLocalNavBlock', array($this));
525 if (Event::handle('StartShowContentBlock', array($this))) {
526 $this->showContentBlock();
527 Event::handle('EndShowContentBlock', array($this));
530 $this->elementEnd('div');
534 * Show local navigation block.
538 function showLocalNavBlock()
540 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
541 $this->element('dt', null, _('Local views'));
542 $this->elementStart('dd');
543 $this->showLocalNav();
544 $this->elementEnd('dd');
545 $this->elementEnd('dl');
549 * Show local navigation.
555 function showLocalNav()
557 // does nothing by default
561 * Show content block.
565 function showContentBlock()
567 $this->elementStart('div', array('id' => 'content'));
568 $this->showPageTitle();
569 $this->showPageNoticeBlock();
570 $this->elementStart('div', array('id' => 'content_inner'));
571 // show the actual content (forms, lists, whatever)
572 $this->showContent();
573 $this->elementEnd('div');
574 $this->elementEnd('div');
582 function showPageTitle()
584 $this->element('h1', null, $this->title());
588 * Show page notice block.
592 function showPageNoticeBlock()
594 $this->elementStart('dl', array('id' => 'page_notice',
595 'class' => 'system_notice'));
596 $this->element('dt', null, _('Page notice'));
597 $this->elementStart('dd');
598 $this->showPageNotice();
599 $this->elementEnd('dd');
600 $this->elementEnd('dl');
606 * SHOULD overload (unless there's not a notice)
610 function showPageNotice()
617 * MUST overload (unless there's not a notice)
621 function showContent()
633 $this->elementStart('div', array('id' => 'aside_primary',
634 'class' => 'aside'));
635 if (Event::handle('StartShowExportData', array($this))) {
636 $this->showExportData();
637 Event::handle('EndShowExportData', array($this));
639 if (Event::handle('StartShowSections', array($this))) {
640 $this->showSections();
641 Event::handle('EndShowSections', array($this));
643 $this->elementEnd('div');
647 * Show export data feeds.
652 function showExportData()
654 $feeds = $this->getFeeds();
656 $fl = new FeedList($this);
668 function showSections()
670 // for each section, show it
678 function showFooter()
680 $this->elementStart('div', array('id' => 'footer'));
681 $this->showSecondaryNav();
682 $this->showLicenses();
683 $this->elementEnd('div');
687 * Show secondary navigation.
691 function showSecondaryNav()
693 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
694 $this->element('dt', null, _('Secondary site navigation'));
695 $this->elementStart('dd', null);
696 $this->elementStart('ul', array('class' => 'nav'));
697 if (Event::handle('StartSecondaryNav', array($this))) {
698 $this->menuItem(common_local_url('doc', array('title' => 'help')),
700 $this->menuItem(common_local_url('doc', array('title' => 'about')),
702 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
704 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
706 $this->menuItem(common_local_url('doc', array('title' => 'source')),
708 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
710 $this->menuItem(common_local_url('doc', array('title' => 'badge')),
712 Event::handle('EndSecondaryNav', array($this));
714 $this->elementEnd('ul');
715 $this->elementEnd('dd');
716 $this->elementEnd('dl');
724 function showLicenses()
726 $this->elementStart('dl', array('id' => 'licenses'));
727 $this->showLaconicaLicense();
728 $this->showContentLicense();
729 $this->elementEnd('dl');
733 * Show Laconica license.
737 function showLaconicaLicense()
739 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
740 $this->elementStart('dd', null);
741 if (common_config('site', 'broughtby')) {
742 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
744 $instr = _('**%%site.name%%** is a microblogging service. ');
746 $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);
747 $output = common_markup_to_html($instr);
749 $this->elementEnd('dd');
754 * Show content license.
758 function showContentLicense()
760 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
761 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
762 $this->elementStart('p');
763 $this->element('img', array('id' => 'license_cc',
764 'src' => common_config('license', 'image'),
765 'alt' => common_config('license', 'title')));
766 //TODO: This is dirty: i18n
767 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
768 $this->element('a', array('class' => 'license',
769 'rel' => 'external license',
770 'href' => common_config('license', 'url')),
771 common_config('license', 'title'));
772 $this->text(_('license.'));
773 $this->elementEnd('p');
774 $this->elementEnd('dd');
778 * Return last modified, if applicable.
782 * @return string last modified http header
784 function lastModified()
786 // For comparison with If-Last-Modified
787 // If not applicable, return null
792 * Return etag, if applicable.
796 * @return string etag http header
804 * Return true if read only.
808 * @param array $args other arguments
810 * @return boolean is read only action?
813 function isReadOnly($args)
819 * Returns query argument or default value if not found
821 * @param string $key requested argument
822 * @param string $def default value to return if $key is not provided
824 * @return boolean is read only action?
826 function arg($key, $def=null)
828 if (array_key_exists($key, $this->args)) {
829 return $this->args[$key];
836 * Returns trimmed query argument or default value if not found
838 * @param string $key requested argument
839 * @param string $def default value to return if $key is not provided
841 * @return boolean is read only action?
843 function trimmed($key, $def=null)
845 $arg = $this->arg($key, $def);
846 return is_string($arg) ? trim($arg) : $arg;
852 * @param array $argarray is ignored since it's now passed in in prepare()
854 * @return boolean is read only action?
856 function handle($argarray=null)
858 $lm = $this->lastModified();
859 $etag = $this->etag();
861 header('ETag: ' . $etag);
864 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
865 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
866 $ims = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
868 $if_none_match = $_SERVER['HTTP_IF_NONE_MATCH'];
869 if (!$if_none_match ||
871 $this->_hasEtag($etag, $if_none_match)) {
872 header('HTTP/1.1 304 Not Modified');
873 // Better way to do this?
882 * HasĀ etag? (private)
884 * @param string $etag etag http header
885 * @param string $if_none_match ifNoneMatch http header
890 function _hasEtag($etag, $if_none_match)
892 $etags = explode(',', $if_none_match);
893 return in_array($etag, $etags) || in_array('*', $etags);
897 * Boolean understands english (yes, no, true, false)
899 * @param string $key query key we're interested in
900 * @param string $def default value
902 * @return boolean interprets yes/no strings as boolean
904 function boolean($key, $def=false)
906 $arg = strtolower($this->trimmed($key));
910 } else if (in_array($arg, array('true', 'yes', '1'))) {
912 } else if (in_array($arg, array('false', 'no', '0'))) {
922 * @param string $msg error message to display
923 * @param integer $code http error code, 500 by default
928 function serverError($msg, $code=500)
930 $action = $this->trimmed('action');
931 common_debug("Server error '$code' on '$action': $msg", __FILE__);
932 throw new ServerException($msg, $code);
938 * @param string $msg error message to display
939 * @param integer $code http error code, 400 by default
944 function clientError($msg, $code=400)
946 $action = $this->trimmed('action');
947 common_debug("User error '$code' on '$action': $msg", __FILE__);
948 throw new ClientException($msg, $code);
952 * Returns the current URL
954 * @return string current URL
959 $action = $this->trimmed('action');
961 unset($args['action']);
962 if (array_key_exists('submit', $args)) {
963 unset($args['submit']);
965 foreach (array_keys($_COOKIE) as $cookie) {
966 unset($args[$cookie]);
968 return common_local_url($action, $args);
972 * Generate a menu item
974 * @param string $url menu URL
975 * @param string $text menu name
976 * @param string $title title attribute, null by default
977 * @param boolean $is_selected current menu item, false by default
978 * @param string $id element id, null by default
982 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
984 // Added @id to li for some control.
985 // XXX: We might want to move this to htmloutputter.php
988 $lattrs['class'] = 'current';
991 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
993 $this->elementStart('li', $lattrs);
994 $attrs['href'] = $url;
996 $attrs['title'] = $title;
998 $this->element('a', $attrs, $text);
999 $this->elementEnd('li');
1003 * Generate pagination links
1005 * @param boolean $have_before is there something before?
1006 * @param boolean $have_after is there something after?
1007 * @param integer $page current page
1008 * @param string $action current action
1009 * @param array $args rest of query arguments
1013 function pagination($have_before, $have_after, $page, $action, $args=null)
1015 // Does a little before-after block for next/prev page
1016 if ($have_before || $have_after) {
1017 $this->elementStart('div', array('class' => 'pagination'));
1018 $this->elementStart('dl', null);
1019 $this->element('dt', null, _('Pagination'));
1020 $this->elementStart('dd', null);
1021 $this->elementStart('ul', array('class' => 'nav'));
1024 $pargs = array('page' => $page-1);
1025 $this->elementStart('li', array('class' => 'nav_prev'));
1026 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1029 $this->elementEnd('li');
1032 $pargs = array('page' => $page+1);
1033 $this->elementStart('li', array('class' => 'nav_next'));
1034 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1037 $this->elementEnd('li');
1039 if ($have_before || $have_after) {
1040 $this->elementEnd('ul');
1041 $this->elementEnd('dd');
1042 $this->elementEnd('dl');
1043 $this->elementEnd('div');
1048 * An array of feeds for this action.
1050 * Returns an array of potential feeds for this action.
1052 * @return array Feed object to show in head and links
1061 * Generate document metadata for sequential navigation
1063 * @param boolean $have_before is there something before?
1064 * @param boolean $have_after is there something after?
1065 * @param integer $page current page
1066 * @param string $action current action
1067 * @param array $args rest of query arguments
1071 function sequenceRelationships($have_next, $have_previous, $page, $action, $args=null)
1073 // Outputs machine-readable pagination in <link> elements.
1074 // Pattern taken from $this->pagination() method.
1076 // "next" is equivalent to "after"
1078 $pargs = array('page' => $page-1);
1079 $this->element('link', array('rel' => 'next',
1080 'href' => common_local_url($action, $args, $pargs),
1081 'title' => _('Next')));
1083 // "previous" is equivalent to "before"
1084 if ($have_previous=true) { // FIXME
1085 $pargs = array('page' => $page+1);
1086 $this->element('link', array('rel' => 'prev',
1087 'href' => common_local_url($action, $args, $pargs),
1088 'title' => _('Previous')));