PHP 5 fix, cure for the error about undeclared extension of class. This
[squirrelmail.git] / functions / addressbook.php
CommitLineData
5100704d 1<?php
2
35586184 3/**
4 * addressbook.php
5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
81fa4801 9 * Functions and classes for the addressbook system.
35586184 10 *
11 * $Id$
12 */
13
81fa4801 14/*
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)
20
21 If you don't want a global site-wide addressbook, comment these
22 two lines out. (They are disabled by default.)
23
24 The global addressbook is unmodifiable by anyone. You must actually
25 use a shell script or whatnot to modify the contents.
26
91e51743 27 global $data_dir, $address_book_global_filename;
81fa4801 28 $address_book_global_filename = "$data_dir/global.abook";
29
81fa4801 30*/
31
81fa4801 32global $addrbook_dsn;
33
81fa4801 34/*
35 Create and initialize an addressbook object.
36 Returns the created object
37*/
38function addressbook_init($showerr = true, $onlylocal = false) {
39 global $data_dir, $username, $ldap_server, $address_book_global_filename;
40 global $addrbook_dsn, $addrbook_table;
41
42 /* Create a new addressbook object */
43 $abook = new AddressBook;
44
45 /*
46 Always add a local backend. We use *either* file-based *or* a
47 database addressbook. If $addrbook_dsn is set, the database
48 backend is used. If not, addressbooks are stores in files.
49 */
50 if (isset($addrbook_dsn) && !empty($addrbook_dsn)) {
51 /* Database */
52 if (!isset($addrbook_table) || empty($addrbook_table)) {
53 $addrbook_table = 'address';
54 }
55 $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
56 'owner' => $username,
57 'table' => $addrbook_table));
58 if (!$r && $showerr) {
59 echo _("Error initializing addressbook database.");
60 exit;
61 }
62 } else {
63 /* File */
64 $filename = getHashedFile($username, $data_dir, "$username.abook");
65 $r = $abook->add_backend('local_file', Array('filename' => $filename,
66 'create' => true));
67 if(!$r && $showerr) {
68 printf( _("Error opening file %s"), $filename );
69 exit;
70 }
71
72 }
73
74 /* This would be for the global addressbook */
75 if (isset($address_book_global_filename)) {
76 $r = $abook->add_backend('global_file');
77 if (!$r && $showerr) {
78 echo _("Error initializing global addressbook.");
79 exit;
80 }
81 }
82
83 if ($onlylocal) {
84 return $abook;
85 }
86
87 /* Load configured LDAP servers (if PHP has LDAP support) */
88 if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) {
89 reset($ldap_server);
90 while (list($undef,$param) = each($ldap_server)) {
91 if (is_array($param)) {
92 $r = $abook->add_backend('ldap_server', $param);
93 if (!$r && $showerr) {
94 printf( '&nbsp;' . _("Error initializing LDAP server %s:") .
95 "<BR>\n", $param['host']);
96 echo '&nbsp;' . $abook->error;
97 exit;
98 }
99 }
100 }
101 }
4935919f 102
81fa4801 103 /* Return the initialized object */
104 return $abook;
4935919f 105}
106
107
81fa4801 108/*
109 * Had to move this function outside of the Addressbook Class
110 * PHP 4.0.4 Seemed to be having problems with inline functions.
111 */
112function addressbook_cmp($a,$b) {
4935919f 113
81fa4801 114 if($a['backend'] > $b['backend']) {
115 return 1;
116 } else if($a['backend'] < $b['backend']) {
117 return -1;
118 }
119
120 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
4935919f 121
81fa4801 122}
4935919f 123
124
81fa4801 125/*
126 * This is the main address book class that connect all the
127 * backends and provide services to the functions above.
128 *
129 */
5100704d 130
81fa4801 131class AddressBook {
4935919f 132
81fa4801 133 var $backends = array();
134 var $numbackends = 0;
135 var $error = '';
136 var $localbackend = 0;
137 var $localbackendname = '';
138
139 // Constructor function.
140 function AddressBook() {
141 $localbackendname = _("Personal address book");
142 }
4935919f 143
81fa4801 144 /*
145 * Return an array of backends of a given type,
146 * or all backends if no type is specified.
147 */
148 function get_backend_list($type = '') {
149 $ret = array();
150 for ($i = 1 ; $i <= $this->numbackends ; $i++) {
151 if (empty($type) || $type == $this->backends[$i]->btype) {
152 $ret[] = &$this->backends[$i];
153 }
4935919f 154 }
81fa4801 155 return $ret;
156 }
4935919f 157
158
81fa4801 159 /*
160 ========================== Public ========================
161
162 Add a new backend. $backend is the name of a backend
163 (without the abook_ prefix), and $param is an optional
164 mixed variable that is passed to the backend constructor.
165 See each of the backend classes for valid parameters.
166 */
167 function add_backend($backend, $param = '') {
168 $backend_name = 'abook_' . $backend;
169 eval('$newback = new ' . $backend_name . '($param);');
170 if(!empty($newback->error)) {
171 $this->error = $newback->error;
172 return false;
173 }
174
175 $this->numbackends++;
176
177 $newback->bnum = $this->numbackends;
178 $this->backends[$this->numbackends] = $newback;
179
180 /* Store ID of first local backend added */
181 if ($this->localbackend == 0 && $newback->btype == 'local') {
182 $this->localbackend = $this->numbackends;
183 $this->localbackendname = $newback->sname;
184 }
185
186 return $this->numbackends;
187 }
4935919f 188
4935919f 189
2e542990 190 /*
191 * This function takes a $row array as returned by the addressbook
192 * search and returns an e-mail address with the full name or
193 * nickname optionally prepended.
194 */
195
196 function full_address($row) {
1ba8cd6b 197 global $addrsrch_fullname, $data_dir, $username;
2e542990 198
1ba8cd6b 199 if (($prefix = getPref($data_dir, $username, 'addrsrch_fullname') or
e7c6d43c 200 isset($addrsrch_fullname) and $prefix = $addrsrch_fullname)
201 and $prefix !== 'noprefix') {
b9d210e3 202 $name = ($prefix === 'nickname') ? $row['nickname']
2e542990 203 : $row['name'];
204 return $name . ' <' . trim($row['email']) . '>';
205 } else {
206 return trim($row['email']);
207 }
208 }
209
81fa4801 210 /*
211 Return a list of addresses matching expression in
212 all backends of a given type.
213 */
214 function search($expression, $bnum = -1) {
215 $ret = array();
216 $this->error = '';
217
218 /* Search all backends */
219 if ($bnum == -1) {
220 $sel = $this->get_backend_list('');
221 $failed = 0;
222 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
223 $backend = &$sel[$i];
224 $backend->error = '';
225 $res = $backend->search($expression);
226 if (is_array($res)) {
227 $ret = array_merge($ret, $res);
228 } else {
229 $this->error .= "<br>\n" . $backend->error;
230 $failed++;
75e19c7f 231 }
232 }
4935919f 233
81fa4801 234 /* Only fail if all backends failed */
235 if( $failed >= sizeof( $sel ) ) {
236 $ret = FALSE;
4935919f 237 }
4935919f 238
81fa4801 239 } else {
4935919f 240
81fa4801 241 /* Search only one backend */
4935919f 242
81fa4801 243 $ret = $this->backends[$bnum]->search($expression);
244 if (!is_array($ret)) {
245 $this->error .= "<br>\n" . $this->backends[$bnum]->error;
246 $ret = FALSE;
247 }
248 }
249
250 return( $ret );
4935919f 251 }
252
253
81fa4801 254 /* Return a sorted search */
255 function s_search($expression, $bnum = -1) {
256
257 $ret = $this->search($expression, $bnum);
258 if ( is_array( $ret ) ) {
259 usort($ret, 'addressbook_cmp');
260 }
261 return $ret;
262 }
4935919f 263
264
81fa4801 265 /*
266 * Lookup an address by alias. Only possible in
267 * local backends.
268 */
269 function lookup($alias, $bnum = -1) {
270
271 $ret = array();
272
273 if ($bnum > -1) {
274 $res = $this->backends[$bnum]->lookup($alias);
275 if (is_array($res)) {
276 return $res;
277 } else {
278 $this->error = $backend->error;
279 return false;
280 }
281 }
282
283 $sel = $this->get_backend_list('local');
284 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
285 $backend = &$sel[$i];
286 $backend->error = '';
287 $res = $backend->lookup($alias);
288 if (is_array($res)) {
289 if(!empty($res))
290 return $res;
291 } else {
292 $this->error = $backend->error;
293 return false;
294 }
295 }
296
297 return $ret;
4935919f 298 }
299
4935919f 300
81fa4801 301 /* Return all addresses */
302 function list_addr($bnum = -1) {
303 $ret = array();
304
305 if ($bnum == -1) {
306 $sel = $this->get_backend_list('local');
307 } else {
308 $sel = array(0 => &$this->backends[$bnum]);
309 }
310
311 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
312 $backend = &$sel[$i];
313 $backend->error = '';
314 $res = $backend->list_addr();
315 if (is_array($res)) {
316 $ret = array_merge($ret, $res);
317 } else {
318 $this->error = $backend->error;
319 return false;
320 }
321 }
322
323 return $ret;
324 }
4935919f 325
81fa4801 326 /*
327 * Create a new address from $userdata, in backend $bnum.
328 * Return the backend number that the/ address was added
329 * to, or false if it failed.
330 */
331 function add($userdata, $bnum) {
4935919f 332
81fa4801 333 /* Validate data */
334 if (!is_array($userdata)) {
335 $this->error = _("Invalid input data");
336 return false;
337 }
338 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
339 $this->error = _("Name is missing");
340 return false;
341 }
342 if (empty($userdata['email'])) {
343 $this->error = _("E-mail address is missing");
344 return false;
345 }
346 if (empty($userdata['nickname'])) {
347 $userdata['nickname'] = $userdata['email'];
348 }
349
350 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
351 $this->error = _("Nickname contains illegal characters");
352 return false;
353 }
354
355 /* Check that specified backend accept new entries */
356 if (!$this->backends[$bnum]->writeable) {
357 $this->error = _("Addressbook is read-only");
358 return false;
359 }
360
361 /* Add address to backend */
362 $res = $this->backends[$bnum]->add($userdata);
363 if ($res) {
364 return $bnum;
365 } else {
366 $this->error = $this->backends[$bnum]->error;
367 return false;
368 }
369
370 return false; // Not reached
371 } /* end of add() */
372
373
374 /*
375 * Remove the user identified by $alias from backend $bnum
376 * If $alias is an array, all users in the array are removed.
377 */
378 function remove($alias, $bnum) {
379
380 /* Check input */
381 if (empty($alias)) {
382 return true;
383 }
384
385 /* Convert string to single element array */
386 if (!is_array($alias)) {
387 $alias = array(0 => $alias);
388 }
389
390 /* Check that specified backend is writable */
391 if (!$this->backends[$bnum]->writeable) {
392 $this->error = _("Addressbook is read-only");
393 return false;
394 }
395
396 /* Remove user from backend */
397 $res = $this->backends[$bnum]->remove($alias);
398 if ($res) {
399 return $bnum;
400 } else {
401 $this->error = $this->backends[$bnum]->error;
402 return false;
403 }
404
405 return FALSE; /* Not reached */
406 } /* end of remove() */
407
408
409 /*
410 * Remove the user identified by $alias from backend $bnum
411 * If $alias is an array, all users in the array are removed.
412 */
413 function modify($alias, $userdata, $bnum) {
414
415 /* Check input */
416 if (empty($alias) || !is_string($alias)) {
417 return true;
418 }
419
420 /* Validate data */
421 if(!is_array($userdata)) {
422 $this->error = _("Invalid input data");
423 return false;
424 }
425 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
426 $this->error = _("Name is missing");
427 return false;
428 }
429 if (empty($userdata['email'])) {
430 $this->error = _("E-mail address is missing");
431 return false;
432 }
433
434 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
435 $this->error = _("Nickname contains illegal characters");
436 return false;
437 }
438
439 if (empty($userdata['nickname'])) {
440 $userdata['nickname'] = $userdata['email'];
441 }
442
443 /* Check that specified backend is writable */
444 if (!$this->backends[$bnum]->writeable) {
445 $this->error = _("Addressbook is read-only");;
446 return false;
447 }
448
449 /* Modify user in backend */
450 $res = $this->backends[$bnum]->modify($alias, $userdata);
451 if ($res) {
452 return $bnum;
453 } else {
454 $this->error = $this->backends[$bnum]->error;
455 return false;
456 }
457
458 return FALSE; /* Not reached */
459 } /* end of modify() */
4935919f 460
4935919f 461
81fa4801 462} /* End of class Addressbook */
463
464/*
465 * Generic backend that all other backends extend
466 */
467class addressbook_backend {
468
469 /* Variables that all backends must provide. */
470 var $btype = 'dummy';
471 var $bname = 'dummy';
472 var $sname = 'Dummy backend';
4935919f 473
81fa4801 474 /*
475 * Variables common for all backends, but that
476 * should not be changed by the backends.
477 */
478 var $bnum = -1;
479 var $error = '';
480 var $writeable = false;
4935919f 481
81fa4801 482 function set_error($string) {
483 $this->error = '[' . $this->sname . '] ' . $string;
484 return false;
485 }
4935919f 486
75e19c7f 487
81fa4801 488 /* ========================== Public ======================== */
489
490 function search($expression) {
491 $this->set_error('search not implemented');
492 return false;
493 }
494
495 function lookup($alias) {
496 $this->set_error('lookup not implemented');
497 return false;
a10110a5 498 }
81fa4801 499
500 function list_addr() {
501 $this->set_error('list_addr not implemented');
502 return false;
503 }
504
505 function add($userdata) {
506 $this->set_error('add not implemented');
507 return false;
508 }
509
510 function remove($alias) {
511 $this->set_error('delete not implemented');
512 return false;
513 }
514
515 function modify($alias, $newuserdata) {
516 $this->set_error('modify not implemented');
517 return false;
518 }
519
520}
521
522/* Sort array by the key "name" */
523function alistcmp($a,$b) {
524 if ($a['backend'] > $b['backend']) {
525 return 1;
526 } else {
527 if ($a['backend'] < $b['backend']) {
528 return -1;
529 }
530 }
531 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
532}
533
0419106e 534
535/*
536 PHP 5 requires that the class be made first, which seems rather
537 logical, and should have been the way it was generated the first time.
538*/
539
540require_once(SM_PATH . 'functions/abook_local_file.php');
541require_once(SM_PATH . 'functions/abook_ldap_server.php');
542
543/* Use this if you wanna have a global address book */
544if (isset($address_book_global_filename)) {
545 include_once(SM_PATH . 'functions/abook_global_file.php');
546}
547
548/* Only load database backend if database is configured */
549if(isset($addrbook_dsn) && !empty($addrbook_dsn)) {
550 include_once(SM_PATH . 'functions/abook_database.php');
551}
552
553
2e542990 554?>