summaryrefslogtreecommitdiff
path: root/common.php
blob: a6061920d4a3cec64bd89b25dd0a678a79b39b99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php

# global configuration object

// default configuration, overwritten in config.php

$config =
  array('site' => 
		array('name' => 'Just another µB'),
		'dsn' =>
		array('phptype' => 'mysql',
			  'username' => 'stoica',
			  'password' => 'apasswd',
			  'hostspec' => 'localhost',
			  'database' => 'thedb')
		'dboptions' =>
		array('debug' => 2,
			  'portability' => DB_PORTABILITY_ALL));

require_once(INSTALLDIR . '/config.php');
require_once('DB.php');

function common_database() {
	global $config;
	$db =& DB::connect($config['dsn'], $config['dboptions']);
	if (PEAR::isError($db)) {
		common_server_error($db->getMessage());
	} else {
		return $db;
	}
}

function common_read_database() {
	// XXX: read from slave server
	return common_database();
}

function common_server_error($msg) {
	header('Status: 500 Server Error');
	header('Content-type: text/plain');

	print $msg;
	exit();
}

function common_user_error($msg) {
	common_show_header('Error');
	common_element('div', array('class' => 'error'), $msg);
	common_show_footer();
}

function common_element_start($tag, $attrs=NULL) {
	print "<$tag";
	if (is_array($attrs)) {
		foreach ($attrs as $name => $value) {
			print " $name='$value'";
		}
	} else if (is_string($attrs)) {
		print " class='$attrs'";
	}
	print '>';
}

function common_element_end($tag) {
	print "</$tag>";
}

function common_element($tag, $attrs=NULL, $content=NULL) {
    common_element_start($tag, $attrs);
	if ($content) print $content;
	common_element_end($tag);
}

function common_show_header($pagetitle) {
	global $config;
	common_element_start('html');
	common_element_start('head');
	common_element('title', NULL, $pagetitle . " - " . $config['site']['name']);
	common_element_end('head');
	common_element_start('body');
}

function common_show_footer() {
	common_element_end('body');
	common_element_end('html');
}

// TODO: set up gettext

function _t($str) { $str }