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->showStylesheets();
125 $this->showScripts();
126 $this->showOpenSearch();
128 $this->showDescription();
130 $this->elementEnd('head');
134 * Show title, a template method.
140 $this->element('title', null,
141 sprintf(_("%s - %s"),
143 common_config('site', 'name')));
147 * Returns the page title
151 * @return string page title
156 return _("Untitled page");
164 function showStylesheets()
166 if (Event::handle('StartShowStyles', array($this))) {
167 if (Event::handle('StartShowLaconicaStyles', array($this))) {
168 $this->element('link', array('rel' => 'stylesheet',
169 'type' => 'text/css',
170 'href' => theme_path('css/display.css', 'base') . '?version=' . LACONICA_VERSION,
171 'media' => 'screen, projection, tv'));
172 $this->element('link', array('rel' => 'stylesheet',
173 'type' => 'text/css',
174 'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
175 'media' => 'screen, projection, tv'));
176 if (common_config('site', 'mobile')) {
177 $this->element('link', array('rel' => 'stylesheet',
178 'type' => 'text/css',
179 'href' => theme_path('css/mobile.css', 'base') . '?version=' . LACONICA_VERSION,
180 // TODO: "handheld" CSS for other mobile devices
181 'media' => 'only screen and (max-device-width: 480px)')); // Mobile WebKit
183 $this->element('link', array('rel' => 'stylesheet',
184 'type' => 'text/css',
185 'href' => theme_path('css/print.css', 'base') . '?version=' . LACONICA_VERSION,
186 'media' => 'print'));
187 Event::handle('EndShowLaconicaStyles', array($this));
189 if (Event::handle('StartShowUAStyles', array($this))) {
190 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
191 'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
192 foreach (array(6,7) as $ver) {
193 if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
194 // Yes, IE people should be put in jail.
195 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
196 'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
199 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
200 'href="'.theme_path('css/ie.css', null).'?version='.LACONICA_VERSION.'" /><![endif]');
201 Event::handle('EndShowUAStyles', array($this));
203 Event::handle('EndShowStyles', array($this));
208 * Show javascript headers
212 function showScripts()
214 if (Event::handle('StartShowScripts', array($this))) {
215 if (Event::handle('StartShowJQueryScripts', array($this))) {
216 $this->element('script', array('type' => 'text/javascript',
217 'src' => common_path('js/jquery.min.js')),
219 $this->element('script', array('type' => 'text/javascript',
220 'src' => common_path('js/jquery.form.js')),
222 Event::handle('EndShowJQueryScripts', array($this));
224 if (Event::handle('StartShowLaconicaScripts', array($this))) {
225 $this->element('script', array('type' => 'text/javascript',
226 'src' => common_path('js/xbImportNode.js')),
228 $this->element('script', array('type' => 'text/javascript',
229 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
231 // Frame-busting code to avoid clickjacking attacks.
232 $this->element('script', array('type' => 'text/javascript'),
233 'if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
234 Event::handle('EndShowLaconicaScripts', array($this));
236 Event::handle('EndShowScripts', array($this));
241 * Show OpenSearch headers
245 function showOpenSearch()
247 $this->element('link', array('rel' => 'search',
248 'type' => 'application/opensearchdescription+xml',
249 'href' => common_local_url('opensearch', array('type' => 'people')),
250 'title' => common_config('site', 'name').' People Search'));
251 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
252 'href' => common_local_url('opensearch', array('type' => 'notice')),
253 'title' => common_config('site', 'name').' Notice Search'));
266 $feeds = $this->getFeeds();
269 foreach ($feeds as $feed) {
270 $this->element('link', array('rel' => $feed->rel(),
271 'href' => $feed->url,
272 'type' => $feed->mimeType(),
273 'title' => $feed->title));
285 function showDescription()
287 // does nothing by default
291 * Show extra stuff in <head>.
299 // does nothing by default
305 * Calls template methods
311 $this->elementStart('body', array('id' => $this->trimmed('action')));
312 $this->elementStart('div', array('id' => 'wrap'));
313 if (Event::handle('StartShowHeader', array($this))) {
315 Event::handle('EndShowHeader', array($this));
318 if (Event::handle('StartShowFooter', array($this))) {
320 Event::handle('EndShowFooter', array($this));
322 $this->elementEnd('div');
323 $this->elementEnd('body');
327 * Show header of the page.
329 * Calls template methods
333 function showHeader()
335 $this->elementStart('div', array('id' => 'header'));
337 $this->showPrimaryNav();
338 $this->showSiteNotice();
339 if (common_logged_in()) {
340 $this->showNoticeForm();
342 $this->showAnonymousMessage();
344 $this->elementEnd('div');
348 * Show configured logo.
354 $this->elementStart('address', array('id' => 'site_contact',
355 'class' => 'vcard'));
356 $this->elementStart('a', array('class' => 'url home bookmark',
357 'href' => common_local_url('public')));
358 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
359 $this->element('img', array('class' => 'logo photo',
360 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
361 'alt' => common_config('site', 'name')));
363 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
364 $this->elementEnd('a');
365 $this->elementEnd('address');
369 * Show primary navigation.
373 function showPrimaryNav()
375 $user = common_current_user();
377 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
378 $this->element('dt', null, _('Primary site navigation'));
379 $this->elementStart('dd');
380 $this->elementStart('ul', array('class' => 'nav'));
381 if (Event::handle('StartPrimaryNav', array($this))) {
383 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
384 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
386 $this->menuItem(common_local_url('peoplesearch'),
387 _('Search'), _('Search for people or text'), false, 'nav_search');
389 $this->menuItem(common_local_url('profilesettings'),
390 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
392 if (common_config('xmpp', 'enabled')) {
393 $this->menuItem(common_local_url('imsettings'),
394 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
396 $this->menuItem(common_local_url('smssettings'),
397 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
399 $this->menuItem(common_local_url('logout'),
400 _('Logout'), _('Logout from the site'), false, 'nav_logout');
402 $this->menuItem(common_local_url('login'),
403 _('Login'), _('Login to the site'), false, 'nav_login');
404 if (!common_config('site', 'closed')) {
405 $this->menuItem(common_local_url('register'),
406 _('Register'), _('Create an account'), false, 'nav_register');
408 $this->menuItem(common_local_url('openidlogin'),
409 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
411 $this->menuItem(common_local_url('doc', array('title' => 'help')),
412 _('Help'), _('Help me!'), false, 'nav_help');
413 Event::handle('EndPrimaryNav', array($this));
415 $this->elementEnd('ul');
416 $this->elementEnd('dd');
417 $this->elementEnd('dl');
425 function showSiteNotice()
427 // Revist. Should probably do an hAtom pattern here
428 $text = common_config('site', 'notice');
430 $this->elementStart('dl', array('id' => 'site_notice',
431 'class' => 'system_notice'));
432 $this->element('dt', null, _('Site notice'));
433 $this->elementStart('dd', null);
435 $this->elementEnd('dd');
436 $this->elementEnd('dl');
443 * MAY overload if no notice form needed... or direct message box????
447 function showNoticeForm()
449 $notice_form = new NoticeForm($this);
450 $notice_form->show();
454 * Show anonymous message.
460 function showAnonymousMessage()
462 // needs to be defined by the class
468 * Shows local navigation, content block and aside.
474 $this->elementStart('div', array('id' => 'core'));
475 if (Event::handle('StartShowLocalNavBlock', array($this))) {
476 $this->showLocalNavBlock();
477 Event::handle('EndShowLocalNavBlock', array($this));
479 if (Event::handle('StartShowContentBlock', array($this))) {
480 $this->showContentBlock();
481 Event::handle('EndShowContentBlock', array($this));
484 $this->elementEnd('div');
488 * Show local navigation block.
492 function showLocalNavBlock()
494 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
495 $this->element('dt', null, _('Local views'));
496 $this->elementStart('dd');
497 $this->showLocalNav();
498 $this->elementEnd('dd');
499 $this->elementEnd('dl');
503 * Show local navigation.
509 function showLocalNav()
511 // does nothing by default
515 * Show content block.
519 function showContentBlock()
521 $this->elementStart('div', array('id' => 'content'));
522 $this->showPageTitle();
523 $this->showPageNoticeBlock();
524 $this->elementStart('div', array('id' => 'content_inner'));
525 // show the actual content (forms, lists, whatever)
526 $this->showContent();
527 $this->elementEnd('div');
528 $this->elementEnd('div');
536 function showPageTitle()
538 $this->element('h1', null, $this->title());
542 * Show page notice block.
546 function showPageNoticeBlock()
548 $this->elementStart('dl', array('id' => 'page_notice',
549 'class' => 'system_notice'));
550 $this->element('dt', null, _('Page notice'));
551 $this->elementStart('dd');
552 $this->showPageNotice();
553 $this->elementEnd('dd');
554 $this->elementEnd('dl');
560 * SHOULD overload (unless there's not a notice)
564 function showPageNotice()
571 * MUST overload (unless there's not a notice)
575 function showContent()
587 $this->elementStart('div', array('id' => 'aside_primary',
588 'class' => 'aside'));
589 if (Event::handle('StartShowExportData', array($this))) {
590 $this->showExportData();
591 Event::handle('EndShowExportData', array($this));
593 if (Event::handle('StartShowSections', array($this))) {
594 $this->showSections();
595 Event::handle('EndShowSections', array($this));
597 $this->elementEnd('div');
601 * Show export data feeds.
606 function showExportData()
608 $feeds = $this->getFeeds();
610 $fl = new FeedList($this);
622 function showSections()
624 // for each section, show it
632 function showFooter()
634 $this->elementStart('div', array('id' => 'footer'));
635 $this->showSecondaryNav();
636 $this->showLicenses();
637 $this->elementEnd('div');
641 * Show secondary navigation.
645 function showSecondaryNav()
647 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
648 $this->element('dt', null, _('Secondary site navigation'));
649 $this->elementStart('dd', null);
650 $this->elementStart('ul', array('class' => 'nav'));
651 if (Event::handle('StartSecondaryNav', array($this))) {
652 $this->menuItem(common_local_url('doc', array('title' => 'help')),
654 $this->menuItem(common_local_url('doc', array('title' => 'about')),
656 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
658 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
660 $this->menuItem(common_local_url('doc', array('title' => 'source')),
662 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
664 $this->menuItem(common_local_url('doc', array('title' => 'badge')),
666 Event::handle('EndSecondaryNav', array($this));
668 $this->elementEnd('ul');
669 $this->elementEnd('dd');
670 $this->elementEnd('dl');
678 function showLicenses()
680 $this->elementStart('dl', array('id' => 'licenses'));
681 $this->showLaconicaLicense();
682 $this->showContentLicense();
683 $this->elementEnd('dl');
687 * Show Laconica license.
691 function showLaconicaLicense()
693 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
694 $this->elementStart('dd', null);
695 if (common_config('site', 'broughtby')) {
696 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
698 $instr = _('**%%site.name%%** is a microblogging service. ');
700 $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);
701 $output = common_markup_to_html($instr);
703 $this->elementEnd('dd');
708 * Show content license.
712 function showContentLicense()
714 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
715 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
716 $this->elementStart('p');
717 $this->element('img', array('id' => 'license_cc',
718 'src' => common_config('license', 'image'),
719 'alt' => common_config('license', 'title')));
720 //TODO: This is dirty: i18n
721 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
722 $this->element('a', array('class' => 'license',
723 'rel' => 'external license',
724 'href' => common_config('license', 'url')),
725 common_config('license', 'title'));
726 $this->text(_('license.'));
727 $this->elementEnd('p');
728 $this->elementEnd('dd');
732 * Return last modified, if applicable.
736 * @return string last modified http header
738 function lastModified()
740 // For comparison with If-Last-Modified
741 // If not applicable, return null
746 * Return etag, if applicable.
750 * @return string etag http header
758 * Return true if read only.
762 * @return boolean is read only action?
764 function isReadOnly()
770 * Returns query argument or default value if not found
772 * @param string $key requested argument
773 * @param string $def default value to return if $key is not provided
775 * @return boolean is read only action?
777 function arg($key, $def=null)
779 if (array_key_exists($key, $this->args)) {
780 return $this->args[$key];
787 * Returns trimmed 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 trimmed($key, $def=null)
796 $arg = $this->arg($key, $def);
797 return is_string($arg) ? trim($arg) : $arg;
803 * @param array $argarray is ignored since it's now passed in in prepare()
805 * @return boolean is read only action?
807 function handle($argarray=null)
809 $lm = $this->lastModified();
810 $etag = $this->etag();
812 header('ETag: ' . $etag);
815 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
816 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
817 $ims = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
819 $if_none_match = $_SERVER['HTTP_IF_NONE_MATCH'];
820 if (!$if_none_match ||
822 $this->_hasEtag($etag, $if_none_match)) {
823 header('HTTP/1.1 304 Not Modified');
824 // Better way to do this?
833 * HasĀ etag? (private)
835 * @param string $etag etag http header
836 * @param string $if_none_match ifNoneMatch http header
841 function _hasEtag($etag, $if_none_match)
843 $etags = explode(',', $if_none_match);
844 return in_array($etag, $etags) || in_array('*', $etags);
848 * Boolean understands english (yes, no, true, false)
850 * @param string $key query key we're interested in
851 * @param string $def default value
853 * @return boolean interprets yes/no strings as boolean
855 function boolean($key, $def=false)
857 $arg = strtolower($this->trimmed($key));
861 } else if (in_array($arg, array('true', 'yes', '1'))) {
863 } else if (in_array($arg, array('false', 'no', '0'))) {
873 * @param string $msg error message to display
874 * @param integer $code http error code, 500 by default
879 function serverError($msg, $code=500)
881 $action = $this->trimmed('action');
882 common_debug("Server error '$code' on '$action': $msg", __FILE__);
883 throw new ServerException($msg, $code);
889 * @param string $msg error message to display
890 * @param integer $code http error code, 400 by default
895 function clientError($msg, $code=400)
897 $action = $this->trimmed('action');
898 common_debug("User error '$code' on '$action': $msg", __FILE__);
899 throw new ClientException($msg, $code);
903 * Returns the current URL
905 * @return string current URL
909 $action = $this->trimmed('action');
911 unset($args['action']);
912 foreach (array_keys($_COOKIE) as $cookie) {
913 unset($args[$cookie]);
915 return common_local_url($action, $args);
919 * Generate a menu item
921 * @param string $url menu URL
922 * @param string $text menu name
923 * @param string $title title attribute, null by default
924 * @param boolean $is_selected current menu item, false by default
925 * @param string $id element id, null by default
929 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
931 // Added @id to li for some control.
932 // XXX: We might want to move this to htmloutputter.php
935 $lattrs['class'] = 'current';
938 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
940 $this->elementStart('li', $lattrs);
941 $attrs['href'] = $url;
943 $attrs['title'] = $title;
945 $this->element('a', $attrs, $text);
946 $this->elementEnd('li');
950 * Generate pagination links
952 * @param boolean $have_before is there something before?
953 * @param boolean $have_after is there something after?
954 * @param integer $page current page
955 * @param string $action current action
956 * @param array $args rest of query arguments
960 function pagination($have_before, $have_after, $page, $action, $args=null)
962 // Does a little before-after block for next/prev page
963 if ($have_before || $have_after) {
964 $this->elementStart('div', array('class' => 'pagination'));
965 $this->elementStart('dl', null);
966 $this->element('dt', null, _('Pagination'));
967 $this->elementStart('dd', null);
968 $this->elementStart('ul', array('class' => 'nav'));
971 $pargs = array('page' => $page-1);
972 $this->elementStart('li', array('class' => 'nav_prev'));
973 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
976 $this->elementEnd('li');
979 $pargs = array('page' => $page+1);
980 $this->elementStart('li', array('class' => 'nav_next'));
981 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
984 $this->elementEnd('li');
986 if ($have_before || $have_after) {
987 $this->elementEnd('ul');
988 $this->elementEnd('dd');
989 $this->elementEnd('dl');
990 $this->elementEnd('div');
995 * An array of feeds for this action.
997 * Returns an array of potential feeds for this action.
999 * @return array Feed object to show in head and links