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