084c0622ee81a144a1c97e70ab54fd215a796dd3
[infodrom/hallinta] / class / database.class.php
1 <?php
2
3 class Database {
4   private $db;
5   private $error_log = false;
6   private $error_mail = false;
7   private $lastInsertTable = null;
8
9   public function __construct($driver, $host, $dbname, $user, $passwd)
10   {
11     $this->db = new PDO(sprintf('%s:host=%s;dbname=%s', $driver, $host, $dbname), $user, $passwd);
12
13     #    $this->db->query("SET NAMES 'utf8'"); 
14     #    $this->db->query("SET CHARACTER SET 'utf8'");
15     #    $this->db->query("SET collation_connection = 'utf8_general_ci'");
16     #    $this->db->query("SET lc_time_names = 'de_DE'");
17   }
18
19   public function enableErrorLog()
20   {
21     $this->error_log = true;
22   }
23
24   public function disableErrorLog()
25   {
26     $this->error_log = false;
27   }
28
29   public function setErrorMail($address)
30   {
31     $this->error_mail = $address;
32   }
33
34   private function logQuery($sql)
35   {
36     if ($this->error_log) error_log(trim(str_replace("\n", ' ', $sql)));
37   }
38
39   private function handleError($sth, $sql)
40   {
41     if (!$this->error_log) error_log(trim(str_replace("\n", ' ', $sql)));
42     $arr = $this->db->errorInfo();
43     foreach (explode("\n", $arr[2]) as $line)
44       error_log($line);
45
46     if ($this->error_mail && defined('MAIL_FROM') && defined('MAIL_FROM_NAME')) {
47       $mail = new Mail();
48       $mail->env_from(MAIL_FROM);
49       $mail->set('From', mb_encode_mimeheader(utf8_decode(sprintf("%s <%s>", MAIL_FROM_NAME, MAIL_FROM)),'latin1'));
50       $mail->set('To', $this->error_mail);
51       $mail->set('Subject', 'SQL Error in ' . $_SERVER['REQUEST_URI']);
52
53       $body = sprintf("http://%s%s%s\nReferer: %s\nUser: %s\n\n%s\n\nError Number %d\n%s\n\n",
54                       $_SERVER['SERVER_NAME'],
55                       $_SERVER['SERVER_PORT'] != 80 ? ':'.$_SERVER['SERVER_PORT'] : '',
56                       $_SERVER['REQUEST_URI'],
57                       $_SERVER['HTTP_REFERER'],
58                       $_SESSION['sys']['login'],
59                       trim($sql),
60                       $arr[1],
61                       $arr[2]);
62
63       ob_start();
64       debug_print_backtrace();
65       $body .= ob_get_contents()."\n";
66       ob_end_clean();
67
68       if (count($_POST)) $body .= 'POST: ' . var_export($_POST,true)."\n";
69       if (count($_GET)) $body .= 'GET: ' . var_export($_GET,true)."\n";
70       if (count($_SESSION)) $body .= 'SESSION: ' . var_export($_SESSION,true)."\n";
71
72       $mail->send($body);
73     }
74   }
75
76   public function quote($string)
77   {
78     if (is_null($string)) return 'NULL';
79     if (strtolower($string) == 'now()' || strtolower($string) == 'current_timestamp') return 'now()';
80     return $this->db->quote($string);
81   }
82
83   public function lastInsertId()
84   {
85     if (DBDRIVER == 'pgsql')
86       return $this->db->lastInsertId($this->lastInsertTable);
87     else
88       return $this->db->lastInsertId();
89   }
90
91   public function query($sql)
92   {
93     $this->logQuery($sql);
94     $sth = $this->db->query($sql);
95
96     if ($sth === false) $this->handleError($sth,$sql);
97
98     if (preg_match('/INSERT\s+INTO\s+(\S+)\s+/i', $sql, $matches))
99       $this->lastInsertTable = $matches[1];
100
101     return $sth;
102   }
103
104   public function execute($sql)
105   {
106     return $this->query($sql) !== false;
107   }
108
109   public function fetchValue($sql)
110   {
111     $sth = $this->query($sql);
112     if ($sth === false) return false;
113     $ret = $sth->fetch(PDO::FETCH_BOTH);
114     if ($ret === false) return false;
115     return $ret[0];
116   }
117
118   public function fetchAssoc($sql)
119   {
120     $sth = $this->query($sql);
121     if ($sth === false) return false;
122     return $sth->fetch(PDO::FETCH_ASSOC);
123   }
124
125   public function fetchObject($sql)
126   {
127     $sth = $this->query($sql);
128     if ($sth === false) return false;
129     return $sth->fetchObject();
130   }
131
132   public function fetchAssocList($sql)
133   {
134     $result = array();
135     $sth = $this->query($sql);
136     if ($sth === false) return $result;
137     while ($row = $sth->fetch(PDO::FETCH_ASSOC))
138       $result[] = $row;
139     return $result;
140   }
141
142   public function fetchObjectList($sql)
143   {
144     $result = array();
145     $sth = $this->query($sql);
146     if ($sth === false) return $result;
147     while ($row = $sth->fetchObject())
148       $result[] = $row;
149     return $result;
150   }
151
152 }
153
154 ?>