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',
47 $config['db'] = &PEAR::getStaticProperty('DB_DataObject','options');
50 array('database' => 'YOU HAVE TO SET THIS IN config.php',
51 'schema_location' => INSTALLDIR . '/classes',
52 'class_location' => INSTALLDIR . '/classes',
53 'require_prefix' => 'classes/',
55 'db_driver' => 'MDB2',
56 'quote_identifiers' => false);
58 require_once(INSTALLDIR.'/config.php');
59 require_once(INSTALLDIR.'/lib/action.php');
61 require_once(INSTALLDIR.'/classes/Avatar.php');
62 require_once(INSTALLDIR.'/classes/Notice.php');
63 require_once(INSTALLDIR.'/classes/Profile.php');
64 require_once(INSTALLDIR.'/classes/Remote_profile.php');
65 require_once(INSTALLDIR.'/classes/Subscription.php');
66 require_once(INSTALLDIR.'/classes/User.php');
70 function common_server_error($msg) {
71 header('Status: 500 Server Error');
72 header('Content-type: text/plain');
79 function common_user_error($msg, $code=200) {
80 common_show_header('Error');
81 common_element('div', array('class' => 'error'), $msg);
85 # Start an HTML element
86 function common_element_start($tag, $attrs=NULL) {
88 if (is_array($attrs)) {
89 foreach ($attrs as $name => $value) {
90 print " $name='$value'";
92 } else if (is_string($attrs)) {
93 print " class='$attrs'";
98 function common_element_end($tag) {
102 function common_element($tag, $attrs=NULL, $content=NULL) {
103 common_element_start($tag, $attrs);
104 if ($content) print htmlspecialchars($content);
105 common_element_end($tag);
108 function common_show_header($pagetitle) {
110 common_element_start('html');
111 common_element_start('head');
112 common_element('title', NULL,
113 $pagetitle . " - " . $config['site']['name']);
114 common_element_end('head');
115 common_element_start('body');
119 function common_show_footer() {
121 common_element_end('body');
122 common_element_end('html');
125 function common_head_menu() {
126 $user = common_current_user();
127 common_element_start('ul', 'headmenu');
128 common_menu_item(common_local_url('doc', array('title' => 'help')),
131 common_menu_item(common_local_url('all', array('nickname' =>
134 common_menu_item(common_local_url('showstream', array('nickname' =>
136 _t('Profile'), $user->fullname || $user->nickname);
137 common_menu_item(common_local_url('profilesettings'),
139 common_menu_item(common_local_url('logout'),
142 common_menu_item(common_local_url('login'),
144 common_menu_item(common_local_url('register'),
147 common_element_end('ul');
150 function common_foot_menu() {
151 common_element_start('ul', 'footmenu');
152 common_menu_item(common_local_url('doc', array('title' => 'about')),
154 common_menu_item(common_local_url('doc', array('title' => 'help')),
156 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
160 function common_menu_item($url, $text, $title=NULL) {
161 $attrs['href'] = $url;
163 $attrs['title'] = $title;
165 common_element_start('li', 'menuitem');
166 common_element('a', $attrs, $text);
167 common_element_end('li');
170 function common_input($id, $label) {
171 common_element('label', array('for' => $id), $label);
172 common_element('input', array('name' => $id,
177 # salted, hashed passwords are stored in the DB
179 function common_munge_password($id, $password) {
180 return md5($id . $password);
183 # check if a username exists and has matching password
184 function common_check_user($nickname, $password) {
185 $user = User::staticGet('nickname', $nickname);
186 if (is_null($user)) {
189 return (0 == strcmp(common_munge_password($password, $user->id),
194 # is the current user logged in?
195 function common_logged_in() {
196 return (!is_null(common_current_user()));
199 function common_have_session() {
200 return (0 != strcmp(session_id(), ''));
203 function common_ensure_session() {
204 if (!common_have_session()) {
209 function common_set_user($nickname) {
210 if (is_null($nickname) && common_have_session()) {
211 unset($_SESSION['userid']);
214 $user = User::staticGet('nickname', $nickname);
216 common_ensure_session();
217 $_SESSION['userid'] = $user->id;
226 # who is the current user?
227 function common_current_user() {
228 static $user = NULL; # FIXME: global memcached
229 if (is_null($user)) {
230 common_ensure_session();
231 $id = $_SESSION['userid'];
233 $user = User::staticGet($id);
239 # get canonical version of nickname for comparison
240 function common_canonical_nickname($nickname) {
241 # XXX: UTF-8 canonicalization (like combining chars)
245 # get canonical version of email for comparison
246 function common_canonical_email($email) {
247 # XXX: canonicalize UTF-8
248 # XXX: lcase the domain part
252 function common_render_content($text) {
256 return htmlspecialchars($text);
259 // where should the avatar go for this user?
261 function common_avatar_filename($user, $extension, $size=NULL) {
265 return $user->id . '-' . $size . $extension;
267 return $user->id . '-original' . $extension;
271 function common_avatar_path($filename) {
273 return $config['avatar']['directory'] . '/' . $filename;
276 function common_avatar_url($filename) {
278 return $config['avatar']['path'] . '/' . $filename;
281 function common_local_url($action, $args=NULL) {
283 /* XXX: pretty URLs */
286 foreach ($args as $key => $value) {
287 $extra .= "&${key}=${value}";
290 $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
291 return "http://".$config['site']['server'].'/'.$pathpart."index.php?action=${action}${extra}";
294 function commmon_date_string($dt) {
295 // XXX: do some sexy date formatting
296 return date(DATE_RFC822);
299 function common_redirect($url, $code=307) {
300 static $status = array(301 => "Moved Permanently",
303 307 => "Temporary Redirect");
304 header("Status: ${code} $status[$code]");
305 header("Location: $url");
306 common_element('a', array('href' => $url), $url);
309 function common_broadcast_notices($id) {
310 // XXX: broadcast notices to remote subscribers
311 // XXX: broadcast notices to SMS
312 // XXX: broadcast notices to Jabber
313 // XXX: broadcast notices to other IM
314 // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
318 // XXX: set up gettext