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', 'base') . '?version=' . LACONICA_VERSION,
115 'media' => 'screen, projection, tv'));
116 $this->element('link', array('rel' => 'stylesheet',
117 'type' => 'text/css',
118 'href' => theme_path('thickbox.css', 'base') . '?version=' . LACONICA_VERSION,
119 'media' => 'screen, projection, tv'));
120 $this->element('link', array('rel' => 'stylesheet',
121 'type' => 'text/css',
122 'href' => theme_path('display.css', null) . '?version=' . LACONICA_VERSION,
123 'media' => 'screen, projection, tv'));
124 foreach (array(6,7) as $ver) {
125 if (file_exists(theme_file('ie'.$ver.'.css'))) {
126 // Yes, IE people should be put in jail.
127 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
128 'href="'.theme_path('ie'.$ver.'.css').'?version='.LACONICA_VERSION.'" /><![endif]');
133 function showScripts()
135 $this->element('script', array('type' => 'text/javascript',
136 'src' => common_path('js/jquery.min.js')),
138 $this->element('script', array('type' => 'text/javascript',
139 'src' => common_path('js/jquery.form.js')),
141 $this->element('script', array('type' => 'text/javascript',
142 'src' => common_path('js/xbImportNode.js')),
144 $this->element('script', array('type' => 'text/javascript',
145 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
149 function showOpenSearch()
151 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
152 'href' => common_local_url('opensearch', array('type' => 'people')),
153 'title' => common_config('site', 'name').' People Search'));
155 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
156 'href' => common_local_url('opensearch', array('type' => 'notice')),
157 'title' => common_config('site', 'name').' Notice Search'));
164 // does nothing by default
169 function showDescription()
171 // does nothing by default
178 // does nothing by default
183 $this->elementStart('body');
184 $this->elementStart('wrap');
188 $this->elementEnd('wrap');
189 $this->elementEnd('body');
192 function showHeader()
194 $this->elementStart('div', array('id' => 'header'));
196 $this->showPrimaryNav();
197 $this->showSiteNotice();
198 $this->showNoticeForm();
199 $this->elementEnd('div');
204 $this->elementStart('address', array('id' => 'site_contact',
205 'class' => 'vcard'));
206 $this->elementStart('a', array('class' => 'url home bookmark',
207 'href' => common_local_url('public')));
208 if ((isset($config['site']['logo']) && is_string($config['site']['logo']) && (strlen($config['site']['logo']) > 0))
209 || file_exists(theme_file('logo.png')))
211 $this->element('img', array('class' => 'logo photo',
212 'src' => isset($config['site']['logo']) ?
213 ($config['site']['logo']) : theme_path('logo.png'),
214 'alt' => $config['site']['name']));
216 $this->element('span', array('class' => 'fn org'), $config['site']['name']);
217 $this->elementEnd('a');
218 $this->elementEnd('address');
221 function showPrimaryNav()
223 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
224 $this->element('dt', null, _('Primary site navigation'));
225 $user = common_current_user();
226 $this->elementStart('ul', array('id' => 'nav'));
228 common_menu_item(common_local_url('all', array('nickname' => $user->nickname)),
231 common_menu_item(common_local_url('peoplesearch'), _('Search'));
233 common_menu_item(common_local_url('profilesettings'),
235 common_menu_item(common_local_url('invite'),
237 common_menu_item(common_local_url('logout'),
240 common_menu_item(common_local_url('login'), _('Login'));
241 if (!common_config('site', 'closed')) {
242 common_menu_item(common_local_url('register'), _('Register'));
244 common_menu_item(common_local_url('openidlogin'), _('OpenID'));
246 common_menu_item(common_local_url('doc', array('title' => 'help')),
248 $this->elementEnd('ul');
249 $this->elementEnd('dl');
252 // Revist. Should probably do an hAtom pattern here
253 function showSiteNotice()
255 $this->elementStart('dl', array('id' => 'site_notice',
256 'class' => 'system_notice'));
257 $this->element('dt', null, _('Site notice'));
258 $this->elementStart('dd', null);
259 // Output a bunch of paragraphs here
260 $this->elementEnd('dd');
263 // MAY overload if no notice form needed... or direct message box????
265 function showNoticeForm()
267 $notice_form = new NoticeForm();
268 $notice_form->show();
273 $this->elementStart('div', array('class' => 'core'));
274 $this->showLocalNav();
275 $this->showContentBlock();
277 $this->elementEnd('div');
280 // SHOULD overload (perhaps this should be a MUST because sometimes it is not used)
282 function showLocalNav($menu)
284 $action = $this->trimmed('action');
286 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
287 $this->element('dt', null, _('Local views'));
288 $this->elementStart('ul', array('id' => 'nav'));
289 foreach ($menu as $menuaction => $menudesc) {
290 common_menu_item(common_local_url($menuaction,
291 isset($menudesc[2]) ? $menudesc[2] : null),
294 $action == $menuaction);
296 $this->elementEnd('ul');
297 $this->elementEnd('dd');
298 $this->elementEnd('dl');
301 function showContentBlock()
303 $this->elementStart('div', array('id' => 'content'));
304 $this->showPageTitle();
305 $this->showPageNotice();
306 $this->showContent();
307 $this->elementEnd('div');
310 function showPageTitle() {
311 $this->element('h1', NULL, $this->title());
314 // SHOULD overload (unless there's not a notice)
316 function showPageNotice()
318 $this->elementStart('dl', array('id' => 'page_notice',
319 'class' => 'system_notice'));
320 $this->element('dt', null, _('Page notice'));
321 $this->elementStart('dd', null);
322 // Output a bunch of paragraphs here
323 $this->elementEnd('dd');
328 function showContent()
330 // show the actual content (forms, lists, whatever)
331 $this->elementStart('div', array('id' => 'content_inner'));
332 $this->elementEnd('div');
337 $this->showExportData();
338 $this->showSections();
341 // MAY overload if there are feeds
343 function showExportData()
345 // is there structure to this?
346 // list of (visible!) feed links
347 // can we reuse list of feeds from showFeeds() ?
352 function showSections() {
353 // for each section, show it
356 function showFooter()
358 $this->elementStart('div', array('id' => 'footer'));
359 $this->showSecondaryNav();
360 $this->showLicenses();
361 $this->elementEnd('div');
364 function showSecondaryNav()
366 $this->elementStart('ul', array('id' => 'nav_sub'));
367 common_menu_item(common_local_url('doc', array('title' => 'help')),
369 common_menu_item(common_local_url('doc', array('title' => 'about')),
371 common_menu_item(common_local_url('doc', array('title' => 'faq')),
373 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
375 common_menu_item(common_local_url('doc', array('title' => 'source')),
377 common_menu_item(common_local_url('doc', array('title' => 'contact')),
379 $this->elementEnd('ul');
382 function showLicenses()
384 $this->elementStart('dl', array('id' => 'licenses'));
385 $this->showLaconicaLicense();
386 $this->showContentLicense();
387 $this->elementEnd('dl');
390 function showLaconicaLicense()
392 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
393 $this->elementStart('dd', null);
394 if (common_config('site', 'broughtby')) {
395 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
397 $instr = _('**%%site.name%%** is a microblogging service. ');
399 $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);
400 $output = common_markup_to_html($instr);
402 $this->elementEnd('dd');
406 function showContentLicense()
408 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
409 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
410 $this->elementStart('p');
411 common_text(_('Unless otherwise specified, contents of this site are copyright by the contributors and available under the '));
412 $this->element('a', array('class' => 'license',
413 'rel' => 'external license',
414 'href' => $config['license']['url']),
415 $config['license']['title']);
416 common_text(_('. Contributors should be attributed by full name or nickname.'));
417 $this->elementEnd('p');
418 $this->element('img', array('id' => 'license_cc',
419 'src' => $config['license']['image'],
420 'alt' => $config['license']['title']));
421 $this->elementEnd('dd');
424 // For comparison with If-Last-Modified
425 // If not applicable, return null
427 function last_modified()
437 function is_readonly()
442 function arg($key, $def=null)
444 if (array_key_exists($key, $this->args)) {
445 return $this->args[$key];
451 function trimmed($key, $def=null)
453 $arg = $this->arg($key, $def);
454 return (is_string($arg)) ? trim($arg) : $arg;
457 // Note: argarray ignored, since it's now passed in in prepare()
459 function handle($argarray=null)
462 $lm = $this->last_modified();
463 $etag = $this->etag();
466 header('ETag: ' . $etag);
470 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
471 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
472 if ($if_modified_since) {
473 $ims = strtotime($if_modified_since);
476 $this->_has_etag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
477 header('HTTP/1.1 304 Not Modified');
478 // Better way to do this?
486 function _has_etag($etag, $if_none_match)
488 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
491 function boolean($key, $def=false)
493 $arg = strtolower($this->trimmed($key));
497 } else if (in_array($arg, array('true', 'yes', '1'))) {
499 } else if (in_array($arg, array('false', 'no', '0'))) {
506 function server_error($msg, $code=500)
508 $action = $this->trimmed('action');
509 common_debug("Server error '$code' on '$action': $msg", __FILE__);
510 common_server_error($msg, $code);
513 function client_error($msg, $code=400)
515 $action = $this->trimmed('action');
516 common_debug("User error '$code' on '$action': $msg", __FILE__);
517 common_user_error($msg, $code);
522 $action = $this->trimmed('action');
524 unset($args['action']);
525 foreach (array_keys($_COOKIE) as $cookie) {
526 unset($args[$cookie]);
528 return common_local_url($action, $args);
531 function nav_menu($menu)
533 $action = $this->trimmed('action');
534 $this->elementStart('ul', array('id' => 'nav_views'));
535 foreach ($menu as $menuaction => $menudesc) {
536 common_menu_item(common_local_url($menuaction,
537 isset($menudesc[2]) ? $menudesc[2] : null),
540 $action == $menuaction);
542 $this->elementEnd('ul');
545 function common_show_header($pagetitle, $callable=null, $data=null, $headercall=null)
548 global $action; /* XXX: kind of cheating here. */
552 $this->elementStart('head');
556 call_user_func($callable, $data);
558 call_user_func($callable);
561 $this->elementEnd('head');
562 $this->elementStart('body', $action);
563 $this->elementStart('div', array('id' => 'wrap'));
564 $this->elementStart('div', array('id' => 'content'));
567 // Added @id to li for some control. We might want to move this to htmloutputter.php
568 function common_menu_item($id=null, $url, $text, $title=null, $is_selected=false)
572 $lattrs['class'] = 'current';
575 $this->elementStart('li', (is_null($id)) ? null : array('id' => $id), $lattrs);
576 $attrs['href'] = $url;
578 $attrs['title'] = $title;
580 $this->element('a', $attrs, $text);
581 $this->elementEnd('li');
584 // Does a little before-after block for next/prev page
586 function pagination($have_before, $have_after, $page, $action, $args=null)
588 if ($have_before || $have_after) {
589 $this->elementStart('div', array('class' => 'pagination'));
590 $this->elementStart('dl', null);
591 $this->element('dt', null, _('Pagination'));
592 $this->elementStart('dd', null);
593 $this->elementStart('ul', array('class' => 'nav'));
597 $pargs = array('page' => $page-1);
598 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
600 $this->elementStart('li', array('class' => 'nav_prev'));
601 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
603 $this->elementEnd('li');
607 $pargs = array('page' => $page+1);
608 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
609 $this->elementStart('li', array('class' => 'nav_next'));
610 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
612 $this->elementEnd('li');
615 if ($have_before || $have_after) {
616 $this->elementEnd('ul');
617 $this->elementEnd('dd');
618 $this->elementEnd('dl');
619 $this->elementEnd('div');