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