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');
76 $xw->startDocument('1.0', 'UTF-8');
77 $xw->writeDTD('html', '-//W3C//DTD XHTML 1.0 Strict//EN',
78 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
80 # FIXME: correct language for interface
82 common_element_start('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
86 common_element_start('head');
87 common_element('title', NULL,
88 $pagetitle . " - " . $config['site']['name']);
89 common_element('link', array('rel' => 'stylesheet',
91 'href' => $config['site']['path'] . 'theme/default/style/html.css',
92 'media' => 'screen, projection, tv'));
93 common_element('link', array('rel' => 'stylesheet',
95 'href' => $config['site']['path'] . 'theme/default/style/layout.css',
96 'media' => 'screen, projection, tv'));
97 common_element('link', array('rel' => 'stylesheet',
99 'href' => $config['site']['path'] . 'theme/default/style/print.css',
100 'media' => 'print'));
101 common_element_end('head');
102 common_element_start('body');
103 common_element_start('div', array('id' => 'wrapper'));
104 common_element_start('div', array('id' => 'content'));
105 common_element_start('div', array('id' => 'header'));
106 common_element('h1', 'title', $pagetitle);
107 common_element('h2', 'subtitle', $config['site']['name']);
108 common_element_end('div');
110 common_element_start('div', array('id' => 'page'));
113 function common_show_footer() {
115 common_element_start('p', 'footer');
117 common_license_block();
118 common_element_end('p');
119 common_element_end('div');
120 common_element_end('div');
121 common_element_end('div');
122 common_element_end('body');
123 common_element_end('html');
128 function common_text($txt) {
133 function common_license_block() {
135 common_element_start('div', 'license');
136 common_element_start('a', array('class' => 'license',
138 href => $config['license']['url']));
139 common_element('img', array('class' => 'license',
140 'src' => $config['license']['image'],
141 'alt' => $config['license']['title']));
142 common_element_end('a');
143 common_text(_t('Unless otherwise specified, contents of this site are copyright by the contributors and available under the '));
144 common_element('a', array('class' => 'license',
146 href => $config['license']['url']),
147 $config['license']['title']);
148 common_text(_t('. Contributors should be attributed by full name or nickname.'));
149 common_element_end('div');
152 function common_head_menu() {
153 $user = common_current_user();
154 common_element_start('ul', array('id' => 'menu', 'class' => ($user) ? 'five' : 'three'));
155 common_menu_item(common_local_url('doc', array('title' => 'help')),
158 common_menu_item(common_local_url('all', array('nickname' =>
161 common_menu_item(common_local_url('showstream', array('nickname' =>
163 _t('Profile'), $user->fullname || $user->nickname);
164 common_menu_item(common_local_url('profilesettings'),
166 common_menu_item(common_local_url('logout'),
169 common_menu_item(common_local_url('login'),
171 common_menu_item(common_local_url('register'),
174 common_element_end('ul');
177 function common_foot_menu() {
178 common_element_start('ul', 'footmenu');
179 common_menu_item(common_local_url('doc', array('title' => 'about')),
181 common_menu_item(common_local_url('doc', array('title' => 'help')),
183 common_menu_item(common_local_url('doc', array('title' => 'privacy')),
187 function common_menu_item($url, $text, $title=NULL) {
188 $attrs['href'] = $url;
190 $attrs['title'] = $title;
192 common_element_start('li', 'menuitem');
193 common_element('a', $attrs, $text);
194 common_element_end('li');
197 function common_input($id, $label, $value=NULL) {
198 common_element_start('p');
199 common_element('label', array('for' => $id), $label);
200 $attrs = array('name' => $id,
204 $attrs['value'] = htmlspecialchars($value);
206 common_element('input', $attrs);
207 common_element_end('p');
210 function common_password($id, $label) {
211 common_element_start('p');
212 common_element('label', array('for' => $id), $label);
213 $attrs = array('name' => $id,
214 'type' => 'password',
216 common_element('input', $attrs);
217 common_element_end('p');
220 function common_submit($id, $label) {
222 common_element_start('p');
223 common_element_start('label', array('for' => $id));
224 $xw->writeRaw(' ');
225 common_element_end('label');
226 common_element('input', array('type' => 'submit',
230 'class' => 'button'));
231 common_element_end('p');
234 # salted, hashed passwords are stored in the DB
236 function common_munge_password($id, $password) {
237 return md5($id . $password);
240 # check if a username exists and has matching password
241 function common_check_user($nickname, $password) {
242 $user = User::staticGet('nickname', $nickname);
243 if (is_null($user)) {
246 return (0 == strcmp(common_munge_password($password, $user->id),
251 # is the current user logged in?
252 function common_logged_in() {
253 return (!is_null(common_current_user()));
256 function common_have_session() {
257 return (0 != strcmp(session_id(), ''));
260 function common_ensure_session() {
261 if (!common_have_session()) {
266 function common_set_user($nickname) {
267 if (is_null($nickname) && common_have_session()) {
268 unset($_SESSION['userid']);
271 $user = User::staticGet('nickname', $nickname);
273 common_ensure_session();
274 $_SESSION['userid'] = $user->id;
283 # who is the current user?
284 function common_current_user() {
285 static $user = NULL; # FIXME: global memcached
286 if (is_null($user)) {
287 common_ensure_session();
288 $id = $_SESSION['userid'];
290 $user = User::staticGet($id);
296 # get canonical version of nickname for comparison
297 function common_canonical_nickname($nickname) {
298 # XXX: UTF-8 canonicalization (like combining chars)
302 # get canonical version of email for comparison
303 function common_canonical_email($email) {
304 # XXX: canonicalize UTF-8
305 # XXX: lcase the domain part
309 function common_render_content($text) {
313 return htmlspecialchars($text);
316 // where should the avatar go for this user?
318 function common_avatar_filename($user, $extension, $size=NULL) {
322 return $user->id . '-' . $size . $extension;
324 return $user->id . '-original' . $extension;
328 function common_avatar_path($filename) {
330 return $config['avatar']['directory'] . '/' . $filename;
333 function common_avatar_url($filename) {
335 return $config['avatar']['path'] . '/' . $filename;
338 function common_local_url($action, $args=NULL) {
340 /* XXX: pretty URLs */
343 foreach ($args as $key => $value) {
344 $extra .= "&${key}=${value}";
347 $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
348 return "http://".$config['site']['server'].'/'.$pathpart."index.php?action=${action}${extra}";
351 function common_date_string($dt) {
352 // XXX: do some sexy date formatting
353 // return date(DATE_RFC822, $dt);
357 function common_redirect($url, $code=307) {
358 static $status = array(301 => "Moved Permanently",
361 307 => "Temporary Redirect");
362 header("Status: ${code} $status[$code]");
363 header("Location: $url");
364 common_element('a', array('href' => $url), $url);
367 function common_broadcast_notices($id) {
368 // XXX: broadcast notices to remote subscribers
369 // XXX: broadcast notices to SMS
370 // XXX: broadcast notices to Jabber
371 // XXX: broadcast notices to other IM
372 // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
376 function common_profile_url($nickname) {
377 return common_local_url('showstream', array('nickname' => $nickname));
380 function common_notice_form() {
381 common_element_start('form', array('id' => 'newnotice', 'method' => 'POST',
382 'action' => common_local_url('newnotice')));
383 common_element_start('p');
384 common_element('label', array('for' => 'content'), _t('What\'s up?'));
385 common_element('textarea', array('rows' => 3, 'cols' => 40,
388 'class' => 'width50'),
390 common_element_end('p');
391 common_submit('submit', _t('Send'));
392 common_element_end('form');
395 // XXX: set up gettext