]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/action.php
773b0dce9c441c851e6a7ac3fbcf782a2109ea52
[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         $this->showNoticeForm();
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 ((isset($config['site']['logo']) && is_string($config['site']['logo']) && (strlen($config['site']['logo']) > 0))
222             || file_exists(theme_file('logo.png')))
223         {
224             $this->element('img', array('class' => 'logo photo',
225                                         'src' => isset($config['site']['logo']) ?
226                                         ($config['site']['logo']) : theme_path('logo.png'),
227                                         'alt' => $config['site']['name']));
228         }
229         $this->element('span', array('class' => 'fn org'), $config['site']['name']);
230         $this->elementEnd('a');
231         $this->elementEnd('address');
232     }
233
234     function showPrimaryNav()
235     {
236         $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
237         $this->element('dt', null, _('Primary site navigation'));
238         $this->elementStart('dd');
239         $user = common_current_user();
240         $this->elementStart('ul', array('class' => 'nav'));
241         if ($user) {
242             $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
243                              _('Home'));
244         }
245         $this->menuItem(common_local_url('peoplesearch'), _('Search'));
246         if ($user) {
247             $this->menuItem(common_local_url('profilesettings'),
248                              _('Settings'));
249             $this->menuItem(common_local_url('invite'),
250                              _('Invite'));
251             $this->menuItem(common_local_url('logout'),
252                              _('Logout'));
253         } else {
254             $this->menuItem(common_local_url('login'), _('Login'));
255             if (!common_config('site', 'closed')) {
256                 $this->menuItem(common_local_url('register'), _('Register'));
257             }
258             $this->menuItem(common_local_url('openidlogin'), _('OpenID'));
259         }
260         $this->menuItem(common_local_url('doc', array('title' => 'help')),
261                          _('Help'));
262         $this->elementEnd('ul');
263         $this->elementEnd('dd');        
264         $this->elementEnd('dl');
265     }
266
267     // Revist. Should probably do an hAtom pattern here
268     function showSiteNotice()
269     {
270         $text = common_config('site', 'notice');
271         if ($text) {
272             $this->elementStart('dl', array('id' => 'site_notice',
273                                             'class' => 'system_notice'));
274             $this->element('dt', null, _('Site notice'));
275             $this->element('dd', null, $text);
276             $this->elementEnd('dl');
277         }
278     }
279     
280     // MAY overload if no notice form needed... or direct message box????
281
282     function showNoticeForm()
283     {
284         $notice_form = new NoticeForm($this);
285         $notice_form->show();
286     }
287
288     function showCore()
289     {
290         $this->elementStart('div', array('id' => 'core'));
291         $this->showLocalNav();
292         $this->showContentBlock();
293         $this->showAside();
294         $this->elementEnd('div');
295     }
296
297     // SHOULD overload
298
299     function showLocalNav()
300     {
301         // does nothing by default
302     }
303
304     function showContentBlock()
305     {
306         $this->elementStart('div', array('id' => 'content'));
307         $this->showPageTitle();
308         $this->showPageNotice();
309         $this->showContent();
310         $this->elementEnd('div');
311     }
312
313     function showPageTitle() {
314         $this->element('h1', NULL, $this->title());
315     }
316
317     // SHOULD overload (unless there's not a notice)
318
319     function showPageNotice()
320     {
321         $this->elementStart('dl', array('id' => 'page_notice',
322                                         'class' => 'system_notice'));
323         $this->element('dt', null, _('Page notice'));
324         $this->elementStart('dd', null);
325         // Output a bunch of paragraphs here
326         $this->elementEnd('dd');
327         $this->elementEnd('dl');
328     }
329
330     // MUST overload
331
332     function showContent()
333     {
334         // show the actual content (forms, lists, whatever)
335         $this->elementStart('div', array('id' => 'content_inner'));
336         $this->elementEnd('div');
337     }
338
339     function showAside()
340     {
341         $this->elementStart('div', array('id' => 'aside_primary',
342                                          'class' => 'aside'));
343         $this->showExportData();
344         $this->showSections();
345         $this->elementEnd('div');
346     }
347
348     // MAY overload if there are feeds
349
350     function showExportData()
351     {
352         // is there structure to this?
353         // list of (visible!) feed links
354         // can we reuse list of feeds from showFeeds() ?
355     }
356
357     // SHOULD overload
358
359     function showSections() {
360         // for each section, show it
361     }
362
363     function showFooter()
364     {
365         $this->elementStart('div', array('id' => 'footer'));
366         $this->showSecondaryNav();
367         $this->showLicenses();
368         $this->elementEnd('div');
369     }
370
371     function showSecondaryNav()
372     {
373         $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
374         $this->element('dt', null, _('Secondary site navigation'));
375         $this->elementStart('dd', null);
376         $this->elementStart('ul', array('class' => 'nav'));
377         $this->menuItem(common_local_url('doc', array('title' => 'help')),
378                          _('Help'));
379         $this->menuItem(common_local_url('doc', array('title' => 'about')),
380                          _('About'));
381         $this->menuItem(common_local_url('doc', array('title' => 'faq')),
382                          _('FAQ'));
383         $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
384                          _('Privacy'));
385         $this->menuItem(common_local_url('doc', array('title' => 'source')),
386                          _('Source'));
387         $this->menuItem(common_local_url('doc', array('title' => 'contact')),
388                          _('Contact'));
389         $this->elementEnd('ul');
390         $this->elementEnd('dd');
391         $this->elementEnd('dl');
392     }
393
394     function showLicenses()
395     {
396         $this->elementStart('dl', array('id' => 'licenses'));
397         $this->showLaconicaLicense();
398         $this->showContentLicense();
399         $this->elementEnd('dl');
400     }
401
402     function showLaconicaLicense()
403     {
404         $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
405         $this->elementStart('dd', null);
406         if (common_config('site', 'broughtby')) {
407             $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
408         } else {
409             $instr = _('**%%site.name%%** is a microblogging service. ');
410         }
411         $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);
412         $output = common_markup_to_html($instr);
413         $this->raw($output);
414         $this->elementEnd('dd');
415         // do it
416     }
417
418     function showContentLicense()
419     {
420         $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
421         $this->elementStart('dd', array('id' => 'site_content_license_cc'));
422         $this->elementStart('p');
423         $this->text(_('Unless otherwise specified, contents of this site are copyright by the contributors and available under the '));
424         $this->element('a', array('class' => 'license',
425                                   'rel' => 'external license',
426                                   'href' => $config['license']['url']),
427                        $config['license']['title']);
428         $this->text(_('. Contributors should be attributed by full name or nickname.'));
429         $this->elementEnd('p');
430         $this->element('img', array('id' => 'license_cc',
431                                     'src' => $config['license']['image'],
432                                     'alt' => $config['license']['title']));
433         $this->elementEnd('dd');
434     }
435
436     // For comparison with If-Last-Modified
437     // If not applicable, return null
438
439     function last_modified()
440     {
441         return null;
442     }
443
444     function etag()
445     {
446         return null;
447     }
448
449     function is_readonly()
450     {
451         return false;
452     }
453
454     function arg($key, $def=null)
455     {
456         if (array_key_exists($key, $this->args)) {
457             return $this->args[$key];
458         } else {
459             return $def;
460         }
461     }
462
463     function trimmed($key, $def=null)
464     {
465         $arg = $this->arg($key, $def);
466         return (is_string($arg)) ? trim($arg) : $arg;
467     }
468
469     // Note: argarray ignored, since it's now passed in in prepare()
470
471     function handle($argarray=null)
472     {
473
474         $lm = $this->last_modified();
475         $etag = $this->etag();
476
477         if ($etag) {
478             header('ETag: ' . $etag);
479         }
480
481         if ($lm) {
482             header('Last-Modified: ' . date(DATE_RFC1123, $lm));
483             $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
484             if ($if_modified_since) {
485                 $ims = strtotime($if_modified_since);
486                 if ($lm <= $ims) {
487                     if (!$etag ||
488                         $this->_has_etag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
489                         header('HTTP/1.1 304 Not Modified');
490                         // Better way to do this?
491                         exit(0);
492                     }
493                 }
494             }
495         }
496     }
497
498     function _has_etag($etag, $if_none_match)
499     {
500         return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
501     }
502
503     function boolean($key, $def=false)
504     {
505         $arg = strtolower($this->trimmed($key));
506
507         if (is_null($arg)) {
508             return $def;
509         } else if (in_array($arg, array('true', 'yes', '1'))) {
510             return true;
511         } else if (in_array($arg, array('false', 'no', '0'))) {
512             return false;
513         } else {
514             return $def;
515         }
516     }
517
518     function server_error($msg, $code=500)
519     {
520         $action = $this->trimmed('action');
521         common_debug("Server error '$code' on '$action': $msg", __FILE__);
522         common_server_error($msg, $code);
523     }
524
525     function client_error($msg, $code=400)
526     {
527         $action = $this->trimmed('action');
528         common_debug("User error '$code' on '$action': $msg", __FILE__);
529         common_user_error($msg, $code);
530     }
531
532     function self_url()
533     {
534         $action = $this->trimmed('action');
535         $args = $this->args;
536         unset($args['action']);
537         foreach (array_keys($_COOKIE) as $cookie) {
538             unset($args[$cookie]);
539         }
540         return common_local_url($action, $args);
541     }
542
543     function nav_menu($menu)
544     {
545         $action = $this->trimmed('action');
546         $this->elementStart('ul', array('id' => 'nav_views'));
547         foreach ($menu as $menuaction => $menudesc) {
548             $this->menuItem(common_local_url($menuaction,
549                                               isset($menudesc[2]) ? $menudesc[2] : null),
550                              $menudesc[0],
551                              $menudesc[1],
552                              $action == $menuaction);
553         }
554         $this->elementEnd('ul');
555     }
556
557     function common_show_header($pagetitle, $callable=null, $data=null, $headercall=null)
558     {
559         global $config, $xw;
560         global $action; /* XXX: kind of cheating here. */
561
562         common_start_html();
563
564         $this->elementStart('head');
565
566         if ($callable) {
567             if ($data) {
568                 call_user_func($callable, $data);
569             } else {
570                 call_user_func($callable);
571             }
572         }
573         $this->elementEnd('head');
574         $this->elementStart('body', $action);
575         $this->elementStart('div', array('id' => 'wrap'));
576         $this->elementStart('div', array('id' => 'content'));
577     }
578
579     // Added @id to li for some control.
580     // XXX: We might want to move this to htmloutputter.php
581
582     function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
583     {
584         $lattrs = array();
585         if ($is_selected) {
586             $lattrs['class'] = 'current';
587         }
588
589         $this->elementStart('li', (is_null($id)) ? null : array('id' => $id),  $lattrs);
590         $attrs['href'] = $url;
591         if ($title) {
592             $attrs['title'] = $title;
593         }
594         $this->element('a', $attrs, $text);
595         $this->elementEnd('li');
596     }
597
598     // Does a little before-after block for next/prev page
599
600     function pagination($have_before, $have_after, $page, $action, $args=null)
601     {
602         if ($have_before || $have_after) {
603             $this->elementStart('div', array('class' => 'pagination'));
604             $this->elementStart('dl', null);
605             $this->element('dt', null, _('Pagination'));
606             $this->elementStart('dd', null);
607             $this->elementStart('ul', array('class' => 'nav'));
608         }
609
610         if ($have_before) {
611             $pargs = array('page' => $page-1);
612             $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
613
614             $this->elementStart('li', array('class' => 'nav_prev'));
615             $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
616                            _('After'));
617             $this->elementEnd('li');
618         }
619
620         if ($have_after) {
621             $pargs = array('page' => $page+1);
622             $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
623             $this->elementStart('li', array('class' => 'nav_next'));
624             $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
625                            _('Before'));
626             $this->elementEnd('li');
627         }
628
629         if ($have_before || $have_after) {
630             $this->elementEnd('ul');
631             $this->elementEnd('dd');
632             $this->elementEnd('dl');
633             $this->elementEnd('div');
634         }
635     }
636 }