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