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));
106 * Show head, a template method.
112 // XXX: attributes (profile?)
113 $this->elementStart('head');
115 $this->showShortcutIcon();
116 $this->showStylesheets();
117 $this->showScripts();
118 $this->showOpenSearch();
120 $this->showDescription();
122 $this->elementEnd('head');
126 * Show title, a template method.
132 $this->element('title', null,
133 sprintf(_("%s - %s"),
135 common_config('site', 'name')));
139 * Returns the page title
143 * @return string page title
148 return _("Untitled page");
152 * Show themed shortcut icon
156 function showShortcutIcon()
158 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/favicon.ico')) {
159 $this->element('link', array('rel' => 'shortcut icon',
160 'href' => theme_path('favicon.ico')));
162 $this->element('link', array('rel' => 'shortcut icon',
163 'href' => common_path('favicon.ico')));
166 if (common_config('site', 'mobile')) {
167 if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/apple-touch-icon.png')) {
168 $this->element('link', array('rel' => 'apple-touch-icon',
169 'href' => theme_path('apple-touch-icon.png')));
171 $this->element('link', array('rel' => 'apple-touch-icon',
172 'href' => common_path('apple-touch-icon.png')));
182 function showStylesheets()
184 if (Event::handle('StartShowStyles', array($this))) {
185 if (Event::handle('StartShowLaconicaStyles', array($this))) {
186 $this->element('link', array('rel' => 'stylesheet',
187 'type' => 'text/css',
188 'href' => theme_path('css/display.css', 'base') . '?version=' . LACONICA_VERSION,
189 'media' => 'screen, projection, tv'));
190 $this->element('link', array('rel' => 'stylesheet',
191 'type' => 'text/css',
192 'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
193 'media' => 'screen, projection, tv'));
194 if (common_config('site', 'mobile')) {
195 $this->element('link', array('rel' => 'stylesheet',
196 'type' => 'text/css',
197 'href' => theme_path('css/mobile.css', 'base') . '?version=' . LACONICA_VERSION,
198 // TODO: "handheld" CSS for other mobile devices
199 'media' => 'only screen and (max-device-width: 480px)')); // Mobile WebKit
201 $this->element('link', array('rel' => 'stylesheet',
202 'type' => 'text/css',
203 'href' => theme_path('css/print.css', 'base') . '?version=' . LACONICA_VERSION,
204 'media' => 'print'));
205 Event::handle('EndShowLaconicaStyles', array($this));
207 if (Event::handle('StartShowUAStyles', array($this))) {
208 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
209 'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
210 foreach (array(6,7) as $ver) {
211 if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
212 // Yes, IE people should be put in jail.
213 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
214 'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
217 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
218 'href="'.theme_path('css/ie.css', null).'?version='.LACONICA_VERSION.'" /><![endif]');
219 Event::handle('EndShowUAStyles', array($this));
221 Event::handle('EndShowStyles', array($this));
226 * Show javascript headers
230 function showScripts()
232 if (Event::handle('StartShowScripts', array($this))) {
233 if (Event::handle('StartShowJQueryScripts', array($this))) {
234 $this->element('script', array('type' => 'text/javascript',
235 'src' => common_path('js/jquery.min.js')),
237 $this->element('script', array('type' => 'text/javascript',
238 'src' => common_path('js/jquery.form.js')),
240 Event::handle('EndShowJQueryScripts', array($this));
242 if (Event::handle('StartShowLaconicaScripts', array($this))) {
243 $this->element('script', array('type' => 'text/javascript',
244 'src' => common_path('js/xbImportNode.js')),
246 $this->element('script', array('type' => 'text/javascript',
247 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
249 // Frame-busting code to avoid clickjacking attacks.
250 $this->element('script', array('type' => 'text/javascript'),
251 'if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
252 Event::handle('EndShowLaconicaScripts', array($this));
254 Event::handle('EndShowScripts', array($this));
259 * Show OpenSearch headers
263 function showOpenSearch()
265 $this->element('link', array('rel' => 'search',
266 'type' => 'application/opensearchdescription+xml',
267 'href' => common_local_url('opensearch', array('type' => 'people')),
268 'title' => common_config('site', 'name').' People Search'));
269 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
270 'href' => common_local_url('opensearch', array('type' => 'notice')),
271 'title' => common_config('site', 'name').' Notice Search'));
284 $feeds = $this->getFeeds();
287 foreach ($feeds as $feed) {
288 $this->element('link', array('rel' => $feed->rel(),
289 'href' => $feed->url,
290 'type' => $feed->mimeType(),
291 'title' => $feed->title));
303 function showDescription()
305 // does nothing by default
309 * Show extra stuff in <head>.
317 // does nothing by default
323 * Calls template methods
329 $this->elementStart('body', (common_current_user()) ? array('id' => $this->trimmed('action'),
330 'class' => 'user_in')
331 : array('id' => $this->trimmed('action')));
332 $this->elementStart('div', array('id' => 'wrap'));
333 if (Event::handle('StartShowHeader', array($this))) {
335 Event::handle('EndShowHeader', array($this));
338 if (Event::handle('StartShowFooter', array($this))) {
340 Event::handle('EndShowFooter', array($this));
342 $this->elementEnd('div');
343 $this->elementEnd('body');
347 * Show header of the page.
349 * Calls template methods
353 function showHeader()
355 $this->elementStart('div', array('id' => 'header'));
357 $this->showPrimaryNav();
358 $this->showSiteNotice();
359 if (common_logged_in()) {
360 $this->showNoticeForm();
362 $this->showAnonymousMessage();
364 $this->elementEnd('div');
368 * Show configured logo.
374 $this->elementStart('address', array('id' => 'site_contact',
375 'class' => 'vcard'));
376 $this->elementStart('a', array('class' => 'url home bookmark',
377 'href' => common_local_url('public')));
378 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
379 $this->element('img', array('class' => 'logo photo',
380 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
381 'alt' => common_config('site', 'name')));
383 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
384 $this->elementEnd('a');
385 $this->elementEnd('address');
389 * Show primary navigation.
393 function showPrimaryNav()
395 $user = common_current_user();
397 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
398 $this->element('dt', null, _('Primary site navigation'));
399 $this->elementStart('dd');
400 $this->elementStart('ul', array('class' => 'nav'));
401 if (Event::handle('StartPrimaryNav', array($this))) {
403 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
404 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
405 $this->menuItem(common_local_url('profilesettings'),
406 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
407 if (common_config('xmpp', 'enabled')) {
408 $this->menuItem(common_local_url('imsettings'),
409 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
411 $this->menuItem(common_local_url('smssettings'),
412 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
414 $this->menuItem(common_local_url('invite'),
416 sprintf(_('Invite friends and colleagues to join you on %s'),
417 common_config('site', 'name')),
418 false, 'nav_invitecontact');
419 $this->menuItem(common_local_url('logout'),
420 _('Logout'), _('Logout from the site'), false, 'nav_logout');
423 if (!common_config('site', 'closed')) {
424 $this->menuItem(common_local_url('register'),
425 _('Register'), _('Create an account'), false, 'nav_register');
427 $this->menuItem(common_local_url('openidlogin'),
428 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
429 $this->menuItem(common_local_url('login'),
430 _('Login'), _('Login to the site'), false, 'nav_login');
432 $this->menuItem(common_local_url('doc', array('title' => 'help')),
433 _('Help'), _('Help me!'), false, 'nav_help');
434 $this->menuItem(common_local_url('peoplesearch'),
435 _('Search'), _('Search for people or text'), false, 'nav_search');
436 Event::handle('EndPrimaryNav', array($this));
438 $this->elementEnd('ul');
439 $this->elementEnd('dd');
440 $this->elementEnd('dl');
448 function showSiteNotice()
450 // Revist. Should probably do an hAtom pattern here
451 $text = common_config('site', 'notice');
453 $this->elementStart('dl', array('id' => 'site_notice',
454 'class' => 'system_notice'));
455 $this->element('dt', null, _('Site notice'));
456 $this->elementStart('dd', null);
458 $this->elementEnd('dd');
459 $this->elementEnd('dl');
466 * MAY overload if no notice form needed... or direct message box????
470 function showNoticeForm()
472 $notice_form = new NoticeForm($this);
473 $notice_form->show();
477 * Show anonymous message.
483 function showAnonymousMessage()
485 // needs to be defined by the class
491 * Shows local navigation, content block and aside.
497 $this->elementStart('div', array('id' => 'core'));
498 if (Event::handle('StartShowLocalNavBlock', array($this))) {
499 $this->showLocalNavBlock();
500 Event::handle('EndShowLocalNavBlock', array($this));
502 if (Event::handle('StartShowContentBlock', array($this))) {
503 $this->showContentBlock();
504 Event::handle('EndShowContentBlock', array($this));
507 $this->elementEnd('div');
511 * Show local navigation block.
515 function showLocalNavBlock()
517 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
518 $this->element('dt', null, _('Local views'));
519 $this->elementStart('dd');
520 $this->showLocalNav();
521 $this->elementEnd('dd');
522 $this->elementEnd('dl');
526 * Show local navigation.
532 function showLocalNav()
534 // does nothing by default
538 * Show content block.
542 function showContentBlock()
544 $this->elementStart('div', array('id' => 'content'));
545 $this->showPageTitle();
546 $this->showPageNoticeBlock();
547 $this->elementStart('div', array('id' => 'content_inner'));
548 // show the actual content (forms, lists, whatever)
549 $this->showContent();
550 $this->elementEnd('div');
551 $this->elementEnd('div');
559 function showPageTitle()
561 $this->element('h1', null, $this->title());
565 * Show page notice block.
569 function showPageNoticeBlock()
571 $this->elementStart('dl', array('id' => 'page_notice',
572 'class' => 'system_notice'));
573 $this->element('dt', null, _('Page notice'));
574 $this->elementStart('dd');
575 $this->showPageNotice();
576 $this->elementEnd('dd');
577 $this->elementEnd('dl');
583 * SHOULD overload (unless there's not a notice)
587 function showPageNotice()
594 * MUST overload (unless there's not a notice)
598 function showContent()
610 $this->elementStart('div', array('id' => 'aside_primary',
611 'class' => 'aside'));
612 $this->showExportData();
613 if (Event::handle('StartShowSections', array($this))) {
614 $this->showSections();
615 Event::handle('EndShowSections', array($this));
617 $this->elementEnd('div');
621 * Show export data feeds.
626 function showExportData()
628 $feeds = $this->getFeeds();
630 $fl = new FeedList($this);
642 function showSections()
644 // for each section, show it
652 function showFooter()
654 $this->elementStart('div', array('id' => 'footer'));
655 $this->showSecondaryNav();
656 $this->showLicenses();
657 $this->elementEnd('div');
661 * Show secondary navigation.
665 function showSecondaryNav()
667 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
668 $this->element('dt', null, _('Secondary site navigation'));
669 $this->elementStart('dd', null);
670 $this->elementStart('ul', array('class' => 'nav'));
671 if (Event::handle('StartSecondaryNav', array($this))) {
672 $this->menuItem(common_local_url('doc', array('title' => 'help')),
674 $this->menuItem(common_local_url('doc', array('title' => 'about')),
676 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
678 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
680 $this->menuItem(common_local_url('doc', array('title' => 'source')),
682 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
684 $this->menuItem(common_local_url('doc', array('title' => 'badge')),
686 Event::handle('EndSecondaryNav', array($this));
688 $this->elementEnd('ul');
689 $this->elementEnd('dd');
690 $this->elementEnd('dl');
698 function showLicenses()
700 $this->elementStart('dl', array('id' => 'licenses'));
701 $this->showLaconicaLicense();
702 $this->showContentLicense();
703 $this->elementEnd('dl');
707 * Show Laconica license.
711 function showLaconicaLicense()
713 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
714 $this->elementStart('dd', null);
715 if (common_config('site', 'broughtby')) {
716 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
718 $instr = _('**%%site.name%%** is a microblogging service. ');
720 $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);
721 $output = common_markup_to_html($instr);
723 $this->elementEnd('dd');
728 * Show content license.
732 function showContentLicense()
734 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
735 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
736 $this->elementStart('p');
737 $this->element('img', array('id' => 'license_cc',
738 'src' => common_config('license', 'image'),
739 'alt' => common_config('license', 'title')));
740 //TODO: This is dirty: i18n
741 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
742 $this->element('a', array('class' => 'license',
743 'rel' => 'external license',
744 'href' => common_config('license', 'url')),
745 common_config('license', 'title'));
746 $this->text(_('license.'));
747 $this->elementEnd('p');
748 $this->elementEnd('dd');
752 * Return last modified, if applicable.
756 * @return string last modified http header
758 function lastModified()
760 // For comparison with If-Last-Modified
761 // If not applicable, return null
766 * Return etag, if applicable.
770 * @return string etag http header
778 * Return true if read only.
782 * @return boolean is read only action?
784 function isReadOnly()
790 * Returns query argument or default value if not found
792 * @param string $key requested argument
793 * @param string $def default value to return if $key is not provided
795 * @return boolean is read only action?
797 function arg($key, $def=null)
799 if (array_key_exists($key, $this->args)) {
800 return $this->args[$key];
807 * Returns trimmed query argument or default value if not found
809 * @param string $key requested argument
810 * @param string $def default value to return if $key is not provided
812 * @return boolean is read only action?
814 function trimmed($key, $def=null)
816 $arg = $this->arg($key, $def);
817 return is_string($arg) ? trim($arg) : $arg;
823 * @param array $argarray is ignored since it's now passed in in prepare()
825 * @return boolean is read only action?
827 function handle($argarray=null)
829 $lm = $this->lastModified();
830 $etag = $this->etag();
832 header('ETag: ' . $etag);
835 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
836 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
837 $ims = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
839 $if_none_match = $_SERVER['HTTP_IF_NONE_MATCH'];
840 if (!$if_none_match ||
842 $this->_hasEtag($etag, $if_none_match)) {
843 header('HTTP/1.1 304 Not Modified');
844 // Better way to do this?
853 * HasĀ etag? (private)
855 * @param string $etag etag http header
856 * @param string $if_none_match ifNoneMatch http header
861 function _hasEtag($etag, $if_none_match)
863 $etags = explode(',', $if_none_match);
864 return in_array($etag, $etags) || in_array('*', $etags);
868 * Boolean understands english (yes, no, true, false)
870 * @param string $key query key we're interested in
871 * @param string $def default value
873 * @return boolean interprets yes/no strings as boolean
875 function boolean($key, $def=false)
877 $arg = strtolower($this->trimmed($key));
881 } else if (in_array($arg, array('true', 'yes', '1'))) {
883 } else if (in_array($arg, array('false', 'no', '0'))) {
893 * @param string $msg error message to display
894 * @param integer $code http error code, 500 by default
899 function serverError($msg, $code=500)
901 $action = $this->trimmed('action');
902 common_debug("Server error '$code' on '$action': $msg", __FILE__);
903 throw new ServerException($msg, $code);
909 * @param string $msg error message to display
910 * @param integer $code http error code, 400 by default
915 function clientError($msg, $code=400)
917 $action = $this->trimmed('action');
918 common_debug("User error '$code' on '$action': $msg", __FILE__);
919 throw new ClientException($msg, $code);
923 * Returns the current URL
925 * @return string current URL
929 $action = $this->trimmed('action');
931 unset($args['action']);
932 foreach (array_keys($_COOKIE) as $cookie) {
933 unset($args[$cookie]);
935 return common_local_url($action, $args);
939 * Generate a menu item
941 * @param string $url menu URL
942 * @param string $text menu name
943 * @param string $title title attribute, null by default
944 * @param boolean $is_selected current menu item, false by default
945 * @param string $id element id, null by default
949 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
951 // Added @id to li for some control.
952 // XXX: We might want to move this to htmloutputter.php
955 $lattrs['class'] = 'current';
958 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
960 $this->elementStart('li', $lattrs);
961 $attrs['href'] = $url;
963 $attrs['title'] = $title;
965 $this->element('a', $attrs, $text);
966 $this->elementEnd('li');
970 * Generate pagination links
972 * @param boolean $have_before is there something before?
973 * @param boolean $have_after is there something after?
974 * @param integer $page current page
975 * @param string $action current action
976 * @param array $args rest of query arguments
980 function pagination($have_before, $have_after, $page, $action, $args=null)
982 // Does a little before-after block for next/prev page
983 if ($have_before || $have_after) {
984 $this->elementStart('div', array('class' => 'pagination'));
985 $this->elementStart('dl', null);
986 $this->element('dt', null, _('Pagination'));
987 $this->elementStart('dd', null);
988 $this->elementStart('ul', array('class' => 'nav'));
991 $pargs = array('page' => $page-1);
992 $this->elementStart('li', array('class' => 'nav_prev'));
993 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
996 $this->elementEnd('li');
999 $pargs = array('page' => $page+1);
1000 $this->elementStart('li', array('class' => 'nav_next'));
1001 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1004 $this->elementEnd('li');
1006 if ($have_before || $have_after) {
1007 $this->elementEnd('ul');
1008 $this->elementEnd('dd');
1009 $this->elementEnd('dl');
1010 $this->elementEnd('div');
1015 * An array of feeds for this action.
1017 * Returns an array of potential feeds for this action.
1019 * @return array Feed object to show in head and links