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 is for checking PEAR_Errors raised by DB_DataObject.
86 // Setting this to PEAR_ERROR_CALLBACK because setting
87 // to PEAR_ERROR_EXCEPTION does't work to allow PEAR_Errors
88 // to be handled as PHP5 exceptions, and PEAR_ERROR_RETURN
89 // does not cause DB_DataObject to actually return PEAR_Errors
90 // that can be checked with PEAR::isError() -- instead
91 // they just disappear into the ether, and can only be checked for
92 // after the fact. -- Zach
93 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK,
94 array($this, "checkDB_DataObjectError"));
96 $this->args =& common_copy_args($argarray);
101 * Show page, a template method.
114 * Show head, a template method.
120 // XXX: attributes (profile?)
121 $this->elementStart('head');
123 $this->showStylesheets();
124 $this->showScripts();
125 $this->showOpenSearch();
127 $this->showDescription();
129 $this->elementEnd('head');
133 * Show title, a template method.
139 $this->element('title', null,
140 sprintf(_("%s - %s"),
142 common_config('site', 'name')));
146 * Returns the page title
150 * @return string page title
155 return _("Untitled page");
163 function showStylesheets()
165 $this->element('link', array('rel' => 'stylesheet',
166 'type' => 'text/css',
167 'href' => theme_path('css/display.css', 'base') . '?version=' . LACONICA_VERSION,
168 'media' => 'screen, projection, tv'));
169 $this->element('link', array('rel' => 'stylesheet',
170 'type' => 'text/css',
171 'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
172 'media' => 'screen, projection, tv'));
173 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
174 'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
175 foreach (array(6,7) as $ver) {
176 if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
177 // Yes, IE people should be put in jail.
178 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
179 'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
182 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
183 'href="'.theme_path('css/ie.css', null).'?version='.LACONICA_VERSION.'" /><![endif]');
187 * Show javascript headers
191 function showScripts()
193 if (Event::handle('StartShowScripts', array($this))) {
194 if (Event::handle('StartShowJQueryScripts', array($this))) {
195 $this->element('script', array('type' => 'text/javascript',
196 'src' => common_path('js/jquery.min.js')),
198 $this->element('script', array('type' => 'text/javascript',
199 'src' => common_path('js/jquery.form.js')),
201 Event::handle('EndShowJQueryScripts', array($this));
203 if (Event::handle('StartShowLaconicaScripts', array($this))) {
204 $this->element('script', array('type' => 'text/javascript',
205 'src' => common_path('js/xbImportNode.js')),
207 $this->element('script', array('type' => 'text/javascript',
208 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
210 Event::handle('EndShowLaconicaScripts', array($this));
212 Event::handle('EndShowScripts', array($this));
217 * Show OpenSearch headers
221 function showOpenSearch()
223 $this->element('link', array('rel' => 'search',
224 'type' => 'application/opensearchdescription+xml',
225 'href' => common_local_url('opensearch', array('type' => 'people')),
226 'title' => common_config('site', 'name').' People Search'));
227 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
228 'href' => common_local_url('opensearch', array('type' => 'notice')),
229 'title' => common_config('site', 'name').' Notice Search'));
242 $feeds = $this->getFeeds();
245 foreach ($feeds as $feed) {
246 $this->element('link', array('rel' => $feed->rel(),
247 'href' => $feed->url,
248 'type' => $feed->mimeType(),
249 'title' => $feed->title));
261 function showDescription()
263 // does nothing by default
267 * Show extra stuff in <head>.
275 // does nothing by default
281 * Calls template methods
287 $this->elementStart('body', array('id' => $this->trimmed('action')));
288 $this->elementStart('div', array('id' => 'wrap'));
292 $this->elementEnd('div');
293 $this->elementEnd('body');
297 * Show header of the page.
299 * Calls template methods
303 function showHeader()
305 $this->elementStart('div', array('id' => 'header'));
307 $this->showPrimaryNav();
308 $this->showSiteNotice();
309 if (common_logged_in()) {
310 $this->showNoticeForm();
312 $this->showAnonymousMessage();
314 $this->elementEnd('div');
318 * Show configured logo.
324 $this->elementStart('address', array('id' => 'site_contact',
325 'class' => 'vcard'));
326 $this->elementStart('a', array('class' => 'url home bookmark',
327 'href' => common_local_url('public')));
328 if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
329 $this->element('img', array('class' => 'logo photo',
330 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
331 'alt' => common_config('site', 'name')));
333 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
334 $this->elementEnd('a');
335 $this->elementEnd('address');
339 * Show primary navigation.
343 function showPrimaryNav()
345 $user = common_current_user();
347 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
348 $this->element('dt', null, _('Primary site navigation'));
349 $this->elementStart('dd');
350 $this->elementStart('ul', array('class' => 'nav'));
351 if (Event::handle('StartPrimaryNav', array($this))) {
353 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
354 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
356 $this->menuItem(common_local_url('peoplesearch'),
357 _('Search'), _('Search for people or text'), false, 'nav_search');
359 $this->menuItem(common_local_url('profilesettings'),
360 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
362 if (common_config('xmpp', 'enabled')) {
363 $this->menuItem(common_local_url('imsettings'),
364 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
366 $this->menuItem(common_local_url('smssettings'),
367 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
369 $this->menuItem(common_local_url('logout'),
370 _('Logout'), _('Logout from the site'), false, 'nav_logout');
372 $this->menuItem(common_local_url('login'),
373 _('Login'), _('Login to the site'), false, 'nav_login');
374 if (!common_config('site', 'closed')) {
375 $this->menuItem(common_local_url('register'),
376 _('Register'), _('Create an account'), false, 'nav_register');
378 $this->menuItem(common_local_url('openidlogin'),
379 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
381 $this->menuItem(common_local_url('doc', array('title' => 'help')),
382 _('Help'), _('Help me!'), false, 'nav_help');
383 Event::handle('EndPrimaryNav', array($this));
385 $this->elementEnd('ul');
386 $this->elementEnd('dd');
387 $this->elementEnd('dl');
395 function showSiteNotice()
397 // Revist. Should probably do an hAtom pattern here
398 $text = common_config('site', 'notice');
400 $this->elementStart('dl', array('id' => 'site_notice',
401 'class' => 'system_notice'));
402 $this->element('dt', null, _('Site notice'));
403 $this->elementStart('dd', null);
405 $this->elementEnd('dd');
406 $this->elementEnd('dl');
413 * MAY overload if no notice form needed... or direct message box????
417 function showNoticeForm()
419 $notice_form = new NoticeForm($this);
420 $notice_form->show();
424 * Show anonymous message.
430 function showAnonymousMessage()
432 // needs to be defined by the class
438 * Shows local navigation, content block and aside.
444 $this->elementStart('div', array('id' => 'core'));
445 $this->showLocalNavBlock();
446 $this->showContentBlock();
448 $this->elementEnd('div');
452 * Show local navigation block.
456 function showLocalNavBlock()
458 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
459 $this->element('dt', null, _('Local views'));
460 $this->elementStart('dd');
461 $this->showLocalNav();
462 $this->elementEnd('dd');
463 $this->elementEnd('dl');
467 * Show local navigation.
473 function showLocalNav()
475 // does nothing by default
479 * Show content block.
483 function showContentBlock()
485 $this->elementStart('div', array('id' => 'content'));
486 $this->showPageTitle();
487 $this->showPageNoticeBlock();
488 $this->elementStart('div', array('id' => 'content_inner'));
489 // show the actual content (forms, lists, whatever)
490 $this->showContent();
491 $this->elementEnd('div');
492 $this->elementEnd('div');
500 function showPageTitle()
502 $this->element('h1', null, $this->title());
506 * Show page notice block.
510 function showPageNoticeBlock()
512 $this->elementStart('dl', array('id' => 'page_notice',
513 'class' => 'system_notice'));
514 $this->element('dt', null, _('Page notice'));
515 $this->elementStart('dd');
516 $this->showPageNotice();
517 $this->elementEnd('dd');
518 $this->elementEnd('dl');
524 * SHOULD overload (unless there's not a notice)
528 function showPageNotice()
535 * MUST overload (unless there's not a notice)
539 function showContent()
551 $this->elementStart('div', array('id' => 'aside_primary',
552 'class' => 'aside'));
553 $this->showExportData();
554 if (Event::handle('StartShowSections', array($this))) {
555 $this->showSections();
556 Event::handle('EndShowSections', array($this));
558 $this->elementEnd('div');
562 * Show export data feeds.
567 function showExportData()
569 $feeds = $this->getFeeds();
571 $fl = new FeedList($this);
583 function showSections()
585 // for each section, show it
593 function showFooter()
595 $this->elementStart('div', array('id' => 'footer'));
596 $this->showSecondaryNav();
597 $this->showLicenses();
598 $this->elementEnd('div');
602 * Show secondary navigation.
606 function showSecondaryNav()
608 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
609 $this->element('dt', null, _('Secondary site navigation'));
610 $this->elementStart('dd', null);
611 $this->elementStart('ul', array('class' => 'nav'));
612 if (Event::handle('StartSecondaryNav', array($this))) {
613 $this->menuItem(common_local_url('doc', array('title' => 'help')),
615 $this->menuItem(common_local_url('doc', array('title' => 'about')),
617 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
619 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
621 $this->menuItem(common_local_url('doc', array('title' => 'source')),
623 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
625 Event::handle('EndSecondaryNav', array($this));
627 $this->elementEnd('ul');
628 $this->elementEnd('dd');
629 $this->elementEnd('dl');
637 function showLicenses()
639 $this->elementStart('dl', array('id' => 'licenses'));
640 $this->showLaconicaLicense();
641 $this->showContentLicense();
642 $this->elementEnd('dl');
646 * Show Laconica license.
650 function showLaconicaLicense()
652 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
653 $this->elementStart('dd', null);
654 if (common_config('site', 'broughtby')) {
655 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
657 $instr = _('**%%site.name%%** is a microblogging service. ');
659 $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);
660 $output = common_markup_to_html($instr);
662 $this->elementEnd('dd');
667 * Show content license.
671 function showContentLicense()
673 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
674 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
675 $this->elementStart('p');
676 $this->element('img', array('id' => 'license_cc',
677 'src' => common_config('license', 'image'),
678 'alt' => common_config('license', 'title')));
679 //TODO: This is dirty: i18n
680 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
681 $this->element('a', array('class' => 'license',
682 'rel' => 'external license',
683 'href' => common_config('license', 'url')),
684 common_config('license', 'title'));
685 $this->text(_('license.'));
686 $this->elementEnd('p');
687 $this->elementEnd('dd');
691 * Return last modified, if applicable.
695 * @return string last modified http header
697 function lastModified()
699 // For comparison with If-Last-Modified
700 // If not applicable, return null
705 * Return etag, if applicable.
709 * @return string etag http header
717 * Return true if read only.
721 * @return boolean is read only action?
723 function isReadOnly()
729 * Returns query argument or default value if not found
731 * @param string $key requested argument
732 * @param string $def default value to return if $key is not provided
734 * @return boolean is read only action?
736 function arg($key, $def=null)
738 if (array_key_exists($key, $this->args)) {
739 return $this->args[$key];
746 * Returns trimmed query argument or default value if not found
748 * @param string $key requested argument
749 * @param string $def default value to return if $key is not provided
751 * @return boolean is read only action?
753 function trimmed($key, $def=null)
755 $arg = $this->arg($key, $def);
756 return is_string($arg) ? trim($arg) : $arg;
762 * @param array $argarray is ignored since it's now passed in in prepare()
764 * @return boolean is read only action?
766 function handle($argarray=null)
768 $lm = $this->lastModified();
769 $etag = $this->etag();
771 header('ETag: ' . $etag);
774 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
775 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
776 if ($if_modified_since) {
777 $ims = strtotime($if_modified_since);
780 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
781 header('HTTP/1.1 304 Not Modified');
782 // Better way to do this?
791 * HasĀ etag? (private)
793 * @param string $etag etag http header
794 * @param string $if_none_match ifNoneMatch http header
798 function _hasEtag($etag, $if_none_match)
800 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
804 * Boolean understands english (yes, no, true, false)
806 * @param string $key query key we're interested in
807 * @param string $def default value
809 * @return boolean interprets yes/no strings as boolean
811 function boolean($key, $def=false)
813 $arg = strtolower($this->trimmed($key));
817 } else if (in_array($arg, array('true', 'yes', '1'))) {
819 } else if (in_array($arg, array('false', 'no', '0'))) {
829 * @param string $msg error message to display
830 * @param integer $code http error code, 500 by default
835 function serverError($msg, $code=500)
837 $action = $this->trimmed('action');
838 common_debug("Server error '$code' on '$action': $msg", __FILE__);
839 throw new ServerException($msg, $code);
845 * @param string $msg error message to display
846 * @param integer $code http error code, 400 by default
851 function clientError($msg, $code=400)
853 $action = $this->trimmed('action');
854 common_debug("User error '$code' on '$action': $msg", __FILE__);
855 throw new ClientException($msg, $code);
859 * Check old fashioned PEAR_Error msgs coming from DB_DataObject
861 * Logs the DB_DataObject error. Override to do something else.
868 function checkDB_DataObjectError($error) {
869 common_log(LOG_ERR, $error->getMessage());
870 // XXX: throw an exception here? --Zach
874 * Returns the current URL
876 * @return string current URL
880 $action = $this->trimmed('action');
882 unset($args['action']);
883 foreach (array_keys($_COOKIE) as $cookie) {
884 unset($args[$cookie]);
886 return common_local_url($action, $args);
890 * Generate a menu item
892 * @param string $url menu URL
893 * @param string $text menu name
894 * @param string $title title attribute, null by default
895 * @param boolean $is_selected current menu item, false by default
896 * @param string $id element id, null by default
900 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
902 // Added @id to li for some control.
903 // XXX: We might want to move this to htmloutputter.php
906 $lattrs['class'] = 'current';
909 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
911 $this->elementStart('li', $lattrs);
912 $attrs['href'] = $url;
914 $attrs['title'] = $title;
916 $this->element('a', $attrs, $text);
917 $this->elementEnd('li');
921 * Generate pagination links
923 * @param boolean $have_before is there something before?
924 * @param boolean $have_after is there something after?
925 * @param integer $page current page
926 * @param string $action current action
927 * @param array $args rest of query arguments
931 function pagination($have_before, $have_after, $page, $action, $args=null)
933 // Does a little before-after block for next/prev page
934 if ($have_before || $have_after) {
935 $this->elementStart('div', array('class' => 'pagination'));
936 $this->elementStart('dl', null);
937 $this->element('dt', null, _('Pagination'));
938 $this->elementStart('dd', null);
939 $this->elementStart('ul', array('class' => 'nav'));
942 $pargs = array('page' => $page-1);
943 $newargs = $args ? array_merge($args, $pargs) : $pargs;
944 $this->elementStart('li', array('class' => 'nav_prev'));
945 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
947 $this->elementEnd('li');
950 $pargs = array('page' => $page+1);
951 $newargs = $args ? array_merge($args, $pargs) : $pargs;
952 $this->elementStart('li', array('class' => 'nav_next'));
953 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
955 $this->elementEnd('li');
957 if ($have_before || $have_after) {
958 $this->elementEnd('ul');
959 $this->elementEnd('dd');
960 $this->elementEnd('dl');
961 $this->elementEnd('div');
966 * An array of feeds for this action.
968 * Returns an array of potential feeds for this action.
970 * @return array Feed object to show in head and links