6 * Copyright (c) 1999-2002 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.
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) && !empty($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;
54 global $addrbook_dsn, $addrbook_table;
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 if (!isset($addrbook_table) ||
empty($addrbook_table)) {
67 $addrbook_table = 'address';
69 $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
71 'table' => $addrbook_table));
72 if (!$r && $showerr) {
73 echo _("Error initializing addressbook database.");
78 $filename = getHashedFile($username, $data_dir, "$username.abook");
79 $r = $abook->add_backend('local_file', Array('filename' => $filename,
82 printf( _("Error opening file %s"), $filename );
88 /* This would be for the global addressbook */
89 if (isset($address_book_global_filename)) {
90 $r = $abook->add_backend('global_file');
91 if (!$r && $showerr) {
92 echo _("Error initializing global addressbook.");
101 /* Load configured LDAP servers (if PHP has LDAP support) */
102 if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) {
104 while (list($undef,$param) = each($ldap_server)) {
105 if (is_array($param)) {
106 $r = $abook->add_backend('ldap_server', $param);
107 if (!$r && $showerr) {
108 printf( ' ' . _("Error initializing LDAP server %s:") .
109 "<BR>\n", $param['host']);
110 echo ' ' . $abook->error
;
117 /* Return the initialized object */
123 * Had to move this function outside of the Addressbook Class
124 * PHP 4.0.4 Seemed to be having problems with inline functions.
126 function addressbook_cmp($a,$b) {
128 if($a['backend'] > $b['backend']) {
130 } else if($a['backend'] < $b['backend']) {
134 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
140 * This is the main address book class that connect all the
141 * backends and provide services to the functions above.
147 var $backends = array();
148 var $numbackends = 0;
150 var $localbackend = 0;
151 var $localbackendname = '';
153 // Constructor function.
154 function AddressBook() {
155 $localbackendname = _("Personal address book");
159 * Return an array of backends of a given type,
160 * or all backends if no type is specified.
162 function get_backend_list($type = '') {
164 for ($i = 1 ; $i <= $this->numbackends
; $i++
) {
165 if (empty($type) ||
$type == $this->backends
[$i]->btype
) {
166 $ret[] = &$this->backends
[$i];
174 ========================== Public ========================
176 Add a new backend. $backend is the name of a backend
177 (without the abook_ prefix), and $param is an optional
178 mixed variable that is passed to the backend constructor.
179 See each of the backend classes for valid parameters.
181 function add_backend($backend, $param = '') {
182 $backend_name = 'abook_' . $backend;
183 eval('$newback = new ' . $backend_name . '($param);');
184 if(!empty($newback->error
)) {
185 $this->error
= $newback->error
;
189 $this->numbackends++
;
191 $newback->bnum
= $this->numbackends
;
192 $this->backends
[$this->numbackends
] = $newback;
194 /* Store ID of first local backend added */
195 if ($this->localbackend
== 0 && $newback->btype
== 'local') {
196 $this->localbackend
= $this->numbackends
;
197 $this->localbackendname
= $newback->sname
;
200 return $this->numbackends
;
205 * This function takes a $row array as returned by the addressbook
206 * search and returns an e-mail address with the full name or
207 * nickname optionally prepended.
210 function full_address($row) {
211 global $addrsrch_fullname;
213 if ((isset($addrsrch_fullname)) && ($addrsrch_fullname)) {
214 $name = ($addrsrch_fullname === 'nickname') ?
$row['nickname']
216 return $name . ' <' . trim($row['email']) . '>';
218 return trim($row['email']);
223 Return a list of addresses matching expression in
224 all backends of a given type.
226 function search($expression, $bnum = -1) {
230 /* Search all backends */
232 $sel = $this->get_backend_list('');
234 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
235 $backend = &$sel[$i];
236 $backend->error
= '';
237 $res = $backend->search($expression);
238 if (is_array($res)) {
239 $ret = array_merge($ret, $res);
241 $this->error
.= "<br>\n" . $backend->error
;
246 /* Only fail if all backends failed */
247 if( $failed >= sizeof( $sel ) ) {
253 /* Search only one backend */
255 $ret = $this->backends
[$bnum]->search($expression);
256 if (!is_array($ret)) {
257 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
266 /* Return a sorted search */
267 function s_search($expression, $bnum = -1) {
269 $ret = $this->search($expression, $bnum);
270 if ( is_array( $ret ) ) {
271 usort($ret, 'addressbook_cmp');
278 * Lookup an address by alias. Only possible in
281 function lookup($alias, $bnum = -1) {
286 $res = $this->backends
[$bnum]->lookup($alias);
287 if (is_array($res)) {
290 $this->error
= $backend->error
;
295 $sel = $this->get_backend_list('local');
296 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
297 $backend = &$sel[$i];
298 $backend->error
= '';
299 $res = $backend->lookup($alias);
300 if (is_array($res)) {
304 $this->error
= $backend->error
;
313 /* Return all addresses */
314 function list_addr($bnum = -1) {
318 $sel = $this->get_backend_list('local');
320 $sel = array(0 => &$this->backends
[$bnum]);
323 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
324 $backend = &$sel[$i];
325 $backend->error
= '';
326 $res = $backend->list_addr();
327 if (is_array($res)) {
328 $ret = array_merge($ret, $res);
330 $this->error
= $backend->error
;
339 * Create a new address from $userdata, in backend $bnum.
340 * Return the backend number that the/ address was added
341 * to, or false if it failed.
343 function add($userdata, $bnum) {
346 if (!is_array($userdata)) {
347 $this->error
= _("Invalid input data");
350 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
351 $this->error
= _("Name is missing");
354 if (empty($userdata['email'])) {
355 $this->error
= _("E-mail address is missing");
358 if (empty($userdata['nickname'])) {
359 $userdata['nickname'] = $userdata['email'];
362 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
363 $this->error
= _("Nickname contains illegal characters");
367 /* Check that specified backend accept new entries */
368 if (!$this->backends
[$bnum]->writeable
) {
369 $this->error
= _("Addressbook is read-only");
373 /* Add address to backend */
374 $res = $this->backends
[$bnum]->add($userdata);
378 $this->error
= $this->backends
[$bnum]->error
;
382 return false; // Not reached
387 * Remove the user identified by $alias from backend $bnum
388 * If $alias is an array, all users in the array are removed.
390 function remove($alias, $bnum) {
397 /* Convert string to single element array */
398 if (!is_array($alias)) {
399 $alias = array(0 => $alias);
402 /* Check that specified backend is writable */
403 if (!$this->backends
[$bnum]->writeable
) {
404 $this->error
= _("Addressbook is read-only");
408 /* Remove user from backend */
409 $res = $this->backends
[$bnum]->remove($alias);
413 $this->error
= $this->backends
[$bnum]->error
;
417 return FALSE; /* Not reached */
418 } /* end of remove() */
422 * Remove the user identified by $alias from backend $bnum
423 * If $alias is an array, all users in the array are removed.
425 function modify($alias, $userdata, $bnum) {
428 if (empty($alias) ||
!is_string($alias)) {
433 if(!is_array($userdata)) {
434 $this->error
= _("Invalid input data");
437 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
438 $this->error
= _("Name is missing");
441 if (empty($userdata['email'])) {
442 $this->error
= _("E-mail address is missing");
446 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
447 $this->error
= _("Nickname contains illegal characters");
451 if (empty($userdata['nickname'])) {
452 $userdata['nickname'] = $userdata['email'];
455 /* Check that specified backend is writable */
456 if (!$this->backends
[$bnum]->writeable
) {
457 $this->error
= _("Addressbook is read-only");;
461 /* Modify user in backend */
462 $res = $this->backends
[$bnum]->modify($alias, $userdata);
466 $this->error
= $this->backends
[$bnum]->error
;
470 return FALSE; /* Not reached */
471 } /* end of modify() */
474 } /* End of class Addressbook */
477 * Generic backend that all other backends extend
479 class addressbook_backend
{
481 /* Variables that all backends must provide. */
482 var $btype = 'dummy';
483 var $bname = 'dummy';
484 var $sname = 'Dummy backend';
487 * Variables common for all backends, but that
488 * should not be changed by the backends.
492 var $writeable = false;
494 function set_error($string) {
495 $this->error
= '[' . $this->sname
. '] ' . $string;
500 /* ========================== Public ======================== */
502 function search($expression) {
503 $this->set_error('search not implemented');
507 function lookup($alias) {
508 $this->set_error('lookup not implemented');
512 function list_addr() {
513 $this->set_error('list_addr not implemented');
517 function add($userdata) {
518 $this->set_error('add not implemented');
522 function remove($alias) {
523 $this->set_error('delete not implemented');
527 function modify($alias, $newuserdata) {
528 $this->set_error('modify not implemented');
534 /* Sort array by the key "name" */
535 function alistcmp($a,$b) {
536 if ($a['backend'] > $b['backend']) {
539 if ($a['backend'] < $b['backend']) {
543 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;