agresive_decoding changed to aggressive_decoding.
[squirrelmail.git] / functions / addressbook.php
CommitLineData
5100704d 1<?php
62f7daa5 2
35586184 3/**
4 * addressbook.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
81fa4801 9 * Functions and classes for the addressbook system.
35586184 10 *
a9d318b0 11 * @version $Id$
d6c32258 12 * @package squirrelmail
a9d318b0 13 * @subpackage addressbook
35586184 14 */
15
d6c32258 16/**
81fa4801 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
91e51743 29 global $data_dir, $address_book_global_filename;
81fa4801 30 $address_book_global_filename = "$data_dir/global.abook";
31
81fa4801 32*/
33
30e9932c 34global $addrbook_dsn, $addrbook_global_dsn;
81fa4801 35
d6c32258 36/**
81fa4801 37 Create and initialize an addressbook object.
38 Returns the created object
39*/
40function addressbook_init($showerr = true, $onlylocal = false) {
41 global $data_dir, $username, $ldap_server, $address_book_global_filename;
42 global $addrbook_dsn, $addrbook_table;
30e9932c 43 global $addrbook_global_dsn, $addrbook_global_table, $addrbook_global_writeable, $addrbook_global_listing;
81fa4801 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
30e9932c 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
df788686 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];
62f7daa5 109
81fa4801 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:") .
6fd95361 122 "<br />\n", $param['host']);
81fa4801 123 echo '&nbsp;' . $abook->error;
124 exit;
125 }
126 }
127 }
128 }
4935919f 129
81fa4801 130 /* Return the initialized object */
131 return $abook;
4935919f 132}
133
134
81fa4801 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.
abd74f7d 138 * Note: this can return now since we don't support 4.0.4 anymore.
62f7daa5 139 */
81fa4801 140function addressbook_cmp($a,$b) {
4935919f 141
81fa4801 142 if($a['backend'] > $b['backend']) {
143 return 1;
144 } else if($a['backend'] < $b['backend']) {
145 return -1;
146 }
62f7daa5 147
81fa4801 148 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
4935919f 149
81fa4801 150}
4935919f 151
152
8f6f9ba5 153/**
81fa4801 154 * This is the main address book class that connect all the
155 * backends and provide services to the functions above.
8f6f9ba5 156 * @package squirrelmail
81fa4801 157 */
5100704d 158
81fa4801 159class AddressBook {
4935919f 160
81fa4801 161 var $backends = array();
162 var $numbackends = 0;
163 var $error = '';
164 var $localbackend = 0;
165 var $localbackendname = '';
62f7daa5 166
81fa4801 167 // Constructor function.
168 function AddressBook() {
169 $localbackendname = _("Personal address book");
170 }
4935919f 171
81fa4801 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 }
4935919f 182 }
81fa4801 183 return $ret;
184 }
4935919f 185
186
81fa4801 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;
62f7daa5 207
81fa4801 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 }
4935919f 216
4935919f 217
2e542990 218 /*
62f7daa5 219 * This function takes a $row array as returned by the addressbook
2e542990 220 * search and returns an e-mail address with the full name or
221 * nickname optionally prepended.
222 */
223
224 function full_address($row) {
1ba8cd6b 225 global $addrsrch_fullname, $data_dir, $username;
20ad4fdd 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']);
2e542990 230 return $name . ' <' . trim($row['email']) . '>';
231 } else {
232 return trim($row['email']);
233 }
234 }
235
81fa4801 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 {
6fd95361 255 $this->error .= "<br />\n" . $backend->error;
81fa4801 256 $failed++;
75e19c7f 257 }
258 }
4935919f 259
81fa4801 260 /* Only fail if all backends failed */
261 if( $failed >= sizeof( $sel ) ) {
262 $ret = FALSE;
4935919f 263 }
4935919f 264
81fa4801 265 } else {
4935919f 266
81fa4801 267 /* Search only one backend */
4935919f 268
81fa4801 269 $ret = $this->backends[$bnum]->search($expression);
270 if (!is_array($ret)) {
6fd95361 271 $this->error .= "<br />\n" . $this->backends[$bnum]->error;
81fa4801 272 $ret = FALSE;
273 }
274 }
275
276 return( $ret );
4935919f 277 }
278
279
81fa4801 280 /* Return a sorted search */
281 function s_search($expression, $bnum = -1) {
62f7daa5 282
81fa4801 283 $ret = $this->search($expression, $bnum);
284 if ( is_array( $ret ) ) {
285 usort($ret, 'addressbook_cmp');
62f7daa5 286 }
81fa4801 287 return $ret;
288 }
4935919f 289
290
81fa4801 291 /*
292 * Lookup an address by alias. Only possible in
293 * local backends.
294 */
295 function lookup($alias, $bnum = -1) {
62f7daa5 296
81fa4801 297 $ret = array();
62f7daa5 298
81fa4801 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 }
62f7daa5 308
81fa4801 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 }
62f7daa5 322
81fa4801 323 return $ret;
4935919f 324 }
325
4935919f 326
81fa4801 327 /* Return all addresses */
328 function list_addr($bnum = -1) {
329 $ret = array();
62f7daa5 330
81fa4801 331 if ($bnum == -1) {
332 $sel = $this->get_backend_list('local');
333 } else {
334 $sel = array(0 => &$this->backends[$bnum]);
335 }
62f7daa5 336
81fa4801 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 }
62f7daa5 348
81fa4801 349 return $ret;
350 }
4935919f 351
81fa4801 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) {
62f7daa5 358
81fa4801 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 }
62f7daa5 375
81fa4801 376 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
377 $this->error = _("Nickname contains illegal characters");
378 return false;
379 }
62f7daa5 380
81fa4801 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 }
62f7daa5 386
81fa4801 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 }
62f7daa5 395
81fa4801 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) {
62f7daa5 405
81fa4801 406 /* Check input */
407 if (empty($alias)) {
408 return true;
409 }
62f7daa5 410
81fa4801 411 /* Convert string to single element array */
412 if (!is_array($alias)) {
413 $alias = array(0 => $alias);
414 }
62f7daa5 415
416 /* Check that specified backend is writable */
81fa4801 417 if (!$this->backends[$bnum]->writeable) {
418 $this->error = _("Addressbook is read-only");
419 return false;
420 }
62f7daa5 421
81fa4801 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 }
62f7daa5 430
81fa4801 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) {
62f7daa5 440
81fa4801 441 /* Check input */
442 if (empty($alias) || !is_string($alias)) {
443 return true;
444 }
62f7daa5 445
81fa4801 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 }
62f7daa5 459
81fa4801 460 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
461 $this->error = _("Nickname contains illegal characters");
462 return false;
463 }
62f7daa5 464
81fa4801 465 if (empty($userdata['nickname'])) {
466 $userdata['nickname'] = $userdata['email'];
467 }
62f7daa5 468
469 /* Check that specified backend is writable */
81fa4801 470 if (!$this->backends[$bnum]->writeable) {
471 $this->error = _("Addressbook is read-only");;
472 return false;
473 }
62f7daa5 474
81fa4801 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 }
62f7daa5 483
81fa4801 484 return FALSE; /* Not reached */
485 } /* end of modify() */
62f7daa5 486
487
81fa4801 488} /* End of class Addressbook */
489
8f6f9ba5 490/**
81fa4801 491 * Generic backend that all other backends extend
8f6f9ba5 492 * @package squirrelmail
81fa4801 493 */
494class addressbook_backend {
495
496 /* Variables that all backends must provide. */
497 var $btype = 'dummy';
498 var $bname = 'dummy';
499 var $sname = 'Dummy backend';
62f7daa5 500
81fa4801 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;
62f7daa5 508
81fa4801 509 function set_error($string) {
510 $this->error = '[' . $this->sname . '] ' . $string;
511 return false;
512 }
62f7daa5 513
514
81fa4801 515 /* ========================== Public ======================== */
62f7daa5 516
81fa4801 517 function search($expression) {
518 $this->set_error('search not implemented');
519 return false;
520 }
62f7daa5 521
81fa4801 522 function lookup($alias) {
523 $this->set_error('lookup not implemented');
524 return false;
a10110a5 525 }
62f7daa5 526
81fa4801 527 function list_addr() {
528 $this->set_error('list_addr not implemented');
529 return false;
530 }
62f7daa5 531
81fa4801 532 function add($userdata) {
533 $this->set_error('add not implemented');
534 return false;
535 }
62f7daa5 536
81fa4801 537 function remove($alias) {
538 $this->set_error('delete not implemented');
539 return false;
540 }
62f7daa5 541
81fa4801 542 function modify($alias, $newuserdata) {
543 $this->set_error('modify not implemented');
544 return false;
545 }
546
547}
548
92948f86 549/**
550 * Sort array by the key "name"
551 */
81fa4801 552function alistcmp($a,$b) {
08e71631 553 $abook_sort_order=get_abook_sort();
554
555 switch ($abook_sort_order) {
556 case 0:
557 case 1:
558 $abook_sort='nickname';
559 break;
560 case 4:
561 case 5:
562 $abook_sort='email';
563 break;
564 case 6:
565 case 7:
566 $abook_sort='label';
567 break;
568 case 2:
569 case 3:
570 case 8:
571 default:
572 $abook_sort='name';
573 }
92948f86 574
81fa4801 575 if ($a['backend'] > $b['backend']) {
576 return 1;
577 } else {
578 if ($a['backend'] < $b['backend']) {
579 return -1;
580 }
581 }
08e71631 582
583 if( (($abook_sort_order+2) % 2) == 1) {
584 return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ? 1 : -1;
585 } else {
586 return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1;
587 }
81fa4801 588}
589
92948f86 590/**
08e71631 591 * Address book sorting options
92948f86 592 *
08e71631 593 * returns address book sorting order
594 * @return integer book sorting options order
92948f86 595 */
596function get_abook_sort() {
597 global $data_dir, $username;
598
599 /* get sorting order */
08e71631 600 if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET)) {
601 $abook_sort_order = (int) $temp;
602
603 if ($abook_sort_order < 0 or $abook_sort_order > 8)
604 $abook_sort_order=8;
605
606 setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order);
92948f86 607 } else {
08e71631 608 /* get previous sorting options. default to unsorted */
609 $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8);
92948f86 610 }
611
08e71631 612 return $abook_sort_order;
613}
614
615/**
616 * This function shows the address book sort button.
617 *
618 * @param integer $abook_sort_order current sort value
619 * @param string $alt_tag alt tag value (string visible to text only browsers)
620 * @param integer $Down sort value when list is sorted ascending
621 * @param integer $Up sort value when list is sorted descending
62f7daa5 622 * @return string html code with sorting images and urls
08e71631 623 */
624function show_abook_sort_button($abook_sort_order, $alt_tag, $Down, $Up ) {
625 global $form_url;
626
627 /* Figure out which image we want to use. */
628 if ($abook_sort_order != $Up && $abook_sort_order != $Down) {
629 $img = 'sort_none.png';
630 $which = $Up;
631 } elseif ($abook_sort_order == $Up) {
632 $img = 'up_pointer.png';
633 $which = $Down;
634 } else {
635 $img = 'down_pointer.png';
636 $which = 8;
637 }
92948f86 638
08e71631 639 /* Now that we have everything figured out, show the actual button. */
640 return ' <a href="' . $form_url .'?abook_sort_order=' . $which
641 . '"><img src="../images/' . $img
642 . '" border="0" width="12" height="10" alt="' . $alt_tag . '" title="'
796f91d9 643 . _("Click here to change the sorting of the address list") .'" /></a>';
92948f86 644}
0419106e 645
646/*
647 PHP 5 requires that the class be made first, which seems rather
648 logical, and should have been the way it was generated the first time.
649*/
650
651require_once(SM_PATH . 'functions/abook_local_file.php');
652require_once(SM_PATH . 'functions/abook_ldap_server.php');
653
654/* Use this if you wanna have a global address book */
655if (isset($address_book_global_filename)) {
656 include_once(SM_PATH . 'functions/abook_global_file.php');
657}
658
659/* Only load database backend if database is configured */
62f7daa5 660if((isset($addrbook_dsn) && !empty($addrbook_dsn)) ||
30e9932c 661 (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) ) {
0419106e 662 include_once(SM_PATH . 'functions/abook_database.php');
663}
664
df788686 665/*
666 * hook allows adding different address book classes.
667 * class must follow address book class coding standards.
668 *
669 * see addressbook_backend class and functions/abook_*.php files.
670 */
671do_hook('abook_add_class');
0419106e 672
796f91d9 673?>