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
176 // output wrap element
182 function showHeader()
184 $this->elementStart('div', array('id' => 'header'));
186 $this->showPrimaryNav();
187 $this->showSiteNotice();
188 $this->showNoticeForm();
189 $this->elementEnd('div');
194 $this->elementStart('address', array('id' => 'site_contact',
195 'class' => 'vcard'));
196 $this->elementStart('a', array('class' => 'url home bookmark',
197 'href' => common_local_url('public')));
198 if ((isset($config['site']['logo']) && is_string($config['site']['logo']) && (strlen($config['site']['logo']) > 0))
199 || file_exists(theme_file('logo.png')))
201 $this->element('img', array('class' => 'logo photo',
202 'src' => isset($config['site']['logo']) ?
203 ($config['site']['logo']) : theme_path('logo.png'),
204 'alt' => $config['site']['name']));
206 $this->element('span', array('class' => 'fn org'), $config['site']['name']);
207 $this->elementEnd('a');
208 $this->elementEnd('address');
211 function showPrimaryNav()
213 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
214 $this->element('dt', null, _('Primary site navigation'));
215 $user = common_current_user();
216 $this->elementStart('ul', array('id' => 'nav'));
218 common_menu_item(common_local_url('all', array('nickname' => $user->nickname)),
221 common_menu_item(common_local_url('peoplesearch'), _('Search'));
223 common_menu_item(common_local_url('profilesettings'),
225 common_menu_item(common_local_url('invite'),
227 common_menu_item(common_local_url('logout'),
230 common_menu_item(common_local_url('login'), _('Login'));
231 if (!common_config('site', 'closed')) {
232 common_menu_item(common_local_url('register'), _('Register'));
234 common_menu_item(common_local_url('openidlogin'), _('OpenID'));
236 common_menu_item(common_local_url('doc', array('title' => 'help')),
238 $this->elementEnd('ul');
239 $this->elementEnd('dl');
242 // Revist. Should probably do an hAtom pattern here
243 function showSiteNotice()
245 $this->elementStart('dl', array('id' => 'site_notice',
246 'class' => 'system_notice'));
247 $this->element('dt', null, _('Site notice'));
248 $this->elementStart('dd', null);
249 // Output a bunch of paragraphs here
250 $this->elementEnd('dd');
253 // MAY overload if no notice form needed... or direct message box????
255 function showNoticeForm()
257 $notice_form = new NoticeForm();
258 $notice_form->show();
264 $this->showLocalNav();
265 $this->showContentBlock();
270 // SHOULD overload (perhaps this should be a MUST because sometimes it is not used)
272 function showLocalNav($menu)
274 $action = $this->trimmed('action');
276 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
277 $this->element('dt', null, _('Local views'));
278 $this->elementStart('ul', array('id' => 'nav'));
279 foreach ($menu as $menuaction => $menudesc) {
280 common_menu_item(common_local_url($menuaction,
281 isset($menudesc[2]) ? $menudesc[2] : null),
284 $action == $menuaction);
286 $this->elementEnd('ul');
287 $this->elementEnd('dd');
288 $this->elementEnd('dl');
291 function showContentBlock()
293 $this->elementStart('div', array('id' => 'content',
294 (is_null("basic single content page e.g., about")) ? null : 'class' => 'hentry'));
295 $this->showPageTitle();
296 $this->showPageNotice();
297 $this->showContent();
298 $this->elementEnd('div');
301 function showPageTitle() {
302 $this->element('h1', NULL, $this->title());
305 // SHOULD overload (unless there's not a notice)
307 function showPageNotice()
309 $this->elementStart('dl', array('id' => 'page_notice',
310 'class' => 'system_notice'));
311 $this->element('dt', null, _('Page notice'));
312 $this->elementStart('dd', null);
313 // Output a bunch of paragraphs here
314 $this->elementEnd('dd');
319 function showContent()
321 // show the actual content (forms, lists, whatever)
322 $this->elementStart('div', array('id' => 'content_inner',
323 (is_null("basic single content page e.g., about")) ? null : 'class' => 'entry-content'));
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'));
559 function common_show_footer()
562 $this->elementEnd('div'); // content div
564 $this->elementEnd('div');
565 $this->elementEnd('body');
566 $this->elementEnd('html');
570 // Added @id to li for some control. We might want to move this to htmloutputter.php
571 function common_menu_item($id=null, $url, $text, $title=null, $is_selected=false)
575 $lattrs['class'] = 'current';
578 $this->elementStart('li', (is_null($id)) ? null : array('id' => $id), $lattrs);
579 $attrs['href'] = $url;
581 $attrs['title'] = $title;
583 $this->element('a', $attrs, $text);
584 $this->elementEnd('li');
587 // Does a little before-after block for next/prev page
589 function pagination($have_before, $have_after, $page, $action, $args=null)
591 if ($have_before || $have_after) {
592 $this->elementStart('div', array('class' => 'pagination'));
593 $this->elementStart('dl', null);
594 $this->element('dt', null, _('Pagination'));
595 $this->elementStart('dd', null);
596 $this->elementStart('ul', array('class' => 'nav'));
600 $pargs = array('page' => $page-1);
601 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
603 $this->elementStart('li', array('class' => 'nav_prev'));
604 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
606 $this->elementEnd('li');
610 $pargs = array('page' => $page+1);
611 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
612 $this->elementStart('li', array('class' => 'nav_next'));
613 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
615 $this->elementEnd('li');
618 if ($have_before || $have_after) {
619 $this->elementEnd('ul');
620 $this->elementEnd('dd');
621 $this->elementEnd('dl');
622 $this->elementEnd('div');