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)
86 // For PEAR_Errors comming from DB_DataObject
87 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK,
88 array($this, "handleError"));
90 $this->args =& common_copy_args($argarray);
95 * Show page, a template method.
108 * Show head, a template method.
114 // XXX: attributes (profile?)
115 $this->elementStart('head');
117 $this->showStylesheets();
118 $this->showScripts();
119 $this->showOpenSearch();
121 $this->showDescription();
123 $this->elementEnd('head');
127 * Show title, a template method.
133 $this->element('title', null,
134 sprintf(_("%s - %s"),
136 common_config('site', 'name')));
140 * Returns the page title
144 * @return string page title
149 return _("Untitled page");
157 function showStylesheets()
159 $this->element('link', array('rel' => 'stylesheet',
160 'type' => 'text/css',
161 'href' => theme_path('css/display.css', 'base') . '?version=' . LACONICA_VERSION,
162 'media' => 'screen, projection, tv'));
163 $this->element('link', array('rel' => 'stylesheet',
164 'type' => 'text/css',
165 'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
166 'media' => 'screen, projection, tv'));
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]');
181 * Show javascript headers
185 function showScripts()
187 if (Event::handle('StartShowScripts', array($this))) {
188 if (Event::handle('StartShowJQueryScripts', array($this))) {
189 $this->element('script', array('type' => 'text/javascript',
190 'src' => common_path('js/jquery.min.js')),
192 $this->element('script', array('type' => 'text/javascript',
193 'src' => common_path('js/jquery.form.js')),
195 Event::handle('EndShowJQueryScripts', array($this));
197 if (Event::handle('StartShowLaconicaScripts', array($this))) {
198 $this->element('script', array('type' => 'text/javascript',
199 'src' => common_path('js/xbImportNode.js')),
201 $this->element('script', array('type' => 'text/javascript',
202 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
204 Event::handle('EndShowLaconicaScripts', array($this));
206 Event::handle('EndShowScripts', array($this));
211 * Show OpenSearch headers
215 function showOpenSearch()
217 $this->element('link', array('rel' => 'search',
218 'type' => 'application/opensearchdescription+xml',
219 'href' => common_local_url('opensearch', array('type' => 'people')),
220 'title' => common_config('site', 'name').' People Search'));
221 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
222 'href' => common_local_url('opensearch', array('type' => 'notice')),
223 'title' => common_config('site', 'name').' Notice Search'));
236 $feeds = $this->getFeeds();
239 foreach ($feeds as $feed) {
240 $this->element('link', array('rel' => $feed->rel(),
241 'href' => $feed->url,
242 'type' => $feed->mimeType(),
243 'title' => $feed->title));
255 function showDescription()
257 // does nothing by default
261 * Show extra stuff in <head>.
269 // does nothing by default
275 * Calls template methods
281 $this->elementStart('body', array('id' => $this->trimmed('action')));
282 $this->elementStart('div', array('id' => 'wrap'));
286 $this->elementEnd('div');
287 $this->elementEnd('body');
291 * Show header of the page.
293 * Calls template methods
297 function showHeader()
299 $this->elementStart('div', array('id' => 'header'));
301 $this->showPrimaryNav();
302 $this->showSiteNotice();
303 if (common_logged_in()) {
304 $this->showNoticeForm();
306 $this->showAnonymousMessage();
308 $this->elementEnd('div');
312 * Show configured logo.
318 $this->elementStart('address', array('id' => 'site_contact',
319 'class' => 'vcard'));
320 $this->elementStart('a', array('class' => 'url home bookmark',
321 'href' => common_local_url('public')));
322 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
323 $this->element('img', array('class' => 'logo photo',
324 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
325 'alt' => common_config('site', 'name')));
327 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
328 $this->elementEnd('a');
329 $this->elementEnd('address');
333 * Show primary navigation.
337 function showPrimaryNav()
339 $user = common_current_user();
341 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
342 $this->element('dt', null, _('Primary site navigation'));
343 $this->elementStart('dd');
344 $this->elementStart('ul', array('class' => 'nav'));
345 if (Event::handle('StartPrimaryNav', array($this))) {
347 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
348 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
350 $this->menuItem(common_local_url('peoplesearch'),
351 _('Search'), _('Search for people or text'), false, 'nav_search');
353 $this->menuItem(common_local_url('profilesettings'),
354 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
356 if (common_config('xmpp', 'enabled')) {
357 $this->menuItem(common_local_url('imsettings'),
358 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
360 $this->menuItem(common_local_url('smssettings'),
361 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
363 $this->menuItem(common_local_url('logout'),
364 _('Logout'), _('Logout from the site'), false, 'nav_logout');
366 $this->menuItem(common_local_url('login'),
367 _('Login'), _('Login to the site'), false, 'nav_login');
368 if (!common_config('site', 'closed')) {
369 $this->menuItem(common_local_url('register'),
370 _('Register'), _('Create an account'), false, 'nav_register');
372 $this->menuItem(common_local_url('openidlogin'),
373 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
375 $this->menuItem(common_local_url('doc', array('title' => 'help')),
376 _('Help'), _('Help me!'), false, 'nav_help');
377 Event::handle('EndPrimaryNav', array($this));
379 $this->elementEnd('ul');
380 $this->elementEnd('dd');
381 $this->elementEnd('dl');
389 function showSiteNotice()
391 // Revist. Should probably do an hAtom pattern here
392 $text = common_config('site', 'notice');
394 $this->elementStart('dl', array('id' => 'site_notice',
395 'class' => 'system_notice'));
396 $this->element('dt', null, _('Site notice'));
397 $this->elementStart('dd', null);
399 $this->elementEnd('dd');
400 $this->elementEnd('dl');
407 * MAY overload if no notice form needed... or direct message box????
411 function showNoticeForm()
413 $notice_form = new NoticeForm($this);
414 $notice_form->show();
418 * Show anonymous message.
424 function showAnonymousMessage()
426 // needs to be defined by the class
432 * Shows local navigation, content block and aside.
438 $this->elementStart('div', array('id' => 'core'));
439 $this->showLocalNavBlock();
440 $this->showContentBlock();
442 $this->elementEnd('div');
446 * Show local navigation block.
450 function showLocalNavBlock()
452 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
453 $this->element('dt', null, _('Local views'));
454 $this->elementStart('dd');
455 $this->showLocalNav();
456 $this->elementEnd('dd');
457 $this->elementEnd('dl');
461 * Show local navigation.
467 function showLocalNav()
469 // does nothing by default
473 * Show content block.
477 function showContentBlock()
479 $this->elementStart('div', array('id' => 'content'));
480 $this->showPageTitle();
481 $this->showPageNoticeBlock();
482 $this->elementStart('div', array('id' => 'content_inner'));
483 // show the actual content (forms, lists, whatever)
484 $this->showContent();
485 $this->elementEnd('div');
486 $this->elementEnd('div');
494 function showPageTitle()
496 $this->element('h1', null, $this->title());
500 * Show page notice block.
504 function showPageNoticeBlock()
506 $this->elementStart('dl', array('id' => 'page_notice',
507 'class' => 'system_notice'));
508 $this->element('dt', null, _('Page notice'));
509 $this->elementStart('dd');
510 $this->showPageNotice();
511 $this->elementEnd('dd');
512 $this->elementEnd('dl');
518 * SHOULD overload (unless there's not a notice)
522 function showPageNotice()
529 * MUST overload (unless there's not a notice)
533 function showContent()
545 $this->elementStart('div', array('id' => 'aside_primary',
546 'class' => 'aside'));
547 $this->showExportData();
548 if (Event::handle('StartShowSections', array($this))) {
549 $this->showSections();
550 Event::handle('EndShowSections', array($this));
552 $this->elementEnd('div');
556 * Show export data feeds.
561 function showExportData()
563 $feeds = $this->getFeeds();
565 $fl = new FeedList($this);
577 function showSections()
579 // for each section, show it
587 function showFooter()
589 $this->elementStart('div', array('id' => 'footer'));
590 $this->showSecondaryNav();
591 $this->showLicenses();
592 $this->elementEnd('div');
596 * Show secondary navigation.
600 function showSecondaryNav()
602 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
603 $this->element('dt', null, _('Secondary site navigation'));
604 $this->elementStart('dd', null);
605 $this->elementStart('ul', array('class' => 'nav'));
606 if (Event::handle('StartSecondaryNav', array($this))) {
607 $this->menuItem(common_local_url('doc', array('title' => 'help')),
609 $this->menuItem(common_local_url('doc', array('title' => 'about')),
611 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
613 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
615 $this->menuItem(common_local_url('doc', array('title' => 'source')),
617 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
619 Event::handle('EndSecondaryNav', array($this));
621 $this->elementEnd('ul');
622 $this->elementEnd('dd');
623 $this->elementEnd('dl');
631 function showLicenses()
633 $this->elementStart('dl', array('id' => 'licenses'));
634 $this->showLaconicaLicense();
635 $this->showContentLicense();
636 $this->elementEnd('dl');
640 * Show Laconica license.
644 function showLaconicaLicense()
646 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
647 $this->elementStart('dd', null);
648 if (common_config('site', 'broughtby')) {
649 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
651 $instr = _('**%%site.name%%** is a microblogging service. ');
653 $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);
654 $output = common_markup_to_html($instr);
656 $this->elementEnd('dd');
661 * Show content license.
665 function showContentLicense()
667 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
668 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
669 $this->elementStart('p');
670 $this->element('img', array('id' => 'license_cc',
671 'src' => common_config('license', 'image'),
672 'alt' => common_config('license', 'title')));
673 //TODO: This is dirty: i18n
674 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
675 $this->element('a', array('class' => 'license',
676 'rel' => 'external license',
677 'href' => common_config('license', 'url')),
678 common_config('license', 'title'));
679 $this->text(_('license.'));
680 $this->elementEnd('p');
681 $this->elementEnd('dd');
685 * Return last modified, if applicable.
689 * @return string last modified http header
691 function lastModified()
693 // For comparison with If-Last-Modified
694 // If not applicable, return null
699 * Return etag, if applicable.
703 * @return string etag http header
711 * Return true if read only.
715 * @return boolean is read only action?
717 function isReadOnly()
723 * Returns query argument or default value if not found
725 * @param string $key requested argument
726 * @param string $def default value to return if $key is not provided
728 * @return boolean is read only action?
730 function arg($key, $def=null)
732 if (array_key_exists($key, $this->args)) {
733 return $this->args[$key];
740 * Returns trimmed query argument or default value if not found
742 * @param string $key requested argument
743 * @param string $def default value to return if $key is not provided
745 * @return boolean is read only action?
747 function trimmed($key, $def=null)
749 $arg = $this->arg($key, $def);
750 return is_string($arg) ? trim($arg) : $arg;
756 * @param array $argarray is ignored since it's now passed in in prepare()
758 * @return boolean is read only action?
760 function handle($argarray=null)
762 $lm = $this->lastModified();
763 $etag = $this->etag();
765 header('ETag: ' . $etag);
768 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
769 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
770 if ($if_modified_since) {
771 $ims = strtotime($if_modified_since);
774 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
775 header('HTTP/1.1 304 Not Modified');
776 // Better way to do this?
785 * HasĀ etag? (private)
787 * @param string $etag etag http header
788 * @param string $if_none_match ifNoneMatch http header
792 function _hasEtag($etag, $if_none_match)
794 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
798 * Boolean understands english (yes, no, true, false)
800 * @param string $key query key we're interested in
801 * @param string $def default value
803 * @return boolean interprets yes/no strings as boolean
805 function boolean($key, $def=false)
807 $arg = strtolower($this->trimmed($key));
811 } else if (in_array($arg, array('true', 'yes', '1'))) {
813 } else if (in_array($arg, array('false', 'no', '0'))) {
823 * @param string $msg error message to display
824 * @param integer $code http error code, 500 by default
829 function serverError($msg, $code=500)
831 $action = $this->trimmed('action');
832 common_debug("Server error '$code' on '$action': $msg", __FILE__);
833 throw new ServerException($msg, $code);
839 * @param string $msg error message to display
840 * @param integer $code http error code, 400 by default
845 function clientError($msg, $code=400)
847 $action = $this->trimmed('action');
848 common_debug("User error '$code' on '$action': $msg", __FILE__);
849 throw new ClientException($msg, $code);
853 * Handle old fashioned PEAR_Error msgs coming from DB_DataObject
855 * Logs the DB_DataObject error. Override to do something else.
862 function handleError($error) {
864 common_log(LOG_ERR, "PEAR error: " . $error->getMessage());
865 $msg = sprintf(_('The database for %s isn\'t responding correctly, '.
866 'so the site won\'t work properly. '.
867 'The site admins probably know about the problem, '.
868 'but you can contact them at %s to make sure. '.
869 'Otherwise, wait a few minutes and try again.'),
870 common_config('site', 'name'),
871 common_config('site', 'email'));
873 $dac = new DBErrorAction($msg, 500);
879 * Returns the current URL
881 * @return string current URL
885 $action = $this->trimmed('action');
887 unset($args['action']);
888 foreach (array_keys($_COOKIE) as $cookie) {
889 unset($args[$cookie]);
891 return common_local_url($action, $args);
895 * Generate a menu item
897 * @param string $url menu URL
898 * @param string $text menu name
899 * @param string $title title attribute, null by default
900 * @param boolean $is_selected current menu item, false by default
901 * @param string $id element id, null by default
905 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
907 // Added @id to li for some control.
908 // XXX: We might want to move this to htmloutputter.php
911 $lattrs['class'] = 'current';
914 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
916 $this->elementStart('li', $lattrs);
917 $attrs['href'] = $url;
919 $attrs['title'] = $title;
921 $this->element('a', $attrs, $text);
922 $this->elementEnd('li');
926 * Generate pagination links
928 * @param boolean $have_before is there something before?
929 * @param boolean $have_after is there something after?
930 * @param integer $page current page
931 * @param string $action current action
932 * @param array $args rest of query arguments
936 function pagination($have_before, $have_after, $page, $action, $args=null)
938 // Does a little before-after block for next/prev page
939 if ($have_before || $have_after) {
940 $this->elementStart('div', array('class' => 'pagination'));
941 $this->elementStart('dl', null);
942 $this->element('dt', null, _('Pagination'));
943 $this->elementStart('dd', null);
944 $this->elementStart('ul', array('class' => 'nav'));
947 $pargs = array('page' => $page-1);
948 $newargs = $args ? array_merge($args, $pargs) : $pargs;
949 $this->elementStart('li', array('class' => 'nav_prev'));
950 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
952 $this->elementEnd('li');
955 $pargs = array('page' => $page+1);
956 $newargs = $args ? array_merge($args, $pargs) : $pargs;
957 $this->elementStart('li', array('class' => 'nav_next'));
958 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
960 $this->elementEnd('li');
962 if ($have_before || $have_after) {
963 $this->elementEnd('ul');
964 $this->elementEnd('dd');
965 $this->elementEnd('dl');
966 $this->elementEnd('div');
971 * An array of feeds for this action.
973 * Returns an array of potential feeds for this action.
975 * @return array Feed object to show in head and links