3 if (!defined('MICROBLOG')) { exit(1) }
5 # global configuration object
7 // default configuration, overwritten in config.php
11 array('name' => 'Just another µB'),
13 array('phptype' => 'mysql',
14 'username' => 'stoica',
15 'password' => 'apasswd',
16 'hostspec' => 'localhost',
17 'database' => 'thedb')
20 'portability' => DB_PORTABILITY_ALL));
22 require_once(INSTALLDIR . '/config.php');
23 require_once('DB.php');
27 function common_server_error($msg) {
28 header('Status: 500 Server Error');
29 header('Content-type: text/plain');
36 function common_user_error($msg, $code=200) {
37 common_show_header('Error');
38 common_element('div', array('class' => 'error'), $msg);
42 # Start an HTML element
43 function common_element_start($tag, $attrs=NULL) {
45 if (is_array($attrs)) {
46 foreach ($attrs as $name => $value) {
47 print " $name='$value'";
49 } else if (is_string($attrs)) {
50 print " class='$attrs'";
55 function common_element_end($tag) {
59 function common_element($tag, $attrs=NULL, $content=NULL) {
60 common_element_start($tag, $attrs);
61 if ($content) print htmlspecialchars($content);
62 common_element_end($tag);
65 function common_show_header($pagetitle) {
67 common_element_start('html');
68 common_element_start('head');
69 common_element('title', NULL,
70 $pagetitle . " - " . $config['site']['name']);
71 common_element_end('head');
72 common_element_start('body');
76 function common_show_footer() {
78 common_element_end('body');
79 common_element_end('html');
82 function common_head_menu() {
83 $user = common_current_user();
84 common_element_start('ul', 'headmenu');
85 common_menu_item(common_local_url('doc', array('title' => 'help')),
88 common_menu_item(common_local_url('all', array('nickname' =>
91 common_menu_item(common_local_url('showstream', array('nickname' =>
93 _t('Profile'), $user->fullname || $user->nickname);
94 common_menu_item(common_local_url('settings'),
96 common_menu_item(common_local_url('logout'),
99 common_menu_item(common_local_url('login'),
101 common_menu_item(common_local_url('register'),
104 common_element_end('ul');
107 function common_foot_menu() {
108 common_element_start('ul', 'footmenu');
109 common_menu_item(common_local_url('doc', array('title' => 'about')),
111 common_menu_item(common_local_url('doc', array('title' => 'help')),
113 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
117 function common_menu_item($url, $text, $title=NULL) {
118 $attrs['href'] = $url;
120 $attrs['title'] = $title;
122 common_element_start('li', 'menuitem');
123 common_element('a', $attrs, $text);
124 common_element_end('li');
127 # salted, hashed passwords are stored in the DB
129 function common_munge_password($id, $password) {
130 return md5($id . $password);
133 # check if a username exists and has matching password
134 function common_check_user($nickname, $password) {
135 $user = User::staticGet('nickname', $nickname);
136 if (is_null($user)) {
139 return (0 == strcmp(common_munge_password($password, $user->id),
144 # is the current user logged in?
145 function common_logged_in() {
146 return (!is_null(common_current_user()));
149 function common_have_session() {
150 return (0 != strcmp(session_id(), ''));
153 function common_ensure_session() {
154 if (!common_have_session()) {
159 function common_set_user($nickname) {
160 if (is_null($nickname) && common_have_session()) {
161 unset($_SESSION['userid']);
164 $user = User::staticGet('nickname', $nickname);
166 common_ensure_session();
167 $_SESSION['userid'] = $user->id;
176 # who is the current user?
177 function common_current_user() {
178 static $user = NULL; # FIXME: global memcached
179 if (is_null($user)) {
180 if (common_have_session()) {
181 $id = $_SESSION['userid'];
183 $user = User::staticGet($id);
190 # get canonical version of nickname for comparison
191 function common_canonical_nickname($nickname) {
192 # XXX: UTF-8 canonicalization (like combining chars)
193 return strtolower($nickname);
196 function common_render_content($text) {
200 return htmlspecialchars($text);
203 // XXX: set up gettext
205 function _t($str) { $str }