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/form.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
65 // For initializing members of the class
67 function prepare($argarray)
69 $this->args =& common_copy_args($argarray);
83 // XXX: attributes (profile?)
84 $this->startElement('head');
86 $this->showStylesheets();
88 $this->showOpenSearch();
90 $this->showDescription();
92 $this->elementElement('head');
97 $this->element('title', null,
100 common_config('site', 'name')));
107 return _("Untitled page");
110 function showStylesheets()
112 $this->element('link', array('rel' => 'stylesheet',
113 'type' => 'text/css',
114 'href' => theme_path('display.css') . '?version=' . LACONICA_VERSION,
115 'media' => 'screen, projection, tv'));
116 foreach (array(6,7) as $ver) {
117 if (file_exists(theme_file('ie'.$ver.'.css'))) {
118 // Yes, IE people should be put in jail.
119 $xw->writeComment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
120 'href="'.theme_path('ie'.$ver.'.css').'?version='.LACONICA_VERSION.'" /><![endif]');
125 function showScripts()
127 $this->element('script', array('type' => 'text/javascript',
128 'src' => common_path('js/jquery.min.js')),
130 $this->element('script', array('type' => 'text/javascript',
131 'src' => common_path('js/jquery.form.js')),
133 $this->element('script', array('type' => 'text/javascript',
134 'src' => common_path('js/xbImportNode.js')),
136 $this->element('script', array('type' => 'text/javascript',
137 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
141 function showOpenSearch()
143 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
144 'href' => common_local_url('opensearch', array('type' => 'people')),
145 'title' => common_config('site', 'name').' People Search'));
147 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
148 'href' => common_local_url('opensearch', array('type' => 'notice')),
149 'title' => common_config('site', 'name').' Notice Search'));
156 // does nothing by default
161 function showDescription()
163 // does nothing by default
170 // does nothing by default
175 $this->elementStart('body');
176 $this->elementStart('wrap');
180 $this->elementEnd('wrap');
181 $this->elementEnd('body');
184 function showHeader()
186 $this->elementStart('div', array('id' => 'header'));
188 $this->showPrimaryNav();
189 $this->showSiteNotice();
190 $this->showNoticeForm();
191 $this->elementEnd('div');
196 $this->elementStart('address', array('id' => 'site_contact',
197 'class' => 'vcard'));
198 $this->elementStart('a', array('class' => 'url home bookmark',
199 'href' => common_local_url('public')));
200 if ((isset($config['site']['logo']) && is_string($config['site']['logo']) && (strlen($config['site']['logo']) > 0))
201 || file_exists(theme_file('logo.png')))
203 $this->element('img', array('class' => 'logo photo',
204 'src' => isset($config['site']['logo']) ?
205 ($config['site']['logo']) : theme_path('logo.png'),
206 'alt' => $config['site']['name']));
208 $this->element('span', array('class' => 'fn org'), $config['site']['name']);
209 $this->elementEnd('a');
210 $this->elementEnd('address');
213 function showPrimaryNav()
215 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
216 $this->element('dt', null, _('Primary site navigation'));
217 $user = common_current_user();
218 $this->elementStart('ul', array('id' => 'nav'));
220 common_menu_item(common_local_url('all', array('nickname' => $user->nickname)),
223 common_menu_item(common_local_url('peoplesearch'), _('Search'));
225 common_menu_item(common_local_url('profilesettings'),
227 common_menu_item(common_local_url('invite'),
229 common_menu_item(common_local_url('logout'),
232 common_menu_item(common_local_url('login'), _('Login'));
233 if (!common_config('site', 'closed')) {
234 common_menu_item(common_local_url('register'), _('Register'));
236 common_menu_item(common_local_url('openidlogin'), _('OpenID'));
238 common_menu_item(common_local_url('doc', array('title' => 'help')),
240 $this->elementEnd('ul');
241 $this->elementEnd('dl');
244 // Revist. Should probably do an hAtom pattern here
245 function showSiteNotice()
247 $this->elementStart('dl', array('id' => 'site_notice',
248 'class' => 'system_notice'));
249 $this->element('dt', null, _('Site notice'));
250 $this->elementStart('dd', null);
251 // Output a bunch of paragraphs here
252 $this->elementEnd('dd');
255 // MAY overload if no notice form needed... or direct message box????
257 function showNoticeForm()
259 $notice_form = new NoticeForm();
260 $notice_form->show();
265 $this->elementStart('div', array('class' => 'core'));
266 $this->showLocalNav();
267 $this->showContentBlock();
269 $this->elementEnd('div');
272 // SHOULD overload (perhaps this should be a MUST because sometimes it is not used)
274 function showLocalNav($menu)
276 $action = $this->trimmed('action');
278 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
279 $this->element('dt', null, _('Local views'));
280 $this->elementStart('ul', array('id' => 'nav'));
281 foreach ($menu as $menuaction => $menudesc) {
282 common_menu_item(common_local_url($menuaction,
283 isset($menudesc[2]) ? $menudesc[2] : null),
286 $action == $menuaction);
288 $this->elementEnd('ul');
289 $this->elementEnd('dd');
290 $this->elementEnd('dl');
293 function showContentBlock()
295 $this->elementStart('div', array('id' => 'content'));
296 $this->showPageTitle();
297 $this->showPageNotice();
298 $this->showContent();
299 $this->elementEnd('div');
302 function showPageTitle() {
303 $this->element('h1', NULL, $this->title());
306 // SHOULD overload (unless there's not a notice)
308 function showPageNotice()
310 $this->elementStart('dl', array('id' => 'page_notice',
311 'class' => 'system_notice'));
312 $this->element('dt', null, _('Page notice'));
313 $this->elementStart('dd', null);
314 // Output a bunch of paragraphs here
315 $this->elementEnd('dd');
320 function showContent()
322 // show the actual content (forms, lists, whatever)
323 $this->elementStart('div', array('id' => 'content_inner'));
324 $this->elementEnd('div');
329 $this->showExportData();
330 $this->showSections();
333 // MAY overload if there are feeds
335 function showExportData()
337 // is there structure to this?
338 // list of (visible!) feed links
339 // can we reuse list of feeds from showFeeds() ?
344 function showSections() {
345 // for each section, show it
348 function showFooter()
350 $this->elementStart('div', array('id' => 'footer'));
351 $this->showSecondaryNav();
352 $this->showLicenses();
353 $this->elementEnd('div');
356 function showSecondaryNav()
358 $this->elementStart('ul', array('id' => 'nav_sub'));
359 common_menu_item(common_local_url('doc', array('title' => 'help')),
361 common_menu_item(common_local_url('doc', array('title' => 'about')),
363 common_menu_item(common_local_url('doc', array('title' => 'faq')),
365 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
367 common_menu_item(common_local_url('doc', array('title' => 'source')),
369 common_menu_item(common_local_url('doc', array('title' => 'contact')),
371 $this->elementEnd('ul');
374 function showLicenses()
376 $this->elementStart('dl', array('id' => 'licenses'));
377 $this->showLaconicaLicense();
378 $this->showContentLicense();
379 $this->elementEnd('dl');
382 function showLaconicaLicense()
384 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
385 $this->elementStart('dd', null);
386 if (common_config('site', 'broughtby')) {
387 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
389 $instr = _('**%%site.name%%** is a microblogging service. ');
391 $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);
392 $output = common_markup_to_html($instr);
394 $this->elementEnd('dd');
398 function showContentLicense()
400 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
401 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
402 $this->elementStart('p');
403 common_text(_('Unless otherwise specified, contents of this site are copyright by the contributors and available under the '));
404 $this->element('a', array('class' => 'license',
405 'rel' => 'external license',
406 'href' => $config['license']['url']),
407 $config['license']['title']);
408 common_text(_('. Contributors should be attributed by full name or nickname.'));
409 $this->elementEnd('p');
410 $this->element('img', array('id' => 'license_cc',
411 'src' => $config['license']['image'],
412 'alt' => $config['license']['title']));
413 $this->elementEnd('dd');
416 // For comparison with If-Last-Modified
417 // If not applicable, return null
419 function last_modified()
429 function is_readonly()
434 function arg($key, $def=null)
436 if (array_key_exists($key, $this->args)) {
437 return $this->args[$key];
443 function trimmed($key, $def=null)
445 $arg = $this->arg($key, $def);
446 return (is_string($arg)) ? trim($arg) : $arg;
449 // Note: argarray ignored, since it's now passed in in prepare()
451 function handle($argarray=null)
454 $lm = $this->last_modified();
455 $etag = $this->etag();
458 header('ETag: ' . $etag);
462 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
463 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
464 if ($if_modified_since) {
465 $ims = strtotime($if_modified_since);
468 $this->_has_etag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
469 header('HTTP/1.1 304 Not Modified');
470 // Better way to do this?
478 function _has_etag($etag, $if_none_match)
480 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
483 function boolean($key, $def=false)
485 $arg = strtolower($this->trimmed($key));
489 } else if (in_array($arg, array('true', 'yes', '1'))) {
491 } else if (in_array($arg, array('false', 'no', '0'))) {
498 function server_error($msg, $code=500)
500 $action = $this->trimmed('action');
501 common_debug("Server error '$code' on '$action': $msg", __FILE__);
502 common_server_error($msg, $code);
505 function client_error($msg, $code=400)
507 $action = $this->trimmed('action');
508 common_debug("User error '$code' on '$action': $msg", __FILE__);
509 common_user_error($msg, $code);
514 $action = $this->trimmed('action');
516 unset($args['action']);
517 foreach (array_keys($_COOKIE) as $cookie) {
518 unset($args[$cookie]);
520 return common_local_url($action, $args);
523 function nav_menu($menu)
525 $action = $this->trimmed('action');
526 $this->elementStart('ul', array('id' => 'nav_views'));
527 foreach ($menu as $menuaction => $menudesc) {
528 common_menu_item(common_local_url($menuaction,
529 isset($menudesc[2]) ? $menudesc[2] : null),
532 $action == $menuaction);
534 $this->elementEnd('ul');
537 function common_show_header($pagetitle, $callable=null, $data=null, $headercall=null)
540 global $action; /* XXX: kind of cheating here. */
544 $this->elementStart('head');
548 call_user_func($callable, $data);
550 call_user_func($callable);
553 $this->elementEnd('head');
554 $this->elementStart('body', $action);
555 $this->elementStart('div', array('id' => 'wrap'));
556 $this->elementStart('div', array('id' => 'content'));
560 // Added @id to li for some control. We might want to move this to htmloutputter.php
561 function common_menu_item($id=null, $url, $text, $title=null, $is_selected=false)
565 $lattrs['class'] = 'current';
568 $this->elementStart('li', (is_null($id)) ? null : array('id' => $id), $lattrs);
569 $attrs['href'] = $url;
571 $attrs['title'] = $title;
573 $this->element('a', $attrs, $text);
574 $this->elementEnd('li');
577 // Does a little before-after block for next/prev page
579 function pagination($have_before, $have_after, $page, $action, $args=null)
581 if ($have_before || $have_after) {
582 $this->elementStart('div', array('class' => 'pagination'));
583 $this->elementStart('dl', null);
584 $this->element('dt', null, _('Pagination'));
585 $this->elementStart('dd', null);
586 $this->elementStart('ul', array('class' => 'nav'));
590 $pargs = array('page' => $page-1);
591 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
593 $this->elementStart('li', array('class' => 'nav_prev'));
594 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
596 $this->elementEnd('li');
600 $pargs = array('page' => $page+1);
601 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
602 $this->elementStart('li', array('class' => 'nav_next'));
603 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
605 $this->elementEnd('li');
608 if ($have_before || $have_after) {
609 $this->elementEnd('ul');
610 $this->elementEnd('dd');
611 $this->elementEnd('dl');
612 $this->elementEnd('div');