6 ** Functions and classes for the addressbook system.
11 if (defined ('addressbook_php'))
13 define ('addressbook_php', true);
15 // Include backends here.
16 include('../functions/abook_local_file.php');
17 include('../functions/abook_ldap_server.php');
19 // Un-comment if you're using database backend
20 // include('../functions/abook_database.php');
23 // Create and initialize an addressbook object.
24 // Returns the created object
25 function addressbook_init($showerr = true, $onlylocal = false) {
26 global $data_dir, $username, $ldap_server;
28 // Create a new addressbook object
29 $abook = new AddressBook
;
31 // Always add a local backend
33 // Use *either* file-based *or* database addressbook. Remove
34 // and insert comments to enable the one you want.
36 // ------ BEGIN Initialize file-based personal addressbook ------
37 $filename = sprintf('%s%s.abook', $data_dir, $username);
38 $r = $abook->add_backend('local_file', Array('filename' => $filename,
42 printf(_("Error opening file %s"), $filename);
45 // ------ END Initialize file-based personal addressbook ------
47 // ------ BEGIN Initialize database-based personal addressbook ------
48 // $r = $abook->add_backend('database', Array('dsn' => 'mysql://dbuser@host/dbname',
49 // 'owner' => $username,
50 // 'table' => 'address'));
51 // if(!$r && $showerr) {
52 // printf(_("Error initializing addressbook: %s"), $filename);
55 // ------ END Initialize database-based personal addressbook ------
60 // Load configured LDAP servers (if PHP has LDAP support)
61 if(isset($ldap_server) && is_array($ldap_server) &&
62 function_exists('ldap_connect')) {
64 while(list($undef,$param) = each($ldap_server)) {
65 if(is_array($param)) {
66 $r = $abook->add_backend('ldap_server', $param);
68 printf(' ' . _("Error initializing LDAP server %s:") .
69 "<BR>\n", $param['host']);
70 print(' ' . $abook->error
);
77 // Return the initialized object
82 // Had to move this function outside of the Addressbook Class
83 // PHP 4.0.4 Seemed to be having problems with inline functions.
84 function addressbook_cmp($a,$b) {
85 if($a['backend'] > $b['backend'])
87 else if($a['backend'] < $b['backend'])
89 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
94 ** This is the main address book class that connect all the
95 ** backends and provide services to the functions above.
100 var $backends = array();
101 var $numbackends = 0;
103 var $localbackend = 0;
104 var $localbackendname = '';
106 // Constructor function.
107 function AddressBook() {
108 $localbackendname = _("Personal address book");
111 // Return an array of backends of a given type,
112 // or all backends if no type is specified.
113 function get_backend_list($type = '') {
115 for($i = 1 ; $i <= $this->numbackends
; $i++
) {
116 if(empty($type) ||
$type == $this->backends
[$i]->btype
) {
117 $ret[] = &$this->backends
[$i];
124 // ========================== Public ========================
126 // Add a new backend. $backend is the name of a backend
127 // (without the abook_ prefix), and $param is an optional
128 // mixed variable that is passed to the backend constructor.
129 // See each of the backend classes for valid parameters.
130 function add_backend($backend, $param = '') {
131 $backend_name = 'abook_' . $backend;
132 eval('$newback = new ' . $backend_name . '($param);');
133 if(!empty($newback->error
)) {
134 $this->error
= $newback->error
;
138 $this->numbackends++
;
140 $newback->bnum
= $this->numbackends
;
141 $this->backends
[$this->numbackends
] = $newback;
143 // Store ID of first local backend added
144 if($this->localbackend
== 0 && $newback->btype
== 'local') {
145 $this->localbackend
= $this->numbackends
;
146 $this->localbackendname
= $newback->sname
;
149 return $this->numbackends
;
153 // Return a list of addresses matching expression in
154 // all backends of a given type.
155 function search($expression, $bnum = -1) {
159 // Search all backends
161 $sel = $this->get_backend_list('');
163 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
164 $backend = &$sel[$i];
165 $backend->error
= '';
166 $res = $backend->search($expression);
168 $ret = array_merge($ret, $res);
170 $this->error
.= "<br>\n" . $backend->error
;
175 // Only fail if all backends failed
176 if($failed >= sizeof($sel))
181 // Search only one backend
183 $ret = $this->backends
[$bnum]->search($expression);
184 if(!is_array($ret)) {
185 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
194 // Return a sorted search
195 function s_search($expression, $bnum = -1) {
197 $ret = $this->search($expression, $bnum);
200 usort($ret, 'addressbook_cmp');
205 // Lookup an address by alias. Only possible in
207 function lookup($alias, $bnum = -1) {
211 $res = $this->backends
[$bnum]->lookup($alias);
215 $this->error
= $backend->error
;
220 $sel = $this->get_backend_list('local');
221 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
222 $backend = &$sel[$i];
223 $backend->error
= '';
224 $res = $backend->lookup($alias);
229 $this->error
= $backend->error
;
238 // Return all addresses
239 function list_addr($bnum = -1) {
243 $sel = $this->get_backend_list('local');
245 $sel = array(0 => &$this->backends
[$bnum]);
247 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
248 $backend = &$sel[$i];
249 $backend->error
= '';
250 $res = $backend->list_addr();
252 $ret = array_merge($ret, $res);
254 $this->error
= $backend->error
;
263 // Create a new address from $userdata, in backend $bnum.
264 // Return the backend number that the/ address was added
265 // to, or false if it failed.
266 function add($userdata, $bnum) {
269 if(!is_array($userdata)) {
270 $this->error
= _("Invalid input data");
273 if(empty($userdata['firstname']) &&
274 empty($userdata['lastname'])) {
275 $this->error
= _("Name is missing");
278 if(empty($userdata['email'])) {
279 $this->error
= _("E-mail address is missing");
282 if(empty($userdata['nickname'])) {
283 $userdata['nickname'] = $userdata['email'];
286 if(eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
287 $this->error
= _("Nickname contain illegal characters");
291 // Check that specified backend accept new entries
292 if(!$this->backends
[$bnum]->writeable
) {
293 $this->error
= _("Addressbook is read-only");
297 // Add address to backend
298 $res = $this->backends
[$bnum]->add($userdata);
302 $this->error
= $this->backends
[$bnum]->error
;
306 return false; // Not reached
310 // Remove the user identified by $alias from backend $bnum
311 // If $alias is an array, all users in the array are removed.
312 function remove($alias, $bnum) {
318 // Convert string to single element array
319 if(!is_array($alias))
320 $alias = array(0 => $alias);
322 // Check that specified backend is writable
323 if(!$this->backends
[$bnum]->writeable
) {
324 $this->error
= _("Addressbook is read-only");
328 // Remove user from backend
329 $res = $this->backends
[$bnum]->remove($alias);
333 $this->error
= $this->backends
[$bnum]->error
;
337 return false; // Not reached
341 // Remove the user identified by $alias from backend $bnum
342 // If $alias is an array, all users in the array are removed.
343 function modify($alias, $userdata, $bnum) {
346 if(empty($alias) ||
!is_string($alias))
350 if(!is_array($userdata)) {
351 $this->error
= _("Invalid input data");
354 if(empty($userdata['firstname']) &&
355 empty($userdata['lastname'])) {
356 $this->error
= _("Name is missing");
359 if(empty($userdata['email'])) {
360 $this->error
= _("E-mail address is missing");
364 if(eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
365 $this->error
= _("Nickname contain illegal characters");
369 if(empty($userdata['nickname'])) {
370 $userdata['nickname'] = $userdata['email'];
373 // Check that specified backend is writable
374 if(!$this->backends
[$bnum]->writeable
) {
375 $this->error
= _("Addressbook is read-only");;
379 // Modify user in backend
380 $res = $this->backends
[$bnum]->modify($alias, $userdata);
384 $this->error
= $this->backends
[$bnum]->error
;
388 return false; // Not reached
392 } // End of class Addressbook
395 ** Generic backend that all other backends extend
397 class addressbook_backend
{
399 // Variables that all backends must provide.
400 var $btype = 'dummy';
401 var $bname = 'dummy';
402 var $sname = 'Dummy backend';
404 // Variables common for all backends, but that
405 // should not be changed by the backends.
408 var $writeable = false;
410 function set_error($string) {
411 $this->error
= '[' . $this->sname
. '] ' . $string;
416 // ========================== Public ========================
418 function search($expression) {
419 $this->set_error('search not implemented');
423 function lookup($alias) {
424 $this->set_error('lookup not implemented');
428 function list_addr() {
429 $this->set_error('list_addr not implemented');
433 function add($userdata) {
434 $this->set_error('add not implemented');
438 function remove($alias) {
439 $this->set_error('delete not implemented');
443 function modify($alias, $newuserdata) {
444 $this->set_error('modify not implemented');