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(isset($ldap_server) && is_array($ldap_server) &&
60 function_exists('ldap_connect')) {
62 while(list($undef,$param) = each($ldap_server)) {
63 if(is_array($param)) {
64 $r = $abook->add_backend('ldap_server', $param);
66 printf(' ' . _("Error initializing LDAP server %s:") .
67 "<BR>\n", $param['host']);
68 print(' ' . $abook->error
);
75 // Return the initialized object
80 // Had to move this function outside of the Addressbook Class
81 // PHP 4.0.4 Seemed to be having problems with inline functions.
82 function addressbook_cmp($a,$b) {
83 if($a['backend'] > $b['backend'])
85 else if($a['backend'] < $b['backend'])
87 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
92 ** This is the main address book class that connect all the
93 ** backends and provide services to the functions above.
98 var $backends = array();
101 var $localbackend = 0;
102 var $localbackendname = '';
104 // Constructor function.
105 function AddressBook() {
106 $localbackendname = _("Personal address book");
109 // Return an array of backends of a given type,
110 // or all backends if no type is specified.
111 function get_backend_list($type = '') {
113 for($i = 1 ; $i <= $this->numbackends
; $i++
) {
114 if(empty($type) ||
$type == $this->backends
[$i]->btype
) {
115 $ret[] = &$this->backends
[$i];
122 // ========================== Public ========================
124 // Add a new backend. $backend is the name of a backend
125 // (without the abook_ prefix), and $param is an optional
126 // mixed variable that is passed to the backend constructor.
127 // See each of the backend classes for valid parameters.
128 function add_backend($backend, $param = '') {
129 $backend_name = 'abook_' . $backend;
130 eval('$newback = new ' . $backend_name . '($param);');
131 if(!empty($newback->error
)) {
132 $this->error
= $newback->error
;
136 $this->numbackends++
;
138 $newback->bnum
= $this->numbackends
;
139 $this->backends
[$this->numbackends
] = $newback;
141 // Store ID of first local backend added
142 if($this->localbackend
== 0 && $newback->btype
== 'local') {
143 $this->localbackend
= $this->numbackends
;
144 $this->localbackendname
= $newback->sname
;
147 return $this->numbackends
;
151 // Return a list of addresses matching expression in
152 // all backends of a given type.
153 function search($expression, $bnum = -1) {
157 // Search all backends
159 $sel = $this->get_backend_list('');
161 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
162 $backend = &$sel[$i];
163 $backend->error
= '';
164 $res = $backend->search($expression);
166 $ret = array_merge($ret, $res);
168 $this->error
.= "<br>\n" . $backend->error
;
173 // Only fail if all backends failed
174 if($failed >= sizeof($sel))
179 // Search only one backend
181 $ret = $this->backends
[$bnum]->search($expression);
182 if(!is_array($ret)) {
183 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
192 // Return a sorted search
193 function s_search($expression, $bnum = -1) {
195 $ret = $this->search($expression, $bnum);
198 usort($ret, 'addressbook_cmp');
203 // Lookup an address by alias. Only possible in
205 function lookup($alias, $bnum = -1) {
209 $res = $this->backends
[$bnum]->lookup($alias);
213 $this->error
= $backend->error
;
218 $sel = $this->get_backend_list('local');
219 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
220 $backend = &$sel[$i];
221 $backend->error
= '';
222 $res = $backend->lookup($alias);
227 $this->error
= $backend->error
;
236 // Return all addresses
237 function list_addr($bnum = -1) {
241 $sel = $this->get_backend_list('local');
243 $sel = array(0 => &$this->backends
[$bnum]);
245 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
246 $backend = &$sel[$i];
247 $backend->error
= '';
248 $res = $backend->list_addr();
250 $ret = array_merge($ret, $res);
252 $this->error
= $backend->error
;
261 // Create a new address from $userdata, in backend $bnum.
262 // Return the backend number that the/ address was added
263 // to, or false if it failed.
264 function add($userdata, $bnum) {
267 if(!is_array($userdata)) {
268 $this->error
= _("Invalid input data");
271 if(empty($userdata['firstname']) &&
272 empty($userdata['lastname'])) {
273 $this->error
= _("Name is missing");
276 if(empty($userdata['email'])) {
277 $this->error
= _("E-mail address is missing");
280 if(empty($userdata['nickname'])) {
281 $userdata['nickname'] = $userdata['email'];
284 if(eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
285 $this->error
= _("Nickname contain illegal characters");
289 // Check that specified backend accept new entries
290 if(!$this->backends
[$bnum]->writeable
) {
291 $this->error
= _("Addressbook is read-only");
295 // Add address to backend
296 $res = $this->backends
[$bnum]->add($userdata);
300 $this->error
= $this->backends
[$bnum]->error
;
304 return false; // Not reached
308 // Remove the user identified by $alias from backend $bnum
309 // If $alias is an array, all users in the array are removed.
310 function remove($alias, $bnum) {
316 // Convert string to single element array
317 if(!is_array($alias))
318 $alias = array(0 => $alias);
320 // Check that specified backend is writable
321 if(!$this->backends
[$bnum]->writeable
) {
322 $this->error
= _("Addressbook is read-only");
326 // Remove user from backend
327 $res = $this->backends
[$bnum]->remove($alias);
331 $this->error
= $this->backends
[$bnum]->error
;
335 return false; // Not reached
339 // Remove the user identified by $alias from backend $bnum
340 // If $alias is an array, all users in the array are removed.
341 function modify($alias, $userdata, $bnum) {
344 if(empty($alias) ||
!is_string($alias))
348 if(!is_array($userdata)) {
349 $this->error
= _("Invalid input data");
352 if(empty($userdata['firstname']) &&
353 empty($userdata['lastname'])) {
354 $this->error
= _("Name is missing");
357 if(empty($userdata['email'])) {
358 $this->error
= _("E-mail address is missing");
362 if(eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
363 $this->error
= _("Nickname contain illegal characters");
367 if(empty($userdata['nickname'])) {
368 $userdata['nickname'] = $userdata['email'];
371 // Check that specified backend is writable
372 if(!$this->backends
[$bnum]->writeable
) {
373 $this->error
= _("Addressbook is read-only");;
377 // Modify user in backend
378 $res = $this->backends
[$bnum]->modify($alias, $userdata);
382 $this->error
= $this->backends
[$bnum]->error
;
386 return false; // Not reached
390 } // End of class Addressbook
393 ** Generic backend that all other backends extend
395 class addressbook_backend
{
397 // Variables that all backends must provide.
398 var $btype = 'dummy';
399 var $bname = 'dummy';
400 var $sname = 'Dummy backend';
402 // Variables common for all backends, but that
403 // should not be changed by the backends.
406 var $writeable = false;
408 function set_error($string) {
409 $this->error
= '[' . $this->sname
. '] ' . $string;
414 // ========================== Public ========================
416 function search($expression) {
417 $this->set_error('search not implemented');
421 function lookup($alias) {
422 $this->set_error('lookup not implemented');
426 function list_addr() {
427 $this->set_error('list_addr not implemented');
431 function add($userdata) {
432 $this->set_error('add not implemented');
436 function remove($alias) {
437 $this->set_error('delete not implemented');
441 function modify($alias, $newuserdata) {
442 $this->set_error('modify not implemented');