]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/action.php
94be04b52018330854d540079ff5c90c20b38c50
[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/display.css', null) . '?version=' . LACONICA_VERSION,
132                                      'media' => 'screen, projection, tv'));
133         foreach (array(6,7) as $ver) {
134             if (file_exists(theme_file('ie'.$ver.'.css'))) {
135                 // Yes, IE people should be put in jail.
136                 $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
137                                'href="'.theme_path('ie'.$ver.'.css').'?version='.LACONICA_VERSION.'" /><![endif]');
138             }
139         }
140     }
141
142     function showScripts()
143     {
144         $this->element('script', array('type' => 'text/javascript',
145                                        'src' => common_path('js/jquery.min.js')),
146                        ' ');
147         $this->element('script', array('type' => 'text/javascript',
148                                        'src' => common_path('js/jquery.form.js')),
149                        ' ');
150         $this->element('script', array('type' => 'text/javascript',
151                                        'src' => common_path('js/xbImportNode.js')),
152                        ' ');
153         $this->element('script', array('type' => 'text/javascript',
154                                        'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
155                        ' ');
156     }
157
158     function showOpenSearch()
159     {
160         $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
161                                      'href' =>  common_local_url('opensearch', array('type' => 'people')),
162                                      'title' => common_config('site', 'name').' People Search'));
163
164         $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
165                                      'href' =>  common_local_url('opensearch', array('type' => 'notice')),
166                                      'title' => common_config('site', 'name').' Notice Search'));
167     }
168
169     // MAY overload
170
171     function showFeeds()
172     {
173         // does nothing by default
174     }
175
176     // SHOULD overload
177
178     function showDescription()
179     {
180         // does nothing by default
181     }
182
183     // MAY overload
184
185     function extraHead()
186     {
187         // does nothing by default
188     }
189
190     function showBody()
191     {
192         $this->elementStart('body', array('id' => $this->trimmed('action')));
193         $this->elementStart('div', 'wrap');
194         $this->showHeader();
195         $this->showCore();
196         $this->showFooter();
197         $this->elementEnd('div');
198         $this->elementEnd('body');
199     }
200
201     function showHeader()
202     {
203         $this->elementStart('div', array('id' => 'header'));
204         $this->showLogo();
205         $this->showPrimaryNav();
206         $this->showSiteNotice();
207         if (common_logged_in()) {
208             $this->showNoticeForm();
209         } else {
210             $this->showAnonymousMessage();
211         }
212         $this->elementEnd('div');
213     }
214
215     function showLogo()
216     {
217         $this->elementStart('address', array('id' => 'site_contact',
218                                               'class' => 'vcard'));
219         $this->elementStart('a', array('class' => 'url home bookmark',
220                                         'href' => common_local_url('public')));
221         if (common_config('site', 'logo') || file_exists(theme_file('logo.png')))
222         {
223             $this->element('img', array('class' => 'logo photo',
224                                         'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
225                                         'alt' => common_config('site', 'name')));
226         }
227         $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
228         $this->elementEnd('a');
229         $this->elementEnd('address');
230     }
231
232     function showPrimaryNav()
233     {
234         $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
235         $this->element('dt', null, _('Primary site navigation'));
236         $this->elementStart('dd');
237         $user = common_current_user();
238         $this->elementStart('ul', array('class' => 'nav'));
239         if ($user) {
240             $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
241                              _('Home'));
242         }
243         $this->menuItem(common_local_url('peoplesearch'), _('Search'));
244         if ($user) {
245             $this->menuItem(common_local_url('profilesettings'),
246                              _('Settings'));
247             $this->menuItem(common_local_url('invite'),
248                              _('Invite'));
249             $this->menuItem(common_local_url('logout'),
250                              _('Logout'));
251         } else {
252             $this->menuItem(common_local_url('login'), _('Login'));
253             if (!common_config('site', 'closed')) {
254                 $this->menuItem(common_local_url('register'), _('Register'));
255             }
256             $this->menuItem(common_local_url('openidlogin'), _('OpenID'));
257         }
258         $this->menuItem(common_local_url('doc', array('title' => 'help')),
259                          _('Help'));
260         $this->elementEnd('ul');
261         $this->elementEnd('dd');        
262         $this->elementEnd('dl');
263     }
264
265     // Revist. Should probably do an hAtom pattern here
266     function showSiteNotice()
267     {
268         $text = common_config('site', 'notice');
269         if ($text) {
270             $this->elementStart('dl', array('id' => 'site_notice',
271                                             'class' => 'system_notice'));
272             $this->element('dt', null, _('Site notice'));
273             $this->element('dd', null, $text);
274             $this->elementEnd('dl');
275         }
276     }
277     
278     // MAY overload if no notice form needed... or direct message box????
279
280     function showNoticeForm()
281     {
282         $notice_form = new NoticeForm($this);
283         $notice_form->show();
284     }
285
286     function showAnonymousMessage()
287     {
288         // needs to be defined by the class
289     }
290     
291     function showCore()
292     {
293         $this->elementStart('div', array('id' => 'core'));
294         $this->elementStart('dl', array('id' => 'site_nav_local_views'));
295         $this->element('dt', null, _('Local views'));
296         $this->elementStart('dd');
297         $this->showLocalNav();
298         $this->elementEnd('dd');
299         $this->elementEnd('dl');
300         $this->showContentBlock();
301         $this->showAside();
302         $this->elementEnd('div');
303     }
304
305     // SHOULD overload
306
307     function showLocalNav()
308     {
309         // does nothing by default
310     }
311
312     function showContentBlock()
313     {
314         $this->elementStart('div', array('id' => 'content'));
315         $this->showPageTitle();
316         $this->showPageNoticeBlock();
317         $this->elementStart('div', array('id' => 'content_inner'));
318         // show the actual content (forms, lists, whatever)
319         $this->showContent();
320         $this->elementEnd('div');
321         $this->elementEnd('div');
322     }
323
324     function showPageTitle() {
325         $this->element('h1', NULL, $this->title());
326     }
327
328     function showPageNoticeBlock()
329     {
330         $this->elementStart('dl', array('id' => 'page_notice',
331                                         'class' => 'system_notice'));
332         $this->element('dt', null, _('Page notice'));
333         $this->elementStart('dd');
334         $this->showPageNotice();
335         $this->elementEnd('dd');
336         $this->elementEnd('dl');
337         }
338
339     // SHOULD overload (unless there's not a notice)
340
341     function showPageNotice()
342     {
343     }
344     
345     // MUST overload
346
347     function showContent()
348     {
349     }
350
351     function showAside()
352     {
353         $this->elementStart('div', array('id' => 'aside_primary',
354                                          'class' => 'aside'));
355         $this->showExportData();
356         $this->showSections();
357         $this->elementEnd('div');
358     }
359
360     // MAY overload if there are feeds
361
362     function showExportData()
363     {
364         // is there structure to this?
365         // list of (visible!) feed links
366         // can we reuse list of feeds from showFeeds() ?
367     }
368
369     // SHOULD overload
370
371     function showSections() {
372         // for each section, show it
373     }
374
375     function showFooter()
376     {
377         $this->elementStart('div', array('id' => 'footer'));
378         $this->showSecondaryNav();
379         $this->showLicenses();
380         $this->elementEnd('div');
381     }
382
383     function showSecondaryNav()
384     {
385         $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
386         $this->element('dt', null, _('Secondary site navigation'));
387         $this->elementStart('dd', null);
388         $this->elementStart('ul', array('class' => 'nav'));
389         $this->menuItem(common_local_url('doc', array('title' => 'help')),
390                          _('Help'));
391         $this->menuItem(common_local_url('doc', array('title' => 'about')),
392                          _('About'));
393         $this->menuItem(common_local_url('doc', array('title' => 'faq')),
394                          _('FAQ'));
395         $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
396                          _('Privacy'));
397         $this->menuItem(common_local_url('doc', array('title' => 'source')),
398                          _('Source'));
399         $this->menuItem(common_local_url('doc', array('title' => 'contact')),
400                          _('Contact'));
401         $this->elementEnd('ul');
402         $this->elementEnd('dd');
403         $this->elementEnd('dl');
404     }
405
406     function showLicenses()
407     {
408         $this->elementStart('dl', array('id' => 'licenses'));
409         $this->showLaconicaLicense();
410         $this->showContentLicense();
411         $this->elementEnd('dl');
412     }
413
414     function showLaconicaLicense()
415     {
416         $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
417         $this->elementStart('dd', null);
418         if (common_config('site', 'broughtby')) {
419             $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
420         } else {
421             $instr = _('**%%site.name%%** is a microblogging service. ');
422         }
423         $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);
424         $output = common_markup_to_html($instr);
425         $this->raw($output);
426         $this->elementEnd('dd');
427         // do it
428     }
429
430     function showContentLicense()
431     {
432         $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
433         $this->elementStart('dd', array('id' => 'site_content_license_cc'));
434         $this->elementStart('p');
435         $this->element('img', array('id' => 'license_cc',
436                                     'src' => common_config('license', 'image'),
437                                     'alt' => common_config('license', 'title')));
438         $this->text(_('All criti.ca content and data are available under the '));
439         $this->element('a', array('class' => 'license',
440                                   'rel' => 'external license',
441                                   'href' => common_config('license', 'url')),
442                        common_config('license', 'title'));
443         $this->text(_('license.'));
444         $this->elementEnd('p');
445         $this->elementEnd('dd');
446     }
447
448     // For comparison with If-Last-Modified
449     // If not applicable, return null
450
451     function last_modified()
452     {
453         return null;
454     }
455
456     function etag()
457     {
458         return null;
459     }
460
461     function isReadOnly()
462     {
463         return false;
464     }
465
466     function arg($key, $def=null)
467     {
468         if (array_key_exists($key, $this->args)) {
469             return $this->args[$key];
470         } else {
471             return $def;
472         }
473     }
474
475     function trimmed($key, $def=null)
476     {
477         $arg = $this->arg($key, $def);
478         return (is_string($arg)) ? trim($arg) : $arg;
479     }
480
481     // Note: argarray ignored, since it's now passed in in prepare()
482
483     function handle($argarray=null)
484     {
485
486         $lm = $this->last_modified();
487         $etag = $this->etag();
488
489         if ($etag) {
490             header('ETag: ' . $etag);
491         }
492
493         if ($lm) {
494             header('Last-Modified: ' . date(DATE_RFC1123, $lm));
495             $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
496             if ($if_modified_since) {
497                 $ims = strtotime($if_modified_since);
498                 if ($lm <= $ims) {
499                     if (!$etag ||
500                         $this->_has_etag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
501                         header('HTTP/1.1 304 Not Modified');
502                         // Better way to do this?
503                         exit(0);
504                     }
505                 }
506             }
507         }
508     }
509
510     function _has_etag($etag, $if_none_match)
511     {
512         return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
513     }
514
515     function boolean($key, $def=false)
516     {
517         $arg = strtolower($this->trimmed($key));
518
519         if (is_null($arg)) {
520             return $def;
521         } else if (in_array($arg, array('true', 'yes', '1'))) {
522             return true;
523         } else if (in_array($arg, array('false', 'no', '0'))) {
524             return false;
525         } else {
526             return $def;
527         }
528     }
529
530     function serverError($msg, $code=500)
531     {
532         $action = $this->trimmed('action');
533         common_debug("Server error '$code' on '$action': $msg", __FILE__);
534         common_server_error($msg, $code);
535     }
536
537     function clientError($msg, $code=400)
538     {
539         $action = $this->trimmed('action');
540         common_debug("User error '$code' on '$action': $msg", __FILE__);
541         common_user_error($msg, $code);
542     }
543
544     function self_url()
545     {
546         $action = $this->trimmed('action');
547         $args = $this->args;
548         unset($args['action']);
549         foreach (array_keys($_COOKIE) as $cookie) {
550             unset($args[$cookie]);
551         }
552         return common_local_url($action, $args);
553     }
554
555     // Added @id to li for some control.
556     // XXX: We might want to move this to htmloutputter.php
557
558     function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
559     {
560         $lattrs = array();
561         if ($is_selected) {
562             $lattrs['class'] = 'current';
563         }
564
565         $this->elementStart('li', (is_null($id)) ? null : array('id' => $id),  $lattrs);
566         $attrs['href'] = $url;
567         if ($title) {
568             $attrs['title'] = $title;
569         }
570         $this->element('a', $attrs, $text);
571         $this->elementEnd('li');
572     }
573
574     // Does a little before-after block for next/prev page
575
576     function pagination($have_before, $have_after, $page, $action, $args=null)
577     {
578         if ($have_before || $have_after) {
579             $this->elementStart('div', array('class' => 'pagination'));
580             $this->elementStart('dl', null);
581             $this->element('dt', null, _('Pagination'));
582             $this->elementStart('dd', null);
583             $this->elementStart('ul', array('class' => 'nav'));
584         }
585
586         if ($have_before) {
587             $pargs = array('page' => $page-1);
588             $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
589
590             $this->elementStart('li', array('class' => 'nav_prev'));
591             $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
592                            _('After'));
593             $this->elementEnd('li');
594         }
595
596         if ($have_after) {
597             $pargs = array('page' => $page+1);
598             $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
599             $this->elementStart('li', array('class' => 'nav_next'));
600             $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
601                            _('Before'));
602             $this->elementEnd('li');
603         }
604
605         if ($have_before || $have_after) {
606             $this->elementEnd('ul');
607             $this->elementEnd('dd');
608             $this->elementEnd('dl');
609             $this->elementEnd('div');
610         }
611     }
612 }