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