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