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