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');
34 require_once('DB/DataObject/Cast.php'); # for dates
36 // default configuration, overwritten in config.php
40 array('name' => 'Just another Laconica microblog',
41 'server' => 'localhost',
44 array('directory' => INSTALLDIR . '/files',
48 $config['db'] = &PEAR::getStaticProperty('DB_DataObject','options');
51 array('database' => 'YOU HAVE TO SET THIS IN config.php',
52 'schema_location' => INSTALLDIR . '/classes',
53 'class_location' => INSTALLDIR . '/classes',
54 'require_prefix' => 'classes/',
56 'db_driver' => 'MDB2',
57 'quote_identifiers' => false);
59 require_once(INSTALLDIR.'/config.php');
60 require_once(INSTALLDIR.'/lib/action.php');
62 require_once(INSTALLDIR.'/classes/Avatar.php');
63 require_once(INSTALLDIR.'/classes/Notice.php');
64 require_once(INSTALLDIR.'/classes/Profile.php');
65 require_once(INSTALLDIR.'/classes/Remote_profile.php');
66 require_once(INSTALLDIR.'/classes/Subscription.php');
67 require_once(INSTALLDIR.'/classes/User.php');
71 function common_server_error($msg) {
72 header('Status: 500 Server Error');
73 header('Content-type: text/plain');
80 function common_user_error($msg, $code=200) {
81 common_show_header('Error');
82 common_element('div', array('class' => 'error'), $msg);
86 # Start an HTML element
87 function common_element_start($tag, $attrs=NULL) {
89 if (is_array($attrs)) {
90 foreach ($attrs as $name => $value) {
91 print " $name='$value'";
93 } else if (is_string($attrs)) {
94 print " class='$attrs'";
99 function common_element_end($tag) {
103 function common_element($tag, $attrs=NULL, $content=NULL) {
104 common_element_start($tag, $attrs);
105 if ($content) print htmlspecialchars($content);
106 common_element_end($tag);
109 function common_show_header($pagetitle) {
111 common_element_start('html');
112 common_element_start('head');
113 common_element('title', NULL,
114 $pagetitle . " - " . $config['site']['name']);
115 common_element_end('head');
116 common_element_start('body');
120 function common_show_footer() {
122 common_element_end('body');
123 common_element_end('html');
126 function common_head_menu() {
127 $user = common_current_user();
128 common_element_start('ul', 'headmenu');
129 common_menu_item(common_local_url('doc', array('title' => 'help')),
132 common_menu_item(common_local_url('all', array('nickname' =>
135 common_menu_item(common_local_url('showstream', array('nickname' =>
137 _t('Profile'), $user->fullname || $user->nickname);
138 common_menu_item(common_local_url('profilesettings'),
140 common_menu_item(common_local_url('logout'),
143 common_menu_item(common_local_url('login'),
145 common_menu_item(common_local_url('register'),
148 common_element_end('ul');
151 function common_foot_menu() {
152 common_element_start('ul', 'footmenu');
153 common_menu_item(common_local_url('doc', array('title' => 'about')),
155 common_menu_item(common_local_url('doc', array('title' => 'help')),
157 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
161 function common_menu_item($url, $text, $title=NULL) {
162 $attrs['href'] = $url;
164 $attrs['title'] = $title;
166 common_element_start('li', 'menuitem');
167 common_element('a', $attrs, $text);
168 common_element_end('li');
171 function common_input($id, $label, $value=NULL) {
172 common_element('label', array('for' => $id), $label);
173 $attrs = array('name' => $id,
177 $attrs['value'] = htmlspecialchars($value);
179 common_element('input', $attrs);
182 # salted, hashed passwords are stored in the DB
184 function common_munge_password($id, $password) {
185 return md5($id . $password);
188 # check if a username exists and has matching password
189 function common_check_user($nickname, $password) {
190 $user = User::staticGet('nickname', $nickname);
191 if (is_null($user)) {
194 return (0 == strcmp(common_munge_password($password, $user->id),
199 # is the current user logged in?
200 function common_logged_in() {
201 return (!is_null(common_current_user()));
204 function common_have_session() {
205 return (0 != strcmp(session_id(), ''));
208 function common_ensure_session() {
209 if (!common_have_session()) {
214 function common_set_user($nickname) {
215 if (is_null($nickname) && common_have_session()) {
216 unset($_SESSION['userid']);
219 $user = User::staticGet('nickname', $nickname);
221 common_ensure_session();
222 $_SESSION['userid'] = $user->id;
231 # who is the current user?
232 function common_current_user() {
233 static $user = NULL; # FIXME: global memcached
234 if (is_null($user)) {
235 common_ensure_session();
236 $id = $_SESSION['userid'];
238 $user = User::staticGet($id);
244 # get canonical version of nickname for comparison
245 function common_canonical_nickname($nickname) {
246 # XXX: UTF-8 canonicalization (like combining chars)
250 # get canonical version of email for comparison
251 function common_canonical_email($email) {
252 # XXX: canonicalize UTF-8
253 # XXX: lcase the domain part
257 function common_render_content($text) {
261 return htmlspecialchars($text);
264 // where should the avatar go for this user?
266 function common_avatar_filename($user, $extension, $size=NULL) {
270 return $user->id . '-' . $size . $extension;
272 return $user->id . '-original' . $extension;
276 function common_avatar_path($filename) {
278 return $config['avatar']['directory'] . '/' . $filename;
281 function common_avatar_url($filename) {
283 return $config['avatar']['path'] . '/' . $filename;
286 function common_local_url($action, $args=NULL) {
288 /* XXX: pretty URLs */
291 foreach ($args as $key => $value) {
292 $extra .= "&${key}=${value}";
295 $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
296 return "http://".$config['site']['server'].'/'.$pathpart."index.php?action=${action}${extra}";
299 function common_date_string($dt) {
300 // XXX: do some sexy date formatting
301 // return date(DATE_RFC822, $dt);
305 function common_redirect($url, $code=307) {
306 static $status = array(301 => "Moved Permanently",
309 307 => "Temporary Redirect");
310 header("Status: ${code} $status[$code]");
311 header("Location: $url");
312 common_element('a', array('href' => $url), $url);
315 function common_broadcast_notices($id) {
316 // XXX: broadcast notices to remote subscribers
317 // XXX: broadcast notices to SMS
318 // XXX: broadcast notices to Jabber
319 // XXX: broadcast notices to other IM
320 // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
324 function common_profile_url($nickname) {
325 return common_local_url('showstream', array('nickname' => $nickname));
328 // XXX: set up gettext