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.
15 This is the path to the global site-wide addressbook.
16 It looks and feels just like a user's .abook file
17 If this is in the data directory, use "$data_dir/global.abook"
18 If not, specify the path as though it was accessed from the
19 src/ directory ("../global.abook" -> in main directory)
21 If you don't want a global site-wide addressbook, comment these
22 two lines out. (They are disabled by default.)
24 The global addressbook is unmodifiable by anyone. You must actually
25 use a shell script or whatnot to modify the contents.
27 global $data_dir, $address_book_global_filename;
28 $address_book_global_filename = "$data_dir/global.abook";
35 Create and initialize an addressbook object.
36 Returns the created object
38 function addressbook_init($showerr = true, $onlylocal = false) {
39 global $data_dir, $username, $ldap_server, $address_book_global_filename;
40 global $addrbook_dsn, $addrbook_table;
42 /* Create a new addressbook object */
43 $abook = new AddressBook
;
46 Always add a local backend. We use *either* file-based *or* a
47 database addressbook. If $addrbook_dsn is set, the database
48 backend is used. If not, addressbooks are stores in files.
50 if (isset($addrbook_dsn) && !empty($addrbook_dsn)) {
52 if (!isset($addrbook_table) ||
empty($addrbook_table)) {
53 $addrbook_table = 'address';
55 $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
57 'table' => $addrbook_table));
58 if (!$r && $showerr) {
59 echo _("Error initializing addressbook database.");
64 $filename = getHashedFile($username, $data_dir, "$username.abook");
65 $r = $abook->add_backend('local_file', Array('filename' => $filename,
68 printf( _("Error opening file %s"), $filename );
74 /* This would be for the global addressbook */
75 if (isset($address_book_global_filename)) {
76 $r = $abook->add_backend('global_file');
77 if (!$r && $showerr) {
78 echo _("Error initializing global addressbook.");
87 /* Load configured LDAP servers (if PHP has LDAP support) */
88 if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) {
90 while (list($undef,$param) = each($ldap_server)) {
91 if (is_array($param)) {
92 $r = $abook->add_backend('ldap_server', $param);
93 if (!$r && $showerr) {
94 printf( ' ' . _("Error initializing LDAP server %s:") .
95 "<BR>\n", $param['host']);
96 echo ' ' . $abook->error
;
103 /* Return the initialized object */
109 * Had to move this function outside of the Addressbook Class
110 * PHP 4.0.4 Seemed to be having problems with inline functions.
112 function addressbook_cmp($a,$b) {
114 if($a['backend'] > $b['backend']) {
116 } else if($a['backend'] < $b['backend']) {
120 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
126 * This is the main address book class that connect all the
127 * backends and provide services to the functions above.
133 var $backends = array();
134 var $numbackends = 0;
136 var $localbackend = 0;
137 var $localbackendname = '';
139 // Constructor function.
140 function AddressBook() {
141 $localbackendname = _("Personal address book");
145 * Return an array of backends of a given type,
146 * or all backends if no type is specified.
148 function get_backend_list($type = '') {
150 for ($i = 1 ; $i <= $this->numbackends
; $i++
) {
151 if (empty($type) ||
$type == $this->backends
[$i]->btype
) {
152 $ret[] = &$this->backends
[$i];
160 ========================== Public ========================
162 Add a new backend. $backend is the name of a backend
163 (without the abook_ prefix), and $param is an optional
164 mixed variable that is passed to the backend constructor.
165 See each of the backend classes for valid parameters.
167 function add_backend($backend, $param = '') {
168 $backend_name = 'abook_' . $backend;
169 eval('$newback = new ' . $backend_name . '($param);');
170 if(!empty($newback->error
)) {
171 $this->error
= $newback->error
;
175 $this->numbackends++
;
177 $newback->bnum
= $this->numbackends
;
178 $this->backends
[$this->numbackends
] = $newback;
180 /* Store ID of first local backend added */
181 if ($this->localbackend
== 0 && $newback->btype
== 'local') {
182 $this->localbackend
= $this->numbackends
;
183 $this->localbackendname
= $newback->sname
;
186 return $this->numbackends
;
191 * This function takes a $row array as returned by the addressbook
192 * search and returns an e-mail address with the full name or
193 * nickname optionally prepended.
196 function full_address($row) {
197 global $addrsrch_fullname, $data_dir, $username;
199 if (($prefix = getPref($data_dir, $username, 'addrsrch_fullname') or
200 isset($addrsrch_fullname) and $prefix = $addrsrch_fullname)
201 and $prefix !== 'noprefix') {
202 $name = ($prefix === 'nickname') ?
$row['nickname']
204 return $name . ' <' . trim($row['email']) . '>';
206 return trim($row['email']);
211 Return a list of addresses matching expression in
212 all backends of a given type.
214 function search($expression, $bnum = -1) {
218 /* Search all backends */
220 $sel = $this->get_backend_list('');
222 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
223 $backend = &$sel[$i];
224 $backend->error
= '';
225 $res = $backend->search($expression);
226 if (is_array($res)) {
227 $ret = array_merge($ret, $res);
229 $this->error
.= "<br>\n" . $backend->error
;
234 /* Only fail if all backends failed */
235 if( $failed >= sizeof( $sel ) ) {
241 /* Search only one backend */
243 $ret = $this->backends
[$bnum]->search($expression);
244 if (!is_array($ret)) {
245 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
254 /* Return a sorted search */
255 function s_search($expression, $bnum = -1) {
257 $ret = $this->search($expression, $bnum);
258 if ( is_array( $ret ) ) {
259 usort($ret, 'addressbook_cmp');
266 * Lookup an address by alias. Only possible in
269 function lookup($alias, $bnum = -1) {
274 $res = $this->backends
[$bnum]->lookup($alias);
275 if (is_array($res)) {
278 $this->error
= $backend->error
;
283 $sel = $this->get_backend_list('local');
284 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
285 $backend = &$sel[$i];
286 $backend->error
= '';
287 $res = $backend->lookup($alias);
288 if (is_array($res)) {
292 $this->error
= $backend->error
;
301 /* Return all addresses */
302 function list_addr($bnum = -1) {
306 $sel = $this->get_backend_list('local');
308 $sel = array(0 => &$this->backends
[$bnum]);
311 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
312 $backend = &$sel[$i];
313 $backend->error
= '';
314 $res = $backend->list_addr();
315 if (is_array($res)) {
316 $ret = array_merge($ret, $res);
318 $this->error
= $backend->error
;
327 * Create a new address from $userdata, in backend $bnum.
328 * Return the backend number that the/ address was added
329 * to, or false if it failed.
331 function add($userdata, $bnum) {
334 if (!is_array($userdata)) {
335 $this->error
= _("Invalid input data");
338 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
339 $this->error
= _("Name is missing");
342 if (empty($userdata['email'])) {
343 $this->error
= _("E-mail address is missing");
346 if (empty($userdata['nickname'])) {
347 $userdata['nickname'] = $userdata['email'];
350 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
351 $this->error
= _("Nickname contains illegal characters");
355 /* Check that specified backend accept new entries */
356 if (!$this->backends
[$bnum]->writeable
) {
357 $this->error
= _("Addressbook is read-only");
361 /* Add address to backend */
362 $res = $this->backends
[$bnum]->add($userdata);
366 $this->error
= $this->backends
[$bnum]->error
;
370 return false; // Not reached
375 * Remove the user identified by $alias from backend $bnum
376 * If $alias is an array, all users in the array are removed.
378 function remove($alias, $bnum) {
385 /* Convert string to single element array */
386 if (!is_array($alias)) {
387 $alias = array(0 => $alias);
390 /* Check that specified backend is writable */
391 if (!$this->backends
[$bnum]->writeable
) {
392 $this->error
= _("Addressbook is read-only");
396 /* Remove user from backend */
397 $res = $this->backends
[$bnum]->remove($alias);
401 $this->error
= $this->backends
[$bnum]->error
;
405 return FALSE; /* Not reached */
406 } /* end of remove() */
410 * Remove the user identified by $alias from backend $bnum
411 * If $alias is an array, all users in the array are removed.
413 function modify($alias, $userdata, $bnum) {
416 if (empty($alias) ||
!is_string($alias)) {
421 if(!is_array($userdata)) {
422 $this->error
= _("Invalid input data");
425 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
426 $this->error
= _("Name is missing");
429 if (empty($userdata['email'])) {
430 $this->error
= _("E-mail address is missing");
434 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
435 $this->error
= _("Nickname contains illegal characters");
439 if (empty($userdata['nickname'])) {
440 $userdata['nickname'] = $userdata['email'];
443 /* Check that specified backend is writable */
444 if (!$this->backends
[$bnum]->writeable
) {
445 $this->error
= _("Addressbook is read-only");;
449 /* Modify user in backend */
450 $res = $this->backends
[$bnum]->modify($alias, $userdata);
454 $this->error
= $this->backends
[$bnum]->error
;
458 return FALSE; /* Not reached */
459 } /* end of modify() */
462 } /* End of class Addressbook */
465 * Generic backend that all other backends extend
467 class addressbook_backend
{
469 /* Variables that all backends must provide. */
470 var $btype = 'dummy';
471 var $bname = 'dummy';
472 var $sname = 'Dummy backend';
475 * Variables common for all backends, but that
476 * should not be changed by the backends.
480 var $writeable = false;
482 function set_error($string) {
483 $this->error
= '[' . $this->sname
. '] ' . $string;
488 /* ========================== Public ======================== */
490 function search($expression) {
491 $this->set_error('search not implemented');
495 function lookup($alias) {
496 $this->set_error('lookup not implemented');
500 function list_addr() {
501 $this->set_error('list_addr not implemented');
505 function add($userdata) {
506 $this->set_error('add not implemented');
510 function remove($alias) {
511 $this->set_error('delete not implemented');
515 function modify($alias, $newuserdata) {
516 $this->set_error('modify not implemented');
522 /* Sort array by the key "name" */
523 function alistcmp($a,$b) {
524 if ($a['backend'] > $b['backend']) {
527 if ($a['backend'] < $b['backend']) {
531 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
536 PHP 5 requires that the class be made first, which seems rather
537 logical, and should have been the way it was generated the first time.
540 require_once(SM_PATH
. 'functions/abook_local_file.php');
541 require_once(SM_PATH
. 'functions/abook_ldap_server.php');
543 /* Use this if you wanna have a global address book */
544 if (isset($address_book_global_filename)) {
545 include_once(SM_PATH
. 'functions/abook_global_file.php');
548 /* Only load database backend if database is configured */
549 if(isset($addrbook_dsn) && !empty($addrbook_dsn)) {
550 include_once(SM_PATH
. 'functions/abook_database.php');