Allow easy editing of address book DSN
[squirrelmail.git] / functions / addressbook.php
1 <?php
2
3 /**
4 * addressbook.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 * Functions and classes for the addressbook system.
10 *
11 * $Id$
12 */
13
14 /*
15 This is the path to the global site-wide addressbook.
16 It looks and feels just like a user's .abook file
17 If this is in the data directory, use "$data_dir/global.abook"
18 If not, specify the path as though it was accessed from the
19 src/ directory ("../global.abook" -> in main directory)
20
21 If you don't want a global site-wide addressbook, comment these
22 two lines out. (They are disabled by default.)
23
24 The global addressbook is unmodifiable by anyone. You must actually
25 use a shell script or whatnot to modify the contents.
26
27 global $data_dir;
28 $address_book_global_filename = "$data_dir/global.abook";
29
30 Include backends here.
31 */
32
33 require_once('../functions/abook_local_file.php');
34 require_once('../functions/abook_ldap_server.php');
35
36 global $addrbook_dsn;
37
38 /* Use this if you wanna have a global address book */
39 if (isset($address_book_global_filename)) {
40 include_once('../functions/abook_global_file.php');
41 }
42
43 /* Only load database backend if database is configured */
44 if(isset($addrbook_dsn) && !empty($addrbook_dsn)) {
45 include_once('../functions/abook_database.php');
46 }
47
48 /*
49 Create and initialize an addressbook object.
50 Returns the created object
51 */
52 function addressbook_init($showerr = true, $onlylocal = false) {
53 global $data_dir, $username, $ldap_server, $address_book_global_filename;
54 global $addrbook_dsn;
55
56 /* Create a new addressbook object */
57 $abook = new AddressBook;
58
59 /*
60 Always add a local backend. We use *either* file-based *or* a
61 database addressbook. If $addrbook_dsn is set, the database
62 backend is used. If not, addressbooks are stores in files.
63 */
64 if (isset($addrbook_dsn) && !empty($addrbook_dsn)) {
65 /* Database */
66 $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
67 'owner' => $username,
68 'table' => 'address'));
69 if (!$r && $showerr) {
70 echo _("Error initializing addressbook database.");
71 exit;
72 }
73 } else {
74 /* File */
75 $filename = getHashedFile($username, $data_dir, "$username.abook");
76 $r = $abook->add_backend('local_file', Array('filename' => $filename,
77 'create' => true));
78 if(!$r && $showerr) {
79 printf( _("Error opening file %s"), $filename );
80 exit;
81 }
82
83 }
84
85 /* This would be for the global addressbook */
86 if (isset($address_book_global_filename)) {
87 $r = $abook->add_backend('global_file');
88 if (!$r && $showerr) {
89 echo _("Error initializing global addressbook.");
90 exit;
91 }
92 }
93
94 if ($onlylocal) {
95 return $abook;
96 }
97
98 /* Load configured LDAP servers (if PHP has LDAP support) */
99 if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) {
100 reset($ldap_server);
101 while (list($undef,$param) = each($ldap_server)) {
102 if (is_array($param)) {
103 $r = $abook->add_backend('ldap_server', $param);
104 if (!$r && $showerr) {
105 printf( '&nbsp;' . _("Error initializing LDAP server %s:") .
106 "<BR>\n", $param['host']);
107 echo '&nbsp;' . $abook->error;
108 exit;
109 }
110 }
111 }
112 }
113
114 /* Return the initialized object */
115 return $abook;
116 }
117
118
119 /*
120 * Had to move this function outside of the Addressbook Class
121 * PHP 4.0.4 Seemed to be having problems with inline functions.
122 */
123 function addressbook_cmp($a,$b) {
124
125 if($a['backend'] > $b['backend']) {
126 return 1;
127 } else if($a['backend'] < $b['backend']) {
128 return -1;
129 }
130
131 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
132
133 }
134
135
136 /*
137 * This is the main address book class that connect all the
138 * backends and provide services to the functions above.
139 *
140 */
141
142 class AddressBook {
143
144 var $backends = array();
145 var $numbackends = 0;
146 var $error = '';
147 var $localbackend = 0;
148 var $localbackendname = '';
149
150 // Constructor function.
151 function AddressBook() {
152 $localbackendname = _("Personal address book");
153 }
154
155 /*
156 * Return an array of backends of a given type,
157 * or all backends if no type is specified.
158 */
159 function get_backend_list($type = '') {
160 $ret = array();
161 for ($i = 1 ; $i <= $this->numbackends ; $i++) {
162 if (empty($type) || $type == $this->backends[$i]->btype) {
163 $ret[] = &$this->backends[$i];
164 }
165 }
166 return $ret;
167 }
168
169
170 /*
171 ========================== Public ========================
172
173 Add a new backend. $backend is the name of a backend
174 (without the abook_ prefix), and $param is an optional
175 mixed variable that is passed to the backend constructor.
176 See each of the backend classes for valid parameters.
177 */
178 function add_backend($backend, $param = '') {
179 $backend_name = 'abook_' . $backend;
180 eval('$newback = new ' . $backend_name . '($param);');
181 if(!empty($newback->error)) {
182 $this->error = $newback->error;
183 return false;
184 }
185
186 $this->numbackends++;
187
188 $newback->bnum = $this->numbackends;
189 $this->backends[$this->numbackends] = $newback;
190
191 /* Store ID of first local backend added */
192 if ($this->localbackend == 0 && $newback->btype == 'local') {
193 $this->localbackend = $this->numbackends;
194 $this->localbackendname = $newback->sname;
195 }
196
197 return $this->numbackends;
198 }
199
200
201 /*
202 Return a list of addresses matching expression in
203 all backends of a given type.
204 */
205 function search($expression, $bnum = -1) {
206 $ret = array();
207 $this->error = '';
208
209 /* Search all backends */
210 if ($bnum == -1) {
211 $sel = $this->get_backend_list('');
212 $failed = 0;
213 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
214 $backend = &$sel[$i];
215 $backend->error = '';
216 $res = $backend->search($expression);
217 if (is_array($res)) {
218 $ret = array_merge($ret, $res);
219 } else {
220 $this->error .= "<br>\n" . $backend->error;
221 $failed++;
222 }
223 }
224
225 /* Only fail if all backends failed */
226 if( $failed >= sizeof( $sel ) ) {
227 $ret = FALSE;
228 }
229
230 } else {
231
232 /* Search only one backend */
233
234 $ret = $this->backends[$bnum]->search($expression);
235 if (!is_array($ret)) {
236 $this->error .= "<br>\n" . $this->backends[$bnum]->error;
237 $ret = FALSE;
238 }
239 }
240
241 return( $ret );
242 }
243
244
245 /* Return a sorted search */
246 function s_search($expression, $bnum = -1) {
247
248 $ret = $this->search($expression, $bnum);
249 if ( is_array( $ret ) ) {
250 usort($ret, 'addressbook_cmp');
251 }
252 return $ret;
253 }
254
255
256 /*
257 * Lookup an address by alias. Only possible in
258 * local backends.
259 */
260 function lookup($alias, $bnum = -1) {
261
262 $ret = array();
263
264 if ($bnum > -1) {
265 $res = $this->backends[$bnum]->lookup($alias);
266 if (is_array($res)) {
267 return $res;
268 } else {
269 $this->error = $backend->error;
270 return false;
271 }
272 }
273
274 $sel = $this->get_backend_list('local');
275 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
276 $backend = &$sel[$i];
277 $backend->error = '';
278 $res = $backend->lookup($alias);
279 if (is_array($res)) {
280 if(!empty($res))
281 return $res;
282 } else {
283 $this->error = $backend->error;
284 return false;
285 }
286 }
287
288 return $ret;
289 }
290
291
292 /* Return all addresses */
293 function list_addr($bnum = -1) {
294 $ret = array();
295
296 if ($bnum == -1) {
297 $sel = $this->get_backend_list('local');
298 } else {
299 $sel = array(0 => &$this->backends[$bnum]);
300 }
301
302 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
303 $backend = &$sel[$i];
304 $backend->error = '';
305 $res = $backend->list_addr();
306 if (is_array($res)) {
307 $ret = array_merge($ret, $res);
308 } else {
309 $this->error = $backend->error;
310 return false;
311 }
312 }
313
314 return $ret;
315 }
316
317 /*
318 * Create a new address from $userdata, in backend $bnum.
319 * Return the backend number that the/ address was added
320 * to, or false if it failed.
321 */
322 function add($userdata, $bnum) {
323
324 /* Validate data */
325 if (!is_array($userdata)) {
326 $this->error = _("Invalid input data");
327 return false;
328 }
329 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
330 $this->error = _("Name is missing");
331 return false;
332 }
333 if (empty($userdata['email'])) {
334 $this->error = _("E-mail address is missing");
335 return false;
336 }
337 if (empty($userdata['nickname'])) {
338 $userdata['nickname'] = $userdata['email'];
339 }
340
341 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
342 $this->error = _("Nickname contains illegal characters");
343 return false;
344 }
345
346 /* Check that specified backend accept new entries */
347 if (!$this->backends[$bnum]->writeable) {
348 $this->error = _("Addressbook is read-only");
349 return false;
350 }
351
352 /* Add address to backend */
353 $res = $this->backends[$bnum]->add($userdata);
354 if ($res) {
355 return $bnum;
356 } else {
357 $this->error = $this->backends[$bnum]->error;
358 return false;
359 }
360
361 return false; // Not reached
362 } /* end of add() */
363
364
365 /*
366 * Remove the user identified by $alias from backend $bnum
367 * If $alias is an array, all users in the array are removed.
368 */
369 function remove($alias, $bnum) {
370
371 /* Check input */
372 if (empty($alias)) {
373 return true;
374 }
375
376 /* Convert string to single element array */
377 if (!is_array($alias)) {
378 $alias = array(0 => $alias);
379 }
380
381 /* Check that specified backend is writable */
382 if (!$this->backends[$bnum]->writeable) {
383 $this->error = _("Addressbook is read-only");
384 return false;
385 }
386
387 /* Remove user from backend */
388 $res = $this->backends[$bnum]->remove($alias);
389 if ($res) {
390 return $bnum;
391 } else {
392 $this->error = $this->backends[$bnum]->error;
393 return false;
394 }
395
396 return FALSE; /* Not reached */
397 } /* end of remove() */
398
399
400 /*
401 * Remove the user identified by $alias from backend $bnum
402 * If $alias is an array, all users in the array are removed.
403 */
404 function modify($alias, $userdata, $bnum) {
405
406 /* Check input */
407 if (empty($alias) || !is_string($alias)) {
408 return true;
409 }
410
411 /* Validate data */
412 if(!is_array($userdata)) {
413 $this->error = _("Invalid input data");
414 return false;
415 }
416 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
417 $this->error = _("Name is missing");
418 return false;
419 }
420 if (empty($userdata['email'])) {
421 $this->error = _("E-mail address is missing");
422 return false;
423 }
424
425 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
426 $this->error = _("Nickname contains illegal characters");
427 return false;
428 }
429
430 if (empty($userdata['nickname'])) {
431 $userdata['nickname'] = $userdata['email'];
432 }
433
434 /* Check that specified backend is writable */
435 if (!$this->backends[$bnum]->writeable) {
436 $this->error = _("Addressbook is read-only");;
437 return false;
438 }
439
440 /* Modify user in backend */
441 $res = $this->backends[$bnum]->modify($alias, $userdata);
442 if ($res) {
443 return $bnum;
444 } else {
445 $this->error = $this->backends[$bnum]->error;
446 return false;
447 }
448
449 return FALSE; /* Not reached */
450 } /* end of modify() */
451
452
453 } /* End of class Addressbook */
454
455 /*
456 * Generic backend that all other backends extend
457 */
458 class addressbook_backend {
459
460 /* Variables that all backends must provide. */
461 var $btype = 'dummy';
462 var $bname = 'dummy';
463 var $sname = 'Dummy backend';
464
465 /*
466 * Variables common for all backends, but that
467 * should not be changed by the backends.
468 */
469 var $bnum = -1;
470 var $error = '';
471 var $writeable = false;
472
473 function set_error($string) {
474 $this->error = '[' . $this->sname . '] ' . $string;
475 return false;
476 }
477
478
479 /* ========================== Public ======================== */
480
481 function search($expression) {
482 $this->set_error('search not implemented');
483 return false;
484 }
485
486 function lookup($alias) {
487 $this->set_error('lookup not implemented');
488 return false;
489 }
490
491 function list_addr() {
492 $this->set_error('list_addr not implemented');
493 return false;
494 }
495
496 function add($userdata) {
497 $this->set_error('add not implemented');
498 return false;
499 }
500
501 function remove($alias) {
502 $this->set_error('delete not implemented');
503 return false;
504 }
505
506 function modify($alias, $newuserdata) {
507 $this->set_error('modify not implemented');
508 return false;
509 }
510
511 }
512
513 /* Sort array by the key "name" */
514 function alistcmp($a,$b) {
515 if ($a['backend'] > $b['backend']) {
516 return 1;
517 } else {
518 if ($a['backend'] < $b['backend']) {
519 return -1;
520 }
521 }
522 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
523 }
524
525 ?>