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 header('Content-Type: application/xhtml+xml');
73 $xw = new XMLWriter();
74 $xw->openURI('php://output');
75 $xw->startDocument('1.0', 'UTF-8');
76 $xw->writeDTD('html', '-//W3C//DTD XHTML 1.0 Strict//EN',
77 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
79 # FIXME: correct language for interface
81 common_element_start('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
85 common_element_start('head');
86 common_element('title', NULL,
87 $pagetitle . " - " . $config['site']['name']);
88 common_element('link', array('rel' => 'stylesheet',
90 'href' => $config['site']['path'] . 'theme/default/style/html.css',
91 'media' => 'screen, projection, tv'));
92 common_element('link', array('rel' => 'stylesheet',
94 'href' => $config['site']['path'] . 'theme/default/style/layout.css',
95 'media' => 'screen, projection, tv'));
96 common_element('link', array('rel' => 'stylesheet',
98 'href' => $config['site']['path'] . 'theme/default/style/print.css',
100 common_element_end('head');
101 common_element_start('body');
102 common_element_start('div', array('id' => 'wrapper'));
103 common_element_start('div', array('id' => 'content'));
104 common_element_start('div', array('id' => 'header'));
105 common_element('h1', 'title', $pagetitle);
106 common_element('h2', 'subtitle', $config['site']['name']);
107 common_element_end('div');
109 common_element_start('div', array('id' => 'page'));
112 function common_show_footer() {
114 common_element_start('p', 'footer');
116 common_license_block();
117 common_element_end('p');
118 common_element_end('div');
119 common_element_end('div');
120 common_element_end('div');
121 common_element_end('body');
122 common_element_end('html');
127 function common_text($txt) {
132 function common_license_block() {
134 common_element_start('div', 'license');
135 common_element_start('a', array('class' => 'license',
137 href => $config['license']['url']));
138 common_element('img', array('class' => 'license',
139 'src' => $config['license']['image'],
140 'alt' => $config['license']['title']));
141 common_element_end('a');
142 common_text(_t('Unless otherwise specified, contents of this site are copyright by the contributors and available under the '));
143 common_element('a', array('class' => 'license',
145 href => $config['license']['url']),
146 $config['license']['title']);
147 common_text(_t('. Contributors should be attributed by full name or nickname.'));
148 common_element_end('div');
151 function common_head_menu() {
152 $user = common_current_user();
153 common_element_start('ul', array('id' => 'menu', 'class' => ($user) ? 'five' : 'three'));
154 common_menu_item(common_local_url('doc', array('title' => 'help')),
157 common_menu_item(common_local_url('all', array('nickname' =>
160 common_menu_item(common_local_url('showstream', array('nickname' =>
162 _t('Profile'), $user->fullname || $user->nickname);
163 common_menu_item(common_local_url('profilesettings'),
165 common_menu_item(common_local_url('logout'),
168 common_menu_item(common_local_url('login'),
170 common_menu_item(common_local_url('register'),
173 common_element_end('ul');
176 function common_foot_menu() {
177 common_element_start('ul', 'footmenu');
178 common_menu_item(common_local_url('doc', array('title' => 'about')),
180 common_menu_item(common_local_url('doc', array('title' => 'help')),
182 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
186 function common_menu_item($url, $text, $title=NULL) {
187 $attrs['href'] = $url;
189 $attrs['title'] = $title;
191 common_element_start('li', 'menuitem');
192 common_element('a', $attrs, $text);
193 common_element_end('li');
196 function common_input($id, $label, $value=NULL) {
197 common_element_start('p');
198 common_element('label', array('for' => $id), $label);
199 $attrs = array('name' => $id,
203 $attrs['value'] = htmlspecialchars($value);
205 common_element('input', $attrs);
206 common_element_end('p');
209 function common_password($id, $label) {
210 common_element_start('p');
211 common_element('label', array('for' => $id), $label);
212 $attrs = array('name' => $id,
213 'type' => 'password',
215 common_element('input', $attrs);
216 common_element_end('p');
219 function common_submit($id, $label) {
220 common_element_start('p');
221 common_element('label', NULL, ' ');
222 common_element('input', array('type' => 'submit',
226 'class' => 'button'));
227 common_element_end('p');
230 # salted, hashed passwords are stored in the DB
232 function common_munge_password($id, $password) {
233 return md5($id . $password);
236 # check if a username exists and has matching password
237 function common_check_user($nickname, $password) {
238 $user = User::staticGet('nickname', $nickname);
239 if (is_null($user)) {
242 return (0 == strcmp(common_munge_password($password, $user->id),
247 # is the current user logged in?
248 function common_logged_in() {
249 return (!is_null(common_current_user()));
252 function common_have_session() {
253 return (0 != strcmp(session_id(), ''));
256 function common_ensure_session() {
257 if (!common_have_session()) {
262 function common_set_user($nickname) {
263 if (is_null($nickname) && common_have_session()) {
264 unset($_SESSION['userid']);
267 $user = User::staticGet('nickname', $nickname);
269 common_ensure_session();
270 $_SESSION['userid'] = $user->id;
279 # who is the current user?
280 function common_current_user() {
281 static $user = NULL; # FIXME: global memcached
282 if (is_null($user)) {
283 common_ensure_session();
284 $id = $_SESSION['userid'];
286 $user = User::staticGet($id);
292 # get canonical version of nickname for comparison
293 function common_canonical_nickname($nickname) {
294 # XXX: UTF-8 canonicalization (like combining chars)
298 # get canonical version of email for comparison
299 function common_canonical_email($email) {
300 # XXX: canonicalize UTF-8
301 # XXX: lcase the domain part
305 function common_render_content($text) {
309 return htmlspecialchars($text);
312 // where should the avatar go for this user?
314 function common_avatar_filename($user, $extension, $size=NULL) {
318 return $user->id . '-' . $size . $extension;
320 return $user->id . '-original' . $extension;
324 function common_avatar_path($filename) {
326 return $config['avatar']['directory'] . '/' . $filename;
329 function common_avatar_url($filename) {
331 return $config['avatar']['path'] . '/' . $filename;
334 function common_local_url($action, $args=NULL) {
336 /* XXX: pretty URLs */
339 foreach ($args as $key => $value) {
340 $extra .= "&${key}=${value}";
343 $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
344 return "http://".$config['site']['server'].'/'.$pathpart."index.php?action=${action}${extra}";
347 function common_date_string($dt) {
348 // XXX: do some sexy date formatting
349 // return date(DATE_RFC822, $dt);
353 function common_redirect($url, $code=307) {
354 static $status = array(301 => "Moved Permanently",
357 307 => "Temporary Redirect");
358 header("Status: ${code} $status[$code]");
359 header("Location: $url");
360 common_element('a', array('href' => $url), $url);
363 function common_broadcast_notices($id) {
364 // XXX: broadcast notices to remote subscribers
365 // XXX: broadcast notices to SMS
366 // XXX: broadcast notices to Jabber
367 // XXX: broadcast notices to other IM
368 // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
372 function common_profile_url($nickname) {
373 return common_local_url('showstream', array('nickname' => $nickname));
376 // XXX: set up gettext