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);
41 # Start an HTML element
42 function common_element_start($tag, $attrs=NULL) {
44 $xw->startElement($tag);
45 if (is_array($attrs)) {
46 foreach ($attrs as $name => $value) {
47 $xw->writeAttribute($name, $value);
49 } else if (is_string($attrs)) {
50 $xw->writeAttribute('class', $attrs);
54 function common_element_end($tag) {
59 function common_element($tag, $attrs=NULL, $content=NULL) {
60 common_element_start($tag, $attrs);
65 common_element_end($tag);
68 function common_show_header($pagetitle) {
71 $xw = new XMLWriter();
72 $xw->openURI('php://output');
73 $xw->startDocument('1.0', 'UTF-8');
74 $xw->writeDTD('html', '-//W3C//DTD XHTML 1.0 Strict//EN',
75 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
77 # FIXME: correct language for interface
79 common_element_start('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
83 common_element_start('head');
84 common_element('title', NULL,
85 $pagetitle . " - " . $config['site']['name']);
86 common_element_end('head');
87 common_element_start('body');
88 common_element('h1', 'title', $pagetitle);
92 function common_show_footer() {
95 common_element_end('body');
96 common_element_end('html');
101 function common_head_menu() {
102 $user = common_current_user();
103 common_element_start('ul', 'headmenu');
104 common_menu_item(common_local_url('doc', array('title' => 'help')),
107 common_menu_item(common_local_url('all', array('nickname' =>
110 common_menu_item(common_local_url('showstream', array('nickname' =>
112 _t('Profile'), $user->fullname || $user->nickname);
113 common_menu_item(common_local_url('profilesettings'),
115 common_menu_item(common_local_url('logout'),
118 common_menu_item(common_local_url('login'),
120 common_menu_item(common_local_url('register'),
123 common_element_end('ul');
126 function common_foot_menu() {
127 common_element_start('ul', 'footmenu');
128 common_menu_item(common_local_url('doc', array('title' => 'about')),
130 common_menu_item(common_local_url('doc', array('title' => 'help')),
132 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
136 function common_menu_item($url, $text, $title=NULL) {
137 $attrs['href'] = $url;
139 $attrs['title'] = $title;
141 common_element_start('li', 'menuitem');
142 common_element('a', $attrs, $text);
143 common_element_end('li');
146 function common_input($id, $label, $value=NULL) {
147 common_element('label', array('for' => $id), $label);
148 $attrs = array('name' => $id,
152 $attrs['value'] = htmlspecialchars($value);
154 common_element('input', $attrs);
157 function common_password($id, $label) {
158 common_element('label', array('for' => $id), $label);
159 $attrs = array('name' => $id,
160 'type' => 'password',
162 common_element('input', $attrs);
165 # salted, hashed passwords are stored in the DB
167 function common_munge_password($id, $password) {
168 return md5($id . $password);
171 # check if a username exists and has matching password
172 function common_check_user($nickname, $password) {
173 $user = User::staticGet('nickname', $nickname);
174 if (is_null($user)) {
177 return (0 == strcmp(common_munge_password($password, $user->id),
182 # is the current user logged in?
183 function common_logged_in() {
184 return (!is_null(common_current_user()));
187 function common_have_session() {
188 return (0 != strcmp(session_id(), ''));
191 function common_ensure_session() {
192 if (!common_have_session()) {
197 function common_set_user($nickname) {
198 if (is_null($nickname) && common_have_session()) {
199 unset($_SESSION['userid']);
202 $user = User::staticGet('nickname', $nickname);
204 common_ensure_session();
205 $_SESSION['userid'] = $user->id;
214 # who is the current user?
215 function common_current_user() {
216 static $user = NULL; # FIXME: global memcached
217 if (is_null($user)) {
218 common_ensure_session();
219 $id = $_SESSION['userid'];
221 $user = User::staticGet($id);
227 # get canonical version of nickname for comparison
228 function common_canonical_nickname($nickname) {
229 # XXX: UTF-8 canonicalization (like combining chars)
233 # get canonical version of email for comparison
234 function common_canonical_email($email) {
235 # XXX: canonicalize UTF-8
236 # XXX: lcase the domain part
240 function common_render_content($text) {
244 return htmlspecialchars($text);
247 // where should the avatar go for this user?
249 function common_avatar_filename($user, $extension, $size=NULL) {
253 return $user->id . '-' . $size . $extension;
255 return $user->id . '-original' . $extension;
259 function common_avatar_path($filename) {
261 return $config['avatar']['directory'] . '/' . $filename;
264 function common_avatar_url($filename) {
266 return $config['avatar']['path'] . '/' . $filename;
269 function common_local_url($action, $args=NULL) {
271 /* XXX: pretty URLs */
274 foreach ($args as $key => $value) {
275 $extra .= "&${key}=${value}";
278 $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
279 return "http://".$config['site']['server'].'/'.$pathpart."index.php?action=${action}${extra}";
282 function common_date_string($dt) {
283 // XXX: do some sexy date formatting
284 // return date(DATE_RFC822, $dt);
288 function common_redirect($url, $code=307) {
289 static $status = array(301 => "Moved Permanently",
292 307 => "Temporary Redirect");
293 header("Status: ${code} $status[$code]");
294 header("Location: $url");
295 common_element('a', array('href' => $url), $url);
298 function common_broadcast_notices($id) {
299 // XXX: broadcast notices to remote subscribers
300 // XXX: broadcast notices to SMS
301 // XXX: broadcast notices to Jabber
302 // XXX: broadcast notices to other IM
303 // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
307 function common_profile_url($nickname) {
308 return common_local_url('showstream', array('nickname' => $nickname));
311 // XXX: set up gettext