]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/util.php
try to float license text right
[quix0rs-gnu-social.git] / lib / util.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /* XXX: break up into separate modules (HTTP, HTML, user, files) */
21
22 # Show a server error
23
24 function common_server_error($msg) {
25         header('Status: 500 Server Error');
26         header('Content-type: text/plain');
27
28         print $msg;
29         exit();
30 }
31
32 # Show a user error
33 function common_user_error($msg, $code=200) {
34         common_show_header('Error');
35         common_element('div', array('class' => 'error'), $msg);
36         common_show_footer();
37 }
38
39 $xw = null;
40
41 # Start an HTML element
42 function common_element_start($tag, $attrs=NULL) {
43         global $xw;
44         $xw->startElement($tag);
45         if (is_array($attrs)) {
46                 foreach ($attrs as $name => $value) {
47                         $xw->writeAttribute($name, $value);
48                 }
49         } else if (is_string($attrs)) {
50                 $xw->writeAttribute('class', $attrs);
51         }
52 }
53
54 function common_element_end($tag) {
55         global $xw;
56         $xw->endElement();
57 }
58
59 function common_element($tag, $attrs=NULL, $content=NULL) {
60     common_element_start($tag, $attrs);
61         if ($content) {
62                 global $xw;
63                 $xw->text($content);
64         }
65         common_element_end($tag);
66 }
67
68 function common_start_xml($doc=NULL, $public=NULL, $system=NULL) {
69         global $xw;
70         $xw = new XMLWriter();
71         $xw->openURI('php://output');
72         $xw->setIndent(true);
73         $xw->startDocument('1.0', 'UTF-8');
74         if ($doc) {
75                 $xw->writeDTD($doc, $public, $system);
76         }
77 }
78
79 function common_end_xml() {
80         global $xw;
81         $xw->endDocument();
82         $xw->flush();
83 }
84
85 function common_show_header($pagetitle, $callable=NULL, $data=NULL) {
86         global $config, $xw;
87
88         header('Content-Type: application/xhtml+xml');
89
90         common_start_xml('html',
91                                          '-//W3C//DTD XHTML 1.0 Strict//EN',
92                                          'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
93
94         # FIXME: correct language for interface
95
96         common_element_start('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
97                                                                            'xml:lang' => 'en',
98                                                                            'lang' => 'en'));
99
100         common_element_start('head');
101         common_element('title', NULL,
102                                    $pagetitle . " - " . $config['site']['name']);
103         common_element('link', array('rel' => 'stylesheet',
104                                                                  'type' => 'text/css',
105                                                                  'href' => $config['site']['path'] . 'theme/default/style/html.css',
106                                                                  'media' => 'screen, projection, tv'));
107         common_element('link', array('rel' => 'stylesheet',
108                                                                  'type' => 'text/css',
109                                                                  'href' => $config['site']['path'] . 'theme/default/style/layout.css',
110                                                                  'media' => 'screen, projection, tv'));
111         common_element('link', array('rel' => 'stylesheet',
112                                                                  'type' => 'text/css',
113                                                                  'href' => $config['site']['path'] . 'theme/default/style/print.css',
114                                                                  'media' => 'print'));
115         if ($callable) {
116                 if ($data) {
117                         call_user_func($callable, $data);
118                 } else {
119                         call_user_func($callable);
120                 }
121         }
122         common_element_end('head');
123         common_element_start('body');
124         common_element_start('div', array('id' => 'wrapper'));
125         common_element_start('div', array('id' => 'content'));
126         common_element_start('div', array('id' => 'header'));
127         common_element('h1', 'title', $pagetitle);
128         common_element('h2', 'subtitle', $config['site']['name']);
129         common_element_end('div');
130         common_head_menu();
131         common_element_start('div', array('id' => 'page'));
132 }
133
134 function common_show_footer() {
135         global $xw, $config;
136         common_element_start('div', 'footer');
137         common_foot_menu();
138         common_license_block();
139         common_element_end('div');
140         common_element_end('div');
141         common_element_end('div');
142         common_element_end('div');
143         common_element_end('body');
144         common_element_end('html');
145         common_end_xml();
146 }
147
148 function common_text($txt) {
149         global $xw;
150         $xw->text($txt);
151 }
152
153 function common_license_block() {
154         global $config, $xw;
155         common_element_start('p', 'license');
156         common_element_start('a', array('class' => 'license floatLeft',
157                                                                         'rel' => 'license',
158                                                                         href => $config['license']['url']));
159         common_element('img', array('class' => 'license',
160                                                                 'src' => $config['license']['image'],
161                                                                 'alt' => $config['license']['title']));
162         common_element_end('a');
163         common_element_start('span', 'floatRight');
164         common_text(_t('Unless otherwise specified, contents of this site are copyright by the contributors and available under the '));
165         common_element('a', array('class' => 'license',
166                                                           'rel' => 'license',
167                                                           href => $config['license']['url']),
168                                    $config['license']['title']);
169         common_text(_t('. Contributors should be attributed by full name or nickname.'));
170         common_element_end('span');
171         common_element_end('p');
172 }
173
174 function common_head_menu() {
175         $user = common_current_user();
176         common_element_start('ul', array('id' => 'menu', 'class' => ($user) ? 'five' : 'three'));
177         common_menu_item(common_local_url('public'), _t('Public'));
178         if ($user) {
179                 common_menu_item(common_local_url('all', array('nickname' =>
180                                                                                                            $user->nickname)),
181                                                  _t('Home'));
182                 common_menu_item(common_local_url('showstream', array('nickname' =>
183                                                                                                                           $user->nickname)),
184                                                  _t('Profile'),  $user->fullname || $user->nickname);
185                 common_menu_item(common_local_url('profilesettings'),
186                                                  _t('Settings'));
187                 common_menu_item(common_local_url('logout'),
188                                                  _t('Logout'));
189         } else {
190                 common_menu_item(common_local_url('login'),
191                                                  _t('Login'));
192                 common_menu_item(common_local_url('register'),
193                                                  _t('Register'));
194         }
195         common_element_end('ul');
196 }
197
198 function common_foot_menu() {
199         common_element_start('ul', 'footmenu menuish');
200         common_menu_item(common_local_url('doc', array('title' => 'about')),
201                                          _t('About'));
202         common_menu_item(common_local_url('doc', array('title' => 'help')),
203                                          _t('Help'));
204         common_menu_item(common_local_url('doc', array('title' => 'privacy')),
205                                          _t('Privacy'));
206         common_menu_item(common_local_url('doc', array('title' => 'source')),
207                                          _t('Source'));
208         common_element_end('ul');
209 }
210
211 function common_menu_item($url, $text, $title=NULL) {
212         $attrs['href'] = $url;
213         if ($title) {
214                 $attrs['title'] = $title;
215         }
216         common_element_start('li', 'menuitem');
217         common_element('a', $attrs, $text);
218         common_element_end('li');
219 }
220
221 function common_input($id, $label, $value=NULL) {
222         common_element_start('p');
223         common_element('label', array('for' => $id), $label);
224         $attrs = array('name' => $id,
225                                    'type' => 'text',
226                                    'id' => $id);
227         if ($value) {
228                 $attrs['value'] = htmlspecialchars($value);
229         }
230         common_element('input', $attrs);
231         common_element_end('p');
232 }
233
234 function common_password($id, $label) {
235         common_element_start('p');
236         common_element('label', array('for' => $id), $label);
237         $attrs = array('name' => $id,
238                                    'type' => 'password',
239                                    'id' => $id);
240         common_element('input', $attrs);
241         common_element_end('p');
242 }
243
244 function common_submit($id, $label) {
245         global $xw;
246         common_element_start('p');
247         common_element_start('label', array('for' => $id));
248         $xw->writeRaw('&nbsp;');
249         common_element_end('label');
250         common_element('input', array('type' => 'submit',
251                                                                   'id' => $id,
252                                                                   'name' => $id,
253                                                                   'value' => $label,
254                                                                   'class' => 'button'));
255         common_element_end('p');
256 }
257
258 function common_textarea($id, $label, $content=NULL) {
259         common_element_start('p');
260         common_element('label', array('for' => $id), $label);
261         common_element('textarea', array('rows' => 3,
262                                                                          'cols' => 40,
263                                                                          'name' => $id,
264                                                                          'id' => $id, 
265                                                                          'class' => 'width50'),
266                                    ($content) ? $content : ' ');
267         common_element_end('p');
268 }
269
270 # salted, hashed passwords are stored in the DB
271
272 function common_munge_password($id, $password) {
273         return md5($id . $password);
274 }
275
276 # check if a username exists and has matching password
277 function common_check_user($nickname, $password) {
278         $user = User::staticGet('nickname', $nickname);
279         if (is_null($user)) {
280                 return false;
281         } else {
282                 return (0 == strcmp(common_munge_password($password, $user->id),
283                                                         $user->password));
284         }
285 }
286
287 # is the current user logged in?
288 function common_logged_in() {
289         return (!is_null(common_current_user()));
290 }
291
292 function common_have_session() {
293         return (0 != strcmp(session_id(), ''));
294 }
295
296 function common_ensure_session() {
297         if (!common_have_session()) {
298                 @session_start();
299         }
300 }
301
302 function common_set_user($nickname) {
303         if (is_null($nickname) && common_have_session()) {
304                 unset($_SESSION['userid']);
305                 return true;
306         } else {
307                 $user = User::staticGet('nickname', $nickname);
308                 if ($user) {
309                         common_ensure_session();
310                         $_SESSION['userid'] = $user->id;
311                         return true;
312                 } else {
313                         return false;
314                 }
315         }
316         return false;
317 }
318
319 # who is the current user?
320 function common_current_user() {
321         static $user = NULL; # FIXME: global memcached
322         if (is_null($user)) {
323                 common_ensure_session();
324                 $id = $_SESSION['userid'];
325                 if ($id) {
326                         $user = User::staticGet($id);
327                 }
328         }
329         return $user;
330 }
331
332 # get canonical version of nickname for comparison
333 function common_canonical_nickname($nickname) {
334         # XXX: UTF-8 canonicalization (like combining chars)
335         return $nickname;
336 }
337
338 # get canonical version of email for comparison
339 function common_canonical_email($email) {
340         # XXX: canonicalize UTF-8
341         # XXX: lcase the domain part
342         return $email;
343 }
344
345 function common_render_content($text) {
346         # XXX: @ messages
347         # XXX: # tags
348         # XXX: machine tags
349         return htmlspecialchars($text);
350 }
351
352 // where should the avatar go for this user?
353
354 function common_avatar_filename($user, $extension, $size=NULL, $extra=NULL) {
355         global $config;
356
357         if ($size) {
358                 return $user->id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
359         } else {
360                 return $user->id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
361         }
362 }
363
364 function common_avatar_path($filename) {
365         global $config;
366         return $config['avatar']['directory'] . '/' . $filename;
367 }
368
369 function common_avatar_url($filename) {
370         global $config;
371         return "http://".$config['site']['server'].$config['avatar']['path'].'/'.$filename;
372 }
373
374 function common_default_avatar($size) {
375         static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
376                                                           AVATAR_STREAM_SIZE => 'stream',
377                                                           AVATAR_MINI_SIZE => 'mini');
378         global $config;
379         return "http://".$config['site']['server'].$config['site']['path'].'/'.$config['avatar']['default'][$sizenames[$size]];
380 }
381
382 function common_local_url($action, $args=NULL) {
383         global $config;
384         /* XXX: pretty URLs */
385         $extra = '';
386         if ($args) {
387                 foreach ($args as $key => $value) {
388                         $extra .= "&${key}=${value}";
389                 }
390         }
391         $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
392         return "http://".$config['site']['server'].'/'.$pathpart."index.php?action=${action}${extra}";
393 }
394
395 function common_date_string($dt) {
396         // XXX: do some sexy date formatting
397         // return date(DATE_RFC822, $dt);
398         return $dt;
399 }
400
401 function common_date_w3dtf($dt) {
402         $t = strtotime($dt);
403         return date(DATE_W3C, $t);
404 }
405
406 function common_redirect($url, $code=307) {
407         static $status = array(301 => "Moved Permanently",
408                                                    302 => "Found",
409                                                    303 => "See Other",
410                                                    307 => "Temporary Redirect");
411         header("Status: ${code} $status[$code]");
412         header("Location: $url");
413         common_element('a', array('href' => $url), $url);
414 }
415
416 function common_broadcast_notice($notice) {
417         // XXX: broadcast notices to remote subscribers
418         // XXX: broadcast notices to SMS
419         // XXX: broadcast notices to Jabber
420         // XXX: broadcast notices to other IM
421         // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
422         return true;
423 }
424
425 function common_profile_url($nickname) {
426         return common_local_url('showstream', array('nickname' => $nickname));
427 }
428
429 function common_notice_form() {
430         common_element_start('form', array('id' => 'newnotice', 'method' => 'POST',
431                                                                            'action' => common_local_url('newnotice')));
432         common_textarea('noticecontent', _t('What\'s up?'));
433         common_submit('submit', _t('Send'));
434         common_element_end('form');
435 }
436
437 function common_mint_tag($extra) {
438         global $config;
439         return 
440           'tag:'.$config['tag']['authority'].','.
441           $config['tag']['date'].':'.$config['tag']['prefix'].$extra;
442 }
443
444 # Should make up a reasonable root URL
445
446 function common_root_url() {
447         global $config;
448         $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
449         return "http://".$config['site']['server'].'/'.$pathpart;
450 }
451
452 # returns $bytes bytes of random data as a hexadecimal string
453 # "good" here is a goal and not a guarantee
454
455 function common_good_rand($bytes) {
456         # XXX: use random.org...?
457         if (file_exists('/dev/urandom')) {
458                 return common_urandom($bytes);
459         } else { # FIXME: this is probably not good enough
460                 return common_mtrand($bytes);
461         }
462 }
463
464 function common_urandom($bytes) {
465         $h = fopen('/dev/urandom', 'rb');
466         # should not block
467         $src = fread($h, $bytes);
468         fclose($h);
469         $enc = '';
470         for ($i = 0; $i < $bytes; $i++) {
471                 $enc .= sprintf("%02x", (ord($src[$i])));
472         }
473         return $enc;
474 }
475
476 function common_mtrand($bytes) {
477         $enc = '';
478         for ($i = 0; $i < $bytes; $i++) {
479                 $enc .= sprintf("%02x", mt_rand(0, 255));
480         }
481         return $enc;
482 }
483
484 function common_timestamp() {
485         return date('YmdHis');
486 }
487         
488 // XXX: set up gettext
489
490 function _t($str) {
491         return $str;
492 }