Subversion Repositories public

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 andreas 1
<?
2
$dbversion = "2.6";
3
$_Handle = -1;
4
$_hCount = 0;
5
 
6
function OpenDB($host = "", $dbname = "") {
7
	global $_Handle;
8
	global $_hCount;
9
 
10
	if ($_hCount > 0) {
11
	   $_hCount++;
12
	   return $_Handle;
13
	}
14
 
15
	if (!strlen($host) || !strlen($dbname)) {
16
	   $ini_array = parse_ini_file("setup/setup.dat");
17
	   $dbname = $ini_array['dbname'];
18
	   $host = $ini_array['domain'];
19
	   $port = $ini_array['port'];
20
	   $user = $ini_array['dbuser'];
21
	   $passwd = $ini_array['dbpasswd'];
22
	}
23
 
24
	if (isset($host) && strlen($host) > 0)
25
	   $connStr = "host=$host ";
26
	else
27
	   $connStr = "host=localhost";
28
 
29
	if (isset($dbname) && strlen($dbname) > 0)
30
	   $connStr .= " dbname=$dbname";
31
 
32
	if (isset($port) && strlen($port) > 0)
33
	   $connStr .= " port=$port";
34
 
35
	if (isset($user) && strlen($user) > 0)
36
	   $connStr .= " user=$user";
37
 
38
	if (isset($passwd) && strlen($passwd) > 0)
39
	   $connStr .= " password=$passwd";
40
 
41
	$db = pg_connect($connStr);
42
 
43
	if ($db == false) {
44
	   echo "<p class=\"error\">OpenDB Error: Datenbank $dbname konnte nicht ge&ouml;ffnet werden!</p>\n";
45
	   return false;
46
	}
47
 
48
	$_Handle = $db;
49
	$_hCount++;
50
	return $db;
51
}
52
 
53
function QueryDB($db, $qstr) {
54
	global $_hCount;
55
 
56
	if ($_hCount == 0) {
57
	   echo "<p class=\"error\">ERROR: Es ist keine Datenbank ge&ouml;ffnet!</p>\n";
58
	   return NULL;
59
	}
60
 
61
	if (!isset($db) || !isset($qstr) || $qstr == "") {
62
		echo "<p class=\"error\">ERROR: Keine g&uuml;ltige SQL-Abfrage!</p>\n";
63
		return NULL;
64
	}
65
 
66
	$result = pg_query($db, $qstr);
67
 
68
	if (!$result) {
69
		$errormessage = pg_errormessage($db);
70
		echo "<p class=\"error\">ERROR: $errormessage<br>\n";
71
		echo "<br>SQL-Error: $qstr</p>\n";
72
		return NULL;
73
	}
74
 
75
	return $result;
76
}
77
 
78
function TQueryDB($db, $qstr) {
79
	global $_hCount;
80
 
81
	if ($_hCount == 0) {
82
	   echo "<p class=\"error\">ERROR: Es ist keine Datenbank ge&ouml;ffnet!</p>\n";
83
	   return NULL;
84
	}
85
 
86
	if (!isset($db) || !isset($qstr) || $qstr == "") {
87
		echo "<p class=\"error\">ERROR: Keine g&uuml;ltige SQL-Abfrage!</p>\n";
88
		return NULL;
89
	}
90
 
91
	$result = pg_query($db, "BEGIN");
92
 
93
	if (!$result) {
94
		$errormessage = pg_errormessage($db);
95
		echo "<p class=\"error\">ERROR: $errormessage</td>\n";
96
		return NULL;
97
	}
98
 
99
	$result = pg_query($db, $qstr);
100
 
101
	if (!$result) {
102
		$errormessage = pg_errormessage($db);
103
		echo "<p class=\"error\">ERROR: $errormessage<br>\n";
104
		echo "<br>SQL-Error: $qstr</p>\n";
105
		pg_query($db, "ROLLBACK");
106
		return NULL;
107
	}
108
 
109
	$result = pg_query($db, "COMMIT");
110
 
111
	if (!$result) {
112
		$errormessage = pg_errormessage($db);
113
		echo "<p class=\"error\">ERROR: $errormessage</p>\n";
114
		return NULL;
115
	}
116
 
117
	return $result;
118
}
119
 
120
function fetchDB($result, $pos) {
121
	global $_hCount;
122
 
123
	if ($_hCount == 0) {
124
	   echo "<p class=\"error\">ERROR: Es ist keine Datenbank ge&ouml;ffnet!</p>\n";
125
	   return false;
126
	}
127
 
128
	if (!isset($result)) {
129
	   Error("fetchDB: Es wurde kein Ergebnisset &uuml;bergeben!");
130
	   return false;
131
	}
132
 
133
	if (!isset($pos) || $pos < 0) {
134
	   Error("fetchDB: Keine oder ung&uuml;ltige Position!");
135
	   return false;
136
	}
137
 
138
	if (($erg = pg_fetch_row($result, $pos)) == false) {
139
	   Error("fetchDB: Fehler beim Auslesen eines Datensatzes!");
140
	   return false;
141
	}
142
 
143
	return $erg;
144
}
145
 
146
function numrowsDB($result) {
147
	global $_hCount;
148
 
149
	if ($_hCount == 0) {
150
	   echo "<p class=\"error\">ERROR: Es ist keine Datenbank ge&ouml;ffnet!</p>\n";
151
	   return -1;
152
	}
153
 
154
	if (!isset($result)) {
155
	   Error("numrowDB: Es wurde kein Ergebnisset &uuml;bergeben!");
156
	   return -1;
157
	}
158
 
159
	if (($rows = pg_num_rows($result)) == -1) {
160
	   Error("numrowDB: Anzahl Datens&auml;tze konnte nicht bestimmt werden!");
161
	   return -1;
162
	}
163
 
164
	return $rows;
165
}
166
 
167
function numrowDB($result) {
168
	return numrowsDB($result);
169
}
170
 
171
function closeDB($db) {
172
	global $_hCount;
173
	global $_Handle;
174
 
175
	if ($_hCount > 1) {
176
	   $_hCount--;
177
	   return true;
178
	}
179
 
180
	$_hCount = 0;
181
	$_Handle = -1;
182
	return pg_close($db);
183
}
184
 
185
function Journal($typ, $message, $db=NULL) {
186
	global $unum;
187
	$dbflag = false;
188
 
189
	if (!isset($unum) || $unum <= 0)
190
	   return;
191
 
192
	if ($db == NULL) {
193
	   $db = OpenDB();
194
	   $dbflag = true;
195
	}
196
 
197
	$query = "select kj_num from key_jtype where kj_num = $typ";
198
	$result = QueryDB($db, $query);
199
 
200
	if (!$result) {
201
	   closeDB($db);
202
	   return;
203
	}
204
 
205
	if (pg_num_rows($result) <= 0)
206
	   $typ = 999;
207
 
208
	$query = "select co_ejournal from counter";
209
	$result = QueryDB($db, $query);
210
 
211
	if (!$result) {
212
	   closeDB($db);
213
	   return;
214
	}
215
 
216
	$data = pg_fetch_row($result, 0);
217
	$co_ejournal = $data[0] + 1;
218
	$zeit = time();
219
 
220
	$query = "insert into ejournal (ej_num, ej_date, ej_uid, ej_type,";
221
	$query .= "ej_text) values ($co_ejournal, $zeit, $unum, $typ, '$message')";
222
 
223
	if (!QueryDB($db, "begin")) {
224
	   closeDB($db);
225
	   return;
226
	}
227
 
228
	if (!QueryDB($db, $query)) {
229
	   QueryDB($db, "rollback");
230
	   closeDB($db);
231
	   return;
232
	}
233
 
234
	$query = "update counter set co_ejournal = $co_ejournal";
235
 
236
	if (!QueryDB($db, $query)) {
237
	   QueryDB($db, "rollback");
238
	   closeDB($db);
239
	   return;
240
	}
241
 
242
	QueryDB($db, "commit");
243
 
244
	if ($dbflag)
245
	   closeDB($db);
246
}
247
 
248
function Error($message) {
249
	echo "<p class=\"error\">$message</p>\n";
250
}
251
 
252
# Pruefen der Datenbankversion:
253
#
254
$dbver = OpenDB();
255
$qs = "select version from counter";
256
 
257
if (!($erg = QueryDB($dbver, $qs))) {
258
   Error("Falsche oder keine Datenbank!");
259
   exit;
260
}
261
 
262
$data = pg_fetch_row($erg, 0);
263
 
264
if ($data[0] != $dbversion) {
265
   Error("Die Datenbank hat eine ung&uuml;ltige Versionsnummer!");
266
   closeDB($dbver);
267
   exit;
268
}
269
 
270
closeDB($dbver);
271
unset($dbver);
272
unset($erg);
273
unset($qs);
274
unset($data);
275
?>