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 if (Event::handle('StartShowStyles', array($this))) {
155 if (Event::handle('StartShowLaconicaStyles', array($this))) {
157 $this->element('link', array('rel' => 'stylesheet',
158 'type' => 'text/css',
159 'href' => theme_path('css/display.css', 'base') . '?version=' . LACONICA_VERSION,
160 'media' => 'screen, projection, tv'));
161 $this->element('link', array('rel' => 'stylesheet',
162 'type' => 'text/css',
163 'href' => theme_path('css/modal.css', 'base') . '?version=' . LACONICA_VERSION,
164 'media' => 'screen, projection, tv'));
165 $this->element('link', array('rel' => 'stylesheet',
166 'type' => 'text/css',
167 'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
168 'media' => 'screen, projection, tv'));
169 if (common_config('site', 'mobile')) {
170 $this->element('link', array('rel' => 'stylesheet',
171 'type' => 'text/css',
172 'href' => theme_path('css/mobile.css', 'base') . '?version=' . LACONICA_VERSION,
173 // TODO: "handheld" CSS for other mobile devices
174 'media' => 'only screen and (max-device-width: 480px)')); // Mobile WebKit
176 Event::handle('EndShowLaconicaStyles', array($this));
178 if (Event::handle('StartShowUAStyles', array($this))) {
179 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
180 'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
181 foreach (array(6,7) as $ver) {
182 if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
183 // Yes, IE people should be put in jail.
184 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
185 'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
188 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
189 'href="'.theme_path('css/ie.css', null).'?version='.LACONICA_VERSION.'" /><![endif]');
190 Event::handle('EndShowUAStyles', array($this));
192 Event::handle('EndShowStyles', array($this));
197 * Show javascript headers
201 function showScripts()
203 if (Event::handle('StartShowScripts', array($this))) {
204 if (Event::handle('StartShowJQueryScripts', array($this))) {
205 $this->element('script', array('type' => 'text/javascript',
206 'src' => common_path('js/jquery.min.js')),
208 $this->element('script', array('type' => 'text/javascript',
209 'src' => common_path('js/jquery.form.js')),
212 $this->element('script', array('type' => 'text/javascript',
213 'src' => common_path('js/jquery.simplemodal-1.2.2.pack.js')),
216 Event::handle('EndShowJQueryScripts', array($this));
218 if (Event::handle('StartShowLaconicaScripts', array($this))) {
219 $this->element('script', array('type' => 'text/javascript',
220 'src' => common_path('js/xbImportNode.js')),
222 $this->element('script', array('type' => 'text/javascript',
223 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
225 // Frame-busting code to avoid clickjacking attacks.
226 $this->element('script', array('type' => 'text/javascript'),
227 'if (window.top !== window.self) { window.top.location.href = window.self.location.href; }');
229 $this->element('script', array('type' => 'text/javascript',
230 'src' => common_path('js/flowplayer-3.0.5.min.js')),
233 $this->element('script', array('type' => 'text/javascript',
234 'src' => common_path('js/video.js')),
236 Event::handle('EndShowLaconicaScripts', array($this));
238 Event::handle('EndShowScripts', array($this));
243 * Show OpenSearch headers
247 function showOpenSearch()
249 $this->element('link', array('rel' => 'search',
250 'type' => 'application/opensearchdescription+xml',
251 'href' => common_local_url('opensearch', array('type' => 'people')),
252 'title' => common_config('site', 'name').' People Search'));
253 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
254 'href' => common_local_url('opensearch', array('type' => 'notice')),
255 'title' => common_config('site', 'name').' Notice Search'));
268 $feeds = $this->getFeeds();
271 foreach ($feeds as $feed) {
272 $this->element('link', array('rel' => $feed->rel(),
273 'href' => $feed->url,
274 'type' => $feed->mimeType(),
275 'title' => $feed->title));
287 function showDescription()
289 // does nothing by default
293 * Show extra stuff in <head>.
301 // does nothing by default
307 * Calls template methods
313 $this->elementStart('body', array('id' => $this->trimmed('action')));
314 $this->elementStart('div', array('id' => 'wrap'));
315 if (Event::handle('StartShowHeader', array($this))) {
317 Event::handle('EndShowHeader', array($this));
320 if (Event::handle('StartShowFooter', array($this))) {
322 Event::handle('EndShowFooter', array($this));
324 $this->elementEnd('div');
325 $this->elementEnd('body');
329 * Show header of the page.
331 * Calls template methods
335 function showHeader()
337 $this->elementStart('div', array('id' => 'header'));
339 $this->showPrimaryNav();
340 $this->showSiteNotice();
341 if (common_logged_in()) {
342 $this->showNoticeForm();
344 $this->showAnonymousMessage();
346 $this->elementEnd('div');
350 * Show configured logo.
356 $this->elementStart('address', array('id' => 'site_contact',
357 'class' => 'vcard'));
358 $this->elementStart('a', array('class' => 'url home bookmark',
359 'href' => common_local_url('public')));
360 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
361 $this->element('img', array('class' => 'logo photo',
362 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
363 'alt' => common_config('site', 'name')));
365 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
366 $this->elementEnd('a');
367 $this->elementEnd('address');
371 * Show primary navigation.
375 function showPrimaryNav()
377 $user = common_current_user();
379 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
380 $this->element('dt', null, _('Primary site navigation'));
381 $this->elementStart('dd');
382 $this->elementStart('ul', array('class' => 'nav'));
383 if (Event::handle('StartPrimaryNav', array($this))) {
385 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
386 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
388 $this->menuItem(common_local_url('peoplesearch'),
389 _('Search'), _('Search for people or text'), false, 'nav_search');
391 $this->menuItem(common_local_url('profilesettings'),
392 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
394 if (common_config('xmpp', 'enabled')) {
395 $this->menuItem(common_local_url('imsettings'),
396 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
398 $this->menuItem(common_local_url('smssettings'),
399 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
401 $this->menuItem(common_local_url('logout'),
402 _('Logout'), _('Logout from the site'), false, 'nav_logout');
404 $this->menuItem(common_local_url('login'),
405 _('Login'), _('Login to the site'), false, 'nav_login');
406 if (!common_config('site', 'closed')) {
407 $this->menuItem(common_local_url('register'),
408 _('Register'), _('Create an account'), false, 'nav_register');
410 $this->menuItem(common_local_url('openidlogin'),
411 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
413 $this->menuItem(common_local_url('doc', array('title' => 'help')),
414 _('Help'), _('Help me!'), false, 'nav_help');
415 Event::handle('EndPrimaryNav', array($this));
417 $this->elementEnd('ul');
418 $this->elementEnd('dd');
419 $this->elementEnd('dl');
427 function showSiteNotice()
429 // Revist. Should probably do an hAtom pattern here
430 $text = common_config('site', 'notice');
432 $this->elementStart('dl', array('id' => 'site_notice',
433 'class' => 'system_notice'));
434 $this->element('dt', null, _('Site notice'));
435 $this->elementStart('dd', null);
437 $this->elementEnd('dd');
438 $this->elementEnd('dl');
445 * MAY overload if no notice form needed... or direct message box????
449 function showNoticeForm()
451 $notice_form = new NoticeForm($this);
452 $notice_form->show();
456 * Show anonymous message.
462 function showAnonymousMessage()
464 // needs to be defined by the class
470 * Shows local navigation, content block and aside.
476 $this->elementStart('div', array('id' => 'core'));
477 $this->showLocalNavBlock();
478 if (Event::handle('StartShowContentBlock', array($this))) {
479 $this->showContentBlock();
480 Event::handle('EndShowContentBlock', array($this));
483 $this->elementEnd('div');
487 * Show local navigation block.
491 function showLocalNavBlock()
493 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
494 $this->element('dt', null, _('Local views'));
495 $this->elementStart('dd');
496 $this->showLocalNav();
497 $this->elementEnd('dd');
498 $this->elementEnd('dl');
502 * Show local navigation.
508 function showLocalNav()
510 // does nothing by default
514 * Show content block.
518 function showContentBlock()
520 $this->elementStart('div', array('id' => 'content'));
521 $this->showPageTitle();
522 $this->showPageNoticeBlock();
523 $this->elementStart('div', array('id' => 'content_inner'));
524 // show the actual content (forms, lists, whatever)
525 $this->showContent();
526 $this->elementEnd('div');
527 $this->elementEnd('div');
535 function showPageTitle()
537 $this->element('h1', null, $this->title());
541 * Show page notice block.
545 function showPageNoticeBlock()
547 $this->elementStart('dl', array('id' => 'page_notice',
548 'class' => 'system_notice'));
549 $this->element('dt', null, _('Page notice'));
550 $this->elementStart('dd');
551 $this->showPageNotice();
552 $this->elementEnd('dd');
553 $this->elementEnd('dl');
559 * SHOULD overload (unless there's not a notice)
563 function showPageNotice()
570 * MUST overload (unless there's not a notice)
574 function showContent()
586 $this->elementStart('div', array('id' => 'aside_primary',
587 'class' => 'aside'));
588 $this->showExportData();
589 if (Event::handle('StartShowSections', array($this))) {
590 $this->showSections();
591 Event::handle('EndShowSections', array($this));
593 $this->elementEnd('div');
597 * Show export data feeds.
602 function showExportData()
604 $feeds = $this->getFeeds();
606 $fl = new FeedList($this);
618 function showSections()
620 // for each section, show it
628 function showFooter()
630 $this->elementStart('div', array('id' => 'footer'));
631 $this->showSecondaryNav();
632 $this->showLicenses();
633 $this->elementEnd('div');
637 * Show secondary navigation.
641 function showSecondaryNav()
643 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
644 $this->element('dt', null, _('Secondary site navigation'));
645 $this->elementStart('dd', null);
646 $this->elementStart('ul', array('class' => 'nav'));
647 if (Event::handle('StartSecondaryNav', array($this))) {
648 $this->menuItem(common_local_url('doc', array('title' => 'help')),
650 $this->menuItem(common_local_url('doc', array('title' => 'about')),
652 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
654 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
656 $this->menuItem(common_local_url('doc', array('title' => 'source')),
658 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
660 $this->menuItem(common_local_url('doc', array('title' => 'badge')),
662 Event::handle('EndSecondaryNav', array($this));
664 $this->elementEnd('ul');
665 $this->elementEnd('dd');
666 $this->elementEnd('dl');
674 function showLicenses()
676 $this->elementStart('dl', array('id' => 'licenses'));
677 $this->showLaconicaLicense();
678 $this->showContentLicense();
679 $this->elementEnd('dl');
683 * Show Laconica license.
687 function showLaconicaLicense()
689 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
690 $this->elementStart('dd', null);
691 if (common_config('site', 'broughtby')) {
692 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
694 $instr = _('**%%site.name%%** is a microblogging service. ');
696 $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);
697 $output = common_markup_to_html($instr);
699 $this->elementEnd('dd');
704 * Show content license.
708 function showContentLicense()
710 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
711 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
712 $this->elementStart('p');
713 $this->element('img', array('id' => 'license_cc',
714 'src' => common_config('license', 'image'),
715 'alt' => common_config('license', 'title')));
716 //TODO: This is dirty: i18n
717 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
718 $this->element('a', array('class' => 'license',
719 'rel' => 'external license',
720 'href' => common_config('license', 'url')),
721 common_config('license', 'title'));
722 $this->text(_('license.'));
723 $this->elementEnd('p');
724 $this->elementEnd('dd');
728 * Return last modified, if applicable.
732 * @return string last modified http header
734 function lastModified()
736 // For comparison with If-Last-Modified
737 // If not applicable, return null
742 * Return etag, if applicable.
746 * @return string etag http header
754 * Return true if read only.
758 * @return boolean is read only action?
760 function isReadOnly()
766 * Returns query argument or default value if not found
768 * @param string $key requested argument
769 * @param string $def default value to return if $key is not provided
771 * @return boolean is read only action?
773 function arg($key, $def=null)
775 if (array_key_exists($key, $this->args)) {
776 return $this->args[$key];
783 * Returns trimmed query argument or default value if not found
785 * @param string $key requested argument
786 * @param string $def default value to return if $key is not provided
788 * @return boolean is read only action?
790 function trimmed($key, $def=null)
792 $arg = $this->arg($key, $def);
793 return is_string($arg) ? trim($arg) : $arg;
799 * @param array $argarray is ignored since it's now passed in in prepare()
801 * @return boolean is read only action?
803 function handle($argarray=null)
805 $lm = $this->lastModified();
806 $etag = $this->etag();
808 header('ETag: ' . $etag);
811 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
812 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
813 if ($if_modified_since) {
814 $ims = strtotime($if_modified_since);
816 $if_none_match = $_SERVER['HTTP_IF_NONE_MATCH'];
817 if (!$if_none_match ||
819 $this->_hasEtag($etag, $if_none_match)) {
820 header('HTTP/1.1 304 Not Modified');
821 // Better way to do this?
830 * HasĀ etag? (private)
832 * @param string $etag etag http header
833 * @param string $if_none_match ifNoneMatch http header
838 function _hasEtag($etag, $if_none_match)
840 $etags = explode(',', $if_none_match);
841 return in_array($etag, $etags) || in_array('*', $etags);
845 * Boolean understands english (yes, no, true, false)
847 * @param string $key query key we're interested in
848 * @param string $def default value
850 * @return boolean interprets yes/no strings as boolean
852 function boolean($key, $def=false)
854 $arg = strtolower($this->trimmed($key));
858 } else if (in_array($arg, array('true', 'yes', '1'))) {
860 } else if (in_array($arg, array('false', 'no', '0'))) {
870 * @param string $msg error message to display
871 * @param integer $code http error code, 500 by default
876 function serverError($msg, $code=500)
878 $action = $this->trimmed('action');
879 common_debug("Server error '$code' on '$action': $msg", __FILE__);
880 throw new ServerException($msg, $code);
886 * @param string $msg error message to display
887 * @param integer $code http error code, 400 by default
892 function clientError($msg, $code=400)
894 $action = $this->trimmed('action');
895 common_debug("User error '$code' on '$action': $msg", __FILE__);
896 throw new ClientException($msg, $code);
900 * Returns the current URL
902 * @return string current URL
906 $action = $this->trimmed('action');
908 unset($args['action']);
909 foreach (array_keys($_COOKIE) as $cookie) {
910 unset($args[$cookie]);
912 return common_local_url($action, $args);
916 * Generate a menu item
918 * @param string $url menu URL
919 * @param string $text menu name
920 * @param string $title title attribute, null by default
921 * @param boolean $is_selected current menu item, false by default
922 * @param string $id element id, null by default
926 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
928 // Added @id to li for some control.
929 // XXX: We might want to move this to htmloutputter.php
932 $lattrs['class'] = 'current';
935 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
937 $this->elementStart('li', $lattrs);
938 $attrs['href'] = $url;
940 $attrs['title'] = $title;
942 $this->element('a', $attrs, $text);
943 $this->elementEnd('li');
947 * Generate pagination links
949 * @param boolean $have_before is there something before?
950 * @param boolean $have_after is there something after?
951 * @param integer $page current page
952 * @param string $action current action
953 * @param array $args rest of query arguments
957 function pagination($have_before, $have_after, $page, $action, $args=null)
959 // Does a little before-after block for next/prev page
960 if ($have_before || $have_after) {
961 $this->elementStart('div', array('class' => 'pagination'));
962 $this->elementStart('dl', null);
963 $this->element('dt', null, _('Pagination'));
964 $this->elementStart('dd', null);
965 $this->elementStart('ul', array('class' => 'nav'));
968 $pargs = array('page' => $page-1);
969 $newargs = $args ? array_merge($args, $pargs) : $pargs;
970 $this->elementStart('li', array('class' => 'nav_prev'));
971 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
973 $this->elementEnd('li');
976 $pargs = array('page' => $page+1);
977 $newargs = $args ? array_merge($args, $pargs) : $pargs;
978 $this->elementStart('li', array('class' => 'nav_next'));
979 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
981 $this->elementEnd('li');
983 if ($have_before || $have_after) {
984 $this->elementEnd('ul');
985 $this->elementEnd('dd');
986 $this->elementEnd('dl');
987 $this->elementEnd('div');
992 * An array of feeds for this action.
994 * Returns an array of potential feeds for this action.
996 * @return array Feed object to show in head and links