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 if (Event::handle('StartShowStyles', array($this))) {
155 if (Event::handle('StartShowLaconicaStyles', array($this))) {
156 $this->element('link', array('rel' => 'stylesheet',
157 'type' => 'text/css',
158 'href' => theme_path('css/display.css', 'base') . '?version=' . LACONICA_VERSION,
159 'media' => 'screen, projection, tv'));
160 $this->element('link', array('rel' => 'stylesheet',
161 'type' => 'text/css',
162 'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
163 'media' => 'screen, projection, tv'));
164 Event::handle('EndShowLaconicaStyles', array($this));
166 if (Event::handle('StartShowUAStyles', array($this))) {
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]');
178 Event::handle('EndShowUAStyles', array($this));
180 Event::handle('EndShowStyles', array($this));
185 * Show javascript headers
189 function showScripts()
191 if (Event::handle('StartShowScripts', array($this))) {
192 if (Event::handle('StartShowJQueryScripts', array($this))) {
193 $this->element('script', array('type' => 'text/javascript',
194 'src' => common_path('js/jquery.min.js')),
196 $this->element('script', array('type' => 'text/javascript',
197 'src' => common_path('js/jquery.form.js')),
199 Event::handle('EndShowJQueryScripts', array($this));
201 if (Event::handle('StartShowLaconicaScripts', array($this))) {
202 $this->element('script', array('type' => 'text/javascript',
203 'src' => common_path('js/xbImportNode.js')),
205 $this->element('script', array('type' => 'text/javascript',
206 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
208 // Frame-busting code to avoid clickjacking attacks.
209 $this->element('script', array('type' => 'text/javascript'),
210 'if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
211 Event::handle('EndShowLaconicaScripts', array($this));
213 Event::handle('EndShowScripts', array($this));
218 * Show OpenSearch headers
222 function showOpenSearch()
224 $this->element('link', array('rel' => 'search',
225 'type' => 'application/opensearchdescription+xml',
226 'href' => common_local_url('opensearch', array('type' => 'people')),
227 'title' => common_config('site', 'name').' People Search'));
228 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
229 'href' => common_local_url('opensearch', array('type' => 'notice')),
230 'title' => common_config('site', 'name').' Notice Search'));
243 $feeds = $this->getFeeds();
246 foreach ($feeds as $feed) {
247 $this->element('link', array('rel' => $feed->rel(),
248 'href' => $feed->url,
249 'type' => $feed->mimeType(),
250 'title' => $feed->title));
262 function showDescription()
264 // does nothing by default
268 * Show extra stuff in <head>.
276 // does nothing by default
282 * Calls template methods
288 $this->elementStart('body', array('id' => $this->trimmed('action')));
289 $this->elementStart('div', array('id' => 'wrap'));
290 if (Event::handle('StartShowHeader', array($this))) {
292 Event::handle('EndShowHeader', array($this));
295 if (Event::handle('StartShowFooter', array($this))) {
297 Event::handle('EndShowFooter', array($this));
299 $this->elementEnd('div');
300 $this->elementEnd('body');
304 * Show header of the page.
306 * Calls template methods
310 function showHeader()
312 $this->elementStart('div', array('id' => 'header'));
314 $this->showPrimaryNav();
315 $this->showSiteNotice();
316 if (common_logged_in()) {
317 $this->showNoticeForm();
319 $this->showAnonymousMessage();
321 $this->elementEnd('div');
325 * Show configured logo.
331 $this->elementStart('address', array('id' => 'site_contact',
332 'class' => 'vcard'));
333 $this->elementStart('a', array('class' => 'url home bookmark',
334 'href' => common_local_url('public')));
335 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
336 $this->element('img', array('class' => 'logo photo',
337 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
338 'alt' => common_config('site', 'name')));
340 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
341 $this->elementEnd('a');
342 $this->elementEnd('address');
346 * Show primary navigation.
350 function showPrimaryNav()
352 $user = common_current_user();
354 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
355 $this->element('dt', null, _('Primary site navigation'));
356 $this->elementStart('dd');
357 $this->elementStart('ul', array('class' => 'nav'));
358 if (Event::handle('StartPrimaryNav', array($this))) {
360 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
361 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
363 $this->menuItem(common_local_url('peoplesearch'),
364 _('Search'), _('Search for people or text'), false, 'nav_search');
366 $this->menuItem(common_local_url('profilesettings'),
367 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
369 if (common_config('xmpp', 'enabled')) {
370 $this->menuItem(common_local_url('imsettings'),
371 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
373 $this->menuItem(common_local_url('smssettings'),
374 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
376 $this->menuItem(common_local_url('logout'),
377 _('Logout'), _('Logout from the site'), false, 'nav_logout');
379 $this->menuItem(common_local_url('login'),
380 _('Login'), _('Login to the site'), false, 'nav_login');
381 if (!common_config('site', 'closed')) {
382 $this->menuItem(common_local_url('register'),
383 _('Register'), _('Create an account'), false, 'nav_register');
385 $this->menuItem(common_local_url('openidlogin'),
386 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
388 $this->menuItem(common_local_url('doc', array('title' => 'help')),
389 _('Help'), _('Help me!'), false, 'nav_help');
390 Event::handle('EndPrimaryNav', array($this));
392 $this->elementEnd('ul');
393 $this->elementEnd('dd');
394 $this->elementEnd('dl');
402 function showSiteNotice()
404 // Revist. Should probably do an hAtom pattern here
405 $text = common_config('site', 'notice');
407 $this->elementStart('dl', array('id' => 'site_notice',
408 'class' => 'system_notice'));
409 $this->element('dt', null, _('Site notice'));
410 $this->elementStart('dd', null);
412 $this->elementEnd('dd');
413 $this->elementEnd('dl');
420 * MAY overload if no notice form needed... or direct message box????
424 function showNoticeForm()
426 $notice_form = new NoticeForm($this);
427 $notice_form->show();
431 * Show anonymous message.
437 function showAnonymousMessage()
439 // needs to be defined by the class
445 * Shows local navigation, content block and aside.
451 $this->elementStart('div', array('id' => 'core'));
452 $this->showLocalNavBlock();
453 if (Event::handle('StartShowContentBlock', array($this))) {
454 $this->showContentBlock();
455 Event::handle('EndShowContentBlock', array($this));
458 $this->elementEnd('div');
462 * Show local navigation block.
466 function showLocalNavBlock()
468 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
469 $this->element('dt', null, _('Local views'));
470 $this->elementStart('dd');
471 $this->showLocalNav();
472 $this->elementEnd('dd');
473 $this->elementEnd('dl');
477 * Show local navigation.
483 function showLocalNav()
485 // does nothing by default
489 * Show content block.
493 function showContentBlock()
495 $this->elementStart('div', array('id' => 'content'));
496 $this->showPageTitle();
497 $this->showPageNoticeBlock();
498 $this->elementStart('div', array('id' => 'content_inner'));
499 // show the actual content (forms, lists, whatever)
500 $this->showContent();
501 $this->elementEnd('div');
502 $this->elementEnd('div');
510 function showPageTitle()
512 $this->element('h1', null, $this->title());
516 * Show page notice block.
520 function showPageNoticeBlock()
522 $this->elementStart('dl', array('id' => 'page_notice',
523 'class' => 'system_notice'));
524 $this->element('dt', null, _('Page notice'));
525 $this->elementStart('dd');
526 $this->showPageNotice();
527 $this->elementEnd('dd');
528 $this->elementEnd('dl');
534 * SHOULD overload (unless there's not a notice)
538 function showPageNotice()
545 * MUST overload (unless there's not a notice)
549 function showContent()
561 $this->elementStart('div', array('id' => 'aside_primary',
562 'class' => 'aside'));
563 $this->showExportData();
564 if (Event::handle('StartShowSections', array($this))) {
565 $this->showSections();
566 Event::handle('EndShowSections', array($this));
568 $this->elementEnd('div');
572 * Show export data feeds.
577 function showExportData()
579 $feeds = $this->getFeeds();
581 $fl = new FeedList($this);
593 function showSections()
595 // for each section, show it
603 function showFooter()
605 $this->elementStart('div', array('id' => 'footer'));
606 $this->showSecondaryNav();
607 $this->showLicenses();
608 $this->elementEnd('div');
612 * Show secondary navigation.
616 function showSecondaryNav()
618 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
619 $this->element('dt', null, _('Secondary site navigation'));
620 $this->elementStart('dd', null);
621 $this->elementStart('ul', array('class' => 'nav'));
622 if (Event::handle('StartSecondaryNav', array($this))) {
623 $this->menuItem(common_local_url('doc', array('title' => 'help')),
625 $this->menuItem(common_local_url('doc', array('title' => 'about')),
627 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
629 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
631 $this->menuItem(common_local_url('doc', array('title' => 'source')),
633 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
635 Event::handle('EndSecondaryNav', array($this));
637 $this->elementEnd('ul');
638 $this->elementEnd('dd');
639 $this->elementEnd('dl');
647 function showLicenses()
649 $this->elementStart('dl', array('id' => 'licenses'));
650 $this->showLaconicaLicense();
651 $this->showContentLicense();
652 $this->elementEnd('dl');
656 * Show Laconica license.
660 function showLaconicaLicense()
662 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
663 $this->elementStart('dd', null);
664 if (common_config('site', 'broughtby')) {
665 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
667 $instr = _('**%%site.name%%** is a microblogging service. ');
669 $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);
670 $output = common_markup_to_html($instr);
672 $this->elementEnd('dd');
677 * Show content license.
681 function showContentLicense()
683 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
684 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
685 $this->elementStart('p');
686 $this->element('img', array('id' => 'license_cc',
687 'src' => common_config('license', 'image'),
688 'alt' => common_config('license', 'title')));
689 //TODO: This is dirty: i18n
690 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
691 $this->element('a', array('class' => 'license',
692 'rel' => 'external license',
693 'href' => common_config('license', 'url')),
694 common_config('license', 'title'));
695 $this->text(_('license.'));
696 $this->elementEnd('p');
697 $this->elementEnd('dd');
701 * Return last modified, if applicable.
705 * @return string last modified http header
707 function lastModified()
709 // For comparison with If-Last-Modified
710 // If not applicable, return null
715 * Return etag, if applicable.
719 * @return string etag http header
727 * Return true if read only.
731 * @return boolean is read only action?
733 function isReadOnly()
739 * Returns query argument or default value if not found
741 * @param string $key requested argument
742 * @param string $def default value to return if $key is not provided
744 * @return boolean is read only action?
746 function arg($key, $def=null)
748 if (array_key_exists($key, $this->args)) {
749 return $this->args[$key];
756 * Returns trimmed query argument or default value if not found
758 * @param string $key requested argument
759 * @param string $def default value to return if $key is not provided
761 * @return boolean is read only action?
763 function trimmed($key, $def=null)
765 $arg = $this->arg($key, $def);
766 return is_string($arg) ? trim($arg) : $arg;
772 * @param array $argarray is ignored since it's now passed in in prepare()
774 * @return boolean is read only action?
776 function handle($argarray=null)
778 $lm = $this->lastModified();
779 $etag = $this->etag();
781 header('ETag: ' . $etag);
784 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
785 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
786 if ($if_modified_since) {
787 $ims = strtotime($if_modified_since);
790 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
791 header('HTTP/1.1 304 Not Modified');
792 // Better way to do this?
801 * HasĀ etag? (private)
803 * @param string $etag etag http header
804 * @param string $if_none_match ifNoneMatch http header
808 function _hasEtag($etag, $if_none_match)
810 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
814 * Boolean understands english (yes, no, true, false)
816 * @param string $key query key we're interested in
817 * @param string $def default value
819 * @return boolean interprets yes/no strings as boolean
821 function boolean($key, $def=false)
823 $arg = strtolower($this->trimmed($key));
827 } else if (in_array($arg, array('true', 'yes', '1'))) {
829 } else if (in_array($arg, array('false', 'no', '0'))) {
839 * @param string $msg error message to display
840 * @param integer $code http error code, 500 by default
845 function serverError($msg, $code=500)
847 $action = $this->trimmed('action');
848 common_debug("Server error '$code' on '$action': $msg", __FILE__);
849 throw new ServerException($msg, $code);
855 * @param string $msg error message to display
856 * @param integer $code http error code, 400 by default
861 function clientError($msg, $code=400)
863 $action = $this->trimmed('action');
864 common_debug("User error '$code' on '$action': $msg", __FILE__);
865 throw new ClientException($msg, $code);
869 * Returns the current URL
871 * @return string current URL
875 $action = $this->trimmed('action');
877 unset($args['action']);
878 foreach (array_keys($_COOKIE) as $cookie) {
879 unset($args[$cookie]);
881 return common_local_url($action, $args);
885 * Generate a menu item
887 * @param string $url menu URL
888 * @param string $text menu name
889 * @param string $title title attribute, null by default
890 * @param boolean $is_selected current menu item, false by default
891 * @param string $id element id, null by default
895 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
897 // Added @id to li for some control.
898 // XXX: We might want to move this to htmloutputter.php
901 $lattrs['class'] = 'current';
904 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
906 $this->elementStart('li', $lattrs);
907 $attrs['href'] = $url;
909 $attrs['title'] = $title;
911 $this->element('a', $attrs, $text);
912 $this->elementEnd('li');
916 * Generate pagination links
918 * @param boolean $have_before is there something before?
919 * @param boolean $have_after is there something after?
920 * @param integer $page current page
921 * @param string $action current action
922 * @param array $args rest of query arguments
926 function pagination($have_before, $have_after, $page, $action, $args=null)
928 // Does a little before-after block for next/prev page
929 if ($have_before || $have_after) {
930 $this->elementStart('div', array('class' => 'pagination'));
931 $this->elementStart('dl', null);
932 $this->element('dt', null, _('Pagination'));
933 $this->elementStart('dd', null);
934 $this->elementStart('ul', array('class' => 'nav'));
937 $pargs = array('page' => $page-1);
938 $newargs = $args ? array_merge($args, $pargs) : $pargs;
939 $this->elementStart('li', array('class' => 'nav_prev'));
940 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
942 $this->elementEnd('li');
945 $pargs = array('page' => $page+1);
946 $newargs = $args ? array_merge($args, $pargs) : $pargs;
947 $this->elementStart('li', array('class' => 'nav_next'));
948 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
950 $this->elementEnd('li');
952 if ($have_before || $have_after) {
953 $this->elementEnd('ul');
954 $this->elementEnd('dd');
955 $this->elementEnd('dl');
956 $this->elementEnd('div');
961 * An array of feeds for this action.
963 * Returns an array of potential feeds for this action.
965 * @return array Feed object to show in head and links