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))) {
160 $this->element('link', array('rel' => 'stylesheet',
161 'type' => 'text/css',
162 'href' => theme_path('css/display.css', 'base') . '?version=' . LACONICA_VERSION,
163 'media' => 'screen, projection, tv'));
164 $this->element('link', array('rel' => 'stylesheet',
165 'type' => 'text/css',
166 'href' => theme_path('css/modal.css', 'base') . '?version=' . LACONICA_VERSION,
167 'media' => 'screen, projection, tv'));
168 $this->element('link', array('rel' => 'stylesheet',
169 'type' => 'text/css',
170 'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
171 'media' => 'screen, projection, tv'));
172 if (common_config('site', 'mobile')) {
173 $this->element('link', array('rel' => 'stylesheet',
174 'type' => 'text/css',
175 'href' => theme_path('css/mobile.css', 'base') . '?version=' . LACONICA_VERSION,
176 // TODO: "handheld" CSS for other mobile devices
177 'media' => 'only screen and (max-device-width: 480px)')); // Mobile WebKit
179 $this->element('link', array('rel' => 'stylesheet',
180 'type' => 'text/css',
181 'href' => theme_path('css/print.css', 'base') . '?version=' . LACONICA_VERSION,
182 'media' => 'print'));
183 Event::handle('EndShowLaconicaStyles', array($this));
185 if (Event::handle('StartShowUAStyles', array($this))) {
186 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
187 'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
188 foreach (array(6,7) as $ver) {
189 if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
190 // Yes, IE people should be put in jail.
191 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
192 'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
195 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
196 'href="'.theme_path('css/ie.css', null).'?version='.LACONICA_VERSION.'" /><![endif]');
197 Event::handle('EndShowUAStyles', array($this));
199 Event::handle('EndShowStyles', array($this));
204 * Show javascript headers
208 function showScripts()
210 if (Event::handle('StartShowScripts', array($this))) {
211 if (Event::handle('StartShowJQueryScripts', array($this))) {
212 $this->element('script', array('type' => 'text/javascript',
213 'src' => common_path('js/jquery.min.js')),
215 $this->element('script', array('type' => 'text/javascript',
216 'src' => common_path('js/jquery.form.js')),
219 $this->element('script', array('type' => 'text/javascript',
220 'src' => common_path('js/jquery.simplemodal-1.2.2.pack.js')),
223 Event::handle('EndShowJQueryScripts', array($this));
225 if (Event::handle('StartShowLaconicaScripts', array($this))) {
226 $this->element('script', array('type' => 'text/javascript',
227 'src' => common_path('js/xbImportNode.js')),
229 $this->element('script', array('type' => 'text/javascript',
230 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
232 // Frame-busting code to avoid clickjacking attacks.
233 $this->element('script', array('type' => 'text/javascript'),
234 'if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
236 $this->element('script', array('type' => 'text/javascript',
237 'src' => common_path('js/flowplayer-3.0.5.min.js')),
240 $this->element('script', array('type' => 'text/javascript',
241 'src' => common_path('js/video.js')),
243 Event::handle('EndShowLaconicaScripts', array($this));
245 Event::handle('EndShowScripts', array($this));
250 * Show OpenSearch headers
254 function showOpenSearch()
256 $this->element('link', array('rel' => 'search',
257 'type' => 'application/opensearchdescription+xml',
258 'href' => common_local_url('opensearch', array('type' => 'people')),
259 'title' => common_config('site', 'name').' People Search'));
260 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
261 'href' => common_local_url('opensearch', array('type' => 'notice')),
262 'title' => common_config('site', 'name').' Notice Search'));
275 $feeds = $this->getFeeds();
278 foreach ($feeds as $feed) {
279 $this->element('link', array('rel' => $feed->rel(),
280 'href' => $feed->url,
281 'type' => $feed->mimeType(),
282 'title' => $feed->title));
294 function showDescription()
296 // does nothing by default
300 * Show extra stuff in <head>.
308 // does nothing by default
314 * Calls template methods
320 $this->elementStart('body', array('id' => $this->trimmed('action')));
321 $this->elementStart('div', array('id' => 'wrap'));
322 if (Event::handle('StartShowHeader', array($this))) {
324 Event::handle('EndShowHeader', array($this));
327 if (Event::handle('StartShowFooter', array($this))) {
329 Event::handle('EndShowFooter', array($this));
331 $this->elementEnd('div');
332 $this->elementEnd('body');
336 * Show header of the page.
338 * Calls template methods
342 function showHeader()
344 $this->elementStart('div', array('id' => 'header'));
346 $this->showPrimaryNav();
347 $this->showSiteNotice();
348 if (common_logged_in()) {
349 $this->showNoticeForm();
351 $this->showAnonymousMessage();
353 $this->elementEnd('div');
357 * Show configured logo.
363 $this->elementStart('address', array('id' => 'site_contact',
364 'class' => 'vcard'));
365 $this->elementStart('a', array('class' => 'url home bookmark',
366 'href' => common_local_url('public')));
367 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
368 $this->element('img', array('class' => 'logo photo',
369 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
370 'alt' => common_config('site', 'name')));
372 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
373 $this->elementEnd('a');
374 $this->elementEnd('address');
378 * Show primary navigation.
382 function showPrimaryNav()
384 $user = common_current_user();
386 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
387 $this->element('dt', null, _('Primary site navigation'));
388 $this->elementStart('dd');
389 $this->elementStart('ul', array('class' => 'nav'));
390 if (Event::handle('StartPrimaryNav', array($this))) {
392 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
393 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
395 $this->menuItem(common_local_url('peoplesearch'),
396 _('Search'), _('Search for people or text'), false, 'nav_search');
398 $this->menuItem(common_local_url('profilesettings'),
399 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
401 if (common_config('xmpp', 'enabled')) {
402 $this->menuItem(common_local_url('imsettings'),
403 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
405 $this->menuItem(common_local_url('smssettings'),
406 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
408 $this->menuItem(common_local_url('logout'),
409 _('Logout'), _('Logout from the site'), false, 'nav_logout');
411 $this->menuItem(common_local_url('login'),
412 _('Login'), _('Login to the site'), false, 'nav_login');
413 if (!common_config('site', 'closed')) {
414 $this->menuItem(common_local_url('register'),
415 _('Register'), _('Create an account'), false, 'nav_register');
417 $this->menuItem(common_local_url('openidlogin'),
418 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
420 $this->menuItem(common_local_url('doc', array('title' => 'help')),
421 _('Help'), _('Help me!'), false, 'nav_help');
422 Event::handle('EndPrimaryNav', array($this));
424 $this->elementEnd('ul');
425 $this->elementEnd('dd');
426 $this->elementEnd('dl');
434 function showSiteNotice()
436 // Revist. Should probably do an hAtom pattern here
437 $text = common_config('site', 'notice');
439 $this->elementStart('dl', array('id' => 'site_notice',
440 'class' => 'system_notice'));
441 $this->element('dt', null, _('Site notice'));
442 $this->elementStart('dd', null);
444 $this->elementEnd('dd');
445 $this->elementEnd('dl');
452 * MAY overload if no notice form needed... or direct message box????
456 function showNoticeForm()
458 $notice_form = new NoticeForm($this);
459 $notice_form->show();
463 * Show anonymous message.
469 function showAnonymousMessage()
471 // needs to be defined by the class
477 * Shows local navigation, content block and aside.
483 $this->elementStart('div', array('id' => 'core'));
484 if (Event::handle('StartShowLocalNavBlock', array($this))) {
485 $this->showLocalNavBlock();
486 Event::handle('EndShowLocalNavBlock', array($this));
488 if (Event::handle('StartShowContentBlock', array($this))) {
489 $this->showContentBlock();
490 Event::handle('EndShowContentBlock', array($this));
493 $this->elementEnd('div');
497 * Show local navigation block.
501 function showLocalNavBlock()
503 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
504 $this->element('dt', null, _('Local views'));
505 $this->elementStart('dd');
506 $this->showLocalNav();
507 $this->elementEnd('dd');
508 $this->elementEnd('dl');
512 * Show local navigation.
518 function showLocalNav()
520 // does nothing by default
524 * Show content block.
528 function showContentBlock()
530 $this->elementStart('div', array('id' => 'content'));
531 $this->showPageTitle();
532 $this->showPageNoticeBlock();
533 $this->elementStart('div', array('id' => 'content_inner'));
534 // show the actual content (forms, lists, whatever)
535 $this->showContent();
536 $this->elementEnd('div');
537 $this->elementEnd('div');
545 function showPageTitle()
547 $this->element('h1', null, $this->title());
551 * Show page notice block.
555 function showPageNoticeBlock()
557 $this->elementStart('dl', array('id' => 'page_notice',
558 'class' => 'system_notice'));
559 $this->element('dt', null, _('Page notice'));
560 $this->elementStart('dd');
561 $this->showPageNotice();
562 $this->elementEnd('dd');
563 $this->elementEnd('dl');
569 * SHOULD overload (unless there's not a notice)
573 function showPageNotice()
580 * MUST overload (unless there's not a notice)
584 function showContent()
596 $this->elementStart('div', array('id' => 'aside_primary',
597 'class' => 'aside'));
598 $this->showExportData();
599 if (Event::handle('StartShowSections', array($this))) {
600 $this->showSections();
601 Event::handle('EndShowSections', array($this));
603 $this->elementEnd('div');
607 * Show export data feeds.
612 function showExportData()
614 $feeds = $this->getFeeds();
616 $fl = new FeedList($this);
628 function showSections()
630 // for each section, show it
638 function showFooter()
640 $this->elementStart('div', array('id' => 'footer'));
641 $this->showSecondaryNav();
642 $this->showLicenses();
643 $this->elementEnd('div');
647 * Show secondary navigation.
651 function showSecondaryNav()
653 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
654 $this->element('dt', null, _('Secondary site navigation'));
655 $this->elementStart('dd', null);
656 $this->elementStart('ul', array('class' => 'nav'));
657 if (Event::handle('StartSecondaryNav', array($this))) {
658 $this->menuItem(common_local_url('doc', array('title' => 'help')),
660 $this->menuItem(common_local_url('doc', array('title' => 'about')),
662 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
664 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
666 $this->menuItem(common_local_url('doc', array('title' => 'source')),
668 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
670 $this->menuItem(common_local_url('doc', array('title' => 'badge')),
672 Event::handle('EndSecondaryNav', array($this));
674 $this->elementEnd('ul');
675 $this->elementEnd('dd');
676 $this->elementEnd('dl');
684 function showLicenses()
686 $this->elementStart('dl', array('id' => 'licenses'));
687 $this->showLaconicaLicense();
688 $this->showContentLicense();
689 $this->elementEnd('dl');
693 * Show Laconica license.
697 function showLaconicaLicense()
699 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
700 $this->elementStart('dd', null);
701 if (common_config('site', 'broughtby')) {
702 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
704 $instr = _('**%%site.name%%** is a microblogging service. ');
706 $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);
707 $output = common_markup_to_html($instr);
709 $this->elementEnd('dd');
714 * Show content license.
718 function showContentLicense()
720 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
721 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
722 $this->elementStart('p');
723 $this->element('img', array('id' => 'license_cc',
724 'src' => common_config('license', 'image'),
725 'alt' => common_config('license', 'title')));
726 //TODO: This is dirty: i18n
727 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
728 $this->element('a', array('class' => 'license',
729 'rel' => 'external license',
730 'href' => common_config('license', 'url')),
731 common_config('license', 'title'));
732 $this->text(_('license.'));
733 $this->elementEnd('p');
734 $this->elementEnd('dd');
738 * Return last modified, if applicable.
742 * @return string last modified http header
744 function lastModified()
746 // For comparison with If-Last-Modified
747 // If not applicable, return null
752 * Return etag, if applicable.
756 * @return string etag http header
764 * Return true if read only.
768 * @return boolean is read only action?
770 function isReadOnly()
776 * Returns query argument or default value if not found
778 * @param string $key requested argument
779 * @param string $def default value to return if $key is not provided
781 * @return boolean is read only action?
783 function arg($key, $def=null)
785 if (array_key_exists($key, $this->args)) {
786 return $this->args[$key];
793 * Returns trimmed query argument or default value if not found
795 * @param string $key requested argument
796 * @param string $def default value to return if $key is not provided
798 * @return boolean is read only action?
800 function trimmed($key, $def=null)
802 $arg = $this->arg($key, $def);
803 return is_string($arg) ? trim($arg) : $arg;
809 * @param array $argarray is ignored since it's now passed in in prepare()
811 * @return boolean is read only action?
813 function handle($argarray=null)
815 $lm = $this->lastModified();
816 $etag = $this->etag();
818 header('ETag: ' . $etag);
821 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
822 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
823 if ($if_modified_since) {
824 $ims = strtotime($if_modified_since);
826 $if_none_match = $_SERVER['HTTP_IF_NONE_MATCH'];
827 if (!$if_none_match ||
829 $this->_hasEtag($etag, $if_none_match)) {
830 header('HTTP/1.1 304 Not Modified');
831 // Better way to do this?
840 * HasĀ etag? (private)
842 * @param string $etag etag http header
843 * @param string $if_none_match ifNoneMatch http header
848 function _hasEtag($etag, $if_none_match)
850 $etags = explode(',', $if_none_match);
851 return in_array($etag, $etags) || in_array('*', $etags);
855 * Boolean understands english (yes, no, true, false)
857 * @param string $key query key we're interested in
858 * @param string $def default value
860 * @return boolean interprets yes/no strings as boolean
862 function boolean($key, $def=false)
864 $arg = strtolower($this->trimmed($key));
868 } else if (in_array($arg, array('true', 'yes', '1'))) {
870 } else if (in_array($arg, array('false', 'no', '0'))) {
880 * @param string $msg error message to display
881 * @param integer $code http error code, 500 by default
886 function serverError($msg, $code=500)
888 $action = $this->trimmed('action');
889 common_debug("Server error '$code' on '$action': $msg", __FILE__);
890 throw new ServerException($msg, $code);
896 * @param string $msg error message to display
897 * @param integer $code http error code, 400 by default
902 function clientError($msg, $code=400)
904 $action = $this->trimmed('action');
905 common_debug("User error '$code' on '$action': $msg", __FILE__);
906 throw new ClientException($msg, $code);
910 * Returns the current URL
912 * @return string current URL
916 $action = $this->trimmed('action');
918 unset($args['action']);
919 foreach (array_keys($_COOKIE) as $cookie) {
920 unset($args[$cookie]);
922 return common_local_url($action, $args);
926 * Generate a menu item
928 * @param string $url menu URL
929 * @param string $text menu name
930 * @param string $title title attribute, null by default
931 * @param boolean $is_selected current menu item, false by default
932 * @param string $id element id, null by default
936 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
938 // Added @id to li for some control.
939 // XXX: We might want to move this to htmloutputter.php
942 $lattrs['class'] = 'current';
945 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
947 $this->elementStart('li', $lattrs);
948 $attrs['href'] = $url;
950 $attrs['title'] = $title;
952 $this->element('a', $attrs, $text);
953 $this->elementEnd('li');
957 * Generate pagination links
959 * @param boolean $have_before is there something before?
960 * @param boolean $have_after is there something after?
961 * @param integer $page current page
962 * @param string $action current action
963 * @param array $args rest of query arguments
967 function pagination($have_before, $have_after, $page, $action, $args=null)
969 // Does a little before-after block for next/prev page
970 if ($have_before || $have_after) {
971 $this->elementStart('div', array('class' => 'pagination'));
972 $this->elementStart('dl', null);
973 $this->element('dt', null, _('Pagination'));
974 $this->elementStart('dd', null);
975 $this->elementStart('ul', array('class' => 'nav'));
978 $pargs = array('page' => $page-1);
979 $newargs = $args ? array_merge($args, $pargs) : $pargs;
980 $this->elementStart('li', array('class' => 'nav_prev'));
981 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
983 $this->elementEnd('li');
986 $pargs = array('page' => $page+1);
987 $newargs = $args ? array_merge($args, $pargs) : $pargs;
988 $this->elementStart('li', array('class' => 'nav_next'));
989 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
991 $this->elementEnd('li');
993 if ($have_before || $have_after) {
994 $this->elementEnd('ul');
995 $this->elementEnd('dd');
996 $this->elementEnd('dl');
997 $this->elementEnd('div');
1002 * An array of feeds for this action.
1004 * Returns an array of potential feeds for this action.
1006 * @return array Feed object to show in head and links