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/
57 class Action extends HTMLOutputter // lawsuit
64 * Just wraps the HTMLOutputter constructor.
66 * @param string $output URI to output to, default = stdout
67 * @param boolean $indent Whether to indent output, default true
69 * @see XMLOutputter::__construct
70 * @see HTMLOutputter::__construct
73 function __construct($output='php://output', $indent=true)
75 parent::__construct($output, $indent);
78 // For initializing members of the class
80 function prepare($argarray)
82 $this->args =& common_copy_args($argarray);
96 // XXX: attributes (profile?)
97 $this->elementStart('head');
99 $this->showStylesheets();
100 $this->showScripts();
101 $this->showOpenSearch();
103 $this->showDescription();
105 $this->elementEnd('head');
110 $this->element('title', null,
111 sprintf(_("%s - %s"),
113 common_config('site', 'name')));
120 return _("Untitled page");
123 function showStylesheets()
125 $this->element('link', array('rel' => 'stylesheet',
126 'type' => 'text/css',
127 'href' => theme_path('css/display.css', 'base') . '?version=' . LACONICA_VERSION,
128 'media' => 'screen, projection, tv'));
129 $this->element('link', array('rel' => 'stylesheet',
130 'type' => 'text/css',
131 'href' => theme_path('css/thickbox.css', 'base') . '?version=' . LACONICA_VERSION,
132 'media' => 'screen, projection, tv'));
133 $this->element('link', array('rel' => 'stylesheet',
134 'type' => 'text/css',
135 'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
136 'media' => 'screen, projection, tv'));
137 foreach (array(6,7) as $ver) {
138 if (file_exists(theme_file('ie'.$ver.'.css'))) {
139 // Yes, IE people should be put in jail.
140 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
141 'href="'.theme_path('ie'.$ver.'.css').'?version='.LACONICA_VERSION.'" /><![endif]');
146 function showScripts()
148 $this->element('script', array('type' => 'text/javascript',
149 'src' => common_path('js/jquery.min.js')),
151 $this->element('script', array('type' => 'text/javascript',
152 'src' => common_path('js/jquery.form.js')),
154 $this->element('script', array('type' => 'text/javascript',
155 'src' => common_path('js/xbImportNode.js')),
157 $this->element('script', array('type' => 'text/javascript',
158 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
162 function showOpenSearch()
164 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
165 'href' => common_local_url('opensearch', array('type' => 'people')),
166 'title' => common_config('site', 'name').' People Search'));
168 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
169 'href' => common_local_url('opensearch', array('type' => 'notice')),
170 'title' => common_config('site', 'name').' Notice Search'));
177 // does nothing by default
182 function showDescription()
184 // does nothing by default
191 // does nothing by default
196 $this->elementStart('body', array('id' => $this->trimmed('action')));
197 $this->elementStart('div', 'wrap');
201 $this->elementEnd('div');
202 $this->elementEnd('body');
205 function showHeader()
207 $this->elementStart('div', array('id' => 'header'));
209 $this->showPrimaryNav();
210 $this->showSiteNotice();
211 if (common_logged_in()) {
212 $this->showNoticeForm();
214 $this->showAnonymousMessage();
216 $this->elementEnd('div');
221 $this->elementStart('address', array('id' => 'site_contact',
222 'class' => 'vcard'));
223 $this->elementStart('a', array('class' => 'url home bookmark',
224 'href' => common_local_url('public')));
225 if (common_config('site', 'logo') || file_exists(theme_file('logo.png')))
227 $this->element('img', array('class' => 'logo photo',
228 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
229 'alt' => common_config('site', 'name')));
231 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
232 $this->elementEnd('a');
233 $this->elementEnd('address');
236 function showPrimaryNav()
238 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
239 $this->element('dt', null, _('Primary site navigation'));
240 $this->elementStart('dd');
241 $user = common_current_user();
242 $this->elementStart('ul', array('class' => 'nav'));
244 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
245 _('Home'), _('Personal timeline'), false, 'nav_home');
247 $this->menuItem(common_local_url('peoplesearch'),
248 _('Search'), _('Search the site for people and text'), false, 'nav_search');
250 $this->menuItem(common_local_url('profilesettings'),
251 _('Account'), _('Account settings'), false, 'nav_account');
252 $this->menuItem(common_local_url('imsettings'),
253 _('Connect'), _('Connect settings'), false, 'nav_connect');
254 $this->menuItem(common_local_url('logout'),
255 _('Logout'), _('Logout from the site'), false, 'nav_logout');
257 $this->menuItem(common_local_url('login'),
258 _('Login'), _('Login to the site'), false, 'nav_login');
259 if (!common_config('site', 'closed')) {
260 $this->menuItem(common_local_url('register'),
261 _('Register'), _('Create an account'), false, 'nav_register');
263 $this->menuItem(common_local_url('openidlogin'),
264 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
266 $this->menuItem(common_local_url('doc', array('title' => 'help')),
267 _('Help'), _('Help me!'), false, 'nav_help');
268 $this->elementEnd('ul');
269 $this->elementEnd('dd');
270 $this->elementEnd('dl');
273 // Revist. Should probably do an hAtom pattern here
274 function showSiteNotice()
276 $text = common_config('site', 'notice');
278 $this->elementStart('dl', array('id' => 'site_notice',
279 'class' => 'system_notice'));
280 $this->element('dt', null, _('Site notice'));
281 $this->element('dd', null, $text);
282 $this->elementEnd('dl');
286 // MAY overload if no notice form needed... or direct message box????
288 function showNoticeForm()
290 $notice_form = new NoticeForm($this);
291 $notice_form->show();
294 function showAnonymousMessage()
296 // needs to be defined by the class
301 $this->elementStart('div', array('id' => 'core'));
302 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
303 $this->element('dt', null, _('Local views'));
304 $this->elementStart('dd');
305 $this->showLocalNav();
306 $this->elementEnd('dd');
307 $this->elementEnd('dl');
308 $this->showContentBlock();
310 $this->elementEnd('div');
315 function showLocalNav()
317 // does nothing by default
320 function showContentBlock()
322 $this->elementStart('div', array('id' => 'content'));
323 $this->showPageTitle();
324 $this->showPageNoticeBlock();
325 $this->elementStart('div', array('id' => 'content_inner'));
326 // show the actual content (forms, lists, whatever)
327 $this->showContent();
328 $this->elementEnd('div');
329 $this->elementEnd('div');
332 function showPageTitle() {
333 $this->element('h1', NULL, $this->title());
336 function showPageNoticeBlock()
338 $this->elementStart('dl', array('id' => 'page_notice',
339 'class' => 'system_notice'));
340 $this->element('dt', null, _('Page notice'));
341 $this->elementStart('dd');
342 $this->showPageNotice();
343 $this->elementEnd('dd');
344 $this->elementEnd('dl');
347 // SHOULD overload (unless there's not a notice)
349 function showPageNotice()
355 function showContent()
361 $this->elementStart('div', array('id' => 'aside_primary',
362 'class' => 'aside'));
363 $this->showExportData();
364 $this->showSections();
365 $this->elementEnd('div');
368 // MAY overload if there are feeds
370 function showExportData()
372 // is there structure to this?
373 // list of (visible!) feed links
374 // can we reuse list of feeds from showFeeds() ?
379 function showSections() {
380 // for each section, show it
383 function showFooter()
385 $this->elementStart('div', array('id' => 'footer'));
386 $this->showSecondaryNav();
387 $this->showLicenses();
388 $this->elementEnd('div');
391 function showSecondaryNav()
393 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
394 $this->element('dt', null, _('Secondary site navigation'));
395 $this->elementStart('dd', null);
396 $this->elementStart('ul', array('class' => 'nav'));
397 $this->menuItem(common_local_url('doc', array('title' => 'help')),
399 $this->menuItem(common_local_url('doc', array('title' => 'about')),
401 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
403 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
405 $this->menuItem(common_local_url('doc', array('title' => 'source')),
407 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
409 $this->elementEnd('ul');
410 $this->elementEnd('dd');
411 $this->elementEnd('dl');
414 function showLicenses()
416 $this->elementStart('dl', array('id' => 'licenses'));
417 $this->showLaconicaLicense();
418 $this->showContentLicense();
419 $this->elementEnd('dl');
422 function showLaconicaLicense()
424 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
425 $this->elementStart('dd', null);
426 if (common_config('site', 'broughtby')) {
427 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
429 $instr = _('**%%site.name%%** is a microblogging service. ');
431 $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);
432 $output = common_markup_to_html($instr);
434 $this->elementEnd('dd');
438 function showContentLicense()
440 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
441 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
442 $this->elementStart('p');
443 $this->element('img', array('id' => 'license_cc',
444 'src' => common_config('license', 'image'),
445 'alt' => common_config('license', 'title')));
446 //TODO: This is dirty: i18n
447 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
448 $this->element('a', array('class' => 'license',
449 'rel' => 'external license',
450 'href' => common_config('license', 'url')),
451 common_config('license', 'title'));
452 $this->text(_('license.'));
453 $this->elementEnd('p');
454 $this->elementEnd('dd');
457 // For comparison with If-Last-Modified
458 // If not applicable, return null
460 function last_modified()
470 function isReadOnly()
475 function arg($key, $def=null)
477 if (array_key_exists($key, $this->args)) {
478 return $this->args[$key];
484 function trimmed($key, $def=null)
486 $arg = $this->arg($key, $def);
487 return (is_string($arg)) ? trim($arg) : $arg;
490 // Note: argarray ignored, since it's now passed in in prepare()
492 function handle($argarray=null)
495 $lm = $this->last_modified();
496 $etag = $this->etag();
499 header('ETag: ' . $etag);
503 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
504 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
505 if ($if_modified_since) {
506 $ims = strtotime($if_modified_since);
509 $this->_has_etag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
510 header('HTTP/1.1 304 Not Modified');
511 // Better way to do this?
519 function _has_etag($etag, $if_none_match)
521 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
524 function boolean($key, $def=false)
526 $arg = strtolower($this->trimmed($key));
530 } else if (in_array($arg, array('true', 'yes', '1'))) {
532 } else if (in_array($arg, array('false', 'no', '0'))) {
539 function serverError($msg, $code=500)
541 $action = $this->trimmed('action');
542 common_debug("Server error '$code' on '$action': $msg", __FILE__);
543 common_server_error($msg, $code);
546 function clientError($msg, $code=400)
548 $action = $this->trimmed('action');
549 common_debug("User error '$code' on '$action': $msg", __FILE__);
550 common_user_error($msg, $code);
555 $action = $this->trimmed('action');
557 unset($args['action']);
558 foreach (array_keys($_COOKIE) as $cookie) {
559 unset($args[$cookie]);
561 return common_local_url($action, $args);
564 // Added @id to li for some control.
565 // XXX: We might want to move this to htmloutputter.php
567 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
571 $lattrs['class'] = 'current';
574 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
576 $this->elementStart('li', $lattrs);
577 $attrs['href'] = $url;
579 $attrs['title'] = $title;
581 $this->element('a', $attrs, $text);
582 $this->elementEnd('li');
585 // Does a little before-after block for next/prev page
587 function pagination($have_before, $have_after, $page, $action, $args=null)
589 if ($have_before || $have_after) {
590 $this->elementStart('div', array('class' => 'pagination'));
591 $this->elementStart('dl', null);
592 $this->element('dt', null, _('Pagination'));
593 $this->elementStart('dd', null);
594 $this->elementStart('ul', array('class' => 'nav'));
598 $pargs = array('page' => $page-1);
599 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
601 $this->elementStart('li', array('class' => 'nav_prev'));
602 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
604 $this->elementEnd('li');
608 $pargs = array('page' => $page+1);
609 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
610 $this->elementStart('li', array('class' => 'nav_next'));
611 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
613 $this->elementEnd('li');
616 if ($have_before || $have_after) {
617 $this->elementEnd('ul');
618 $this->elementEnd('dd');
619 $this->elementEnd('dl');
620 $this->elementEnd('div');