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.
96 if (Event::handle('StartShowHTML', array($this))) {
98 Event::handle('EndShowHTML', array($this));
106 * Show head, a template method.
112 // XXX: attributes (profile?)
113 $this->elementStart('head');
115 $this->showStylesheets();
116 $this->showScripts();
117 $this->showOpenSearch();
119 $this->showDescription();
121 $this->elementEnd('head');
125 * Show title, a template method.
131 $this->element('title', null,
132 sprintf(_("%s - %s"),
134 common_config('site', 'name')));
138 * Returns the page title
142 * @return string page title
147 return _("Untitled page");
155 function showStylesheets()
157 if (Event::handle('StartShowStyles', array($this))) {
158 if (Event::handle('StartShowLaconicaStyles', array($this))) {
159 $this->element('link', array('rel' => 'stylesheet',
160 'type' => 'text/css',
161 'href' => theme_path('css/display.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 if (common_config('site', 'mobile')) {
168 $this->element('link', array('rel' => 'stylesheet',
169 'type' => 'text/css',
170 'href' => theme_path('css/mobile.css', 'base') . '?version=' . LACONICA_VERSION,
171 // TODO: "handheld" CSS for other mobile devices
172 'media' => 'only screen and (max-device-width: 480px)')); // Mobile WebKit
174 $this->element('link', array('rel' => 'stylesheet',
175 'type' => 'text/css',
176 'href' => theme_path('css/print.css', 'base') . '?version=' . LACONICA_VERSION,
177 'media' => 'print'));
178 Event::handle('EndShowLaconicaStyles', array($this));
180 if (Event::handle('StartShowUAStyles', array($this))) {
181 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
182 'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
183 foreach (array(6,7) as $ver) {
184 if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
185 // Yes, IE people should be put in jail.
186 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
187 'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
190 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
191 'href="'.theme_path('css/ie.css', null).'?version='.LACONICA_VERSION.'" /><![endif]');
192 Event::handle('EndShowUAStyles', array($this));
194 Event::handle('EndShowStyles', array($this));
199 * Show javascript headers
203 function showScripts()
205 if (Event::handle('StartShowScripts', array($this))) {
206 if (Event::handle('StartShowJQueryScripts', array($this))) {
207 $this->element('script', array('type' => 'text/javascript',
208 'src' => common_path('js/jquery.min.js')),
210 $this->element('script', array('type' => 'text/javascript',
211 'src' => common_path('js/jquery.form.js')),
213 Event::handle('EndShowJQueryScripts', array($this));
215 if (Event::handle('StartShowLaconicaScripts', array($this))) {
216 $this->element('script', array('type' => 'text/javascript',
217 'src' => common_path('js/xbImportNode.js')),
219 $this->element('script', array('type' => 'text/javascript',
220 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
222 // Frame-busting code to avoid clickjacking attacks.
223 $this->element('script', array('type' => 'text/javascript'),
224 'if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
225 Event::handle('EndShowLaconicaScripts', array($this));
227 Event::handle('EndShowScripts', array($this));
232 * Show OpenSearch headers
236 function showOpenSearch()
238 $this->element('link', array('rel' => 'search',
239 'type' => 'application/opensearchdescription+xml',
240 'href' => common_local_url('opensearch', array('type' => 'people')),
241 'title' => common_config('site', 'name').' People Search'));
242 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
243 'href' => common_local_url('opensearch', array('type' => 'notice')),
244 'title' => common_config('site', 'name').' Notice Search'));
257 $feeds = $this->getFeeds();
260 foreach ($feeds as $feed) {
261 $this->element('link', array('rel' => $feed->rel(),
262 'href' => $feed->url,
263 'type' => $feed->mimeType(),
264 'title' => $feed->title));
276 function showDescription()
278 // does nothing by default
282 * Show extra stuff in <head>.
290 // does nothing by default
296 * Calls template methods
302 $this->elementStart('body', (common_current_user()) ? array('id' => $this->trimmed('action'),
303 'class' => 'user_in')
304 : array('id' => $this->trimmed('action')));
305 $this->elementStart('div', array('id' => 'wrap'));
306 if (Event::handle('StartShowHeader', array($this))) {
308 Event::handle('EndShowHeader', array($this));
311 if (Event::handle('StartShowFooter', array($this))) {
313 Event::handle('EndShowFooter', array($this));
315 $this->elementEnd('div');
316 $this->elementEnd('body');
320 * Show header of the page.
322 * Calls template methods
326 function showHeader()
328 $this->elementStart('div', array('id' => 'header'));
330 $this->showPrimaryNav();
331 $this->showSiteNotice();
332 if (common_logged_in()) {
333 $this->showNoticeForm();
335 $this->showAnonymousMessage();
337 $this->elementEnd('div');
341 * Show configured logo.
347 $this->elementStart('address', array('id' => 'site_contact',
348 'class' => 'vcard'));
349 $this->elementStart('a', array('class' => 'url home bookmark',
350 'href' => common_local_url('public')));
351 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
352 $this->element('img', array('class' => 'logo photo',
353 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
354 'alt' => common_config('site', 'name')));
356 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
357 $this->elementEnd('a');
358 $this->elementEnd('address');
362 * Show primary navigation.
366 function showPrimaryNav()
368 $user = common_current_user();
370 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
371 $this->element('dt', null, _('Primary site navigation'));
372 $this->elementStart('dd');
373 $this->elementStart('ul', array('class' => 'nav'));
374 if (Event::handle('StartPrimaryNav', array($this))) {
376 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
377 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
379 $this->menuItem(common_local_url('peoplesearch'),
380 _('Search'), _('Search for people or text'), false, 'nav_search');
382 $this->menuItem(common_local_url('profilesettings'),
383 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
385 if (common_config('xmpp', 'enabled')) {
386 $this->menuItem(common_local_url('imsettings'),
387 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
389 $this->menuItem(common_local_url('smssettings'),
390 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
392 $this->menuItem(common_local_url('logout'),
393 _('Logout'), _('Logout from the site'), false, 'nav_logout');
395 $this->menuItem(common_local_url('login'),
396 _('Login'), _('Login to the site'), false, 'nav_login');
397 if (!common_config('site', 'closed')) {
398 $this->menuItem(common_local_url('register'),
399 _('Register'), _('Create an account'), false, 'nav_register');
401 $this->menuItem(common_local_url('openidlogin'),
402 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
404 $this->menuItem(common_local_url('doc', array('title' => 'help')),
405 _('Help'), _('Help me!'), false, 'nav_help');
406 Event::handle('EndPrimaryNav', array($this));
408 $this->elementEnd('ul');
409 $this->elementEnd('dd');
410 $this->elementEnd('dl');
418 function showSiteNotice()
420 // Revist. Should probably do an hAtom pattern here
421 $text = common_config('site', 'notice');
423 $this->elementStart('dl', array('id' => 'site_notice',
424 'class' => 'system_notice'));
425 $this->element('dt', null, _('Site notice'));
426 $this->elementStart('dd', null);
428 $this->elementEnd('dd');
429 $this->elementEnd('dl');
436 * MAY overload if no notice form needed... or direct message box????
440 function showNoticeForm()
442 $notice_form = new NoticeForm($this);
443 $notice_form->show();
447 * Show anonymous message.
453 function showAnonymousMessage()
455 // needs to be defined by the class
461 * Shows local navigation, content block and aside.
467 $this->elementStart('div', array('id' => 'core'));
468 if (Event::handle('StartShowLocalNavBlock', array($this))) {
469 $this->showLocalNavBlock();
470 Event::handle('EndShowLocalNavBlock', array($this));
472 if (Event::handle('StartShowContentBlock', array($this))) {
473 $this->showContentBlock();
474 Event::handle('EndShowContentBlock', array($this));
477 $this->elementEnd('div');
481 * Show local navigation block.
485 function showLocalNavBlock()
487 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
488 $this->element('dt', null, _('Local views'));
489 $this->elementStart('dd');
490 $this->showLocalNav();
491 $this->elementEnd('dd');
492 $this->elementEnd('dl');
496 * Show local navigation.
502 function showLocalNav()
504 // does nothing by default
508 * Show content block.
512 function showContentBlock()
514 $this->elementStart('div', array('id' => 'content'));
515 $this->showPageTitle();
516 $this->showPageNoticeBlock();
517 $this->elementStart('div', array('id' => 'content_inner'));
518 // show the actual content (forms, lists, whatever)
519 $this->showContent();
520 $this->elementEnd('div');
521 $this->elementEnd('div');
529 function showPageTitle()
531 $this->element('h1', null, $this->title());
535 * Show page notice block.
539 function showPageNoticeBlock()
541 $this->elementStart('dl', array('id' => 'page_notice',
542 'class' => 'system_notice'));
543 $this->element('dt', null, _('Page notice'));
544 $this->elementStart('dd');
545 $this->showPageNotice();
546 $this->elementEnd('dd');
547 $this->elementEnd('dl');
553 * SHOULD overload (unless there's not a notice)
557 function showPageNotice()
564 * MUST overload (unless there's not a notice)
568 function showContent()
580 $this->elementStart('div', array('id' => 'aside_primary',
581 'class' => 'aside'));
582 $this->showExportData();
583 if (Event::handle('StartShowSections', array($this))) {
584 $this->showSections();
585 Event::handle('EndShowSections', array($this));
587 $this->elementEnd('div');
591 * Show export data feeds.
596 function showExportData()
598 $feeds = $this->getFeeds();
600 $fl = new FeedList($this);
612 function showSections()
614 // for each section, show it
622 function showFooter()
624 $this->elementStart('div', array('id' => 'footer'));
625 $this->showSecondaryNav();
626 $this->showLicenses();
627 $this->elementEnd('div');
631 * Show secondary navigation.
635 function showSecondaryNav()
637 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
638 $this->element('dt', null, _('Secondary site navigation'));
639 $this->elementStart('dd', null);
640 $this->elementStart('ul', array('class' => 'nav'));
641 if (Event::handle('StartSecondaryNav', array($this))) {
642 $this->menuItem(common_local_url('doc', array('title' => 'help')),
644 $this->menuItem(common_local_url('doc', array('title' => 'about')),
646 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
648 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
650 $this->menuItem(common_local_url('doc', array('title' => 'source')),
652 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
654 $this->menuItem(common_local_url('doc', array('title' => 'badge')),
656 Event::handle('EndSecondaryNav', array($this));
658 $this->elementEnd('ul');
659 $this->elementEnd('dd');
660 $this->elementEnd('dl');
668 function showLicenses()
670 $this->elementStart('dl', array('id' => 'licenses'));
671 $this->showLaconicaLicense();
672 $this->showContentLicense();
673 $this->elementEnd('dl');
677 * Show Laconica license.
681 function showLaconicaLicense()
683 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
684 $this->elementStart('dd', null);
685 if (common_config('site', 'broughtby')) {
686 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
688 $instr = _('**%%site.name%%** is a microblogging service. ');
690 $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);
691 $output = common_markup_to_html($instr);
693 $this->elementEnd('dd');
698 * Show content license.
702 function showContentLicense()
704 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
705 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
706 $this->elementStart('p');
707 $this->element('img', array('id' => 'license_cc',
708 'src' => common_config('license', 'image'),
709 'alt' => common_config('license', 'title')));
710 //TODO: This is dirty: i18n
711 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
712 $this->element('a', array('class' => 'license',
713 'rel' => 'external license',
714 'href' => common_config('license', 'url')),
715 common_config('license', 'title'));
716 $this->text(_('license.'));
717 $this->elementEnd('p');
718 $this->elementEnd('dd');
722 * Return last modified, if applicable.
726 * @return string last modified http header
728 function lastModified()
730 // For comparison with If-Last-Modified
731 // If not applicable, return null
736 * Return etag, if applicable.
740 * @return string etag http header
748 * Return true if read only.
752 * @return boolean is read only action?
754 function isReadOnly()
760 * Returns query argument or default value if not found
762 * @param string $key requested argument
763 * @param string $def default value to return if $key is not provided
765 * @return boolean is read only action?
767 function arg($key, $def=null)
769 if (array_key_exists($key, $this->args)) {
770 return $this->args[$key];
777 * Returns trimmed query argument or default value if not found
779 * @param string $key requested argument
780 * @param string $def default value to return if $key is not provided
782 * @return boolean is read only action?
784 function trimmed($key, $def=null)
786 $arg = $this->arg($key, $def);
787 return is_string($arg) ? trim($arg) : $arg;
793 * @param array $argarray is ignored since it's now passed in in prepare()
795 * @return boolean is read only action?
797 function handle($argarray=null)
799 $lm = $this->lastModified();
800 $etag = $this->etag();
802 header('ETag: ' . $etag);
805 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
806 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
807 $ims = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
809 $if_none_match = $_SERVER['HTTP_IF_NONE_MATCH'];
810 if (!$if_none_match ||
812 $this->_hasEtag($etag, $if_none_match)) {
813 header('HTTP/1.1 304 Not Modified');
814 // Better way to do this?
823 * HasĀ etag? (private)
825 * @param string $etag etag http header
826 * @param string $if_none_match ifNoneMatch http header
831 function _hasEtag($etag, $if_none_match)
833 $etags = explode(',', $if_none_match);
834 return in_array($etag, $etags) || in_array('*', $etags);
838 * Boolean understands english (yes, no, true, false)
840 * @param string $key query key we're interested in
841 * @param string $def default value
843 * @return boolean interprets yes/no strings as boolean
845 function boolean($key, $def=false)
847 $arg = strtolower($this->trimmed($key));
851 } else if (in_array($arg, array('true', 'yes', '1'))) {
853 } else if (in_array($arg, array('false', 'no', '0'))) {
863 * @param string $msg error message to display
864 * @param integer $code http error code, 500 by default
869 function serverError($msg, $code=500)
871 $action = $this->trimmed('action');
872 common_debug("Server error '$code' on '$action': $msg", __FILE__);
873 throw new ServerException($msg, $code);
879 * @param string $msg error message to display
880 * @param integer $code http error code, 400 by default
885 function clientError($msg, $code=400)
887 $action = $this->trimmed('action');
888 common_debug("User error '$code' on '$action': $msg", __FILE__);
889 throw new ClientException($msg, $code);
893 * Returns the current URL
895 * @return string current URL
899 $action = $this->trimmed('action');
901 unset($args['action']);
902 foreach (array_keys($_COOKIE) as $cookie) {
903 unset($args[$cookie]);
905 return common_local_url($action, $args);
909 * Generate a menu item
911 * @param string $url menu URL
912 * @param string $text menu name
913 * @param string $title title attribute, null by default
914 * @param boolean $is_selected current menu item, false by default
915 * @param string $id element id, null by default
919 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
921 // Added @id to li for some control.
922 // XXX: We might want to move this to htmloutputter.php
925 $lattrs['class'] = 'current';
928 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
930 $this->elementStart('li', $lattrs);
931 $attrs['href'] = $url;
933 $attrs['title'] = $title;
935 $this->element('a', $attrs, $text);
936 $this->elementEnd('li');
940 * Generate pagination links
942 * @param boolean $have_before is there something before?
943 * @param boolean $have_after is there something after?
944 * @param integer $page current page
945 * @param string $action current action
946 * @param array $args rest of query arguments
950 function pagination($have_before, $have_after, $page, $action, $args=null)
952 // Does a little before-after block for next/prev page
953 if ($have_before || $have_after) {
954 $this->elementStart('div', array('class' => 'pagination'));
955 $this->elementStart('dl', null);
956 $this->element('dt', null, _('Pagination'));
957 $this->elementStart('dd', null);
958 $this->elementStart('ul', array('class' => 'nav'));
961 $pargs = array('page' => $page-1);
962 $this->elementStart('li', array('class' => 'nav_prev'));
963 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
966 $this->elementEnd('li');
969 $pargs = array('page' => $page+1);
970 $this->elementStart('li', array('class' => 'nav_next'));
971 $this->element('a', array('href' => common_local_url($action, $args, $pargs),
974 $this->elementEnd('li');
976 if ($have_before || $have_after) {
977 $this->elementEnd('ul');
978 $this->elementEnd('dd');
979 $this->elementEnd('dl');
980 $this->elementEnd('div');
985 * An array of feeds for this action.
987 * Returns an array of potential feeds for this action.
989 * @return array Feed object to show in head and links