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]');
176 * Show javascript headers
180 function showScripts()
182 $this->element('script', array('type' => 'text/javascript',
183 'src' => common_path('js/jquery.min.js')),
185 $this->element('script', array('type' => 'text/javascript',
186 'src' => common_path('js/jquery.form.js')),
188 $this->element('script', array('type' => 'text/javascript',
189 'src' => common_path('js/xbImportNode.js')),
191 $this->element('script', array('type' => 'text/javascript',
192 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
197 * Show OpenSearch headers
201 function showOpenSearch()
203 $this->element('link', array('rel' => 'search',
204 'type' => 'application/opensearchdescription+xml',
205 'href' => common_local_url('opensearch', array('type' => 'people')),
206 'title' => common_config('site', 'name').' People Search'));
207 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
208 'href' => common_local_url('opensearch', array('type' => 'notice')),
209 'title' => common_config('site', 'name').' Notice Search'));
221 // does nothing by default
231 function showDescription()
233 // does nothing by default
237 * Show extra stuff in <head>.
245 // does nothing by default
251 * Calls template methods
257 $this->elementStart('body', array('id' => $this->trimmed('action')));
258 $this->elementStart('div', array('id' => '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 $user = common_current_user();
317 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
318 $this->element('dt', null, _('Primary site navigation'));
319 $this->elementStart('dd');
320 $this->elementStart('ul', array('class' => 'nav'));
321 if (Event::handle('StartPrimaryNav', array($this))) {
323 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
324 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
326 $this->menuItem(common_local_url('peoplesearch'),
327 _('Search'), _('Search for people or text'), false, 'nav_search');
329 $this->menuItem(common_local_url('profilesettings'),
330 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
332 if (common_config('xmpp', 'enabled')) {
333 $this->menuItem(common_local_url('imsettings'),
334 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
336 $this->menuItem(common_local_url('smssettings'),
337 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
339 $this->menuItem(common_local_url('logout'),
340 _('Logout'), _('Logout from the site'), false, 'nav_logout');
342 $this->menuItem(common_local_url('login'),
343 _('Login'), _('Login to the site'), false, 'nav_login');
344 if (!common_config('site', 'closed')) {
345 $this->menuItem(common_local_url('register'),
346 _('Register'), _('Create an account'), false, 'nav_register');
348 $this->menuItem(common_local_url('openidlogin'),
349 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
351 $this->menuItem(common_local_url('doc', array('title' => 'help')),
352 _('Help'), _('Help me!'), false, 'nav_help');
353 Event::handle('EndPrimaryNav', array($this));
355 $this->elementEnd('ul');
356 $this->elementEnd('dd');
357 $this->elementEnd('dl');
365 function showSiteNotice()
367 // Revist. Should probably do an hAtom pattern here
368 $text = common_config('site', 'notice');
370 $this->elementStart('dl', array('id' => 'site_notice',
371 'class' => 'system_notice'));
372 $this->element('dt', null, _('Site notice'));
373 $this->elementStart('dd', null);
375 $this->elementEnd('dd');
376 $this->elementEnd('dl');
383 * MAY overload if no notice form needed... or direct message box????
387 function showNoticeForm()
389 $notice_form = new NoticeForm($this);
390 $notice_form->show();
394 * Show anonymous message.
400 function showAnonymousMessage()
402 // needs to be defined by the class
408 * Shows local navigation, content block and aside.
414 $this->elementStart('div', array('id' => 'core'));
415 $this->showLocalNavBlock();
416 $this->showContentBlock();
418 $this->elementEnd('div');
422 * Show local navigation block.
426 function showLocalNavBlock()
428 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
429 $this->element('dt', null, _('Local views'));
430 $this->elementStart('dd');
431 $this->showLocalNav();
432 $this->elementEnd('dd');
433 $this->elementEnd('dl');
437 * Show local navigation.
443 function showLocalNav()
445 // does nothing by default
449 * Show content block.
453 function showContentBlock()
455 $this->elementStart('div', array('id' => 'content'));
456 $this->showPageTitle();
457 $this->showPageNoticeBlock();
458 $this->elementStart('div', array('id' => 'content_inner'));
459 // show the actual content (forms, lists, whatever)
460 $this->showContent();
461 $this->elementEnd('div');
462 $this->elementEnd('div');
470 function showPageTitle()
472 $this->element('h1', null, $this->title());
476 * Show page notice block.
480 function showPageNoticeBlock()
482 $this->elementStart('dl', array('id' => 'page_notice',
483 'class' => 'system_notice'));
484 $this->element('dt', null, _('Page notice'));
485 $this->elementStart('dd');
486 $this->showPageNotice();
487 $this->elementEnd('dd');
488 $this->elementEnd('dl');
494 * SHOULD overload (unless there's not a notice)
498 function showPageNotice()
505 * MUST overload (unless there's not a notice)
509 function showContent()
520 $this->elementStart('div', array('id' => 'aside_primary',
521 'class' => 'aside'));
522 $this->showExportData();
523 $this->showSections();
524 $this->elementEnd('div');
528 * Show export data feeds.
530 * MAY overload if there are feeds
534 function showExportData()
536 // is there structure to this?
537 // list of (visible!) feed links
538 // can we reuse list of feeds from showFeeds() ?
548 function showSections()
550 // for each section, show it
558 function showFooter()
560 $this->elementStart('div', array('id' => 'footer'));
561 $this->showSecondaryNav();
562 $this->showLicenses();
563 $this->elementEnd('div');
567 * Show secondary navigation.
571 function showSecondaryNav()
573 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
574 $this->element('dt', null, _('Secondary site navigation'));
575 $this->elementStart('dd', null);
576 $this->elementStart('ul', array('class' => 'nav'));
577 if (Event::handle('StartSecondaryNav', array($this))) {
578 $this->menuItem(common_local_url('doc', array('title' => 'help')),
580 $this->menuItem(common_local_url('doc', array('title' => 'about')),
582 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
584 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
586 $this->menuItem(common_local_url('doc', array('title' => 'source')),
588 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
590 Event::handle('EndSecondaryNav', array($this));
592 $this->elementEnd('ul');
593 $this->elementEnd('dd');
594 $this->elementEnd('dl');
602 function showLicenses()
604 $this->elementStart('dl', array('id' => 'licenses'));
605 $this->showLaconicaLicense();
606 $this->showContentLicense();
607 $this->elementEnd('dl');
611 * Show Laconica license.
615 function showLaconicaLicense()
617 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
618 $this->elementStart('dd', null);
619 if (common_config('site', 'broughtby')) {
620 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
622 $instr = _('**%%site.name%%** is a microblogging service. ');
624 $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);
625 $output = common_markup_to_html($instr);
627 $this->elementEnd('dd');
632 * Show content license.
636 function showContentLicense()
638 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
639 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
640 $this->elementStart('p');
641 $this->element('img', array('id' => 'license_cc',
642 'src' => common_config('license', 'image'),
643 'alt' => common_config('license', 'title')));
644 //TODO: This is dirty: i18n
645 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
646 $this->element('a', array('class' => 'license',
647 'rel' => 'external license',
648 'href' => common_config('license', 'url')),
649 common_config('license', 'title'));
650 $this->text(_('license.'));
651 $this->elementEnd('p');
652 $this->elementEnd('dd');
656 * Return last modified, if applicable.
660 * @return string last modified http header
662 function lastModified()
664 // For comparison with If-Last-Modified
665 // If not applicable, return null
670 * Return etag, if applicable.
674 * @return string etag http header
682 * Return true if read only.
686 * @return boolean is read only action?
688 function isReadOnly()
694 * Returns query argument or default value if not found
696 * @param string $key requested argument
697 * @param string $def default value to return if $key is not provided
699 * @return boolean is read only action?
701 function arg($key, $def=null)
703 if (array_key_exists($key, $this->args)) {
704 return $this->args[$key];
711 * Returns trimmed query argument or default value if not found
713 * @param string $key requested argument
714 * @param string $def default value to return if $key is not provided
716 * @return boolean is read only action?
718 function trimmed($key, $def=null)
720 $arg = $this->arg($key, $def);
721 return is_string($arg) ? trim($arg) : $arg;
727 * @param array $argarray is ignored since it's now passed in in prepare()
729 * @return boolean is read only action?
731 function handle($argarray=null)
733 $lm = $this->lastModified();
734 $etag = $this->etag();
736 header('ETag: ' . $etag);
739 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
740 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
741 if ($if_modified_since) {
742 $ims = strtotime($if_modified_since);
745 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
746 header('HTTP/1.1 304 Not Modified');
747 // Better way to do this?
756 * HasĀ etag? (private)
758 * @param string $etag etag http header
759 * @param string $if_none_match ifNoneMatch http header
763 function _hasEtag($etag, $if_none_match)
765 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
769 * Boolean understands english (yes, no, true, false)
771 * @param string $key query key we're interested in
772 * @param string $def default value
774 * @return boolean interprets yes/no strings as boolean
776 function boolean($key, $def=false)
778 $arg = strtolower($this->trimmed($key));
782 } else if (in_array($arg, array('true', 'yes', '1'))) {
784 } else if (in_array($arg, array('false', 'no', '0'))) {
794 * @param string $msg error message to display
795 * @param integer $code http error code, 500 by default
800 function serverError($msg, $code=500)
802 $action = $this->trimmed('action');
803 common_debug("Server error '$code' on '$action': $msg", __FILE__);
804 throw new ServerException($msg, $code);
810 * @param string $msg error message to display
811 * @param integer $code http error code, 400 by default
816 function clientError($msg, $code=400)
818 $action = $this->trimmed('action');
819 common_debug("User error '$code' on '$action': $msg", __FILE__);
820 throw new ClientException($msg, $code);
824 * Returns the current URL
826 * @return string current URL
830 $action = $this->trimmed('action');
832 unset($args['action']);
833 foreach (array_keys($_COOKIE) as $cookie) {
834 unset($args[$cookie]);
836 return common_local_url($action, $args);
840 * Generate a menu item
842 * @param string $url menu URL
843 * @param string $text menu name
844 * @param string $title title attribute, null by default
845 * @param boolean $is_selected current menu item, false by default
846 * @param string $id element id, null by default
850 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
852 // Added @id to li for some control.
853 // XXX: We might want to move this to htmloutputter.php
856 $lattrs['class'] = 'current';
859 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
861 $this->elementStart('li', $lattrs);
862 $attrs['href'] = $url;
864 $attrs['title'] = $title;
866 $this->element('a', $attrs, $text);
867 $this->elementEnd('li');
871 * Generate pagination links
873 * @param boolean $have_before is there something before?
874 * @param boolean $have_after is there something after?
875 * @param integer $page current page
876 * @param string $action current action
877 * @param array $args rest of query arguments
881 function pagination($have_before, $have_after, $page, $action, $args=null)
883 // Does a little before-after block for next/prev page
884 if ($have_before || $have_after) {
885 $this->elementStart('div', array('class' => 'pagination'));
886 $this->elementStart('dl', null);
887 $this->element('dt', null, _('Pagination'));
888 $this->elementStart('dd', null);
889 $this->elementStart('ul', array('class' => 'nav'));
892 $pargs = array('page' => $page-1);
893 $newargs = $args ? array_merge($args, $pargs) : $pargs;
894 $this->elementStart('li', array('class' => 'nav_prev'));
895 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
897 $this->elementEnd('li');
900 $pargs = array('page' => $page+1);
901 $newargs = $args ? array_merge($args, $pargs) : $pargs;
902 $this->elementStart('li', array('class' => 'nav_next'));
903 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
905 $this->elementEnd('li');
907 if ($have_before || $have_after) {
908 $this->elementEnd('ul');
909 $this->elementEnd('dd');
910 $this->elementEnd('dl');
911 $this->elementEnd('div');