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')),
245 Event::handle('EndShowJQueryScripts', array($this));
247 if (Event::handle('StartShowLaconicaScripts', array($this))) {
248 $this->element('script', array('type' => 'text/javascript',
249 'src' => common_path('js/xbImportNode.js')),
251 $this->element('script', array('type' => 'text/javascript',
252 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
254 // Frame-busting code to avoid clickjacking attacks.
255 $this->element('script', array('type' => 'text/javascript'),
256 'if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
257 Event::handle('EndShowLaconicaScripts', array($this));
259 Event::handle('EndShowScripts', array($this));
264 * Show OpenSearch headers
268 function showOpenSearch()
270 $this->element('link', array('rel' => 'search',
271 'type' => 'application/opensearchdescription+xml',
272 'href' => common_local_url('opensearch', array('type' => 'people')),
273 'title' => common_config('site', 'name').' People Search'));
274 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
275 'href' => common_local_url('opensearch', array('type' => 'notice')),
276 'title' => common_config('site', 'name').' Notice Search'));
289 $feeds = $this->getFeeds();
292 foreach ($feeds as $feed) {
293 $this->element('link', array('rel' => $feed->rel(),
294 'href' => $feed->url,
295 'type' => $feed->mimeType(),
296 'title' => $feed->title));
308 function showDescription()
310 // does nothing by default
314 * Show extra stuff in <head>.
322 // does nothing by default
328 * Calls template methods
334 $this->elementStart('body', (common_current_user()) ? array('id' => $this->trimmed('action'),
335 'class' => 'user_in')
336 : array('id' => $this->trimmed('action')));
337 $this->elementStart('div', array('id' => 'wrap'));
338 if (Event::handle('StartShowHeader', array($this))) {
340 Event::handle('EndShowHeader', array($this));
343 if (Event::handle('StartShowFooter', array($this))) {
345 Event::handle('EndShowFooter', array($this));
347 $this->elementEnd('div');
348 $this->elementEnd('body');
352 * Show header of the page.
354 * Calls template methods
358 function showHeader()
360 $this->elementStart('div', array('id' => 'header'));
362 $this->showPrimaryNav();
363 $this->showSiteNotice();
364 if (common_logged_in()) {
365 $this->showNoticeForm();
367 $this->showAnonymousMessage();
369 $this->elementEnd('div');
373 * Show configured logo.
379 $this->elementStart('address', array('id' => 'site_contact',
380 'class' => 'vcard'));
381 $this->elementStart('a', array('class' => 'url home bookmark',
382 'href' => common_local_url('public')));
383 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
384 $this->element('img', array('class' => 'logo photo',
385 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
386 'alt' => common_config('site', 'name')));
388 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
389 $this->elementEnd('a');
390 $this->elementEnd('address');
394 * Show primary navigation.
398 function showPrimaryNav()
400 $user = common_current_user();
402 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
403 $this->element('dt', null, _('Primary site navigation'));
404 $this->elementStart('dd');
405 $this->elementStart('ul', array('class' => 'nav'));
406 if (Event::handle('StartPrimaryNav', array($this))) {
408 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
409 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
410 $this->menuItem(common_local_url('profilesettings'),
411 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
412 if (common_config('xmpp', 'enabled')) {
413 $this->menuItem(common_local_url('imsettings'),
414 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
416 $this->menuItem(common_local_url('smssettings'),
417 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
419 $this->menuItem(common_local_url('invite'),
421 sprintf(_('Invite friends and colleagues to join you on %s'),
422 common_config('site', 'name')),
423 false, 'nav_invitecontact');
424 $this->menuItem(common_local_url('logout'),
425 _('Logout'), _('Logout from the site'), false, 'nav_logout');
428 if (!common_config('site', 'closed')) {
429 $this->menuItem(common_local_url('register'),
430 _('Register'), _('Create an account'), false, 'nav_register');
432 $this->menuItem(common_local_url('openidlogin'),
433 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
434 $this->menuItem(common_local_url('login'),
435 _('Login'), _('Login to the site'), false, 'nav_login');
437 $this->menuItem(common_local_url('doc', array('title' => 'help')),
438 _('Help'), _('Help me!'), false, 'nav_help');
439 $this->menuItem(common_local_url('peoplesearch'),
440 _('Search'), _('Search for people or text'), false, 'nav_search');
441 Event::handle('EndPrimaryNav', array($this));
443 $this->elementEnd('ul');
444 $this->elementEnd('dd');
445 $this->elementEnd('dl');
453 function showSiteNotice()
455 // Revist. Should probably do an hAtom pattern here
456 $text = common_config('site', 'notice');
458 $this->elementStart('dl', array('id' => 'site_notice',
459 'class' => 'system_notice'));
460 $this->element('dt', null, _('Site notice'));
461 $this->elementStart('dd', null);
463 $this->elementEnd('dd');
464 $this->elementEnd('dl');
471 * MAY overload if no notice form needed... or direct message box????
475 function showNoticeForm()
477 $notice_form = new NoticeForm($this);
478 $notice_form->show();
482 * Show anonymous message.
488 function showAnonymousMessage()
490 // needs to be defined by the class
496 * Shows local navigation, content block and aside.
502 $this->elementStart('div', array('id' => 'core'));
503 if (Event::handle('StartShowLocalNavBlock', array($this))) {
504 $this->showLocalNavBlock();
505 Event::handle('EndShowLocalNavBlock', array($this));
507 if (Event::handle('StartShowContentBlock', array($this))) {
508 $this->showContentBlock();
509 Event::handle('EndShowContentBlock', array($this));
512 $this->elementEnd('div');
516 * Show local navigation block.
520 function showLocalNavBlock()
522 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
523 $this->element('dt', null, _('Local views'));
524 $this->elementStart('dd');
525 $this->showLocalNav();
526 $this->elementEnd('dd');
527 $this->elementEnd('dl');
531 * Show local navigation.
537 function showLocalNav()
539 // does nothing by default
543 * Show content block.
547 function showContentBlock()
549 $this->elementStart('div', array('id' => 'content'));
550 $this->showPageTitle();
551 $this->showPageNoticeBlock();
552 $this->elementStart('div', array('id' => 'content_inner'));
553 // show the actual content (forms, lists, whatever)
554 $this->showContent();
555 $this->elementEnd('div');
556 $this->elementEnd('div');
564 function showPageTitle()
566 $this->element('h1', null, $this->title());
570 * Show page notice block.
574 function showPageNoticeBlock()
576 $this->elementStart('dl', array('id' => 'page_notice',
577 'class' => 'system_notice'));
578 $this->element('dt', null, _('Page notice'));
579 $this->elementStart('dd');
580 if (Event::handle('StartShowPageNotice', array($this))) {
581 $this->showPageNotice();
582 Event::handle('EndShowPageNotice', array($this));
584 $this->elementEnd('dd');
585 $this->elementEnd('dl');
591 * SHOULD overload (unless there's not a notice)
595 function showPageNotice()
602 * MUST overload (unless there's not a notice)
606 function showContent()
618 $this->elementStart('div', array('id' => 'aside_primary',
619 'class' => 'aside'));
620 if (Event::handle('StartShowExportData', array($this))) {
621 $this->showExportData();
622 Event::handle('EndShowExportData', array($this));
624 if (Event::handle('StartShowSections', array($this))) {
625 $this->showSections();
626 Event::handle('EndShowSections', array($this));
628 $this->elementEnd('div');
632 * Show export data feeds.
637 function showExportData()
639 $feeds = $this->getFeeds();
641 $fl = new FeedList($this);
653 function showSections()
655 // for each section, show it
663 function showFooter()
665 $this->elementStart('div', array('id' => 'footer'));
666 $this->showSecondaryNav();
667 $this->showLicenses();
668 $this->elementEnd('div');
672 * Show secondary navigation.
676 function showSecondaryNav()
678 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
679 $this->element('dt', null, _('Secondary site navigation'));
680 $this->elementStart('dd', null);
681 $this->elementStart('ul', array('class' => 'nav'));
682 if (Event::handle('StartSecondaryNav', array($this))) {
683 $this->menuItem(common_local_url('doc', array('title' => 'help')),
685 $this->menuItem(common_local_url('doc', array('title' => 'about')),
687 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
689 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
691 $this->menuItem(common_local_url('doc', array('title' => 'source')),
693 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
695 $this->menuItem(common_local_url('doc', array('title' => 'badge')),
697 Event::handle('EndSecondaryNav', array($this));
699 $this->elementEnd('ul');
700 $this->elementEnd('dd');
701 $this->elementEnd('dl');
709 function showLicenses()
711 $this->elementStart('dl', array('id' => 'licenses'));
712 $this->showLaconicaLicense();
713 $this->showContentLicense();
714 $this->elementEnd('dl');
718 * Show Laconica license.
722 function showLaconicaLicense()
724 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
725 $this->elementStart('dd', null);
726 if (common_config('site', 'broughtby')) {
727 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
729 $instr = _('**%%site.name%%** is a microblogging service. ');
731 $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);
732 $output = common_markup_to_html($instr);
734 $this->elementEnd('dd');
739 * Show content license.
743 function showContentLicense()
745 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
746 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
747 $this->elementStart('p');
748 $this->element('img', array('id' => 'license_cc',
749 'src' => common_config('license', 'image'),
750 'alt' => common_config('license', 'title')));
751 //TODO: This is dirty: i18n
752 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
753 $this->element('a', array('class' => 'license',
754 'rel' => 'external license',
755 'href' => common_config('license', 'url')),
756 common_config('license', 'title'));
757 $this->text(_('license.'));
758 $this->elementEnd('p');
759 $this->elementEnd('dd');
763 * Return last modified, if applicable.
767 * @return string last modified http header
769 function lastModified()
771 // For comparison with If-Last-Modified
772 // If not applicable, return null
777 * Return etag, if applicable.
781 * @return string etag http header
789 * Return true if read only.
793 * @param array $args other arguments
795 * @return boolean is read only action?
798 function isReadOnly($args)
804 * Returns query argument or default value if not found
806 * @param string $key requested argument
807 * @param string $def default value to return if $key is not provided
809 * @return boolean is read only action?
811 function arg($key, $def=null)
813 if (array_key_exists($key, $this->args)) {
814 return $this->args[$key];
821 * Returns trimmed query argument or default value if not found
823 * @param string $key requested argument
824 * @param string $def default value to return if $key is not provided
826 * @return boolean is read only action?
828 function trimmed($key, $def=null)
830 $arg = $this->arg($key, $def);
831 return is_string($arg) ? trim($arg) : $arg;
837 * @param array $argarray is ignored since it's now passed in in prepare()
839 * @return boolean is read only action?
841 function handle($argarray=null)
843 $lm = $this->lastModified();
844 $etag = $this->etag();
846 header('ETag: ' . $etag);
849 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
850 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
851 $ims = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
853 $if_none_match = $_SERVER['HTTP_IF_NONE_MATCH'];
854 if (!$if_none_match ||
856 $this->_hasEtag($etag, $if_none_match)) {
857 header('HTTP/1.1 304 Not Modified');
858 // Better way to do this?
867 * HasĀ etag? (private)
869 * @param string $etag etag http header
870 * @param string $if_none_match ifNoneMatch http header
875 function _hasEtag($etag, $if_none_match)
877 $etags = explode(',', $if_none_match);
878 return in_array($etag, $etags) || in_array('*', $etags);
882 * Boolean understands english (yes, no, true, false)
884 * @param string $key query key we're interested in
885 * @param string $def default value
887 * @return boolean interprets yes/no strings as boolean
889 function boolean($key, $def=false)
891 $arg = strtolower($this->trimmed($key));
895 } else if (in_array($arg, array('true', 'yes', '1'))) {
897 } else if (in_array($arg, array('false', 'no', '0'))) {
907 * @param string $msg error message to display
908 * @param integer $code http error code, 500 by default
913 function serverError($msg, $code=500)
915 $action = $this->trimmed('action');
916 common_debug("Server error '$code' on '$action': $msg", __FILE__);
917 throw new ServerException($msg, $code);
923 * @param string $msg error message to display
924 * @param integer $code http error code, 400 by default
929 function clientError($msg, $code=400)
931 $action = $this->trimmed('action');
932 common_debug("User error '$code' on '$action': $msg", __FILE__);
933 throw new ClientException($msg, $code);
937 * Returns the current URL
939 * @return string current URL
944 $action = $this->trimmed('action');
946 unset($args['action']);
947 if (array_key_exists('submit', $args)) {
948 unset($args['submit']);
950 foreach (array_keys($_COOKIE) as $cookie) {
951 unset($args[$cookie]);
953 return common_local_url($action, $args);
957 * Generate a menu item
959 * @param string $url menu URL
960 * @param string $text menu name
961 * @param string $title title attribute, null by default
962 * @param boolean $is_selected current menu item, false by default
963 * @param string $id element id, null by default
967 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
969 // Added @id to li for some control.
970 // XXX: We might want to move this to htmloutputter.php
973 $lattrs['class'] = 'current';
976 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
978 $this->elementStart('li', $lattrs);
979 $attrs['href'] = $url;
981 $attrs['title'] = $title;
983 $this->element('a', $attrs, $text);
984 $this->elementEnd('li');
988 * Generate pagination links
990 * @param boolean $have_before is there something before?
991 * @param boolean $have_after is there something after?
992 * @param integer $page current page
993 * @param string $action current action
994 * @param array $args rest of query arguments
998 function pagination($have_before, $have_after, $page, $action, $args=null)
1000 // Does a little before-after block for next/prev page
1001 if ($have_before || $have_after) {
1002 $this->elementStart('div', array('class' => 'pagination'));
1003 $this->elementStart('dl', null);
1004 $this->element('dt', null, _('Pagination'));
1005 $this->elementStart('dd', null);
1006 $this->elementStart('ul', array('class' => 'nav'));
1009 $pargs = array('page' => $page-1);
1010 $this->elementStart('li', array('class' => 'nav_prev'));
1011 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1014 $this->elementEnd('li');
1017 $pargs = array('page' => $page+1);
1018 $this->elementStart('li', array('class' => 'nav_next'));
1019 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1022 $this->elementEnd('li');
1024 if ($have_before || $have_after) {
1025 $this->elementEnd('ul');
1026 $this->elementEnd('dd');
1027 $this->elementEnd('dl');
1028 $this->elementEnd('div');
1033 * An array of feeds for this action.
1035 * Returns an array of potential feeds for this action.
1037 * @return array Feed object to show in head and links