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]');
173 if (common_config('site', 'mobile')) {
174 $this->element('link', array('rel' => 'stylesheet',
175 'type' => 'text/css',
176 'href' => theme_path('css/mobile.css', 'base') . '?version=' . LACONICA_VERSION,
177 // TODO: "handheld" CSS for other mobile devices
178 'media' => 'only screen and (max-device-width: 480px)')); // Mobile WebKit
183 * Show javascript headers
187 function showScripts()
189 if (Event::handle('StartShowScripts', array($this))) {
190 if (Event::handle('StartShowJQueryScripts', array($this))) {
191 $this->element('script', array('type' => 'text/javascript',
192 'src' => common_path('js/jquery.min.js')),
194 $this->element('script', array('type' => 'text/javascript',
195 'src' => common_path('js/jquery.form.js')),
197 Event::handle('EndShowJQueryScripts', array($this));
199 if (Event::handle('StartShowLaconicaScripts', array($this))) {
200 $this->element('script', array('type' => 'text/javascript',
201 'src' => common_path('js/xbImportNode.js')),
203 $this->element('script', array('type' => 'text/javascript',
204 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
206 Event::handle('EndShowLaconicaScripts', array($this));
208 Event::handle('EndShowScripts', array($this));
213 * Show OpenSearch headers
217 function showOpenSearch()
219 $this->element('link', array('rel' => 'search',
220 'type' => 'application/opensearchdescription+xml',
221 'href' => common_local_url('opensearch', array('type' => 'people')),
222 'title' => common_config('site', 'name').' People Search'));
223 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
224 'href' => common_local_url('opensearch', array('type' => 'notice')),
225 'title' => common_config('site', 'name').' Notice Search'));
237 // does nothing by default
247 function showDescription()
249 // does nothing by default
253 * Show extra stuff in <head>.
261 // does nothing by default
267 * Calls template methods
273 $this->elementStart('body', array('id' => $this->trimmed('action')));
274 $this->elementStart('div', array('id' => 'wrap'));
278 $this->elementEnd('div');
279 $this->elementEnd('body');
283 * Show header of the page.
285 * Calls template methods
289 function showHeader()
291 $this->elementStart('div', array('id' => 'header'));
293 $this->showPrimaryNav();
294 $this->showSiteNotice();
295 if (common_logged_in()) {
296 $this->showNoticeForm();
298 $this->showAnonymousMessage();
300 $this->elementEnd('div');
304 * Show configured logo.
310 $this->elementStart('address', array('id' => 'site_contact',
311 'class' => 'vcard'));
312 $this->elementStart('a', array('class' => 'url home bookmark',
313 'href' => common_local_url('public')));
314 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
315 $this->element('img', array('class' => 'logo photo',
316 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
317 'alt' => common_config('site', 'name')));
319 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
320 $this->elementEnd('a');
321 $this->elementEnd('address');
325 * Show primary navigation.
329 function showPrimaryNav()
331 $user = common_current_user();
333 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
334 $this->element('dt', null, _('Primary site navigation'));
335 $this->elementStart('dd');
336 $this->elementStart('ul', array('class' => 'nav'));
337 if (Event::handle('StartPrimaryNav', array($this))) {
339 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
340 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
342 $this->menuItem(common_local_url('peoplesearch'),
343 _('Search'), _('Search for people or text'), false, 'nav_search');
345 $this->menuItem(common_local_url('profilesettings'),
346 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
348 if (common_config('xmpp', 'enabled')) {
349 $this->menuItem(common_local_url('imsettings'),
350 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
352 $this->menuItem(common_local_url('smssettings'),
353 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
355 $this->menuItem(common_local_url('logout'),
356 _('Logout'), _('Logout from the site'), false, 'nav_logout');
358 $this->menuItem(common_local_url('login'),
359 _('Login'), _('Login to the site'), false, 'nav_login');
360 if (!common_config('site', 'closed')) {
361 $this->menuItem(common_local_url('register'),
362 _('Register'), _('Create an account'), false, 'nav_register');
364 $this->menuItem(common_local_url('openidlogin'),
365 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
367 $this->menuItem(common_local_url('doc', array('title' => 'help')),
368 _('Help'), _('Help me!'), false, 'nav_help');
369 Event::handle('EndPrimaryNav', array($this));
371 $this->elementEnd('ul');
372 $this->elementEnd('dd');
373 $this->elementEnd('dl');
381 function showSiteNotice()
383 // Revist. Should probably do an hAtom pattern here
384 $text = common_config('site', 'notice');
386 $this->elementStart('dl', array('id' => 'site_notice',
387 'class' => 'system_notice'));
388 $this->element('dt', null, _('Site notice'));
389 $this->elementStart('dd', null);
391 $this->elementEnd('dd');
392 $this->elementEnd('dl');
399 * MAY overload if no notice form needed... or direct message box????
403 function showNoticeForm()
405 $notice_form = new NoticeForm($this);
406 $notice_form->show();
410 * Show anonymous message.
416 function showAnonymousMessage()
418 // needs to be defined by the class
424 * Shows local navigation, content block and aside.
430 $this->elementStart('div', array('id' => 'core'));
431 $this->showLocalNavBlock();
432 $this->showContentBlock();
434 $this->elementEnd('div');
438 * Show local navigation block.
442 function showLocalNavBlock()
444 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
445 $this->element('dt', null, _('Local views'));
446 $this->elementStart('dd');
447 $this->showLocalNav();
448 $this->elementEnd('dd');
449 $this->elementEnd('dl');
453 * Show local navigation.
459 function showLocalNav()
461 // does nothing by default
465 * Show content block.
469 function showContentBlock()
471 $this->elementStart('div', array('id' => 'content'));
472 $this->showPageTitle();
473 $this->showPageNoticeBlock();
474 $this->elementStart('div', array('id' => 'content_inner'));
475 // show the actual content (forms, lists, whatever)
476 $this->showContent();
477 $this->elementEnd('div');
478 $this->elementEnd('div');
486 function showPageTitle()
488 $this->element('h1', null, $this->title());
492 * Show page notice block.
496 function showPageNoticeBlock()
498 $this->elementStart('dl', array('id' => 'page_notice',
499 'class' => 'system_notice'));
500 $this->element('dt', null, _('Page notice'));
501 $this->elementStart('dd');
502 $this->showPageNotice();
503 $this->elementEnd('dd');
504 $this->elementEnd('dl');
510 * SHOULD overload (unless there's not a notice)
514 function showPageNotice()
521 * MUST overload (unless there's not a notice)
525 function showContent()
537 $this->elementStart('div', array('id' => 'aside_primary',
538 'class' => 'aside'));
539 $this->showExportData();
540 if (Event::handle('StartShowSections', array($this))) {
541 $this->showSections();
542 Event::handle('EndShowSections', array($this));
544 $this->elementEnd('div');
548 * Show export data feeds.
550 * MAY overload if there are feeds
554 function showExportData()
556 // is there structure to this?
557 // list of (visible!) feed links
558 // can we reuse list of feeds from showFeeds() ?
568 function showSections()
570 // for each section, show it
578 function showFooter()
580 $this->elementStart('div', array('id' => 'footer'));
581 $this->showSecondaryNav();
582 $this->showLicenses();
583 $this->elementEnd('div');
587 * Show secondary navigation.
591 function showSecondaryNav()
593 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
594 $this->element('dt', null, _('Secondary site navigation'));
595 $this->elementStart('dd', null);
596 $this->elementStart('ul', array('class' => 'nav'));
597 if (Event::handle('StartSecondaryNav', array($this))) {
598 $this->menuItem(common_local_url('doc', array('title' => 'help')),
600 $this->menuItem(common_local_url('doc', array('title' => 'about')),
602 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
604 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
606 $this->menuItem(common_local_url('doc', array('title' => 'source')),
608 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
610 Event::handle('EndSecondaryNav', array($this));
612 $this->elementEnd('ul');
613 $this->elementEnd('dd');
614 $this->elementEnd('dl');
622 function showLicenses()
624 $this->elementStart('dl', array('id' => 'licenses'));
625 $this->showLaconicaLicense();
626 $this->showContentLicense();
627 $this->elementEnd('dl');
631 * Show Laconica license.
635 function showLaconicaLicense()
637 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
638 $this->elementStart('dd', null);
639 if (common_config('site', 'broughtby')) {
640 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
642 $instr = _('**%%site.name%%** is a microblogging service. ');
644 $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);
645 $output = common_markup_to_html($instr);
647 $this->elementEnd('dd');
652 * Show content license.
656 function showContentLicense()
658 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
659 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
660 $this->elementStart('p');
661 $this->element('img', array('id' => 'license_cc',
662 'src' => common_config('license', 'image'),
663 'alt' => common_config('license', 'title')));
664 //TODO: This is dirty: i18n
665 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
666 $this->element('a', array('class' => 'license',
667 'rel' => 'external license',
668 'href' => common_config('license', 'url')),
669 common_config('license', 'title'));
670 $this->text(_('license.'));
671 $this->elementEnd('p');
672 $this->elementEnd('dd');
676 * Return last modified, if applicable.
680 * @return string last modified http header
682 function lastModified()
684 // For comparison with If-Last-Modified
685 // If not applicable, return null
690 * Return etag, if applicable.
694 * @return string etag http header
702 * Return true if read only.
706 * @return boolean is read only action?
708 function isReadOnly()
714 * Returns query argument or default value if not found
716 * @param string $key requested argument
717 * @param string $def default value to return if $key is not provided
719 * @return boolean is read only action?
721 function arg($key, $def=null)
723 if (array_key_exists($key, $this->args)) {
724 return $this->args[$key];
731 * Returns trimmed query argument or default value if not found
733 * @param string $key requested argument
734 * @param string $def default value to return if $key is not provided
736 * @return boolean is read only action?
738 function trimmed($key, $def=null)
740 $arg = $this->arg($key, $def);
741 return is_string($arg) ? trim($arg) : $arg;
747 * @param array $argarray is ignored since it's now passed in in prepare()
749 * @return boolean is read only action?
751 function handle($argarray=null)
753 $lm = $this->lastModified();
754 $etag = $this->etag();
756 header('ETag: ' . $etag);
759 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
760 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
761 if ($if_modified_since) {
762 $ims = strtotime($if_modified_since);
765 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
766 header('HTTP/1.1 304 Not Modified');
767 // Better way to do this?
776 * HasĀ etag? (private)
778 * @param string $etag etag http header
779 * @param string $if_none_match ifNoneMatch http header
783 function _hasEtag($etag, $if_none_match)
785 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
789 * Boolean understands english (yes, no, true, false)
791 * @param string $key query key we're interested in
792 * @param string $def default value
794 * @return boolean interprets yes/no strings as boolean
796 function boolean($key, $def=false)
798 $arg = strtolower($this->trimmed($key));
802 } else if (in_array($arg, array('true', 'yes', '1'))) {
804 } else if (in_array($arg, array('false', 'no', '0'))) {
814 * @param string $msg error message to display
815 * @param integer $code http error code, 500 by default
820 function serverError($msg, $code=500)
822 $action = $this->trimmed('action');
823 common_debug("Server error '$code' on '$action': $msg", __FILE__);
824 throw new ServerException($msg, $code);
830 * @param string $msg error message to display
831 * @param integer $code http error code, 400 by default
836 function clientError($msg, $code=400)
838 $action = $this->trimmed('action');
839 common_debug("User error '$code' on '$action': $msg", __FILE__);
840 throw new ClientException($msg, $code);
844 * Returns the current URL
846 * @return string current URL
850 $action = $this->trimmed('action');
852 unset($args['action']);
853 foreach (array_keys($_COOKIE) as $cookie) {
854 unset($args[$cookie]);
856 return common_local_url($action, $args);
860 * Generate a menu item
862 * @param string $url menu URL
863 * @param string $text menu name
864 * @param string $title title attribute, null by default
865 * @param boolean $is_selected current menu item, false by default
866 * @param string $id element id, null by default
870 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
872 // Added @id to li for some control.
873 // XXX: We might want to move this to htmloutputter.php
876 $lattrs['class'] = 'current';
879 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
881 $this->elementStart('li', $lattrs);
882 $attrs['href'] = $url;
884 $attrs['title'] = $title;
886 $this->element('a', $attrs, $text);
887 $this->elementEnd('li');
891 * Generate pagination links
893 * @param boolean $have_before is there something before?
894 * @param boolean $have_after is there something after?
895 * @param integer $page current page
896 * @param string $action current action
897 * @param array $args rest of query arguments
901 function pagination($have_before, $have_after, $page, $action, $args=null)
903 // Does a little before-after block for next/prev page
904 if ($have_before || $have_after) {
905 $this->elementStart('div', array('class' => 'pagination'));
906 $this->elementStart('dl', null);
907 $this->element('dt', null, _('Pagination'));
908 $this->elementStart('dd', null);
909 $this->elementStart('ul', array('class' => 'nav'));
912 $pargs = array('page' => $page-1);
913 $newargs = $args ? array_merge($args, $pargs) : $pargs;
914 $this->elementStart('li', array('class' => 'nav_prev'));
915 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
917 $this->elementEnd('li');
920 $pargs = array('page' => $page+1);
921 $newargs = $args ? array_merge($args, $pargs) : $pargs;
922 $this->elementStart('li', array('class' => 'nav_next'));
923 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
925 $this->elementEnd('li');
927 if ($have_before || $have_after) {
928 $this->elementEnd('ul');
929 $this->elementEnd('dd');
930 $this->elementEnd('dl');
931 $this->elementEnd('div');