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.
103 * Show head, a template method.
109 // XXX: attributes (profile?)
110 $this->elementStart('head');
112 $this->showStylesheets();
113 $this->showScripts();
114 $this->showOpenSearch();
116 $this->showDescription();
118 $this->elementEnd('head');
122 * Show title, a template method.
128 $this->element('title', null,
129 sprintf(_("%s - %s"),
131 common_config('site', 'name')));
135 * Returns the page title
139 * @return string page title
144 return _("Untitled page");
152 function showStylesheets()
154 if (Event::handle('StartShowStyles', array($this))) {
155 if (Event::handle('StartShowLaconicaStyles', array($this))) {
156 $this->element('link', array('rel' => 'stylesheet',
157 'type' => 'text/css',
158 'href' => theme_path('css/display.css', 'base') . '?version=' . LACONICA_VERSION,
159 'media' => 'screen, projection, tv'));
160 $this->element('link', array('rel' => 'stylesheet',
161 'type' => 'text/css',
162 'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
163 'media' => 'screen, projection, tv'));
164 Event::handle('EndShowLaconicaStyles', array($this));
166 if (Event::handle('StartShowUAStyles', array($this))) {
167 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
168 'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
169 foreach (array(6,7) as $ver) {
170 if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
171 // Yes, IE people should be put in jail.
172 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
173 'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
176 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
177 'href="'.theme_path('css/ie.css', null).'?version='.LACONICA_VERSION.'" /><![endif]');
178 Event::handle('EndShowUAStyles', array($this));
180 Event::handle('EndShowStyles', array($this));
185 * Show javascript headers
189 function showScripts()
191 if (Event::handle('StartShowScripts', array($this))) {
192 if (Event::handle('StartShowJQueryScripts', array($this))) {
193 $this->element('script', array('type' => 'text/javascript',
194 'src' => common_path('js/jquery.min.js')),
196 $this->element('script', array('type' => 'text/javascript',
197 'src' => common_path('js/jquery.form.js')),
199 Event::handle('EndShowJQueryScripts', array($this));
201 if (Event::handle('StartShowLaconicaScripts', array($this))) {
202 $this->element('script', array('type' => 'text/javascript',
203 'src' => common_path('js/xbImportNode.js')),
205 $this->element('script', array('type' => 'text/javascript',
206 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
208 Event::handle('EndShowLaconicaScripts', array($this));
210 Event::handle('EndShowScripts', array($this));
215 * Show OpenSearch headers
219 function showOpenSearch()
221 $this->element('link', array('rel' => 'search',
222 'type' => 'application/opensearchdescription+xml',
223 'href' => common_local_url('opensearch', array('type' => 'people')),
224 'title' => common_config('site', 'name').' People Search'));
225 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
226 'href' => common_local_url('opensearch', array('type' => 'notice')),
227 'title' => common_config('site', 'name').' Notice Search'));
240 $feeds = $this->getFeeds();
243 foreach ($feeds as $feed) {
244 $this->element('link', array('rel' => $feed->rel(),
245 'href' => $feed->url,
246 'type' => $feed->mimeType(),
247 'title' => $feed->title));
259 function showDescription()
261 // does nothing by default
265 * Show extra stuff in <head>.
273 // does nothing by default
279 * Calls template methods
285 $this->elementStart('body', array('id' => $this->trimmed('action')));
286 $this->elementStart('div', array('id' => 'wrap'));
287 if (Event::handle('StartShowHeader', array($this))) {
289 Event::handle('EndShowHeader', array($this));
292 if (Event::handle('StartShowFooter', array($this))) {
294 Event::handle('EndShowFooter', array($this));
296 $this->elementEnd('div');
297 $this->elementEnd('body');
301 * Show header of the page.
303 * Calls template methods
307 function showHeader()
309 $this->elementStart('div', array('id' => 'header'));
311 $this->showPrimaryNav();
312 $this->showSiteNotice();
313 if (common_logged_in()) {
314 $this->showNoticeForm();
316 $this->showAnonymousMessage();
318 $this->elementEnd('div');
322 * Show configured logo.
328 $this->elementStart('address', array('id' => 'site_contact',
329 'class' => 'vcard'));
330 $this->elementStart('a', array('class' => 'url home bookmark',
331 'href' => common_local_url('public')));
332 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
333 $this->element('img', array('class' => 'logo photo',
334 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
335 'alt' => common_config('site', 'name')));
337 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
338 $this->elementEnd('a');
339 $this->elementEnd('address');
343 * Show primary navigation.
347 function showPrimaryNav()
349 $user = common_current_user();
351 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
352 $this->element('dt', null, _('Primary site navigation'));
353 $this->elementStart('dd');
354 $this->elementStart('ul', array('class' => 'nav'));
355 if (Event::handle('StartPrimaryNav', array($this))) {
357 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
358 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
360 $this->menuItem(common_local_url('peoplesearch'),
361 _('Search'), _('Search for people or text'), false, 'nav_search');
363 $this->menuItem(common_local_url('profilesettings'),
364 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
366 if (common_config('xmpp', 'enabled')) {
367 $this->menuItem(common_local_url('imsettings'),
368 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
370 $this->menuItem(common_local_url('smssettings'),
371 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
373 $this->menuItem(common_local_url('logout'),
374 _('Logout'), _('Logout from the site'), false, 'nav_logout');
376 $this->menuItem(common_local_url('login'),
377 _('Login'), _('Login to the site'), false, 'nav_login');
378 if (!common_config('site', 'closed')) {
379 $this->menuItem(common_local_url('register'),
380 _('Register'), _('Create an account'), false, 'nav_register');
382 $this->menuItem(common_local_url('openidlogin'),
383 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
385 $this->menuItem(common_local_url('doc', array('title' => 'help')),
386 _('Help'), _('Help me!'), false, 'nav_help');
387 Event::handle('EndPrimaryNav', array($this));
389 $this->elementEnd('ul');
390 $this->elementEnd('dd');
391 $this->elementEnd('dl');
399 function showSiteNotice()
401 // Revist. Should probably do an hAtom pattern here
402 $text = common_config('site', 'notice');
404 $this->elementStart('dl', array('id' => 'site_notice',
405 'class' => 'system_notice'));
406 $this->element('dt', null, _('Site notice'));
407 $this->elementStart('dd', null);
409 $this->elementEnd('dd');
410 $this->elementEnd('dl');
417 * MAY overload if no notice form needed... or direct message box????
421 function showNoticeForm()
423 $notice_form = new NoticeForm($this);
424 $notice_form->show();
428 * Show anonymous message.
434 function showAnonymousMessage()
436 // needs to be defined by the class
442 * Shows local navigation, content block and aside.
448 $this->elementStart('div', array('id' => 'core'));
449 $this->showLocalNavBlock();
450 if (Event::handle('StartShowContentBlock', array($this))) {
451 $this->showContentBlock();
452 Event::handle('EndShowContentBlock', array($this));
455 $this->elementEnd('div');
459 * Show local navigation block.
463 function showLocalNavBlock()
465 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
466 $this->element('dt', null, _('Local views'));
467 $this->elementStart('dd');
468 $this->showLocalNav();
469 $this->elementEnd('dd');
470 $this->elementEnd('dl');
474 * Show local navigation.
480 function showLocalNav()
482 // does nothing by default
486 * Show content block.
490 function showContentBlock()
492 $this->elementStart('div', array('id' => 'content'));
493 $this->showPageTitle();
494 $this->showPageNoticeBlock();
495 $this->elementStart('div', array('id' => 'content_inner'));
496 // show the actual content (forms, lists, whatever)
497 $this->showContent();
498 $this->elementEnd('div');
499 $this->elementEnd('div');
507 function showPageTitle()
509 $this->element('h1', null, $this->title());
513 * Show page notice block.
517 function showPageNoticeBlock()
519 $this->elementStart('dl', array('id' => 'page_notice',
520 'class' => 'system_notice'));
521 $this->element('dt', null, _('Page notice'));
522 $this->elementStart('dd');
523 $this->showPageNotice();
524 $this->elementEnd('dd');
525 $this->elementEnd('dl');
531 * SHOULD overload (unless there's not a notice)
535 function showPageNotice()
542 * MUST overload (unless there's not a notice)
546 function showContent()
558 $this->elementStart('div', array('id' => 'aside_primary',
559 'class' => 'aside'));
560 $this->showExportData();
561 if (Event::handle('StartShowSections', array($this))) {
562 $this->showSections();
563 Event::handle('EndShowSections', array($this));
565 $this->elementEnd('div');
569 * Show export data feeds.
574 function showExportData()
576 $feeds = $this->getFeeds();
578 $fl = new FeedList($this);
590 function showSections()
592 // for each section, show it
600 function showFooter()
602 $this->elementStart('div', array('id' => 'footer'));
603 $this->showSecondaryNav();
604 $this->showLicenses();
605 $this->elementEnd('div');
609 * Show secondary navigation.
613 function showSecondaryNav()
615 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
616 $this->element('dt', null, _('Secondary site navigation'));
617 $this->elementStart('dd', null);
618 $this->elementStart('ul', array('class' => 'nav'));
619 if (Event::handle('StartSecondaryNav', array($this))) {
620 $this->menuItem(common_local_url('doc', array('title' => 'help')),
622 $this->menuItem(common_local_url('doc', array('title' => 'about')),
624 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
626 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
628 $this->menuItem(common_local_url('doc', array('title' => 'source')),
630 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
632 Event::handle('EndSecondaryNav', array($this));
634 $this->elementEnd('ul');
635 $this->elementEnd('dd');
636 $this->elementEnd('dl');
644 function showLicenses()
646 $this->elementStart('dl', array('id' => 'licenses'));
647 $this->showLaconicaLicense();
648 $this->showContentLicense();
649 $this->elementEnd('dl');
653 * Show Laconica license.
657 function showLaconicaLicense()
659 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
660 $this->elementStart('dd', null);
661 if (common_config('site', 'broughtby')) {
662 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
664 $instr = _('**%%site.name%%** is a microblogging service. ');
666 $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);
667 $output = common_markup_to_html($instr);
669 $this->elementEnd('dd');
674 * Show content license.
678 function showContentLicense()
680 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
681 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
682 $this->elementStart('p');
683 $this->element('img', array('id' => 'license_cc',
684 'src' => common_config('license', 'image'),
685 'alt' => common_config('license', 'title')));
686 //TODO: This is dirty: i18n
687 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
688 $this->element('a', array('class' => 'license',
689 'rel' => 'external license',
690 'href' => common_config('license', 'url')),
691 common_config('license', 'title'));
692 $this->text(_('license.'));
693 $this->elementEnd('p');
694 $this->elementEnd('dd');
698 * Return last modified, if applicable.
702 * @return string last modified http header
704 function lastModified()
706 // For comparison with If-Last-Modified
707 // If not applicable, return null
712 * Return etag, if applicable.
716 * @return string etag http header
724 * Return true if read only.
728 * @return boolean is read only action?
730 function isReadOnly()
736 * Returns query argument or default value if not found
738 * @param string $key requested argument
739 * @param string $def default value to return if $key is not provided
741 * @return boolean is read only action?
743 function arg($key, $def=null)
745 if (array_key_exists($key, $this->args)) {
746 return $this->args[$key];
753 * Returns trimmed query argument or default value if not found
755 * @param string $key requested argument
756 * @param string $def default value to return if $key is not provided
758 * @return boolean is read only action?
760 function trimmed($key, $def=null)
762 $arg = $this->arg($key, $def);
763 return is_string($arg) ? trim($arg) : $arg;
769 * @param array $argarray is ignored since it's now passed in in prepare()
771 * @return boolean is read only action?
773 function handle($argarray=null)
775 $lm = $this->lastModified();
776 $etag = $this->etag();
778 header('ETag: ' . $etag);
781 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
782 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
783 if ($if_modified_since) {
784 $ims = strtotime($if_modified_since);
787 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
788 header('HTTP/1.1 304 Not Modified');
789 // Better way to do this?
798 * HasĀ etag? (private)
800 * @param string $etag etag http header
801 * @param string $if_none_match ifNoneMatch http header
805 function _hasEtag($etag, $if_none_match)
807 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
811 * Boolean understands english (yes, no, true, false)
813 * @param string $key query key we're interested in
814 * @param string $def default value
816 * @return boolean interprets yes/no strings as boolean
818 function boolean($key, $def=false)
820 $arg = strtolower($this->trimmed($key));
824 } else if (in_array($arg, array('true', 'yes', '1'))) {
826 } else if (in_array($arg, array('false', 'no', '0'))) {
836 * @param string $msg error message to display
837 * @param integer $code http error code, 500 by default
842 function serverError($msg, $code=500)
844 $action = $this->trimmed('action');
845 common_debug("Server error '$code' on '$action': $msg", __FILE__);
846 throw new ServerException($msg, $code);
852 * @param string $msg error message to display
853 * @param integer $code http error code, 400 by default
858 function clientError($msg, $code=400)
860 $action = $this->trimmed('action');
861 common_debug("User error '$code' on '$action': $msg", __FILE__);
862 throw new ClientException($msg, $code);
866 * Returns the current URL
868 * @return string current URL
872 $action = $this->trimmed('action');
874 unset($args['action']);
875 foreach (array_keys($_COOKIE) as $cookie) {
876 unset($args[$cookie]);
878 return common_local_url($action, $args);
882 * Generate a menu item
884 * @param string $url menu URL
885 * @param string $text menu name
886 * @param string $title title attribute, null by default
887 * @param boolean $is_selected current menu item, false by default
888 * @param string $id element id, null by default
892 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
894 // Added @id to li for some control.
895 // XXX: We might want to move this to htmloutputter.php
898 $lattrs['class'] = 'current';
901 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
903 $this->elementStart('li', $lattrs);
904 $attrs['href'] = $url;
906 $attrs['title'] = $title;
908 $this->element('a', $attrs, $text);
909 $this->elementEnd('li');
913 * Generate pagination links
915 * @param boolean $have_before is there something before?
916 * @param boolean $have_after is there something after?
917 * @param integer $page current page
918 * @param string $action current action
919 * @param array $args rest of query arguments
923 function pagination($have_before, $have_after, $page, $action, $args=null)
925 // Does a little before-after block for next/prev page
926 if ($have_before || $have_after) {
927 $this->elementStart('div', array('class' => 'pagination'));
928 $this->elementStart('dl', null);
929 $this->element('dt', null, _('Pagination'));
930 $this->elementStart('dd', null);
931 $this->elementStart('ul', array('class' => 'nav'));
934 $pargs = array('page' => $page-1);
935 $newargs = $args ? array_merge($args, $pargs) : $pargs;
936 $this->elementStart('li', array('class' => 'nav_prev'));
937 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
939 $this->elementEnd('li');
942 $pargs = array('page' => $page+1);
943 $newargs = $args ? array_merge($args, $pargs) : $pargs;
944 $this->elementStart('li', array('class' => 'nav_next'));
945 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
947 $this->elementEnd('li');
949 if ($have_before || $have_after) {
950 $this->elementEnd('ul');
951 $this->elementEnd('dd');
952 $this->elementEnd('dl');
953 $this->elementEnd('div');
958 * An array of feeds for this action.
960 * Returns an array of potential feeds for this action.
962 * @return array Feed object to show in head and links