2e2ccfda3192a5474199624d3da4d7ca9c510fa4
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');
18 // Create and initialize an addressbook object.
19 // Returns the created object
20 function addressbook_init($showerr = true, $onlylocal = false) {
21 global $data_dir, $username, $ldap_server;
23 // Create a new addressbook object
24 $abook = new AddressBook
;
26 // Always add a local backend
27 $filename = sprintf('%s%s.abook', $data_dir, $username);
28 $r = $abook->add_backend('local_file', Array('filename' => $filename,
31 printf(_("Error opening file %s"), $filename);
38 // Load configured LDAP servers (if PHP has LDAP support)
39 if(is_array($ldap_server) && function_exists('ldap_connect')) {
41 while(list($undef,$param) = each($ldap_server)) {
42 if(is_array($param)) {
43 $r = $abook->add_backend('ldap_server', $param);
45 printf(' ' . _("Error initializing LDAP server %s:") .
46 "<BR>\n", $param['host']);
47 print(' ' . $abook->error
);
54 // Return the initialized object
59 // Had to move this function outside of the Addressbook Class
60 // PHP 4.0.4 Seemed to be having problems with inline functions.
61 function addressbook_cmp($a,$b) {
62 if($a['backend'] > $b['backend'])
64 else if($a['backend'] < $b['backend'])
66 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
71 ** This is the main address book class that connect all the
72 ** backends and provide services to the functions above.
77 var $backends = array();
80 var $localbackend = 0;
81 var $localbackendname = '';
83 // Constructor function.
84 function AddressBook() {
85 $localbackendname = _("Personal address book");
88 // Return an array of backends of a given type,
89 // or all backends if no type is specified.
90 function get_backend_list($type = '') {
92 for($i = 1 ; $i <= $this->numbackends
; $i++
) {
93 if(empty($type) ||
$type == $this->backends
[$i]->btype
) {
94 array_push($ret, &$this->backends
[$i]);
101 // ========================== Public ========================
103 // Add a new backend. $backend is the name of a backend
104 // (without the abook_ prefix), and $param is an optional
105 // mixed variable that is passed to the backend constructor.
106 // See each of the backend classes for valid parameters.
107 function add_backend($backend, $param = '') {
108 $backend_name = 'abook_' . $backend;
109 eval('$newback = new ' . $backend_name . '($param);');
110 if(!empty($newback->error
)) {
111 $this->error
= $newback->error
;
115 $this->numbackends++
;
117 $newback->bnum
= $this->numbackends
;
118 $this->backends
[$this->numbackends
] = $newback;
120 // Store ID of first local backend added
121 if($this->localbackend
== 0 && $newback->btype
== 'local') {
122 $this->localbackend
= $this->numbackends
;
123 $this->localbackendname
= $newback->sname
;
126 return $this->numbackends
;
130 // Return a list of addresses matching expression in
131 // all backends of a given type.
132 function search($expression, $bnum = -1) {
136 // Search all backends
138 $sel = $this->get_backend_list('');
140 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
141 $backend = &$sel[$i];
142 $backend->error
= '';
143 $res = $backend->search($expression);
145 $ret = array_merge($ret, $res);
147 $this->error
.= "<br>\n" . $backend->error
;
152 // Only fail if all backends failed
153 if($failed >= sizeof($sel))
158 // Search only one backend
160 $ret = $this->backends
[$bnum]->search($expression);
161 if(!is_array($ret)) {
162 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
171 // Return a sorted search
172 function s_search($expression, $bnum = -1) {
174 $ret = $this->search($expression, $bnum);
177 usort($ret, 'addressbook_cmp');
182 // Lookup an address by alias. Only possible in
184 function lookup($alias, $bnum = -1) {
188 $res = $this->backends
[$bnum]->lookup($alias);
192 $this->error
= $backend->error
;
197 $sel = $this->get_backend_list('local');
198 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
199 $backend = &$sel[$i];
200 $backend->error
= '';
201 $res = $backend->lookup($alias);
206 $this->error
= $backend->error
;
215 // Return all addresses
216 function list_addr($bnum = -1) {
220 $sel = $this->get_backend_list('local');
222 $sel = array(0 => &$this->backends
[$bnum]);
224 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
225 $backend = &$sel[$i];
226 $backend->error
= '';
227 $res = $backend->list_addr();
229 $ret = array_merge($ret, $res);
231 $this->error
= $backend->error
;
240 // Create a new address from $userdata, in backend $bnum.
241 // Return the backend number that the/ address was added
242 // to, or false if it failed.
243 function add($userdata, $bnum) {
246 if(!is_array($userdata)) {
247 $this->error
= _("Invalid input data");
250 if(empty($userdata['firstname']) &&
251 empty($userdata['lastname'])) {
252 $this->error
= _("Name is missing");
255 if(empty($userdata['email'])) {
256 $this->error
= _("E-mail address is missing");
259 if(empty($userdata['nickname'])) {
260 $userdata['nickname'] = $userdata['email'];
263 if(eregi('[\\: \\|\\#\"\\!]', $userdata['nickname'])) {
264 $this->error
= _("Nickname contain illegal characters");
268 // Check that specified backend accept new entries
269 if(!$this->backends
[$bnum]->writeable
) {
270 $this->error
= _("Addressbook is read-only");
274 // Add address to backend
275 $res = $this->backends
[$bnum]->add($userdata);
279 $this->error
= $this->backends
[$bnum]->error
;
283 return false; // Not reached
287 // Remove the user identified by $alias from backend $bnum
288 // If $alias is an array, all users in the array are removed.
289 function remove($alias, $bnum) {
295 // Convert string to single element array
296 if(!is_array($alias))
297 $alias = array(0 => $alias);
299 // Check that specified backend is writable
300 if(!$this->backends
[$bnum]->writeable
) {
301 $this->error
= _("Addressbook is read-only");
305 // Remove user from backend
306 $res = $this->backends
[$bnum]->remove($alias);
310 $this->error
= $this->backends
[$bnum]->error
;
314 return false; // Not reached
318 // Remove the user identified by $alias from backend $bnum
319 // If $alias is an array, all users in the array are removed.
320 function modify($alias, $userdata, $bnum) {
323 if(empty($alias) ||
!is_string($alias))
327 if(!is_array($userdata)) {
328 $this->error
= _("Invalid input data");
331 if(empty($userdata['firstname']) &&
332 empty($userdata['lastname'])) {
333 $this->error
= _("Name is missing");
336 if(empty($userdata['email'])) {
337 $this->error
= _("E-mail address is missing");
341 if(eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
342 $this->error
= _("Nickname contain illegal characters");
346 if(empty($userdata['nickname'])) {
347 $userdata['nickname'] = $userdata['email'];
350 // Check that specified backend is writable
351 if(!$this->backends
[$bnum]->writeable
) {
352 $this->error
= _("Addressbook is read-only");;
356 // Modify user in backend
357 $res = $this->backends
[$bnum]->modify($alias, $userdata);
361 $this->error
= $this->backends
[$bnum]->error
;
365 return false; // Not reached
369 } // End of class Addressbook
372 ** Generic backend that all other backends extend
374 class addressbook_backend
{
376 // Variables that all backends must provide.
377 var $btype = 'dummy';
378 var $bname = 'dummy';
379 var $sname = 'Dummy backend';
381 // Variables common for all backends, but that
382 // should not be changed by the backends.
385 var $writeable = false;
387 function set_error($string) {
388 $this->error
= '[' . $this->sname
. '] ' . $string;
393 // ========================== Public ========================
395 function search($expression) {
396 $this->set_error('search not implemented');
400 function lookup($alias) {
401 $this->set_error('lookup not implemented');
405 function list_addr() {
406 $this->set_error('list_addr not implemented');
410 function add($userdata) {
411 $this->set_error('add not implemented');
415 function remove($alias) {
416 $this->set_error('delete not implemented');
420 function modify($alias, $newuserdata) {
421 $this->set_error('modify not implemented');