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 if (common_logged_in()) {
212 $this->showNoticeForm();
214 $this->showAnonymousMessage();
216 $this->elementEnd('div');
221 $this->elementStart('address', array('id' => 'site_contact',
222 'class' => 'vcard'));
223 $this->elementStart('a', array('class' => 'url home bookmark',
224 'href' => common_local_url('public')));
225 if ((isset($config['site']['logo']) && is_string($config['site']['logo']) && (strlen($config['site']['logo']) > 0))
226 || file_exists(theme_file('logo.png')))
228 $this->element('img', array('class' => 'logo photo',
229 'src' => isset($config['site']['logo']) ?
230 ($config['site']['logo']) : theme_path('logo.png'),
231 'alt' => $config['site']['name']));
233 $this->element('span', array('class' => 'fn org'), $config['site']['name']);
234 $this->elementEnd('a');
235 $this->elementEnd('address');
238 function showPrimaryNav()
240 $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
241 $this->element('dt', null, _('Primary site navigation'));
242 $this->elementStart('dd');
243 $user = common_current_user();
244 $this->elementStart('ul', array('class' => 'nav'));
246 $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
249 $this->menuItem(common_local_url('peoplesearch'), _('Search'));
251 $this->menuItem(common_local_url('profilesettings'),
253 $this->menuItem(common_local_url('invite'),
255 $this->menuItem(common_local_url('logout'),
258 $this->menuItem(common_local_url('login'), _('Login'));
259 if (!common_config('site', 'closed')) {
260 $this->menuItem(common_local_url('register'), _('Register'));
262 $this->menuItem(common_local_url('openidlogin'), _('OpenID'));
264 $this->menuItem(common_local_url('doc', array('title' => 'help')),
266 $this->elementEnd('ul');
267 $this->elementEnd('dd');
268 $this->elementEnd('dl');
271 // Revist. Should probably do an hAtom pattern here
272 function showSiteNotice()
274 $text = common_config('site', 'notice');
276 $this->elementStart('dl', array('id' => 'site_notice',
277 'class' => 'system_notice'));
278 $this->element('dt', null, _('Site notice'));
279 $this->element('dd', null, $text);
280 $this->elementEnd('dl');
284 // MAY overload if no notice form needed... or direct message box????
286 function showNoticeForm()
288 $notice_form = new NoticeForm($this);
289 $notice_form->show();
292 function showAnonymousMessage()
294 // needs to be defined by the class
299 $this->elementStart('div', array('id' => 'core'));
300 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
301 $this->element('dt', null, _('Local views'));
302 $this->elementStart('dd');
303 $this->showLocalNav();
304 $this->elementEnd('dd');
305 $this->elementEnd('dl');
306 $this->showContentBlock();
308 $this->elementEnd('div');
313 function showLocalNav()
315 // does nothing by default
318 function showContentBlock()
320 $this->elementStart('div', array('id' => 'content'));
321 $this->showPageTitle();
322 $this->showPageNoticeBlock();
323 $this->elementStart('div', array('id' => 'content_inner'));
324 // show the actual content (forms, lists, whatever)
325 $this->showContent();
326 $this->elementEnd('div');
327 $this->elementEnd('div');
330 function showPageTitle() {
331 $this->element('h1', NULL, $this->title());
334 function showPageNoticeBlock()
336 $this->elementStart('dl', array('id' => 'page_notice',
337 'class' => 'system_notice'));
338 $this->element('dt', null, _('Page notice'));
339 $this->elementStart('dd');
340 $this->showPageNotice();
341 $this->elementEnd('dd');
342 $this->elementEnd('dl');
345 // SHOULD overload (unless there's not a notice)
347 function showPageNotice()
353 function showContent()
359 $this->elementStart('div', array('id' => 'aside_primary',
360 'class' => 'aside'));
361 $this->showExportData();
362 $this->showSections();
363 $this->elementEnd('div');
366 // MAY overload if there are feeds
368 function showExportData()
370 // is there structure to this?
371 // list of (visible!) feed links
372 // can we reuse list of feeds from showFeeds() ?
377 function showSections() {
378 // for each section, show it
381 function showFooter()
383 $this->elementStart('div', array('id' => 'footer'));
384 $this->showSecondaryNav();
385 $this->showLicenses();
386 $this->elementEnd('div');
389 function showSecondaryNav()
391 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
392 $this->element('dt', null, _('Secondary site navigation'));
393 $this->elementStart('dd', null);
394 $this->elementStart('ul', array('class' => 'nav'));
395 $this->menuItem(common_local_url('doc', array('title' => 'help')),
397 $this->menuItem(common_local_url('doc', array('title' => 'about')),
399 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
401 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
403 $this->menuItem(common_local_url('doc', array('title' => 'source')),
405 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
407 $this->elementEnd('ul');
408 $this->elementEnd('dd');
409 $this->elementEnd('dl');
412 function showLicenses()
414 $this->elementStart('dl', array('id' => 'licenses'));
415 $this->showLaconicaLicense();
416 $this->showContentLicense();
417 $this->elementEnd('dl');
420 function showLaconicaLicense()
422 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
423 $this->elementStart('dd', null);
424 if (common_config('site', 'broughtby')) {
425 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
427 $instr = _('**%%site.name%%** is a microblogging service. ');
429 $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);
430 $output = common_markup_to_html($instr);
432 $this->elementEnd('dd');
436 function showContentLicense()
438 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
439 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
440 $this->elementStart('p');
441 $this->text(_('Unless otherwise specified, contents of this site are copyright by the contributors and available under the '));
442 $this->element('a', array('class' => 'license',
443 'rel' => 'external license',
444 'href' => common_config('license', 'url')),
445 common_config('license', 'title'));
446 $this->text(_('. Contributors should be attributed by full name or nickname.'));
447 $this->elementEnd('p');
448 $this->element('img', array('id' => 'license_cc',
449 'src' => common_config('license', 'image'),
450 'alt' => common_config('license', 'title')));
451 $this->elementEnd('dd');
454 // For comparison with If-Last-Modified
455 // If not applicable, return null
457 function last_modified()
467 function isReadOnly()
472 function arg($key, $def=null)
474 if (array_key_exists($key, $this->args)) {
475 return $this->args[$key];
481 function trimmed($key, $def=null)
483 $arg = $this->arg($key, $def);
484 return (is_string($arg)) ? trim($arg) : $arg;
487 // Note: argarray ignored, since it's now passed in in prepare()
489 function handle($argarray=null)
492 $lm = $this->last_modified();
493 $etag = $this->etag();
496 header('ETag: ' . $etag);
500 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
501 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
502 if ($if_modified_since) {
503 $ims = strtotime($if_modified_since);
506 $this->_has_etag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
507 header('HTTP/1.1 304 Not Modified');
508 // Better way to do this?
516 function _has_etag($etag, $if_none_match)
518 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
521 function boolean($key, $def=false)
523 $arg = strtolower($this->trimmed($key));
527 } else if (in_array($arg, array('true', 'yes', '1'))) {
529 } else if (in_array($arg, array('false', 'no', '0'))) {
536 function serverError($msg, $code=500)
538 $action = $this->trimmed('action');
539 common_debug("Server error '$code' on '$action': $msg", __FILE__);
540 common_server_error($msg, $code);
543 function clientError($msg, $code=400)
545 $action = $this->trimmed('action');
546 common_debug("User error '$code' on '$action': $msg", __FILE__);
547 common_user_error($msg, $code);
552 $action = $this->trimmed('action');
554 unset($args['action']);
555 foreach (array_keys($_COOKIE) as $cookie) {
556 unset($args[$cookie]);
558 return common_local_url($action, $args);
561 // Added @id to li for some control.
562 // XXX: We might want to move this to htmloutputter.php
564 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
568 $lattrs['class'] = 'current';
571 $this->elementStart('li', (is_null($id)) ? null : array('id' => $id), $lattrs);
572 $attrs['href'] = $url;
574 $attrs['title'] = $title;
576 $this->element('a', $attrs, $text);
577 $this->elementEnd('li');
580 // Does a little before-after block for next/prev page
582 function pagination($have_before, $have_after, $page, $action, $args=null)
584 if ($have_before || $have_after) {
585 $this->elementStart('div', array('class' => 'pagination'));
586 $this->elementStart('dl', null);
587 $this->element('dt', null, _('Pagination'));
588 $this->elementStart('dd', null);
589 $this->elementStart('ul', array('class' => 'nav'));
593 $pargs = array('page' => $page-1);
594 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
596 $this->elementStart('li', array('class' => 'nav_prev'));
597 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
599 $this->elementEnd('li');
603 $pargs = array('page' => $page+1);
604 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
605 $this->elementStart('li', array('class' => 'nav_next'));
606 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
608 $this->elementEnd('li');
611 if ($have_before || $have_after) {
612 $this->elementEnd('ul');
613 $this->elementEnd('dd');
614 $this->elementEnd('dl');
615 $this->elementEnd('div');