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.
129 function addressbook_cmp($a,$b) {
131 if($a['backend'] > $b['backend']) {
133 } else if($a['backend'] < $b['backend']) {
137 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
143 * This is the main address book class that connect all the
144 * backends and provide services to the functions above.
145 * @package squirrelmail
150 var $backends = array();
151 var $numbackends = 0;
153 var $localbackend = 0;
154 var $localbackendname = '';
156 // Constructor function.
157 function AddressBook() {
158 $localbackendname = _("Personal address book");
162 * Return an array of backends of a given type,
163 * or all backends if no type is specified.
165 function get_backend_list($type = '') {
167 for ($i = 1 ; $i <= $this->numbackends
; $i++
) {
168 if (empty($type) ||
$type == $this->backends
[$i]->btype
) {
169 $ret[] = &$this->backends
[$i];
177 ========================== Public ========================
179 Add a new backend. $backend is the name of a backend
180 (without the abook_ prefix), and $param is an optional
181 mixed variable that is passed to the backend constructor.
182 See each of the backend classes for valid parameters.
184 function add_backend($backend, $param = '') {
185 $backend_name = 'abook_' . $backend;
186 eval('$newback = new ' . $backend_name . '($param);');
187 if(!empty($newback->error
)) {
188 $this->error
= $newback->error
;
192 $this->numbackends++
;
194 $newback->bnum
= $this->numbackends
;
195 $this->backends
[$this->numbackends
] = $newback;
197 /* Store ID of first local backend added */
198 if ($this->localbackend
== 0 && $newback->btype
== 'local') {
199 $this->localbackend
= $this->numbackends
;
200 $this->localbackendname
= $newback->sname
;
203 return $this->numbackends
;
208 * This function takes a $row array as returned by the addressbook
209 * search and returns an e-mail address with the full name or
210 * nickname optionally prepended.
213 function full_address($row) {
214 global $addrsrch_fullname, $data_dir, $username;
216 if (($prefix = getPref($data_dir, $username, 'addrsrch_fullname') or
217 isset($addrsrch_fullname) and $prefix = $addrsrch_fullname)
218 and $prefix !== 'noprefix') {
219 $name = ($prefix === 'nickname') ?
$row['nickname']
221 return $name . ' <' . trim($row['email']) . '>';
223 return trim($row['email']);
228 Return a list of addresses matching expression in
229 all backends of a given type.
231 function search($expression, $bnum = -1) {
235 /* Search all backends */
237 $sel = $this->get_backend_list('');
239 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
240 $backend = &$sel[$i];
241 $backend->error
= '';
242 $res = $backend->search($expression);
243 if (is_array($res)) {
244 $ret = array_merge($ret, $res);
246 $this->error
.= "<br>\n" . $backend->error
;
251 /* Only fail if all backends failed */
252 if( $failed >= sizeof( $sel ) ) {
258 /* Search only one backend */
260 $ret = $this->backends
[$bnum]->search($expression);
261 if (!is_array($ret)) {
262 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
271 /* Return a sorted search */
272 function s_search($expression, $bnum = -1) {
274 $ret = $this->search($expression, $bnum);
275 if ( is_array( $ret ) ) {
276 usort($ret, 'addressbook_cmp');
283 * Lookup an address by alias. Only possible in
286 function lookup($alias, $bnum = -1) {
291 $res = $this->backends
[$bnum]->lookup($alias);
292 if (is_array($res)) {
295 $this->error
= $backend->error
;
300 $sel = $this->get_backend_list('local');
301 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
302 $backend = &$sel[$i];
303 $backend->error
= '';
304 $res = $backend->lookup($alias);
305 if (is_array($res)) {
309 $this->error
= $backend->error
;
318 /* Return all addresses */
319 function list_addr($bnum = -1) {
323 $sel = $this->get_backend_list('local');
325 $sel = array(0 => &$this->backends
[$bnum]);
328 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
329 $backend = &$sel[$i];
330 $backend->error
= '';
331 $res = $backend->list_addr();
332 if (is_array($res)) {
333 $ret = array_merge($ret, $res);
335 $this->error
= $backend->error
;
344 * Create a new address from $userdata, in backend $bnum.
345 * Return the backend number that the/ address was added
346 * to, or false if it failed.
348 function add($userdata, $bnum) {
351 if (!is_array($userdata)) {
352 $this->error
= _("Invalid input data");
355 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
356 $this->error
= _("Name is missing");
359 if (empty($userdata['email'])) {
360 $this->error
= _("E-mail address is missing");
363 if (empty($userdata['nickname'])) {
364 $userdata['nickname'] = $userdata['email'];
367 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
368 $this->error
= _("Nickname contains illegal characters");
372 /* Check that specified backend accept new entries */
373 if (!$this->backends
[$bnum]->writeable
) {
374 $this->error
= _("Addressbook is read-only");
378 /* Add address to backend */
379 $res = $this->backends
[$bnum]->add($userdata);
383 $this->error
= $this->backends
[$bnum]->error
;
387 return false; // Not reached
392 * Remove the user identified by $alias from backend $bnum
393 * If $alias is an array, all users in the array are removed.
395 function remove($alias, $bnum) {
402 /* Convert string to single element array */
403 if (!is_array($alias)) {
404 $alias = array(0 => $alias);
407 /* Check that specified backend is writeable */
408 if (!$this->backends
[$bnum]->writeable
) {
409 $this->error
= _("Addressbook is read-only");
413 /* Remove user from backend */
414 $res = $this->backends
[$bnum]->remove($alias);
418 $this->error
= $this->backends
[$bnum]->error
;
422 return FALSE; /* Not reached */
423 } /* end of remove() */
427 * Remove the user identified by $alias from backend $bnum
428 * If $alias is an array, all users in the array are removed.
430 function modify($alias, $userdata, $bnum) {
433 if (empty($alias) ||
!is_string($alias)) {
438 if(!is_array($userdata)) {
439 $this->error
= _("Invalid input data");
442 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
443 $this->error
= _("Name is missing");
446 if (empty($userdata['email'])) {
447 $this->error
= _("E-mail address is missing");
451 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
452 $this->error
= _("Nickname contains illegal characters");
456 if (empty($userdata['nickname'])) {
457 $userdata['nickname'] = $userdata['email'];
460 /* Check that specified backend is writeable */
461 if (!$this->backends
[$bnum]->writeable
) {
462 $this->error
= _("Addressbook is read-only");;
466 /* Modify user in backend */
467 $res = $this->backends
[$bnum]->modify($alias, $userdata);
471 $this->error
= $this->backends
[$bnum]->error
;
475 return FALSE; /* Not reached */
476 } /* end of modify() */
479 } /* End of class Addressbook */
482 * Generic backend that all other backends extend
483 * @package squirrelmail
485 class addressbook_backend
{
487 /* Variables that all backends must provide. */
488 var $btype = 'dummy';
489 var $bname = 'dummy';
490 var $sname = 'Dummy backend';
493 * Variables common for all backends, but that
494 * should not be changed by the backends.
498 var $writeable = false;
500 function set_error($string) {
501 $this->error
= '[' . $this->sname
. '] ' . $string;
506 /* ========================== Public ======================== */
508 function search($expression) {
509 $this->set_error('search not implemented');
513 function lookup($alias) {
514 $this->set_error('lookup not implemented');
518 function list_addr() {
519 $this->set_error('list_addr not implemented');
523 function add($userdata) {
524 $this->set_error('add not implemented');
528 function remove($alias) {
529 $this->set_error('delete not implemented');
533 function modify($alias, $newuserdata) {
534 $this->set_error('modify not implemented');
540 /* Sort array by the key "name" */
541 function alistcmp($a,$b) {
542 if ($a['backend'] > $b['backend']) {
545 if ($a['backend'] < $b['backend']) {
549 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
554 PHP 5 requires that the class be made first, which seems rather
555 logical, and should have been the way it was generated the first time.
558 require_once(SM_PATH
. 'functions/abook_local_file.php');
559 require_once(SM_PATH
. 'functions/abook_ldap_server.php');
561 /* Use this if you wanna have a global address book */
562 if (isset($address_book_global_filename)) {
563 include_once(SM_PATH
. 'functions/abook_global_file.php');
566 /* Only load database backend if database is configured */
567 if((isset($addrbook_dsn) && !empty($addrbook_dsn)) ||
568 (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) ) {
569 include_once(SM_PATH
. 'functions/abook_database.php');