]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/util.php
83fbb4a5b0fb19bc558e7b270b393b0cf1f076ea
[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_show_header($pagetitle) {
69         global $config, $xw;
70
71         header('Content-Type: application/xhtml+xml');
72         
73         $xw = new XMLWriter();
74         $xw->openURI('php://output');
75         $xw->startDocument('1.0', 'UTF-8');
76         $xw->writeDTD('html', '-//W3C//DTD XHTML 1.0 Strict//EN',
77                                   'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
78
79         # FIXME: correct language for interface
80         
81         common_element_start('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
82                                                                            'xml:lang' => 'en',
83                                                                            'lang' => 'en'));
84         
85         common_element_start('head');
86         common_element('title', NULL, 
87                                    $pagetitle . " - " . $config['site']['name']);
88         common_element('link', array('rel' => 'stylesheet',
89                                                                  'type' => 'text/css',
90                                                                  'href' => $config['site']['path'] . 'theme/default/style/html.css',
91                                                                  'media' => 'screen, projection, tv'));
92         common_element('link', array('rel' => 'stylesheet',
93                                                                  'type' => 'text/css',
94                                                                  'href' => $config['site']['path'] . 'theme/default/style/layout.css',
95                                                                  'media' => 'screen, projection, tv'));
96         common_element('link', array('rel' => 'stylesheet',
97                                                                  'type' => 'text/css',
98                                                                  'href' => $config['site']['path'] . 'theme/default/style/print.css',
99                                                                  'media' => 'print'));
100         common_element_end('head');
101         common_element_start('body');
102         common_element_start('div', array('id' => 'wrapper'));
103         common_element_start('div', array('id' => 'content'));
104         common_element_start('div', array('id' => 'header'));   
105         common_element('h1', 'title', $pagetitle);
106         common_element('h2', 'subtitle', $config['site']['name']);
107         common_element_end('div');
108         common_head_menu();
109         common_element_start('div', array('id' => 'page'));
110 }
111
112 function common_show_footer() {
113         global $xw, $config;
114         common_element_start('p', 'footer');
115         common_foot_menu();
116         common_license_block();
117         common_element_end('p');
118         common_element_end('div');
119         common_element_end('div');
120         common_element_end('div');
121         common_element_end('body');
122         common_element_end('html');
123         $xw->endDocument();
124         $xw->flush();
125 }
126
127 function common_text($txt) {
128         global $xw;
129         $xw->text($txt);
130 }
131
132 function common_license_block() {
133         global $config, $xw;
134         common_element_start('div', 'license');
135         common_element_start('a', array('class' => 'license',
136                                                                         'rel' => 'license',
137                                                                         href => $config['license']['url']));
138         common_element('img', array('class' => 'license',
139                                                                 'src' => $config['license']['image'],
140                                                                 'alt' => $config['license']['title']));
141         common_element_end('a');
142         common_text(_t('Unless otherwise specified, contents of this site are copyright by the contributors and available under the '));
143         common_element('a', array('class' => 'license',
144                                                           'rel' => 'license',
145                                                           href => $config['license']['url']),
146                                    $config['license']['title']);
147         common_text(_t('. Contributors should be attributed by full name or nickname.'));
148         common_element_end('div');
149 }
150
151 function common_head_menu() {
152         $user = common_current_user();
153         common_element_start('ul', array('id' => 'menu', 'class' => ($user) ? 'five' : 'three'));
154         common_menu_item(common_local_url('doc', array('title' => 'help')),
155                                          _t('Help'));
156         if ($user) {
157                 common_menu_item(common_local_url('all', array('nickname' => 
158                                                                                                            $user->nickname)),
159                                                  _t('Home'));
160                 common_menu_item(common_local_url('showstream', array('nickname' =>
161                                                                                                                           $user->nickname)),
162                                                  _t('Profile'),  $user->fullname || $user->nickname);
163                 common_menu_item(common_local_url('profilesettings'),
164                                                  _t('Settings'));
165                 common_menu_item(common_local_url('logout'),
166                                                  _t('Logout'));
167         } else {
168                 common_menu_item(common_local_url('login'),
169                                                  _t('Login'));
170                 common_menu_item(common_local_url('register'),
171                                                  _t('Register'));
172         }
173         common_element_end('ul');
174 }
175
176 function common_foot_menu() {
177         common_element_start('ul', 'footmenu');
178         common_menu_item(common_local_url('doc', array('title' => 'about')),
179                                          _t('About'));
180         common_menu_item(common_local_url('doc', array('title' => 'help')),
181                                          _t('Help'));
182         common_menu_item(common_local_url('doc', array('title' => 'privacy')),
183                                          _t('Privacy'));
184 }
185
186 function common_menu_item($url, $text, $title=NULL) {
187         $attrs['href'] = $url;
188         if ($title) {
189                 $attrs['title'] = $title;
190         }
191         common_element_start('li', 'menuitem');
192         common_element('a', $attrs, $text);
193         common_element_end('li');
194 }
195
196 function common_input($id, $label, $value=NULL) {
197         common_element_start('p');
198         common_element('label', array('for' => $id), $label);
199         $attrs = array('name' => $id,
200                                    'type' => 'text',
201                                    'id' => $id);
202         if ($value) {
203                 $attrs['value'] = htmlspecialchars($value);
204         }
205         common_element('input', $attrs);
206         comon_element_end('p');
207 }
208
209 function common_password($id, $label) {
210         common_element_start('p');
211         common_element('label', array('for' => $id), $label);
212         $attrs = array('name' => $id,
213                                    'type' => 'password',
214                                    'id' => $id);
215         common_element('input', $attrs);
216         comon_element_end('p');
217 }
218
219 function common_submit($id, $label) {
220         common_element_start('p');
221         common_element('input', array('type' => 'submit',
222                                                                   'id' => $id,
223                                                                   'name' => $id,
224                                                                   'value' => $label));
225         comon_element_end('p');
226 }
227
228 # salted, hashed passwords are stored in the DB
229
230 function common_munge_password($id, $password) {
231         return md5($id . $password);
232 }
233
234 # check if a username exists and has matching password
235 function common_check_user($nickname, $password) {
236         $user = User::staticGet('nickname', $nickname);
237         if (is_null($user)) {
238                 return false;
239         } else {
240                 return (0 == strcmp(common_munge_password($password, $user->id), 
241                                                         $user->password));
242         }
243 }
244
245 # is the current user logged in?
246 function common_logged_in() {
247         return (!is_null(common_current_user()));
248 }
249
250 function common_have_session() {
251         return (0 != strcmp(session_id(), ''));
252 }
253
254 function common_ensure_session() {
255         if (!common_have_session()) {
256                 @session_start();
257         }
258 }
259
260 function common_set_user($nickname) {
261         if (is_null($nickname) && common_have_session()) {
262                 unset($_SESSION['userid']);
263                 return true;
264         } else {
265                 $user = User::staticGet('nickname', $nickname);
266                 if ($user) {
267                         common_ensure_session();
268                         $_SESSION['userid'] = $user->id;
269                         return true;
270                 } else {
271                         return false;
272                 }
273         }
274         return false;
275 }
276
277 # who is the current user?
278 function common_current_user() {
279         static $user = NULL; # FIXME: global memcached
280         if (is_null($user)) {
281                 common_ensure_session();
282                 $id = $_SESSION['userid'];
283                 if ($id) {
284                         $user = User::staticGet($id);
285                 }
286         }
287         return $user;
288 }
289
290 # get canonical version of nickname for comparison
291 function common_canonical_nickname($nickname) {
292         # XXX: UTF-8 canonicalization (like combining chars)
293         return $nickname;
294 }
295
296 # get canonical version of email for comparison
297 function common_canonical_email($email) {
298         # XXX: canonicalize UTF-8
299         # XXX: lcase the domain part
300         return $email;
301 }
302
303 function common_render_content($text) {
304         # XXX: @ messages
305         # XXX: # tags
306         # XXX: machine tags
307         return htmlspecialchars($text);
308 }
309
310 // where should the avatar go for this user?
311
312 function common_avatar_filename($user, $extension, $size=NULL) {
313         global $config;
314
315         if ($size) {
316                 return $user->id . '-' . $size . $extension;
317         } else {
318                 return $user->id . '-original' . $extension;
319         }
320 }
321
322 function common_avatar_path($filename) {
323         global $config;
324         return $config['avatar']['directory'] . '/' . $filename;
325 }
326
327 function common_avatar_url($filename) {
328         global $config;
329         return $config['avatar']['path'] . '/' . $filename;
330 }
331
332 function common_local_url($action, $args=NULL) {
333         global $config;
334         /* XXX: pretty URLs */
335         $extra = '';
336         if ($args) {
337                 foreach ($args as $key => $value) {
338                         $extra .= "&${key}=${value}";
339                 }
340         }
341         $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
342         return "http://".$config['site']['server'].'/'.$pathpart."index.php?action=${action}${extra}";
343 }
344
345 function common_date_string($dt) {
346         // XXX: do some sexy date formatting
347         // return date(DATE_RFC822, $dt);
348         return $dt;
349 }
350
351 function common_redirect($url, $code=307) {
352         static $status = array(301 => "Moved Permanently",
353                                                    302 => "Found",
354                                                    303 => "See Other",
355                                                    307 => "Temporary Redirect");
356         header("Status: ${code} $status[$code]");
357         header("Location: $url");
358         common_element('a', array('href' => $url), $url);
359 }
360
361 function common_broadcast_notices($id) {
362         // XXX: broadcast notices to remote subscribers
363         // XXX: broadcast notices to SMS
364         // XXX: broadcast notices to Jabber
365         // XXX: broadcast notices to other IM
366         // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
367         return true;
368 }
369
370 function common_profile_url($nickname) {
371         return common_local_url('showstream', array('nickname' => $nickname));
372 }
373
374 // XXX: set up gettext
375
376 function _t($str) { 
377         return $str;
378 }