6 ** Functions and classes for the addressbook system.
11 if (defined ('addressbook_php'))
13 define ('addressbook_php', true);
15 // This is the path to the global site-wide addressbook.
16 // It looks and feels just like a user's .abook file
17 // If this is in the data directory, use "$data_dir/global.abook"
18 // If not, specify the path as though it was accessed from the
19 // src/ directory ("../global.abook" -> in main directory)
21 // If you don't want a global site-wide addressbook, comment these
22 // two lines out. (They are disabled by default.)
24 // The global addressbook is unmodifiable by anyone. You must actually
25 // use a shell script or whatnot to modify the contents.
28 //$address_book_global_filename = "$data_dir/global.abook";
32 // Include backends here.
33 require_once('../functions/abook_local_file.php');
34 require_once('../functions/abook_ldap_server.php');
36 // Use this if you wanna have a global address book
37 if (isset($address_book_global_filename))
38 include_once('../functions/abook_global_file.php');
40 // Only load database backend if database is configured
42 if(isset($addrbook_dsn))
43 include_once('../functions/abook_database.php');
46 // Create and initialize an addressbook object.
47 // Returns the created object
48 function addressbook_init($showerr = true, $onlylocal = false) {
49 global $data_dir, $username, $ldap_server, $address_book_global_filename;
52 // Create a new addressbook object
53 $abook = new AddressBook
;
55 // Always add a local backend. We use *either* file-based *or* a
56 // database addressbook. If $addrbook_dsn is set, the database
57 // backend is used. If not, addressbooks are stores in files.
58 if(isset($addrbook_dsn) && !empty($addrbook_dsn)) {
60 $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
62 'table' => 'address'));
64 printf(_("Error initializing addressbook database."));
69 $filename = sprintf('%s%s.abook', $data_dir, $username);
70 $r = $abook->add_backend('local_file', Array('filename' => $filename,
73 printf(_("Error opening file %s"), $filename);
79 // This would be for the global addressbook
80 if (isset($address_book_global_filename)) {
81 $r = $abook->add_backend('global_file');
82 if (!$r && $showerr) {
83 printf(_("Error initializing global addressbook."));
91 // Load configured LDAP servers (if PHP has LDAP support)
92 if(isset($ldap_server) && is_array($ldap_server) &&
93 function_exists('ldap_connect')) {
95 while(list($undef,$param) = each($ldap_server)) {
96 if(is_array($param)) {
97 $r = $abook->add_backend('ldap_server', $param);
99 printf(' ' . _("Error initializing LDAP server %s:") .
100 "<BR>\n", $param['host']);
101 print(' ' . $abook->error
);
108 // Return the initialized object
113 // Had to move this function outside of the Addressbook Class
114 // PHP 4.0.4 Seemed to be having problems with inline functions.
115 function addressbook_cmp($a,$b) {
116 if($a['backend'] > $b['backend'])
118 else if($a['backend'] < $b['backend'])
120 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
125 ** This is the main address book class that connect all the
126 ** backends and provide services to the functions above.
131 var $backends = array();
132 var $numbackends = 0;
134 var $localbackend = 0;
135 var $localbackendname = '';
137 // Constructor function.
138 function AddressBook() {
139 $localbackendname = _("Personal address book");
142 // Return an array of backends of a given type,
143 // or all backends if no type is specified.
144 function get_backend_list($type = '') {
146 for($i = 1 ; $i <= $this->numbackends
; $i++
) {
147 if(empty($type) ||
$type == $this->backends
[$i]->btype
) {
148 $ret[] = &$this->backends
[$i];
155 // ========================== Public ========================
157 // Add a new backend. $backend is the name of a backend
158 // (without the abook_ prefix), and $param is an optional
159 // mixed variable that is passed to the backend constructor.
160 // See each of the backend classes for valid parameters.
161 function add_backend($backend, $param = '') {
162 $backend_name = 'abook_' . $backend;
163 eval('$newback = new ' . $backend_name . '($param);');
164 if(!empty($newback->error
)) {
165 $this->error
= $newback->error
;
169 $this->numbackends++
;
171 $newback->bnum
= $this->numbackends
;
172 $this->backends
[$this->numbackends
] = $newback;
174 // Store ID of first local backend added
175 if($this->localbackend
== 0 && $newback->btype
== 'local') {
176 $this->localbackend
= $this->numbackends
;
177 $this->localbackendname
= $newback->sname
;
180 return $this->numbackends
;
184 // Return a list of addresses matching expression in
185 // all backends of a given type.
186 function search($expression, $bnum = -1) {
190 // Search all backends
192 $sel = $this->get_backend_list('');
194 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
195 $backend = &$sel[$i];
196 $backend->error
= '';
197 $res = $backend->search($expression);
199 $ret = array_merge($ret, $res);
201 $this->error
.= "<br>\n" . $backend->error
;
206 // Only fail if all backends failed
207 if($failed >= sizeof($sel))
212 // Search only one backend
214 $ret = $this->backends
[$bnum]->search($expression);
215 if(!is_array($ret)) {
216 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
225 // Return a sorted search
226 function s_search($expression, $bnum = -1) {
228 $ret = $this->search($expression, $bnum);
231 usort($ret, 'addressbook_cmp');
236 // Lookup an address by alias. Only possible in
238 function lookup($alias, $bnum = -1) {
242 $res = $this->backends
[$bnum]->lookup($alias);
246 $this->error
= $backend->error
;
251 $sel = $this->get_backend_list('local');
252 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
253 $backend = &$sel[$i];
254 $backend->error
= '';
255 $res = $backend->lookup($alias);
260 $this->error
= $backend->error
;
269 // Return all addresses
270 function list_addr($bnum = -1) {
274 $sel = $this->get_backend_list('local');
276 $sel = array(0 => &$this->backends
[$bnum]);
278 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
279 $backend = &$sel[$i];
280 $backend->error
= '';
281 $res = $backend->list_addr();
283 $ret = array_merge($ret, $res);
285 $this->error
= $backend->error
;
294 // Create a new address from $userdata, in backend $bnum.
295 // Return the backend number that the/ address was added
296 // to, or false if it failed.
297 function add($userdata, $bnum) {
300 if(!is_array($userdata)) {
301 $this->error
= _("Invalid input data");
304 if(empty($userdata['firstname']) &&
305 empty($userdata['lastname'])) {
306 $this->error
= _("Name is missing");
309 if(empty($userdata['email'])) {
310 $this->error
= _("E-mail address is missing");
313 if(empty($userdata['nickname'])) {
314 $userdata['nickname'] = $userdata['email'];
317 if(eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
318 $this->error
= _("Nickname contain illegal characters");
322 // Check that specified backend accept new entries
323 if(!$this->backends
[$bnum]->writeable
) {
324 $this->error
= _("Addressbook is read-only");
328 // Add address to backend
329 $res = $this->backends
[$bnum]->add($userdata);
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 remove($alias, $bnum) {
349 // Convert string to single element array
350 if(!is_array($alias))
351 $alias = array(0 => $alias);
353 // Check that specified backend is writable
354 if(!$this->backends
[$bnum]->writeable
) {
355 $this->error
= _("Addressbook is read-only");
359 // Remove user from backend
360 $res = $this->backends
[$bnum]->remove($alias);
364 $this->error
= $this->backends
[$bnum]->error
;
368 return false; // Not reached
372 // Remove the user identified by $alias from backend $bnum
373 // If $alias is an array, all users in the array are removed.
374 function modify($alias, $userdata, $bnum) {
377 if(empty($alias) ||
!is_string($alias))
381 if(!is_array($userdata)) {
382 $this->error
= _("Invalid input data");
385 if(empty($userdata['firstname']) &&
386 empty($userdata['lastname'])) {
387 $this->error
= _("Name is missing");
390 if(empty($userdata['email'])) {
391 $this->error
= _("E-mail address is missing");
395 if(eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
396 $this->error
= _("Nickname contain illegal characters");
400 if(empty($userdata['nickname'])) {
401 $userdata['nickname'] = $userdata['email'];
404 // Check that specified backend is writable
405 if(!$this->backends
[$bnum]->writeable
) {
406 $this->error
= _("Addressbook is read-only");;
410 // Modify user in backend
411 $res = $this->backends
[$bnum]->modify($alias, $userdata);
415 $this->error
= $this->backends
[$bnum]->error
;
419 return false; // Not reached
423 } // End of class Addressbook
426 ** Generic backend that all other backends extend
428 class addressbook_backend
{
430 // Variables that all backends must provide.
431 var $btype = 'dummy';
432 var $bname = 'dummy';
433 var $sname = 'Dummy backend';
435 // Variables common for all backends, but that
436 // should not be changed by the backends.
439 var $writeable = false;
441 function set_error($string) {
442 $this->error
= '[' . $this->sname
. '] ' . $string;
447 // ========================== Public ========================
449 function search($expression) {
450 $this->set_error('search not implemented');
454 function lookup($alias) {
455 $this->set_error('lookup not implemented');
459 function list_addr() {
460 $this->set_error('list_addr not implemented');
464 function add($userdata) {
465 $this->set_error('add not implemented');
469 function remove($alias) {
470 $this->set_error('delete not implemented');
474 function modify($alias, $newuserdata) {
475 $this->set_error('modify not implemented');