6 ** Functions and classes for the addressbook system.
12 // This is the path to the global site-wide addressbook.
13 // It looks and feels just like a user's .abook file
14 // If this is in the data directory, use "$data_dir/global.abook"
15 // If not, specify the path as though it was accessed from the
16 // src/ directory ("../global.abook" -> in main directory)
18 // If you don't want a global site-wide addressbook, comment these
19 // two lines out. (They are disabled by default.)
21 // The global addressbook is unmodifiable by anyone. You must actually
22 // use a shell script or whatnot to modify the contents.
25 //$address_book_global_filename = "$data_dir/global.abook";
29 // Include backends here.
30 require_once('../functions/abook_local_file.php');
31 require_once('../functions/abook_ldap_server.php');
33 // Use this if you wanna have a global address book
34 if (isset($address_book_global_filename))
35 include_once('../functions/abook_global_file.php');
37 // Only load database backend if database is configured
39 if(isset($addrbook_dsn))
40 include_once('../functions/abook_database.php');
43 // Create and initialize an addressbook object.
44 // Returns the created object
45 function addressbook_init($showerr = true, $onlylocal = false) {
46 global $data_dir, $username, $ldap_server, $address_book_global_filename;
49 // Create a new addressbook object
50 $abook = new AddressBook
;
52 // Always add a local backend. We use *either* file-based *or* a
53 // database addressbook. If $addrbook_dsn is set, the database
54 // backend is used. If not, addressbooks are stores in files.
55 if(isset($addrbook_dsn) && !empty($addrbook_dsn)) {
57 $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
59 'table' => 'address'));
61 printf(_("Error initializing addressbook database."));
66 $filename = sprintf('%s%s.abook', $data_dir, $username);
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 printf(_("Error initializing global addressbook."));
88 // Load configured LDAP servers (if PHP has LDAP support)
89 if(isset($ldap_server) && is_array($ldap_server) &&
90 function_exists('ldap_connect')) {
92 while(list($undef,$param) = each($ldap_server)) {
93 if(is_array($param)) {
94 $r = $abook->add_backend('ldap_server', $param);
96 printf(' ' . _("Error initializing LDAP server %s:") .
97 "<BR>\n", $param['host']);
98 print(' ' . $abook->error
);
105 // Return the initialized object
110 // Had to move this function outside of the Addressbook Class
111 // PHP 4.0.4 Seemed to be having problems with inline functions.
112 function addressbook_cmp($a,$b) {
113 if($a['backend'] > $b['backend'])
115 else if($a['backend'] < $b['backend'])
117 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
122 ** This is the main address book class that connect all the
123 ** backends and provide services to the functions above.
128 var $backends = array();
129 var $numbackends = 0;
131 var $localbackend = 0;
132 var $localbackendname = '';
134 // Constructor function.
135 function AddressBook() {
136 $localbackendname = _("Personal address book");
139 // Return an array of backends of a given type,
140 // or all backends if no type is specified.
141 function get_backend_list($type = '') {
143 for($i = 1 ; $i <= $this->numbackends
; $i++
) {
144 if(empty($type) ||
$type == $this->backends
[$i]->btype
) {
145 $ret[] = &$this->backends
[$i];
152 // ========================== Public ========================
154 // Add a new backend. $backend is the name of a backend
155 // (without the abook_ prefix), and $param is an optional
156 // mixed variable that is passed to the backend constructor.
157 // See each of the backend classes for valid parameters.
158 function add_backend($backend, $param = '') {
159 $backend_name = 'abook_' . $backend;
160 eval('$newback = new ' . $backend_name . '($param);');
161 if(!empty($newback->error
)) {
162 $this->error
= $newback->error
;
166 $this->numbackends++
;
168 $newback->bnum
= $this->numbackends
;
169 $this->backends
[$this->numbackends
] = $newback;
171 // Store ID of first local backend added
172 if($this->localbackend
== 0 && $newback->btype
== 'local') {
173 $this->localbackend
= $this->numbackends
;
174 $this->localbackendname
= $newback->sname
;
177 return $this->numbackends
;
181 // Return a list of addresses matching expression in
182 // all backends of a given type.
183 function search($expression, $bnum = -1) {
187 // Search all backends
189 $sel = $this->get_backend_list('');
191 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
192 $backend = &$sel[$i];
193 $backend->error
= '';
194 $res = $backend->search($expression);
196 $ret = array_merge($ret, $res);
198 $this->error
.= "<br>\n" . $backend->error
;
203 // Only fail if all backends failed
204 if($failed >= sizeof($sel))
209 // Search only one backend
211 $ret = $this->backends
[$bnum]->search($expression);
212 if(!is_array($ret)) {
213 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
222 // Return a sorted search
223 function s_search($expression, $bnum = -1) {
225 $ret = $this->search($expression, $bnum);
228 usort($ret, 'addressbook_cmp');
233 // Lookup an address by alias. Only possible in
235 function lookup($alias, $bnum = -1) {
239 $res = $this->backends
[$bnum]->lookup($alias);
243 $this->error
= $backend->error
;
248 $sel = $this->get_backend_list('local');
249 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
250 $backend = &$sel[$i];
251 $backend->error
= '';
252 $res = $backend->lookup($alias);
257 $this->error
= $backend->error
;
266 // Return all addresses
267 function list_addr($bnum = -1) {
271 $sel = $this->get_backend_list('local');
273 $sel = array(0 => &$this->backends
[$bnum]);
275 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
276 $backend = &$sel[$i];
277 $backend->error
= '';
278 $res = $backend->list_addr();
280 $ret = array_merge($ret, $res);
282 $this->error
= $backend->error
;
291 // Create a new address from $userdata, in backend $bnum.
292 // Return the backend number that the/ address was added
293 // to, or false if it failed.
294 function add($userdata, $bnum) {
297 if(!is_array($userdata)) {
298 $this->error
= _("Invalid input data");
301 if(empty($userdata['firstname']) &&
302 empty($userdata['lastname'])) {
303 $this->error
= _("Name is missing");
306 if(empty($userdata['email'])) {
307 $this->error
= _("E-mail address is missing");
310 if(empty($userdata['nickname'])) {
311 $userdata['nickname'] = $userdata['email'];
314 if(eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
315 $this->error
= _("Nickname contains illegal characters");
319 // Check that specified backend accept new entries
320 if(!$this->backends
[$bnum]->writeable
) {
321 $this->error
= _("Addressbook is read-only");
325 // Add address to backend
326 $res = $this->backends
[$bnum]->add($userdata);
330 $this->error
= $this->backends
[$bnum]->error
;
334 return false; // Not reached
338 // Remove the user identified by $alias from backend $bnum
339 // If $alias is an array, all users in the array are removed.
340 function remove($alias, $bnum) {
346 // Convert string to single element array
347 if(!is_array($alias))
348 $alias = array(0 => $alias);
350 // Check that specified backend is writable
351 if(!$this->backends
[$bnum]->writeable
) {
352 $this->error
= _("Addressbook is read-only");
356 // Remove user from backend
357 $res = $this->backends
[$bnum]->remove($alias);
361 $this->error
= $this->backends
[$bnum]->error
;
365 return false; // Not reached
369 // Remove the user identified by $alias from backend $bnum
370 // If $alias is an array, all users in the array are removed.
371 function modify($alias, $userdata, $bnum) {
374 if(empty($alias) ||
!is_string($alias))
378 if(!is_array($userdata)) {
379 $this->error
= _("Invalid input data");
382 if(empty($userdata['firstname']) &&
383 empty($userdata['lastname'])) {
384 $this->error
= _("Name is missing");
387 if(empty($userdata['email'])) {
388 $this->error
= _("E-mail address is missing");
392 if(eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
393 $this->error
= _("Nickname contains illegal characters");
397 if(empty($userdata['nickname'])) {
398 $userdata['nickname'] = $userdata['email'];
401 // Check that specified backend is writable
402 if(!$this->backends
[$bnum]->writeable
) {
403 $this->error
= _("Addressbook is read-only");;
407 // Modify user in backend
408 $res = $this->backends
[$bnum]->modify($alias, $userdata);
412 $this->error
= $this->backends
[$bnum]->error
;
416 return false; // Not reached
420 } // End of class Addressbook
423 ** Generic backend that all other backends extend
425 class addressbook_backend
{
427 // Variables that all backends must provide.
428 var $btype = 'dummy';
429 var $bname = 'dummy';
430 var $sname = 'Dummy backend';
432 // Variables common for all backends, but that
433 // should not be changed by the backends.
436 var $writeable = false;
438 function set_error($string) {
439 $this->error
= '[' . $this->sname
. '] ' . $string;
444 // ========================== Public ========================
446 function search($expression) {
447 $this->set_error('search not implemented');
451 function lookup($alias) {
452 $this->set_error('lookup not implemented');
456 function list_addr() {
457 $this->set_error('list_addr not implemented');
461 function add($userdata) {
462 $this->set_error('add not implemented');
466 function remove($alias) {
467 $this->set_error('delete not implemented');
471 function modify($alias, $newuserdata) {
472 $this->set_error('modify not implemented');