]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/util.php
make the code use the default style
[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('label', array('for' => $id), $label);
198         $attrs = array('name' => $id,
199                                    'type' => 'text',
200                                    'id' => $id);
201         if ($value) {
202                 $attrs['value'] = htmlspecialchars($value);
203         }
204         common_element('input', $attrs);
205 }
206
207 function common_password($id, $label) {
208         common_element('label', array('for' => $id), $label);
209         $attrs = array('name' => $id,
210                                    'type' => 'password',
211                                    'id' => $id);
212         common_element('input', $attrs);
213 }
214
215 # salted, hashed passwords are stored in the DB
216
217 function common_munge_password($id, $password) {
218         return md5($id . $password);
219 }
220
221 # check if a username exists and has matching password
222 function common_check_user($nickname, $password) {
223         $user = User::staticGet('nickname', $nickname);
224         if (is_null($user)) {
225                 return false;
226         } else {
227                 return (0 == strcmp(common_munge_password($password, $user->id), 
228                                                         $user->password));
229         }
230 }
231
232 # is the current user logged in?
233 function common_logged_in() {
234         return (!is_null(common_current_user()));
235 }
236
237 function common_have_session() {
238         return (0 != strcmp(session_id(), ''));
239 }
240
241 function common_ensure_session() {
242         if (!common_have_session()) {
243                 @session_start();
244         }
245 }
246
247 function common_set_user($nickname) {
248         if (is_null($nickname) && common_have_session()) {
249                 unset($_SESSION['userid']);
250                 return true;
251         } else {
252                 $user = User::staticGet('nickname', $nickname);
253                 if ($user) {
254                         common_ensure_session();
255                         $_SESSION['userid'] = $user->id;
256                         return true;
257                 } else {
258                         return false;
259                 }
260         }
261         return false;
262 }
263
264 # who is the current user?
265 function common_current_user() {
266         static $user = NULL; # FIXME: global memcached
267         if (is_null($user)) {
268                 common_ensure_session();
269                 $id = $_SESSION['userid'];
270                 if ($id) {
271                         $user = User::staticGet($id);
272                 }
273         }
274         return $user;
275 }
276
277 # get canonical version of nickname for comparison
278 function common_canonical_nickname($nickname) {
279         # XXX: UTF-8 canonicalization (like combining chars)
280         return $nickname;
281 }
282
283 # get canonical version of email for comparison
284 function common_canonical_email($email) {
285         # XXX: canonicalize UTF-8
286         # XXX: lcase the domain part
287         return $email;
288 }
289
290 function common_render_content($text) {
291         # XXX: @ messages
292         # XXX: # tags
293         # XXX: machine tags
294         return htmlspecialchars($text);
295 }
296
297 // where should the avatar go for this user?
298
299 function common_avatar_filename($user, $extension, $size=NULL) {
300         global $config;
301
302         if ($size) {
303                 return $user->id . '-' . $size . $extension;
304         } else {
305                 return $user->id . '-original' . $extension;
306         }
307 }
308
309 function common_avatar_path($filename) {
310         global $config;
311         return $config['avatar']['directory'] . '/' . $filename;
312 }
313
314 function common_avatar_url($filename) {
315         global $config;
316         return $config['avatar']['path'] . '/' . $filename;
317 }
318
319 function common_local_url($action, $args=NULL) {
320         global $config;
321         /* XXX: pretty URLs */
322         $extra = '';
323         if ($args) {
324                 foreach ($args as $key => $value) {
325                         $extra .= "&${key}=${value}";
326                 }
327         }
328         $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
329         return "http://".$config['site']['server'].'/'.$pathpart."index.php?action=${action}${extra}";
330 }
331
332 function common_date_string($dt) {
333         // XXX: do some sexy date formatting
334         // return date(DATE_RFC822, $dt);
335         return $dt;
336 }
337
338 function common_redirect($url, $code=307) {
339         static $status = array(301 => "Moved Permanently",
340                                                    302 => "Found",
341                                                    303 => "See Other",
342                                                    307 => "Temporary Redirect");
343         header("Status: ${code} $status[$code]");
344         header("Location: $url");
345         common_element('a', array('href' => $url), $url);
346 }
347
348 function common_broadcast_notices($id) {
349         // XXX: broadcast notices to remote subscribers
350         // XXX: broadcast notices to SMS
351         // XXX: broadcast notices to Jabber
352         // XXX: broadcast notices to other IM
353         // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
354         return true;
355 }
356
357 function common_profile_url($nickname) {
358         return common_local_url('showstream', array('nickname' => $nickname));
359 }
360
361 // XXX: set up gettext
362
363 function _t($str) { 
364         return $str;
365 }