6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Functions and classes for the addressbook system.
12 * @package squirrelmail
13 * @subpackage addressbook
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)
23 If you don't want a global site-wide addressbook, comment these
24 two lines out. (They are disabled by default.)
26 The global addressbook is unmodifiable by anyone. You must actually
27 use a shell script or whatnot to modify the contents.
29 global $data_dir, $address_book_global_filename;
30 $address_book_global_filename = "$data_dir/global.abook";
34 global $addrbook_dsn, $addrbook_global_dsn;
37 Create and initialize an addressbook object.
38 Returns the created object
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;
45 /* Create a new addressbook object */
46 $abook = new AddressBook
;
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.
53 if (isset($addrbook_dsn) && !empty($addrbook_dsn)) {
55 if (!isset($addrbook_table) ||
empty($addrbook_table)) {
56 $addrbook_table = 'address';
58 $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
60 'table' => $addrbook_table));
61 if (!$r && $showerr) {
62 echo _("Error initializing addressbook database.");
67 $filename = getHashedFile($username, $data_dir, "$username.abook");
68 $r = $abook->add_backend('local_file', Array('filename' => $filename,
71 printf( _("Error opening file %s"), $filename );
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.");
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';
92 $r = $abook->add_backend('database',
93 Array('dsn' => $addrbook_global_dsn,
95 'name' => _("Global address book"),
96 'writeable' => $addrbook_global_writeable,
97 'listing' => $addrbook_global_listing,
98 'table' => $addrbook_global_table));
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.
106 $hookReturn = do_hook('abook_init', $abook, $r);
107 $abook = $hookReturn[1];
114 /* Load configured LDAP servers (if PHP has LDAP support) */
115 if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) {
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( ' ' . _("Error initializing LDAP server %s:") .
122 "<BR>\n", $param['host']);
123 echo ' ' . $abook->error
;
130 /* Return the initialized object */
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.
140 function addressbook_cmp($a,$b) {
142 if($a['backend'] > $b['backend']) {
144 } else if($a['backend'] < $b['backend']) {
148 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
154 * This is the main address book class that connect all the
155 * backends and provide services to the functions above.
156 * @package squirrelmail
161 var $backends = array();
162 var $numbackends = 0;
164 var $localbackend = 0;
165 var $localbackendname = '';
167 // Constructor function.
168 function AddressBook() {
169 $localbackendname = _("Personal address book");
173 * Return an array of backends of a given type,
174 * or all backends if no type is specified.
176 function get_backend_list($type = '') {
178 for ($i = 1 ; $i <= $this->numbackends
; $i++
) {
179 if (empty($type) ||
$type == $this->backends
[$i]->btype
) {
180 $ret[] = &$this->backends
[$i];
188 ========================== Public ========================
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.
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
;
203 $this->numbackends++
;
205 $newback->bnum
= $this->numbackends
;
206 $this->backends
[$this->numbackends
] = $newback;
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
;
214 return $this->numbackends
;
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.
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']) . '>';
232 return trim($row['email']);
237 Return a list of addresses matching expression in
238 all backends of a given type.
240 function search($expression, $bnum = -1) {
244 /* Search all backends */
246 $sel = $this->get_backend_list('');
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);
255 $this->error
.= "<br>\n" . $backend->error
;
260 /* Only fail if all backends failed */
261 if( $failed >= sizeof( $sel ) ) {
267 /* Search only one backend */
269 $ret = $this->backends
[$bnum]->search($expression);
270 if (!is_array($ret)) {
271 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
280 /* Return a sorted search */
281 function s_search($expression, $bnum = -1) {
283 $ret = $this->search($expression, $bnum);
284 if ( is_array( $ret ) ) {
285 usort($ret, 'addressbook_cmp');
292 * Lookup an address by alias. Only possible in
295 function lookup($alias, $bnum = -1) {
300 $res = $this->backends
[$bnum]->lookup($alias);
301 if (is_array($res)) {
304 $this->error
= $backend->error
;
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)) {
318 $this->error
= $backend->error
;
327 /* Return all addresses */
328 function list_addr($bnum = -1) {
332 $sel = $this->get_backend_list('local');
334 $sel = array(0 => &$this->backends
[$bnum]);
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);
344 $this->error
= $backend->error
;
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.
357 function add($userdata, $bnum) {
360 if (!is_array($userdata)) {
361 $this->error
= _("Invalid input data");
364 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
365 $this->error
= _("Name is missing");
368 if (empty($userdata['email'])) {
369 $this->error
= _("E-mail address is missing");
372 if (empty($userdata['nickname'])) {
373 $userdata['nickname'] = $userdata['email'];
376 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
377 $this->error
= _("Nickname contains illegal characters");
381 /* Check that specified backend accept new entries */
382 if (!$this->backends
[$bnum]->writeable
) {
383 $this->error
= _("Addressbook is read-only");
387 /* Add address to backend */
388 $res = $this->backends
[$bnum]->add($userdata);
392 $this->error
= $this->backends
[$bnum]->error
;
396 return false; // Not reached
401 * Remove the user identified by $alias from backend $bnum
402 * If $alias is an array, all users in the array are removed.
404 function remove($alias, $bnum) {
411 /* Convert string to single element array */
412 if (!is_array($alias)) {
413 $alias = array(0 => $alias);
416 /* Check that specified backend is writeable */
417 if (!$this->backends
[$bnum]->writeable
) {
418 $this->error
= _("Addressbook is read-only");
422 /* Remove user from backend */
423 $res = $this->backends
[$bnum]->remove($alias);
427 $this->error
= $this->backends
[$bnum]->error
;
431 return FALSE; /* Not reached */
432 } /* end of remove() */
436 * Remove the user identified by $alias from backend $bnum
437 * If $alias is an array, all users in the array are removed.
439 function modify($alias, $userdata, $bnum) {
442 if (empty($alias) ||
!is_string($alias)) {
447 if(!is_array($userdata)) {
448 $this->error
= _("Invalid input data");
451 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
452 $this->error
= _("Name is missing");
455 if (empty($userdata['email'])) {
456 $this->error
= _("E-mail address is missing");
460 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
461 $this->error
= _("Nickname contains illegal characters");
465 if (empty($userdata['nickname'])) {
466 $userdata['nickname'] = $userdata['email'];
469 /* Check that specified backend is writeable */
470 if (!$this->backends
[$bnum]->writeable
) {
471 $this->error
= _("Addressbook is read-only");;
475 /* Modify user in backend */
476 $res = $this->backends
[$bnum]->modify($alias, $userdata);
480 $this->error
= $this->backends
[$bnum]->error
;
484 return FALSE; /* Not reached */
485 } /* end of modify() */
488 } /* End of class Addressbook */
491 * Generic backend that all other backends extend
492 * @package squirrelmail
494 class addressbook_backend
{
496 /* Variables that all backends must provide. */
497 var $btype = 'dummy';
498 var $bname = 'dummy';
499 var $sname = 'Dummy backend';
502 * Variables common for all backends, but that
503 * should not be changed by the backends.
507 var $writeable = false;
509 function set_error($string) {
510 $this->error
= '[' . $this->sname
. '] ' . $string;
515 /* ========================== Public ======================== */
517 function search($expression) {
518 $this->set_error('search not implemented');
522 function lookup($alias) {
523 $this->set_error('lookup not implemented');
527 function list_addr() {
528 $this->set_error('list_addr not implemented');
532 function add($userdata) {
533 $this->set_error('add not implemented');
537 function remove($alias) {
538 $this->set_error('delete not implemented');
542 function modify($alias, $newuserdata) {
543 $this->set_error('modify not implemented');
549 /* Sort array by the key "name" */
550 function alistcmp($a,$b) {
551 if ($a['backend'] > $b['backend']) {
554 if ($a['backend'] < $b['backend']) {
558 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
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.
567 require_once(SM_PATH
. 'functions/abook_local_file.php');
568 require_once(SM_PATH
. 'functions/abook_ldap_server.php');
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');
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');
582 * hook allows adding different address book classes.
583 * class must follow address book class coding standards.
585 * see addressbook_backend class and functions/abook_*.php files.
587 do_hook('abook_add_class');