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 if (!defined('LACONICA')) { exit(1) }
22 define('AVATAR_PROFILE_SIZE', 96);
23 define('AVATAR_STREAM_SIZE', 48);
24 define('AVATAR_MINI_SIZE', 24);
25 define('MAX_AVATAR_SIZE', 256 * 1024);
27 # global configuration object
29 // default configuration, overwritten in config.php
33 array('name' => 'Just another µB'),
35 array('directory' => INSTALLDIR . 'files',
38 array('phptype' => 'mysql',
39 'username' => 'stoica',
40 'password' => 'apasswd',
41 'hostspec' => 'localhost',
42 'database' => 'thedb')
45 'portability' => DB_PORTABILITY_ALL));
47 require_once(INSTALLDIR . '/config.php');
48 require_once('DB.php');
52 function common_server_error($msg) {
53 header('Status: 500 Server Error');
54 header('Content-type: text/plain');
61 function common_user_error($msg, $code=200) {
62 common_show_header('Error');
63 common_element('div', array('class' => 'error'), $msg);
67 # Start an HTML element
68 function common_element_start($tag, $attrs=NULL) {
70 if (is_array($attrs)) {
71 foreach ($attrs as $name => $value) {
72 print " $name='$value'";
74 } else if (is_string($attrs)) {
75 print " class='$attrs'";
80 function common_element_end($tag) {
84 function common_element($tag, $attrs=NULL, $content=NULL) {
85 common_element_start($tag, $attrs);
86 if ($content) print htmlspecialchars($content);
87 common_element_end($tag);
90 function common_show_header($pagetitle) {
92 common_element_start('html');
93 common_element_start('head');
94 common_element('title', NULL,
95 $pagetitle . " - " . $config['site']['name']);
96 common_element_end('head');
97 common_element_start('body');
101 function common_show_footer() {
103 common_element_end('body');
104 common_element_end('html');
107 function common_head_menu() {
108 $user = common_current_user();
109 common_element_start('ul', 'headmenu');
110 common_menu_item(common_local_url('doc', array('title' => 'help')),
113 common_menu_item(common_local_url('all', array('nickname' =>
116 common_menu_item(common_local_url('showstream', array('nickname' =>
118 _t('Profile'), $user->fullname || $user->nickname);
119 common_menu_item(common_local_url('profilesettings'),
121 common_menu_item(common_local_url('logout'),
124 common_menu_item(common_local_url('login'),
126 common_menu_item(common_local_url('register'),
129 common_element_end('ul');
132 function common_foot_menu() {
133 common_element_start('ul', 'footmenu');
134 common_menu_item(common_local_url('doc', array('title' => 'about')),
136 common_menu_item(common_local_url('doc', array('title' => 'help')),
138 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
142 function common_menu_item($url, $text, $title=NULL) {
143 $attrs['href'] = $url;
145 $attrs['title'] = $title;
147 common_element_start('li', 'menuitem');
148 common_element('a', $attrs, $text);
149 common_element_end('li');
152 function common_input($id, $label) {
153 common_element('label', array('for' => $id), $label);
154 common_element('input', array('name' => $id,
159 # salted, hashed passwords are stored in the DB
161 function common_munge_password($id, $password) {
162 return md5($id . $password);
165 # check if a username exists and has matching password
166 function common_check_user($nickname, $password) {
167 $user = User::staticGet('nickname', $nickname);
168 if (is_null($user)) {
171 return (0 == strcmp(common_munge_password($password, $user->id),
176 # is the current user logged in?
177 function common_logged_in() {
178 return (!is_null(common_current_user()));
181 function common_have_session() {
182 return (0 != strcmp(session_id(), ''));
185 function common_ensure_session() {
186 if (!common_have_session()) {
191 function common_set_user($nickname) {
192 if (is_null($nickname) && common_have_session()) {
193 unset($_SESSION['userid']);
196 $user = User::staticGet('nickname', $nickname);
198 common_ensure_session();
199 $_SESSION['userid'] = $user->id;
208 # who is the current user?
209 function common_current_user() {
210 static $user = NULL; # FIXME: global memcached
211 if (is_null($user)) {
212 if (common_have_session()) {
213 $id = $_SESSION['userid'];
215 $user = User::staticGet($id);
222 # get canonical version of nickname for comparison
223 function common_canonical_nickname($nickname) {
224 # XXX: UTF-8 canonicalization (like combining chars)
225 return strtolower($nickname);
228 function common_render_content($text) {
232 return htmlspecialchars($text);
235 // where should the avatar go for this user?
237 function common_avatar_filename($user, $extension, $size=NULL) {
241 return $user->id . '-' . $size . $extension;
243 return $user->id . '-original' . $extension;
247 function common_avatar_path($filename) {
249 return $config['avatar']['directory'] . '/' . $filename;
252 function common_avatar_url($filename) {
254 return $config['avatar']['path'] . '/' . $filename;
257 // XXX: set up gettext
259 function _t($str) { $str }