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'),
38 array('directory' => INSTALLDIR . 'files',
41 array('phptype' => 'mysql',
42 'username' => 'stoica',
43 'password' => 'apasswd',
44 'hostspec' => 'localhost',
45 'database' => 'thedb')
48 'portability' => DB_PORTABILITY_ALL));
50 require_once(INSTALLDIR . '/config.php');
51 require_once('DB.php');
55 function common_server_error($msg) {
56 header('Status: 500 Server Error');
57 header('Content-type: text/plain');
64 function common_user_error($msg, $code=200) {
65 common_show_header('Error');
66 common_element('div', array('class' => 'error'), $msg);
70 # Start an HTML element
71 function common_element_start($tag, $attrs=NULL) {
73 if (is_array($attrs)) {
74 foreach ($attrs as $name => $value) {
75 print " $name='$value'";
77 } else if (is_string($attrs)) {
78 print " class='$attrs'";
83 function common_element_end($tag) {
87 function common_element($tag, $attrs=NULL, $content=NULL) {
88 common_element_start($tag, $attrs);
89 if ($content) print htmlspecialchars($content);
90 common_element_end($tag);
93 function common_show_header($pagetitle) {
95 common_element_start('html');
96 common_element_start('head');
97 common_element('title', NULL,
98 $pagetitle . " - " . $config['site']['name']);
99 common_element_end('head');
100 common_element_start('body');
104 function common_show_footer() {
106 common_element_end('body');
107 common_element_end('html');
110 function common_head_menu() {
111 $user = common_current_user();
112 common_element_start('ul', 'headmenu');
113 common_menu_item(common_local_url('doc', array('title' => 'help')),
116 common_menu_item(common_local_url('all', array('nickname' =>
119 common_menu_item(common_local_url('showstream', array('nickname' =>
121 _t('Profile'), $user->fullname || $user->nickname);
122 common_menu_item(common_local_url('profilesettings'),
124 common_menu_item(common_local_url('logout'),
127 common_menu_item(common_local_url('login'),
129 common_menu_item(common_local_url('register'),
132 common_element_end('ul');
135 function common_foot_menu() {
136 common_element_start('ul', 'footmenu');
137 common_menu_item(common_local_url('doc', array('title' => 'about')),
139 common_menu_item(common_local_url('doc', array('title' => 'help')),
141 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
145 function common_menu_item($url, $text, $title=NULL) {
146 $attrs['href'] = $url;
148 $attrs['title'] = $title;
150 common_element_start('li', 'menuitem');
151 common_element('a', $attrs, $text);
152 common_element_end('li');
155 function common_input($id, $label) {
156 common_element('label', array('for' => $id), $label);
157 common_element('input', array('name' => $id,
162 # salted, hashed passwords are stored in the DB
164 function common_munge_password($id, $password) {
165 return md5($id . $password);
168 # check if a username exists and has matching password
169 function common_check_user($nickname, $password) {
170 $user = User::staticGet('nickname', $nickname);
171 if (is_null($user)) {
174 return (0 == strcmp(common_munge_password($password, $user->id),
179 # is the current user logged in?
180 function common_logged_in() {
181 return (!is_null(common_current_user()));
184 function common_have_session() {
185 return (0 != strcmp(session_id(), ''));
188 function common_ensure_session() {
189 if (!common_have_session()) {
194 function common_set_user($nickname) {
195 if (is_null($nickname) && common_have_session()) {
196 unset($_SESSION['userid']);
199 $user = User::staticGet('nickname', $nickname);
201 common_ensure_session();
202 $_SESSION['userid'] = $user->id;
211 # who is the current user?
212 function common_current_user() {
213 static $user = NULL; # FIXME: global memcached
214 if (is_null($user)) {
215 if (common_have_session()) {
216 $id = $_SESSION['userid'];
218 $user = User::staticGet($id);
225 # get canonical version of nickname for comparison
226 function common_canonical_nickname($nickname) {
227 # XXX: UTF-8 canonicalization (like combining chars)
228 return strtolower($nickname);
231 function common_render_content($text) {
235 return htmlspecialchars($text);
238 // where should the avatar go for this user?
240 function common_avatar_filename($user, $extension, $size=NULL) {
244 return $user->id . '-' . $size . $extension;
246 return $user->id . '-original' . $extension;
250 function common_avatar_path($filename) {
252 return $config['avatar']['directory'] . '/' . $filename;
255 function common_avatar_url($filename) {
257 return $config['avatar']['path'] . '/' . $filename;
260 function common_local_url($action, $args) {
261 /* XXX: pretty URLs */
263 foreach ($args as $key => $value) {
264 $extra .= "&${key}=${value}";
266 /* XXX: correct path */
267 return "/index.php?action=${action}${extra}";
270 // XXX: set up gettext
272 function _t($str) { $str }