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 # global configuration object
24 // default configuration, overwritten in config.php
28 array('name' => 'Just another µB'),
30 array('phptype' => 'mysql',
31 'username' => 'stoica',
32 'password' => 'apasswd',
33 'hostspec' => 'localhost',
34 'database' => 'thedb')
37 'portability' => DB_PORTABILITY_ALL));
39 require_once(INSTALLDIR . '/config.php');
40 require_once('DB.php');
44 function common_server_error($msg) {
45 header('Status: 500 Server Error');
46 header('Content-type: text/plain');
53 function common_user_error($msg, $code=200) {
54 common_show_header('Error');
55 common_element('div', array('class' => 'error'), $msg);
59 # Start an HTML element
60 function common_element_start($tag, $attrs=NULL) {
62 if (is_array($attrs)) {
63 foreach ($attrs as $name => $value) {
64 print " $name='$value'";
66 } else if (is_string($attrs)) {
67 print " class='$attrs'";
72 function common_element_end($tag) {
76 function common_element($tag, $attrs=NULL, $content=NULL) {
77 common_element_start($tag, $attrs);
78 if ($content) print htmlspecialchars($content);
79 common_element_end($tag);
82 function common_show_header($pagetitle) {
84 common_element_start('html');
85 common_element_start('head');
86 common_element('title', NULL,
87 $pagetitle . " - " . $config['site']['name']);
88 common_element_end('head');
89 common_element_start('body');
93 function common_show_footer() {
95 common_element_end('body');
96 common_element_end('html');
99 function common_head_menu() {
100 $user = common_current_user();
101 common_element_start('ul', 'headmenu');
102 common_menu_item(common_local_url('doc', array('title' => 'help')),
105 common_menu_item(common_local_url('all', array('nickname' =>
108 common_menu_item(common_local_url('showstream', array('nickname' =>
110 _t('Profile'), $user->fullname || $user->nickname);
111 common_menu_item(common_local_url('settings'),
113 common_menu_item(common_local_url('logout'),
116 common_menu_item(common_local_url('login'),
118 common_menu_item(common_local_url('register'),
121 common_element_end('ul');
124 function common_foot_menu() {
125 common_element_start('ul', 'footmenu');
126 common_menu_item(common_local_url('doc', array('title' => 'about')),
128 common_menu_item(common_local_url('doc', array('title' => 'help')),
130 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
134 function common_menu_item($url, $text, $title=NULL) {
135 $attrs['href'] = $url;
137 $attrs['title'] = $title;
139 common_element_start('li', 'menuitem');
140 common_element('a', $attrs, $text);
141 common_element_end('li');
144 # salted, hashed passwords are stored in the DB
146 function common_munge_password($id, $password) {
147 return md5($id . $password);
150 # check if a username exists and has matching password
151 function common_check_user($nickname, $password) {
152 $user = User::staticGet('nickname', $nickname);
153 if (is_null($user)) {
156 return (0 == strcmp(common_munge_password($password, $user->id),
161 # is the current user logged in?
162 function common_logged_in() {
163 return (!is_null(common_current_user()));
166 function common_have_session() {
167 return (0 != strcmp(session_id(), ''));
170 function common_ensure_session() {
171 if (!common_have_session()) {
176 function common_set_user($nickname) {
177 if (is_null($nickname) && common_have_session()) {
178 unset($_SESSION['userid']);
181 $user = User::staticGet('nickname', $nickname);
183 common_ensure_session();
184 $_SESSION['userid'] = $user->id;
193 # who is the current user?
194 function common_current_user() {
195 static $user = NULL; # FIXME: global memcached
196 if (is_null($user)) {
197 if (common_have_session()) {
198 $id = $_SESSION['userid'];
200 $user = User::staticGet($id);
207 # get canonical version of nickname for comparison
208 function common_canonical_nickname($nickname) {
209 # XXX: UTF-8 canonicalization (like combining chars)
210 return strtolower($nickname);
213 function common_render_content($text) {
217 return htmlspecialchars($text);
220 // XXX: set up gettext
222 function _t($str) { $str }