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) */
24 function common_server_error($msg) {
25 header('Status: 500 Server Error');
26 header('Content-type: text/plain');
33 function common_user_error($msg, $code=200) {
34 common_show_header('Error');
35 common_element('div', array('class' => 'error'), $msg);
39 # Start an HTML element
40 function common_element_start($tag, $attrs=NULL) {
42 if (is_array($attrs)) {
43 foreach ($attrs as $name => $value) {
44 print " $name='$value'";
46 } else if (is_string($attrs)) {
47 print " class='$attrs'";
52 function common_element_end($tag) {
56 function common_element($tag, $attrs=NULL, $content=NULL) {
57 common_element_start($tag, $attrs);
58 if ($content) print htmlspecialchars($content);
59 common_element_end($tag);
62 function common_show_header($pagetitle) {
64 common_element_start('html');
65 common_element_start('head');
66 common_element('title', NULL,
67 $pagetitle . " - " . $config['site']['name']);
68 common_element_end('head');
69 common_element_start('body');
70 common_element('h1', 'title', $pagetitle);
74 function common_show_footer() {
76 common_element_end('body');
77 common_element_end('html');
80 function common_head_menu() {
81 $user = common_current_user();
82 common_element_start('ul', 'headmenu');
83 common_menu_item(common_local_url('doc', array('title' => 'help')),
86 common_menu_item(common_local_url('all', array('nickname' =>
89 common_menu_item(common_local_url('showstream', array('nickname' =>
91 _t('Profile'), $user->fullname || $user->nickname);
92 common_menu_item(common_local_url('profilesettings'),
94 common_menu_item(common_local_url('logout'),
97 common_menu_item(common_local_url('login'),
99 common_menu_item(common_local_url('register'),
102 common_element_end('ul');
105 function common_foot_menu() {
106 common_element_start('ul', 'footmenu');
107 common_menu_item(common_local_url('doc', array('title' => 'about')),
109 common_menu_item(common_local_url('doc', array('title' => 'help')),
111 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
115 function common_menu_item($url, $text, $title=NULL) {
116 $attrs['href'] = $url;
118 $attrs['title'] = $title;
120 common_element_start('li', 'menuitem');
121 common_element('a', $attrs, $text);
122 common_element_end('li');
125 function common_input($id, $label, $value=NULL) {
126 common_element('label', array('for' => $id), $label);
127 $attrs = array('name' => $id,
131 $attrs['value'] = htmlspecialchars($value);
133 common_element('input', $attrs);
136 function common_password($id, $label) {
137 common_element('label', array('for' => $id), $label);
138 $attrs = array('name' => $id,
139 'type' => 'password',
141 common_element('input', $attrs);
144 # salted, hashed passwords are stored in the DB
146 function common_munge_password($id, $password) {
147 return md5($id . $password);
150 # check if a username exists and has matching password
151 function common_check_user($nickname, $password) {
152 $user = User::staticGet('nickname', $nickname);
153 if (is_null($user)) {
156 return (0 == strcmp(common_munge_password($password, $user->id),
161 # is the current user logged in?
162 function common_logged_in() {
163 return (!is_null(common_current_user()));
166 function common_have_session() {
167 return (0 != strcmp(session_id(), ''));
170 function common_ensure_session() {
171 if (!common_have_session()) {
176 function common_set_user($nickname) {
177 if (is_null($nickname) && common_have_session()) {
178 unset($_SESSION['userid']);
181 $user = User::staticGet('nickname', $nickname);
183 common_ensure_session();
184 $_SESSION['userid'] = $user->id;
193 # who is the current user?
194 function common_current_user() {
195 static $user = NULL; # FIXME: global memcached
196 if (is_null($user)) {
197 common_ensure_session();
198 $id = $_SESSION['userid'];
200 $user = User::staticGet($id);
206 # get canonical version of nickname for comparison
207 function common_canonical_nickname($nickname) {
208 # XXX: UTF-8 canonicalization (like combining chars)
212 # get canonical version of email for comparison
213 function common_canonical_email($email) {
214 # XXX: canonicalize UTF-8
215 # XXX: lcase the domain part
219 function common_render_content($text) {
223 return htmlspecialchars($text);
226 // where should the avatar go for this user?
228 function common_avatar_filename($user, $extension, $size=NULL) {
232 return $user->id . '-' . $size . $extension;
234 return $user->id . '-original' . $extension;
238 function common_avatar_path($filename) {
240 return $config['avatar']['directory'] . '/' . $filename;
243 function common_avatar_url($filename) {
245 return $config['avatar']['path'] . '/' . $filename;
248 function common_local_url($action, $args=NULL) {
250 /* XXX: pretty URLs */
253 foreach ($args as $key => $value) {
254 $extra .= "&${key}=${value}";
257 $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
258 return "http://".$config['site']['server'].'/'.$pathpart."index.php?action=${action}${extra}";
261 function common_date_string($dt) {
262 // XXX: do some sexy date formatting
263 // return date(DATE_RFC822, $dt);
267 function common_redirect($url, $code=307) {
268 static $status = array(301 => "Moved Permanently",
271 307 => "Temporary Redirect");
272 header("Status: ${code} $status[$code]");
273 header("Location: $url");
274 common_element('a', array('href' => $url), $url);
277 function common_broadcast_notices($id) {
278 // XXX: broadcast notices to remote subscribers
279 // XXX: broadcast notices to SMS
280 // XXX: broadcast notices to Jabber
281 // XXX: broadcast notices to other IM
282 // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
286 function common_profile_url($nickname) {
287 return common_local_url('showstream', array('nickname' => $nickname));
290 // XXX: set up gettext