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 if (Event::handle('StartShowScripts', array($this))) {
183 if (Event::handle('StartShowJQueryScripts', array($this))) {
184 $this->element('script', array('type' => 'text/javascript',
185 'src' => common_path('js/jquery.min.js')),
187 $this->element('script', array('type' => 'text/javascript',
188 'src' => common_path('js/jquery.form.js')),
190 Event::handle('EndShowJQueryScripts', array($this));
192 if (Event::handle('StartShowLaconicaScripts', array($this))) {
193 $this->element('script', array('type' => 'text/javascript',
194 'src' => common_path('js/xbImportNode.js')),
196 $this->element('script', array('type' => 'text/javascript',
197 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
199 Event::handle('EndShowLaconicaScripts', array($this));
201 Event::handle('EndShowScripts', array($this));
206 * Show OpenSearch headers
210 function showOpenSearch()
212 $this->element('link', array('rel' => 'search',
213 'type' => 'application/opensearchdescription+xml',
214 'href' => common_local_url('opensearch', array('type' => 'people')),
215 'title' => common_config('site', 'name').' People Search'));
216 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
217 'href' => common_local_url('opensearch', array('type' => 'notice')),
218 'title' => common_config('site', 'name').' Notice Search'));
230 // does nothing by default
240 function showDescription()
242 // does nothing by default
246 * Show extra stuff in <head>.
254 // does nothing by default
260 * Calls template methods
266 $this->elementStart('body', array('id' => $this->trimmed('action')));
267 $this->elementStart('div', array('id' => 'wrap'));
271 $this->elementEnd('div');
272 $this->elementEnd('body');
276 * Show header of the page.
278 * Calls template methods
282 function showHeader()
284 $this->elementStart('div', array('id' => 'header'));
286 $this->showPrimaryNav();
287 $this->showSiteNotice();
288 if (common_logged_in()) {
289 $this->showNoticeForm();
291 $this->showAnonymousMessage();
293 $this->elementEnd('div');
297 * Show configured logo.
303 $this->elementStart('address', array('id' => 'site_contact',
304 'class' => 'vcard'));
305 $this->elementStart('a', array('class' => 'url home bookmark',
306 'href' => common_local_url('public')));
307 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
308 $this->element('img', array('class' => 'logo photo',
309 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
310 'alt' => common_config('site', 'name')));
312 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
313 $this->elementEnd('a');
314 $this->elementEnd('address');
318 * Show primary navigation.
322 function showPrimaryNav()
324 $user = common_current_user();
326 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
327 $this->element('dt', null, _('Primary site navigation'));
328 $this->elementStart('dd');
329 $this->elementStart('ul', array('class' => 'nav'));
330 if (Event::handle('StartPrimaryNav', array($this))) {
332 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
333 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
335 $this->menuItem(common_local_url('peoplesearch'),
336 _('Search'), _('Search for people or text'), false, 'nav_search');
338 $this->menuItem(common_local_url('profilesettings'),
339 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
341 if (common_config('xmpp', 'enabled')) {
342 $this->menuItem(common_local_url('imsettings'),
343 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
345 $this->menuItem(common_local_url('smssettings'),
346 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
348 $this->menuItem(common_local_url('logout'),
349 _('Logout'), _('Logout from the site'), false, 'nav_logout');
351 $this->menuItem(common_local_url('login'),
352 _('Login'), _('Login to the site'), false, 'nav_login');
353 if (!common_config('site', 'closed')) {
354 $this->menuItem(common_local_url('register'),
355 _('Register'), _('Create an account'), false, 'nav_register');
357 $this->menuItem(common_local_url('openidlogin'),
358 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
360 $this->menuItem(common_local_url('doc', array('title' => 'help')),
361 _('Help'), _('Help me!'), false, 'nav_help');
362 Event::handle('EndPrimaryNav', array($this));
364 $this->elementEnd('ul');
365 $this->elementEnd('dd');
366 $this->elementEnd('dl');
374 function showSiteNotice()
376 // Revist. Should probably do an hAtom pattern here
377 $text = common_config('site', 'notice');
379 $this->elementStart('dl', array('id' => 'site_notice',
380 'class' => 'system_notice'));
381 $this->element('dt', null, _('Site notice'));
382 $this->elementStart('dd', null);
384 $this->elementEnd('dd');
385 $this->elementEnd('dl');
392 * MAY overload if no notice form needed... or direct message box????
396 function showNoticeForm()
398 $notice_form = new NoticeForm($this);
399 $notice_form->show();
403 * Show anonymous message.
409 function showAnonymousMessage()
411 // needs to be defined by the class
417 * Shows local navigation, content block and aside.
423 $this->elementStart('div', array('id' => 'core'));
424 $this->showLocalNavBlock();
425 $this->showContentBlock();
427 $this->elementEnd('div');
431 * Show local navigation block.
435 function showLocalNavBlock()
437 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
438 $this->element('dt', null, _('Local views'));
439 $this->elementStart('dd');
440 $this->showLocalNav();
441 $this->elementEnd('dd');
442 $this->elementEnd('dl');
446 * Show local navigation.
452 function showLocalNav()
454 // does nothing by default
458 * Show content block.
462 function showContentBlock()
464 $this->elementStart('div', array('id' => 'content'));
465 $this->showPageTitle();
466 $this->showPageNoticeBlock();
467 $this->elementStart('div', array('id' => 'content_inner'));
468 // show the actual content (forms, lists, whatever)
469 $this->showContent();
470 $this->elementEnd('div');
471 $this->elementEnd('div');
479 function showPageTitle()
481 $this->element('h1', null, $this->title());
485 * Show page notice block.
489 function showPageNoticeBlock()
491 $this->elementStart('dl', array('id' => 'page_notice',
492 'class' => 'system_notice'));
493 $this->element('dt', null, _('Page notice'));
494 $this->elementStart('dd');
495 $this->showPageNotice();
496 $this->elementEnd('dd');
497 $this->elementEnd('dl');
503 * SHOULD overload (unless there's not a notice)
507 function showPageNotice()
514 * MUST overload (unless there's not a notice)
518 function showContent()
529 $this->elementStart('div', array('id' => 'aside_primary',
530 'class' => 'aside'));
531 $this->showExportData();
532 $this->showSections();
533 $this->elementEnd('div');
537 * Show export data feeds.
539 * MAY overload if there are feeds
543 function showExportData()
545 // is there structure to this?
546 // list of (visible!) feed links
547 // can we reuse list of feeds from showFeeds() ?
557 function showSections()
559 // for each section, show it
567 function showFooter()
569 $this->elementStart('div', array('id' => 'footer'));
570 $this->showSecondaryNav();
571 $this->showLicenses();
572 $this->elementEnd('div');
576 * Show secondary navigation.
580 function showSecondaryNav()
582 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
583 $this->element('dt', null, _('Secondary site navigation'));
584 $this->elementStart('dd', null);
585 $this->elementStart('ul', array('class' => 'nav'));
586 if (Event::handle('StartSecondaryNav', array($this))) {
587 $this->menuItem(common_local_url('doc', array('title' => 'help')),
589 $this->menuItem(common_local_url('doc', array('title' => 'about')),
591 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
593 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
595 $this->menuItem(common_local_url('doc', array('title' => 'source')),
597 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
599 Event::handle('EndSecondaryNav', array($this));
601 $this->elementEnd('ul');
602 $this->elementEnd('dd');
603 $this->elementEnd('dl');
611 function showLicenses()
613 $this->elementStart('dl', array('id' => 'licenses'));
614 $this->showLaconicaLicense();
615 $this->showContentLicense();
616 $this->elementEnd('dl');
620 * Show Laconica license.
624 function showLaconicaLicense()
626 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
627 $this->elementStart('dd', null);
628 if (common_config('site', 'broughtby')) {
629 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
631 $instr = _('**%%site.name%%** is a microblogging service. ');
633 $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);
634 $output = common_markup_to_html($instr);
636 $this->elementEnd('dd');
641 * Show content license.
645 function showContentLicense()
647 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
648 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
649 $this->elementStart('p');
650 $this->element('img', array('id' => 'license_cc',
651 'src' => common_config('license', 'image'),
652 'alt' => common_config('license', 'title')));
653 //TODO: This is dirty: i18n
654 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
655 $this->element('a', array('class' => 'license',
656 'rel' => 'external license',
657 'href' => common_config('license', 'url')),
658 common_config('license', 'title'));
659 $this->text(_('license.'));
660 $this->elementEnd('p');
661 $this->elementEnd('dd');
665 * Return last modified, if applicable.
669 * @return string last modified http header
671 function lastModified()
673 // For comparison with If-Last-Modified
674 // If not applicable, return null
679 * Return etag, if applicable.
683 * @return string etag http header
691 * Return true if read only.
695 * @return boolean is read only action?
697 function isReadOnly()
703 * Returns query argument or default value if not found
705 * @param string $key requested argument
706 * @param string $def default value to return if $key is not provided
708 * @return boolean is read only action?
710 function arg($key, $def=null)
712 if (array_key_exists($key, $this->args)) {
713 return $this->args[$key];
720 * Returns trimmed query argument or default value if not found
722 * @param string $key requested argument
723 * @param string $def default value to return if $key is not provided
725 * @return boolean is read only action?
727 function trimmed($key, $def=null)
729 $arg = $this->arg($key, $def);
730 return is_string($arg) ? trim($arg) : $arg;
736 * @param array $argarray is ignored since it's now passed in in prepare()
738 * @return boolean is read only action?
740 function handle($argarray=null)
742 $lm = $this->lastModified();
743 $etag = $this->etag();
745 header('ETag: ' . $etag);
748 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
749 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
750 if ($if_modified_since) {
751 $ims = strtotime($if_modified_since);
754 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
755 header('HTTP/1.1 304 Not Modified');
756 // Better way to do this?
765 * HasĀ etag? (private)
767 * @param string $etag etag http header
768 * @param string $if_none_match ifNoneMatch http header
772 function _hasEtag($etag, $if_none_match)
774 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
778 * Boolean understands english (yes, no, true, false)
780 * @param string $key query key we're interested in
781 * @param string $def default value
783 * @return boolean interprets yes/no strings as boolean
785 function boolean($key, $def=false)
787 $arg = strtolower($this->trimmed($key));
791 } else if (in_array($arg, array('true', 'yes', '1'))) {
793 } else if (in_array($arg, array('false', 'no', '0'))) {
803 * @param string $msg error message to display
804 * @param integer $code http error code, 500 by default
809 function serverError($msg, $code=500)
811 $action = $this->trimmed('action');
812 common_debug("Server error '$code' on '$action': $msg", __FILE__);
813 throw new ServerException($msg, $code);
819 * @param string $msg error message to display
820 * @param integer $code http error code, 400 by default
825 function clientError($msg, $code=400)
827 $action = $this->trimmed('action');
828 common_debug("User error '$code' on '$action': $msg", __FILE__);
829 throw new ClientException($msg, $code);
833 * Returns the current URL
835 * @return string current URL
839 $action = $this->trimmed('action');
841 unset($args['action']);
842 foreach (array_keys($_COOKIE) as $cookie) {
843 unset($args[$cookie]);
845 return common_local_url($action, $args);
849 * Generate a menu item
851 * @param string $url menu URL
852 * @param string $text menu name
853 * @param string $title title attribute, null by default
854 * @param boolean $is_selected current menu item, false by default
855 * @param string $id element id, null by default
859 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
861 // Added @id to li for some control.
862 // XXX: We might want to move this to htmloutputter.php
865 $lattrs['class'] = 'current';
868 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
870 $this->elementStart('li', $lattrs);
871 $attrs['href'] = $url;
873 $attrs['title'] = $title;
875 $this->element('a', $attrs, $text);
876 $this->elementEnd('li');
880 * Generate pagination links
882 * @param boolean $have_before is there something before?
883 * @param boolean $have_after is there something after?
884 * @param integer $page current page
885 * @param string $action current action
886 * @param array $args rest of query arguments
890 function pagination($have_before, $have_after, $page, $action, $args=null)
892 // Does a little before-after block for next/prev page
893 if ($have_before || $have_after) {
894 $this->elementStart('div', array('class' => 'pagination'));
895 $this->elementStart('dl', null);
896 $this->element('dt', null, _('Pagination'));
897 $this->elementStart('dd', null);
898 $this->elementStart('ul', array('class' => 'nav'));
901 $pargs = array('page' => $page-1);
902 $newargs = $args ? array_merge($args, $pargs) : $pargs;
903 $this->elementStart('li', array('class' => 'nav_prev'));
904 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
906 $this->elementEnd('li');
909 $pargs = array('page' => $page+1);
910 $newargs = $args ? array_merge($args, $pargs) : $pargs;
911 $this->elementStart('li', array('class' => 'nav_next'));
912 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
914 $this->elementEnd('li');
916 if ($have_before || $have_after) {
917 $this->elementEnd('ul');
918 $this->elementEnd('dd');
919 $this->elementEnd('dl');
920 $this->elementEnd('div');