6 ** Functions and classes for the addressbook system.
11 $addressbook_php = true;
13 // Include backends here.
14 include('../functions/abook_local_file.php');
15 include('../functions/abook_ldap_server.php');
17 // Un-comment if you're using database backend
18 // include('../functions/abook_database.php');
21 // Create and initialize an addressbook object.
22 // Returns the created object
23 function addressbook_init($showerr = true, $onlylocal = false) {
24 global $data_dir, $username, $ldap_server;
26 // Create a new addressbook object
27 $abook = new AddressBook
;
29 // Always add a local backend
31 // Use *either* file-based *or* database addressbook. Remove
32 // and insert comments to enable the one you want.
34 // ------ BEGIN Initialize file-based personal addressbook ------
35 $filename = sprintf('%s%s.abook', $data_dir, $username);
36 $r = $abook->add_backend('local_file', Array('filename' => $filename,
40 printf(_("Error opening file %s"), $filename);
43 // ------ END Initialize file-based personal addressbook ------
45 // ------ BEGIN Initialize database-based personal addressbook ------
46 // $r = $abook->add_backend('database', Array('dsn' => 'mysql://dbuser@host/dbname',
47 // 'owner' => $username,
48 // 'table' => 'address'));
49 // if(!$r && $showerr) {
50 // printf(_("Error initializing addressbook: %s"), $filename);
53 // ------ END Initialize database-based personal addressbook ------
58 // Load configured LDAP servers (if PHP has LDAP support)
59 if(is_array($ldap_server) && function_exists('ldap_connect')) {
61 while(list($undef,$param) = each($ldap_server)) {
62 if(is_array($param)) {
63 $r = $abook->add_backend('ldap_server', $param);
65 printf(' ' . _("Error initializing LDAP server %s:") .
66 "<BR>\n", $param['host']);
67 print(' ' . $abook->error
);
74 // Return the initialized object
79 // Had to move this function outside of the Addressbook Class
80 // PHP 4.0.4 Seemed to be having problems with inline functions.
81 function addressbook_cmp($a,$b) {
82 if($a['backend'] > $b['backend'])
84 else if($a['backend'] < $b['backend'])
86 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
91 ** This is the main address book class that connect all the
92 ** backends and provide services to the functions above.
97 var $backends = array();
100 var $localbackend = 0;
101 var $localbackendname = '';
103 // Constructor function.
104 function AddressBook() {
105 $localbackendname = _("Personal address book");
108 // Return an array of backends of a given type,
109 // or all backends if no type is specified.
110 function get_backend_list($type = '') {
112 for($i = 1 ; $i <= $this->numbackends
; $i++
) {
113 if(empty($type) ||
$type == $this->backends
[$i]->btype
) {
114 array_push($ret, &$this->backends
[$i]);
121 // ========================== Public ========================
123 // Add a new backend. $backend is the name of a backend
124 // (without the abook_ prefix), and $param is an optional
125 // mixed variable that is passed to the backend constructor.
126 // See each of the backend classes for valid parameters.
127 function add_backend($backend, $param = '') {
128 $backend_name = 'abook_' . $backend;
129 eval('$newback = new ' . $backend_name . '($param);');
130 if(!empty($newback->error
)) {
131 $this->error
= $newback->error
;
135 $this->numbackends++
;
137 $newback->bnum
= $this->numbackends
;
138 $this->backends
[$this->numbackends
] = $newback;
140 // Store ID of first local backend added
141 if($this->localbackend
== 0 && $newback->btype
== 'local') {
142 $this->localbackend
= $this->numbackends
;
143 $this->localbackendname
= $newback->sname
;
146 return $this->numbackends
;
150 // Return a list of addresses matching expression in
151 // all backends of a given type.
152 function search($expression, $bnum = -1) {
156 // Search all backends
158 $sel = $this->get_backend_list('');
160 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
161 $backend = &$sel[$i];
162 $backend->error
= '';
163 $res = $backend->search($expression);
165 $ret = array_merge($ret, $res);
167 $this->error
.= "<br>\n" . $backend->error
;
172 // Only fail if all backends failed
173 if($failed >= sizeof($sel))
178 // Search only one backend
180 $ret = $this->backends
[$bnum]->search($expression);
181 if(!is_array($ret)) {
182 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
191 // Return a sorted search
192 function s_search($expression, $bnum = -1) {
194 $ret = $this->search($expression, $bnum);
197 usort($ret, 'addressbook_cmp');
202 // Lookup an address by alias. Only possible in
204 function lookup($alias, $bnum = -1) {
208 $res = $this->backends
[$bnum]->lookup($alias);
212 $this->error
= $backend->error
;
217 $sel = $this->get_backend_list('local');
218 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
219 $backend = &$sel[$i];
220 $backend->error
= '';
221 $res = $backend->lookup($alias);
226 $this->error
= $backend->error
;
235 // Return all addresses
236 function list_addr($bnum = -1) {
240 $sel = $this->get_backend_list('local');
242 $sel = array(0 => &$this->backends
[$bnum]);
244 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
245 $backend = &$sel[$i];
246 $backend->error
= '';
247 $res = $backend->list_addr();
249 $ret = array_merge($ret, $res);
251 $this->error
= $backend->error
;
260 // Create a new address from $userdata, in backend $bnum.
261 // Return the backend number that the/ address was added
262 // to, or false if it failed.
263 function add($userdata, $bnum) {
266 if(!is_array($userdata)) {
267 $this->error
= _("Invalid input data");
270 if(empty($userdata['firstname']) &&
271 empty($userdata['lastname'])) {
272 $this->error
= _("Name is missing");
275 if(empty($userdata['email'])) {
276 $this->error
= _("E-mail address is missing");
279 if(empty($userdata['nickname'])) {
280 $userdata['nickname'] = $userdata['email'];
283 if(eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
284 $this->error
= _("Nickname contain illegal characters");
288 // Check that specified backend accept new entries
289 if(!$this->backends
[$bnum]->writeable
) {
290 $this->error
= _("Addressbook is read-only");
294 // Add address to backend
295 $res = $this->backends
[$bnum]->add($userdata);
299 $this->error
= $this->backends
[$bnum]->error
;
303 return false; // Not reached
307 // Remove the user identified by $alias from backend $bnum
308 // If $alias is an array, all users in the array are removed.
309 function remove($alias, $bnum) {
315 // Convert string to single element array
316 if(!is_array($alias))
317 $alias = array(0 => $alias);
319 // Check that specified backend is writable
320 if(!$this->backends
[$bnum]->writeable
) {
321 $this->error
= _("Addressbook is read-only");
325 // Remove user from backend
326 $res = $this->backends
[$bnum]->remove($alias);
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 modify($alias, $userdata, $bnum) {
343 if(empty($alias) ||
!is_string($alias))
347 if(!is_array($userdata)) {
348 $this->error
= _("Invalid input data");
351 if(empty($userdata['firstname']) &&
352 empty($userdata['lastname'])) {
353 $this->error
= _("Name is missing");
356 if(empty($userdata['email'])) {
357 $this->error
= _("E-mail address is missing");
361 if(eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
362 $this->error
= _("Nickname contain illegal characters");
366 if(empty($userdata['nickname'])) {
367 $userdata['nickname'] = $userdata['email'];
370 // Check that specified backend is writable
371 if(!$this->backends
[$bnum]->writeable
) {
372 $this->error
= _("Addressbook is read-only");;
376 // Modify user in backend
377 $res = $this->backends
[$bnum]->modify($alias, $userdata);
381 $this->error
= $this->backends
[$bnum]->error
;
385 return false; // Not reached
389 } // End of class Addressbook
392 ** Generic backend that all other backends extend
394 class addressbook_backend
{
396 // Variables that all backends must provide.
397 var $btype = 'dummy';
398 var $bname = 'dummy';
399 var $sname = 'Dummy backend';
401 // Variables common for all backends, but that
402 // should not be changed by the backends.
405 var $writeable = false;
407 function set_error($string) {
408 $this->error
= '[' . $this->sname
. '] ' . $string;
413 // ========================== Public ========================
415 function search($expression) {
416 $this->set_error('search not implemented');
420 function lookup($alias) {
421 $this->set_error('lookup not implemented');
425 function list_addr() {
426 $this->set_error('list_addr not implemented');
430 function add($userdata) {
431 $this->set_error('add not implemented');
435 function remove($alias) {
436 $this->set_error('delete not implemented');
440 function modify($alias, $newuserdata) {
441 $this->set_error('modify not implemented');