]>
Commit | Line | Data |
---|---|---|
a5eae6b7 MR |
1 | <?php |
2 | /* $Id$ */ | |
3 | ||
4 | include_once 'config.inc.php'; | |
5 | require_once 'persistens.php'; | |
6 | require_once 'helper.php'; | |
7 | ||
8 | class Pgsql extends PDO implements WebcalSupport { | |
9 | ||
10 | private $user; | |
11 | ||
12 | function __construct($dns = NULL) { | |
13 | if ($dns) | |
14 | $db_dns = "pgsql:$dns"; | |
15 | else | |
16 | $db_dns = 'pgsql:'.DNS; | |
17 | try { | |
18 | parent::__construct($db_dns); | |
19 | $locale = iconv_get_encoding('internal_encoding'); | |
20 | parent::exec("set client_encoding = '$locale'"); | |
21 | } | |
22 | catch (PDOException $ex) { | |
23 | throw new Exception($ex->getMessage()); | |
24 | } | |
25 | } | |
26 | ||
27 | private function getMsg($resource) { | |
28 | $err = $resource->errorInfo(); | |
29 | if (count($err) > 2) | |
30 | return $err[2]; | |
31 | else | |
32 | return ""; | |
33 | } | |
34 | ||
35 | function initDatabase($name, $pwd, $uid) { | |
36 | $locale = "select pg_encoding_to_char(encoding) as encoding from pg_database where datname='template0'"; | |
37 | $usr = "create user $uid with encrypted password '$pwd'"; | |
38 | $init = "create database $name with encoding 'utf8' template template0 owner $uid"; | |
39 | try { | |
40 | $sth = $this->prepare($locale); | |
41 | $sth->execute(); | |
42 | $res = $sth->fetch(); | |
43 | if (strcasecmp($res['encoding'], 'UTF8') !== 0) | |
44 | throw new Exception("Template0 encoding [".$res['encoding']."]. Must be UTF8"); | |
45 | $this->exec($usr); | |
46 | $this->exec($init); | |
47 | } | |
48 | catch (PDOException $ex) { | |
49 | throw new Exception($ex->getMessage()); | |
50 | } | |
51 | } | |
52 | ||
53 | function createDatabase($name) { | |
54 | $sql = "select count(*) as exist from pg_database where datname=?"; | |
55 | $sth = $this->prepare($sql); | |
56 | $sth->execute(array($name)); | |
57 | $res = $sth->fetch(); | |
58 | if ($res['exist'] != 0) { | |
59 | $this->beginTransaction(); | |
60 | include_once 'db_create.postgresql.php'; | |
61 | $db = make_sql_stm(); | |
62 | foreach ($db as $sql) { | |
63 | $sth->closeCursor(); | |
64 | $sth = $this->prepare($sql); | |
65 | if (! $sth) { | |
66 | $this->rollBack(); | |
67 | $err = "$sql\n"; | |
68 | foreach ($this->errorInfo() as $info) | |
69 | $err .= "$info\n"; | |
70 | throw new Exception($err); | |
71 | } | |
72 | $sth->execute(); | |
73 | //throw new Exception(var_export($sth, true)); | |
74 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
75 | $this->rollBack(); | |
76 | $err = "$sql\n"; | |
77 | foreach ($this->errorInfo() as $info) | |
78 | $err .= "$info\n"; | |
79 | throw new Exception($err); | |
80 | } | |
81 | } | |
82 | $this->commit(); | |
83 | } | |
84 | else | |
85 | throw new Exception("$name: Database does not exist"); | |
86 | } | |
87 | ||
88 | function getViewStyle($uid) { | |
89 | $sql = "select userview from users u where u.uid = ?"; | |
90 | $sth = $this->prepare($sql); | |
91 | $sth->execute(array($uid)); | |
92 | $res = $sth->fetch(); | |
93 | return $res[0]; | |
94 | } | |
95 | ||
96 | function getRole($uid) { | |
97 | $sql = "select userrole from users u where u.uid = ?"; | |
98 | $sth = $this->prepare($sql); | |
99 | $sth->execute(array($uid)); | |
100 | $res = $sth->fetch(); | |
101 | return $res[0]; | |
102 | } | |
103 | ||
104 | function getTimeout($uid) { | |
105 | $sql = "select timeout from users u where u.uid = ?"; | |
106 | $sth = $this->prepare($sql); | |
107 | $sth->execute(array($uid)); | |
108 | $res = $sth->fetch(); | |
109 | return $res[0]; | |
110 | } | |
111 | ||
112 | function getTimezone($uid) { | |
113 | $sql = "select timezone from users u where u.uid = ?"; | |
114 | $sth = $this->prepare($sql); | |
115 | $sth->execute(array($uid)); | |
116 | $res = $sth->fetch(); | |
117 | return $res[0]; | |
118 | } | |
119 | ||
120 | function getStartWeek($uid) { | |
121 | $sql = "select weekstart from users u where u.uid = ?"; | |
122 | $sth = $this->prepare($sql); | |
123 | $sth->execute(array($uid)); | |
124 | $res = $sth->fetch(); | |
125 | return ($res[0]) ? 'MO' : 'SU'; | |
126 | } | |
127 | ||
128 | function getStartHour($uid) { | |
129 | $sql = "select daystart from users u where u.uid = ?"; | |
130 | $sth = $this->prepare($sql); | |
131 | $sth->execute(array($uid)); | |
132 | $res = $sth->fetch(); | |
133 | return $res[0]; | |
134 | } | |
135 | ||
136 | function getEndHour($uid) { | |
137 | $sql = "select daystart from users u where u.uid = ?"; | |
138 | $sth = $this->prepare($sql); | |
139 | $sth->execute(array($uid)); | |
140 | $res = $sth->fetch(); | |
141 | return $res[0]; | |
142 | } | |
143 | ||
144 | function authenticate($uid, $pwd) { | |
145 | $res = array(); | |
146 | $sql = "select userrole, timeout, userview, weekstart, daystart, | |
147 | dayend, timezone, seckey, pubkey, c.id as id, name, color, config | |
148 | from users u left join calendar c on c.uid = u.id where | |
149 | u.uid = ? and u.pwd = ?"; | |
150 | $sth = $this->prepare($sql); | |
151 | if (! $sth) { | |
152 | $err = "$sql\n"; | |
153 | foreach ($this->errorInfo() as $info) | |
154 | $err .= "$info\n"; | |
155 | throw new Exception($err); | |
156 | } | |
157 | $sth->execute(array($uid,$pwd)); | |
158 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
159 | $err = "$sql\n"; | |
160 | foreach ($this->errorInfo() as $info) | |
161 | $err .= "$info\n"; | |
162 | throw new Exception($err); | |
163 | } | |
164 | $res = $sth->fetchAll(PDO::FETCH_ASSOC); | |
165 | ||
166 | /* | |
167 | if ($res[0]['config']) { | |
168 | //$res[0]['config'] = base64_decode($res[0]['config']); | |
169 | //$res[0]['config'] = quoted_printable_decode($res[0]['config']); | |
170 | $res[0]['config'] = $res[0]['config']; | |
171 | } | |
172 | */ | |
173 | ||
174 | return $res; | |
175 | } | |
176 | ||
177 | function addUser($data) { | |
178 | //$fp = fopen('/tmp/davical.log', 'a'); | |
179 | //fwrite($fp, "New user\nuid: {$data['uid']}\npwd: {$data['pwd']}\ntimezone: {$data['timezone']}\nuserrole: {$data['userrole']}\nseckey: {$data['seckey']}\npubkey: {$data['pubkey']}"); | |
180 | //fclose($fp); | |
181 | $sql = "insert into users (uid, pwd, timezone, userrole, seckey, pubkey, " . | |
182 | "timeout, userview, weekstart, daystart, dayend) values (?,?,?,?,?,?,?,?,?,?,?)"; | |
183 | $sth = $this->prepare($sql); | |
184 | if (! $sth) { | |
185 | foreach ($this->errorInfo() as $info) | |
186 | $err .= "$info\n"; | |
187 | return $err; | |
188 | } | |
189 | $this->beginTransaction(); | |
190 | $sth->execute(array($data['uid'],$data['pwd'],$data['timezone'], | |
191 | $data['userrole'],$data['seckey'],$data['pubkey'], | |
192 | $data['timeout'], $data['view'], ($data['week_start'] == false) ? 0 : 1, | |
193 | $data['start'], $data['end'])); | |
194 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
195 | $this->rollBack(); | |
196 | $err = "$sql\n"; | |
197 | foreach ($this->errorInfo() as $info) | |
198 | $err .= "$info\n"; | |
199 | return $err; | |
200 | } | |
201 | $this->commit(); | |
202 | return TRUE; | |
203 | } | |
204 | ||
205 | function setUserSettings($uid, $data) { | |
206 | $sql = "update users set userrole = ?, timeout = ?, userview = ?, | |
207 | weekstart = ?, daystart = ?, dayend = ?, timezone = ? | |
208 | where uid = ?"; | |
209 | $sth = $this->prepare($sql); | |
210 | if (! $sth) { | |
211 | $err = "$sql\n"; | |
212 | foreach ($this->errorInfo() as $info) | |
213 | $err .= "$info\n"; | |
214 | return $err; | |
215 | } | |
216 | $this->beginTransaction(); | |
217 | $sth->execute(array($data['userrole'],$data['timeout'], | |
218 | $data['userview'],$data['weekstart'],$data['daystart'], | |
219 | $data['dayend'],$data['timezone'],$uid)); | |
220 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
221 | $this->rollBack(); | |
222 | $err = "$sql\n"; | |
223 | foreach ($this->errorInfo() as $info) | |
224 | $err .= "$info\n"; | |
225 | return $err; | |
226 | } | |
227 | $this->commit(); | |
228 | return TRUE; | |
229 | } | |
230 | ||
231 | function changeDefault($data) { | |
232 | $this->beginTransaction(); | |
233 | foreach ($data as $column => $default) { | |
234 | $sql = "alter table users alter $column set default $default"; | |
235 | //echo "$sql<br/>"; | |
236 | $res = $this->exec($sql); | |
237 | if ($res === FALSE) { | |
238 | $err = "$sql\n"; | |
239 | foreach ($this->errorInfo() as $info) | |
240 | $err .= "$info\n"; | |
241 | $this->rollBack(); | |
242 | return $err; | |
243 | } | |
244 | } | |
245 | $this->commit(); | |
246 | return TRUE; | |
247 | } | |
248 | ||
249 | function deleteUser($uid) { | |
250 | $sql = "delete from users where uid = ?"; | |
251 | $sth = $this->prepare($sql); | |
252 | if (! $sth) { | |
253 | $err = "$sql\n"; | |
254 | foreach ($this->errorInfo() as $info) | |
255 | $err .= "$info\n"; | |
256 | return $err; | |
257 | } | |
258 | $this->beginTransaction(); | |
259 | $sth->execute(array($uid)); | |
260 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
261 | $this->rollBack(); | |
262 | $err = "$sql\n"; | |
263 | foreach ($this->errorInfo() as $info) | |
264 | $err .= "$info\n"; | |
265 | return $err; | |
266 | } | |
267 | $this->commit(); | |
268 | return TRUE; | |
269 | } | |
270 | ||
271 | function addCalendar($uid, CalendarInfo $cal) { | |
272 | //$fp = fopen('/tmp/add_calender.log', 'a'); | |
273 | //fwrite($fp, "New calendar\nUID: $uid\nname: {$cal->name}\nColor: {$cal->color}\nConfig: {$cal->config}\n"); | |
274 | $this->beginTransaction(); | |
275 | $sql = "select id from users where uid = ?"; | |
276 | //fwrite($fp, "Get ID: $sql\n"); | |
277 | $sth = $this->prepare($sql); | |
278 | if (! $sth) { | |
279 | $err = "$sql\n"; | |
280 | foreach ($this->errorInfo() as $info) | |
281 | $err .= "$info\n"; | |
282 | $this->rollBack(); | |
283 | return $err; | |
284 | } | |
285 | $sth->execute(array($uid)); | |
286 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
287 | $err = "$sql\n"; | |
288 | foreach ($this->errorInfo() as $info) | |
289 | $err .= "$info\n"; | |
290 | $this->rollBack(); | |
291 | return $err; | |
292 | } | |
293 | $sth->bindColumn(1, $id); | |
294 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
295 | $err = "$sql\n"; | |
296 | foreach ($this->errorInfo() as $info) | |
297 | $err .= "$info\n"; | |
298 | $this->rollBack(); | |
299 | return $err; | |
300 | } | |
301 | $sth->fetch(PDO::FETCH_BOUND); | |
302 | //fwrite($fp, "Returned users ID: $id\n"); | |
303 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
304 | $err = "$sql\n"; | |
305 | foreach ($this->errorInfo() as $info) | |
306 | $err .= "$info\n"; | |
307 | $this->rollBack(); | |
308 | return $err; | |
309 | } | |
310 | $sth->closeCursor(); | |
311 | $sql = "insert into calendar (uid, name, color, config) | |
312 | values ($id,?,?,?)"; | |
313 | //var_dump($sql); | |
314 | //fwrite($fp, "Insert into calendar: $sql\n"); | |
315 | $sth = $this->prepare($sql); | |
316 | if (! $sth) { | |
317 | $err = "$sql\n"; | |
318 | foreach ($this->errorInfo() as $info) | |
319 | $err .= "$info\n"; | |
320 | $this->rollBack(); | |
321 | return $err; | |
322 | } | |
323 | //$sth->execute(array($cal->name,$cal->color,base64_encode($cal->config))); | |
324 | $sth->execute(array($cal->name, $cal->color, $cal->config)); | |
325 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
326 | $err = "$sql\n"; | |
327 | foreach ($this->errorInfo() as $info) | |
328 | $err .= "$info\n"; | |
329 | $this->rollBack(); | |
330 | return $err; | |
331 | } | |
332 | $sth->closeCursor(); | |
333 | $sth = $this->prepare('select max(id) from calendar'); | |
334 | $sth->execute(); | |
335 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
336 | $err = "$sql\n"; | |
337 | foreach ($this->errorInfo() as $info) | |
338 | $err .= "$info\n"; | |
339 | $this->rollBack(); | |
340 | return $err; | |
341 | } | |
342 | $sth->bindColumn(1, $id); | |
343 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
344 | $err = "$sql\n"; | |
345 | foreach ($this->errorInfo() as $info) | |
346 | $err .= "$info\n"; | |
347 | $this->rollBack(); | |
348 | return $err; | |
349 | } | |
350 | $sth->fetch(PDO::FETCH_BOUND); | |
351 | //fwrite($fp, "Returned ID for created calendar: $id\n"); | |
352 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
353 | $err = "$sql\n"; | |
354 | foreach ($this->errorInfo() as $info) | |
355 | $err .= "$info\n"; | |
356 | $this->rollBack(); | |
357 | return $err; | |
358 | } | |
359 | $sth->closeCursor(); | |
360 | $this->commit(); | |
361 | /* $sql = "select * from calendar where id = $id"; | |
362 | $sth = $this->prepare($sql); | |
363 | $sth->execute(); | |
364 | $result = $sth->fetchAll(PDO::FETCH_ASSOC); | |
365 | if ($result) | |
366 | fwrite($fp, var_export($result[0], TRUE) . "\n"); | |
367 | else | |
368 | fwrite($fp, "No date found in calendar relation\n"); | |
369 | fclose($fp);*/ | |
370 | return $id; | |
371 | } | |
372 | ||
373 | function deleteCalendar($uid, $id) { | |
374 | $this->beginTransaction(); | |
375 | $sql = "select id from users where uid = ?"; | |
376 | $sth = $this->prepare($sql); | |
377 | if (! $sth) { | |
378 | $err = "$sql\n"; | |
379 | foreach ($this->errorInfo() as $info) | |
380 | $err .= "$info\n"; | |
381 | $this->rollBack(); | |
382 | return $err; | |
383 | } | |
384 | $sth->execute(array($uid)); | |
385 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
386 | $err = "$sql\n"; | |
387 | foreach ($this->errorInfo() as $info) | |
388 | $err .= "$info\n"; | |
389 | $this->rollBack(); | |
390 | return $err; | |
391 | } | |
392 | $sth->bindColumn(1, $uid); | |
393 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
394 | $err = "$sql\n"; | |
395 | foreach ($this->errorInfo() as $info) | |
396 | $err .= "$info\n"; | |
397 | $this->rollBack(); | |
398 | return $err; | |
399 | } | |
400 | $sth->fetch(PDO::FETCH_BOUND); | |
401 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
402 | $err = "$sql\n"; | |
403 | foreach ($this->errorInfo() as $info) | |
404 | $err .= "$info\n"; | |
405 | $this->rollBack(); | |
406 | return $err; | |
407 | } | |
408 | $sth->closeCursor(); | |
409 | $sql = "delete from calendar where id = ? and uid = $uid"; | |
410 | $sth = $this->prepare($sql); | |
411 | if (! $sth) { | |
412 | $err = "$sql\n"; | |
413 | foreach ($this->errorInfo() as $info) | |
414 | $err .= "$info\n"; | |
415 | $this->rollBack(); | |
416 | return $err; | |
417 | } | |
418 | $sth->execute(array($id)); | |
419 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
420 | $err = "$sql\n"; | |
421 | foreach ($this->errorInfo() as $info) | |
422 | $err .= "$info\n"; | |
423 | $this->rollBack(); | |
424 | return $err; | |
425 | } | |
426 | $this->commit(); | |
427 | return TRUE; | |
428 | } | |
429 | ||
430 | function updateCalendar($uid, $id, CalendarInfo $cal) { | |
431 | //echo "$uid:$id:".var_export($cal,true)."<br/>"; | |
432 | $this->beginTransaction(); | |
433 | $sql = "select id from users where uid = ?"; | |
434 | $sth = $this->prepare($sql); | |
435 | if (! $sth) { | |
436 | $err = "$sql\n"; | |
437 | foreach ($this->errorInfo() as $info) | |
438 | $err .= "$info\n"; | |
439 | $this->rollBack(); | |
440 | return $err; | |
441 | } | |
442 | $sth->execute(array($uid)); | |
443 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
444 | $err = "$sql\n"; | |
445 | foreach ($this->errorInfo() as $info) | |
446 | $err .= "$info\n"; | |
447 | $this->rollBack(); | |
448 | return $err; | |
449 | } | |
450 | $sth->bindColumn(1, $uid); | |
451 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
452 | $err = "$sql\n"; | |
453 | foreach ($this->errorInfo() as $info) | |
454 | $err .= "$info\n"; | |
455 | $this->rollBack(); | |
456 | return $err; | |
457 | } | |
458 | $sth->fetch(PDO::FETCH_BOUND); | |
459 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
460 | $err = "$sql\n"; | |
461 | foreach ($this->errorInfo() as $info) | |
462 | $err .= "$info\n"; | |
463 | $this->rollBack(); | |
464 | return $err; | |
465 | } | |
466 | $sth->closeCursor(); | |
467 | $sql = "update calendar set name = ?, color = ?, config = ? | |
468 | where id = ? and uid = $uid"; | |
469 | $sth = $this->prepare($sql); | |
470 | if (! $sth) { | |
471 | $err = "$sql\n"; | |
472 | foreach ($this->errorInfo() as $info) | |
473 | $err .= "$info\n"; | |
474 | $this->rollBack(); | |
475 | return $err; | |
476 | } | |
477 | //$sth->execute(array($cal->name,$cal->color,base64_encode($cal->config),$id)); | |
478 | $sth->execute(array($cal->name,$cal->color, $cal->config,$id)); | |
479 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
480 | $err = "$sql\n"; | |
481 | foreach ($this->errorInfo() as $info) | |
482 | $err .= "$info\n"; | |
483 | $this->rollBack(); | |
484 | return $err; | |
485 | } | |
486 | $this->commit(); | |
487 | return TRUE; | |
488 | } | |
489 | ||
490 | function changePassword($uid, $pwd) { | |
491 | $this->beginTransaction(); | |
492 | $sql = "update users set pwd = ? where uid = ?"; | |
493 | $sth = $this->prepare($sql); | |
494 | if (! $sth) { | |
495 | $err = "$sql\n"; | |
496 | foreach ($this->errorInfo() as $info) | |
497 | $err .= "$info\n"; | |
498 | return $err; | |
499 | } | |
500 | $sth->execute(array($pwd,$uid)); | |
501 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
502 | $this->rollBack(); | |
503 | $err = "$sql\n"; | |
504 | foreach ($this->errorInfo() as $info) | |
505 | $err .= "$info\n"; | |
506 | return $err; | |
507 | } | |
508 | $this->commit(); | |
509 | return TRUE; | |
510 | } | |
511 | ||
512 | function getAllUsers($limit, $offset) { | |
513 | if ($limit == -1) { | |
514 | $sql = "select u.uid, r.name as userrole from users u, roles r | |
515 | where u.userrole = r.id limit all offset ?"; | |
516 | } | |
517 | else { | |
518 | $sql = "select u.uid, r.name as userrole from users u, roles r | |
519 | where u.userrole = r.id limit ? offset ?"; | |
520 | } | |
521 | $sth = $this->prepare($sql); | |
522 | if (! $sth) { | |
523 | $err = "$sql\n"; | |
524 | foreach ($this->errorInfo() as $info) | |
525 | $err .= "$info\n"; | |
526 | return $err; | |
527 | } | |
528 | if ($limit == -1) | |
529 | $sth->execute(array($offset)); | |
530 | else | |
531 | $sth->execute(array($limit,$offset)); | |
532 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
533 | $err = "$sql\n"; | |
534 | foreach ($this->errorInfo() as $info) | |
535 | $err .= "$info\n"; | |
536 | return $err; | |
537 | } | |
538 | return $sth->fetchAll(PDO::FETCH_ASSOC); | |
539 | } | |
540 | ||
541 | function getRoles() { | |
542 | $sql = "select id, name from roles order by id desc"; | |
543 | $sth = $this->prepare($sql); | |
544 | if (! $sth) { | |
545 | $err = "$sql\n"; | |
546 | foreach ($this->errorInfo() as $info) | |
547 | $err .= "$info\n"; | |
548 | return $err; | |
549 | } | |
550 | $sth->execute(); | |
551 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
552 | $err = "$sql\n"; | |
553 | foreach ($this->errorInfo() as $info) | |
554 | $err .= "$info\n"; | |
555 | return $err; | |
556 | } | |
557 | return $sth->fetchAll(PDO::FETCH_ASSOC); | |
558 | } | |
559 | ||
560 | function getRoleName($id) { | |
561 | $sql = "select name from roles where id = ?"; | |
562 | $sth = $this->prepare($sql); | |
563 | if (! $sth) { | |
564 | $err = "$sql\n"; | |
565 | foreach ($this->errorInfo() as $info) | |
566 | $err .= "$info\n"; | |
567 | return $err; | |
568 | } | |
569 | $sth->execute(array($id)); | |
570 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
571 | $err = "$sql\n"; | |
572 | foreach ($this->errorInfo() as $info) | |
573 | $err .= "$info\n"; | |
574 | return $err; | |
575 | } | |
576 | return $sth->fetch(PDO::FETCH_ASSOC); | |
577 | } | |
578 | ||
579 | function getVersion() { | |
580 | $version = array('version' => 0); | |
581 | $sql = "select count(*) as exist from pg_tables where tablename = 'about'"; | |
582 | $sth = $this->prepare($sql); | |
583 | if (! $sth) { | |
584 | $err = "$sql\n"; | |
585 | foreach ($this->errorInfo() as $info) | |
586 | $err .= "$info\n"; | |
587 | return $err; | |
588 | } | |
589 | $sth->execute(); | |
590 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
591 | $err = "$sql\n"; | |
592 | foreach ($this->errorInfo() as $info) | |
593 | $err .= "$info\n"; | |
594 | return $err; | |
595 | } | |
596 | $res = $sth->fetch(); | |
597 | if ($res['exist'] > 0) { | |
598 | $sql = "select version from about where id = 1"; | |
599 | $sth = $this->prepare($sql); | |
600 | if (! $sth) { | |
601 | $err = "$sql\n"; | |
602 | foreach ($this->errorInfo() as $info) | |
603 | $err .= "$info\n"; | |
604 | return $err; | |
605 | } | |
606 | $sth->execute(); | |
607 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
608 | $err = "$sql\n"; | |
609 | foreach ($this->errorInfo() as $info) | |
610 | $err .= "$info\n"; | |
611 | return $err; | |
612 | } | |
613 | $res = $sth->fetch(); | |
614 | $version = $res; | |
615 | } | |
616 | return $version; | |
617 | } | |
618 | ||
619 | function execute($sql) { | |
620 | $sth = $this->prepare($sql); | |
621 | if (! $sth) { | |
622 | $err = "$sql\n"; | |
623 | foreach ($this->errorInfo() as $info) | |
624 | $err .= "$info\n"; | |
625 | return $err; | |
626 | } | |
627 | $sth->execute(); | |
628 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
629 | $err = "$sql\n"; | |
630 | foreach ($this->errorInfo() as $info) | |
631 | $err .= "$info\n"; | |
632 | return $err; | |
633 | } | |
634 | return true; | |
635 | } | |
636 | ||
637 | function getCalendarConfig($id) { | |
638 | if ($id == -1) { | |
639 | $sql = "select id, config from calendar"; | |
640 | } | |
641 | else { | |
642 | $sql = "select id, config from calendar where id = ?"; | |
643 | } | |
644 | $sth = $this->prepare($sql); | |
645 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
646 | $err = "$sql\n"; | |
647 | $err .= $this->getMsg($sth)."\n"; | |
648 | return $err; | |
649 | } | |
650 | $sth->execute(array($id)); | |
651 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
652 | $err = "$sql\n"; | |
653 | $err .= $this->getMsg($sth)."\n"; | |
654 | return $err; | |
655 | } | |
656 | return $sth->fetchAll(PDO::FETCH_ASSOC); | |
657 | } | |
658 | ||
659 | function nextTableNumber($name) { | |
660 | $sql = "select count(*) as found from pg_tables where tableowner = 'webcal' " . | |
661 | "and tablename like ?"; | |
662 | $sth = $this->prepare($sql); | |
663 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
664 | $err = "$sql\n"; | |
665 | $err .= $this->getMsg($sth)."\n"; | |
666 | return $err; | |
667 | } | |
668 | $sth->execute(array($name . '%')); | |
669 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
670 | $err = "$sql\n"; | |
671 | $err .= $this->getMsg($sth)."\n"; | |
672 | return $err; | |
673 | } | |
674 | $res = $sth->fetch(PDO::FETCH_ASSOC); | |
675 | return $res['found']; | |
676 | } | |
677 | ||
678 | function getLdapConfig() { | |
679 | $sql = "select enable, dns, tls, base_dn, user_attr from ldap"; | |
680 | $sth = $this->prepare($sql); | |
681 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
682 | $err = "$sql\n"; | |
683 | $err .= $this->getMsg($sth)."\n"; | |
684 | return $err; | |
685 | } | |
686 | $sth->execute(); | |
687 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
688 | $err = "$sql\n"; | |
689 | $err .= $this->getMsg($sth)."\n"; | |
690 | return $err; | |
691 | } | |
692 | return $sth->fetch(PDO::FETCH_ASSOC); | |
693 | } | |
694 | ||
695 | function setLdapConfig(array $config) { | |
696 | $old_config = $this->getLdapConfig(); | |
697 | if (! is_array($old_config) && $old_config) | |
698 | return $old_config; | |
699 | else if (is_array($old_config)) | |
700 | $sql = "update ldap set enable=?, dns=?, tls=?, base_dn=?, user_attr=?"; | |
701 | else | |
702 | $sql = "insert into ldap values(?, ?, ?, ?, ?)"; | |
703 | $this->beginTransaction(); | |
704 | $sth = $this->prepare($sql); | |
705 | if (! $sth) { | |
706 | $err = "$sql\n"; | |
707 | foreach ($this->errorInfo() as $info) | |
708 | $err .= "$info\n"; | |
709 | $this->rollBack(); | |
710 | return $err; | |
711 | } | |
712 | $sth->execute($config); | |
713 | if ($sth->errorCode() && $sth->errorCode() != '00000') { | |
714 | $this->rollBack(); | |
715 | $err = "$sql\n"; | |
716 | foreach ($this->errorInfo() as $info) | |
717 | $err .= "$info\n"; | |
718 | return $err; | |
719 | } | |
720 | $this->commit(); | |
721 | return true; | |
722 | } | |
723 | ||
724 | } |