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