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