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