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