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');
63 function common_server_error($msg) {
64 header('Status: 500 Server Error');
65 header('Content-type: text/plain');
72 function common_user_error($msg, $code=200) {
73 common_show_header('Error');
74 common_element('div', array('class' => 'error'), $msg);
78 # Start an HTML element
79 function common_element_start($tag, $attrs=NULL) {
81 if (is_array($attrs)) {
82 foreach ($attrs as $name => $value) {
83 print " $name='$value'";
85 } else if (is_string($attrs)) {
86 print " class='$attrs'";
91 function common_element_end($tag) {
95 function common_element($tag, $attrs=NULL, $content=NULL) {
96 common_element_start($tag, $attrs);
97 if ($content) print htmlspecialchars($content);
98 common_element_end($tag);
101 function common_show_header($pagetitle) {
103 common_element_start('html');
104 common_element_start('head');
105 common_element('title', NULL,
106 $pagetitle . " - " . $config['site']['name']);
107 common_element_end('head');
108 common_element_start('body');
112 function common_show_footer() {
114 common_element_end('body');
115 common_element_end('html');
118 function common_head_menu() {
119 $user = common_current_user();
120 common_element_start('ul', 'headmenu');
121 common_menu_item(common_local_url('doc', array('title' => 'help')),
124 common_menu_item(common_local_url('all', array('nickname' =>
127 common_menu_item(common_local_url('showstream', array('nickname' =>
129 _t('Profile'), $user->fullname || $user->nickname);
130 common_menu_item(common_local_url('profilesettings'),
132 common_menu_item(common_local_url('logout'),
135 common_menu_item(common_local_url('login'),
137 common_menu_item(common_local_url('register'),
140 common_element_end('ul');
143 function common_foot_menu() {
144 common_element_start('ul', 'footmenu');
145 common_menu_item(common_local_url('doc', array('title' => 'about')),
147 common_menu_item(common_local_url('doc', array('title' => 'help')),
149 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
153 function common_menu_item($url, $text, $title=NULL) {
154 $attrs['href'] = $url;
156 $attrs['title'] = $title;
158 common_element_start('li', 'menuitem');
159 common_element('a', $attrs, $text);
160 common_element_end('li');
163 function common_input($id, $label) {
164 common_element('label', array('for' => $id), $label);
165 common_element('input', array('name' => $id,
170 # salted, hashed passwords are stored in the DB
172 function common_munge_password($id, $password) {
173 return md5($id . $password);
176 # check if a username exists and has matching password
177 function common_check_user($nickname, $password) {
178 $user = User::staticGet('nickname', $nickname);
179 if (is_null($user)) {
182 return (0 == strcmp(common_munge_password($password, $user->id),
187 # is the current user logged in?
188 function common_logged_in() {
189 return (!is_null(common_current_user()));
192 function common_have_session() {
193 return (0 != strcmp(session_id(), ''));
196 function common_ensure_session() {
197 if (!common_have_session()) {
202 function common_set_user($nickname) {
203 if (is_null($nickname) && common_have_session()) {
204 unset($_SESSION['userid']);
207 $user = User::staticGet('nickname', $nickname);
209 common_ensure_session();
210 $_SESSION['userid'] = $user->id;
219 # who is the current user?
220 function common_current_user() {
221 static $user = NULL; # FIXME: global memcached
222 if (is_null($user)) {
223 if (common_have_session()) {
224 $id = $_SESSION['userid'];
226 $user = User::staticGet($id);
233 # get canonical version of nickname for comparison
234 function common_canonical_nickname($nickname) {
235 # XXX: UTF-8 canonicalization (like combining chars)
236 return strtolower($nickname);
239 # get canonical version of email for comparison
240 function common_canonical_email($email) {
241 # XXX: canonicalize UTF-8
242 # XXX: lcase the domain part
246 function common_render_content($text) {
250 return htmlspecialchars($text);
253 // where should the avatar go for this user?
255 function common_avatar_filename($user, $extension, $size=NULL) {
259 return $user->id . '-' . $size . $extension;
261 return $user->id . '-original' . $extension;
265 function common_avatar_path($filename) {
267 return $config['avatar']['directory'] . '/' . $filename;
270 function common_avatar_url($filename) {
272 return $config['avatar']['path'] . '/' . $filename;
275 function common_local_url($action, $args=NULL) {
277 /* XXX: pretty URLs */
280 foreach ($args as $key => $value) {
281 $extra .= "&${key}=${value}";
284 $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
285 return "http://".$config['site']['server'].'/'.$pathpart."index.php?action=${action}${extra}";
288 function commmon_date_string($dt) {
289 // XXX: do some sexy date formatting
290 return date(DATE_RFC822);
293 function common_redirect($url, $code=307) {
294 static $status = array(301 => "Moved Permanently",
297 307 => "Temporary Redirect");
298 header("Status: ${code} $status[$code]");
299 header("Location: $url");
300 common_element('a', array('href' => $url), $url);
303 function common_broadcast_notices($id) {
304 // XXX: broadcast notices to remote subscribers
305 // XXX: broadcast notices to SMS
306 // XXX: broadcast notices to Jabber
307 // XXX: broadcast notices to other IM
308 // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
312 // XXX: set up gettext