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('ie'.$ver.'.css'))) {
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]');
175 * Show javascript headers
179 function showScripts()
181 $this->element('script', array('type' => 'text/javascript',
182 'src' => common_path('js/jquery.min.js')),
184 $this->element('script', array('type' => 'text/javascript',
185 'src' => common_path('js/jquery.form.js')),
187 $this->element('script', array('type' => 'text/javascript',
188 'src' => common_path('js/xbImportNode.js')),
190 $this->element('script', array('type' => 'text/javascript',
191 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
196 * Show OpenSearch headers
200 function showOpenSearch()
202 $this->element('link', array('rel' => 'search',
203 'type' => 'application/opensearchdescription+xml',
204 'href' => common_local_url('opensearch', array('type' => 'people')),
205 'title' => common_config('site', 'name').' People Search'));
206 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
207 'href' => common_local_url('opensearch', array('type' => 'notice')),
208 'title' => common_config('site', 'name').' Notice Search'));
220 // does nothing by default
230 function showDescription()
232 // does nothing by default
236 * Show extra stuff in <head>.
244 // does nothing by default
251 * Calls template methods
257 $this->elementStart('body', array('id' => $this->trimmed('action')));
258 $this->elementStart('div', 'wrap');
262 $this->elementEnd('div');
263 $this->elementEnd('body');
267 * Show header of the page.
269 * Calls template methods
273 function showHeader()
275 $this->elementStart('div', array('id' => 'header'));
277 $this->showPrimaryNav();
278 $this->showSiteNotice();
279 if (common_logged_in()) {
280 $this->showNoticeForm();
282 $this->showAnonymousMessage();
284 $this->elementEnd('div');
288 * Show configured logo.
294 $this->elementStart('address', array('id' => 'site_contact',
295 'class' => 'vcard'));
296 $this->elementStart('a', array('class' => 'url home bookmark',
297 'href' => common_local_url('public')));
298 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
299 $this->element('img', array('class' => 'logo photo',
300 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
301 'alt' => common_config('site', 'name')));
303 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
304 $this->elementEnd('a');
305 $this->elementEnd('address');
309 * Show primary navigation.
313 function showPrimaryNav()
315 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
316 $this->element('dt', null, _('Primary site navigation'));
317 $this->elementStart('dd');
318 $user = common_current_user();
319 $this->elementStart('ul', array('class' => 'nav'));
321 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
322 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
324 $this->menuItem(common_local_url('peoplesearch'),
325 _('Search'), _('Search for people or text'), false, 'nav_search');
327 $this->menuItem(common_local_url('profilesettings'),
328 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
330 if (common_config('xmpp', 'enabled')) {
331 $this->menuItem(common_local_url('imsettings'),
332 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
334 $this->menuItem(common_local_url('smssettings'),
335 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
337 $this->menuItem(common_local_url('logout'),
338 _('Logout'), _('Logout from the site'), false, 'nav_logout');
340 $this->menuItem(common_local_url('login'),
341 _('Login'), _('Login to the site'), false, 'nav_login');
342 if (!common_config('site', 'closed')) {
343 $this->menuItem(common_local_url('register'),
344 _('Register'), _('Create an account'), false, 'nav_register');
346 $this->menuItem(common_local_url('openidlogin'),
347 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
349 $this->menuItem(common_local_url('doc', array('title' => 'help')),
350 _('Help'), _('Help me!'), false, 'nav_help');
351 $this->elementEnd('ul');
352 $this->elementEnd('dd');
353 $this->elementEnd('dl');
361 function showSiteNotice()
363 // Revist. Should probably do an hAtom pattern here
364 $text = common_config('site', 'notice');
366 $this->elementStart('dl', array('id' => 'site_notice',
367 'class' => 'system_notice'));
368 $this->element('dt', null, _('Site notice'));
369 $this->element('dd', null, $text);
370 $this->elementEnd('dl');
377 * MAY overload if no notice form needed... or direct message box????
381 function showNoticeForm()
383 $notice_form = new NoticeForm($this);
384 $notice_form->show();
388 * Show anonymous message.
394 function showAnonymousMessage()
396 // needs to be defined by the class
402 * Shows local navigation, content block and aside.
408 $this->elementStart('div', array('id' => 'core'));
409 $this->showLocalNavBlock();
410 $this->showContentBlock();
412 $this->elementEnd('div');
416 * Show local navigation block.
420 function showLocalNavBlock()
422 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
423 $this->element('dt', null, _('Local views'));
424 $this->elementStart('dd');
425 $this->showLocalNav();
426 $this->elementEnd('dd');
427 $this->elementEnd('dl');
431 * Show local navigation.
437 function showLocalNav()
439 // does nothing by default
443 * Show content block.
447 function showContentBlock()
449 $this->elementStart('div', array('id' => 'content'));
450 $this->showPageTitle();
451 $this->showPageNoticeBlock();
452 $this->elementStart('div', array('id' => 'content_inner'));
453 // show the actual content (forms, lists, whatever)
454 $this->showContent();
455 $this->elementEnd('div');
456 $this->elementEnd('div');
464 function showPageTitle()
466 $this->element('h1', null, $this->title());
470 * Show page notice block.
474 function showPageNoticeBlock()
476 $this->elementStart('dl', array('id' => 'page_notice',
477 'class' => 'system_notice'));
478 $this->element('dt', null, _('Page notice'));
479 $this->elementStart('dd');
480 $this->showPageNotice();
481 $this->elementEnd('dd');
482 $this->elementEnd('dl');
488 * SHOULD overload (unless there's not a notice)
492 function showPageNotice()
499 * MUST overload (unless there's not a notice)
503 function showContent()
514 $this->elementStart('div', array('id' => 'aside_primary',
515 'class' => 'aside'));
516 $this->showExportData();
517 $this->showSections();
518 $this->elementEnd('div');
522 * Show export data feeds.
524 * MAY overload if there are feeds
528 function showExportData()
530 // is there structure to this?
531 // list of (visible!) feed links
532 // can we reuse list of feeds from showFeeds() ?
542 function showSections()
544 // for each section, show it
552 function showFooter()
554 $this->elementStart('div', array('id' => 'footer'));
555 $this->showSecondaryNav();
556 $this->showLicenses();
557 $this->elementEnd('div');
561 * Show secondary navigation.
565 function showSecondaryNav()
567 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
568 $this->element('dt', null, _('Secondary site navigation'));
569 $this->elementStart('dd', null);
570 $this->elementStart('ul', array('class' => 'nav'));
571 $this->menuItem(common_local_url('doc', array('title' => 'help')),
573 $this->menuItem(common_local_url('doc', array('title' => 'about')),
575 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
577 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
579 $this->menuItem(common_local_url('doc', array('title' => 'source')),
581 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
583 $this->elementEnd('ul');
584 $this->elementEnd('dd');
585 $this->elementEnd('dl');
593 function showLicenses()
595 $this->elementStart('dl', array('id' => 'licenses'));
596 $this->showLaconicaLicense();
597 $this->showContentLicense();
598 $this->elementEnd('dl');
602 * Show Laconica license.
606 function showLaconicaLicense()
608 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
609 $this->elementStart('dd', null);
610 if (common_config('site', 'broughtby')) {
611 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
613 $instr = _('**%%site.name%%** is a microblogging service. ');
615 $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);
616 $output = common_markup_to_html($instr);
618 $this->elementEnd('dd');
623 * Show content license.
627 function showContentLicense()
629 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
630 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
631 $this->elementStart('p');
632 $this->element('img', array('id' => 'license_cc',
633 'src' => common_config('license', 'image'),
634 'alt' => common_config('license', 'title')));
635 //TODO: This is dirty: i18n
636 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
637 $this->element('a', array('class' => 'license',
638 'rel' => 'external license',
639 'href' => common_config('license', 'url')),
640 common_config('license', 'title'));
641 $this->text(_('license.'));
642 $this->elementEnd('p');
643 $this->elementEnd('dd');
647 * Return last modified, if applicable.
651 * @return string last modified http header
653 function lastModified()
655 // For comparison with If-Last-Modified
656 // If not applicable, return null
661 * Return etag, if applicable.
665 * @return string etag http header
673 * Return true if read only.
677 * @return boolean is read only action?
679 function isReadOnly()
685 * Returns query argument or default value if not found
687 * @param string $key requested argument
688 * @param string $def default value to return if $key is not provided
690 * @return boolean is read only action?
692 function arg($key, $def=null)
694 if (array_key_exists($key, $this->args)) {
695 return $this->args[$key];
702 * Returns trimmed query argument or default value if not found
704 * @param string $key requested argument
705 * @param string $def default value to return if $key is not provided
707 * @return boolean is read only action?
709 function trimmed($key, $def=null)
711 $arg = $this->arg($key, $def);
712 return is_string($arg) ? trim($arg) : $arg;
718 * @param array $argarray is ignored since it's now passed in in prepare()
720 * @return boolean is read only action?
722 function handle($argarray=null)
724 $lm = $this->lastModified();
725 $etag = $this->etag();
727 header('ETag: ' . $etag);
730 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
731 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
732 if ($if_modified_since) {
733 $ims = strtotime($if_modified_since);
736 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
737 header('HTTP/1.1 304 Not Modified');
738 // Better way to do this?
747 * HasĀ etag? (private)
749 * @param string $etag etag http header
750 * @param string $if_none_match ifNoneMatch http header
754 function _hasEtag($etag, $if_none_match)
756 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
760 * Boolean understands english (yes, no, true, false)
762 * @param string $key query key we're interested in
763 * @param string $def default value
765 * @return boolean interprets yes/no strings as boolean
767 function boolean($key, $def=false)
769 $arg = strtolower($this->trimmed($key));
773 } else if (in_array($arg, array('true', 'yes', '1'))) {
775 } else if (in_array($arg, array('false', 'no', '0'))) {
785 * @param string $msg error message to display
786 * @param integer $code http error code, 500 by default
790 function serverError($msg, $code=500)
792 $action = $this->trimmed('action');
793 common_debug("Server error '$code' on '$action': $msg", __FILE__);
794 common_server_error($msg, $code);
800 * @param string $msg error message to display
801 * @param integer $code http error code, 400 by default
805 function clientError($msg, $code=400)
807 $action = $this->trimmed('action');
808 common_debug("User error '$code' on '$action': $msg", __FILE__);
809 common_user_error($msg, $code);
813 * Returns the current URL
815 * @return string current URL
819 $action = $this->trimmed('action');
821 unset($args['action']);
822 foreach (array_keys($_COOKIE) as $cookie) {
823 unset($args[$cookie]);
825 return common_local_url($action, $args);
829 * Generate a menu item
831 * @param string $url menu URL
832 * @param string $text menu name
833 * @param string $title title attribute, null by default
834 * @param boolean $is_selected current menu item, false by default
835 * @param string $id element id, null by default
839 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
841 // Added @id to li for some control.
842 // XXX: We might want to move this to htmloutputter.php
845 $lattrs['class'] = 'current';
848 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
850 $this->elementStart('li', $lattrs);
851 $attrs['href'] = $url;
853 $attrs['title'] = $title;
855 $this->element('a', $attrs, $text);
856 $this->elementEnd('li');
860 * Generate pagination links
862 * @param boolean $have_before is there something before?
863 * @param boolean $have_after is there something after?
864 * @param integer $page current page
865 * @param string $action current action
866 * @param array $args rest of query arguments
870 function pagination($have_before, $have_after, $page, $action, $args=null)
872 // Does a little before-after block for next/prev page
873 if ($have_before || $have_after) {
874 $this->elementStart('div', array('class' => 'pagination'));
875 $this->elementStart('dl', null);
876 $this->element('dt', null, _('Pagination'));
877 $this->elementStart('dd', null);
878 $this->elementStart('ul', array('class' => 'nav'));
881 $pargs = array('page' => $page-1);
882 $newargs = $args ? array_merge($args, $pargs) : $pargs;
883 $this->elementStart('li', array('class' => 'nav_prev'));
884 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
886 $this->elementEnd('li');
889 $pargs = array('page' => $page+1);
890 $newargs = $args ? array_merge($args, $pargs) : $pargs;
891 $this->elementStart('li', array('class' => 'nav_next'));
892 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
894 $this->elementEnd('li');
896 if ($have_before || $have_after) {
897 $this->elementEnd('ul');
898 $this->elementEnd('dd');
899 $this->elementEnd('dl');
900 $this->elementEnd('div');