]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/util.php
add tags on creation of notices and users
[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('div', 'license');
156         common_element_start('a', array('class' => 'license',
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_text(_t('Unless otherwise specified, contents of this site are copyright by the contributors and available under the '));
164         common_element('a', array('class' => 'license',
165                                                           'rel' => 'license',
166                                                           href => $config['license']['url']),
167                                    $config['license']['title']);
168         common_text(_t('. Contributors should be attributed by full name or nickname.'));
169         common_element_end('div');
170 }
171
172 function common_head_menu() {
173         $user = common_current_user();
174         common_element_start('ul', array('id' => 'menu', 'class' => ($user) ? 'five' : 'three'));
175         common_menu_item(common_local_url('public'), _t('Public'));
176         if ($user) {
177                 common_menu_item(common_local_url('all', array('nickname' =>
178                                                                                                            $user->nickname)),
179                                                  _t('Home'));
180                 common_menu_item(common_local_url('showstream', array('nickname' =>
181                                                                                                                           $user->nickname)),
182                                                  _t('Profile'),  $user->fullname || $user->nickname);
183                 common_menu_item(common_local_url('profilesettings'),
184                                                  _t('Settings'));
185                 common_menu_item(common_local_url('logout'),
186                                                  _t('Logout'));
187         } else {
188                 common_menu_item(common_local_url('login'),
189                                                  _t('Login'));
190                 common_menu_item(common_local_url('register'),
191                                                  _t('Register'));
192         }
193         common_element_end('ul');
194 }
195
196 function common_foot_menu() {
197         common_element_start('ul', 'footmenu menuish');
198         common_menu_item(common_local_url('doc', array('title' => 'about')),
199                                          _t('About'));
200         common_menu_item(common_local_url('doc', array('title' => 'help')),
201                                          _t('Help'));
202         common_menu_item(common_local_url('doc', array('title' => 'privacy')),
203                                          _t('Privacy'));
204         common_menu_item(common_local_url('doc', array('title' => 'source')),
205                                          _t('Privacy'));
206         common_element_end('ul');
207 }
208
209 function common_menu_item($url, $text, $title=NULL) {
210         $attrs['href'] = $url;
211         if ($title) {
212                 $attrs['title'] = $title;
213         }
214         common_element_start('li', 'menuitem');
215         common_element('a', $attrs, $text);
216         common_element_end('li');
217 }
218
219 function common_input($id, $label, $value=NULL) {
220         common_element_start('p');
221         common_element('label', array('for' => $id), $label);
222         $attrs = array('name' => $id,
223                                    'type' => 'text',
224                                    'id' => $id);
225         if ($value) {
226                 $attrs['value'] = htmlspecialchars($value);
227         }
228         common_element('input', $attrs);
229         common_element_end('p');
230 }
231
232 function common_password($id, $label) {
233         common_element_start('p');
234         common_element('label', array('for' => $id), $label);
235         $attrs = array('name' => $id,
236                                    'type' => 'password',
237                                    'id' => $id);
238         common_element('input', $attrs);
239         common_element_end('p');
240 }
241
242 function common_submit($id, $label) {
243         global $xw;
244         common_element_start('p');
245         common_element_start('label', array('for' => $id));
246         $xw->writeRaw('&nbsp;');
247         common_element_end('label');
248         common_element('input', array('type' => 'submit',
249                                                                   'id' => $id,
250                                                                   'name' => $id,
251                                                                   'value' => $label,
252                                                                   'class' => 'button'));
253         common_element_end('p');
254 }
255
256 function common_textarea($id, $label, $content=NULL) {
257         common_element_start('p');
258         common_element('label', array('for' => $id), $label);
259         common_element('textarea', array('rows' => 3,
260                                                                          'cols' => 40,
261                                                                          'name' => $id,
262                                                                          'id' => $id, 
263                                                                          'class' => 'width50'),
264                                    ($content) ? $content : ' ');
265         common_element_end('p');
266 }
267
268 # salted, hashed passwords are stored in the DB
269
270 function common_munge_password($id, $password) {
271         return md5($id . $password);
272 }
273
274 # check if a username exists and has matching password
275 function common_check_user($nickname, $password) {
276         $user = User::staticGet('nickname', $nickname);
277         if (is_null($user)) {
278                 return false;
279         } else {
280                 return (0 == strcmp(common_munge_password($password, $user->id),
281                                                         $user->password));
282         }
283 }
284
285 # is the current user logged in?
286 function common_logged_in() {
287         return (!is_null(common_current_user()));
288 }
289
290 function common_have_session() {
291         return (0 != strcmp(session_id(), ''));
292 }
293
294 function common_ensure_session() {
295         if (!common_have_session()) {
296                 @session_start();
297         }
298 }
299
300 function common_set_user($nickname) {
301         if (is_null($nickname) && common_have_session()) {
302                 unset($_SESSION['userid']);
303                 return true;
304         } else {
305                 $user = User::staticGet('nickname', $nickname);
306                 if ($user) {
307                         common_ensure_session();
308                         $_SESSION['userid'] = $user->id;
309                         return true;
310                 } else {
311                         return false;
312                 }
313         }
314         return false;
315 }
316
317 # who is the current user?
318 function common_current_user() {
319         static $user = NULL; # FIXME: global memcached
320         if (is_null($user)) {
321                 common_ensure_session();
322                 $id = $_SESSION['userid'];
323                 if ($id) {
324                         $user = User::staticGet($id);
325                 }
326         }
327         return $user;
328 }
329
330 # get canonical version of nickname for comparison
331 function common_canonical_nickname($nickname) {
332         # XXX: UTF-8 canonicalization (like combining chars)
333         return $nickname;
334 }
335
336 # get canonical version of email for comparison
337 function common_canonical_email($email) {
338         # XXX: canonicalize UTF-8
339         # XXX: lcase the domain part
340         return $email;
341 }
342
343 function common_render_content($text) {
344         # XXX: @ messages
345         # XXX: # tags
346         # XXX: machine tags
347         return htmlspecialchars($text);
348 }
349
350 // where should the avatar go for this user?
351
352 function common_avatar_filename($user, $extension, $size=NULL) {
353         global $config;
354
355         if ($size) {
356                 return $user->id . '-' . $size . $extension;
357         } else {
358                 return $user->id . '-original' . $extension;
359         }
360 }
361
362 function common_avatar_path($filename) {
363         global $config;
364         return $config['avatar']['directory'] . '/' . $filename;
365 }
366
367 function common_avatar_url($filename) {
368         global $config;
369         return "http://".$config['site']['server'].$config['avatar']['path'].'/'.$filename;
370 }
371
372 function common_default_avatar($size) {
373         static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
374                                                           AVATAR_STREAM_SIZE => 'stream',
375                                                           AVATAR_MINI_SIZE => 'mini');
376         global $config;
377         return "http://".$config['site']['server'].$config['site']['path'].'/'.$config['avatar']['default'][$sizenames[$size]];
378 }
379
380 function common_local_url($action, $args=NULL) {
381         global $config;
382         /* XXX: pretty URLs */
383         $extra = '';
384         if ($args) {
385                 foreach ($args as $key => $value) {
386                         $extra .= "&${key}=${value}";
387                 }
388         }
389         $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
390         return "http://".$config['site']['server'].'/'.$pathpart."index.php?action=${action}${extra}";
391 }
392
393 function common_date_string($dt) {
394         // XXX: do some sexy date formatting
395         // return date(DATE_RFC822, $dt);
396         return $dt;
397 }
398
399 function common_date_w3dtf($dt) {
400         $t = strtotime($dt);
401         return date(DATE_W3C, $t);
402 }
403
404 function common_redirect($url, $code=307) {
405         static $status = array(301 => "Moved Permanently",
406                                                    302 => "Found",
407                                                    303 => "See Other",
408                                                    307 => "Temporary Redirect");
409         header("Status: ${code} $status[$code]");
410         header("Location: $url");
411         common_element('a', array('href' => $url), $url);
412 }
413
414 function common_broadcast_notice($notice) {
415         // XXX: broadcast notices to remote subscribers
416         // XXX: broadcast notices to SMS
417         // XXX: broadcast notices to Jabber
418         // XXX: broadcast notices to other IM
419         // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
420         return true;
421 }
422
423 function common_profile_url($nickname) {
424         return common_local_url('showstream', array('nickname' => $nickname));
425 }
426
427 function common_notice_form() {
428         common_element_start('form', array('id' => 'newnotice', 'method' => 'POST',
429                                                                            'action' => common_local_url('newnotice')));
430         common_textarea('noticecontent', _t('What\'s up?'));
431         common_submit('submit', _t('Send'));
432         common_element_end('form');
433 }
434
435 function common_mint_tag($extra) {
436         global $config;
437         return 
438           'tag:'.$config['tag']['authority'].','.
439           $config['tag']['date'].':'.$config['tag']['prefix'].$extra;
440 }
441
442 // XXX: set up gettext
443
444 function _t($str) {
445         return $str;
446 }