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->elementStart('dl', array('id' => 'site_nav_local_views'));
305 $this->element('dt', null, _('Local views'));
306 $this->elementStart('dd');
307 $this->showLocalNav();
308 $this->elementEnd('dd');
309 $this->elementEnd('dl');
310 $this->showContentBlock();
312 $this->elementEnd('div');
317 function showLocalNav()
319 // does nothing by default
322 function showContentBlock()
324 $this->elementStart('div', array('id' => 'content'));
325 $this->showPageTitle();
326 $this->showPageNoticeBlock();
327 $this->elementStart('div', array('id' => 'content_inner'));
328 // show the actual content (forms, lists, whatever)
329 $this->showContent();
330 $this->elementEnd('div');
331 $this->elementEnd('div');
334 function showPageTitle() {
335 $this->element('h1', NULL, $this->title());
338 function showPageNoticeBlock()
340 $this->elementStart('dl', array('id' => 'page_notice',
341 'class' => 'system_notice'));
342 $this->element('dt', null, _('Page notice'));
343 $this->elementStart('dd');
344 $this->showPageNotice();
345 $this->elementEnd('dd');
346 $this->elementEnd('dl');
349 // SHOULD overload (unless there's not a notice)
351 function showPageNotice()
357 function showContent()
363 $this->elementStart('div', array('id' => 'aside_primary',
364 'class' => 'aside'));
365 $this->showExportData();
366 $this->showSections();
367 $this->elementEnd('div');
370 // MAY overload if there are feeds
372 function showExportData()
374 // is there structure to this?
375 // list of (visible!) feed links
376 // can we reuse list of feeds from showFeeds() ?
381 function showSections() {
382 // for each section, show it
385 function showFooter()
387 $this->elementStart('div', array('id' => 'footer'));
388 $this->showSecondaryNav();
389 $this->showLicenses();
390 $this->elementEnd('div');
393 function showSecondaryNav()
395 $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
396 $this->element('dt', null, _('Secondary site navigation'));
397 $this->elementStart('dd', null);
398 $this->elementStart('ul', array('class' => 'nav'));
399 $this->menuItem(common_local_url('doc', array('title' => 'help')),
401 $this->menuItem(common_local_url('doc', array('title' => 'about')),
403 $this->menuItem(common_local_url('doc', array('title' => 'faq')),
405 $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
407 $this->menuItem(common_local_url('doc', array('title' => 'source')),
409 $this->menuItem(common_local_url('doc', array('title' => 'contact')),
411 $this->elementEnd('ul');
412 $this->elementEnd('dd');
413 $this->elementEnd('dl');
416 function showLicenses()
418 $this->elementStart('dl', array('id' => 'licenses'));
419 $this->showLaconicaLicense();
420 $this->showContentLicense();
421 $this->elementEnd('dl');
424 function showLaconicaLicense()
426 $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
427 $this->elementStart('dd', null);
428 if (common_config('site', 'broughtby')) {
429 $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
431 $instr = _('**%%site.name%%** is a microblogging service. ');
433 $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);
434 $output = common_markup_to_html($instr);
436 $this->elementEnd('dd');
440 function showContentLicense()
442 $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
443 $this->elementStart('dd', array('id' => 'site_content_license_cc'));
444 $this->elementStart('p');
445 $this->element('img', array('id' => 'license_cc',
446 'src' => common_config('license', 'image'),
447 'alt' => common_config('license', 'title')));
448 //TODO: This is dirty: i18n
449 $this->text(_('All '.common_config('site', 'name').' content and data are available under the '));
450 $this->element('a', array('class' => 'license',
451 'rel' => 'external license',
452 'href' => common_config('license', 'url')),
453 common_config('license', 'title'));
454 $this->text(_('license.'));
455 $this->elementEnd('p');
456 $this->elementEnd('dd');
459 // For comparison with If-Last-Modified
460 // If not applicable, return null
462 function lastModified()
472 function isReadOnly()
477 function arg($key, $def=null)
479 if (array_key_exists($key, $this->args)) {
480 return $this->args[$key];
486 function trimmed($key, $def=null)
488 $arg = $this->arg($key, $def);
489 return (is_string($arg)) ? trim($arg) : $arg;
492 // Note: argarray ignored, since it's now passed in in prepare()
494 function handle($argarray=null)
497 $lm = $this->lastModified();
498 $etag = $this->etag();
501 header('ETag: ' . $etag);
505 header('Last-Modified: ' . date(DATE_RFC1123, $lm));
506 $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
507 if ($if_modified_since) {
508 $ims = strtotime($if_modified_since);
511 $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
512 header('HTTP/1.1 304 Not Modified');
513 // Better way to do this?
521 function _hasEtag($etag, $if_none_match)
523 return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
526 function boolean($key, $def=false)
528 $arg = strtolower($this->trimmed($key));
532 } else if (in_array($arg, array('true', 'yes', '1'))) {
534 } else if (in_array($arg, array('false', 'no', '0'))) {
541 function serverError($msg, $code=500)
543 $action = $this->trimmed('action');
544 common_debug("Server error '$code' on '$action': $msg", __FILE__);
545 common_server_error($msg, $code);
548 function clientError($msg, $code=400)
550 $action = $this->trimmed('action');
551 common_debug("User error '$code' on '$action': $msg", __FILE__);
552 common_user_error($msg, $code);
557 $action = $this->trimmed('action');
559 unset($args['action']);
560 foreach (array_keys($_COOKIE) as $cookie) {
561 unset($args[$cookie]);
563 return common_local_url($action, $args);
566 // Added @id to li for some control.
567 // XXX: We might want to move this to htmloutputter.php
569 function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
573 $lattrs['class'] = 'current';
576 (is_null($id)) ? $lattrs : $lattrs['id'] = $id;
578 $this->elementStart('li', $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');