]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/util.php
change names, update
[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('HTTP/1.1 500 Server Error');
26         header('Content-type: text/plain');
27
28         print $msg;
29         print "\n";
30         exit();
31 }
32
33 # Show a user error
34 function common_user_error($msg, $code=200) {
35         common_show_header('Error');
36         common_element('div', array('class' => 'error'), $msg);
37         common_show_footer();
38 }
39
40 $xw = null;
41
42 # Start an HTML element
43 function common_element_start($tag, $attrs=NULL) {
44         global $xw;
45         $xw->startElement($tag);
46         if (is_array($attrs)) {
47                 foreach ($attrs as $name => $value) {
48                         $xw->writeAttribute($name, $value);
49                 }
50         } else if (is_string($attrs)) {
51                 $xw->writeAttribute('class', $attrs);
52         }
53 }
54
55 function common_element_end($tag) {
56         global $xw;
57         $xw->endElement();
58 }
59
60 function common_element($tag, $attrs=NULL, $content=NULL) {
61     common_element_start($tag, $attrs);
62         if ($content) {
63                 global $xw;
64                 $xw->text($content);
65         }
66         common_element_end($tag);
67 }
68
69 function common_start_xml($doc=NULL, $public=NULL, $system=NULL) {
70         global $xw;
71         $xw = new XMLWriter();
72         $xw->openURI('php://output');
73         $xw->setIndent(true);
74         $xw->startDocument('1.0', 'UTF-8');
75         if ($doc) {
76                 $xw->writeDTD($doc, $public, $system);
77         }
78 }
79
80 function common_end_xml() {
81         global $xw;
82         $xw->endDocument();
83         $xw->flush();
84 }
85
86 function common_show_header($pagetitle, $callable=NULL, $data=NULL) {
87         global $config, $xw;
88
89         header('Content-Type: application/xhtml+xml');
90
91         common_start_xml('html',
92                                          '-//W3C//DTD XHTML 1.0 Strict//EN',
93                                          'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
94
95         # FIXME: correct language for interface
96
97         common_element_start('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
98                                                                            'xml:lang' => 'en',
99                                                                            'lang' => 'en'));
100
101         common_element_start('head');
102         common_element('title', NULL,
103                                    $pagetitle . " - " . $config['site']['name']);
104         common_element('link', array('rel' => 'stylesheet',
105                                                                  'type' => 'text/css',
106                                                                  'href' => common_path('theme/default/style/html.css'),
107                                                                  'media' => 'screen, projection, tv'));
108         common_element('link', array('rel' => 'stylesheet',
109                                                                  'type' => 'text/css',
110                                                                  'href' => common_path('theme/default/style/layout.css'),
111                                                                  'media' => 'screen, projection, tv'));
112         common_element('link', array('rel' => 'stylesheet',
113                                                                  'type' => 'text/css',
114                                                                  'href' => common_path('theme/default/style/print.css'),
115                                                                  'media' => 'print'));
116         if ($callable) {
117                 if ($data) {
118                         call_user_func($callable, $data);
119                 } else {
120                         call_user_func($callable);
121                 }
122         }
123         common_element_end('head');
124         common_element_start('body');
125         common_element_start('div', array('id' => 'wrapper'));
126         common_element_start('div', array('id' => 'content'));
127         common_element_start('div', array('id' => 'header'));
128         common_element('h1', 'title', $pagetitle);
129         common_element('h2', 'subtitle', $config['site']['name']);
130         common_element_end('div');
131         common_head_menu();
132         common_element_start('div', array('id' => 'page'));
133 }
134
135 function common_show_footer() {
136         global $xw, $config;
137         common_element_start('div', 'footer');
138         common_foot_menu();
139         common_license_block();
140         common_element_end('div');
141         common_element_end('div');
142         common_element_end('div');
143         common_element_end('div');
144         common_element_end('body');
145         common_element_end('html');
146         common_end_xml();
147 }
148
149 function common_text($txt) {
150         global $xw;
151         $xw->text($txt);
152 }
153
154 function common_raw($xml) {
155         global $xw;
156         $xw->writeRaw($xml);
157 }
158
159 function common_license_block() {
160         global $config, $xw;
161         common_element_start('p', 'license greenBg');
162         common_element_start('span', 'floatLeft width25');
163         common_element_start('a', array('class' => 'license',
164                                                                         'rel' => 'license',
165                                                                         href => $config['license']['url']));
166         common_element('img', array('class' => 'license',
167                                                                 'src' => $config['license']['image'],
168                                                                 'alt' => $config['license']['title']));
169         common_element_end('a');
170         common_element_end('span');
171         common_element_start('span', 'floatRight width75');
172         common_text(_t('Unless otherwise specified, contents of this site are copyright by the contributors and available under the '));
173         common_element('a', array('class' => 'license',
174                                                           'rel' => 'license',
175                                                           href => $config['license']['url']),
176                                    $config['license']['title']);
177         common_text(_t('. Contributors should be attributed by full name or nickname.'));
178         common_element_end('span');
179         common_element_end('p');
180 }
181
182 function common_head_menu() {
183         $user = common_current_user();
184         common_element_start('ul', array('id' => 'menu', 'class' => ($user) ? 'five' : 'three'));
185         common_menu_item(common_local_url('public'), _t('Public'));
186         if ($user) {
187                 common_menu_item(common_local_url('all', array('nickname' =>
188                                                                                                            $user->nickname)),
189                                                  _t('Home'));
190                 common_menu_item(common_local_url('showstream', array('nickname' =>
191                                                                                                                           $user->nickname)),
192                                                  _t('Profile'),  $user->fullname || $user->nickname);
193                 common_menu_item(common_local_url('profilesettings'),
194                                                  _t('Settings'));
195                 common_menu_item(common_local_url('logout'),
196                                                  _t('Logout'));
197         } else {
198                 common_menu_item(common_local_url('login'),
199                                                  _t('Login'));
200                 common_menu_item(common_local_url('register'),
201                                                  _t('Register'));
202         }
203         common_element_end('ul');
204 }
205
206 function common_foot_menu() {
207         common_element_start('ul', 'footmenu menuish');
208         common_menu_item(common_local_url('doc', array('title' => 'about')),
209                                          _t('About'));
210         common_menu_item(common_local_url('doc', array('title' => 'help')),
211                                          _t('Help'));
212         common_menu_item(common_local_url('doc', array('title' => 'privacy')),
213                                          _t('Privacy'));
214         common_menu_item(common_local_url('doc', array('title' => 'source')),
215                                          _t('Source'));
216         common_element_end('ul');
217 }
218
219 function common_menu_item($url, $text, $title=NULL) {
220         $attrs['href'] = $url;
221         if ($title) {
222                 $attrs['title'] = $title;
223         }
224         common_element_start('li', 'menuitem');
225         common_element('a', $attrs, $text);
226         common_element_end('li');
227 }
228
229 function common_input($id, $label, $value=NULL) {
230         common_element_start('p');
231         common_element('label', array('for' => $id), $label);
232         $attrs = array('name' => $id,
233                                    'type' => 'text',
234                                    'id' => $id);
235         if ($value) {
236                 $attrs['value'] = htmlspecialchars($value);
237         }
238         common_element('input', $attrs);
239         common_element_end('p');
240 }
241
242 function common_hidden($id, $value) {
243         common_element('input', array('name' => $id,
244                                                                   'type' => 'hidden',
245                                                                   'id' => $id,
246                                                                   'value' => $value));
247 }
248
249 function common_password($id, $label) {
250         common_element_start('p');
251         common_element('label', array('for' => $id), $label);
252         $attrs = array('name' => $id,
253                                    'type' => 'password',
254                                    'id' => $id);
255         common_element('input', $attrs);
256         common_element_end('p');
257 }
258
259 function common_submit($id, $label) {
260         global $xw;
261         common_element_start('p');
262         common_element_start('label', array('for' => $id));
263         $xw->writeRaw('&nbsp;');
264         common_element_end('label');
265         common_element('input', array('type' => 'submit',
266                                                                   'id' => $id,
267                                                                   'name' => $id,
268                                                                   'value' => $label,
269                                                                   'class' => 'button'));
270         common_element_end('p');
271 }
272
273 function common_textarea($id, $label, $content=NULL) {
274         common_element_start('p');
275         common_element('label', array('for' => $id), $label);
276         common_element('textarea', array('rows' => 3,
277                                                                          'cols' => 40,
278                                                                          'name' => $id,
279                                                                          'id' => $id, 
280                                                                          'class' => 'width50'),
281                                    ($content) ? $content : ' ');
282         common_element_end('p');
283 }
284
285 # salted, hashed passwords are stored in the DB
286
287 function common_munge_password($id, $password) {
288         return md5($id . $password);
289 }
290
291 # check if a username exists and has matching password
292 function common_check_user($nickname, $password) {
293         $user = User::staticGet('nickname', $nickname);
294         if (is_null($user)) {
295                 return false;
296         } else {
297                 return (0 == strcmp(common_munge_password($password, $user->id),
298                                                         $user->password));
299         }
300 }
301
302 # is the current user logged in?
303 function common_logged_in() {
304         return (!is_null(common_current_user()));
305 }
306
307 function common_have_session() {
308         return (0 != strcmp(session_id(), ''));
309 }
310
311 function common_ensure_session() {
312         if (!common_have_session()) {
313                 @session_start();
314         }
315 }
316
317 function common_set_user($nickname) {
318         if (is_null($nickname) && common_have_session()) {
319                 unset($_SESSION['userid']);
320                 return true;
321         } else {
322                 $user = User::staticGet('nickname', $nickname);
323                 if ($user) {
324                         common_ensure_session();
325                         $_SESSION['userid'] = $user->id;
326                         return true;
327                 } else {
328                         return false;
329                 }
330         }
331         return false;
332 }
333
334 # who is the current user?
335 function common_current_user() {
336         static $user = NULL; # FIXME: global memcached
337         if (is_null($user)) {
338                 common_ensure_session();
339                 $id = $_SESSION['userid'];
340                 if ($id) {
341                         $user = User::staticGet($id);
342                 }
343         }
344         return $user;
345 }
346
347 # get canonical version of nickname for comparison
348 function common_canonical_nickname($nickname) {
349         # XXX: UTF-8 canonicalization (like combining chars)
350         return $nickname;
351 }
352
353 # get canonical version of email for comparison
354 function common_canonical_email($email) {
355         # XXX: canonicalize UTF-8
356         # XXX: lcase the domain part
357         return $email;
358 }
359
360 define('URL_REGEX', '^|[ \t\r\n])((ftp|http|https|gopher|mailto|news|nntp|telnet|wais|file|prospero|aim|webcal):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))');
361
362 function common_render_content($text, $notice) {
363         $r = htmlspecialchars($text);
364         $id = $notice->profile_id;
365         $r = preg_replace('@https?://\S+@', '<a href="\0" class="extlink">\0</a>', $r);
366         $r = preg_replace('/(^|\b)@([\w-]+)($|\b)/e', "'\\1@'.common_at_link($id, '\\2').'\\3'", $r);
367         # XXX: # tags
368         # XXX: machine tags
369         return $r;
370 }
371
372 function common_at_link($sender_id, $nickname) {
373         # Try to find profiles this profile is subscribed to that have this nickname
374         $recipient = new Profile();
375         # XXX: chokety and bad
376         $recipient->whereAdd('EXISTS (SELECT subscribed from subscription where subscriber = '.$sender_id.' and subscribed = id)', 'AND');
377         $recipient->whereAdd('nickname = "' . trim($nickname) . '"', 'AND');
378         if ($recipient->find(TRUE)) {
379                 return '<a href="'.htmlspecialchars($recipient->profileurl).'" class="atlink tolistenee">'.$nickname.'</a>';
380         }
381         # Try to find profiles that listen to this profile and that have this nickname
382         $recipient = new Profile();
383         # XXX: chokety and bad
384         $recipient->whereAdd('EXISTS (SELECT subscriber from subscription where subscribed = '.$sender_id.' and subscriber = id)', 'AND');
385         $recipient->whereAdd('nickname = "' . trim($nickname) . '"', 'AND');
386         if ($recipient->find(TRUE)) {
387                 return '<a href="'.htmlspecialchars($recipient->profileurl).'" class="atlink tolistener">'.$nickname.'</a>';
388         }
389         # If this is a local user, try to find a local user with that nickname.
390         $sender = User::staticGet($sender_id);
391         if ($sender) {
392                 $recipient_user = User::staticGet('nickname', $nickname);
393                 if ($recipient_user) {
394                         $recipient = $recipient->getProfile();
395                         return '<a href="'.htmlspecialchars($recipient->profileurl).'" class="atlink usertouser">'.$nickname.'</a>';
396                 }
397         }
398         # Otherwise, no links. @messages from local users to remote users,
399         # or from remote users to other remote users, are just
400         # outside our ability to make intelligent guesses about
401         return $nickname;
402 }
403
404 // where should the avatar go for this user?
405
406 function common_avatar_filename($user, $extension, $size=NULL, $extra=NULL) {
407         global $config;
408
409         if ($size) {
410                 return $user->id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
411         } else {
412                 return $user->id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
413         }
414 }
415
416 function common_avatar_path($filename) {
417         global $config;
418         return INSTALLDIR . '/avatar/' . $filename;
419 }
420
421 function common_avatar_url($filename) {
422         return common_path('avatar/'.$filename);
423 }
424
425 function common_default_avatar($size) {
426         static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
427                                                           AVATAR_STREAM_SIZE => 'stream',
428                                                           AVATAR_MINI_SIZE => 'mini');
429         global $config;
430         
431         return common_path($config['avatar']['default'][$sizenames[$size]]);
432 }
433
434 function common_local_url($action, $args=NULL) {
435         global $config;
436         if ($config['site']['fancy']) {
437                 return common_fancy_url($action, $args);
438         } else {
439                 return common_simple_url($action, $args);
440         }
441 }
442
443 function common_fancy_url($action, $args=NULL) {
444         switch (strtolower($action)) {
445          default:
446                 return common_simple_url($action, $args);
447         }
448 }
449
450 function common_simple_url($action, $args=NULL) {
451         global $config;
452         /* XXX: pretty URLs */
453         $extra = '';
454         if ($args) {
455                 foreach ($args as $key => $value) {
456                         $extra .= "&${key}=${value}";
457                 }
458         }
459         return common_path("index.php?action=${action}${extra}");
460 }
461
462 function common_path($relative) {
463         global $config;
464         $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
465         return "http://".$config['site']['server'].'/'.$pathpart.$relative;
466 }
467
468 function common_date_string($dt) {
469         // XXX: do some sexy date formatting
470         // return date(DATE_RFC822, $dt);
471         return $dt;
472 }
473
474 function common_date_w3dtf($dt) {
475         $t = strtotime($dt);
476         return date(DATE_W3C, $t);
477 }
478
479 function common_redirect($url, $code=307) {
480         static $status = array(301 => "Moved Permanently",
481                                                    302 => "Found",
482                                                    303 => "See Other",
483                                                    307 => "Temporary Redirect");
484         header("Status: ${code} $status[$code]");
485         header("Location: $url");
486         common_element('a', array('href' => $url), $url);
487 }
488
489 function common_broadcast_notice($notice) {
490         // XXX: broadcast notices to remote subscribers
491         // XXX: broadcast notices to SMS
492         // XXX: broadcast notices to Jabber
493         // XXX: broadcast notices to other IM
494         // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
495         return true;
496 }
497
498 function common_profile_url($nickname) {
499         return common_local_url('showstream', array('nickname' => $nickname));
500 }
501
502 function common_notice_form() {
503         common_element_start('form', array('id' => 'newnotice', 'method' => 'POST',
504                                                                            'action' => common_local_url('newnotice')));
505         common_textarea('noticecontent', _t('What\'s up?'));
506         common_submit('submit', _t('Send'));
507         common_element_end('form');
508 }
509
510 function common_mint_tag($extra) {
511         global $config;
512         return 
513           'tag:'.$config['tag']['authority'].','.
514           $config['tag']['date'].':'.$config['tag']['prefix'].$extra;
515 }
516
517 # Should make up a reasonable root URL
518
519 function common_root_url() {
520         return common_path('');
521 }
522
523 # returns $bytes bytes of random data as a hexadecimal string
524 # "good" here is a goal and not a guarantee
525
526 function common_good_rand($bytes) {
527         # XXX: use random.org...?
528         if (file_exists('/dev/urandom')) {
529                 return common_urandom($bytes);
530         } else { # FIXME: this is probably not good enough
531                 return common_mtrand($bytes);
532         }
533 }
534
535 function common_urandom($bytes) {
536         $h = fopen('/dev/urandom', 'rb');
537         # should not block
538         $src = fread($h, $bytes);
539         fclose($h);
540         $enc = '';
541         for ($i = 0; $i < $bytes; $i++) {
542                 $enc .= sprintf("%02x", (ord($src[$i])));
543         }
544         return $enc;
545 }
546
547 function common_mtrand($bytes) {
548         $enc = '';
549         for ($i = 0; $i < $bytes; $i++) {
550                 $enc .= sprintf("%02x", mt_rand(0, 255));
551         }
552         return $enc;
553 }
554
555 function common_set_returnto($url) {
556         common_ensure_session();
557         $_SESSION['returnto'] = $url;
558 }
559
560 function common_get_returnto() {
561         common_ensure_session();
562         return $_SESSION['returnto'];
563 }
564
565 function common_timestamp() {
566         return date('YmdHis');
567 }
568         
569 // XXX: set up gettext
570
571 function _t($str) {
572         return $str;
573 }
574
575 function common_ensure_syslog() {
576         static $initialized = false;
577         if (!$initialized) {
578                 define_syslog_variables();
579                 openlog("laconica", 0, LOG_USER);
580                 $initialized = true;
581         }
582 }
583
584 function common_log($priority, $msg) {
585         common_ensure_syslog();
586         syslog($priority, $msg);
587 }
588
589 function common_debug($msg) {
590         common_log(LOG_DEBUG, $msg);
591 }