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