]>
git.datanom.net - webcal.git/blob - utils/pgsql.php
4 include_once 'config.inc.php';
5 require_once 'persistens.php';
6 require_once 'helper.php';
8 class Pgsql
extends PDO
implements WebcalSupport
{
12 function __construct($dns = NULL) {
14 $db_dns = "pgsql:$dns";
16 $db_dns = 'pgsql:'.DNS
;
18 parent
::__construct($db_dns);
19 $locale = iconv_get_encoding('internal_encoding');
20 parent
::exec("set client_encoding = '$locale'");
22 catch (PDOException
$ex) {
23 throw new Exception($ex->getMessage());
27 private function getMsg($resource) {
28 $err = $resource->errorInfo();
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";
40 $sth = $this->prepare($locale);
43 if (strcasecmp($res['encoding'], 'UTF8') !== 0)
44 throw new Exception("Template0 encoding [".$res['encoding']."]. Must be UTF8");
48 catch (PDOException
$ex) {
49 throw new Exception($ex->getMessage());
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));
58 if ($res['exist'] != 0) {
59 $this->beginTransaction();
60 include_once 'db_create.postgresql.php';
62 foreach ($db as $sql) {
64 $sth = $this->prepare($sql);
68 foreach ($this->errorInfo() as $info)
70 throw new Exception($err);
73 //throw new Exception(var_export($sth, true));
74 if ($sth->errorCode() && $sth->errorCode() != '00000') {
77 foreach ($this->errorInfo() as $info)
79 throw new Exception($err);
85 throw new Exception("$name: Database does not exist");
88 function getViewStyle($uid) {
89 $sql = "select userview from users u where u.uid = ?";
90 $sth = $this->prepare($sql);
91 $sth->execute(array($uid));
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();
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();
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();
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';
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();
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();
144 function authenticate($uid, $pwd) {
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);
153 foreach ($this->errorInfo() as $info)
155 throw new Exception($err);
157 $sth->execute(array($uid,$pwd));
158 if ($sth->errorCode() && $sth->errorCode() != '00000') {
160 foreach ($this->errorInfo() as $info)
162 throw new Exception($err);
164 $res = $sth->fetchAll(PDO
::FETCH_ASSOC
);
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'];
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']}");
181 $sql = "insert into users (uid, pwd, timezone, userrole, seckey, pubkey, " .
182 "timeout, userview, weekstart, daystart, dayend) values (?,?,?,?,?,?,?,?,?,?,?)";
183 $sth = $this->prepare($sql);
185 foreach ($this->errorInfo() as $info)
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') {
197 foreach ($this->errorInfo() as $info)
205 function setUserSettings($uid, $data) {
206 $sql = "update users set userrole = ?, timeout = ?, userview = ?,
207 weekstart = ?, daystart = ?, dayend = ?, timezone = ?
209 $sth = $this->prepare($sql);
212 foreach ($this->errorInfo() as $info)
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') {
223 foreach ($this->errorInfo() as $info)
231 function changeDefault($data) {
232 $this->beginTransaction();
233 foreach ($data as $column => $default) {
234 $sql = "alter table users alter $column set default $default";
236 $res = $this->exec($sql);
237 if ($res === FALSE) {
239 foreach ($this->errorInfo() as $info)
249 function deleteUser($uid) {
250 $sql = "delete from users where uid = ?";
251 $sth = $this->prepare($sql);
254 foreach ($this->errorInfo() as $info)
258 $this->beginTransaction();
259 $sth->execute(array($uid));
260 if ($sth->errorCode() && $sth->errorCode() != '00000') {
263 foreach ($this->errorInfo() as $info)
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);
280 foreach ($this->errorInfo() as $info)
285 $sth->execute(array($uid));
286 if ($sth->errorCode() && $sth->errorCode() != '00000') {
288 foreach ($this->errorInfo() as $info)
293 $sth->bindColumn(1, $id);
294 if ($sth->errorCode() && $sth->errorCode() != '00000') {
296 foreach ($this->errorInfo() as $info)
301 $sth->fetch(PDO
::FETCH_BOUND
);
302 //fwrite($fp, "Returned users ID: $id\n");
303 if ($sth->errorCode() && $sth->errorCode() != '00000') {
305 foreach ($this->errorInfo() as $info)
311 $sql = "insert into calendar (uid, name, color, config)
314 //fwrite($fp, "Insert into calendar: $sql\n");
315 $sth = $this->prepare($sql);
318 foreach ($this->errorInfo() as $info)
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') {
327 foreach ($this->errorInfo() as $info)
333 $sth = $this->prepare('select max(id) from calendar');
335 if ($sth->errorCode() && $sth->errorCode() != '00000') {
337 foreach ($this->errorInfo() as $info)
342 $sth->bindColumn(1, $id);
343 if ($sth->errorCode() && $sth->errorCode() != '00000') {
345 foreach ($this->errorInfo() as $info)
350 $sth->fetch(PDO
::FETCH_BOUND
);
351 //fwrite($fp, "Returned ID for created calendar: $id\n");
352 if ($sth->errorCode() && $sth->errorCode() != '00000') {
354 foreach ($this->errorInfo() as $info)
361 /* $sql = "select * from calendar where id = $id";
362 $sth = $this->prepare($sql);
364 $result = $sth->fetchAll(PDO::FETCH_ASSOC);
366 fwrite($fp, var_export($result[0], TRUE) . "\n");
368 fwrite($fp, "No date found in calendar relation\n");
373 function deleteCalendar($uid, $id) {
374 $this->beginTransaction();
375 $sql = "select id from users where uid = ?";
376 $sth = $this->prepare($sql);
379 foreach ($this->errorInfo() as $info)
384 $sth->execute(array($uid));
385 if ($sth->errorCode() && $sth->errorCode() != '00000') {
387 foreach ($this->errorInfo() as $info)
392 $sth->bindColumn(1, $uid);
393 if ($sth->errorCode() && $sth->errorCode() != '00000') {
395 foreach ($this->errorInfo() as $info)
400 $sth->fetch(PDO
::FETCH_BOUND
);
401 if ($sth->errorCode() && $sth->errorCode() != '00000') {
403 foreach ($this->errorInfo() as $info)
409 $sql = "delete from calendar where id = ? and uid = $uid";
410 $sth = $this->prepare($sql);
413 foreach ($this->errorInfo() as $info)
418 $sth->execute(array($id));
419 if ($sth->errorCode() && $sth->errorCode() != '00000') {
421 foreach ($this->errorInfo() as $info)
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);
437 foreach ($this->errorInfo() as $info)
442 $sth->execute(array($uid));
443 if ($sth->errorCode() && $sth->errorCode() != '00000') {
445 foreach ($this->errorInfo() as $info)
450 $sth->bindColumn(1, $uid);
451 if ($sth->errorCode() && $sth->errorCode() != '00000') {
453 foreach ($this->errorInfo() as $info)
458 $sth->fetch(PDO
::FETCH_BOUND
);
459 if ($sth->errorCode() && $sth->errorCode() != '00000') {
461 foreach ($this->errorInfo() as $info)
467 $sql = "update calendar set name = ?, color = ?, config = ?
468 where id = ? and uid = $uid";
469 $sth = $this->prepare($sql);
472 foreach ($this->errorInfo() as $info)
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') {
481 foreach ($this->errorInfo() as $info)
490 function changePassword($uid, $pwd) {
491 $this->beginTransaction();
492 $sql = "update users set pwd = ? where uid = ?";
493 $sth = $this->prepare($sql);
496 foreach ($this->errorInfo() as $info)
500 $sth->execute(array($pwd,$uid));
501 if ($sth->errorCode() && $sth->errorCode() != '00000') {
504 foreach ($this->errorInfo() as $info)
512 function getAllUsers($limit, $offset) {
514 $sql = "select u.uid, r.name as userrole from users u, roles r
515 where u.userrole = r.id limit all offset ?";
518 $sql = "select u.uid, r.name as userrole from users u, roles r
519 where u.userrole = r.id limit ? offset ?";
521 $sth = $this->prepare($sql);
524 foreach ($this->errorInfo() as $info)
529 $sth->execute(array($offset));
531 $sth->execute(array($limit,$offset));
532 if ($sth->errorCode() && $sth->errorCode() != '00000') {
534 foreach ($this->errorInfo() as $info)
538 return $sth->fetchAll(PDO
::FETCH_ASSOC
);
541 function getRoles() {
542 $sql = "select id, name from roles order by id desc";
543 $sth = $this->prepare($sql);
546 foreach ($this->errorInfo() as $info)
551 if ($sth->errorCode() && $sth->errorCode() != '00000') {
553 foreach ($this->errorInfo() as $info)
557 return $sth->fetchAll(PDO
::FETCH_ASSOC
);
560 function getRoleName($id) {
561 $sql = "select name from roles where id = ?";
562 $sth = $this->prepare($sql);
565 foreach ($this->errorInfo() as $info)
569 $sth->execute(array($id));
570 if ($sth->errorCode() && $sth->errorCode() != '00000') {
572 foreach ($this->errorInfo() as $info)
576 return $sth->fetch(PDO
::FETCH_ASSOC
);
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);
585 foreach ($this->errorInfo() as $info)
590 if ($sth->errorCode() && $sth->errorCode() != '00000') {
592 foreach ($this->errorInfo() as $info)
596 $res = $sth->fetch();
597 if ($res['exist'] > 0) {
598 $sql = "select version from about where id = 1";
599 $sth = $this->prepare($sql);
602 foreach ($this->errorInfo() as $info)
607 if ($sth->errorCode() && $sth->errorCode() != '00000') {
609 foreach ($this->errorInfo() as $info)
613 $res = $sth->fetch();
619 function execute($sql) {
620 $sth = $this->prepare($sql);
623 foreach ($this->errorInfo() as $info)
628 if ($sth->errorCode() && $sth->errorCode() != '00000') {
630 foreach ($this->errorInfo() as $info)
637 function getCalendarConfig($id) {
639 $sql = "select id, config from calendar";
642 $sql = "select id, config from calendar where id = ?";
644 $sth = $this->prepare($sql);
645 if ($sth->errorCode() && $sth->errorCode() != '00000') {
647 $err .= $this->getMsg($sth)."\n";
650 $sth->execute(array($id));
651 if ($sth->errorCode() && $sth->errorCode() != '00000') {
653 $err .= $this->getMsg($sth)."\n";
656 return $sth->fetchAll(PDO
::FETCH_ASSOC
);
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') {
665 $err .= $this->getMsg($sth)."\n";
668 $sth->execute(array($name . '%'));
669 if ($sth->errorCode() && $sth->errorCode() != '00000') {
671 $err .= $this->getMsg($sth)."\n";
674 $res = $sth->fetch(PDO
::FETCH_ASSOC
);
675 return $res['found'];
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') {
683 $err .= $this->getMsg($sth)."\n";
687 if ($sth->errorCode() && $sth->errorCode() != '00000') {
689 $err .= $this->getMsg($sth)."\n";
692 return $sth->fetch(PDO
::FETCH_ASSOC
);
695 function setLdapConfig(array $config) {
696 $old_config = $this->getLdapConfig();
697 if (! is_array($old_config) && $old_config)
699 else if (is_array($old_config))
700 $sql = "update ldap set enable=?, dns=?, tls=?, base_dn=?, user_attr=?";
702 $sql = "insert into ldap values(?, ?, ?, ?, ?)";
703 $this->beginTransaction();
704 $sth = $this->prepare($sql);
707 foreach ($this->errorInfo() as $info)
712 $sth->execute($config);
713 if ($sth->errorCode() && $sth->errorCode() != '00000') {
716 foreach ($this->errorInfo() as $info)
This page took 0.151791 seconds and 6 git commands to generate.