9b9474d6 |
1 | <?php |
35586184 |
2 | /** |
3 | * abook_database.php |
4 | * |
6c84ba1e |
5 | * Copyright (c) 1999-2005 The SquirrelMail Project Team |
35586184 |
6 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
7 | * |
a9d318b0 |
8 | * @version $Id$ |
d6c32258 |
9 | * @package squirrelmail |
a9d318b0 |
10 | * @subpackage addressbook |
35586184 |
11 | */ |
d6c32258 |
12 | |
62f7daa5 |
13 | /** Needs the DB functions */ |
c9fcea56 |
14 | if (!include_once('DB.php')) { |
15 | // same error also in db_prefs.php |
16 | require_once(SM_PATH . 'functions/display_messages.php'); |
17 | $error = _("Could not include PEAR database functions required for the database backend.") . "<br />\n"; |
2ad4cea9 |
18 | $error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"), |
19 | '<tt>DB.php</tt>') . "<br />\n"; |
c9fcea56 |
20 | $error .= _("Please contact your system administrator and report this error."); |
21 | error_box($error, $color); |
17fca61d |
22 | exit; |
c9fcea56 |
23 | } |
d6c32258 |
24 | |
25 | /** |
0541c5ae |
26 | * Address book in a database backend |
27 | * |
28 | * Backend for personal/shared address book stored in a database, |
29 | * accessed using the DB-classes in PEAR. |
30 | * |
31 | * IMPORTANT: The PEAR modules must be in the include path |
32 | * for this class to work. |
33 | * |
34 | * An array with the following elements must be passed to |
35 | * the class constructor (elements marked ? are optional): |
36 | * <pre> |
37 | * dsn => database DNS (see PEAR for syntax) |
38 | * table => table to store addresses in (must exist) |
39 | * owner => current user (owner of address data) |
40 | * ? name => name of address book |
41 | * ? writeable => set writeable flag (true/false) |
42 | * ? listing => enable/disable listing |
43 | * </pre> |
44 | * The table used should have the following columns: |
45 | * owner, nickname, firstname, lastname, email, label |
46 | * The pair (owner,nickname) should be unique (primary key). |
47 | * |
48 | * NOTE. This class should not be used directly. Use the |
49 | * "AddressBook" class instead. |
d6c32258 |
50 | * @package squirrelmail |
0541c5ae |
51 | * @subpackage addressbook |
d6c32258 |
52 | */ |
9b9474d6 |
53 | class abook_database extends addressbook_backend { |
0541c5ae |
54 | /** |
55 | * Backend type |
56 | * @var string |
57 | */ |
9b9474d6 |
58 | var $btype = 'local'; |
0541c5ae |
59 | /** |
60 | * Backend name |
61 | * @var string |
62 | */ |
9b9474d6 |
63 | var $bname = 'database'; |
62f7daa5 |
64 | |
0541c5ae |
65 | /** |
66 | * Data Source Name (connection description) |
67 | * @var string |
68 | */ |
9b9474d6 |
69 | var $dsn = ''; |
0541c5ae |
70 | /** |
71 | * Table that stores addresses |
72 | * @var string |
73 | */ |
9b9474d6 |
74 | var $table = ''; |
0541c5ae |
75 | /** |
76 | * Owner name |
77 | * |
78 | * Limits list of database entries visible to end user |
79 | * @var string |
80 | */ |
9b9474d6 |
81 | var $owner = ''; |
0541c5ae |
82 | /** |
83 | * Database Handle |
84 | * @var resource |
85 | */ |
9b9474d6 |
86 | var $dbh = false; |
0541c5ae |
87 | /** |
88 | * Enable/disable writing into address book |
89 | * @var bool |
90 | */ |
9b9474d6 |
91 | var $writeable = true; |
0541c5ae |
92 | /** |
93 | * Enable/disable address book listing |
94 | * @var bool |
95 | */ |
96 | var $listing = true; |
62f7daa5 |
97 | |
9b9474d6 |
98 | /* ========================== Private ======================= */ |
62f7daa5 |
99 | |
0541c5ae |
100 | /** |
101 | * Constructor |
102 | * @param array $param address book backend options |
103 | */ |
9b9474d6 |
104 | function abook_database($param) { |
105 | $this->sname = _("Personal address book"); |
62f7daa5 |
106 | |
9b9474d6 |
107 | if (is_array($param)) { |
62f7daa5 |
108 | if (empty($param['dsn']) || |
109 | empty($param['table']) || |
9b9474d6 |
110 | empty($param['owner'])) { |
111 | return $this->set_error('Invalid parameters'); |
112 | } |
62f7daa5 |
113 | |
91be2362 |
114 | $this->dsn = $param['dsn']; |
115 | $this->table = $param['table']; |
116 | $this->owner = $param['owner']; |
62f7daa5 |
117 | |
9b9474d6 |
118 | if (!empty($param['name'])) { |
91be2362 |
119 | $this->sname = $param['name']; |
9b9474d6 |
120 | } |
7902aca2 |
121 | |
9b9474d6 |
122 | if (isset($param['writeable'])) { |
91be2362 |
123 | $this->writeable = $param['writeable']; |
9b9474d6 |
124 | } |
7902aca2 |
125 | |
30e9932c |
126 | if (isset($param['listing'])) { |
127 | $this->listing = $param['listing']; |
128 | } |
129 | |
7902aca2 |
130 | $this->open(true); |
9b9474d6 |
131 | } |
132 | else { |
91be2362 |
133 | return $this->set_error('Invalid argument to constructor'); |
9b9474d6 |
134 | } |
135 | } |
62f7daa5 |
136 | |
137 | |
0541c5ae |
138 | /** |
e50f5ac2 |
139 | * Open the database. |
0541c5ae |
140 | * @param bool $new new connection if it is true |
141 | * @return bool |
142 | */ |
9b9474d6 |
143 | function open($new = false) { |
144 | $this->error = ''; |
62f7daa5 |
145 | |
9b9474d6 |
146 | /* Return true is file is open and $new is unset */ |
147 | if ($this->dbh && !$new) { |
7902aca2 |
148 | return true; |
9b9474d6 |
149 | } |
62f7daa5 |
150 | |
9b9474d6 |
151 | /* Close old file, if any */ |
152 | if ($this->dbh) { |
153 | $this->close(); |
154 | } |
62f7daa5 |
155 | |
9b9474d6 |
156 | $dbh = DB::connect($this->dsn, true); |
62f7daa5 |
157 | |
286fe80b |
158 | if (DB::isError($dbh)) { |
701c9c6b |
159 | return $this->set_error(sprintf(_("Database error: %s"), |
7902aca2 |
160 | DB::errorMessage($dbh))); |
9b9474d6 |
161 | } |
62f7daa5 |
162 | |
9b9474d6 |
163 | $this->dbh = $dbh; |
164 | return true; |
165 | } |
7902aca2 |
166 | |
0541c5ae |
167 | /** |
168 | * Close the file and forget the filehandle |
169 | */ |
9b9474d6 |
170 | function close() { |
171 | $this->dbh->disconnect(); |
172 | $this->dbh = false; |
173 | } |
7902aca2 |
174 | |
9b9474d6 |
175 | /* ========================== Public ======================== */ |
62f7daa5 |
176 | |
0541c5ae |
177 | /** |
178 | * Search the database |
179 | * @param string $expr search expression |
180 | * @return array search results |
181 | */ |
9b9474d6 |
182 | function &search($expr) { |
183 | $ret = array(); |
184 | if(!$this->open()) { |
7902aca2 |
185 | return false; |
9b9474d6 |
186 | } |
30e9932c |
187 | |
9b9474d6 |
188 | /* To be replaced by advanded search expression parsing */ |
189 | if (is_array($expr)) { |
190 | return; |
191 | } |
192 | |
193 | /* Make regexp from glob'ed expression */ |
194 | $expr = str_replace('?', '_', $expr); |
195 | $expr = str_replace('*', '%', $expr); |
196 | $expr = $this->dbh->quoteString($expr); |
197 | $expr = "%$expr%"; |
198 | |
199 | $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " . |
200 | "(firstname LIKE '%s' OR lastname LIKE '%s')", |
201 | $this->table, $this->owner, $expr, $expr); |
202 | $res = $this->dbh->query($query); |
203 | |
204 | if (DB::isError($res)) { |
701c9c6b |
205 | return $this->set_error(sprintf(_("Database error: %s"), |
7902aca2 |
206 | DB::errorMessage($res))); |
9b9474d6 |
207 | } |
7902aca2 |
208 | |
9b9474d6 |
209 | while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { |
91be2362 |
210 | array_push($ret, array('nickname' => $row['nickname'], |
211 | 'name' => "$row[firstname] $row[lastname]", |
212 | 'firstname' => $row['firstname'], |
213 | 'lastname' => $row['lastname'], |
214 | 'email' => $row['email'], |
215 | 'label' => $row['label'], |
216 | 'backend' => $this->bnum, |
217 | 'source' => &$this->sname)); |
9b9474d6 |
218 | } |
219 | return $ret; |
220 | } |
62f7daa5 |
221 | |
0541c5ae |
222 | /** |
223 | * Lookup alias |
224 | * @param string $alias alias |
225 | * @return array search results |
226 | */ |
9b9474d6 |
227 | function &lookup($alias) { |
228 | if (empty($alias)) { |
7902aca2 |
229 | return array(); |
9b9474d6 |
230 | } |
62f7daa5 |
231 | |
9b9474d6 |
232 | $alias = strtolower($alias); |
7902aca2 |
233 | |
9b9474d6 |
234 | if (!$this->open()) { |
7902aca2 |
235 | return false; |
9b9474d6 |
236 | } |
62f7daa5 |
237 | |
2c90a9f2 |
238 | $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND LOWER(nickname)='%s'", |
11f2b6ba |
239 | $this->table, $this->owner, $this->dbh->quoteString($alias)); |
7902aca2 |
240 | |
9b9474d6 |
241 | $res = $this->dbh->query($query); |
7902aca2 |
242 | |
9b9474d6 |
243 | if (DB::isError($res)) { |
701c9c6b |
244 | return $this->set_error(sprintf(_("Database error: %s"), |
7902aca2 |
245 | DB::errorMessage($res))); |
9b9474d6 |
246 | } |
7902aca2 |
247 | |
9b9474d6 |
248 | if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { |
91be2362 |
249 | return array('nickname' => $row['nickname'], |
250 | 'name' => "$row[firstname] $row[lastname]", |
251 | 'firstname' => $row['firstname'], |
252 | 'lastname' => $row['lastname'], |
253 | 'email' => $row['email'], |
254 | 'label' => $row['label'], |
255 | 'backend' => $this->bnum, |
256 | 'source' => &$this->sname); |
9b9474d6 |
257 | } |
258 | return array(); |
259 | } |
260 | |
0541c5ae |
261 | /** |
e50f5ac2 |
262 | * List all addresses |
0541c5ae |
263 | * @return array search results |
264 | */ |
9b9474d6 |
265 | function &list_addr() { |
266 | $ret = array(); |
267 | if (!$this->open()) { |
7902aca2 |
268 | return false; |
9b9474d6 |
269 | } |
62f7daa5 |
270 | |
91e0dccc |
271 | if(isset($this->listing) && !$this->listing) { |
272 | return array(); |
273 | } |
30e9932c |
274 | |
7902aca2 |
275 | |
9b9474d6 |
276 | $query = sprintf("SELECT * FROM %s WHERE owner='%s'", |
277 | $this->table, $this->owner); |
7902aca2 |
278 | |
9b9474d6 |
279 | $res = $this->dbh->query($query); |
62f7daa5 |
280 | |
9b9474d6 |
281 | if (DB::isError($res)) { |
701c9c6b |
282 | return $this->set_error(sprintf(_("Database error: %s"), |
7902aca2 |
283 | DB::errorMessage($res))); |
9b9474d6 |
284 | } |
7902aca2 |
285 | |
9b9474d6 |
286 | while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { |
91be2362 |
287 | array_push($ret, array('nickname' => $row['nickname'], |
288 | 'name' => "$row[firstname] $row[lastname]", |
289 | 'firstname' => $row['firstname'], |
290 | 'lastname' => $row['lastname'], |
291 | 'email' => $row['email'], |
292 | 'label' => $row['label'], |
293 | 'backend' => $this->bnum, |
294 | 'source' => &$this->sname)); |
9b9474d6 |
295 | } |
296 | return $ret; |
297 | } |
7902aca2 |
298 | |
0541c5ae |
299 | /** |
300 | * Add address |
301 | * @param array $userdata added data |
302 | * @return bool |
303 | */ |
9b9474d6 |
304 | function add($userdata) { |
305 | if (!$this->writeable) { |
701c9c6b |
306 | return $this->set_error(_("Addressbook is read-only")); |
9b9474d6 |
307 | } |
7902aca2 |
308 | |
9b9474d6 |
309 | if (!$this->open()) { |
7902aca2 |
310 | return false; |
9b9474d6 |
311 | } |
62f7daa5 |
312 | |
9b9474d6 |
313 | /* See if user exist already */ |
314 | $ret = $this->lookup($userdata['nickname']); |
315 | if (!empty($ret)) { |
b9bfd165 |
316 | return $this->set_error(sprintf(_("User '%s' already exist"), |
91be2362 |
317 | $ret['nickname'])); |
9b9474d6 |
318 | } |
319 | |
320 | /* Create query */ |
321 | $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " . |
322 | "lastname, email, label) VALUES('%s','%s','%s'," . |
323 | "'%s','%s','%s')", |
324 | $this->table, $this->owner, |
325 | $this->dbh->quoteString($userdata['nickname']), |
62f7daa5 |
326 | $this->dbh->quoteString($userdata['firstname']), |
9b9474d6 |
327 | $this->dbh->quoteString($userdata['lastname']), |
62f7daa5 |
328 | $this->dbh->quoteString($userdata['email']), |
9b9474d6 |
329 | $this->dbh->quoteString($userdata['label']) ); |
330 | |
331 | /* Do the insert */ |
7902aca2 |
332 | $r = $this->dbh->simpleQuery($query); |
9b9474d6 |
333 | if ($r == DB_OK) { |
334 | return true; |
335 | } |
7902aca2 |
336 | |
9b9474d6 |
337 | /* Fail */ |
701c9c6b |
338 | return $this->set_error(sprintf(_("Database error: %s"), |
7902aca2 |
339 | DB::errorMessage($r))); |
9b9474d6 |
340 | } |
7902aca2 |
341 | |
0541c5ae |
342 | /** |
343 | * Delete address |
344 | * @param string $alias alias that has to be deleted |
345 | * @return bool |
346 | */ |
9b9474d6 |
347 | function remove($alias) { |
348 | if (!$this->writeable) { |
701c9c6b |
349 | return $this->set_error(_("Addressbook is read-only")); |
9b9474d6 |
350 | } |
7902aca2 |
351 | |
9b9474d6 |
352 | if (!$this->open()) { |
7902aca2 |
353 | return false; |
9b9474d6 |
354 | } |
62f7daa5 |
355 | |
9b9474d6 |
356 | /* Create query */ |
357 | $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (", |
358 | $this->table, $this->owner); |
7902aca2 |
359 | |
9b9474d6 |
360 | $sepstr = ''; |
361 | while (list($undef, $nickname) = each($alias)) { |
16a973d7 |
362 | $query .= sprintf("%s nickname='%s' ", $sepstr, |
7902aca2 |
363 | $this->dbh->quoteString($nickname)); |
91be2362 |
364 | $sepstr = 'OR'; |
9b9474d6 |
365 | } |
366 | $query .= ')'; |
7902aca2 |
367 | |
9b9474d6 |
368 | /* Delete entry */ |
369 | $r = $this->dbh->simpleQuery($query); |
370 | if ($r == DB_OK) { |
371 | return true; |
372 | } |
7902aca2 |
373 | |
9b9474d6 |
374 | /* Fail */ |
375 | return $this->set_error(sprintf(_("Database error: %s"), |
7902aca2 |
376 | DB::errorMessage($r))); |
9b9474d6 |
377 | } |
7902aca2 |
378 | |
0541c5ae |
379 | /** |
380 | * Modify address |
381 | * @param string $alias modified alias |
382 | * @param array $userdata new data |
383 | * @return bool |
384 | */ |
9b9474d6 |
385 | function modify($alias, $userdata) { |
386 | if (!$this->writeable) { |
701c9c6b |
387 | return $this->set_error(_("Addressbook is read-only")); |
9b9474d6 |
388 | } |
7902aca2 |
389 | |
9b9474d6 |
390 | if (!$this->open()) { |
7902aca2 |
391 | return false; |
9b9474d6 |
392 | } |
62f7daa5 |
393 | |
9b9474d6 |
394 | /* See if user exist */ |
395 | $ret = $this->lookup($alias); |
396 | if (empty($ret)) { |
701c9c6b |
397 | return $this->set_error(sprintf(_("User '%s' does not exist"), |
7902aca2 |
398 | $alias)); |
9b9474d6 |
399 | } |
400 | |
401 | /* Create query */ |
402 | $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ". |
403 | "lastname='%s', email='%s', label='%s' ". |
404 | "WHERE owner='%s' AND nickname='%s'", |
62f7daa5 |
405 | $this->table, |
9b9474d6 |
406 | $this->dbh->quoteString($userdata['nickname']), |
62f7daa5 |
407 | $this->dbh->quoteString($userdata['firstname']), |
9b9474d6 |
408 | $this->dbh->quoteString($userdata['lastname']), |
62f7daa5 |
409 | $this->dbh->quoteString($userdata['email']), |
9b9474d6 |
410 | $this->dbh->quoteString($userdata['label']), |
411 | $this->owner, |
412 | $this->dbh->quoteString($alias) ); |
413 | |
414 | /* Do the insert */ |
415 | $r = $this->dbh->simpleQuery($query); |
416 | if ($r == DB_OK) { |
417 | return true; |
418 | } |
7902aca2 |
419 | |
9b9474d6 |
420 | /* Fail */ |
421 | return $this->set_error(sprintf(_("Database error: %s"), |
422 | DB::errorMessage($r))); |
423 | } |
424 | } /* End of class abook_database */ |
7902aca2 |
425 | |
c9fcea56 |
426 | // vim: et ts=4 |
62f7daa5 |
427 | ?> |