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');
406 $this->menuItem(common_local_url('peoplesearch'),
407 _('Search'), _('Search for people or text'), false, 'nav_search');
409 $this->menuItem(common_local_url('profilesettings'),
410 _('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('logout'),
420 _('Logout'), _('Logout from the site'), false, 'nav_logout');
422 $this->menuItem(common_local_url('login'),
423 _('Login'), _('Login to the site'), false, 'nav_login');
424 if (!common_config('site', 'closed')) {
425 $this->menuItem(common_local_url('register'),
426 _('Register'), _('Create an account'), false, 'nav_register');
428 $this->menuItem(common_local_url('openidlogin'),
429 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
431 $this->menuItem(common_local_url('doc', array('title' => 'help')),
432 _('Help'), _('Help me!'), false, 'nav_help');
433 Event::handle('EndPrimaryNav', array($this));
435 $this->elementEnd('ul');
436 $this->elementEnd('dd');
437 $this->elementEnd('dl');
445 function showSiteNotice()
447 // Revist. Should probably do an hAtom pattern here
448 $text = common_config('site', 'notice');
450 $this->elementStart('dl', array('id' => 'site_notice',
451 'class' => 'system_notice'));
452 $this->element('dt', null, _('Site notice'));
453 $this->elementStart('dd', null);
455 $this->elementEnd('dd');
456 $this->elementEnd('dl');
463 * MAY overload if no notice form needed... or direct message box????
467 function showNoticeForm()
469 $notice_form = new NoticeForm($this);
470 $notice_form->show();
474 * Show anonymous message.
480 function showAnonymousMessage()
482 // needs to be defined by the class
488 * Shows local navigation, content block and aside.
494 $this->elementStart('div', array('id' => 'core'));
495 if (Event::handle('StartShowLocalNavBlock', array($this))) {
496 $this->showLocalNavBlock();
497 Event::handle('EndShowLocalNavBlock', array($this));
499 if (Event::handle('StartShowContentBlock', array($this))) {
500 $this->showContentBlock();
501 Event::handle('EndShowContentBlock', array($this));
504 $this->elementEnd('div');
508 * Show local navigation block.
512 function showLocalNavBlock()
514 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
515 $this->element('dt', null, _('Local views'));
516 $this->elementStart('dd');
517 $this->showLocalNav();
518 $this->elementEnd('dd');
519 $this->elementEnd('dl');
523 * Show local navigation.
529 function showLocalNav()
531 // does nothing by default
535 * Show content block.
539 function showContentBlock()
541 $this->elementStart('div', array('id' => 'content'));
542 $this->showPageTitle();
543 $this->showPageNoticeBlock();
544 $this->elementStart('div', array('id' => 'content_inner'));
545 // show the actual content (forms, lists, whatever)
546 $this->showContent();
547 $this->elementEnd('div');
548 $this->elementEnd('div');
556 function showPageTitle()
558 $this->element('h1', null, $this->title());
562 * Show page notice block.
566 function showPageNoticeBlock()
568 $this->elementStart('dl', array('id' => 'page_notice',
569 'class' => 'system_notice'));
570 $this->element('dt', null, _('Page notice'));
571 $this->elementStart('dd');
572 $this->showPageNotice();
573 $this->elementEnd('dd');
574 $this->elementEnd('dl');
580 * SHOULD overload (unless there's not a notice)
584 function showPageNotice()
591 * MUST overload (unless there's not a notice)
595 function showContent()
607 $this->elementStart('div', array('id' => 'aside_primary',
608 'class' => 'aside'));
609 $this->showExportData();
610 if (Event::handle('StartShowSections', array($this))) {
611 $this->showSections();
612 Event::handle('EndShowSections', array($this));
614 $this->elementEnd('div');
618 * Show export data feeds.
623 function showExportData()
625 $feeds = $this->getFeeds();
627 $fl = new FeedList($this);
639 function showSections()
641 // for each section, show it
649 function showFooter()
651 $this->elementStart('div', array('id' => 'footer'));
652 $this->showSecondaryNav();
653 $this->showLicenses();
654 $this->elementEnd('div');
658 * Show secondary navigation.
662 function showSecondaryNav()
664 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
665 $this->element('dt', null, _('Secondary site navigation'));
666 $this->elementStart('dd', null);
667 $this->elementStart('ul', array('class' => 'nav'));
668 if (Event::handle('StartSecondaryNav', array($this))) {
669 $this->menuItem(common_local_url('doc', array('title' => 'help')),
671 $this->menuItem(common_local_url('doc', array('title' => 'about')),
673 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
675 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
677 $this->menuItem(common_local_url('doc', array('title' => 'source')),
679 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
681 $this->menuItem(common_local_url('doc', array('title' => 'badge')),
683 Event::handle('EndSecondaryNav', array($this));
685 $this->elementEnd('ul');
686 $this->elementEnd('dd');
687 $this->elementEnd('dl');
695 function showLicenses()
697 $this->elementStart('dl', array('id' => 'licenses'));
698 $this->showLaconicaLicense();
699 $this->showContentLicense();
700 $this->elementEnd('dl');
704 * Show Laconica license.
708 function showLaconicaLicense()
710 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
711 $this->elementStart('dd', null);
712 if (common_config('site', 'broughtby')) {
713 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
715 $instr = _('**%%site.name%%** is a microblogging service. ');
717 $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);
718 $output = common_markup_to_html($instr);
720 $this->elementEnd('dd');
725 * Show content license.
729 function showContentLicense()
731 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
732 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
733 $this->elementStart('p');
734 $this->element('img', array('id' => 'license_cc',
735 'src' => common_config('license', 'image'),
736 'alt' => common_config('license', 'title')));
737 //TODO: This is dirty: i18n
738 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
739 $this->element('a', array('class' => 'license',
740 'rel' => 'external license',
741 'href' => common_config('license', 'url')),
742 common_config('license', 'title'));
743 $this->text(_('license.'));
744 $this->elementEnd('p');
745 $this->elementEnd('dd');
749 * Return last modified, if applicable.
753 * @return string last modified http header
755 function lastModified()
757 // For comparison with If-Last-Modified
758 // If not applicable, return null
763 * Return etag, if applicable.
767 * @return string etag http header
775 * Return true if read only.
779 * @return boolean is read only action?
781 function isReadOnly()
787 * Returns query argument or default value if not found
789 * @param string $key requested argument
790 * @param string $def default value to return if $key is not provided
792 * @return boolean is read only action?
794 function arg($key, $def=null)
796 if (array_key_exists($key, $this->args)) {
797 return $this->args[$key];
804 * Returns trimmed 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 trimmed($key, $def=null)
813 $arg = $this->arg($key, $def);
814 return is_string($arg) ? trim($arg) : $arg;
820 * @param array $argarray is ignored since it's now passed in in prepare()
822 * @return boolean is read only action?
824 function handle($argarray=null)
826 $lm = $this->lastModified();
827 $etag = $this->etag();
829 header('ETag: ' . $etag);
832 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
833 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
834 $ims = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
836 $if_none_match = $_SERVER['HTTP_IF_NONE_MATCH'];
837 if (!$if_none_match ||
839 $this->_hasEtag($etag, $if_none_match)) {
840 header('HTTP/1.1 304 Not Modified');
841 // Better way to do this?
850 * HasĀ etag? (private)
852 * @param string $etag etag http header
853 * @param string $if_none_match ifNoneMatch http header
858 function _hasEtag($etag, $if_none_match)
860 $etags = explode(',', $if_none_match);
861 return in_array($etag, $etags) || in_array('*', $etags);
865 * Boolean understands english (yes, no, true, false)
867 * @param string $key query key we're interested in
868 * @param string $def default value
870 * @return boolean interprets yes/no strings as boolean
872 function boolean($key, $def=false)
874 $arg = strtolower($this->trimmed($key));
878 } else if (in_array($arg, array('true', 'yes', '1'))) {
880 } else if (in_array($arg, array('false', 'no', '0'))) {
890 * @param string $msg error message to display
891 * @param integer $code http error code, 500 by default
896 function serverError($msg, $code=500)
898 $action = $this->trimmed('action');
899 common_debug("Server error '$code' on '$action': $msg", __FILE__);
900 throw new ServerException($msg, $code);
906 * @param string $msg error message to display
907 * @param integer $code http error code, 400 by default
912 function clientError($msg, $code=400)
914 $action = $this->trimmed('action');
915 common_debug("User error '$code' on '$action': $msg", __FILE__);
916 throw new ClientException($msg, $code);
920 * Returns the current URL
922 * @return string current URL
926 $action = $this->trimmed('action');
928 unset($args['action']);
929 foreach (array_keys($_COOKIE) as $cookie) {
930 unset($args[$cookie]);
932 return common_local_url($action, $args);
936 * Generate a menu item
938 * @param string $url menu URL
939 * @param string $text menu name
940 * @param string $title title attribute, null by default
941 * @param boolean $is_selected current menu item, false by default
942 * @param string $id element id, null by default
946 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
948 // Added @id to li for some control.
949 // XXX: We might want to move this to htmloutputter.php
952 $lattrs['class'] = 'current';
955 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
957 $this->elementStart('li', $lattrs);
958 $attrs['href'] = $url;
960 $attrs['title'] = $title;
962 $this->element('a', $attrs, $text);
963 $this->elementEnd('li');
967 * Generate pagination links
969 * @param boolean $have_before is there something before?
970 * @param boolean $have_after is there something after?
971 * @param integer $page current page
972 * @param string $action current action
973 * @param array $args rest of query arguments
977 function pagination($have_before, $have_after, $page, $action, $args=null)
979 // Does a little before-after block for next/prev page
980 if ($have_before || $have_after) {
981 $this->elementStart('div', array('class' => 'pagination'));
982 $this->elementStart('dl', null);
983 $this->element('dt', null, _('Pagination'));
984 $this->elementStart('dd', null);
985 $this->elementStart('ul', array('class' => 'nav'));
988 $pargs = array('page' => $page-1);
989 $this->elementStart('li', array('class' => 'nav_prev'));
990 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
993 $this->elementEnd('li');
996 $pargs = array('page' => $page+1);
997 $this->elementStart('li', array('class' => 'nav_next'));
998 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
1001 $this->elementEnd('li');
1003 if ($have_before || $have_after) {
1004 $this->elementEnd('ul');
1005 $this->elementEnd('dd');
1006 $this->elementEnd('dl');
1007 $this->elementEnd('div');
1012 * An array of feeds for this action.
1014 * Returns an array of potential feeds for this action.
1016 * @return array Feed object to show in head and links