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');
197 $this->elementStart('div', 'wrap');
201 $this->elementEnd('div', 'wrap');
202 $this->elementEnd('body');
205 function showHeader()
207 $this->elementStart('div', array('id' => 'header'));
209 $this->showPrimaryNav();
210 $this->showSiteNotice();
211 $this->showNoticeForm();
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 ((isset($config['site']['logo']) && is_string($config['site']['logo']) && (strlen($config['site']['logo']) > 0))
222 || file_exists(theme_file('logo.png')))
224 $this->element('img', array('class' => 'logo photo',
225 'src' => isset($config['site']['logo']) ?
226 ($config['site']['logo']) : theme_path('logo.png'),
227 'alt' => $config['site']['name']));
229 $this->element('span', array('class' => 'fn org'), $config['site']['name']);
230 $this->elementEnd('a');
231 $this->elementEnd('address');
234 function showPrimaryNav()
236 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
237 $this->element('dt', null, _('Primary site navigation'));
238 $this->elementStart('dd');
239 $user = common_current_user();
240 $this->elementStart('ul', array('class' => 'nav'));
242 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
245 $this->menuItem(common_local_url('peoplesearch'), _('Search'));
247 $this->menuItem(common_local_url('profilesettings'),
249 $this->menuItem(common_local_url('invite'),
251 $this->menuItem(common_local_url('logout'),
254 $this->menuItem(common_local_url('login'), _('Login'));
255 if (!common_config('site', 'closed')) {
256 $this->menuItem(common_local_url('register'), _('Register'));
258 $this->menuItem(common_local_url('openidlogin'), _('OpenID'));
260 $this->menuItem(common_local_url('doc', array('title' => 'help')),
262 $this->elementEnd('ul');
263 $this->elementEnd('dd');
264 $this->elementEnd('dl');
267 // Revist. Should probably do an hAtom pattern here
268 function showSiteNotice()
270 $text = common_config('site', 'notice');
272 $this->elementStart('dl', array('id' => 'site_notice',
273 'class' => 'system_notice'));
274 $this->element('dt', null, _('Site notice'));
275 $this->element('dd', null, $text);
276 $this->elementEnd('dl');
280 // MAY overload if no notice form needed... or direct message box????
282 function showNoticeForm()
284 $notice_form = new NoticeForm($this);
285 $notice_form->show();
290 $this->elementStart('div', array('id' => 'core'));
291 $this->showLocalNav();
292 $this->showContentBlock();
294 $this->elementEnd('div');
299 function showLocalNav()
301 // does nothing by default
304 function showContentBlock()
306 $this->elementStart('div', array('id' => 'content'));
307 $this->showPageTitle();
308 $this->showPageNotice();
309 $this->showContent();
310 $this->elementEnd('div');
313 function showPageTitle() {
314 $this->element('h1', NULL, $this->title());
317 // SHOULD overload (unless there's not a notice)
319 function showPageNotice()
321 $this->elementStart('dl', array('id' => 'page_notice',
322 'class' => 'system_notice'));
323 $this->element('dt', null, _('Page notice'));
324 $this->elementStart('dd', null);
325 // Output a bunch of paragraphs here
326 $this->elementEnd('dd');
327 $this->elementEnd('dl');
332 function showContent()
334 // show the actual content (forms, lists, whatever)
335 $this->elementStart('div', array('id' => 'content_inner'));
336 $this->elementEnd('div');
341 $this->showExportData();
342 $this->showSections();
345 // MAY overload if there are feeds
347 function showExportData()
349 // is there structure to this?
350 // list of (visible!) feed links
351 // can we reuse list of feeds from showFeeds() ?
356 function showSections() {
357 // for each section, show it
360 function showFooter()
362 $this->elementStart('div', array('id' => 'footer'));
363 $this->showSecondaryNav();
364 $this->showLicenses();
365 $this->elementEnd('div');
368 function showSecondaryNav()
370 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
371 $this->element('dt', null, _('Secondary site navigation'));
372 $this->elementStart('dd', null);
373 $this->elementStart('ul', array('class' => 'nav'));
374 $this->menuItem(common_local_url('doc', array('title' => 'help')),
376 $this->menuItem(common_local_url('doc', array('title' => 'about')),
378 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
380 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
382 $this->menuItem(common_local_url('doc', array('title' => 'source')),
384 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
386 $this->elementEnd('ul');
387 $this->elementEnd('dd');
388 $this->elementEnd('dl');
391 function showLicenses()
393 $this->elementStart('dl', array('id' => 'licenses'));
394 $this->showLaconicaLicense();
395 $this->showContentLicense();
396 $this->elementEnd('dl');
399 function showLaconicaLicense()
401 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
402 $this->elementStart('dd', null);
403 if (common_config('site', 'broughtby')) {
404 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
406 $instr = _('**%%site.name%%** is a microblogging service. ');
408 $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);
409 $output = common_markup_to_html($instr);
411 $this->elementEnd('dd');
415 function showContentLicense()
417 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
418 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
419 $this->elementStart('p');
420 $this->text(_('Unless otherwise specified, contents of this site are copyright by the contributors and available under the '));
421 $this->element('a', array('class' => 'license',
422 'rel' => 'external license',
423 'href' => $config['license']['url']),
424 $config['license']['title']);
425 $this->text(_('. Contributors should be attributed by full name or nickname.'));
426 $this->elementEnd('p');
427 $this->element('img', array('id' => 'license_cc',
428 'src' => $config['license']['image'],
429 'alt' => $config['license']['title']));
430 $this->elementEnd('dd');
433 // For comparison with If-Last-Modified
434 // If not applicable, return null
436 function last_modified()
446 function is_readonly()
451 function arg($key, $def=null)
453 if (array_key_exists($key, $this->args)) {
454 return $this->args[$key];
460 function trimmed($key, $def=null)
462 $arg = $this->arg($key, $def);
463 return (is_string($arg)) ? trim($arg) : $arg;
466 // Note: argarray ignored, since it's now passed in in prepare()
468 function handle($argarray=null)
471 $lm = $this->last_modified();
472 $etag = $this->etag();
475 header('ETag: ' . $etag);
479 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
480 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
481 if ($if_modified_since) {
482 $ims = strtotime($if_modified_since);
485 $this->_has_etag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
486 header('HTTP/1.1 304 Not Modified');
487 // Better way to do this?
495 function _has_etag($etag, $if_none_match)
497 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
500 function boolean($key, $def=false)
502 $arg = strtolower($this->trimmed($key));
506 } else if (in_array($arg, array('true', 'yes', '1'))) {
508 } else if (in_array($arg, array('false', 'no', '0'))) {
515 function server_error($msg, $code=500)
517 $action = $this->trimmed('action');
518 common_debug("Server error '$code' on '$action': $msg", __FILE__);
519 common_server_error($msg, $code);
522 function client_error($msg, $code=400)
524 $action = $this->trimmed('action');
525 common_debug("User error '$code' on '$action': $msg", __FILE__);
526 common_user_error($msg, $code);
531 $action = $this->trimmed('action');
533 unset($args['action']);
534 foreach (array_keys($_COOKIE) as $cookie) {
535 unset($args[$cookie]);
537 return common_local_url($action, $args);
540 function nav_menu($menu)
542 $action = $this->trimmed('action');
543 $this->elementStart('ul', array('id' => 'nav_views'));
544 foreach ($menu as $menuaction => $menudesc) {
545 $this->menuItem(common_local_url($menuaction,
546 isset($menudesc[2]) ? $menudesc[2] : null),
549 $action == $menuaction);
551 $this->elementEnd('ul');
554 function common_show_header($pagetitle, $callable=null, $data=null, $headercall=null)
557 global $action; /* XXX: kind of cheating here. */
561 $this->elementStart('head');
565 call_user_func($callable, $data);
567 call_user_func($callable);
570 $this->elementEnd('head');
571 $this->elementStart('body', $action);
572 $this->elementStart('div', array('id' => 'wrap'));
573 $this->elementStart('div', array('id' => 'content'));
576 // Added @id to li for some control.
577 // XXX: We might want to move this to htmloutputter.php
579 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
583 $lattrs['class'] = 'current';
586 $this->elementStart('li', (is_null($id)) ? null : array('id' => $id), $lattrs);
587 $attrs['href'] = $url;
589 $attrs['title'] = $title;
591 $this->element('a', $attrs, $text);
592 $this->elementEnd('li');
595 // Does a little before-after block for next/prev page
597 function pagination($have_before, $have_after, $page, $action, $args=null)
599 if ($have_before || $have_after) {
600 $this->elementStart('div', array('class' => 'pagination'));
601 $this->elementStart('dl', null);
602 $this->element('dt', null, _('Pagination'));
603 $this->elementStart('dd', null);
604 $this->elementStart('ul', array('class' => 'nav'));
608 $pargs = array('page' => $page-1);
609 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
611 $this->elementStart('li', array('class' => 'nav_prev'));
612 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
614 $this->elementEnd('li');
618 $pargs = array('page' => $page+1);
619 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
620 $this->elementStart('li', array('class' => 'nav_next'));
621 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
623 $this->elementEnd('li');
626 if ($have_before || $have_after) {
627 $this->elementEnd('ul');
628 $this->elementEnd('dd');
629 $this->elementEnd('dl');
630 $this->elementEnd('div');