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->showRelationshipLinks();
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 if (Event::handle('StartShowScripts', array($this))) {
184 if (Event::handle('StartShowJQueryScripts', array($this))) {
185 $this->element('script', array('type' => 'text/javascript',
186 'src' => common_path('js/jquery.min.js')),
188 $this->element('script', array('type' => 'text/javascript',
189 'src' => common_path('js/jquery.form.js')),
191 Event::handle('EndShowJQueryScripts', array($this));
193 if (Event::handle('StartShowLaconicaScripts', array($this))) {
194 $this->element('script', array('type' => 'text/javascript',
195 'src' => common_path('js/xbImportNode.js')),
197 $this->element('script', array('type' => 'text/javascript',
198 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
200 Event::handle('EndShowLaconicaScripts', array($this));
202 Event::handle('EndShowScripts', array($this));
207 * Show document relationship links
213 function showRelationshipLinks()
215 // output <link> elements with appropriate HTML4.01 link types:
216 // http://www.w3.org/TR/html401/types.html#type-links
220 * Show OpenSearch headers
224 function showOpenSearch()
226 $this->element('link', array('rel' => 'search',
227 'type' => 'application/opensearchdescription+xml',
228 'href' => common_local_url('opensearch', array('type' => 'people')),
229 'title' => common_config('site', 'name').' People Search'));
230 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
231 'href' => common_local_url('opensearch', array('type' => 'notice')),
232 'title' => common_config('site', 'name').' Notice Search'));
244 // does nothing by default
254 function showDescription()
256 // does nothing by default
260 * Show extra stuff in <head>.
268 // does nothing by default
274 * Calls template methods
280 $this->elementStart('body', array('id' => $this->trimmed('action')));
281 $this->elementStart('div', array('id' => 'wrap'));
285 $this->elementEnd('div');
286 $this->elementEnd('body');
290 * Show header of the page.
292 * Calls template methods
296 function showHeader()
298 $this->elementStart('div', array('id' => 'header'));
300 $this->showPrimaryNav();
301 $this->showSiteNotice();
302 if (common_logged_in()) {
303 $this->showNoticeForm();
305 $this->showAnonymousMessage();
307 $this->elementEnd('div');
311 * Show configured logo.
317 $this->elementStart('address', array('id' => 'site_contact',
318 'class' => 'vcard'));
319 $this->elementStart('a', array('class' => 'url home bookmark',
320 'href' => common_local_url('public')));
321 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
322 $this->element('img', array('class' => 'logo photo',
323 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
324 'alt' => common_config('site', 'name')));
326 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
327 $this->elementEnd('a');
328 $this->elementEnd('address');
332 * Show primary navigation.
336 function showPrimaryNav()
338 $user = common_current_user();
340 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
341 $this->element('dt', null, _('Primary site navigation'));
342 $this->elementStart('dd');
343 $this->elementStart('ul', array('class' => 'nav'));
344 if (Event::handle('StartPrimaryNav', array($this))) {
346 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
347 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
349 $this->menuItem(common_local_url('peoplesearch'),
350 _('Search'), _('Search for people or text'), false, 'nav_search');
352 $this->menuItem(common_local_url('profilesettings'),
353 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
355 if (common_config('xmpp', 'enabled')) {
356 $this->menuItem(common_local_url('imsettings'),
357 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
359 $this->menuItem(common_local_url('smssettings'),
360 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
362 $this->menuItem(common_local_url('logout'),
363 _('Logout'), _('Logout from the site'), false, 'nav_logout');
365 $this->menuItem(common_local_url('login'),
366 _('Login'), _('Login to the site'), false, 'nav_login');
367 if (!common_config('site', 'closed')) {
368 $this->menuItem(common_local_url('register'),
369 _('Register'), _('Create an account'), false, 'nav_register');
371 $this->menuItem(common_local_url('openidlogin'),
372 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
374 $this->menuItem(common_local_url('doc', array('title' => 'help')),
375 _('Help'), _('Help me!'), false, 'nav_help');
376 Event::handle('EndPrimaryNav', array($this));
378 $this->elementEnd('ul');
379 $this->elementEnd('dd');
380 $this->elementEnd('dl');
388 function showSiteNotice()
390 // Revist. Should probably do an hAtom pattern here
391 $text = common_config('site', 'notice');
393 $this->elementStart('dl', array('id' => 'site_notice',
394 'class' => 'system_notice'));
395 $this->element('dt', null, _('Site notice'));
396 $this->elementStart('dd', null);
398 $this->elementEnd('dd');
399 $this->elementEnd('dl');
406 * MAY overload if no notice form needed... or direct message box????
410 function showNoticeForm()
412 $notice_form = new NoticeForm($this);
413 $notice_form->show();
417 * Show anonymous message.
423 function showAnonymousMessage()
425 // needs to be defined by the class
431 * Shows local navigation, content block and aside.
437 $this->elementStart('div', array('id' => 'core'));
438 $this->showLocalNavBlock();
439 $this->showContentBlock();
441 $this->elementEnd('div');
445 * Show local navigation block.
449 function showLocalNavBlock()
451 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
452 $this->element('dt', null, _('Local views'));
453 $this->elementStart('dd');
454 $this->showLocalNav();
455 $this->elementEnd('dd');
456 $this->elementEnd('dl');
460 * Show local navigation.
466 function showLocalNav()
468 // does nothing by default
472 * Show content block.
476 function showContentBlock()
478 $this->elementStart('div', array('id' => 'content'));
479 $this->showPageTitle();
480 $this->showPageNoticeBlock();
481 $this->elementStart('div', array('id' => 'content_inner'));
482 // show the actual content (forms, lists, whatever)
483 $this->showContent();
484 $this->elementEnd('div');
485 $this->elementEnd('div');
493 function showPageTitle()
495 $this->element('h1', null, $this->title());
499 * Show page notice block.
503 function showPageNoticeBlock()
505 $this->elementStart('dl', array('id' => 'page_notice',
506 'class' => 'system_notice'));
507 $this->element('dt', null, _('Page notice'));
508 $this->elementStart('dd');
509 $this->showPageNotice();
510 $this->elementEnd('dd');
511 $this->elementEnd('dl');
517 * SHOULD overload (unless there's not a notice)
521 function showPageNotice()
528 * MUST overload (unless there's not a notice)
532 function showContent()
544 $this->elementStart('div', array('id' => 'aside_primary',
545 'class' => 'aside'));
546 $this->showExportData();
547 if (Event::handle('StartShowSections', array($this))) {
548 $this->showSections();
549 Event::handle('EndShowSections', array($this));
551 $this->elementEnd('div');
555 * Show export data feeds.
557 * MAY overload if there are feeds
561 function showExportData()
563 // is there structure to this?
564 // list of (visible!) feed links
565 // can we reuse list of feeds from showFeeds() ?
575 function showSections()
577 // for each section, show it
585 function showFooter()
587 $this->elementStart('div', array('id' => 'footer'));
588 $this->showSecondaryNav();
589 $this->showLicenses();
590 $this->elementEnd('div');
594 * Show secondary navigation.
598 function showSecondaryNav()
600 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
601 $this->element('dt', null, _('Secondary site navigation'));
602 $this->elementStart('dd', null);
603 $this->elementStart('ul', array('class' => 'nav'));
604 if (Event::handle('StartSecondaryNav', array($this))) {
605 $this->menuItem(common_local_url('doc', array('title' => 'help')),
607 $this->menuItem(common_local_url('doc', array('title' => 'about')),
609 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
611 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
613 $this->menuItem(common_local_url('doc', array('title' => 'source')),
615 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
617 Event::handle('EndSecondaryNav', array($this));
619 $this->elementEnd('ul');
620 $this->elementEnd('dd');
621 $this->elementEnd('dl');
629 function showLicenses()
631 $this->elementStart('dl', array('id' => 'licenses'));
632 $this->showLaconicaLicense();
633 $this->showContentLicense();
634 $this->elementEnd('dl');
638 * Show Laconica license.
642 function showLaconicaLicense()
644 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
645 $this->elementStart('dd', null);
646 if (common_config('site', 'broughtby')) {
647 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
649 $instr = _('**%%site.name%%** is a microblogging service. ');
651 $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);
652 $output = common_markup_to_html($instr);
654 $this->elementEnd('dd');
659 * Show content license.
663 function showContentLicense()
665 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
666 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
667 $this->elementStart('p');
668 $this->element('img', array('id' => 'license_cc',
669 'src' => common_config('license', 'image'),
670 'alt' => common_config('license', 'title')));
671 //TODO: This is dirty: i18n
672 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
673 $this->element('a', array('class' => 'license',
674 'rel' => 'external license',
675 'href' => common_config('license', 'url')),
676 common_config('license', 'title'));
677 $this->text(_('license.'));
678 $this->elementEnd('p');
679 $this->elementEnd('dd');
683 * Return last modified, if applicable.
687 * @return string last modified http header
689 function lastModified()
691 // For comparison with If-Last-Modified
692 // If not applicable, return null
697 * Return etag, if applicable.
701 * @return string etag http header
709 * Return true if read only.
713 * @return boolean is read only action?
715 function isReadOnly()
721 * Returns query argument or default value if not found
723 * @param string $key requested argument
724 * @param string $def default value to return if $key is not provided
726 * @return boolean is read only action?
728 function arg($key, $def=null)
730 if (array_key_exists($key, $this->args)) {
731 return $this->args[$key];
738 * Returns trimmed query argument or default value if not found
740 * @param string $key requested argument
741 * @param string $def default value to return if $key is not provided
743 * @return boolean is read only action?
745 function trimmed($key, $def=null)
747 $arg = $this->arg($key, $def);
748 return is_string($arg) ? trim($arg) : $arg;
754 * @param array $argarray is ignored since it's now passed in in prepare()
756 * @return boolean is read only action?
758 function handle($argarray=null)
760 $lm = $this->lastModified();
761 $etag = $this->etag();
763 header('ETag: ' . $etag);
766 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
767 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
768 if ($if_modified_since) {
769 $ims = strtotime($if_modified_since);
772 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
773 header('HTTP/1.1 304 Not Modified');
774 // Better way to do this?
783 * HasĀ etag? (private)
785 * @param string $etag etag http header
786 * @param string $if_none_match ifNoneMatch http header
790 function _hasEtag($etag, $if_none_match)
792 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
796 * Boolean understands english (yes, no, true, false)
798 * @param string $key query key we're interested in
799 * @param string $def default value
801 * @return boolean interprets yes/no strings as boolean
803 function boolean($key, $def=false)
805 $arg = strtolower($this->trimmed($key));
809 } else if (in_array($arg, array('true', 'yes', '1'))) {
811 } else if (in_array($arg, array('false', 'no', '0'))) {
821 * @param string $msg error message to display
822 * @param integer $code http error code, 500 by default
827 function serverError($msg, $code=500)
829 $action = $this->trimmed('action');
830 common_debug("Server error '$code' on '$action': $msg", __FILE__);
831 throw new ServerException($msg, $code);
837 * @param string $msg error message to display
838 * @param integer $code http error code, 400 by default
843 function clientError($msg, $code=400)
845 $action = $this->trimmed('action');
846 common_debug("User error '$code' on '$action': $msg", __FILE__);
847 throw new ClientException($msg, $code);
851 * Returns the current URL
853 * @return string current URL
857 $action = $this->trimmed('action');
859 unset($args['action']);
860 foreach (array_keys($_COOKIE) as $cookie) {
861 unset($args[$cookie]);
863 return common_local_url($action, $args);
867 * Generate a menu item
869 * @param string $url menu URL
870 * @param string $text menu name
871 * @param string $title title attribute, null by default
872 * @param boolean $is_selected current menu item, false by default
873 * @param string $id element id, null by default
877 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
879 // Added @id to li for some control.
880 // XXX: We might want to move this to htmloutputter.php
883 $lattrs['class'] = 'current';
886 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
888 $this->elementStart('li', $lattrs);
889 $attrs['href'] = $url;
891 $attrs['title'] = $title;
893 $this->element('a', $attrs, $text);
894 $this->elementEnd('li');
898 * Generate pagination links
900 * @param boolean $have_before is there something before?
901 * @param boolean $have_after is there something after?
902 * @param integer $page current page
903 * @param string $action current action
904 * @param array $args rest of query arguments
908 function pagination($have_before, $have_after, $page, $action, $args=null)
910 // Does a little before-after block for next/prev page
911 if ($have_before || $have_after) {
912 $this->elementStart('div', array('class' => 'pagination'));
913 $this->elementStart('dl', null);
914 $this->element('dt', null, _('Pagination'));
915 $this->elementStart('dd', null);
916 $this->elementStart('ul', array('class' => 'nav'));
919 $pargs = array('page' => $page-1);
920 $newargs = $args ? array_merge($args, $pargs) : $pargs;
921 $this->elementStart('li', array('class' => 'nav_prev'));
922 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
924 $this->elementEnd('li');
927 $pargs = array('page' => $page+1);
928 $newargs = $args ? array_merge($args, $pargs) : $pargs;
929 $this->elementStart('li', array('class' => 'nav_next'));
930 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
932 $this->elementEnd('li');
934 if ($have_before || $have_after) {
935 $this->elementEnd('ul');
936 $this->elementEnd('dd');
937 $this->elementEnd('dl');
938 $this->elementEnd('div');
943 * Generate document metadata for sequential navigation
945 * @param boolean $have_before is there something before?
946 * @param boolean $have_after is there something after?
947 * @param integer $page current page
948 * @param string $action current action
949 * @param array $args rest of query arguments
953 function sequenceRelationships($have_next, $have_previous, $page, $action, $args=null)
955 // Outputs machine-readable pagination in <link> elements.
956 // Pattern taken from $this->pagination() method.
958 // "next" is equivalent to "after"
960 $pargs = array('page' => $page-1);
961 $newargs = $args ? array_merge($args, $pargs) : $pargs;
962 $this->element('link', array('rel' => 'next',
963 'href' => common_local_url($action, $newargs),
964 'title' => _('Next')));
966 // "previous" is equivalent to "before"
967 if ($have_previous=true) { // FIXME
968 $pargs = array('page' => $page+1);
969 $newargs = $args ? array_merge($args, $pargs) : $pargs;
970 $this->element('link', array('rel' => 'prev',
971 'href' => common_local_url($action, $newargs),
972 'title' => _('Previous')));