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