extract textarea code and make bio a textarea
[statusnet:evans-mainline.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->setIndent(true);
76         $xw->startDocument('1.0', 'UTF-8');
77         $xw->writeDTD('html', '-//W3C//DTD XHTML 1.0 Strict//EN',
78                                   'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
79
80         # FIXME: correct language for interface
81
82         common_element_start('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
83                                                                            'xml:lang' => 'en',
84                                                                            'lang' => 'en'));
85
86         common_element_start('head');
87         common_element('title', NULL,
88                                    $pagetitle . " - " . $config['site']['name']);
89         common_element('link', array('rel' => 'stylesheet',
90                                                                  'type' => 'text/css',
91                                                                  'href' => $config['site']['path'] . 'theme/default/style/html.css',
92                                                                  'media' => 'screen, projection, tv'));
93         common_element('link', array('rel' => 'stylesheet',
94                                                                  'type' => 'text/css',
95                                                                  'href' => $config['site']['path'] . 'theme/default/style/layout.css',
96                                                                  'media' => 'screen, projection, tv'));
97         common_element('link', array('rel' => 'stylesheet',
98                                                                  'type' => 'text/css',
99                                                                  'href' => $config['site']['path'] . 'theme/default/style/print.css',
100                                                                  'media' => 'print'));
101         common_element_end('head');
102         common_element_start('body');
103         common_element_start('div', array('id' => 'wrapper'));
104         common_element_start('div', array('id' => 'content'));
105         common_element_start('div', array('id' => 'header'));
106         common_element('h1', 'title', $pagetitle);
107         common_element('h2', 'subtitle', $config['site']['name']);
108         common_element_end('div');
109         common_head_menu();
110         common_element_start('div', array('id' => 'page'));
111 }
112
113 function common_show_footer() {
114         global $xw, $config;
115         common_element_start('div', 'footer');
116         common_foot_menu();
117         common_license_block();
118         common_element_end('div');
119         common_element_end('div');
120         common_element_end('div');
121         common_element_end('div');
122         common_element_end('body');
123         common_element_end('html');
124         $xw->endDocument();
125         $xw->flush();
126 }
127
128 function common_text($txt) {
129         global $xw;
130         $xw->text($txt);
131 }
132
133 function common_license_block() {
134         global $config, $xw;
135         common_element_start('div', 'license');
136         common_element_start('a', array('class' => 'license',
137                                                                         'rel' => 'license',
138                                                                         href => $config['license']['url']));
139         common_element('img', array('class' => 'license',
140                                                                 'src' => $config['license']['image'],
141                                                                 'alt' => $config['license']['title']));
142         common_element_end('a');
143         common_text(_t('Unless otherwise specified, contents of this site are copyright by the contributors and available under the '));
144         common_element('a', array('class' => 'license',
145                                                           'rel' => 'license',
146                                                           href => $config['license']['url']),
147                                    $config['license']['title']);
148         common_text(_t('. Contributors should be attributed by full name or nickname.'));
149         common_element_end('div');
150 }
151
152 function common_head_menu() {
153         $user = common_current_user();
154         common_element_start('ul', array('id' => 'menu', 'class' => ($user) ? 'five' : 'three'));
155         common_menu_item(common_local_url('doc', array('title' => 'help')),
156                                          _t('Help'));
157         if ($user) {
158                 common_menu_item(common_local_url('all', array('nickname' =>
159                                                                                                            $user->nickname)),
160                                                  _t('Home'));
161                 common_menu_item(common_local_url('showstream', array('nickname' =>
162                                                                                                                           $user->nickname)),
163                                                  _t('Profile'),  $user->fullname || $user->nickname);
164                 common_menu_item(common_local_url('profilesettings'),
165                                                  _t('Settings'));
166                 common_menu_item(common_local_url('logout'),
167                                                  _t('Logout'));
168         } else {
169                 common_menu_item(common_local_url('login'),
170                                                  _t('Login'));
171                 common_menu_item(common_local_url('register'),
172                                                  _t('Register'));
173         }
174         common_element_end('ul');
175 }
176
177 function common_foot_menu() {
178         common_element_start('ul', 'footmenu menuish');
179         common_menu_item(common_local_url('doc', array('title' => 'about')),
180                                          _t('About'));
181         common_menu_item(common_local_url('doc', array('title' => 'help')),
182                                          _t('Help'));
183         common_menu_item(common_local_url('doc', array('title' => 'privacy')),
184                                          _t('Privacy'));
185         common_element_end('ul');
186 }
187
188 function common_menu_item($url, $text, $title=NULL) {
189         $attrs['href'] = $url;
190         if ($title) {
191                 $attrs['title'] = $title;
192         }
193         common_element_start('li', 'menuitem');
194         common_element('a', $attrs, $text);
195         common_element_end('li');
196 }
197
198 function common_input($id, $label, $value=NULL) {
199         common_element_start('p');
200         common_element('label', array('for' => $id), $label);
201         $attrs = array('name' => $id,
202                                    'type' => 'text',
203                                    'id' => $id);
204         if ($value) {
205                 $attrs['value'] = htmlspecialchars($value);
206         }
207         common_element('input', $attrs);
208         common_element_end('p');
209 }
210
211 function common_password($id, $label) {
212         common_element_start('p');
213         common_element('label', array('for' => $id), $label);
214         $attrs = array('name' => $id,
215                                    'type' => 'password',
216                                    'id' => $id);
217         common_element('input', $attrs);
218         common_element_end('p');
219 }
220
221 function common_submit($id, $label) {
222         global $xw;
223         common_element_start('p');
224         common_element_start('label', array('for' => $id));
225         $xw->writeRaw('&nbsp;');
226         common_element_end('label');
227         common_element('input', array('type' => 'submit',
228                                                                   'id' => $id,
229                                                                   'name' => $id,
230                                                                   'value' => $label,
231                                                                   'class' => 'button'));
232         common_element_end('p');
233 }
234
235 function common_textarea($id, $label, $content=NULL) {
236         common_element_start('p');
237         common_element('label', array('for' => $id), $label);
238         common_element('textarea', array('rows' => 3, 'cols' => 40,
239                                                                          'name' => $id,
240                                                                          'id' => $id, 
241                                                                          'class' => 'width50'),
242                                    ($content) ? $content : ' ');
243         common_element_end('p');
244 }
245
246 # salted, hashed passwords are stored in the DB
247
248 function common_munge_password($id, $password) {
249         return md5($id . $password);
250 }
251
252 # check if a username exists and has matching password
253 function common_check_user($nickname, $password) {
254         $user = User::staticGet('nickname', $nickname);
255         if (is_null($user)) {
256                 return false;
257         } else {
258                 return (0 == strcmp(common_munge_password($password, $user->id),
259                                                         $user->password));
260         }
261 }
262
263 # is the current user logged in?
264 function common_logged_in() {
265         return (!is_null(common_current_user()));
266 }
267
268 function common_have_session() {
269         return (0 != strcmp(session_id(), ''));
270 }
271
272 function common_ensure_session() {
273         if (!common_have_session()) {
274                 @session_start();
275         }
276 }
277
278 function common_set_user($nickname) {
279         if (is_null($nickname) && common_have_session()) {
280                 unset($_SESSION['userid']);
281                 return true;
282         } else {
283                 $user = User::staticGet('nickname', $nickname);
284                 if ($user) {
285                         common_ensure_session();
286                         $_SESSION['userid'] = $user->id;
287                         return true;
288                 } else {
289                         return false;
290                 }
291         }
292         return false;
293 }
294
295 # who is the current user?
296 function common_current_user() {
297         static $user = NULL; # FIXME: global memcached
298         if (is_null($user)) {
299                 common_ensure_session();
300                 $id = $_SESSION['userid'];
301                 if ($id) {
302                         $user = User::staticGet($id);
303                 }
304         }
305         return $user;
306 }
307
308 # get canonical version of nickname for comparison
309 function common_canonical_nickname($nickname) {
310         # XXX: UTF-8 canonicalization (like combining chars)
311         return $nickname;
312 }
313
314 # get canonical version of email for comparison
315 function common_canonical_email($email) {
316         # XXX: canonicalize UTF-8
317         # XXX: lcase the domain part
318         return $email;
319 }
320
321 function common_render_content($text) {
322         # XXX: @ messages
323         # XXX: # tags
324         # XXX: machine tags
325         return htmlspecialchars($text);
326 }
327
328 // where should the avatar go for this user?
329
330 function common_avatar_filename($user, $extension, $size=NULL) {
331         global $config;
332
333         if ($size) {
334                 return $user->id . '-' . $size . $extension;
335         } else {
336                 return $user->id . '-original' . $extension;
337         }
338 }
339
340 function common_avatar_path($filename) {
341         global $config;
342         return $config['avatar']['directory'] . '/' . $filename;
343 }
344
345 function common_avatar_url($filename) {
346         global $config;
347         return $config['avatar']['path'] . '/' . $filename;
348 }
349
350 function common_local_url($action, $args=NULL) {
351         global $config;
352         /* XXX: pretty URLs */
353         $extra = '';
354         if ($args) {
355                 foreach ($args as $key => $value) {
356                         $extra .= "&${key}=${value}";
357                 }
358         }
359         $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
360         return "http://".$config['site']['server'].'/'.$pathpart."index.php?action=${action}${extra}";
361 }
362
363 function common_date_string($dt) {
364         // XXX: do some sexy date formatting
365         // return date(DATE_RFC822, $dt);
366         return $dt;
367 }
368
369 function common_redirect($url, $code=307) {
370         static $status = array(301 => "Moved Permanently",
371                                                    302 => "Found",
372                                                    303 => "See Other",
373                                                    307 => "Temporary Redirect");
374         header("Status: ${code} $status[$code]");
375         header("Location: $url");
376         common_element('a', array('href' => $url), $url);
377 }
378
379 function common_broadcast_notices($id) {
380         // XXX: broadcast notices to remote subscribers
381         // XXX: broadcast notices to SMS
382         // XXX: broadcast notices to Jabber
383         // XXX: broadcast notices to other IM
384         // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
385         return true;
386 }
387
388 function common_profile_url($nickname) {
389         return common_local_url('showstream', array('nickname' => $nickname));
390 }
391
392 function common_notice_form() {
393         common_element_start('form', array('id' => 'newnotice', 'method' => 'POST',
394                                                                            'action' => common_local_url('newnotice')));
395         common_textarea('content', _t('What\'s up?'));
396         common_submit('submit', _t('Send'));
397         common_element_end('form');
398 }
399
400 // XXX: set up gettext
401
402 function _t($str) {
403         return $str;
404 }