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');
329 $this->menuItem(common_local_url('imsettings'),
330 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
331 $this->menuItem(common_local_url('logout'),
332 _('Logout'), _('Logout from the site'), false, 'nav_logout');
334 $this->menuItem(common_local_url('login'),
335 _('Login'), _('Login to the site'), false, 'nav_login');
336 if (!common_config('site', 'closed')) {
337 $this->menuItem(common_local_url('register'),
338 _('Register'), _('Create an account'), false, 'nav_register');
340 $this->menuItem(common_local_url('openidlogin'),
341 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
343 $this->menuItem(common_local_url('doc', array('title' => 'help')),
344 _('Help'), _('Help me!'), false, 'nav_help');
345 $this->elementEnd('ul');
346 $this->elementEnd('dd');
347 $this->elementEnd('dl');
355 function showSiteNotice()
357 // Revist. Should probably do an hAtom pattern here
358 $text = common_config('site', 'notice');
360 $this->elementStart('dl', array('id' => 'site_notice',
361 'class' => 'system_notice'));
362 $this->element('dt', null, _('Site notice'));
363 $this->element('dd', null, $text);
364 $this->elementEnd('dl');
371 * MAY overload if no notice form needed... or direct message box????
375 function showNoticeForm()
377 $notice_form = new NoticeForm($this);
378 $notice_form->show();
382 * Show anonymous message.
388 function showAnonymousMessage()
390 // needs to be defined by the class
396 * Shows local navigation, content block and aside.
402 $this->elementStart('div', array('id' => 'core'));
403 $this->showLocalNavBlock();
404 $this->showContentBlock();
406 $this->elementEnd('div');
410 * Show local navigation block.
414 function showLocalNavBlock()
416 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
417 $this->element('dt', null, _('Local views'));
418 $this->elementStart('dd');
419 $this->showLocalNav();
420 $this->elementEnd('dd');
421 $this->elementEnd('dl');
425 * Show local navigation.
431 function showLocalNav()
433 // does nothing by default
437 * Show content block.
441 function showContentBlock()
443 $this->elementStart('div', array('id' => 'content'));
444 $this->showPageTitle();
445 $this->showPageNoticeBlock();
446 $this->elementStart('div', array('id' => 'content_inner'));
447 // show the actual content (forms, lists, whatever)
448 $this->showContent();
449 $this->elementEnd('div');
450 $this->elementEnd('div');
458 function showPageTitle()
460 $this->element('h1', null, $this->title());
464 * Show page notice block.
468 function showPageNoticeBlock()
470 $this->elementStart('dl', array('id' => 'page_notice',
471 'class' => 'system_notice'));
472 $this->element('dt', null, _('Page notice'));
473 $this->elementStart('dd');
474 $this->showPageNotice();
475 $this->elementEnd('dd');
476 $this->elementEnd('dl');
482 * SHOULD overload (unless there's not a notice)
486 function showPageNotice()
493 * MUST overload (unless there's not a notice)
497 function showContent()
508 $this->elementStart('div', array('id' => 'aside_primary',
509 'class' => 'aside'));
510 $this->showExportData();
511 $this->showSections();
512 $this->elementEnd('div');
516 * Show export data feeds.
518 * MAY overload if there are feeds
522 function showExportData()
524 // is there structure to this?
525 // list of (visible!) feed links
526 // can we reuse list of feeds from showFeeds() ?
536 function showSections()
538 // for each section, show it
546 function showFooter()
548 $this->elementStart('div', array('id' => 'footer'));
549 $this->showSecondaryNav();
550 $this->showLicenses();
551 $this->elementEnd('div');
555 * Show secondary navigation.
559 function showSecondaryNav()
561 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
562 $this->element('dt', null, _('Secondary site navigation'));
563 $this->elementStart('dd', null);
564 $this->elementStart('ul', array('class' => 'nav'));
565 $this->menuItem(common_local_url('doc', array('title' => 'help')),
567 $this->menuItem(common_local_url('doc', array('title' => 'about')),
569 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
571 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
573 $this->menuItem(common_local_url('doc', array('title' => 'source')),
575 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
577 $this->elementEnd('ul');
578 $this->elementEnd('dd');
579 $this->elementEnd('dl');
587 function showLicenses()
589 $this->elementStart('dl', array('id' => 'licenses'));
590 $this->showLaconicaLicense();
591 $this->showContentLicense();
592 $this->elementEnd('dl');
596 * Show Laconica license.
600 function showLaconicaLicense()
602 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
603 $this->elementStart('dd', null);
604 if (common_config('site', 'broughtby')) {
605 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
607 $instr = _('**%%site.name%%** is a microblogging service. ');
609 $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);
610 $output = common_markup_to_html($instr);
612 $this->elementEnd('dd');
617 * Show content license.
621 function showContentLicense()
623 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
624 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
625 $this->elementStart('p');
626 $this->element('img', array('id' => 'license_cc',
627 'src' => common_config('license', 'image'),
628 'alt' => common_config('license', 'title')));
629 //TODO: This is dirty: i18n
630 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
631 $this->element('a', array('class' => 'license',
632 'rel' => 'external license',
633 'href' => common_config('license', 'url')),
634 common_config('license', 'title'));
635 $this->text(_('license.'));
636 $this->elementEnd('p');
637 $this->elementEnd('dd');
641 * Return last modified, if applicable.
645 * @return string last modified http header
647 function lastModified()
649 // For comparison with If-Last-Modified
650 // If not applicable, return null
655 * Return etag, if applicable.
659 * @return string etag http header
667 * Return true if read only.
671 * @return boolean is read only action?
673 function isReadOnly()
679 * Returns query argument or default value if not found
681 * @param string $key requested argument
682 * @param string $def default value to return if $key is not provided
684 * @return boolean is read only action?
686 function arg($key, $def=null)
688 if (array_key_exists($key, $this->args)) {
689 return $this->args[$key];
696 * Returns trimmed query argument or default value if not found
698 * @param string $key requested argument
699 * @param string $def default value to return if $key is not provided
701 * @return boolean is read only action?
703 function trimmed($key, $def=null)
705 $arg = $this->arg($key, $def);
706 return is_string($arg) ? trim($arg) : $arg;
712 * @param array $argarray is ignored since it's now passed in in prepare()
714 * @return boolean is read only action?
716 function handle($argarray=null)
718 $lm = $this->lastModified();
719 $etag = $this->etag();
721 header('ETag: ' . $etag);
724 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
725 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
726 if ($if_modified_since) {
727 $ims = strtotime($if_modified_since);
730 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
731 header('HTTP/1.1 304 Not Modified');
732 // Better way to do this?
741 * HasĀ etag? (private)
743 * @param string $etag etag http header
744 * @param string $if_none_match ifNoneMatch http header
748 function _hasEtag($etag, $if_none_match)
750 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
754 * Boolean understands english (yes, no, true, false)
756 * @param string $key query key we're interested in
757 * @param string $def default value
759 * @return boolean interprets yes/no strings as boolean
761 function boolean($key, $def=false)
763 $arg = strtolower($this->trimmed($key));
767 } else if (in_array($arg, array('true', 'yes', '1'))) {
769 } else if (in_array($arg, array('false', 'no', '0'))) {
779 * @param string $msg error message to display
780 * @param integer $code http error code, 500 by default
784 function serverError($msg, $code=500)
786 $action = $this->trimmed('action');
787 common_debug("Server error '$code' on '$action': $msg", __FILE__);
788 common_server_error($msg, $code);
794 * @param string $msg error message to display
795 * @param integer $code http error code, 400 by default
799 function clientError($msg, $code=400)
801 $action = $this->trimmed('action');
802 common_debug("User error '$code' on '$action': $msg", __FILE__);
803 common_user_error($msg, $code);
807 * Returns the current URL
809 * @return string current URL
813 $action = $this->trimmed('action');
815 unset($args['action']);
816 foreach (array_keys($_COOKIE) as $cookie) {
817 unset($args[$cookie]);
819 return common_local_url($action, $args);
823 * Generate a menu item
825 * @param string $url menu URL
826 * @param string $text menu name
827 * @param string $title title attribute, null by default
828 * @param boolean $is_selected current menu item, false by default
829 * @param string $id element id, null by default
833 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
835 // Added @id to li for some control.
836 // XXX: We might want to move this to htmloutputter.php
839 $lattrs['class'] = 'current';
842 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
844 $this->elementStart('li', $lattrs);
845 $attrs['href'] = $url;
847 $attrs['title'] = $title;
849 $this->element('a', $attrs, $text);
850 $this->elementEnd('li');
854 * Generate pagination links
856 * @param boolean $have_before is there something before?
857 * @param boolean $have_after is there something after?
858 * @param integer $page current page
859 * @param string $action current action
860 * @param array $args rest of query arguments
864 function pagination($have_before, $have_after, $page, $action, $args=null)
866 // Does a little before-after block for next/prev page
867 if ($have_before || $have_after) {
868 $this->elementStart('div', array('class' => 'pagination'));
869 $this->elementStart('dl', null);
870 $this->element('dt', null, _('Pagination'));
871 $this->elementStart('dd', null);
872 $this->elementStart('ul', array('class' => 'nav'));
875 $pargs = array('page' => $page-1);
876 $newargs = $args ? array_merge($args, $pargs) : $pargs;
877 $this->elementStart('li', array('class' => 'nav_prev'));
878 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
880 $this->elementEnd('li');
883 $pargs = array('page' => $page+1);
884 $newargs = $args ? array_merge($args, $pargs) : $pargs;
885 $this->elementStart('li', array('class' => 'nav_next'));
886 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
888 $this->elementEnd('li');
890 if ($have_before || $have_after) {
891 $this->elementEnd('ul');
892 $this->elementEnd('dd');
893 $this->elementEnd('dl');
894 $this->elementEnd('div');