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
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));
104 /* Load configured LDAP servers (if PHP has LDAP support) */
105 if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) {
107 while (list($undef,$param) = each($ldap_server)) {
108 if (is_array($param)) {
109 $r = $abook->add_backend('ldap_server', $param);
110 if (!$r && $showerr) {
111 printf( ' ' . _("Error initializing LDAP server %s:") .
112 "<BR>\n", $param['host']);
113 echo ' ' . $abook->error
;
120 /* Return the initialized object */
126 * Had to move this function outside of the Addressbook Class
127 * PHP 4.0.4 Seemed to be having problems with inline functions.
128 * Note: this can return now since we don't support 4.0.4 anymore.
130 function addressbook_cmp($a,$b) {
132 if($a['backend'] > $b['backend']) {
134 } else if($a['backend'] < $b['backend']) {
138 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
144 * This is the main address book class that connect all the
145 * backends and provide services to the functions above.
146 * @package squirrelmail
151 var $backends = array();
152 var $numbackends = 0;
154 var $localbackend = 0;
155 var $localbackendname = '';
157 // Constructor function.
158 function AddressBook() {
159 $localbackendname = _("Personal address book");
163 * Return an array of backends of a given type,
164 * or all backends if no type is specified.
166 function get_backend_list($type = '') {
168 for ($i = 1 ; $i <= $this->numbackends
; $i++
) {
169 if (empty($type) ||
$type == $this->backends
[$i]->btype
) {
170 $ret[] = &$this->backends
[$i];
178 ========================== Public ========================
180 Add a new backend. $backend is the name of a backend
181 (without the abook_ prefix), and $param is an optional
182 mixed variable that is passed to the backend constructor.
183 See each of the backend classes for valid parameters.
185 function add_backend($backend, $param = '') {
186 $backend_name = 'abook_' . $backend;
187 eval('$newback = new ' . $backend_name . '($param);');
188 if(!empty($newback->error
)) {
189 $this->error
= $newback->error
;
193 $this->numbackends++
;
195 $newback->bnum
= $this->numbackends
;
196 $this->backends
[$this->numbackends
] = $newback;
198 /* Store ID of first local backend added */
199 if ($this->localbackend
== 0 && $newback->btype
== 'local') {
200 $this->localbackend
= $this->numbackends
;
201 $this->localbackendname
= $newback->sname
;
204 return $this->numbackends
;
209 * This function takes a $row array as returned by the addressbook
210 * search and returns an e-mail address with the full name or
211 * nickname optionally prepended.
214 function full_address($row) {
215 global $addrsrch_fullname, $data_dir, $username;
217 if (($prefix = getPref($data_dir, $username, 'addrsrch_fullname') or
218 isset($addrsrch_fullname) and $prefix = $addrsrch_fullname)
219 and $prefix !== 'noprefix') {
220 $name = ($prefix === 'nickname') ?
$row['nickname']
222 return $name . ' <' . trim($row['email']) . '>';
224 return trim($row['email']);
229 Return a list of addresses matching expression in
230 all backends of a given type.
232 function search($expression, $bnum = -1) {
236 /* Search all backends */
238 $sel = $this->get_backend_list('');
240 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
241 $backend = &$sel[$i];
242 $backend->error
= '';
243 $res = $backend->search($expression);
244 if (is_array($res)) {
245 $ret = array_merge($ret, $res);
247 $this->error
.= "<br>\n" . $backend->error
;
252 /* Only fail if all backends failed */
253 if( $failed >= sizeof( $sel ) ) {
259 /* Search only one backend */
261 $ret = $this->backends
[$bnum]->search($expression);
262 if (!is_array($ret)) {
263 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
272 /* Return a sorted search */
273 function s_search($expression, $bnum = -1) {
275 $ret = $this->search($expression, $bnum);
276 if ( is_array( $ret ) ) {
277 usort($ret, 'addressbook_cmp');
284 * Lookup an address by alias. Only possible in
287 function lookup($alias, $bnum = -1) {
292 $res = $this->backends
[$bnum]->lookup($alias);
293 if (is_array($res)) {
296 $this->error
= $backend->error
;
301 $sel = $this->get_backend_list('local');
302 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
303 $backend = &$sel[$i];
304 $backend->error
= '';
305 $res = $backend->lookup($alias);
306 if (is_array($res)) {
310 $this->error
= $backend->error
;
319 /* Return all addresses */
320 function list_addr($bnum = -1) {
324 $sel = $this->get_backend_list('local');
326 $sel = array(0 => &$this->backends
[$bnum]);
329 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
330 $backend = &$sel[$i];
331 $backend->error
= '';
332 $res = $backend->list_addr();
333 if (is_array($res)) {
334 $ret = array_merge($ret, $res);
336 $this->error
= $backend->error
;
345 * Create a new address from $userdata, in backend $bnum.
346 * Return the backend number that the/ address was added
347 * to, or false if it failed.
349 function add($userdata, $bnum) {
352 if (!is_array($userdata)) {
353 $this->error
= _("Invalid input data");
356 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
357 $this->error
= _("Name is missing");
360 if (empty($userdata['email'])) {
361 $this->error
= _("E-mail address is missing");
364 if (empty($userdata['nickname'])) {
365 $userdata['nickname'] = $userdata['email'];
368 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
369 $this->error
= _("Nickname contains illegal characters");
373 /* Check that specified backend accept new entries */
374 if (!$this->backends
[$bnum]->writeable
) {
375 $this->error
= _("Addressbook is read-only");
379 /* Add address to backend */
380 $res = $this->backends
[$bnum]->add($userdata);
384 $this->error
= $this->backends
[$bnum]->error
;
388 return false; // Not reached
393 * Remove the user identified by $alias from backend $bnum
394 * If $alias is an array, all users in the array are removed.
396 function remove($alias, $bnum) {
403 /* Convert string to single element array */
404 if (!is_array($alias)) {
405 $alias = array(0 => $alias);
408 /* Check that specified backend is writeable */
409 if (!$this->backends
[$bnum]->writeable
) {
410 $this->error
= _("Addressbook is read-only");
414 /* Remove user from backend */
415 $res = $this->backends
[$bnum]->remove($alias);
419 $this->error
= $this->backends
[$bnum]->error
;
423 return FALSE; /* Not reached */
424 } /* end of remove() */
428 * Remove the user identified by $alias from backend $bnum
429 * If $alias is an array, all users in the array are removed.
431 function modify($alias, $userdata, $bnum) {
434 if (empty($alias) ||
!is_string($alias)) {
439 if(!is_array($userdata)) {
440 $this->error
= _("Invalid input data");
443 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
444 $this->error
= _("Name is missing");
447 if (empty($userdata['email'])) {
448 $this->error
= _("E-mail address is missing");
452 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
453 $this->error
= _("Nickname contains illegal characters");
457 if (empty($userdata['nickname'])) {
458 $userdata['nickname'] = $userdata['email'];
461 /* Check that specified backend is writeable */
462 if (!$this->backends
[$bnum]->writeable
) {
463 $this->error
= _("Addressbook is read-only");;
467 /* Modify user in backend */
468 $res = $this->backends
[$bnum]->modify($alias, $userdata);
472 $this->error
= $this->backends
[$bnum]->error
;
476 return FALSE; /* Not reached */
477 } /* end of modify() */
480 } /* End of class Addressbook */
483 * Generic backend that all other backends extend
484 * @package squirrelmail
486 class addressbook_backend
{
488 /* Variables that all backends must provide. */
489 var $btype = 'dummy';
490 var $bname = 'dummy';
491 var $sname = 'Dummy backend';
494 * Variables common for all backends, but that
495 * should not be changed by the backends.
499 var $writeable = false;
501 function set_error($string) {
502 $this->error
= '[' . $this->sname
. '] ' . $string;
507 /* ========================== Public ======================== */
509 function search($expression) {
510 $this->set_error('search not implemented');
514 function lookup($alias) {
515 $this->set_error('lookup not implemented');
519 function list_addr() {
520 $this->set_error('list_addr not implemented');
524 function add($userdata) {
525 $this->set_error('add not implemented');
529 function remove($alias) {
530 $this->set_error('delete not implemented');
534 function modify($alias, $newuserdata) {
535 $this->set_error('modify not implemented');
541 /* Sort array by the key "name" */
542 function alistcmp($a,$b) {
543 if ($a['backend'] > $b['backend']) {
546 if ($a['backend'] < $b['backend']) {
550 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
555 PHP 5 requires that the class be made first, which seems rather
556 logical, and should have been the way it was generated the first time.
559 require_once(SM_PATH
. 'functions/abook_local_file.php');
560 require_once(SM_PATH
. 'functions/abook_ldap_server.php');
562 /* Use this if you wanna have a global address book */
563 if (isset($address_book_global_filename)) {
564 include_once(SM_PATH
. 'functions/abook_global_file.php');
567 /* Only load database backend if database is configured */
568 if((isset($addrbook_dsn) && !empty($addrbook_dsn)) ||
569 (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) ) {
570 include_once(SM_PATH
. 'functions/abook_database.php');