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);
78 * For initializing members of the class.
80 * @param array $argarray misc. arguments
82 * @return boolean true
84 function prepare($argarray)
86 $this->args =& common_copy_args($argarray);
91 * Show page, a template method.
104 * Show head, a template method.
110 // XXX: attributes (profile?)
111 $this->elementStart('head');
113 $this->showStylesheets();
114 $this->showScripts();
115 $this->showOpenSearch();
117 $this->showDescription();
119 $this->elementEnd('head');
123 * Show title, a template method.
129 $this->element('title', null,
130 sprintf(_("%s - %s"),
132 common_config('site', 'name')));
136 * Returns the page title
140 * @return string page title
145 return _("Untitled page");
153 function showStylesheets()
155 $this->element('link', array('rel' => 'stylesheet',
156 'type' => 'text/css',
157 'href' => theme_path('css/display.css', 'base') . '?version=' . LACONICA_VERSION,
158 'media' => 'screen, projection, tv'));
159 $this->element('link', array('rel' => 'stylesheet',
160 'type' => 'text/css',
161 'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
162 'media' => 'screen, projection, tv'));
163 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
164 'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
165 foreach (array(6,7) as $ver) {
166 if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
167 // Yes, IE people should be put in jail.
168 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
169 'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
172 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
173 'href="'.theme_path('css/ie.css', null).'?version='.LACONICA_VERSION.'" /><![endif]');
177 * Show javascript headers
181 function showScripts()
183 $this->element('script', array('type' => 'text/javascript',
184 'src' => common_path('js/jquery.min.js')),
186 $this->element('script', array('type' => 'text/javascript',
187 'src' => common_path('js/jquery.form.js')),
189 $this->element('script', array('type' => 'text/javascript',
190 'src' => common_path('js/xbImportNode.js')),
192 $this->element('script', array('type' => 'text/javascript',
193 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
198 * Show OpenSearch headers
202 function showOpenSearch()
204 $this->element('link', array('rel' => 'search',
205 'type' => 'application/opensearchdescription+xml',
206 'href' => common_local_url('opensearch', array('type' => 'people')),
207 'title' => common_config('site', 'name').' People Search'));
208 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
209 'href' => common_local_url('opensearch', array('type' => 'notice')),
210 'title' => common_config('site', 'name').' Notice Search'));
222 // does nothing by default
232 function showDescription()
234 // does nothing by default
238 * Show extra stuff in <head>.
246 // does nothing by default
253 * Calls template methods
259 $this->elementStart('body', array('id' => $this->trimmed('action')));
260 $this->elementStart('div', 'wrap');
264 $this->elementEnd('div');
265 $this->elementEnd('body');
269 * Show header of the page.
271 * Calls template methods
275 function showHeader()
277 $this->elementStart('div', array('id' => 'header'));
279 $this->showPrimaryNav();
280 $this->showSiteNotice();
281 if (common_logged_in()) {
282 $this->showNoticeForm();
284 $this->showAnonymousMessage();
286 $this->elementEnd('div');
290 * Show configured logo.
296 $this->elementStart('address', array('id' => 'site_contact',
297 'class' => 'vcard'));
298 $this->elementStart('a', array('class' => 'url home bookmark',
299 'href' => common_local_url('public')));
300 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
301 $this->element('img', array('class' => 'logo photo',
302 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
303 'alt' => common_config('site', 'name')));
305 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
306 $this->elementEnd('a');
307 $this->elementEnd('address');
311 * Show primary navigation.
315 function showPrimaryNav()
317 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
318 $this->element('dt', null, _('Primary site navigation'));
319 $this->elementStart('dd');
320 $user = common_current_user();
321 $this->elementStart('ul', array('class' => 'nav'));
323 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
324 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
326 $this->menuItem(common_local_url('peoplesearch'),
327 _('Search'), _('Search for people or text'), false, 'nav_search');
329 $this->menuItem(common_local_url('profilesettings'),
330 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
331 $this->menuItem(common_local_url('imsettings'),
332 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
333 $this->menuItem(common_local_url('logout'),
334 _('Logout'), _('Logout from the site'), false, 'nav_logout');
336 $this->menuItem(common_local_url('login'),
337 _('Login'), _('Login to the site'), false, 'nav_login');
338 if (!common_config('site', 'closed')) {
339 $this->menuItem(common_local_url('register'),
340 _('Register'), _('Create an account'), false, 'nav_register');
342 $this->menuItem(common_local_url('openidlogin'),
343 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
345 $this->menuItem(common_local_url('doc', array('title' => 'help')),
346 _('Help'), _('Help me!'), false, 'nav_help');
347 $this->elementEnd('ul');
348 $this->elementEnd('dd');
349 $this->elementEnd('dl');
357 function showSiteNotice()
359 // Revist. Should probably do an hAtom pattern here
360 $text = common_config('site', 'notice');
362 $this->elementStart('dl', array('id' => 'site_notice',
363 'class' => 'system_notice'));
364 $this->element('dt', null, _('Site notice'));
365 $this->element('dd', null, $text);
366 $this->elementEnd('dl');
373 * MAY overload if no notice form needed... or direct message box????
377 function showNoticeForm()
379 $notice_form = new NoticeForm($this);
380 $notice_form->show();
384 * Show anonymous message.
390 function showAnonymousMessage()
392 // needs to be defined by the class
398 * Shows local navigation, content block and aside.
404 $this->elementStart('div', array('id' => 'core'));
405 $this->showLocalNavBlock();
406 $this->showContentBlock();
408 $this->elementEnd('div');
412 * Show local navigation block.
416 function showLocalNavBlock()
418 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
419 $this->element('dt', null, _('Local views'));
420 $this->elementStart('dd');
421 $this->showLocalNav();
422 $this->elementEnd('dd');
423 $this->elementEnd('dl');
427 * Show local navigation.
433 function showLocalNav()
435 // does nothing by default
439 * Show content block.
443 function showContentBlock()
445 $this->elementStart('div', array('id' => 'content'));
446 $this->showPageTitle();
447 $this->showPageNoticeBlock();
448 $this->elementStart('div', array('id' => 'content_inner'));
449 // show the actual content (forms, lists, whatever)
450 $this->showContent();
451 $this->elementEnd('div');
452 $this->elementEnd('div');
460 function showPageTitle()
462 $this->element('h1', null, $this->title());
466 * Show page notice block.
470 function showPageNoticeBlock()
472 $this->elementStart('dl', array('id' => 'page_notice',
473 'class' => 'system_notice'));
474 $this->element('dt', null, _('Page notice'));
475 $this->elementStart('dd');
476 $this->showPageNotice();
477 $this->elementEnd('dd');
478 $this->elementEnd('dl');
484 * SHOULD overload (unless there's not a notice)
488 function showPageNotice()
495 * MUST overload (unless there's not a notice)
499 function showContent()
510 $this->elementStart('div', array('id' => 'aside_primary',
511 'class' => 'aside'));
512 $this->showExportData();
513 $this->showSections();
514 $this->elementEnd('div');
518 * Show export data feeds.
520 * MAY overload if there are feeds
524 function showExportData()
526 // is there structure to this?
527 // list of (visible!) feed links
528 // can we reuse list of feeds from showFeeds() ?
538 function showSections()
540 // for each section, show it
548 function showFooter()
550 $this->elementStart('div', array('id' => 'footer'));
551 $this->showSecondaryNav();
552 $this->showLicenses();
553 $this->elementEnd('div');
557 * Show secondary navigation.
561 function showSecondaryNav()
563 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
564 $this->element('dt', null, _('Secondary site navigation'));
565 $this->elementStart('dd', null);
566 $this->elementStart('ul', array('class' => 'nav'));
567 $this->menuItem(common_local_url('doc', array('title' => 'help')),
569 $this->menuItem(common_local_url('doc', array('title' => 'about')),
571 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
573 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
575 $this->menuItem(common_local_url('doc', array('title' => 'source')),
577 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
579 $this->elementEnd('ul');
580 $this->elementEnd('dd');
581 $this->elementEnd('dl');
589 function showLicenses()
591 $this->elementStart('dl', array('id' => 'licenses'));
592 $this->showLaconicaLicense();
593 $this->showContentLicense();
594 $this->elementEnd('dl');
598 * Show Laconica license.
602 function showLaconicaLicense()
604 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
605 $this->elementStart('dd', null);
606 if (common_config('site', 'broughtby')) {
607 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
609 $instr = _('**%%site.name%%** is a microblogging service. ');
611 $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);
612 $output = common_markup_to_html($instr);
614 $this->elementEnd('dd');
619 * Show content license.
623 function showContentLicense()
625 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
626 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
627 $this->elementStart('p');
628 $this->element('img', array('id' => 'license_cc',
629 'src' => common_config('license', 'image'),
630 'alt' => common_config('license', 'title')));
631 //TODO: This is dirty: i18n
632 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
633 $this->element('a', array('class' => 'license',
634 'rel' => 'external license',
635 'href' => common_config('license', 'url')),
636 common_config('license', 'title'));
637 $this->text(_('license.'));
638 $this->elementEnd('p');
639 $this->elementEnd('dd');
643 * Return last modified, if applicable.
647 * @return string last modified http header
649 function lastModified()
651 // For comparison with If-Last-Modified
652 // If not applicable, return null
657 * Return etag, if applicable.
661 * @return string etag http header
669 * Return true if read only.
673 * @return boolean is read only action?
675 function isReadOnly()
681 * Returns query argument or default value if not found
683 * @param string $key requested argument
684 * @param string $def default value to return if $key is not provided
686 * @return boolean is read only action?
688 function arg($key, $def=null)
690 if (array_key_exists($key, $this->args)) {
691 return $this->args[$key];
698 * Returns trimmed query argument or default value if not found
700 * @param string $key requested argument
701 * @param string $def default value to return if $key is not provided
703 * @return boolean is read only action?
705 function trimmed($key, $def=null)
707 $arg = $this->arg($key, $def);
708 return is_string($arg) ? trim($arg) : $arg;
714 * @param array $argarray is ignored since it's now passed in in prepare()
716 * @return boolean is read only action?
718 function handle($argarray=null)
720 $lm = $this->lastModified();
721 $etag = $this->etag();
723 header('ETag: ' . $etag);
726 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
727 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
728 if ($if_modified_since) {
729 $ims = strtotime($if_modified_since);
732 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
733 header('HTTP/1.1 304 Not Modified');
734 // Better way to do this?
743 * HasĀ etag? (private)
745 * @param string $etag etag http header
746 * @param string $if_none_match ifNoneMatch http header
750 function _hasEtag($etag, $if_none_match)
752 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
756 * Boolean understands english (yes, no, true, false)
758 * @param string $key query key we're interested in
759 * @param string $def default value
761 * @return boolean interprets yes/no strings as boolean
763 function boolean($key, $def=false)
765 $arg = strtolower($this->trimmed($key));
769 } else if (in_array($arg, array('true', 'yes', '1'))) {
771 } else if (in_array($arg, array('false', 'no', '0'))) {
781 * @param string $msg error message to display
782 * @param integer $code http error code, 500 by default
786 function serverError($msg, $code=500)
788 $action = $this->trimmed('action');
789 common_debug("Server error '$code' on '$action': $msg", __FILE__);
790 common_server_error($msg, $code);
796 * @param string $msg error message to display
797 * @param integer $code http error code, 400 by default
801 function clientError($msg, $code=400)
803 $action = $this->trimmed('action');
804 common_debug("User error '$code' on '$action': $msg", __FILE__);
805 common_user_error($msg, $code);
809 * Returns the current URL
811 * @return string current URL
815 $action = $this->trimmed('action');
817 unset($args['action']);
818 foreach (array_keys($_COOKIE) as $cookie) {
819 unset($args[$cookie]);
821 return common_local_url($action, $args);
825 * Generate a menu item
827 * @param string $url menu URL
828 * @param string $text menu name
829 * @param string $title title attribute, null by default
830 * @param boolean $is_selected current menu item, false by default
831 * @param string $id element id, null by default
835 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
837 // Added @id to li for some control.
838 // XXX: We might want to move this to htmloutputter.php
841 $lattrs['class'] = 'current';
844 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
846 $this->elementStart('li', $lattrs);
847 $attrs['href'] = $url;
849 $attrs['title'] = $title;
851 $this->element('a', $attrs, $text);
852 $this->elementEnd('li');
856 * Generate pagination links
858 * @param boolean $have_before is there something before?
859 * @param boolean $have_after is there something after?
860 * @param integer $page current page
861 * @param string $action current action
862 * @param array $args rest of query arguments
866 function pagination($have_before, $have_after, $page, $action, $args=null)
868 // Does a little before-after block for next/prev page
869 if ($have_before || $have_after) {
870 $this->elementStart('div', array('class' => 'pagination'));
871 $this->elementStart('dl', null);
872 $this->element('dt', null, _('Pagination'));
873 $this->elementStart('dd', null);
874 $this->elementStart('ul', array('class' => 'nav'));
877 $pargs = array('page' => $page-1);
878 $newargs = $args ? array_merge($args, $pargs) : $pargs;
879 $this->elementStart('li', array('class' => 'nav_prev'));
880 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
882 $this->elementEnd('li');
885 $pargs = array('page' => $page+1);
886 $newargs = $args ? array_merge($args, $pargs) : $pargs;
887 $this->elementStart('li', array('class' => 'nav_next'));
888 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
890 $this->elementEnd('li');
892 if ($have_before || $have_after) {
893 $this->elementEnd('ul');
894 $this->elementEnd('dd');
895 $this->elementEnd('dl');
896 $this->elementEnd('div');