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