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