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');
62 function common_server_error($msg) {
63 header('Status: 500 Server Error');
64 header('Content-type: text/plain');
71 function common_user_error($msg, $code=200) {
72 common_show_header('Error');
73 common_element('div', array('class' => 'error'), $msg);
77 # Start an HTML element
78 function common_element_start($tag, $attrs=NULL) {
80 if (is_array($attrs)) {
81 foreach ($attrs as $name => $value) {
82 print " $name='$value'";
84 } else if (is_string($attrs)) {
85 print " class='$attrs'";
90 function common_element_end($tag) {
94 function common_element($tag, $attrs=NULL, $content=NULL) {
95 common_element_start($tag, $attrs);
96 if ($content) print htmlspecialchars($content);
97 common_element_end($tag);
100 function common_show_header($pagetitle) {
102 common_element_start('html');
103 common_element_start('head');
104 common_element('title', NULL,
105 $pagetitle . " - " . $config['site']['name']);
106 common_element_end('head');
107 common_element_start('body');
111 function common_show_footer() {
113 common_element_end('body');
114 common_element_end('html');
117 function common_head_menu() {
118 $user = common_current_user();
119 common_element_start('ul', 'headmenu');
120 common_menu_item(common_local_url('doc', array('title' => 'help')),
123 common_menu_item(common_local_url('all', array('nickname' =>
126 common_menu_item(common_local_url('showstream', array('nickname' =>
128 _t('Profile'), $user->fullname || $user->nickname);
129 common_menu_item(common_local_url('profilesettings'),
131 common_menu_item(common_local_url('logout'),
134 common_menu_item(common_local_url('login'),
136 common_menu_item(common_local_url('register'),
139 common_element_end('ul');
142 function common_foot_menu() {
143 common_element_start('ul', 'footmenu');
144 common_menu_item(common_local_url('doc', array('title' => 'about')),
146 common_menu_item(common_local_url('doc', array('title' => 'help')),
148 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
152 function common_menu_item($url, $text, $title=NULL) {
153 $attrs['href'] = $url;
155 $attrs['title'] = $title;
157 common_element_start('li', 'menuitem');
158 common_element('a', $attrs, $text);
159 common_element_end('li');
162 function common_input($id, $label) {
163 common_element('label', array('for' => $id), $label);
164 common_element('input', array('name' => $id,
169 # salted, hashed passwords are stored in the DB
171 function common_munge_password($id, $password) {
172 return md5($id . $password);
175 # check if a username exists and has matching password
176 function common_check_user($nickname, $password) {
177 $user = User::staticGet('nickname', $nickname);
178 if (is_null($user)) {
181 return (0 == strcmp(common_munge_password($password, $user->id),
186 # is the current user logged in?
187 function common_logged_in() {
188 return (!is_null(common_current_user()));
191 function common_have_session() {
192 return (0 != strcmp(session_id(), ''));
195 function common_ensure_session() {
196 if (!common_have_session()) {
201 function common_set_user($nickname) {
202 if (is_null($nickname) && common_have_session()) {
203 unset($_SESSION['userid']);
206 $user = User::staticGet('nickname', $nickname);
208 common_ensure_session();
209 $_SESSION['userid'] = $user->id;
218 # who is the current user?
219 function common_current_user() {
220 static $user = NULL; # FIXME: global memcached
221 if (is_null($user)) {
222 if (common_have_session()) {
223 $id = $_SESSION['userid'];
225 $user = User::staticGet($id);
232 # get canonical version of nickname for comparison
233 function common_canonical_nickname($nickname) {
234 # XXX: UTF-8 canonicalization (like combining chars)
235 return strtolower($nickname);
238 function common_render_content($text) {
242 return htmlspecialchars($text);
245 // where should the avatar go for this user?
247 function common_avatar_filename($user, $extension, $size=NULL) {
251 return $user->id . '-' . $size . $extension;
253 return $user->id . '-original' . $extension;
257 function common_avatar_path($filename) {
259 return $config['avatar']['directory'] . '/' . $filename;
262 function common_avatar_url($filename) {
264 return $config['avatar']['path'] . '/' . $filename;
267 function common_local_url($action, $args) {
269 /* XXX: pretty URLs */
271 foreach ($args as $key => $value) {
272 $extra .= "&${key}=${value}";
274 return "http://".$config['site']['server'].'/'.$config['site']['path']."/index.php?action=${action}${extra}";
277 function commmon_date_string($dt) {
278 // XXX: do some sexy date formatting
279 return date(DATE_RFC822);
282 function common_redirect($url, $code=307) {
283 static $status = array(301 => "Moved Permanently",
286 307 => "Temporary Redirect");
287 header("Status: ${code} $status[$code]");
288 header("Location: $url");
289 common_element('a', array('href' => $url), $url);
292 function common_broadcast_notices($id) {
293 // XXX: broadcast notices to remote subscribers
294 // XXX: broadcast notices to SMS
295 // XXX: broadcast notices to Jabber
296 // XXX: broadcast notices to other IM
297 // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
301 // XXX: set up gettext