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', array('id' => $this->trimmed('action')));
330 $this->elementStart('div', array('id' => 'wrap'));
331 if (Event::handle('StartShowHeader', array($this))) {
333 Event::handle('EndShowHeader', array($this));
336 if (Event::handle('StartShowFooter', array($this))) {
338 Event::handle('EndShowFooter', array($this));
340 $this->elementEnd('div');
341 $this->elementEnd('body');
345 * Show header of the page.
347 * Calls template methods
351 function showHeader()
353 $this->elementStart('div', array('id' => 'header'));
355 $this->showPrimaryNav();
356 $this->showSiteNotice();
357 if (common_logged_in()) {
358 $this->showNoticeForm();
360 $this->showAnonymousMessage();
362 $this->elementEnd('div');
366 * Show configured logo.
372 $this->elementStart('address', array('id' => 'site_contact',
373 'class' => 'vcard'));
374 $this->elementStart('a', array('class' => 'url home bookmark',
375 'href' => common_local_url('public')));
376 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
377 $this->element('img', array('class' => 'logo photo',
378 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
379 'alt' => common_config('site', 'name')));
381 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
382 $this->elementEnd('a');
383 $this->elementEnd('address');
387 * Show primary navigation.
391 function showPrimaryNav()
393 $user = common_current_user();
395 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
396 $this->element('dt', null, _('Primary site navigation'));
397 $this->elementStart('dd');
398 $this->elementStart('ul', array('class' => 'nav'));
399 if (Event::handle('StartPrimaryNav', array($this))) {
401 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
402 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
404 $this->menuItem(common_local_url('peoplesearch'),
405 _('Search'), _('Search for people or text'), false, 'nav_search');
407 $this->menuItem(common_local_url('profilesettings'),
408 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
410 if (common_config('xmpp', 'enabled')) {
411 $this->menuItem(common_local_url('imsettings'),
412 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
414 $this->menuItem(common_local_url('smssettings'),
415 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
417 $this->menuItem(common_local_url('logout'),
418 _('Logout'), _('Logout from the site'), false, 'nav_logout');
420 $this->menuItem(common_local_url('login'),
421 _('Login'), _('Login to the site'), false, 'nav_login');
422 if (!common_config('site', 'closed')) {
423 $this->menuItem(common_local_url('register'),
424 _('Register'), _('Create an account'), false, 'nav_register');
426 $this->menuItem(common_local_url('openidlogin'),
427 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
429 $this->menuItem(common_local_url('doc', array('title' => 'help')),
430 _('Help'), _('Help me!'), false, 'nav_help');
431 Event::handle('EndPrimaryNav', array($this));
433 $this->elementEnd('ul');
434 $this->elementEnd('dd');
435 $this->elementEnd('dl');
443 function showSiteNotice()
445 // Revist. Should probably do an hAtom pattern here
446 $text = common_config('site', 'notice');
448 $this->elementStart('dl', array('id' => 'site_notice',
449 'class' => 'system_notice'));
450 $this->element('dt', null, _('Site notice'));
451 $this->elementStart('dd', null);
453 $this->elementEnd('dd');
454 $this->elementEnd('dl');
461 * MAY overload if no notice form needed... or direct message box????
465 function showNoticeForm()
467 $notice_form = new NoticeForm($this);
468 $notice_form->show();
472 * Show anonymous message.
478 function showAnonymousMessage()
480 // needs to be defined by the class
486 * Shows local navigation, content block and aside.
492 $this->elementStart('div', array('id' => 'core'));
493 if (Event::handle('StartShowLocalNavBlock', array($this))) {
494 $this->showLocalNavBlock();
495 Event::handle('EndShowLocalNavBlock', array($this));
497 if (Event::handle('StartShowContentBlock', array($this))) {
498 $this->showContentBlock();
499 Event::handle('EndShowContentBlock', array($this));
502 $this->elementEnd('div');
506 * Show local navigation block.
510 function showLocalNavBlock()
512 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
513 $this->element('dt', null, _('Local views'));
514 $this->elementStart('dd');
515 $this->showLocalNav();
516 $this->elementEnd('dd');
517 $this->elementEnd('dl');
521 * Show local navigation.
527 function showLocalNav()
529 // does nothing by default
533 * Show content block.
537 function showContentBlock()
539 $this->elementStart('div', array('id' => 'content'));
540 $this->showPageTitle();
541 $this->showPageNoticeBlock();
542 $this->elementStart('div', array('id' => 'content_inner'));
543 // show the actual content (forms, lists, whatever)
544 $this->showContent();
545 $this->elementEnd('div');
546 $this->elementEnd('div');
554 function showPageTitle()
556 $this->element('h1', null, $this->title());
560 * Show page notice block.
564 function showPageNoticeBlock()
566 $this->elementStart('dl', array('id' => 'page_notice',
567 'class' => 'system_notice'));
568 $this->element('dt', null, _('Page notice'));
569 $this->elementStart('dd');
570 $this->showPageNotice();
571 $this->elementEnd('dd');
572 $this->elementEnd('dl');
578 * SHOULD overload (unless there's not a notice)
582 function showPageNotice()
589 * MUST overload (unless there's not a notice)
593 function showContent()
605 $this->elementStart('div', array('id' => 'aside_primary',
606 'class' => 'aside'));
607 $this->showExportData();
608 if (Event::handle('StartShowSections', array($this))) {
609 $this->showSections();
610 Event::handle('EndShowSections', array($this));
612 $this->elementEnd('div');
616 * Show export data feeds.
621 function showExportData()
623 $feeds = $this->getFeeds();
625 $fl = new FeedList($this);
637 function showSections()
639 // for each section, show it
647 function showFooter()
649 $this->elementStart('div', array('id' => 'footer'));
650 $this->showSecondaryNav();
651 $this->showLicenses();
652 $this->elementEnd('div');
656 * Show secondary navigation.
660 function showSecondaryNav()
662 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
663 $this->element('dt', null, _('Secondary site navigation'));
664 $this->elementStart('dd', null);
665 $this->elementStart('ul', array('class' => 'nav'));
666 if (Event::handle('StartSecondaryNav', array($this))) {
667 $this->menuItem(common_local_url('doc', array('title' => 'help')),
669 $this->menuItem(common_local_url('doc', array('title' => 'about')),
671 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
673 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
675 $this->menuItem(common_local_url('doc', array('title' => 'source')),
677 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
679 $this->menuItem(common_local_url('doc', array('title' => 'badge')),
681 Event::handle('EndSecondaryNav', array($this));
683 $this->elementEnd('ul');
684 $this->elementEnd('dd');
685 $this->elementEnd('dl');
693 function showLicenses()
695 $this->elementStart('dl', array('id' => 'licenses'));
696 $this->showLaconicaLicense();
697 $this->showContentLicense();
698 $this->elementEnd('dl');
702 * Show Laconica license.
706 function showLaconicaLicense()
708 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
709 $this->elementStart('dd', null);
710 if (common_config('site', 'broughtby')) {
711 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
713 $instr = _('**%%site.name%%** is a microblogging service. ');
715 $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);
716 $output = common_markup_to_html($instr);
718 $this->elementEnd('dd');
723 * Show content license.
727 function showContentLicense()
729 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
730 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
731 $this->elementStart('p');
732 $this->element('img', array('id' => 'license_cc',
733 'src' => common_config('license', 'image'),
734 'alt' => common_config('license', 'title')));
735 //TODO: This is dirty: i18n
736 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
737 $this->element('a', array('class' => 'license',
738 'rel' => 'external license',
739 'href' => common_config('license', 'url')),
740 common_config('license', 'title'));
741 $this->text(_('license.'));
742 $this->elementEnd('p');
743 $this->elementEnd('dd');
747 * Return last modified, if applicable.
751 * @return string last modified http header
753 function lastModified()
755 // For comparison with If-Last-Modified
756 // If not applicable, return null
761 * Return etag, if applicable.
765 * @return string etag http header
773 * Return true if read only.
777 * @return boolean is read only action?
779 function isReadOnly()
785 * Returns query argument or default value if not found
787 * @param string $key requested argument
788 * @param string $def default value to return if $key is not provided
790 * @return boolean is read only action?
792 function arg($key, $def=null)
794 if (array_key_exists($key, $this->args)) {
795 return $this->args[$key];
802 * Returns trimmed query argument or default value if not found
804 * @param string $key requested argument
805 * @param string $def default value to return if $key is not provided
807 * @return boolean is read only action?
809 function trimmed($key, $def=null)
811 $arg = $this->arg($key, $def);
812 return is_string($arg) ? trim($arg) : $arg;
818 * @param array $argarray is ignored since it's now passed in in prepare()
820 * @return boolean is read only action?
822 function handle($argarray=null)
824 $lm = $this->lastModified();
825 $etag = $this->etag();
827 header('ETag: ' . $etag);
830 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
831 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
832 $ims = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
834 $if_none_match = $_SERVER['HTTP_IF_NONE_MATCH'];
835 if (!$if_none_match ||
837 $this->_hasEtag($etag, $if_none_match)) {
838 header('HTTP/1.1 304 Not Modified');
839 // Better way to do this?
848 * HasĀ etag? (private)
850 * @param string $etag etag http header
851 * @param string $if_none_match ifNoneMatch http header
856 function _hasEtag($etag, $if_none_match)
858 $etags = explode(',', $if_none_match);
859 return in_array($etag, $etags) || in_array('*', $etags);
863 * Boolean understands english (yes, no, true, false)
865 * @param string $key query key we're interested in
866 * @param string $def default value
868 * @return boolean interprets yes/no strings as boolean
870 function boolean($key, $def=false)
872 $arg = strtolower($this->trimmed($key));
876 } else if (in_array($arg, array('true', 'yes', '1'))) {
878 } else if (in_array($arg, array('false', 'no', '0'))) {
888 * @param string $msg error message to display
889 * @param integer $code http error code, 500 by default
894 function serverError($msg, $code=500)
896 $action = $this->trimmed('action');
897 common_debug("Server error '$code' on '$action': $msg", __FILE__);
898 throw new ServerException($msg, $code);
904 * @param string $msg error message to display
905 * @param integer $code http error code, 400 by default
910 function clientError($msg, $code=400)
912 $action = $this->trimmed('action');
913 common_debug("User error '$code' on '$action': $msg", __FILE__);
914 throw new ClientException($msg, $code);
918 * Returns the current URL
920 * @return string current URL
924 $action = $this->trimmed('action');
926 unset($args['action']);
927 foreach (array_keys($_COOKIE) as $cookie) {
928 unset($args[$cookie]);
930 return common_local_url($action, $args);
934 * Generate a menu item
936 * @param string $url menu URL
937 * @param string $text menu name
938 * @param string $title title attribute, null by default
939 * @param boolean $is_selected current menu item, false by default
940 * @param string $id element id, null by default
944 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
946 // Added @id to li for some control.
947 // XXX: We might want to move this to htmloutputter.php
950 $lattrs['class'] = 'current';
953 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
955 $this->elementStart('li', $lattrs);
956 $attrs['href'] = $url;
958 $attrs['title'] = $title;
960 $this->element('a', $attrs, $text);
961 $this->elementEnd('li');
965 * Generate pagination links
967 * @param boolean $have_before is there something before?
968 * @param boolean $have_after is there something after?
969 * @param integer $page current page
970 * @param string $action current action
971 * @param array $args rest of query arguments
975 function pagination($have_before, $have_after, $page, $action, $args=null)
977 // Does a little before-after block for next/prev page
978 if ($have_before || $have_after) {
979 $this->elementStart('div', array('class' => 'pagination'));
980 $this->elementStart('dl', null);
981 $this->element('dt', null, _('Pagination'));
982 $this->elementStart('dd', null);
983 $this->elementStart('ul', array('class' => 'nav'));
986 $pargs = array('page' => $page-1);
987 $this->elementStart('li', array('class' => 'nav_prev'));
988 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
991 $this->elementEnd('li');
994 $pargs = array('page' => $page+1);
995 $this->elementStart('li', array('class' => 'nav_next'));
996 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
999 $this->elementEnd('li');
1001 if ($have_before || $have_after) {
1002 $this->elementEnd('ul');
1003 $this->elementEnd('dd');
1004 $this->elementEnd('dl');
1005 $this->elementEnd('div');
1010 * An array of feeds for this action.
1012 * Returns an array of potential feeds for this action.
1014 * @return array Feed object to show in head and links