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/thickbox.css', 'base') . '?version=' . LACONICA_VERSION,
162 'media' => 'screen, projection, tv'));
163 $this->element('link', array('rel' => 'stylesheet',
164 'type' => 'text/css',
165 'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
166 'media' => 'screen, projection, tv'));
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('ie'.$ver.'.css'))) {
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]');
179 * Show javascript headers
183 function showScripts()
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 $this->element('script', array('type' => 'text/javascript',
192 'src' => common_path('js/xbImportNode.js')),
194 $this->element('script', array('type' => 'text/javascript',
195 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
200 * Show OpenSearch headers
204 function showOpenSearch()
206 $this->element('link', array('rel' => 'search',
207 'type' => 'application/opensearchdescription+xml',
208 'href' => common_local_url('opensearch', array('type' => 'people')),
209 'title' => common_config('site', 'name').' People Search'));
210 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
211 'href' => common_local_url('opensearch', array('type' => 'notice')),
212 'title' => common_config('site', 'name').' Notice Search'));
224 // does nothing by default
234 function showDescription()
236 // does nothing by default
240 * Show extra stuff in <head>.
248 // does nothing by default
255 * Calls template methods
261 $this->elementStart('body', array('id' => $this->trimmed('action')));
262 $this->elementStart('div', 'wrap');
266 $this->elementEnd('div');
267 $this->elementEnd('body');
271 * Show header of the page.
273 * Calls template methods
277 function showHeader()
279 $this->elementStart('div', array('id' => 'header'));
281 $this->showPrimaryNav();
282 $this->showSiteNotice();
283 if (common_logged_in()) {
284 $this->showNoticeForm();
286 $this->showAnonymousMessage();
288 $this->elementEnd('div');
292 * Show configured logo.
298 $this->elementStart('address', array('id' => 'site_contact',
299 'class' => 'vcard'));
300 $this->elementStart('a', array('class' => 'url home bookmark',
301 'href' => common_local_url('public')));
302 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
303 $this->element('img', array('class' => 'logo photo',
304 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
305 'alt' => common_config('site', 'name')));
307 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
308 $this->elementEnd('a');
309 $this->elementEnd('address');
313 * Show primary navigation.
317 function showPrimaryNav()
319 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
320 $this->element('dt', null, _('Primary site navigation'));
321 $this->elementStart('dd');
322 $user = common_current_user();
323 $this->elementStart('ul', array('class' => 'nav'));
325 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
326 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
328 $this->menuItem(common_local_url('peoplesearch'),
329 _('Search'), _('Search for people or text'), false, 'nav_search');
331 $this->menuItem(common_local_url('profilesettings'),
332 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
333 $this->menuItem(common_local_url('imsettings'),
334 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
335 $this->menuItem(common_local_url('logout'),
336 _('Logout'), _('Logout from the site'), false, 'nav_logout');
338 $this->menuItem(common_local_url('login'),
339 _('Login'), _('Login to the site'), false, 'nav_login');
340 if (!common_config('site', 'closed')) {
341 $this->menuItem(common_local_url('register'),
342 _('Register'), _('Create an account'), false, 'nav_register');
344 $this->menuItem(common_local_url('openidlogin'),
345 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
347 $this->menuItem(common_local_url('doc', array('title' => 'help')),
348 _('Help'), _('Help me!'), false, 'nav_help');
349 $this->elementEnd('ul');
350 $this->elementEnd('dd');
351 $this->elementEnd('dl');
359 function showSiteNotice()
361 // Revist. Should probably do an hAtom pattern here
362 $text = common_config('site', 'notice');
364 $this->elementStart('dl', array('id' => 'site_notice',
365 'class' => 'system_notice'));
366 $this->element('dt', null, _('Site notice'));
367 $this->element('dd', null, $text);
368 $this->elementEnd('dl');
375 * MAY overload if no notice form needed... or direct message box????
379 function showNoticeForm()
381 $notice_form = new NoticeForm($this);
382 $notice_form->show();
386 * Show anonymous message.
392 function showAnonymousMessage()
394 // needs to be defined by the class
400 * Shows local navigation, content block and aside.
406 $this->elementStart('div', array('id' => 'core'));
407 $this->showLocalNavBlock();
408 $this->showContentBlock();
410 $this->elementEnd('div');
414 * Show local navigation block.
418 function showLocalNavBlock()
420 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
421 $this->element('dt', null, _('Local views'));
422 $this->elementStart('dd');
423 $this->showLocalNav();
424 $this->elementEnd('dd');
425 $this->elementEnd('dl');
429 * Show local navigation.
435 function showLocalNav()
437 // does nothing by default
441 * Show content block.
445 function showContentBlock()
447 $this->elementStart('div', array('id' => 'content'));
448 $this->showPageTitle();
449 $this->showPageNoticeBlock();
450 $this->elementStart('div', array('id' => 'content_inner'));
451 // show the actual content (forms, lists, whatever)
452 $this->showContent();
453 $this->elementEnd('div');
454 $this->elementEnd('div');
462 function showPageTitle()
464 $this->element('h1', null, $this->title());
468 * Show page notice block.
472 function showPageNoticeBlock()
474 $this->elementStart('dl', array('id' => 'page_notice',
475 'class' => 'system_notice'));
476 $this->element('dt', null, _('Page notice'));
477 $this->elementStart('dd');
478 $this->showPageNotice();
479 $this->elementEnd('dd');
480 $this->elementEnd('dl');
486 * SHOULD overload (unless there's not a notice)
490 function showPageNotice()
497 * MUST overload (unless there's not a notice)
501 function showContent()
512 $this->elementStart('div', array('id' => 'aside_primary',
513 'class' => 'aside'));
514 $this->showExportData();
515 $this->showSections();
516 $this->elementEnd('div');
520 * Show export data feeds.
522 * MAY overload if there are feeds
526 function showExportData()
528 // is there structure to this?
529 // list of (visible!) feed links
530 // can we reuse list of feeds from showFeeds() ?
540 function showSections()
542 // for each section, show it
550 function showFooter()
552 $this->elementStart('div', array('id' => 'footer'));
553 $this->showSecondaryNav();
554 $this->showLicenses();
555 $this->elementEnd('div');
559 * Show secondary navigation.
563 function showSecondaryNav()
565 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
566 $this->element('dt', null, _('Secondary site navigation'));
567 $this->elementStart('dd', null);
568 $this->elementStart('ul', array('class' => 'nav'));
569 $this->menuItem(common_local_url('doc', array('title' => 'help')),
571 $this->menuItem(common_local_url('doc', array('title' => 'about')),
573 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
575 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
577 $this->menuItem(common_local_url('doc', array('title' => 'source')),
579 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
581 $this->elementEnd('ul');
582 $this->elementEnd('dd');
583 $this->elementEnd('dl');
591 function showLicenses()
593 $this->elementStart('dl', array('id' => 'licenses'));
594 $this->showLaconicaLicense();
595 $this->showContentLicense();
596 $this->elementEnd('dl');
600 * Show Laconica license.
604 function showLaconicaLicense()
606 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
607 $this->elementStart('dd', null);
608 if (common_config('site', 'broughtby')) {
609 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
611 $instr = _('**%%site.name%%** is a microblogging service. ');
613 $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);
614 $output = common_markup_to_html($instr);
616 $this->elementEnd('dd');
621 * Show content license.
625 function showContentLicense()
627 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
628 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
629 $this->elementStart('p');
630 $this->element('img', array('id' => 'license_cc',
631 'src' => common_config('license', 'image'),
632 'alt' => common_config('license', 'title')));
633 //TODO: This is dirty: i18n
634 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
635 $this->element('a', array('class' => 'license',
636 'rel' => 'external license',
637 'href' => common_config('license', 'url')),
638 common_config('license', 'title'));
639 $this->text(_('license.'));
640 $this->elementEnd('p');
641 $this->elementEnd('dd');
645 * Return last modified, if applicable.
649 * @return string last modified http header
651 function lastModified()
653 // For comparison with If-Last-Modified
654 // If not applicable, return null
659 * Return etag, if applicable.
663 * @return string etag http header
671 * Return true if read only.
675 * @return boolean is read only action?
677 function isReadOnly()
683 * Returns query argument or default value if not found
685 * @param string $key requested argument
686 * @param string $def default value to return if $key is not provided
688 * @return boolean is read only action?
690 function arg($key, $def=null)
692 if (array_key_exists($key, $this->args)) {
693 return $this->args[$key];
700 * Returns trimmed query argument or default value if not found
702 * @param string $key requested argument
703 * @param string $def default value to return if $key is not provided
705 * @return boolean is read only action?
707 function trimmed($key, $def=null)
709 $arg = $this->arg($key, $def);
710 return is_string($arg) ? trim($arg) : $arg;
716 * @param array $argarray is ignored since it's now passed in in prepare()
718 * @return boolean is read only action?
720 function handle($argarray=null)
722 $lm = $this->lastModified();
723 $etag = $this->etag();
725 header('ETag: ' . $etag);
728 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
729 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
730 if ($if_modified_since) {
731 $ims = strtotime($if_modified_since);
734 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
735 header('HTTP/1.1 304 Not Modified');
736 // Better way to do this?
745 * HasĀ etag? (private)
747 * @param string $etag etag http header
748 * @param string $if_none_match ifNoneMatch http header
752 function _hasEtag($etag, $if_none_match)
754 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
758 * Boolean understands english (yes, no, true, false)
760 * @param string $key query key we're interested in
761 * @param string $def default value
763 * @return boolean interprets yes/no strings as boolean
765 function boolean($key, $def=false)
767 $arg = strtolower($this->trimmed($key));
771 } else if (in_array($arg, array('true', 'yes', '1'))) {
773 } else if (in_array($arg, array('false', 'no', '0'))) {
783 * @param string $msg error message to display
784 * @param integer $code http error code, 500 by default
788 function serverError($msg, $code=500)
790 $action = $this->trimmed('action');
791 common_debug("Server error '$code' on '$action': $msg", __FILE__);
792 common_server_error($msg, $code);
798 * @param string $msg error message to display
799 * @param integer $code http error code, 400 by default
803 function clientError($msg, $code=400)
805 $action = $this->trimmed('action');
806 common_debug("User error '$code' on '$action': $msg", __FILE__);
807 common_user_error($msg, $code);
811 * Returns the current URL
813 * @return string current URL
817 $action = $this->trimmed('action');
819 unset($args['action']);
820 foreach (array_keys($_COOKIE) as $cookie) {
821 unset($args[$cookie]);
823 return common_local_url($action, $args);
827 * Generate a menu item
829 * @param string $url menu URL
830 * @param string $text menu name
831 * @param string $title title attribute, null by default
832 * @param boolean $is_selected current menu item, false by default
833 * @param string $id element id, null by default
837 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
839 // Added @id to li for some control.
840 // XXX: We might want to move this to htmloutputter.php
843 $lattrs['class'] = 'current';
846 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
848 $this->elementStart('li', $lattrs);
849 $attrs['href'] = $url;
851 $attrs['title'] = $title;
853 $this->element('a', $attrs, $text);
854 $this->elementEnd('li');
858 * Generate pagination links
860 * @param boolean $have_before is there something before?
861 * @param boolean $have_after is there something after?
862 * @param integer $page current page
863 * @param string $action current action
864 * @param array $args rest of query arguments
868 function pagination($have_before, $have_after, $page, $action, $args=null)
870 // Does a little before-after block for next/prev page
871 if ($have_before || $have_after) {
872 $this->elementStart('div', array('class' => 'pagination'));
873 $this->elementStart('dl', null);
874 $this->element('dt', null, _('Pagination'));
875 $this->elementStart('dd', null);
876 $this->elementStart('ul', array('class' => 'nav'));
879 $pargs = array('page' => $page-1);
880 $newargs = $args ? array_merge($args, $pargs) : $pargs;
881 $this->elementStart('li', array('class' => 'nav_prev'));
882 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
884 $this->elementEnd('li');
887 $pargs = array('page' => $page+1);
888 $newargs = $args ? array_merge($args, $pargs) : $pargs;
889 $this->elementStart('li', array('class' => 'nav_next'));
890 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
892 $this->elementEnd('li');
894 if ($have_before || $have_after) {
895 $this->elementEnd('ul');
896 $this->elementEnd('dd');
897 $this->elementEnd('dl');
898 $this->elementEnd('div');