XHTML fixes
[squirrelmail.git] / functions / addressbook.php
1 <?php
2 /**
3 * addressbook.php
4 *
5 * Copyright (c) 1999-2004 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * Functions and classes for the addressbook system.
9 *
10 * @version $Id$
11 * @package squirrelmail
12 * @subpackage addressbook
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 /**
549 * Sort array by the key "name"
550 */
551 function alistcmp($a,$b) {
552 $abook_sort_order=get_abook_sort();
553
554 switch ($abook_sort_order) {
555 case 0:
556 case 1:
557 $abook_sort='nickname';
558 break;
559 case 4:
560 case 5:
561 $abook_sort='email';
562 break;
563 case 6:
564 case 7:
565 $abook_sort='label';
566 break;
567 case 2:
568 case 3:
569 case 8:
570 default:
571 $abook_sort='name';
572 }
573
574 if ($a['backend'] > $b['backend']) {
575 return 1;
576 } else {
577 if ($a['backend'] < $b['backend']) {
578 return -1;
579 }
580 }
581
582 if( (($abook_sort_order+2) % 2) == 1) {
583 return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ? 1 : -1;
584 } else {
585 return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1;
586 }
587 }
588
589 /**
590 * Address book sorting options
591 *
592 * returns address book sorting order
593 * @return integer book sorting options order
594 */
595 function get_abook_sort() {
596 global $data_dir, $username;
597
598 /* get sorting order */
599 if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET)) {
600 $abook_sort_order = (int) $temp;
601
602 if ($abook_sort_order < 0 or $abook_sort_order > 8)
603 $abook_sort_order=8;
604
605 setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order);
606 } else {
607 /* get previous sorting options. default to unsorted */
608 $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8);
609 }
610
611 return $abook_sort_order;
612 }
613
614 /**
615 * This function shows the address book sort button.
616 *
617 * @param integer $abook_sort_order current sort value
618 * @param string $alt_tag alt tag value (string visible to text only browsers)
619 * @param integer $Down sort value when list is sorted ascending
620 * @param integer $Up sort value when list is sorted descending
621 * @return string html code with sorting images and urls
622 */
623 function show_abook_sort_button($abook_sort_order, $alt_tag, $Down, $Up ) {
624 global $form_url;
625
626 /* Figure out which image we want to use. */
627 if ($abook_sort_order != $Up && $abook_sort_order != $Down) {
628 $img = 'sort_none.png';
629 $which = $Up;
630 } elseif ($abook_sort_order == $Up) {
631 $img = 'up_pointer.png';
632 $which = $Down;
633 } else {
634 $img = 'down_pointer.png';
635 $which = 8;
636 }
637
638 /* Now that we have everything figured out, show the actual button. */
639 return ' <a href="' . $form_url .'?abook_sort_order=' . $which
640 . '"><img src="../images/' . $img
641 . '" border="0" width="12" height="10" alt="' . $alt_tag . '" title="'
642 . _("Click here to change the sorting of the address list") .'" /></a>';
643 }
644
645 /*
646 PHP 5 requires that the class be made first, which seems rather
647 logical, and should have been the way it was generated the first time.
648 */
649
650 require_once(SM_PATH . 'functions/abook_local_file.php');
651 require_once(SM_PATH . 'functions/abook_ldap_server.php');
652
653 /* Use this if you wanna have a global address book */
654 if (isset($address_book_global_filename)) {
655 include_once(SM_PATH . 'functions/abook_global_file.php');
656 }
657
658 /* Only load database backend if database is configured */
659 if((isset($addrbook_dsn) && !empty($addrbook_dsn)) ||
660 (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) ) {
661 include_once(SM_PATH . 'functions/abook_database.php');
662 }
663
664 /*
665 * hook allows adding different address book classes.
666 * class must follow address book class coding standards.
667 *
668 * see addressbook_backend class and functions/abook_*.php files.
669 */
670 do_hook('abook_add_class');
671
672 ?>