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 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
138 'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
139 foreach (array(6,7) as $ver) {
140 if (file_exists(theme_file('ie'.$ver.'.css'))) {
141 // Yes, IE people should be put in jail.
142 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
143 'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
148 function showScripts()
150 $this->element('script', array('type' => 'text/javascript',
151 'src' => common_path('js/jquery.min.js')),
153 $this->element('script', array('type' => 'text/javascript',
154 'src' => common_path('js/jquery.form.js')),
156 $this->element('script', array('type' => 'text/javascript',
157 'src' => common_path('js/xbImportNode.js')),
159 $this->element('script', array('type' => 'text/javascript',
160 'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
164 function showOpenSearch()
166 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
167 'href' => common_local_url('opensearch', array('type' => 'people')),
168 'title' => common_config('site', 'name').' People Search'));
170 $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
171 'href' => common_local_url('opensearch', array('type' => 'notice')),
172 'title' => common_config('site', 'name').' Notice Search'));
179 // does nothing by default
184 function showDescription()
186 // does nothing by default
193 // does nothing by default
198 $this->elementStart('body', array('id' => $this->trimmed('action')));
199 $this->elementStart('div', 'wrap');
203 $this->elementEnd('div');
204 $this->elementEnd('body');
207 function showHeader()
209 $this->elementStart('div', array('id' => 'header'));
211 $this->showPrimaryNav();
212 $this->showSiteNotice();
213 if (common_logged_in()) {
214 $this->showNoticeForm();
216 $this->showAnonymousMessage();
218 $this->elementEnd('div');
223 $this->elementStart('address', array('id' => 'site_contact',
224 'class' => 'vcard'));
225 $this->elementStart('a', array('class' => 'url home bookmark',
226 'href' => common_local_url('public')));
227 if (common_config('site', 'logo') || file_exists(theme_file('logo.png')))
229 $this->element('img', array('class' => 'logo photo',
230 'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
231 'alt' => common_config('site', 'name')));
233 $this->element('span', array('class' => 'fn org'), common_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)),
247 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
249 $this->menuItem(common_local_url('peoplesearch'),
250 _('Search'), _('Search for people or text'), false, 'nav_search');
252 $this->menuItem(common_local_url('profilesettings'),
253 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
254 $this->menuItem(common_local_url('imsettings'),
255 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
256 $this->menuItem(common_local_url('logout'),
257 _('Logout'), _('Logout from the site'), false, 'nav_logout');
259 $this->menuItem(common_local_url('login'),
260 _('Login'), _('Login to the site'), false, 'nav_login');
261 if (!common_config('site', 'closed')) {
262 $this->menuItem(common_local_url('register'),
263 _('Register'), _('Create an account'), false, 'nav_register');
265 $this->menuItem(common_local_url('openidlogin'),
266 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
268 $this->menuItem(common_local_url('doc', array('title' => 'help')),
269 _('Help'), _('Help me!'), false, 'nav_help');
270 $this->elementEnd('ul');
271 $this->elementEnd('dd');
272 $this->elementEnd('dl');
275 // Revist. Should probably do an hAtom pattern here
276 function showSiteNotice()
278 $text = common_config('site', 'notice');
280 $this->elementStart('dl', array('id' => 'site_notice',
281 'class' => 'system_notice'));
282 $this->element('dt', null, _('Site notice'));
283 $this->element('dd', null, $text);
284 $this->elementEnd('dl');
288 // MAY overload if no notice form needed... or direct message box????
290 function showNoticeForm()
292 $notice_form = new NoticeForm($this);
293 $notice_form->show();
296 function showAnonymousMessage()
298 // needs to be defined by the class
303 $this->elementStart('div', array('id' => 'core'));
304 $this->showLocalNavBlock();
305 $this->elementEnd('dl');
306 $this->showContentBlock();
308 $this->elementEnd('div');
311 function showLocalNavBlock()
313 $this->elementStart('dl', array('id' => 'site_nav_local_views'));
314 $this->element('dt', null, _('Local views'));
315 $this->elementStart('dd');
316 $this->showLocalNav();
317 $this->elementEnd('dd');
322 function showLocalNav()
324 // does nothing by default
327 function showContentBlock()
329 $this->elementStart('div', array('id' => 'content'));
330 $this->showPageTitle();
331 $this->showPageNoticeBlock();
332 $this->elementStart('div', array('id' => 'content_inner'));
333 // show the actual content (forms, lists, whatever)
334 $this->showContent();
335 $this->elementEnd('div');
336 $this->elementEnd('div');
339 function showPageTitle() {
340 $this->element('h1', NULL, $this->title());
343 function showPageNoticeBlock()
345 $this->elementStart('dl', array('id' => 'page_notice',
346 'class' => 'system_notice'));
347 $this->element('dt', null, _('Page notice'));
348 $this->elementStart('dd');
349 $this->showPageNotice();
350 $this->elementEnd('dd');
351 $this->elementEnd('dl');
354 // SHOULD overload (unless there's not a notice)
356 function showPageNotice()
362 function showContent()
368 $this->elementStart('div', array('id' => 'aside_primary',
369 'class' => 'aside'));
370 $this->showExportData();
371 $this->showSections();
372 $this->elementEnd('div');
375 // MAY overload if there are feeds
377 function showExportData()
379 // is there structure to this?
380 // list of (visible!) feed links
381 // can we reuse list of feeds from showFeeds() ?
386 function showSections() {
387 // for each section, show it
390 function showFooter()
392 $this->elementStart('div', array('id' => 'footer'));
393 $this->showSecondaryNav();
394 $this->showLicenses();
395 $this->elementEnd('div');
398 function showSecondaryNav()
400 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
401 $this->element('dt', null, _('Secondary site navigation'));
402 $this->elementStart('dd', null);
403 $this->elementStart('ul', array('class' => 'nav'));
404 $this->menuItem(common_local_url('doc', array('title' => 'help')),
406 $this->menuItem(common_local_url('doc', array('title' => 'about')),
408 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
410 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
412 $this->menuItem(common_local_url('doc', array('title' => 'source')),
414 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
416 $this->elementEnd('ul');
417 $this->elementEnd('dd');
418 $this->elementEnd('dl');
421 function showLicenses()
423 $this->elementStart('dl', array('id' => 'licenses'));
424 $this->showLaconicaLicense();
425 $this->showContentLicense();
426 $this->elementEnd('dl');
429 function showLaconicaLicense()
431 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
432 $this->elementStart('dd', null);
433 if (common_config('site', 'broughtby')) {
434 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
436 $instr = _('**%%site.name%%** is a microblogging service. ');
438 $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);
439 $output = common_markup_to_html($instr);
441 $this->elementEnd('dd');
445 function showContentLicense()
447 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
448 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
449 $this->elementStart('p');
450 $this->element('img', array('id' => 'license_cc',
451 'src' => common_config('license', 'image'),
452 'alt' => common_config('license', 'title')));
453 //TODO: This is dirty: i18n
454 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
455 $this->element('a', array('class' => 'license',
456 'rel' => 'external license',
457 'href' => common_config('license', 'url')),
458 common_config('license', 'title'));
459 $this->text(_('license.'));
460 $this->elementEnd('p');
461 $this->elementEnd('dd');
464 // For comparison with If-Last-Modified
465 // If not applicable, return null
467 function lastModified()
477 function isReadOnly()
482 function arg($key, $def=null)
484 if (array_key_exists($key, $this->args)) {
485 return $this->args[$key];
491 function trimmed($key, $def=null)
493 $arg = $this->arg($key, $def);
494 return (is_string($arg)) ? trim($arg) : $arg;
497 // Note: argarray ignored, since it's now passed in in prepare()
499 function handle($argarray=null)
502 $lm = $this->lastModified();
503 $etag = $this->etag();
506 header('ETag: ' . $etag);
510 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
511 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
512 if ($if_modified_since) {
513 $ims = strtotime($if_modified_since);
516 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
517 header('HTTP/1.1 304 Not Modified');
518 // Better way to do this?
526 function _hasEtag($etag, $if_none_match)
528 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
531 function boolean($key, $def=false)
533 $arg = strtolower($this->trimmed($key));
537 } else if (in_array($arg, array('true', 'yes', '1'))) {
539 } else if (in_array($arg, array('false', 'no', '0'))) {
546 function serverError($msg, $code=500)
548 $action = $this->trimmed('action');
549 common_debug("Server error '$code' on '$action': $msg", __FILE__);
550 common_server_error($msg, $code);
553 function clientError($msg, $code=400)
555 $action = $this->trimmed('action');
556 common_debug("User error '$code' on '$action': $msg", __FILE__);
557 common_user_error($msg, $code);
562 $action = $this->trimmed('action');
564 unset($args['action']);
565 foreach (array_keys($_COOKIE) as $cookie) {
566 unset($args[$cookie]);
568 return common_local_url($action, $args);
571 // Added @id to li for some control.
572 // XXX: We might want to move this to htmloutputter.php
574 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
578 $lattrs['class'] = 'current';
581 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
583 $this->elementStart('li', $lattrs);
584 $attrs['href'] = $url;
586 $attrs['title'] = $title;
588 $this->element('a', $attrs, $text);
589 $this->elementEnd('li');
592 // Does a little before-after block for next/prev page
594 function pagination($have_before, $have_after, $page, $action, $args=null)
596 if ($have_before || $have_after) {
597 $this->elementStart('div', array('class' => 'pagination'));
598 $this->elementStart('dl', null);
599 $this->element('dt', null, _('Pagination'));
600 $this->elementStart('dd', null);
601 $this->elementStart('ul', array('class' => 'nav'));
605 $pargs = array('page' => $page-1);
606 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
608 $this->elementStart('li', array('class' => 'nav_prev'));
609 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
611 $this->elementEnd('li');
615 $pargs = array('page' => $page+1);
616 $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
617 $this->elementStart('li', array('class' => 'nav_next'));
618 $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
620 $this->elementEnd('li');
623 if ($have_before || $have_after) {
624 $this->elementEnd('ul');
625 $this->elementEnd('dd');
626 $this->elementEnd('dl');
627 $this->elementEnd('div');