6 * Copyright (c) 1999-2001 The Squirrelmail Development 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.
28 $address_book_global_filename = "$data_dir/global.abook";
30 Include backends here.
33 require_once('../functions/abook_local_file.php');
34 require_once('../functions/abook_ldap_server.php');
38 /* Use this if you wanna have a global address book */
39 if (isset($address_book_global_filename)) {
40 include_once('../functions/abook_global_file.php');
43 /* Only load database backend if database is configured */
44 if(isset($addrbook_dsn)) {
45 include_once('../functions/abook_database.php');
49 Create and initialize an addressbook object.
50 Returns the created object
52 function addressbook_init($showerr = true, $onlylocal = false) {
53 global $data_dir, $username, $ldap_server, $address_book_global_filename;
56 /* Create a new addressbook object */
57 $abook = new AddressBook
;
60 Always add a local backend. We use *either* file-based *or* a
61 database addressbook. If $addrbook_dsn is set, the database
62 backend is used. If not, addressbooks are stores in files.
64 if (isset($addrbook_dsn) && !empty($addrbook_dsn)) {
66 $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
68 'table' => 'address'));
69 if (!$r && $showerr) {
70 echo _("Error initializing addressbook database.");
75 $filename = getHashedFile($username, $data_dir, "$username.abook");
76 $r = $abook->add_backend('local_file', Array('filename' => $filename,
79 printf( _("Error opening file %s"), $filename );
85 /* This would be for the global addressbook */
86 if (isset($address_book_global_filename)) {
87 $r = $abook->add_backend('global_file');
88 if (!$r && $showerr) {
89 echo _("Error initializing global addressbook.");
98 /* Load configured LDAP servers (if PHP has LDAP support) */
99 if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) {
101 while (list($undef,$param) = each($ldap_server)) {
102 if (is_array($param)) {
103 $r = $abook->add_backend('ldap_server', $param);
104 if (!$r && $showerr) {
105 printf( ' ' . _("Error initializing LDAP server %s:") .
106 "<BR>\n", $param['host']);
107 echo ' ' . $abook->error
;
114 /* Return the initialized object */
120 * Had to move this function outside of the Addressbook Class
121 * PHP 4.0.4 Seemed to be having problems with inline functions.
123 function addressbook_cmp($a,$b) {
125 if($a['backend'] > $b['backend']) {
127 } else if($a['backend'] < $b['backend']) {
131 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
137 * This is the main address book class that connect all the
138 * backends and provide services to the functions above.
144 var $backends = array();
145 var $numbackends = 0;
147 var $localbackend = 0;
148 var $localbackendname = '';
150 // Constructor function.
151 function AddressBook() {
152 $localbackendname = _("Personal address book");
156 * Return an array of backends of a given type,
157 * or all backends if no type is specified.
159 function get_backend_list($type = '') {
161 for ($i = 1 ; $i <= $this->numbackends
; $i++
) {
162 if (empty($type) ||
$type == $this->backends
[$i]->btype
) {
163 $ret[] = &$this->backends
[$i];
171 ========================== Public ========================
173 Add a new backend. $backend is the name of a backend
174 (without the abook_ prefix), and $param is an optional
175 mixed variable that is passed to the backend constructor.
176 See each of the backend classes for valid parameters.
178 function add_backend($backend, $param = '') {
179 $backend_name = 'abook_' . $backend;
180 eval('$newback = new ' . $backend_name . '($param);');
181 if(!empty($newback->error
)) {
182 $this->error
= $newback->error
;
186 $this->numbackends++
;
188 $newback->bnum
= $this->numbackends
;
189 $this->backends
[$this->numbackends
] = $newback;
191 /* Store ID of first local backend added */
192 if ($this->localbackend
== 0 && $newback->btype
== 'local') {
193 $this->localbackend
= $this->numbackends
;
194 $this->localbackendname
= $newback->sname
;
197 return $this->numbackends
;
202 Return a list of addresses matching expression in
203 all backends of a given type.
205 function search($expression, $bnum = -1) {
209 /* Search all backends */
211 $sel = $this->get_backend_list('');
213 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
214 $backend = &$sel[$i];
215 $backend->error
= '';
216 $res = $backend->search($expression);
217 if (is_array($res)) {
218 $ret = array_merge($ret, $res);
220 $this->error
.= "<br>\n" . $backend->error
;
225 /* Only fail if all backends failed */
226 if( $failed >= sizeof( $sel ) ) {
232 /* Search only one backend */
234 $ret = $this->backends
[$bnum]->search($expression);
235 if (!is_array($ret)) {
236 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
245 /* Return a sorted search */
246 function s_search($expression, $bnum = -1) {
248 $ret = $this->search($expression, $bnum);
249 if ( is_array( $ret ) ) {
250 usort($ret, 'addressbook_cmp');
257 * Lookup an address by alias. Only possible in
260 function lookup($alias, $bnum = -1) {
265 $res = $this->backends
[$bnum]->lookup($alias);
266 if (is_array($res)) {
269 $this->error
= $backend->error
;
274 $sel = $this->get_backend_list('local');
275 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
276 $backend = &$sel[$i];
277 $backend->error
= '';
278 $res = $backend->lookup($alias);
279 if (is_array($res)) {
283 $this->error
= $backend->error
;
292 /* Return all addresses */
293 function list_addr($bnum = -1) {
297 $sel = $this->get_backend_list('local');
299 $sel = array(0 => &$this->backends
[$bnum]);
302 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
303 $backend = &$sel[$i];
304 $backend->error
= '';
305 $res = $backend->list_addr();
306 if (is_array($res)) {
307 $ret = array_merge($ret, $res);
309 $this->error
= $backend->error
;
319 * Create a new address from $userdata, in backend $bnum.
320 * Return the backend number that the/ address was added
321 * to, or false if it failed.
323 function add($userdata, $bnum) {
326 if (!is_array($userdata)) {
327 $this->error
= _("Invalid input data");
330 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
331 $this->error
= _("Name is missing");
334 if (empty($userdata['email'])) {
335 $this->error
= _("E-mail address is missing");
338 if (empty($userdata['nickname'])) {
339 $userdata['nickname'] = $userdata['email'];
342 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
343 $this->error
= _("Nickname contains illegal characters");
347 /* Check that specified backend accept new entries */
348 if (!$this->backends
[$bnum]->writeable
) {
349 $this->error
= _("Addressbook is read-only");
353 /* Add address to backend */
354 $res = $this->backends
[$bnum]->add($userdata);
358 $this->error
= $this->backends
[$bnum]->error
;
362 return false; // Not reached
367 * Remove the user identified by $alias from backend $bnum
368 * If $alias is an array, all users in the array are removed.
370 function remove($alias, $bnum) {
377 /* Convert string to single element array */
378 if (!is_array($alias)) {
379 $alias = array(0 => $alias);
382 /* Check that specified backend is writable */
383 if (!$this->backends
[$bnum]->writeable
) {
384 $this->error
= _("Addressbook is read-only");
388 /* Remove user from backend */
389 $res = $this->backends
[$bnum]->remove($alias);
393 $this->error
= $this->backends
[$bnum]->error
;
397 return FALSE; /* Not reached */
398 } /* end of remove() */
402 * Remove the user identified by $alias from backend $bnum
403 * If $alias is an array, all users in the array are removed.
405 function modify($alias, $userdata, $bnum) {
408 if (empty($alias) ||
!is_string($alias)) {
413 if(!is_array($userdata)) {
414 $this->error
= _("Invalid input data");
417 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
418 $this->error
= _("Name is missing");
421 if (empty($userdata['email'])) {
422 $this->error
= _("E-mail address is missing");
426 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
427 $this->error
= _("Nickname contains illegal characters");
431 if (empty($userdata['nickname'])) {
432 $userdata['nickname'] = $userdata['email'];
435 /* Check that specified backend is writable */
436 if (!$this->backends
[$bnum]->writeable
) {
437 $this->error
= _("Addressbook is read-only");;
441 /* Modify user in backend */
442 $res = $this->backends
[$bnum]->modify($alias, $userdata);
446 $this->error
= $this->backends
[$bnum]->error
;
450 return FALSE; /* Not reached */
451 } /* end of modify() */
454 } /* End of class Addressbook */
457 * Generic backend that all other backends extend
459 class addressbook_backend
{
461 /* Variables that all backends must provide. */
462 var $btype = 'dummy';
463 var $bname = 'dummy';
464 var $sname = 'Dummy backend';
467 * Variables common for all backends, but that
468 * should not be changed by the backends.
472 var $writeable = false;
474 function set_error($string) {
475 $this->error
= '[' . $this->sname
. '] ' . $string;
480 /* ========================== Public ======================== */
482 function search($expression) {
483 $this->set_error('search not implemented');
487 function lookup($alias) {
488 $this->set_error('lookup not implemented');
492 function list_addr() {
493 $this->set_error('list_addr not implemented');
497 function add($userdata) {
498 $this->set_error('add not implemented');
502 function remove($alias) {
503 $this->set_error('delete not implemented');
507 function modify($alias, $newuserdata) {
508 $this->set_error('modify not implemented');