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 require_once('PEAR.php');
33 require_once('DB_DataObject.php');
35 // default configuration, overwritten in config.php
39 array('name' => 'Just another Laconica microblog',
40 'server' => 'localhost',
43 array('directory' => INSTALLDIR . 'files',
45 'db' => &PEAR::getStaticProperty('DB_DataObject','options'),
49 array('database' => 'YOU HAVE TO SET THIS IN config.php',
50 'schema_location' => $INSTALLDIR . '/classes',
51 'class_location' => $INSTALLDIR . '/classes',
52 'require_prefix' => 'classes/',
54 'db_driver' => 'MDB2',
55 'quote_identifiers' => false);
57 require_once(INSTALLDIR . '/config.php');
61 function common_server_error($msg) {
62 header('Status: 500 Server Error');
63 header('Content-type: text/plain');
70 function common_user_error($msg, $code=200) {
71 common_show_header('Error');
72 common_element('div', array('class' => 'error'), $msg);
76 # Start an HTML element
77 function common_element_start($tag, $attrs=NULL) {
79 if (is_array($attrs)) {
80 foreach ($attrs as $name => $value) {
81 print " $name='$value'";
83 } else if (is_string($attrs)) {
84 print " class='$attrs'";
89 function common_element_end($tag) {
93 function common_element($tag, $attrs=NULL, $content=NULL) {
94 common_element_start($tag, $attrs);
95 if ($content) print htmlspecialchars($content);
96 common_element_end($tag);
99 function common_show_header($pagetitle) {
101 common_element_start('html');
102 common_element_start('head');
103 common_element('title', NULL,
104 $pagetitle . " - " . $config['site']['name']);
105 common_element_end('head');
106 common_element_start('body');
110 function common_show_footer() {
112 common_element_end('body');
113 common_element_end('html');
116 function common_head_menu() {
117 $user = common_current_user();
118 common_element_start('ul', 'headmenu');
119 common_menu_item(common_local_url('doc', array('title' => 'help')),
122 common_menu_item(common_local_url('all', array('nickname' =>
125 common_menu_item(common_local_url('showstream', array('nickname' =>
127 _t('Profile'), $user->fullname || $user->nickname);
128 common_menu_item(common_local_url('profilesettings'),
130 common_menu_item(common_local_url('logout'),
133 common_menu_item(common_local_url('login'),
135 common_menu_item(common_local_url('register'),
138 common_element_end('ul');
141 function common_foot_menu() {
142 common_element_start('ul', 'footmenu');
143 common_menu_item(common_local_url('doc', array('title' => 'about')),
145 common_menu_item(common_local_url('doc', array('title' => 'help')),
147 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
151 function common_menu_item($url, $text, $title=NULL) {
152 $attrs['href'] = $url;
154 $attrs['title'] = $title;
156 common_element_start('li', 'menuitem');
157 common_element('a', $attrs, $text);
158 common_element_end('li');
161 function common_input($id, $label) {
162 common_element('label', array('for' => $id), $label);
163 common_element('input', array('name' => $id,
168 # salted, hashed passwords are stored in the DB
170 function common_munge_password($id, $password) {
171 return md5($id . $password);
174 # check if a username exists and has matching password
175 function common_check_user($nickname, $password) {
176 $user = User::staticGet('nickname', $nickname);
177 if (is_null($user)) {
180 return (0 == strcmp(common_munge_password($password, $user->id),
185 # is the current user logged in?
186 function common_logged_in() {
187 return (!is_null(common_current_user()));
190 function common_have_session() {
191 return (0 != strcmp(session_id(), ''));
194 function common_ensure_session() {
195 if (!common_have_session()) {
200 function common_set_user($nickname) {
201 if (is_null($nickname) && common_have_session()) {
202 unset($_SESSION['userid']);
205 $user = User::staticGet('nickname', $nickname);
207 common_ensure_session();
208 $_SESSION['userid'] = $user->id;
217 # who is the current user?
218 function common_current_user() {
219 static $user = NULL; # FIXME: global memcached
220 if (is_null($user)) {
221 if (common_have_session()) {
222 $id = $_SESSION['userid'];
224 $user = User::staticGet($id);
231 # get canonical version of nickname for comparison
232 function common_canonical_nickname($nickname) {
233 # XXX: UTF-8 canonicalization (like combining chars)
234 return strtolower($nickname);
237 function common_render_content($text) {
241 return htmlspecialchars($text);
244 // where should the avatar go for this user?
246 function common_avatar_filename($user, $extension, $size=NULL) {
250 return $user->id . '-' . $size . $extension;
252 return $user->id . '-original' . $extension;
256 function common_avatar_path($filename) {
258 return $config['avatar']['directory'] . '/' . $filename;
261 function common_avatar_url($filename) {
263 return $config['avatar']['path'] . '/' . $filename;
266 function common_local_url($action, $args) {
268 /* XXX: pretty URLs */
270 foreach ($args as $key => $value) {
271 $extra .= "&${key}=${value}";
273 return "http://".$config['site']['server'].'/'.$config['site']['path']."/index.php?action=${action}${extra}";
276 function commmon_date_string($dt) {
277 // XXX: do some sexy date formatting
278 return date(DATE_RFC822);
281 function common_redirect($url, $code=307) {
282 static $status = (301 => "Moved Permanently",
285 307 => "Temporary Redirect");
286 header("Status: ${code} $status[$code]");
287 header("Location: $url");
288 common_element('a', array('href' => $url), $url);
291 function common_broadcast_notices($id) {
292 // XXX: broadcast notices to remote subscribers
293 // XXX: broadcast notices to SMS
294 // XXX: broadcast notices to Jabber
295 // XXX: broadcast notices to other IM
296 // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
300 // XXX: set up gettext
302 function _t($str) { $str }