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 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
316 $this->element('dt', null, _('Primary site navigation'));
317 $this->elementStart('dd');
318 $user = common_current_user();
319 $this->elementStart('ul', array('class' => 'nav'));
321 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
322 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
324 $this->menuItem(common_local_url('peoplesearch'),
325 _('Search'), _('Search for people or text'), false, 'nav_search');
327 $this->menuItem(common_local_url('profilesettings'),
328 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
330 if (common_config('xmpp', 'enabled')) {
331 $this->menuItem(common_local_url('imsettings'),
332 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
334 $this->menuItem(common_local_url('smssettings'),
335 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
337 $this->menuItem(common_local_url('logout'),
338 _('Logout'), _('Logout from the site'), false, 'nav_logout');
340 $this->menuItem(common_local_url('login'),
341 _('Login'), _('Login to the site'), false, 'nav_login');
342 if (!common_config('site', 'closed')) {
343 $this->menuItem(common_local_url('register'),
344 _('Register'), _('Create an account'), false, 'nav_register');
346 $this->menuItem(common_local_url('openidlogin'),
347 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
349 $this->menuItem(common_local_url('doc', array('title' => 'help')),
350 _('Help'), _('Help me!'), false, 'nav_help');
351 $this->elementEnd('ul');
352 $this->elementEnd('dd');
353 $this->elementEnd('dl');
361 function showSiteNotice()
363 // Revist. Should probably do an hAtom pattern here
364 $text = common_config('site', 'notice');
366 $this->elementStart('dl', array('id' => 'site_notice',
367 'class' => 'system_notice'));
368 $this->element('dt', null, _('Site notice'));
369 $this->elementStart('dd', null);
371 $this->elementEnd('dd');
372 $this->elementEnd('dl');
379 * MAY overload if no notice form needed... or direct message box????
383 function showNoticeForm()
385 $notice_form = new NoticeForm($this);
386 $notice_form->show();
390 * Show anonymous message.
396 function showAnonymousMessage()
398 // needs to be defined by the class
404 * Shows local navigation, content block and aside.
410 $this->elementStart('div', array('id' => 'core'));
411 $this->showLocalNavBlock();
412 $this->showContentBlock();
414 $this->elementEnd('div');
418 * Show local navigation block.
422 function showLocalNavBlock()
424 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
425 $this->element('dt', null, _('Local views'));
426 $this->elementStart('dd');
427 $this->showLocalNav();
428 $this->elementEnd('dd');
429 $this->elementEnd('dl');
433 * Show local navigation.
439 function showLocalNav()
441 // does nothing by default
445 * Show content block.
449 function showContentBlock()
451 $this->elementStart('div', array('id' => 'content'));
452 $this->showPageTitle();
453 $this->showPageNoticeBlock();
454 $this->elementStart('div', array('id' => 'content_inner'));
455 // show the actual content (forms, lists, whatever)
456 $this->showContent();
457 $this->elementEnd('div');
458 $this->elementEnd('div');
466 function showPageTitle()
468 $this->element('h1', null, $this->title());
472 * Show page notice block.
476 function showPageNoticeBlock()
478 $this->elementStart('dl', array('id' => 'page_notice',
479 'class' => 'system_notice'));
480 $this->element('dt', null, _('Page notice'));
481 $this->elementStart('dd');
482 $this->showPageNotice();
483 $this->elementEnd('dd');
484 $this->elementEnd('dl');
490 * SHOULD overload (unless there's not a notice)
494 function showPageNotice()
501 * MUST overload (unless there's not a notice)
505 function showContent()
516 $this->elementStart('div', array('id' => 'aside_primary',
517 'class' => 'aside'));
518 $this->showExportData();
519 $this->showSections();
520 $this->elementEnd('div');
524 * Show export data feeds.
526 * MAY overload if there are feeds
530 function showExportData()
532 // is there structure to this?
533 // list of (visible!) feed links
534 // can we reuse list of feeds from showFeeds() ?
544 function showSections()
546 // for each section, show it
554 function showFooter()
556 $this->elementStart('div', array('id' => 'footer'));
557 $this->showSecondaryNav();
558 $this->showLicenses();
559 $this->elementEnd('div');
563 * Show secondary navigation.
567 function showSecondaryNav()
569 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
570 $this->element('dt', null, _('Secondary site navigation'));
571 $this->elementStart('dd', null);
572 $this->elementStart('ul', array('class' => 'nav'));
573 $this->menuItem(common_local_url('doc', array('title' => 'help')),
575 $this->menuItem(common_local_url('doc', array('title' => 'about')),
577 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
579 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
581 $this->menuItem(common_local_url('doc', array('title' => 'source')),
583 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
585 $this->elementEnd('ul');
586 $this->elementEnd('dd');
587 $this->elementEnd('dl');
595 function showLicenses()
597 $this->elementStart('dl', array('id' => 'licenses'));
598 $this->showLaconicaLicense();
599 $this->showContentLicense();
600 $this->elementEnd('dl');
604 * Show Laconica license.
608 function showLaconicaLicense()
610 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
611 $this->elementStart('dd', null);
612 if (common_config('site', 'broughtby')) {
613 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
615 $instr = _('**%%site.name%%** is a microblogging service. ');
617 $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);
618 $output = common_markup_to_html($instr);
620 $this->elementEnd('dd');
625 * Show content license.
629 function showContentLicense()
631 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
632 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
633 $this->elementStart('p');
634 $this->element('img', array('id' => 'license_cc',
635 'src' => common_config('license', 'image'),
636 'alt' => common_config('license', 'title')));
637 //TODO: This is dirty: i18n
638 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
639 $this->element('a', array('class' => 'license',
640 'rel' => 'external license',
641 'href' => common_config('license', 'url')),
642 common_config('license', 'title'));
643 $this->text(_('license.'));
644 $this->elementEnd('p');
645 $this->elementEnd('dd');
649 * Return last modified, if applicable.
653 * @return string last modified http header
655 function lastModified()
657 // For comparison with If-Last-Modified
658 // If not applicable, return null
663 * Return etag, if applicable.
667 * @return string etag http header
675 * Return true if read only.
679 * @return boolean is read only action?
681 function isReadOnly()
687 * Returns query argument or default value if not found
689 * @param string $key requested argument
690 * @param string $def default value to return if $key is not provided
692 * @return boolean is read only action?
694 function arg($key, $def=null)
696 if (array_key_exists($key, $this->args)) {
697 return $this->args[$key];
704 * Returns trimmed query argument or default value if not found
706 * @param string $key requested argument
707 * @param string $def default value to return if $key is not provided
709 * @return boolean is read only action?
711 function trimmed($key, $def=null)
713 $arg = $this->arg($key, $def);
714 return is_string($arg) ? trim($arg) : $arg;
720 * @param array $argarray is ignored since it's now passed in in prepare()
722 * @return boolean is read only action?
724 function handle($argarray=null)
726 $lm = $this->lastModified();
727 $etag = $this->etag();
729 header('ETag: ' . $etag);
732 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
733 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
734 if ($if_modified_since) {
735 $ims = strtotime($if_modified_since);
738 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
739 header('HTTP/1.1 304 Not Modified');
740 // Better way to do this?
749 * HasĀ etag? (private)
751 * @param string $etag etag http header
752 * @param string $if_none_match ifNoneMatch http header
756 function _hasEtag($etag, $if_none_match)
758 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
762 * Boolean understands english (yes, no, true, false)
764 * @param string $key query key we're interested in
765 * @param string $def default value
767 * @return boolean interprets yes/no strings as boolean
769 function boolean($key, $def=false)
771 $arg = strtolower($this->trimmed($key));
775 } else if (in_array($arg, array('true', 'yes', '1'))) {
777 } else if (in_array($arg, array('false', 'no', '0'))) {
787 * @param string $msg error message to display
788 * @param integer $code http error code, 500 by default
793 function serverError($msg, $code=500)
795 $action = $this->trimmed('action');
796 common_debug("Server error '$code' on '$action': $msg", __FILE__);
797 throw new ServerException($msg, $code);
803 * @param string $msg error message to display
804 * @param integer $code http error code, 400 by default
809 function clientError($msg, $code=400)
811 $action = $this->trimmed('action');
812 common_debug("User error '$code' on '$action': $msg", __FILE__);
813 throw new ClientException($msg, $code);
817 * Returns the current URL
819 * @return string current URL
823 $action = $this->trimmed('action');
825 unset($args['action']);
826 foreach (array_keys($_COOKIE) as $cookie) {
827 unset($args[$cookie]);
829 return common_local_url($action, $args);
833 * Generate a menu item
835 * @param string $url menu URL
836 * @param string $text menu name
837 * @param string $title title attribute, null by default
838 * @param boolean $is_selected current menu item, false by default
839 * @param string $id element id, null by default
843 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
845 // Added @id to li for some control.
846 // XXX: We might want to move this to htmloutputter.php
849 $lattrs['class'] = 'current';
852 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
854 $this->elementStart('li', $lattrs);
855 $attrs['href'] = $url;
857 $attrs['title'] = $title;
859 $this->element('a', $attrs, $text);
860 $this->elementEnd('li');
864 * Generate pagination links
866 * @param boolean $have_before is there something before?
867 * @param boolean $have_after is there something after?
868 * @param integer $page current page
869 * @param string $action current action
870 * @param array $args rest of query arguments
874 function pagination($have_before, $have_after, $page, $action, $args=null)
876 // Does a little before-after block for next/prev page
877 if ($have_before || $have_after) {
878 $this->elementStart('div', array('class' => 'pagination'));
879 $this->elementStart('dl', null);
880 $this->element('dt', null, _('Pagination'));
881 $this->elementStart('dd', null);
882 $this->elementStart('ul', array('class' => 'nav'));
885 $pargs = array('page' => $page-1);
886 $newargs = $args ? array_merge($args, $pargs) : $pargs;
887 $this->elementStart('li', array('class' => 'nav_prev'));
888 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
890 $this->elementEnd('li');
893 $pargs = array('page' => $page+1);
894 $newargs = $args ? array_merge($args, $pargs) : $pargs;
895 $this->elementStart('li', array('class' => 'nav_next'));
896 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
898 $this->elementEnd('li');
900 if ($have_before || $have_after) {
901 $this->elementEnd('ul');
902 $this->elementEnd('dd');
903 $this->elementEnd('dl');
904 $this->elementEnd('div');