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 $this->element('link', array('rel' => 'stylesheet',
155 'type' => 'text/css',
156 'href' => theme_path('css/display.css', 'base') . '?version=' . LACONICA_VERSION,
157 'media' => 'screen, projection, tv'));
158 $this->element('link', array('rel' => 'stylesheet',
159 'type' => 'text/css',
160 'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
161 'media' => 'screen, projection, tv'));
162 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
163 'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
164 foreach (array(6,7) as $ver) {
165 if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
166 // Yes, IE people should be put in jail.
167 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
168 'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
171 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
172 'href="'.theme_path('css/ie.css', null).'?version='.LACONICA_VERSION.'" /><![endif]');
176 * Show javascript headers
180 function showScripts()
182 if (Event::handle('StartShowScripts', array($this))) {
183 if (Event::handle('StartShowJQueryScripts', array($this))) {
184 $this->element('script', array('type' => 'text/javascript',
185 'src' => common_path('js/jquery.min.js')),
187 $this->element('script', array('type' => 'text/javascript',
188 'src' => common_path('js/jquery.form.js')),
190 Event::handle('EndShowJQueryScripts', array($this));
192 if (Event::handle('StartShowLaconicaScripts', array($this))) {
193 $this->element('script', array('type' => 'text/javascript',
194 'src' => common_path('js/xbImportNode.js')),
196 $this->element('script', array('type' => 'text/javascript',
197 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
199 Event::handle('EndShowLaconicaScripts', array($this));
201 Event::handle('EndShowScripts', array($this));
206 * Show OpenSearch headers
210 function showOpenSearch()
212 $this->element('link', array('rel' => 'search',
213 'type' => 'application/opensearchdescription+xml',
214 'href' => common_local_url('opensearch', array('type' => 'people')),
215 'title' => common_config('site', 'name').' People Search'));
216 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
217 'href' => common_local_url('opensearch', array('type' => 'notice')),
218 'title' => common_config('site', 'name').' Notice Search'));
231 $feeds = $this->getFeeds();
234 foreach ($feeds as $feed) {
235 $this->element('link', array('rel' => $feed->rel(),
236 'href' => $feed->url,
237 'type' => $feed->mimeType(),
238 'title' => $feed->title));
250 function showDescription()
252 // does nothing by default
256 * Show extra stuff in <head>.
264 // does nothing by default
270 * Calls template methods
276 $this->elementStart('body', array('id' => $this->trimmed('action')));
277 $this->elementStart('div', array('id' => 'wrap'));
281 $this->elementEnd('div');
282 $this->elementEnd('body');
286 * Show header of the page.
288 * Calls template methods
292 function showHeader()
294 $this->elementStart('div', array('id' => 'header'));
296 $this->showPrimaryNav();
297 $this->showSiteNotice();
298 if (common_logged_in()) {
299 $this->showNoticeForm();
301 $this->showAnonymousMessage();
303 $this->elementEnd('div');
307 * Show configured logo.
313 $this->elementStart('address', array('id' => 'site_contact',
314 'class' => 'vcard'));
315 $this->elementStart('a', array('class' => 'url home bookmark',
316 'href' => common_local_url('public')));
317 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
318 $this->element('img', array('class' => 'logo photo',
319 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
320 'alt' => common_config('site', 'name')));
322 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
323 $this->elementEnd('a');
324 $this->elementEnd('address');
328 * Show primary navigation.
332 function showPrimaryNav()
334 $user = common_current_user();
336 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
337 $this->element('dt', null, _('Primary site navigation'));
338 $this->elementStart('dd');
339 $this->elementStart('ul', array('class' => 'nav'));
340 if (Event::handle('StartPrimaryNav', array($this))) {
342 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
343 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
345 $this->menuItem(common_local_url('peoplesearch'),
346 _('Search'), _('Search for people or text'), false, 'nav_search');
348 $this->menuItem(common_local_url('profilesettings'),
349 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
351 if (common_config('xmpp', 'enabled')) {
352 $this->menuItem(common_local_url('imsettings'),
353 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
355 $this->menuItem(common_local_url('smssettings'),
356 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
358 $this->menuItem(common_local_url('logout'),
359 _('Logout'), _('Logout from the site'), false, 'nav_logout');
361 $this->menuItem(common_local_url('login'),
362 _('Login'), _('Login to the site'), false, 'nav_login');
363 if (!common_config('site', 'closed')) {
364 $this->menuItem(common_local_url('register'),
365 _('Register'), _('Create an account'), false, 'nav_register');
367 $this->menuItem(common_local_url('openidlogin'),
368 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
370 $this->menuItem(common_local_url('doc', array('title' => 'help')),
371 _('Help'), _('Help me!'), false, 'nav_help');
372 Event::handle('EndPrimaryNav', array($this));
374 $this->elementEnd('ul');
375 $this->elementEnd('dd');
376 $this->elementEnd('dl');
384 function showSiteNotice()
386 // Revist. Should probably do an hAtom pattern here
387 $text = common_config('site', 'notice');
389 $this->elementStart('dl', array('id' => 'site_notice',
390 'class' => 'system_notice'));
391 $this->element('dt', null, _('Site notice'));
392 $this->elementStart('dd', null);
394 $this->elementEnd('dd');
395 $this->elementEnd('dl');
402 * MAY overload if no notice form needed... or direct message box????
406 function showNoticeForm()
408 $notice_form = new NoticeForm($this);
409 $notice_form->show();
413 * Show anonymous message.
419 function showAnonymousMessage()
421 // needs to be defined by the class
427 * Shows local navigation, content block and aside.
433 $this->elementStart('div', array('id' => 'core'));
434 $this->showLocalNavBlock();
435 $this->showContentBlock();
437 $this->elementEnd('div');
441 * Show local navigation block.
445 function showLocalNavBlock()
447 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
448 $this->element('dt', null, _('Local views'));
449 $this->elementStart('dd');
450 $this->showLocalNav();
451 $this->elementEnd('dd');
452 $this->elementEnd('dl');
456 * Show local navigation.
462 function showLocalNav()
464 // does nothing by default
468 * Show content block.
472 function showContentBlock()
474 $this->elementStart('div', array('id' => 'content'));
475 $this->showPageTitle();
476 $this->showPageNoticeBlock();
477 $this->elementStart('div', array('id' => 'content_inner'));
478 // show the actual content (forms, lists, whatever)
479 $this->showContent();
480 $this->elementEnd('div');
481 $this->elementEnd('div');
489 function showPageTitle()
491 $this->element('h1', null, $this->title());
495 * Show page notice block.
499 function showPageNoticeBlock()
501 $this->elementStart('dl', array('id' => 'page_notice',
502 'class' => 'system_notice'));
503 $this->element('dt', null, _('Page notice'));
504 $this->elementStart('dd');
505 $this->showPageNotice();
506 $this->elementEnd('dd');
507 $this->elementEnd('dl');
513 * SHOULD overload (unless there's not a notice)
517 function showPageNotice()
524 * MUST overload (unless there's not a notice)
528 function showContent()
540 $this->elementStart('div', array('id' => 'aside_primary',
541 'class' => 'aside'));
542 $this->showExportData();
543 if (Event::handle('StartShowSections', array($this))) {
544 $this->showSections();
545 Event::handle('EndShowSections', array($this));
547 $this->elementEnd('div');
551 * Show export data feeds.
556 function showExportData()
558 $feeds = $this->getFeeds();
560 $fl = new FeedList($this);
572 function showSections()
574 // for each section, show it
582 function showFooter()
584 $this->elementStart('div', array('id' => 'footer'));
585 $this->showSecondaryNav();
586 $this->showLicenses();
587 $this->elementEnd('div');
591 * Show secondary navigation.
595 function showSecondaryNav()
597 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
598 $this->element('dt', null, _('Secondary site navigation'));
599 $this->elementStart('dd', null);
600 $this->elementStart('ul', array('class' => 'nav'));
601 if (Event::handle('StartSecondaryNav', array($this))) {
602 $this->menuItem(common_local_url('doc', array('title' => 'help')),
604 $this->menuItem(common_local_url('doc', array('title' => 'about')),
606 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
608 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
610 $this->menuItem(common_local_url('doc', array('title' => 'source')),
612 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
614 Event::handle('EndSecondaryNav', array($this));
616 $this->elementEnd('ul');
617 $this->elementEnd('dd');
618 $this->elementEnd('dl');
626 function showLicenses()
628 $this->elementStart('dl', array('id' => 'licenses'));
629 $this->showLaconicaLicense();
630 $this->showContentLicense();
631 $this->elementEnd('dl');
635 * Show Laconica license.
639 function showLaconicaLicense()
641 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
642 $this->elementStart('dd', null);
643 if (common_config('site', 'broughtby')) {
644 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
646 $instr = _('**%%site.name%%** is a microblogging service. ');
648 $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);
649 $output = common_markup_to_html($instr);
651 $this->elementEnd('dd');
656 * Show content license.
660 function showContentLicense()
662 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
663 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
664 $this->elementStart('p');
665 $this->element('img', array('id' => 'license_cc',
666 'src' => common_config('license', 'image'),
667 'alt' => common_config('license', 'title')));
668 //TODO: This is dirty: i18n
669 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
670 $this->element('a', array('class' => 'license',
671 'rel' => 'external license',
672 'href' => common_config('license', 'url')),
673 common_config('license', 'title'));
674 $this->text(_('license.'));
675 $this->elementEnd('p');
676 $this->elementEnd('dd');
680 * Return last modified, if applicable.
684 * @return string last modified http header
686 function lastModified()
688 // For comparison with If-Last-Modified
689 // If not applicable, return null
694 * Return etag, if applicable.
698 * @return string etag http header
706 * Return true if read only.
710 * @return boolean is read only action?
712 function isReadOnly()
718 * Returns query argument or default value if not found
720 * @param string $key requested argument
721 * @param string $def default value to return if $key is not provided
723 * @return boolean is read only action?
725 function arg($key, $def=null)
727 if (array_key_exists($key, $this->args)) {
728 return $this->args[$key];
735 * Returns trimmed query argument or default value if not found
737 * @param string $key requested argument
738 * @param string $def default value to return if $key is not provided
740 * @return boolean is read only action?
742 function trimmed($key, $def=null)
744 $arg = $this->arg($key, $def);
745 return is_string($arg) ? trim($arg) : $arg;
751 * @param array $argarray is ignored since it's now passed in in prepare()
753 * @return boolean is read only action?
755 function handle($argarray=null)
757 $lm = $this->lastModified();
758 $etag = $this->etag();
760 header('ETag: ' . $etag);
763 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
764 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
765 if ($if_modified_since) {
766 $ims = strtotime($if_modified_since);
769 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
770 header('HTTP/1.1 304 Not Modified');
771 // Better way to do this?
780 * HasĀ etag? (private)
782 * @param string $etag etag http header
783 * @param string $if_none_match ifNoneMatch http header
787 function _hasEtag($etag, $if_none_match)
789 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
793 * Boolean understands english (yes, no, true, false)
795 * @param string $key query key we're interested in
796 * @param string $def default value
798 * @return boolean interprets yes/no strings as boolean
800 function boolean($key, $def=false)
802 $arg = strtolower($this->trimmed($key));
806 } else if (in_array($arg, array('true', 'yes', '1'))) {
808 } else if (in_array($arg, array('false', 'no', '0'))) {
818 * @param string $msg error message to display
819 * @param integer $code http error code, 500 by default
824 function serverError($msg, $code=500)
826 $action = $this->trimmed('action');
827 common_debug("Server error '$code' on '$action': $msg", __FILE__);
828 throw new ServerException($msg, $code);
834 * @param string $msg error message to display
835 * @param integer $code http error code, 400 by default
840 function clientError($msg, $code=400)
842 $action = $this->trimmed('action');
843 common_debug("User error '$code' on '$action': $msg", __FILE__);
844 throw new ClientException($msg, $code);
848 * Returns the current URL
850 * @return string current URL
854 $action = $this->trimmed('action');
856 unset($args['action']);
857 foreach (array_keys($_COOKIE) as $cookie) {
858 unset($args[$cookie]);
860 return common_local_url($action, $args);
864 * Generate a menu item
866 * @param string $url menu URL
867 * @param string $text menu name
868 * @param string $title title attribute, null by default
869 * @param boolean $is_selected current menu item, false by default
870 * @param string $id element id, null by default
874 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
876 // Added @id to li for some control.
877 // XXX: We might want to move this to htmloutputter.php
880 $lattrs['class'] = 'current';
883 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
885 $this->elementStart('li', $lattrs);
886 $attrs['href'] = $url;
888 $attrs['title'] = $title;
890 $this->element('a', $attrs, $text);
891 $this->elementEnd('li');
895 * Generate pagination links
897 * @param boolean $have_before is there something before?
898 * @param boolean $have_after is there something after?
899 * @param integer $page current page
900 * @param string $action current action
901 * @param array $args rest of query arguments
905 function pagination($have_before, $have_after, $page, $action, $args=null)
907 // Does a little before-after block for next/prev page
908 if ($have_before || $have_after) {
909 $this->elementStart('div', array('class' => 'pagination'));
910 $this->elementStart('dl', null);
911 $this->element('dt', null, _('Pagination'));
912 $this->elementStart('dd', null);
913 $this->elementStart('ul', array('class' => 'nav'));
916 $pargs = array('page' => $page-1);
917 $newargs = $args ? array_merge($args, $pargs) : $pargs;
918 $this->elementStart('li', array('class' => 'nav_prev'));
919 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
921 $this->elementEnd('li');
924 $pargs = array('page' => $page+1);
925 $newargs = $args ? array_merge($args, $pargs) : $pargs;
926 $this->elementStart('li', array('class' => 'nav_next'));
927 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
929 $this->elementEnd('li');
931 if ($have_before || $have_after) {
932 $this->elementEnd('ul');
933 $this->elementEnd('dd');
934 $this->elementEnd('dl');
935 $this->elementEnd('div');
940 * An array of feeds for this action.
942 * Returns an array of potential feeds for this action.
944 * @return array Feed object to show in head and links