]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/action.php
is_readonly() -> isReadOnly()
[quix0rs-gnu-social.git] / lib / action.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Base class for all actions (~views)
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Action
23  * @package   Laconica
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/
29  */
30
31 if (!defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/noticeform.php';
36 require_once INSTALLDIR.'/lib/htmloutputter.php';
37
38 /**
39  * Base class for all actions
40  *
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.
43  *
44  * Actions are responsible for extracting and validating parameters; using
45  * model classes to read and write to the database; and doing ouput.
46  *
47  * @category Output
48  * @package  Laconica
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/
53  *
54  * @see      HTMLOutputter
55  */
56
57 class Action extends HTMLOutputter // lawsuit
58 {
59     var $args;
60
61     /**
62      * Constructor
63      *
64      * Just wraps the HTMLOutputter constructor.
65      *
66      * @param string  $output URI to output to, default = stdout
67      * @param boolean $indent Whether to indent output, default true
68      *
69      * @see XMLOutputter::__construct
70      * @see HTMLOutputter::__construct
71      */
72
73     function __construct($output='php://output', $indent=true)
74     {
75         parent::__construct($output, $indent);
76     }
77
78     // For initializing members of the class
79
80     function prepare($argarray)
81     {
82         $this->args =& common_copy_args($argarray);
83         return true;
84     }
85
86     function showPage()
87     {
88         $this->startHTML();
89         $this->showHead();
90         $this->showBody();
91         $this->endHTML();
92     }
93
94     function showHead()
95     {
96         // XXX: attributes (profile?)
97         $this->elementStart('head');
98         $this->showTitle();
99         $this->showStylesheets();
100         $this->showScripts();
101         $this->showOpenSearch();
102         $this->showFeeds();
103         $this->showDescription();
104         $this->extraHead();
105         $this->elementEnd('head');
106     }
107
108     function showTitle()
109     {
110         $this->element('title', null,
111                        sprintf(_("%s - %s"),
112                                $this->title(),
113                                common_config('site', 'name')));
114     }
115
116     // SHOULD overload
117
118     function title()
119     {
120         return _("Untitled page");
121     }
122
123     function showStylesheets()
124     {
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]');
142             }
143         }
144     }
145
146     function showScripts()
147     {
148         $this->element('script', array('type' => 'text/javascript',
149                                        'src' => common_path('js/jquery.min.js')),
150                        ' ');
151         $this->element('script', array('type' => 'text/javascript',
152                                        'src' => common_path('js/jquery.form.js')),
153                        ' ');
154         $this->element('script', array('type' => 'text/javascript',
155                                        'src' => common_path('js/xbImportNode.js')),
156                        ' ');
157         $this->element('script', array('type' => 'text/javascript',
158                                        'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
159                        ' ');
160     }
161
162     function showOpenSearch()
163     {
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'));
167
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'));
171     }
172
173     // MAY overload
174
175     function showFeeds()
176     {
177         // does nothing by default
178     }
179
180     // SHOULD overload
181
182     function showDescription()
183     {
184         // does nothing by default
185     }
186
187     // MAY overload
188
189     function extraHead()
190     {
191         // does nothing by default
192     }
193
194     function showBody()
195     {
196         $this->elementStart('body');
197         $this->elementStart('div', 'wrap');
198         $this->showHeader();
199         $this->showCore();
200         $this->showFooter();
201         $this->elementEnd('div', 'wrap');
202         $this->elementEnd('body');
203     }
204
205     function showHeader()
206     {
207         $this->elementStart('div', array('id' => 'header'));
208         $this->showLogo();
209         $this->showPrimaryNav();
210         $this->showSiteNotice();
211         if (common_logged_in()) {
212             $this->showNoticeForm();
213         } else {
214             $this->showAnonymousMessage();
215         }
216         $this->elementEnd('div');
217     }
218
219     function showLogo()
220     {
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')))
227         {
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']));
232         }
233         $this->element('span', array('class' => 'fn org'), $config['site']['name']);
234         $this->elementEnd('a');
235         $this->elementEnd('address');
236     }
237
238     function showPrimaryNav()
239     {
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'));
245         if ($user) {
246             $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
247                              _('Home'));
248         }
249         $this->menuItem(common_local_url('peoplesearch'), _('Search'));
250         if ($user) {
251             $this->menuItem(common_local_url('profilesettings'),
252                              _('Settings'));
253             $this->menuItem(common_local_url('invite'),
254                              _('Invite'));
255             $this->menuItem(common_local_url('logout'),
256                              _('Logout'));
257         } else {
258             $this->menuItem(common_local_url('login'), _('Login'));
259             if (!common_config('site', 'closed')) {
260                 $this->menuItem(common_local_url('register'), _('Register'));
261             }
262             $this->menuItem(common_local_url('openidlogin'), _('OpenID'));
263         }
264         $this->menuItem(common_local_url('doc', array('title' => 'help')),
265                          _('Help'));
266         $this->elementEnd('ul');
267         $this->elementEnd('dd');        
268         $this->elementEnd('dl');
269     }
270
271     // Revist. Should probably do an hAtom pattern here
272     function showSiteNotice()
273     {
274         $text = common_config('site', 'notice');
275         if ($text) {
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');
281         }
282     }
283     
284     // MAY overload if no notice form needed... or direct message box????
285
286     function showNoticeForm()
287     {
288         $notice_form = new NoticeForm($this);
289         $notice_form->show();
290     }
291
292     function showAnonymousMessage()
293     {
294         // needs to be defined by the class
295     }
296     
297     function showCore()
298     {
299         $this->elementStart('div', array('id' => 'core'));
300         $this->showLocalNav();
301         $this->showContentBlock();
302         $this->showAside();
303         $this->elementEnd('div');
304     }
305
306     // SHOULD overload
307
308     function showLocalNav()
309     {
310         // does nothing by default
311     }
312
313     function showContentBlock()
314     {
315         $this->elementStart('div', array('id' => 'content'));
316         $this->showPageTitle();
317         $this->showPageNoticeBlock();
318         $this->elementStart('div', array('id' => 'content_inner'));
319         // show the actual content (forms, lists, whatever)
320         $this->showContent();
321         $this->elementEnd('div');
322         $this->elementEnd('div');
323     }
324
325     function showPageTitle() {
326         $this->element('h1', NULL, $this->title());
327     }
328
329     function showPageNoticeBlock()
330     {
331         $this->elementStart('dl', array('id' => 'page_notice',
332                                         'class' => 'system_notice'));
333         $this->element('dt', null, _('Page notice'));
334         $this->elementStart('dd', null);
335         $this->showPageNotice();
336         $this->elementEnd('dd');
337         $this->elementEnd('dl');
338         }
339     }
340
341     // SHOULD overload (unless there's not a notice)
342
343     function showPageNotice()
344     {
345     }
346     
347     // MUST overload
348
349     function showContent()
350     {
351     }
352
353     function showAside()
354     {
355         $this->elementStart('div', array('id' => 'aside_primary',
356                                          'class' => 'aside'));
357         $this->showExportData();
358         $this->showSections();
359         $this->elementEnd('div');
360     }
361
362     // MAY overload if there are feeds
363
364     function showExportData()
365     {
366         // is there structure to this?
367         // list of (visible!) feed links
368         // can we reuse list of feeds from showFeeds() ?
369     }
370
371     // SHOULD overload
372
373     function showSections() {
374         // for each section, show it
375     }
376
377     function showFooter()
378     {
379         $this->elementStart('div', array('id' => 'footer'));
380         $this->showSecondaryNav();
381         $this->showLicenses();
382         $this->elementEnd('div');
383     }
384
385     function showSecondaryNav()
386     {
387         $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
388         $this->element('dt', null, _('Secondary site navigation'));
389         $this->elementStart('dd', null);
390         $this->elementStart('ul', array('class' => 'nav'));
391         $this->menuItem(common_local_url('doc', array('title' => 'help')),
392                          _('Help'));
393         $this->menuItem(common_local_url('doc', array('title' => 'about')),
394                          _('About'));
395         $this->menuItem(common_local_url('doc', array('title' => 'faq')),
396                          _('FAQ'));
397         $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
398                          _('Privacy'));
399         $this->menuItem(common_local_url('doc', array('title' => 'source')),
400                          _('Source'));
401         $this->menuItem(common_local_url('doc', array('title' => 'contact')),
402                          _('Contact'));
403         $this->elementEnd('ul');
404         $this->elementEnd('dd');
405         $this->elementEnd('dl');
406     }
407
408     function showLicenses()
409     {
410         $this->elementStart('dl', array('id' => 'licenses'));
411         $this->showLaconicaLicense();
412         $this->showContentLicense();
413         $this->elementEnd('dl');
414     }
415
416     function showLaconicaLicense()
417     {
418         $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
419         $this->elementStart('dd', null);
420         if (common_config('site', 'broughtby')) {
421             $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
422         } else {
423             $instr = _('**%%site.name%%** is a microblogging service. ');
424         }
425         $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);
426         $output = common_markup_to_html($instr);
427         $this->raw($output);
428         $this->elementEnd('dd');
429         // do it
430     }
431
432     function showContentLicense()
433     {
434         $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
435         $this->elementStart('dd', array('id' => 'site_content_license_cc'));
436         $this->elementStart('p');
437         $this->text(_('Unless otherwise specified, contents of this site are copyright by the contributors and available under the '));
438         $this->element('a', array('class' => 'license',
439                                   'rel' => 'external license',
440                                   'href' => common_config('license', 'url')),
441                        common_config('license', 'title'));
442         $this->text(_('. Contributors should be attributed by full name or nickname.'));
443         $this->elementEnd('p');
444         $this->element('img', array('id' => 'license_cc',
445                                     'src' => common_config('license', 'image'),
446                                     'alt' => common_config('license', 'title')));
447         $this->elementEnd('dd');
448     }
449
450     // For comparison with If-Last-Modified
451     // If not applicable, return null
452
453     function last_modified()
454     {
455         return null;
456     }
457
458     function etag()
459     {
460         return null;
461     }
462
463     function isReadOnly()
464     {
465         return false;
466     }
467
468     function arg($key, $def=null)
469     {
470         if (array_key_exists($key, $this->args)) {
471             return $this->args[$key];
472         } else {
473             return $def;
474         }
475     }
476
477     function trimmed($key, $def=null)
478     {
479         $arg = $this->arg($key, $def);
480         return (is_string($arg)) ? trim($arg) : $arg;
481     }
482
483     // Note: argarray ignored, since it's now passed in in prepare()
484
485     function handle($argarray=null)
486     {
487
488         $lm = $this->last_modified();
489         $etag = $this->etag();
490
491         if ($etag) {
492             header('ETag: ' . $etag);
493         }
494
495         if ($lm) {
496             header('Last-Modified: ' . date(DATE_RFC1123, $lm));
497             $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
498             if ($if_modified_since) {
499                 $ims = strtotime($if_modified_since);
500                 if ($lm <= $ims) {
501                     if (!$etag ||
502                         $this->_has_etag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
503                         header('HTTP/1.1 304 Not Modified');
504                         // Better way to do this?
505                         exit(0);
506                     }
507                 }
508             }
509         }
510     }
511
512     function _has_etag($etag, $if_none_match)
513     {
514         return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
515     }
516
517     function boolean($key, $def=false)
518     {
519         $arg = strtolower($this->trimmed($key));
520
521         if (is_null($arg)) {
522             return $def;
523         } else if (in_array($arg, array('true', 'yes', '1'))) {
524             return true;
525         } else if (in_array($arg, array('false', 'no', '0'))) {
526             return false;
527         } else {
528             return $def;
529         }
530     }
531
532     function serverError($msg, $code=500)
533     {
534         $action = $this->trimmed('action');
535         common_debug("Server error '$code' on '$action': $msg", __FILE__);
536         common_server_error($msg, $code);
537     }
538
539     function clientError($msg, $code=400)
540     {
541         $action = $this->trimmed('action');
542         common_debug("User error '$code' on '$action': $msg", __FILE__);
543         common_user_error($msg, $code);
544     }
545
546     function self_url()
547     {
548         $action = $this->trimmed('action');
549         $args = $this->args;
550         unset($args['action']);
551         foreach (array_keys($_COOKIE) as $cookie) {
552             unset($args[$cookie]);
553         }
554         return common_local_url($action, $args);
555     }
556
557     function nav_menu($menu)
558     {
559         $action = $this->trimmed('action');
560         $this->elementStart('ul', array('id' => 'nav_views'));
561         foreach ($menu as $menuaction => $menudesc) {
562             $this->menuItem(common_local_url($menuaction,
563                                               isset($menudesc[2]) ? $menudesc[2] : null),
564                              $menudesc[0],
565                              $menudesc[1],
566                              $action == $menuaction);
567         }
568         $this->elementEnd('ul');
569     }
570
571     function common_show_header($pagetitle, $callable=null, $data=null, $headercall=null)
572     {
573         global $config, $xw;
574         global $action; /* XXX: kind of cheating here. */
575
576         common_start_html();
577
578         $this->elementStart('head');
579
580         if ($callable) {
581             if ($data) {
582                 call_user_func($callable, $data);
583             } else {
584                 call_user_func($callable);
585             }
586         }
587         $this->elementEnd('head');
588         $this->elementStart('body', $action);
589         $this->elementStart('div', array('id' => 'wrap'));
590         $this->elementStart('div', array('id' => 'content'));
591     }
592
593     // Added @id to li for some control.
594     // XXX: We might want to move this to htmloutputter.php
595
596     function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
597     {
598         $lattrs = array();
599         if ($is_selected) {
600             $lattrs['class'] = 'current';
601         }
602
603         $this->elementStart('li', (is_null($id)) ? null : array('id' => $id),  $lattrs);
604         $attrs['href'] = $url;
605         if ($title) {
606             $attrs['title'] = $title;
607         }
608         $this->element('a', $attrs, $text);
609         $this->elementEnd('li');
610     }
611
612     // Does a little before-after block for next/prev page
613
614     function pagination($have_before, $have_after, $page, $action, $args=null)
615     {
616         if ($have_before || $have_after) {
617             $this->elementStart('div', array('class' => 'pagination'));
618             $this->elementStart('dl', null);
619             $this->element('dt', null, _('Pagination'));
620             $this->elementStart('dd', null);
621             $this->elementStart('ul', array('class' => 'nav'));
622         }
623
624         if ($have_before) {
625             $pargs = array('page' => $page-1);
626             $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
627
628             $this->elementStart('li', array('class' => 'nav_prev'));
629             $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
630                            _('After'));
631             $this->elementEnd('li');
632         }
633
634         if ($have_after) {
635             $pargs = array('page' => $page+1);
636             $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
637             $this->elementStart('li', array('class' => 'nav_next'));
638             $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
639                            _('Before'));
640             $this->elementEnd('li');
641         }
642
643         if ($have_before || $have_after) {
644             $this->elementEnd('ul');
645             $this->elementEnd('dd');
646             $this->elementEnd('dl');
647             $this->elementEnd('div');
648         }
649     }
650 }