5 * Copyright (c) 1999-2004 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * Functions and classes for the addressbook system.
11 * @package squirrelmail
12 * @subpackage addressbook
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)
22 If you don't want a global site-wide addressbook, comment these
23 two lines out. (They are disabled by default.)
25 The global addressbook is unmodifiable by anyone. You must actually
26 use a shell script or whatnot to modify the contents.
28 global $data_dir, $address_book_global_filename;
29 $address_book_global_filename = "$data_dir/global.abook";
33 global $addrbook_dsn, $addrbook_global_dsn;
36 Create and initialize an addressbook object.
37 Returns the created object
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;
44 /* Create a new addressbook object */
45 $abook = new AddressBook
;
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.
52 if (isset($addrbook_dsn) && !empty($addrbook_dsn)) {
54 if (!isset($addrbook_table) ||
empty($addrbook_table)) {
55 $addrbook_table = 'address';
57 $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
59 'table' => $addrbook_table));
60 if (!$r && $showerr) {
61 echo _("Error initializing addressbook database.");
66 $filename = getHashedFile($username, $data_dir, "$username.abook");
67 $r = $abook->add_backend('local_file', Array('filename' => $filename,
70 printf( _("Error opening file %s"), $filename );
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.");
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';
91 $r = $abook->add_backend('database',
92 Array('dsn' => $addrbook_global_dsn,
94 'name' => _("Global address book"),
95 'writeable' => $addrbook_global_writeable,
96 'listing' => $addrbook_global_listing,
97 'table' => $addrbook_global_table));
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.
105 $hookReturn = do_hook('abook_init', $abook, $r);
106 $abook = $hookReturn[1];
113 /* Load configured LDAP servers (if PHP has LDAP support) */
114 if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) {
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( ' ' . _("Error initializing LDAP server %s:") .
121 "<BR>\n", $param['host']);
122 echo ' ' . $abook->error
;
129 /* Return the initialized object */
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.
139 function addressbook_cmp($a,$b) {
141 if($a['backend'] > $b['backend']) {
143 } else if($a['backend'] < $b['backend']) {
147 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
153 * This is the main address book class that connect all the
154 * backends and provide services to the functions above.
155 * @package squirrelmail
160 var $backends = array();
161 var $numbackends = 0;
163 var $localbackend = 0;
164 var $localbackendname = '';
166 // Constructor function.
167 function AddressBook() {
168 $localbackendname = _("Personal address book");
172 * Return an array of backends of a given type,
173 * or all backends if no type is specified.
175 function get_backend_list($type = '') {
177 for ($i = 1 ; $i <= $this->numbackends
; $i++
) {
178 if (empty($type) ||
$type == $this->backends
[$i]->btype
) {
179 $ret[] = &$this->backends
[$i];
187 ========================== Public ========================
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.
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
;
202 $this->numbackends++
;
204 $newback->bnum
= $this->numbackends
;
205 $this->backends
[$this->numbackends
] = $newback;
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
;
213 return $this->numbackends
;
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.
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']) . '>';
231 return trim($row['email']);
236 Return a list of addresses matching expression in
237 all backends of a given type.
239 function search($expression, $bnum = -1) {
243 /* Search all backends */
245 $sel = $this->get_backend_list('');
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);
254 $this->error
.= "<br>\n" . $backend->error
;
259 /* Only fail if all backends failed */
260 if( $failed >= sizeof( $sel ) ) {
266 /* Search only one backend */
268 $ret = $this->backends
[$bnum]->search($expression);
269 if (!is_array($ret)) {
270 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
279 /* Return a sorted search */
280 function s_search($expression, $bnum = -1) {
282 $ret = $this->search($expression, $bnum);
283 if ( is_array( $ret ) ) {
284 usort($ret, 'addressbook_cmp');
291 * Lookup an address by alias. Only possible in
294 function lookup($alias, $bnum = -1) {
299 $res = $this->backends
[$bnum]->lookup($alias);
300 if (is_array($res)) {
303 $this->error
= $backend->error
;
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)) {
317 $this->error
= $backend->error
;
326 /* Return all addresses */
327 function list_addr($bnum = -1) {
331 $sel = $this->get_backend_list('local');
333 $sel = array(0 => &$this->backends
[$bnum]);
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);
343 $this->error
= $backend->error
;
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.
356 function add($userdata, $bnum) {
359 if (!is_array($userdata)) {
360 $this->error
= _("Invalid input data");
363 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
364 $this->error
= _("Name is missing");
367 if (empty($userdata['email'])) {
368 $this->error
= _("E-mail address is missing");
371 if (empty($userdata['nickname'])) {
372 $userdata['nickname'] = $userdata['email'];
375 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
376 $this->error
= _("Nickname contains illegal characters");
380 /* Check that specified backend accept new entries */
381 if (!$this->backends
[$bnum]->writeable
) {
382 $this->error
= _("Addressbook is read-only");
386 /* Add address to backend */
387 $res = $this->backends
[$bnum]->add($userdata);
391 $this->error
= $this->backends
[$bnum]->error
;
395 return false; // Not reached
400 * Remove the user identified by $alias from backend $bnum
401 * If $alias is an array, all users in the array are removed.
403 function remove($alias, $bnum) {
410 /* Convert string to single element array */
411 if (!is_array($alias)) {
412 $alias = array(0 => $alias);
415 /* Check that specified backend is writeable */
416 if (!$this->backends
[$bnum]->writeable
) {
417 $this->error
= _("Addressbook is read-only");
421 /* Remove user from backend */
422 $res = $this->backends
[$bnum]->remove($alias);
426 $this->error
= $this->backends
[$bnum]->error
;
430 return FALSE; /* Not reached */
431 } /* end of remove() */
435 * Remove the user identified by $alias from backend $bnum
436 * If $alias is an array, all users in the array are removed.
438 function modify($alias, $userdata, $bnum) {
441 if (empty($alias) ||
!is_string($alias)) {
446 if(!is_array($userdata)) {
447 $this->error
= _("Invalid input data");
450 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
451 $this->error
= _("Name is missing");
454 if (empty($userdata['email'])) {
455 $this->error
= _("E-mail address is missing");
459 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
460 $this->error
= _("Nickname contains illegal characters");
464 if (empty($userdata['nickname'])) {
465 $userdata['nickname'] = $userdata['email'];
468 /* Check that specified backend is writeable */
469 if (!$this->backends
[$bnum]->writeable
) {
470 $this->error
= _("Addressbook is read-only");;
474 /* Modify user in backend */
475 $res = $this->backends
[$bnum]->modify($alias, $userdata);
479 $this->error
= $this->backends
[$bnum]->error
;
483 return FALSE; /* Not reached */
484 } /* end of modify() */
487 } /* End of class Addressbook */
490 * Generic backend that all other backends extend
491 * @package squirrelmail
493 class addressbook_backend
{
495 /* Variables that all backends must provide. */
496 var $btype = 'dummy';
497 var $bname = 'dummy';
498 var $sname = 'Dummy backend';
501 * Variables common for all backends, but that
502 * should not be changed by the backends.
506 var $writeable = false;
508 function set_error($string) {
509 $this->error
= '[' . $this->sname
. '] ' . $string;
514 /* ========================== Public ======================== */
516 function search($expression) {
517 $this->set_error('search not implemented');
521 function lookup($alias) {
522 $this->set_error('lookup not implemented');
526 function list_addr() {
527 $this->set_error('list_addr not implemented');
531 function add($userdata) {
532 $this->set_error('add not implemented');
536 function remove($alias) {
537 $this->set_error('delete not implemented');
541 function modify($alias, $newuserdata) {
542 $this->set_error('modify not implemented');
549 * Sort array by the key "name"
551 function alistcmp($a,$b) {
552 $abook_sort_order=get_abook_sort();
554 switch ($abook_sort_order) {
557 $abook_sort='nickname';
574 if ($a['backend'] > $b['backend']) {
577 if ($a['backend'] < $b['backend']) {
582 if( (($abook_sort_order+
2) %
2) == 1) {
583 return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ?
1 : -1;
585 return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ?
1 : -1;
590 * Address book sorting options
592 * returns address book sorting order
593 * @return integer book sorting options order
595 function get_abook_sort() {
596 global $data_dir, $username;
598 /* get sorting order */
599 if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET
)) {
600 $abook_sort_order = (int) $temp;
602 if ($abook_sort_order < 0 or $abook_sort_order > 8)
605 setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order);
607 /* get previous sorting options. default to unsorted */
608 $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8);
611 return $abook_sort_order;
615 * This function shows the address book sort button.
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
623 function show_abook_sort_button($abook_sort_order, $alt_tag, $Down, $Up ) {
626 /* Figure out which image we want to use. */
627 if ($abook_sort_order != $Up && $abook_sort_order != $Down) {
628 $img = 'sort_none.png';
630 } elseif ($abook_sort_order == $Up) {
631 $img = 'up_pointer.png';
634 $img = 'down_pointer.png';
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>';
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.
650 require_once(SM_PATH
. 'functions/abook_local_file.php');
651 require_once(SM_PATH
. 'functions/abook_ldap_server.php');
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');
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');
665 * hook allows adding different address book classes.
666 * class must follow address book class coding standards.
668 * see addressbook_backend class and functions/abook_*.php files.
670 do_hook('abook_add_class');