3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 /* XXX: break up into separate modules (HTTP, HTML, user, files) */
24 function common_server_error($msg) {
25 header('HTTP/1.1 500 Server Error');
26 header('Content-type: text/plain');
34 function common_user_error($msg, $code=200) {
35 common_show_header('Error');
36 common_element('div', array('class' => 'error'), $msg);
42 # Start an HTML element
43 function common_element_start($tag, $attrs=NULL) {
45 $xw->startElement($tag);
46 if (is_array($attrs)) {
47 foreach ($attrs as $name => $value) {
48 $xw->writeAttribute($name, $value);
50 } else if (is_string($attrs)) {
51 $xw->writeAttribute('class', $attrs);
55 function common_element_end($tag) {
60 function common_element($tag, $attrs=NULL, $content=NULL) {
61 common_element_start($tag, $attrs);
66 common_element_end($tag);
69 function common_start_xml($doc=NULL, $public=NULL, $system=NULL) {
71 $xw = new XMLWriter();
72 $xw->openURI('php://output');
74 $xw->startDocument('1.0', 'UTF-8');
76 $xw->writeDTD($doc, $public, $system);
80 function common_end_xml() {
86 function common_show_header($pagetitle, $callable=NULL, $data=NULL) {
89 header('Content-Type: application/xhtml+xml');
91 common_start_xml('html',
92 '-//W3C//DTD XHTML 1.0 Strict//EN',
93 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
95 # FIXME: correct language for interface
97 common_element_start('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
101 common_element_start('head');
102 common_element('title', NULL,
103 $pagetitle . " - " . $config['site']['name']);
104 common_element('link', array('rel' => 'stylesheet',
105 'type' => 'text/css',
106 'href' => common_path('theme/default/style/html.css'),
107 'media' => 'screen, projection, tv'));
108 common_element('link', array('rel' => 'stylesheet',
109 'type' => 'text/css',
110 'href' => common_path('theme/default/style/layout.css'),
111 'media' => 'screen, projection, tv'));
112 common_element('link', array('rel' => 'stylesheet',
113 'type' => 'text/css',
114 'href' => common_path('theme/default/style/print.css'),
115 'media' => 'print'));
118 call_user_func($callable, $data);
120 call_user_func($callable);
123 common_element_end('head');
124 common_element_start('body');
125 common_element_start('div', array('id' => 'wrapper'));
126 common_element_start('div', array('id' => 'content'));
127 common_element_start('div', array('id' => 'header'));
128 common_element('h1', 'title', $pagetitle);
129 common_element('h2', 'subtitle', $config['site']['name']);
130 common_element_end('div');
132 common_element_start('div', array('id' => 'page'));
135 function common_show_footer() {
137 common_element_start('div', 'footer');
139 common_license_block();
140 common_element_end('div');
141 common_element_end('div');
142 common_element_end('div');
143 common_element_end('div');
144 common_element_end('body');
145 common_element_end('html');
149 function common_text($txt) {
154 function common_raw($xml) {
159 function common_license_block() {
161 common_element_start('p', 'license greenBg');
162 common_element_start('span', 'floatLeft width25');
163 common_element_start('a', array('class' => 'license',
165 href => $config['license']['url']));
166 common_element('img', array('class' => 'license',
167 'src' => $config['license']['image'],
168 'alt' => $config['license']['title']));
169 common_element_end('a');
170 common_element_end('span');
171 common_element_start('span', 'floatRight width75');
172 common_text(_t('Unless otherwise specified, contents of this site are copyright by the contributors and available under the '));
173 common_element('a', array('class' => 'license',
175 href => $config['license']['url']),
176 $config['license']['title']);
177 common_text(_t('. Contributors should be attributed by full name or nickname.'));
178 common_element_end('span');
179 common_element_end('p');
182 function common_head_menu() {
183 $user = common_current_user();
184 common_element_start('ul', array('id' => 'menu', 'class' => ($user) ? 'five' : 'three'));
185 common_menu_item(common_local_url('public'), _t('Public'));
187 common_menu_item(common_local_url('all', array('nickname' =>
190 common_menu_item(common_local_url('showstream', array('nickname' =>
192 _t('Profile'), $user->fullname || $user->nickname);
193 common_menu_item(common_local_url('profilesettings'),
195 common_menu_item(common_local_url('logout'),
198 common_menu_item(common_local_url('login'),
200 common_menu_item(common_local_url('register'),
203 common_element_end('ul');
206 function common_foot_menu() {
207 common_element_start('ul', 'footmenu menuish');
208 common_menu_item(common_local_url('doc', array('title' => 'about')),
210 common_menu_item(common_local_url('doc', array('title' => 'help')),
212 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
214 common_menu_item(common_local_url('doc', array('title' => 'source')),
216 common_element_end('ul');
219 function common_menu_item($url, $text, $title=NULL) {
220 $attrs['href'] = $url;
222 $attrs['title'] = $title;
224 common_element_start('li', 'menuitem');
225 common_element('a', $attrs, $text);
226 common_element_end('li');
229 function common_input($id, $label, $value=NULL) {
230 common_element_start('p');
231 common_element('label', array('for' => $id), $label);
232 $attrs = array('name' => $id,
236 $attrs['value'] = htmlspecialchars($value);
238 common_element('input', $attrs);
239 common_element_end('p');
242 function common_hidden($id, $value) {
243 common_element('input', array('name' => $id,
249 function common_password($id, $label) {
250 common_element_start('p');
251 common_element('label', array('for' => $id), $label);
252 $attrs = array('name' => $id,
253 'type' => 'password',
255 common_element('input', $attrs);
256 common_element_end('p');
259 function common_submit($id, $label) {
261 common_element_start('p');
262 common_element_start('label', array('for' => $id));
263 $xw->writeRaw(' ');
264 common_element_end('label');
265 common_element('input', array('type' => 'submit',
269 'class' => 'button'));
270 common_element_end('p');
273 function common_textarea($id, $label, $content=NULL) {
274 common_element_start('p');
275 common_element('label', array('for' => $id), $label);
276 common_element('textarea', array('rows' => 3,
280 'class' => 'width50'),
281 ($content) ? $content : ' ');
282 common_element_end('p');
285 # salted, hashed passwords are stored in the DB
287 function common_munge_password($id, $password) {
288 return md5($id . $password);
291 # check if a username exists and has matching password
292 function common_check_user($nickname, $password) {
293 $user = User::staticGet('nickname', $nickname);
294 if (is_null($user)) {
297 return (0 == strcmp(common_munge_password($password, $user->id),
302 # is the current user logged in?
303 function common_logged_in() {
304 return (!is_null(common_current_user()));
307 function common_have_session() {
308 return (0 != strcmp(session_id(), ''));
311 function common_ensure_session() {
312 if (!common_have_session()) {
317 function common_set_user($nickname) {
318 if (is_null($nickname) && common_have_session()) {
319 unset($_SESSION['userid']);
322 $user = User::staticGet('nickname', $nickname);
324 common_ensure_session();
325 $_SESSION['userid'] = $user->id;
334 # who is the current user?
335 function common_current_user() {
336 static $user = NULL; # FIXME: global memcached
337 if (is_null($user)) {
338 common_ensure_session();
339 $id = $_SESSION['userid'];
341 $user = User::staticGet($id);
347 # get canonical version of nickname for comparison
348 function common_canonical_nickname($nickname) {
349 # XXX: UTF-8 canonicalization (like combining chars)
353 # get canonical version of email for comparison
354 function common_canonical_email($email) {
355 # XXX: canonicalize UTF-8
356 # XXX: lcase the domain part
360 define('URL_REGEX', '^|[ \t\r\n])((ftp|http|https|gopher|mailto|news|nntp|telnet|wais|file|prospero|aim|webcal):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))');
362 function common_render_content($text, $notice) {
363 $r = htmlspecialchars($text);
364 $id = $notice->profile_id;
365 $r = preg_replace('@https?://\S+@', '<a href="\0" class="extlink">\0</a>', $r);
366 $r = preg_replace('/(^|\b)@([\w-]+)($|\b)/e', "'\\1@'.common_at_link($id, '\\2').'\\3'", $r);
372 function common_at_link($sender_id, $nickname) {
373 # Try to find profiles this profile is subscribed to that have this nickname
374 $recipient = new Profile();
375 # XXX: chokety and bad
376 $recipient->whereAdd('EXISTS (SELECT subscribed from subscription where subscriber = '.$sender_id.' and subscribed = id)', 'AND');
377 $recipient->whereAdd('nickname = "' . trim($nickname) . '"', 'AND');
378 if ($recipient->find(TRUE)) {
379 return '<a href="'.htmlspecialchars($recipient->profileurl).'" class="atlink tolistenee">'.$nickname.'</a>';
381 # Try to find profiles that listen to this profile and that have this nickname
382 $recipient = new Profile();
383 # XXX: chokety and bad
384 $recipient->whereAdd('EXISTS (SELECT subscriber from subscription where subscribed = '.$sender_id.' and subscriber = id)', 'AND');
385 $recipient->whereAdd('nickname = "' . trim($nickname) . '"', 'AND');
386 if ($recipient->find(TRUE)) {
387 return '<a href="'.htmlspecialchars($recipient->profileurl).'" class="atlink tolistener">'.$nickname.'</a>';
389 # If this is a local user, try to find a local user with that nickname.
390 $sender = User::staticGet($sender_id);
392 $recipient_user = User::staticGet('nickname', $nickname);
393 if ($recipient_user) {
394 $recipient = $recipient->getProfile();
395 return '<a href="'.htmlspecialchars($recipient->profileurl).'" class="atlink usertouser">'.$nickname.'</a>';
398 # Otherwise, no links. @messages from local users to remote users,
399 # or from remote users to other remote users, are just
400 # outside our ability to make intelligent guesses about
404 // where should the avatar go for this user?
406 function common_avatar_filename($user, $extension, $size=NULL, $extra=NULL) {
410 return $user->id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
412 return $user->id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
416 function common_avatar_path($filename) {
418 return INSTALLDIR . '/avatar/' . $filename;
421 function common_avatar_url($filename) {
422 return common_path('avatar/'.$filename);
425 function common_default_avatar($size) {
426 static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
427 AVATAR_STREAM_SIZE => 'stream',
428 AVATAR_MINI_SIZE => 'mini');
431 return common_path($config['avatar']['default'][$sizenames[$size]]);
434 function common_local_url($action, $args=NULL) {
436 if ($config['site']['fancy']) {
437 return common_fancy_url($action, $args);
439 return common_simple_url($action, $args);
443 function common_fancy_url($action, $args=NULL) {
444 switch (strtolower($action)) {
446 return common_simple_url($action, $args);
450 function common_simple_url($action, $args=NULL) {
452 /* XXX: pretty URLs */
455 foreach ($args as $key => $value) {
456 $extra .= "&${key}=${value}";
459 return common_path("index.php?action=${action}${extra}");
462 function common_path($relative) {
464 $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
465 return "http://".$config['site']['server'].'/'.$pathpart.$relative;
468 function common_date_string($dt) {
469 // XXX: do some sexy date formatting
470 // return date(DATE_RFC822, $dt);
474 function common_date_w3dtf($dt) {
476 return date(DATE_W3C, $t);
479 function common_redirect($url, $code=307) {
480 static $status = array(301 => "Moved Permanently",
483 307 => "Temporary Redirect");
484 header("Status: ${code} $status[$code]");
485 header("Location: $url");
486 common_element('a', array('href' => $url), $url);
489 function common_broadcast_notice($notice) {
490 // XXX: broadcast notices to remote subscribers
491 // XXX: broadcast notices to SMS
492 // XXX: broadcast notices to Jabber
493 // XXX: broadcast notices to other IM
494 // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
498 function common_profile_url($nickname) {
499 return common_local_url('showstream', array('nickname' => $nickname));
502 function common_notice_form() {
503 common_element_start('form', array('id' => 'newnotice', 'method' => 'POST',
504 'action' => common_local_url('newnotice')));
505 common_textarea('noticecontent', _t('What\'s up?'));
506 common_submit('submit', _t('Send'));
507 common_element_end('form');
510 function common_mint_tag($extra) {
513 'tag:'.$config['tag']['authority'].','.
514 $config['tag']['date'].':'.$config['tag']['prefix'].$extra;
517 # Should make up a reasonable root URL
519 function common_root_url() {
520 return common_path('');
523 # returns $bytes bytes of random data as a hexadecimal string
524 # "good" here is a goal and not a guarantee
526 function common_good_rand($bytes) {
527 # XXX: use random.org...?
528 if (file_exists('/dev/urandom')) {
529 return common_urandom($bytes);
530 } else { # FIXME: this is probably not good enough
531 return common_mtrand($bytes);
535 function common_urandom($bytes) {
536 $h = fopen('/dev/urandom', 'rb');
538 $src = fread($h, $bytes);
541 for ($i = 0; $i < $bytes; $i++) {
542 $enc .= sprintf("%02x", (ord($src[$i])));
547 function common_mtrand($bytes) {
549 for ($i = 0; $i < $bytes; $i++) {
550 $enc .= sprintf("%02x", mt_rand(0, 255));
555 function common_set_returnto($url) {
556 common_ensure_session();
557 $_SESSION['returnto'] = $url;
560 function common_get_returnto() {
561 common_ensure_session();
562 return $_SESSION['returnto'];
565 function common_timestamp() {
566 return date('YmdHis');
569 // XXX: set up gettext
575 function common_ensure_syslog() {
576 static $initialized = false;
579 define_syslog_variables();
580 openlog($config['site']['appname'], 0, LOG_USER);
585 function common_log($priority, $msg, $filename=NULL) {
586 common_ensure_syslog();
588 syslog($priority, basename($filename).' - '.$msg);
592 function common_debug($msg, $filename=NULL) {
593 common_log(LOG_DEBUG, $msg, $filename);
596 function common_valid_http_url($url) {
597 return Validate::uri($url, array('allowed_schemes' => array('http', 'https')));