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/display.css', null) . '?version=' . LACONICA_VERSION,
132 'media' => 'screen, projection, tv'));
133 foreach (array(6,7) as $ver) {
134 if (file_exists(theme_file('ie'.$ver.'.css'))) {
135 // Yes, IE people should be put in jail.
136 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
137 'href="'.theme_path('ie'.$ver.'.css').'?version='.LACONICA_VERSION.'" /><![endif]');
142 function showScripts()
144 $this->element('script', array('type' => 'text/javascript',
145 'src' => common_path('js/jquery.min.js')),
147 $this->element('script', array('type' => 'text/javascript',
148 'src' => common_path('js/jquery.form.js')),
150 $this->element('script', array('type' => 'text/javascript',
151 'src' => common_path('js/xbImportNode.js')),
153 $this->element('script', array('type' => 'text/javascript',
154 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
158 function showOpenSearch()
160 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
161 'href' => common_local_url('opensearch', array('type' => 'people')),
162 'title' => common_config('site', 'name').' People Search'));
164 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
165 'href' => common_local_url('opensearch', array('type' => 'notice')),
166 'title' => common_config('site', 'name').' Notice Search'));
173 // does nothing by default
178 function showDescription()
180 // does nothing by default
187 // does nothing by default
192 $this->elementStart('body', array('id' => $this->trimmed('action')));
193 $this->elementStart('div', 'wrap');
197 $this->elementEnd('div');
198 $this->elementEnd('body');
201 function showHeader()
203 $this->elementStart('div', array('id' => 'header'));
205 $this->showPrimaryNav();
206 $this->showSiteNotice();
207 if (common_logged_in()) {
208 $this->showNoticeForm();
210 $this->showAnonymousMessage();
212 $this->elementEnd('div');
217 $this->elementStart('address', array('id' => 'site_contact',
218 'class' => 'vcard'));
219 $this->elementStart('a', array('class' => 'url home bookmark',
220 'href' => common_local_url('public')));
221 if (common_config('site', 'logo') || file_exists(theme_file('logo.png')))
223 $this->element('img', array('class' => 'logo photo',
224 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
225 'alt' => common_config('site', 'name')));
227 $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
228 $this->elementEnd('a');
229 $this->elementEnd('address');
232 function showPrimaryNav()
234 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
235 $this->element('dt', null, _('Primary site navigation'));
236 $this->elementStart('dd');
237 $user = common_current_user();
238 $this->elementStart('ul', array('class' => 'nav'));
240 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
243 $this->menuItem(common_local_url('peoplesearch'), _('Search'));
245 $this->menuItem(common_local_url('profilesettings'),
247 $this->menuItem(common_local_url('invite'),
249 $this->menuItem(common_local_url('logout'),
252 $this->menuItem(common_local_url('login'), _('Login'));
253 if (!common_config('site', 'closed')) {
254 $this->menuItem(common_local_url('register'), _('Register'));
256 $this->menuItem(common_local_url('openidlogin'), _('OpenID'));
258 $this->menuItem(common_local_url('doc', array('title' => 'help')),
260 $this->elementEnd('ul');
261 $this->elementEnd('dd');
262 $this->elementEnd('dl');
265 // Revist. Should probably do an hAtom pattern here
266 function showSiteNotice()
268 $text = common_config('site', 'notice');
270 $this->elementStart('dl', array('id' => 'site_notice',
271 'class' => 'system_notice'));
272 $this->element('dt', null, _('Site notice'));
273 $this->element('dd', null, $text);
274 $this->elementEnd('dl');
278 // MAY overload if no notice form needed... or direct message box????
280 function showNoticeForm()
282 $notice_form = new NoticeForm($this);
283 $notice_form->show();
286 function showAnonymousMessage()
288 // needs to be defined by the class
293 $this->elementStart('div', array('id' => 'core'));
294 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
295 $this->element('dt', null, _('Local views'));
296 $this->elementStart('dd');
297 $this->showLocalNav();
298 $this->elementEnd('dd');
299 $this->elementEnd('dl');
300 $this->showContentBlock();
302 $this->elementEnd('div');
307 function showLocalNav()
309 // does nothing by default
312 function showContentBlock()
314 $this->elementStart('div', array('id' => 'content'));
315 $this->showPageTitle();
316 $this->showPageNoticeBlock();
317 $this->elementStart('div', array('id' => 'content_inner'));
318 // show the actual content (forms, lists, whatever)
319 $this->showContent();
320 $this->elementEnd('div');
321 $this->elementEnd('div');
324 function showPageTitle() {
325 $this->element('h1', NULL, $this->title());
328 function showPageNoticeBlock()
330 $this->elementStart('dl', array('id' => 'page_notice',
331 'class' => 'system_notice'));
332 $this->element('dt', null, _('Page notice'));
333 $this->elementStart('dd');
334 $this->showPageNotice();
335 $this->elementEnd('dd');
336 $this->elementEnd('dl');
339 // SHOULD overload (unless there's not a notice)
341 function showPageNotice()
347 function showContent()
353 $this->elementStart('div', array('id' => 'aside_primary',
354 'class' => 'aside'));
355 $this->showExportData();
356 $this->showSections();
357 $this->elementEnd('div');
360 // MAY overload if there are feeds
362 function showExportData()
364 // is there structure to this?
365 // list of (visible!) feed links
366 // can we reuse list of feeds from showFeeds() ?
371 function showSections() {
372 // for each section, show it
375 function showFooter()
377 $this->elementStart('div', array('id' => 'footer'));
378 $this->showSecondaryNav();
379 $this->showLicenses();
380 $this->elementEnd('div');
383 function showSecondaryNav()
385 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
386 $this->element('dt', null, _('Secondary site navigation'));
387 $this->elementStart('dd', null);
388 $this->elementStart('ul', array('class' => 'nav'));
389 $this->menuItem(common_local_url('doc', array('title' => 'help')),
391 $this->menuItem(common_local_url('doc', array('title' => 'about')),
393 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
395 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
397 $this->menuItem(common_local_url('doc', array('title' => 'source')),
399 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
401 $this->elementEnd('ul');
402 $this->elementEnd('dd');
403 $this->elementEnd('dl');
406 function showLicenses()
408 $this->elementStart('dl', array('id' => 'licenses'));
409 $this->showLaconicaLicense();
410 $this->showContentLicense();
411 $this->elementEnd('dl');
414 function showLaconicaLicense()
416 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
417 $this->elementStart('dd', null);
418 if (common_config('site', 'broughtby')) {
419 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
421 $instr = _('**%%site.name%%** is a microblogging service. ');
423 $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);
424 $output = common_markup_to_html($instr);
426 $this->elementEnd('dd');
430 function showContentLicense()
432 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
433 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
434 $this->elementStart('p');
435 $this->element('img', array('id' => 'license_cc',
436 'src' => common_config('license', 'image'),
437 'alt' => common_config('license', 'title')));
438 $this->text(_('All criti.ca content and data are available under the '));
439 $this->element('a', array('class' => 'license',
440 'rel' => 'external license',
441 'href' => common_config('license', 'url')),
442 common_config('license', 'title'));
443 $this->text(_('license.'));
444 $this->elementEnd('p');
445 $this->elementEnd('dd');
448 // For comparison with If-Last-Modified
449 // If not applicable, return null
451 function last_modified()
461 function isReadOnly()
466 function arg($key, $def=null)
468 if (array_key_exists($key, $this->args)) {
469 return $this->args[$key];
475 function trimmed($key, $def=null)
477 $arg = $this->arg($key, $def);
478 return (is_string($arg)) ? trim($arg) : $arg;
481 // Note: argarray ignored, since it's now passed in in prepare()
483 function handle($argarray=null)
486 $lm = $this->last_modified();
487 $etag = $this->etag();
490 header('ETag: ' . $etag);
494 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
495 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
496 if ($if_modified_since) {
497 $ims = strtotime($if_modified_since);
500 $this->_has_etag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
501 header('HTTP/1.1 304 Not Modified');
502 // Better way to do this?
510 function _has_etag($etag, $if_none_match)
512 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
515 function boolean($key, $def=false)
517 $arg = strtolower($this->trimmed($key));
521 } else if (in_array($arg, array('true', 'yes', '1'))) {
523 } else if (in_array($arg, array('false', 'no', '0'))) {
530 function serverError($msg, $code=500)
532 $action = $this->trimmed('action');
533 common_debug("Server error '$code' on '$action': $msg", __FILE__);
534 common_server_error($msg, $code);
537 function clientError($msg, $code=400)
539 $action = $this->trimmed('action');
540 common_debug("User error '$code' on '$action': $msg", __FILE__);
541 common_user_error($msg, $code);
546 $action = $this->trimmed('action');
548 unset($args['action']);
549 foreach (array_keys($_COOKIE) as $cookie) {
550 unset($args[$cookie]);
552 return common_local_url($action, $args);
555 // Added @id to li for some control.
556 // XXX: We might want to move this to htmloutputter.php
558 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
562 $lattrs['class'] = 'current';
565 $this->elementStart('li', (is_null($id)) ? null : array('id' => $id), $lattrs);
566 $attrs['href'] = $url;
568 $attrs['title'] = $title;
570 $this->element('a', $attrs, $text);
571 $this->elementEnd('li');
574 // Does a little before-after block for next/prev page
576 function pagination($have_before, $have_after, $page, $action, $args=null)
578 if ($have_before || $have_after) {
579 $this->elementStart('div', array('class' => 'pagination'));
580 $this->elementStart('dl', null);
581 $this->element('dt', null, _('Pagination'));
582 $this->elementStart('dd', null);
583 $this->elementStart('ul', array('class' => 'nav'));
587 $pargs = array('page' => $page-1);
588 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
590 $this->elementStart('li', array('class' => 'nav_prev'));
591 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
593 $this->elementEnd('li');
597 $pargs = array('page' => $page+1);
598 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
599 $this->elementStart('li', array('class' => 'nav_next'));
600 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
602 $this->elementEnd('li');
605 if ($have_before || $have_after) {
606 $this->elementEnd('ul');
607 $this->elementEnd('dd');
608 $this->elementEnd('dl');
609 $this->elementEnd('div');