Updated version for 1.2.0 release...
[squirrelmail.git] / functions / abook_database.php
1 <?php
2
3 /**
4 * abook_database.php
5 *
6 * Copyright (c) 1999-2002 The Squirrelmail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Backend for personal addressbook stored in a database,
10 * accessed using the DB-classes in PEAR.
11 *
12 * IMPORTANT: The PEAR modules must be in the include path
13 * for this class to work.
14 *
15 * An array with the following elements must be passed to
16 * the class constructor (elements marked ? are optional):
17 *
18 * dsn => database DNS (see PEAR for syntax)
19 * table => table to store addresses in (must exist)
20 * owner => current user (owner of address data)
21 * ? writeable => set writeable flag (true/false)
22 *
23 * The table used should have the following columns:
24 * owner, nickname, firstname, lastname, email, label
25 * The pair (owner,nickname) should be unique (primary key).
26 *
27 * NOTE. This class should not be used directly. Use the
28 * "AddressBook" class instead.
29 *
30 * $Id$
31 */
32
33 require_once('DB.php');
34
35 class abook_database extends addressbook_backend {
36 var $btype = 'local';
37 var $bname = 'database';
38
39 var $dsn = '';
40 var $table = '';
41 var $owner = '';
42 var $dbh = false;
43
44 var $writeable = true;
45
46 /* ========================== Private ======================= */
47
48 /* Constructor */
49 function abook_database($param) {
50 $this->sname = _("Personal address book");
51
52 if (is_array($param)) {
53 if (empty($param['dsn']) ||
54 empty($param['table']) ||
55 empty($param['owner'])) {
56 return $this->set_error('Invalid parameters');
57 }
58
59 $this->dsn = $param['dsn'];
60 $this->table = $param['table'];
61 $this->owner = $param['owner'];
62
63 if (!empty($param['name'])) {
64 $this->sname = $param['name'];
65 }
66
67 if (isset($param['writeable'])) {
68 $this->writeable = $param['writeable'];
69 }
70
71 $this->open(true);
72 }
73 else {
74 return $this->set_error('Invalid argument to constructor');
75 }
76 }
77
78
79 /* Open the database. New connection if $new is true */
80 function open($new = false) {
81 $this->error = '';
82
83 /* Return true is file is open and $new is unset */
84 if ($this->dbh && !$new) {
85 return true;
86 }
87
88 /* Close old file, if any */
89 if ($this->dbh) {
90 $this->close();
91 }
92
93 $dbh = DB::connect($this->dsn, true);
94
95 if (DB::isError($dbh) || DB::isWarning($dbh)) {
96 return $this->set_error(sprintf(_("Database error: %s"),
97 DB::errorMessage($dbh)));
98 }
99
100 $this->dbh = $dbh;
101 return true;
102 }
103
104 /* Close the file and forget the filehandle */
105 function close() {
106 $this->dbh->disconnect();
107 $this->dbh = false;
108 }
109
110 /* ========================== Public ======================== */
111
112 /* Search the file */
113 function &search($expr) {
114 $ret = array();
115 if(!$this->open()) {
116 return false;
117 }
118
119 /* To be replaced by advanded search expression parsing */
120 if (is_array($expr)) {
121 return;
122 }
123
124 /* Make regexp from glob'ed expression */
125 $expr = str_replace('?', '_', $expr);
126 $expr = str_replace('*', '%', $expr);
127 $expr = $this->dbh->quoteString($expr);
128 $expr = "%$expr%";
129
130 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
131 "(firstname LIKE '%s' OR lastname LIKE '%s')",
132 $this->table, $this->owner, $expr, $expr);
133 $res = $this->dbh->query($query);
134
135 if (DB::isError($res)) {
136 return $this->set_error(sprintf(_("Database error: %s"),
137 DB::errorMessage($res)));
138 }
139
140 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
141 array_push($ret, array('nickname' => $row['nickname'],
142 'name' => "$row[firstname] $row[lastname]",
143 'firstname' => $row['firstname'],
144 'lastname' => $row['lastname'],
145 'email' => $row['email'],
146 'label' => $row['label'],
147 'backend' => $this->bnum,
148 'source' => &$this->sname));
149 }
150 return $ret;
151 }
152
153 /* Lookup alias */
154 function &lookup($alias) {
155 if (empty($alias)) {
156 return array();
157 }
158
159 $alias = strtolower($alias);
160
161 if (!$this->open()) {
162 return false;
163 }
164
165 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND nickname='%s'",
166 $this->table, $this->owner, $alias);
167
168 $res = $this->dbh->query($query);
169
170 if (DB::isError($res)) {
171 return $this->set_error(sprintf(_("Database error: %s"),
172 DB::errorMessage($res)));
173 }
174
175 if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
176 return array('nickname' => $row['nickname'],
177 'name' => "$row[firstname] $row[lastname]",
178 'firstname' => $row['firstname'],
179 'lastname' => $row['lastname'],
180 'email' => $row['email'],
181 'label' => $row['label'],
182 'backend' => $this->bnum,
183 'source' => &$this->sname);
184 }
185 return array();
186 }
187
188 /* List all addresses */
189 function &list_addr() {
190 $ret = array();
191 if (!$this->open()) {
192 return false;
193 }
194
195 $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
196 $this->table, $this->owner);
197
198 $res = $this->dbh->query($query);
199
200 if (DB::isError($res)) {
201 return $this->set_error(sprintf(_("Database error: %s"),
202 DB::errorMessage($res)));
203 }
204
205 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
206 array_push($ret, array('nickname' => $row['nickname'],
207 'name' => "$row[firstname] $row[lastname]",
208 'firstname' => $row['firstname'],
209 'lastname' => $row['lastname'],
210 'email' => $row['email'],
211 'label' => $row['label'],
212 'backend' => $this->bnum,
213 'source' => &$this->sname));
214 }
215 return $ret;
216 }
217
218 /* Add address */
219 function add($userdata) {
220 if (!$this->writeable) {
221 return $this->set_error(_("Addressbook is read-only"));
222 }
223
224 if (!$this->open()) {
225 return false;
226 }
227
228 /* See if user exist already */
229 $ret = $this->lookup($userdata['nickname']);
230 if (!empty($ret)) {
231 return $this->set_error(sprintf(_("User '%s' already exist"),
232 $ret['nickname']));
233 }
234
235 /* Create query */
236 $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
237 "lastname, email, label) VALUES('%s','%s','%s'," .
238 "'%s','%s','%s')",
239 $this->table, $this->owner,
240 $this->dbh->quoteString($userdata['nickname']),
241 $this->dbh->quoteString($userdata['firstname']),
242 $this->dbh->quoteString($userdata['lastname']),
243 $this->dbh->quoteString($userdata['email']),
244 $this->dbh->quoteString($userdata['label']) );
245
246 /* Do the insert */
247 $r = $this->dbh->simpleQuery($query);
248 if ($r == DB_OK) {
249 return true;
250 }
251
252 /* Fail */
253 return $this->set_error(sprintf(_("Database error: %s"),
254 DB::errorMessage($r)));
255 }
256
257 /* Delete address */
258 function remove($alias) {
259 if (!$this->writeable) {
260 return $this->set_error(_("Addressbook is read-only"));
261 }
262
263 if (!$this->open()) {
264 return false;
265 }
266
267 /* Create query */
268 $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
269 $this->table, $this->owner);
270
271 $sepstr = '';
272 while (list($undef, $nickname) = each($alias)) {
273 $query .= sprintf("%s nickname='%s' ", $sepstr,
274 $this->dbh->quoteString($nickname));
275 $sepstr = 'OR';
276 }
277 $query .= ')';
278
279 /* Delete entry */
280 $r = $this->dbh->simpleQuery($query);
281 if ($r == DB_OK) {
282 return true;
283 }
284
285 /* Fail */
286 return $this->set_error(sprintf(_("Database error: %s"),
287 DB::errorMessage($r)));
288 }
289
290 /* Modify address */
291 function modify($alias, $userdata) {
292 if (!$this->writeable) {
293 return $this->set_error(_("Addressbook is read-only"));
294 }
295
296 if (!$this->open()) {
297 return false;
298 }
299
300 /* See if user exist */
301 $ret = $this->lookup($alias);
302 if (empty($ret)) {
303 return $this->set_error(sprintf(_("User '%s' does not exist"),
304 $alias));
305 }
306
307 /* Create query */
308 $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
309 "lastname='%s', email='%s', label='%s' ".
310 "WHERE owner='%s' AND nickname='%s'",
311 $this->table,
312 $this->dbh->quoteString($userdata['nickname']),
313 $this->dbh->quoteString($userdata['firstname']),
314 $this->dbh->quoteString($userdata['lastname']),
315 $this->dbh->quoteString($userdata['email']),
316 $this->dbh->quoteString($userdata['label']),
317 $this->owner,
318 $this->dbh->quoteString($alias) );
319
320 /* Do the insert */
321 $r = $this->dbh->simpleQuery($query);
322 if ($r == DB_OK) {
323 return true;
324 }
325
326 /* Fail */
327 return $this->set_error(sprintf(_("Database error: %s"),
328 DB::errorMessage($r)));
329 }
330 } /* End of class abook_database */
331
332 ?>