6 * Copyright (c) 1999-2003 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
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";
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;
43 /* Create a new addressbook object */
44 $abook = new AddressBook
;
47 Always add a local backend. We use *either* file-based *or* a
48 database addressbook. If $addrbook_dsn is set, the database
49 backend is used. If not, addressbooks are stores in files.
51 if (isset($addrbook_dsn) && !empty($addrbook_dsn)) {
53 if (!isset($addrbook_table) ||
empty($addrbook_table)) {
54 $addrbook_table = 'address';
56 $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
58 'table' => $addrbook_table));
59 if (!$r && $showerr) {
60 echo _("Error initializing addressbook database.");
65 $filename = getHashedFile($username, $data_dir, "$username.abook");
66 $r = $abook->add_backend('local_file', Array('filename' => $filename,
69 printf( _("Error opening file %s"), $filename );
75 /* This would be for the global addressbook */
76 if (isset($address_book_global_filename)) {
77 $r = $abook->add_backend('global_file');
78 if (!$r && $showerr) {
79 echo _("Error initializing global addressbook.");
88 /* Load configured LDAP servers (if PHP has LDAP support) */
89 if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) {
91 while (list($undef,$param) = each($ldap_server)) {
92 if (is_array($param)) {
93 $r = $abook->add_backend('ldap_server', $param);
94 if (!$r && $showerr) {
95 printf( ' ' . _("Error initializing LDAP server %s:") .
96 "<BR>\n", $param['host']);
97 echo ' ' . $abook->error
;
104 /* Return the initialized object */
110 * Had to move this function outside of the Addressbook Class
111 * PHP 4.0.4 Seemed to be having problems with inline functions.
113 function addressbook_cmp($a,$b) {
115 if($a['backend'] > $b['backend']) {
117 } else if($a['backend'] < $b['backend']) {
121 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
127 * This is the main address book class that connect all the
128 * backends and provide services to the functions above.
129 * @package squirrelmail
134 var $backends = array();
135 var $numbackends = 0;
137 var $localbackend = 0;
138 var $localbackendname = '';
140 // Constructor function.
141 function AddressBook() {
142 $localbackendname = _("Personal address book");
146 * Return an array of backends of a given type,
147 * or all backends if no type is specified.
149 function get_backend_list($type = '') {
151 for ($i = 1 ; $i <= $this->numbackends
; $i++
) {
152 if (empty($type) ||
$type == $this->backends
[$i]->btype
) {
153 $ret[] = &$this->backends
[$i];
161 ========================== Public ========================
163 Add a new backend. $backend is the name of a backend
164 (without the abook_ prefix), and $param is an optional
165 mixed variable that is passed to the backend constructor.
166 See each of the backend classes for valid parameters.
168 function add_backend($backend, $param = '') {
169 $backend_name = 'abook_' . $backend;
170 eval('$newback = new ' . $backend_name . '($param);');
171 if(!empty($newback->error
)) {
172 $this->error
= $newback->error
;
176 $this->numbackends++
;
178 $newback->bnum
= $this->numbackends
;
179 $this->backends
[$this->numbackends
] = $newback;
181 /* Store ID of first local backend added */
182 if ($this->localbackend
== 0 && $newback->btype
== 'local') {
183 $this->localbackend
= $this->numbackends
;
184 $this->localbackendname
= $newback->sname
;
187 return $this->numbackends
;
192 * This function takes a $row array as returned by the addressbook
193 * search and returns an e-mail address with the full name or
194 * nickname optionally prepended.
197 function full_address($row) {
198 global $addrsrch_fullname, $data_dir, $username;
200 if (($prefix = getPref($data_dir, $username, 'addrsrch_fullname') or
201 isset($addrsrch_fullname) and $prefix = $addrsrch_fullname)
202 and $prefix !== 'noprefix') {
203 $name = ($prefix === 'nickname') ?
$row['nickname']
205 return $name . ' <' . trim($row['email']) . '>';
207 return trim($row['email']);
212 Return a list of addresses matching expression in
213 all backends of a given type.
215 function search($expression, $bnum = -1) {
219 /* Search all backends */
221 $sel = $this->get_backend_list('');
223 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
224 $backend = &$sel[$i];
225 $backend->error
= '';
226 $res = $backend->search($expression);
227 if (is_array($res)) {
228 $ret = array_merge($ret, $res);
230 $this->error
.= "<br>\n" . $backend->error
;
235 /* Only fail if all backends failed */
236 if( $failed >= sizeof( $sel ) ) {
242 /* Search only one backend */
244 $ret = $this->backends
[$bnum]->search($expression);
245 if (!is_array($ret)) {
246 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
255 /* Return a sorted search */
256 function s_search($expression, $bnum = -1) {
258 $ret = $this->search($expression, $bnum);
259 if ( is_array( $ret ) ) {
260 usort($ret, 'addressbook_cmp');
267 * Lookup an address by alias. Only possible in
270 function lookup($alias, $bnum = -1) {
275 $res = $this->backends
[$bnum]->lookup($alias);
276 if (is_array($res)) {
279 $this->error
= $backend->error
;
284 $sel = $this->get_backend_list('local');
285 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
286 $backend = &$sel[$i];
287 $backend->error
= '';
288 $res = $backend->lookup($alias);
289 if (is_array($res)) {
293 $this->error
= $backend->error
;
302 /* Return all addresses */
303 function list_addr($bnum = -1) {
307 $sel = $this->get_backend_list('local');
309 $sel = array(0 => &$this->backends
[$bnum]);
312 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
313 $backend = &$sel[$i];
314 $backend->error
= '';
315 $res = $backend->list_addr();
316 if (is_array($res)) {
317 $ret = array_merge($ret, $res);
319 $this->error
= $backend->error
;
328 * Create a new address from $userdata, in backend $bnum.
329 * Return the backend number that the/ address was added
330 * to, or false if it failed.
332 function add($userdata, $bnum) {
335 if (!is_array($userdata)) {
336 $this->error
= _("Invalid input data");
339 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
340 $this->error
= _("Name is missing");
343 if (empty($userdata['email'])) {
344 $this->error
= _("E-mail address is missing");
347 if (empty($userdata['nickname'])) {
348 $userdata['nickname'] = $userdata['email'];
351 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
352 $this->error
= _("Nickname contains illegal characters");
356 /* Check that specified backend accept new entries */
357 if (!$this->backends
[$bnum]->writeable
) {
358 $this->error
= _("Addressbook is read-only");
362 /* Add address to backend */
363 $res = $this->backends
[$bnum]->add($userdata);
367 $this->error
= $this->backends
[$bnum]->error
;
371 return false; // Not reached
376 * Remove the user identified by $alias from backend $bnum
377 * If $alias is an array, all users in the array are removed.
379 function remove($alias, $bnum) {
386 /* Convert string to single element array */
387 if (!is_array($alias)) {
388 $alias = array(0 => $alias);
391 /* Check that specified backend is writable */
392 if (!$this->backends
[$bnum]->writeable
) {
393 $this->error
= _("Addressbook is read-only");
397 /* Remove user from backend */
398 $res = $this->backends
[$bnum]->remove($alias);
402 $this->error
= $this->backends
[$bnum]->error
;
406 return FALSE; /* Not reached */
407 } /* end of remove() */
411 * Remove the user identified by $alias from backend $bnum
412 * If $alias is an array, all users in the array are removed.
414 function modify($alias, $userdata, $bnum) {
417 if (empty($alias) ||
!is_string($alias)) {
422 if(!is_array($userdata)) {
423 $this->error
= _("Invalid input data");
426 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
427 $this->error
= _("Name is missing");
430 if (empty($userdata['email'])) {
431 $this->error
= _("E-mail address is missing");
435 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
436 $this->error
= _("Nickname contains illegal characters");
440 if (empty($userdata['nickname'])) {
441 $userdata['nickname'] = $userdata['email'];
444 /* Check that specified backend is writable */
445 if (!$this->backends
[$bnum]->writeable
) {
446 $this->error
= _("Addressbook is read-only");;
450 /* Modify user in backend */
451 $res = $this->backends
[$bnum]->modify($alias, $userdata);
455 $this->error
= $this->backends
[$bnum]->error
;
459 return FALSE; /* Not reached */
460 } /* end of modify() */
463 } /* End of class Addressbook */
466 * Generic backend that all other backends extend
467 * @package squirrelmail
469 class addressbook_backend
{
471 /* Variables that all backends must provide. */
472 var $btype = 'dummy';
473 var $bname = 'dummy';
474 var $sname = 'Dummy backend';
477 * Variables common for all backends, but that
478 * should not be changed by the backends.
482 var $writeable = false;
484 function set_error($string) {
485 $this->error
= '[' . $this->sname
. '] ' . $string;
490 /* ========================== Public ======================== */
492 function search($expression) {
493 $this->set_error('search not implemented');
497 function lookup($alias) {
498 $this->set_error('lookup not implemented');
502 function list_addr() {
503 $this->set_error('list_addr not implemented');
507 function add($userdata) {
508 $this->set_error('add not implemented');
512 function remove($alias) {
513 $this->set_error('delete not implemented');
517 function modify($alias, $newuserdata) {
518 $this->set_error('modify not implemented');
524 /* Sort array by the key "name" */
525 function alistcmp($a,$b) {
526 if ($a['backend'] > $b['backend']) {
529 if ($a['backend'] < $b['backend']) {
533 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
538 PHP 5 requires that the class be made first, which seems rather
539 logical, and should have been the way it was generated the first time.
542 require_once(SM_PATH
. 'functions/abook_local_file.php');
543 require_once(SM_PATH
. 'functions/abook_ldap_server.php');
545 /* Use this if you wanna have a global address book */
546 if (isset($address_book_global_filename)) {
547 include_once(SM_PATH
. 'functions/abook_global_file.php');
550 /* Only load database backend if database is configured */
551 if(isset($addrbook_dsn) && !empty($addrbook_dsn)) {
552 include_once(SM_PATH
. 'functions/abook_database.php');