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) */
23 if (!defined('LACONICA')) { exit(1) }
25 define('AVATAR_PROFILE_SIZE', 96);
26 define('AVATAR_STREAM_SIZE', 48);
27 define('AVATAR_MINI_SIZE', 24);
28 define('MAX_AVATAR_SIZE', 256 * 1024);
30 # global configuration object
32 // default configuration, overwritten in config.php
36 array('name' => 'Just another µB',
37 'server' => 'localhost',
40 array('directory' => INSTALLDIR . 'files',
43 array('phptype' => 'mysql',
44 'username' => 'stoica',
45 'password' => 'apasswd',
46 'hostspec' => 'localhost',
47 'database' => 'thedb')
50 'portability' => DB_PORTABILITY_ALL));
52 require_once(INSTALLDIR . '/config.php');
53 require_once('DB.php');
57 function common_server_error($msg) {
58 header('Status: 500 Server Error');
59 header('Content-type: text/plain');
66 function common_user_error($msg, $code=200) {
67 common_show_header('Error');
68 common_element('div', array('class' => 'error'), $msg);
72 # Start an HTML element
73 function common_element_start($tag, $attrs=NULL) {
75 if (is_array($attrs)) {
76 foreach ($attrs as $name => $value) {
77 print " $name='$value'";
79 } else if (is_string($attrs)) {
80 print " class='$attrs'";
85 function common_element_end($tag) {
89 function common_element($tag, $attrs=NULL, $content=NULL) {
90 common_element_start($tag, $attrs);
91 if ($content) print htmlspecialchars($content);
92 common_element_end($tag);
95 function common_show_header($pagetitle) {
97 common_element_start('html');
98 common_element_start('head');
99 common_element('title', NULL,
100 $pagetitle . " - " . $config['site']['name']);
101 common_element_end('head');
102 common_element_start('body');
106 function common_show_footer() {
108 common_element_end('body');
109 common_element_end('html');
112 function common_head_menu() {
113 $user = common_current_user();
114 common_element_start('ul', 'headmenu');
115 common_menu_item(common_local_url('doc', array('title' => 'help')),
118 common_menu_item(common_local_url('all', array('nickname' =>
121 common_menu_item(common_local_url('showstream', array('nickname' =>
123 _t('Profile'), $user->fullname || $user->nickname);
124 common_menu_item(common_local_url('profilesettings'),
126 common_menu_item(common_local_url('logout'),
129 common_menu_item(common_local_url('login'),
131 common_menu_item(common_local_url('register'),
134 common_element_end('ul');
137 function common_foot_menu() {
138 common_element_start('ul', 'footmenu');
139 common_menu_item(common_local_url('doc', array('title' => 'about')),
141 common_menu_item(common_local_url('doc', array('title' => 'help')),
143 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
147 function common_menu_item($url, $text, $title=NULL) {
148 $attrs['href'] = $url;
150 $attrs['title'] = $title;
152 common_element_start('li', 'menuitem');
153 common_element('a', $attrs, $text);
154 common_element_end('li');
157 function common_input($id, $label) {
158 common_element('label', array('for' => $id), $label);
159 common_element('input', array('name' => $id,
164 # salted, hashed passwords are stored in the DB
166 function common_munge_password($id, $password) {
167 return md5($id . $password);
170 # check if a username exists and has matching password
171 function common_check_user($nickname, $password) {
172 $user = User::staticGet('nickname', $nickname);
173 if (is_null($user)) {
176 return (0 == strcmp(common_munge_password($password, $user->id),
181 # is the current user logged in?
182 function common_logged_in() {
183 return (!is_null(common_current_user()));
186 function common_have_session() {
187 return (0 != strcmp(session_id(), ''));
190 function common_ensure_session() {
191 if (!common_have_session()) {
196 function common_set_user($nickname) {
197 if (is_null($nickname) && common_have_session()) {
198 unset($_SESSION['userid']);
201 $user = User::staticGet('nickname', $nickname);
203 common_ensure_session();
204 $_SESSION['userid'] = $user->id;
213 # who is the current user?
214 function common_current_user() {
215 static $user = NULL; # FIXME: global memcached
216 if (is_null($user)) {
217 if (common_have_session()) {
218 $id = $_SESSION['userid'];
220 $user = User::staticGet($id);
227 # get canonical version of nickname for comparison
228 function common_canonical_nickname($nickname) {
229 # XXX: UTF-8 canonicalization (like combining chars)
230 return strtolower($nickname);
233 function common_render_content($text) {
237 return htmlspecialchars($text);
240 // where should the avatar go for this user?
242 function common_avatar_filename($user, $extension, $size=NULL) {
246 return $user->id . '-' . $size . $extension;
248 return $user->id . '-original' . $extension;
252 function common_avatar_path($filename) {
254 return $config['avatar']['directory'] . '/' . $filename;
257 function common_avatar_url($filename) {
259 return $config['avatar']['path'] . '/' . $filename;
262 function common_local_url($action, $args) {
264 /* XXX: pretty URLs */
266 foreach ($args as $key => $value) {
267 $extra .= "&${key}=${value}";
269 return "http://".$config['site']['server'].'/'.$config['site']['path']."/index.php?action=${action}${extra}";
272 function commmon_date_string($dt) {
273 // XXX: do some sexy date formatting
274 return date(DATE_RFC822);
277 function common_redirect($url, $code=307) {
278 static $status = (301 => "Moved Permanently",
281 307 => "Temporary Redirect");
282 header("Status: ${code} $status[$code]");
283 header("Location: $url");
284 common_element('a', array('href' => $url), $url);
287 function common_broadcast_notices($id) {
288 // XXX: broadcast notices to remote subscribers
289 // XXX: broadcast notices to SMS
290 // XXX: broadcast notices to Jabber
291 // XXX: broadcast notices to other IM
292 // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
296 // XXX: set up gettext
298 function _t($str) { $str }