0fd0451bbe283eb0e1c8788961d942b08416b5a2
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 // Only load database backend if database is configured
20 if(isset($addrbook_dsn))
21 include('../functions/abook_database.php');
24 // Create and initialize an addressbook object.
25 // Returns the created object
26 function addressbook_init($showerr = true, $onlylocal = false) {
27 global $data_dir, $username, $ldap_server;
30 // Create a new addressbook object
31 $abook = new AddressBook
;
33 // Always add a local backend. We use *either* file-based *or* a
34 // database addressbook. If $addrbook_dsn is set, the database
35 // backend is used. If not, addressbooks are stores in files.
36 if(isset($addrbook_dsn) && !empty($addrbook_dsn)) {
38 $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
40 'table' => 'address'));
42 printf(_("Error initializing addressbook database."));
47 $filename = sprintf('%s%s.abook', $data_dir, $username);
48 $r = $abook->add_backend('local_file', Array('filename' => $filename,
51 printf(_("Error opening file %s"), $filename);
59 // Load configured LDAP servers (if PHP has LDAP support)
60 if(isset($ldap_server) && is_array($ldap_server) &&
61 function_exists('ldap_connect')) {
63 while(list($undef,$param) = each($ldap_server)) {
64 if(is_array($param)) {
65 $r = $abook->add_backend('ldap_server', $param);
67 printf(' ' . _("Error initializing LDAP server %s:") .
68 "<BR>\n", $param['host']);
69 print(' ' . $abook->error
);
76 // Return the initialized object
81 // Had to move this function outside of the Addressbook Class
82 // PHP 4.0.4 Seemed to be having problems with inline functions.
83 function addressbook_cmp($a,$b) {
84 if($a['backend'] > $b['backend'])
86 else if($a['backend'] < $b['backend'])
88 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
93 ** This is the main address book class that connect all the
94 ** backends and provide services to the functions above.
99 var $backends = array();
100 var $numbackends = 0;
102 var $localbackend = 0;
103 var $localbackendname = '';
105 // Constructor function.
106 function AddressBook() {
107 $localbackendname = _("Personal address book");
110 // Return an array of backends of a given type,
111 // or all backends if no type is specified.
112 function get_backend_list($type = '') {
114 for($i = 1 ; $i <= $this->numbackends
; $i++
) {
115 if(empty($type) ||
$type == $this->backends
[$i]->btype
) {
116 $ret[] = &$this->backends
[$i];
123 // ========================== Public ========================
125 // Add a new backend. $backend is the name of a backend
126 // (without the abook_ prefix), and $param is an optional
127 // mixed variable that is passed to the backend constructor.
128 // See each of the backend classes for valid parameters.
129 function add_backend($backend, $param = '') {
130 $backend_name = 'abook_' . $backend;
131 eval('$newback = new ' . $backend_name . '($param);');
132 if(!empty($newback->error
)) {
133 $this->error
= $newback->error
;
137 $this->numbackends++
;
139 $newback->bnum
= $this->numbackends
;
140 $this->backends
[$this->numbackends
] = $newback;
142 // Store ID of first local backend added
143 if($this->localbackend
== 0 && $newback->btype
== 'local') {
144 $this->localbackend
= $this->numbackends
;
145 $this->localbackendname
= $newback->sname
;
148 return $this->numbackends
;
152 // Return a list of addresses matching expression in
153 // all backends of a given type.
154 function search($expression, $bnum = -1) {
158 // Search all backends
160 $sel = $this->get_backend_list('');
162 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
163 $backend = &$sel[$i];
164 $backend->error
= '';
165 $res = $backend->search($expression);
167 $ret = array_merge($ret, $res);
169 $this->error
.= "<br>\n" . $backend->error
;
174 // Only fail if all backends failed
175 if($failed >= sizeof($sel))
180 // Search only one backend
182 $ret = $this->backends
[$bnum]->search($expression);
183 if(!is_array($ret)) {
184 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
193 // Return a sorted search
194 function s_search($expression, $bnum = -1) {
196 $ret = $this->search($expression, $bnum);
199 usort($ret, 'addressbook_cmp');
204 // Lookup an address by alias. Only possible in
206 function lookup($alias, $bnum = -1) {
210 $res = $this->backends
[$bnum]->lookup($alias);
214 $this->error
= $backend->error
;
219 $sel = $this->get_backend_list('local');
220 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
221 $backend = &$sel[$i];
222 $backend->error
= '';
223 $res = $backend->lookup($alias);
228 $this->error
= $backend->error
;
237 // Return all addresses
238 function list_addr($bnum = -1) {
242 $sel = $this->get_backend_list('local');
244 $sel = array(0 => &$this->backends
[$bnum]);
246 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
247 $backend = &$sel[$i];
248 $backend->error
= '';
249 $res = $backend->list_addr();
251 $ret = array_merge($ret, $res);
253 $this->error
= $backend->error
;
262 // Create a new address from $userdata, in backend $bnum.
263 // Return the backend number that the/ address was added
264 // to, or false if it failed.
265 function add($userdata, $bnum) {
268 if(!is_array($userdata)) {
269 $this->error
= _("Invalid input data");
272 if(empty($userdata['firstname']) &&
273 empty($userdata['lastname'])) {
274 $this->error
= _("Name is missing");
277 if(empty($userdata['email'])) {
278 $this->error
= _("E-mail address is missing");
281 if(empty($userdata['nickname'])) {
282 $userdata['nickname'] = $userdata['email'];
285 if(eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
286 $this->error
= _("Nickname contain illegal characters");
290 // Check that specified backend accept new entries
291 if(!$this->backends
[$bnum]->writeable
) {
292 $this->error
= _("Addressbook is read-only");
296 // Add address to backend
297 $res = $this->backends
[$bnum]->add($userdata);
301 $this->error
= $this->backends
[$bnum]->error
;
305 return false; // Not reached
309 // Remove the user identified by $alias from backend $bnum
310 // If $alias is an array, all users in the array are removed.
311 function remove($alias, $bnum) {
317 // Convert string to single element array
318 if(!is_array($alias))
319 $alias = array(0 => $alias);
321 // Check that specified backend is writable
322 if(!$this->backends
[$bnum]->writeable
) {
323 $this->error
= _("Addressbook is read-only");
327 // Remove user from backend
328 $res = $this->backends
[$bnum]->remove($alias);
332 $this->error
= $this->backends
[$bnum]->error
;
336 return false; // Not reached
340 // Remove the user identified by $alias from backend $bnum
341 // If $alias is an array, all users in the array are removed.
342 function modify($alias, $userdata, $bnum) {
345 if(empty($alias) ||
!is_string($alias))
349 if(!is_array($userdata)) {
350 $this->error
= _("Invalid input data");
353 if(empty($userdata['firstname']) &&
354 empty($userdata['lastname'])) {
355 $this->error
= _("Name is missing");
358 if(empty($userdata['email'])) {
359 $this->error
= _("E-mail address is missing");
363 if(eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
364 $this->error
= _("Nickname contain illegal characters");
368 if(empty($userdata['nickname'])) {
369 $userdata['nickname'] = $userdata['email'];
372 // Check that specified backend is writable
373 if(!$this->backends
[$bnum]->writeable
) {
374 $this->error
= _("Addressbook is read-only");;
378 // Modify user in backend
379 $res = $this->backends
[$bnum]->modify($alias, $userdata);
383 $this->error
= $this->backends
[$bnum]->error
;
387 return false; // Not reached
391 } // End of class Addressbook
394 ** Generic backend that all other backends extend
396 class addressbook_backend
{
398 // Variables that all backends must provide.
399 var $btype = 'dummy';
400 var $bname = 'dummy';
401 var $sname = 'Dummy backend';
403 // Variables common for all backends, but that
404 // should not be changed by the backends.
407 var $writeable = false;
409 function set_error($string) {
410 $this->error
= '[' . $this->sname
. '] ' . $string;
415 // ========================== Public ========================
417 function search($expression) {
418 $this->set_error('search not implemented');
422 function lookup($alias) {
423 $this->set_error('lookup not implemented');
427 function list_addr() {
428 $this->set_error('list_addr not implemented');
432 function add($userdata) {
433 $this->set_error('add not implemented');
437 function remove($alias) {
438 $this->set_error('delete not implemented');
442 function modify($alias, $newuserdata) {
443 $this->set_error('modify not implemented');