More SM_PATH changes
[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(SM_PATH . 'functions/abook_local_file.php');
34 require_once(SM_PATH . '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(SM_PATH . '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(SM_PATH . '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 and $prefix !== 'noprefix') {
216 $name = ($prefix === 'nickname') ? $row['nickname']
217 : $row['name'];
218 return $name . ' <' . trim($row['email']) . '>';
219 } else {
220 return trim($row['email']);
221 }
222 }
223
224 /*
225 Return a list of addresses matching expression in
226 all backends of a given type.
227 */
228 function search($expression, $bnum = -1) {
229 $ret = array();
230 $this->error = '';
231
232 /* Search all backends */
233 if ($bnum == -1) {
234 $sel = $this->get_backend_list('');
235 $failed = 0;
236 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
237 $backend = &$sel[$i];
238 $backend->error = '';
239 $res = $backend->search($expression);
240 if (is_array($res)) {
241 $ret = array_merge($ret, $res);
242 } else {
243 $this->error .= "<br>\n" . $backend->error;
244 $failed++;
245 }
246 }
247
248 /* Only fail if all backends failed */
249 if( $failed >= sizeof( $sel ) ) {
250 $ret = FALSE;
251 }
252
253 } else {
254
255 /* Search only one backend */
256
257 $ret = $this->backends[$bnum]->search($expression);
258 if (!is_array($ret)) {
259 $this->error .= "<br>\n" . $this->backends[$bnum]->error;
260 $ret = FALSE;
261 }
262 }
263
264 return( $ret );
265 }
266
267
268 /* Return a sorted search */
269 function s_search($expression, $bnum = -1) {
270
271 $ret = $this->search($expression, $bnum);
272 if ( is_array( $ret ) ) {
273 usort($ret, 'addressbook_cmp');
274 }
275 return $ret;
276 }
277
278
279 /*
280 * Lookup an address by alias. Only possible in
281 * local backends.
282 */
283 function lookup($alias, $bnum = -1) {
284
285 $ret = array();
286
287 if ($bnum > -1) {
288 $res = $this->backends[$bnum]->lookup($alias);
289 if (is_array($res)) {
290 return $res;
291 } else {
292 $this->error = $backend->error;
293 return false;
294 }
295 }
296
297 $sel = $this->get_backend_list('local');
298 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
299 $backend = &$sel[$i];
300 $backend->error = '';
301 $res = $backend->lookup($alias);
302 if (is_array($res)) {
303 if(!empty($res))
304 return $res;
305 } else {
306 $this->error = $backend->error;
307 return false;
308 }
309 }
310
311 return $ret;
312 }
313
314
315 /* Return all addresses */
316 function list_addr($bnum = -1) {
317 $ret = array();
318
319 if ($bnum == -1) {
320 $sel = $this->get_backend_list('local');
321 } else {
322 $sel = array(0 => &$this->backends[$bnum]);
323 }
324
325 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
326 $backend = &$sel[$i];
327 $backend->error = '';
328 $res = $backend->list_addr();
329 if (is_array($res)) {
330 $ret = array_merge($ret, $res);
331 } else {
332 $this->error = $backend->error;
333 return false;
334 }
335 }
336
337 return $ret;
338 }
339
340 /*
341 * Create a new address from $userdata, in backend $bnum.
342 * Return the backend number that the/ address was added
343 * to, or false if it failed.
344 */
345 function add($userdata, $bnum) {
346
347 /* Validate data */
348 if (!is_array($userdata)) {
349 $this->error = _("Invalid input data");
350 return false;
351 }
352 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
353 $this->error = _("Name is missing");
354 return false;
355 }
356 if (empty($userdata['email'])) {
357 $this->error = _("E-mail address is missing");
358 return false;
359 }
360 if (empty($userdata['nickname'])) {
361 $userdata['nickname'] = $userdata['email'];
362 }
363
364 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
365 $this->error = _("Nickname contains illegal characters");
366 return false;
367 }
368
369 /* Check that specified backend accept new entries */
370 if (!$this->backends[$bnum]->writeable) {
371 $this->error = _("Addressbook is read-only");
372 return false;
373 }
374
375 /* Add address to backend */
376 $res = $this->backends[$bnum]->add($userdata);
377 if ($res) {
378 return $bnum;
379 } else {
380 $this->error = $this->backends[$bnum]->error;
381 return false;
382 }
383
384 return false; // Not reached
385 } /* end of add() */
386
387
388 /*
389 * Remove the user identified by $alias from backend $bnum
390 * If $alias is an array, all users in the array are removed.
391 */
392 function remove($alias, $bnum) {
393
394 /* Check input */
395 if (empty($alias)) {
396 return true;
397 }
398
399 /* Convert string to single element array */
400 if (!is_array($alias)) {
401 $alias = array(0 => $alias);
402 }
403
404 /* Check that specified backend is writable */
405 if (!$this->backends[$bnum]->writeable) {
406 $this->error = _("Addressbook is read-only");
407 return false;
408 }
409
410 /* Remove user from backend */
411 $res = $this->backends[$bnum]->remove($alias);
412 if ($res) {
413 return $bnum;
414 } else {
415 $this->error = $this->backends[$bnum]->error;
416 return false;
417 }
418
419 return FALSE; /* Not reached */
420 } /* end of remove() */
421
422
423 /*
424 * Remove the user identified by $alias from backend $bnum
425 * If $alias is an array, all users in the array are removed.
426 */
427 function modify($alias, $userdata, $bnum) {
428
429 /* Check input */
430 if (empty($alias) || !is_string($alias)) {
431 return true;
432 }
433
434 /* Validate data */
435 if(!is_array($userdata)) {
436 $this->error = _("Invalid input data");
437 return false;
438 }
439 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
440 $this->error = _("Name is missing");
441 return false;
442 }
443 if (empty($userdata['email'])) {
444 $this->error = _("E-mail address is missing");
445 return false;
446 }
447
448 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
449 $this->error = _("Nickname contains illegal characters");
450 return false;
451 }
452
453 if (empty($userdata['nickname'])) {
454 $userdata['nickname'] = $userdata['email'];
455 }
456
457 /* Check that specified backend is writable */
458 if (!$this->backends[$bnum]->writeable) {
459 $this->error = _("Addressbook is read-only");;
460 return false;
461 }
462
463 /* Modify user in backend */
464 $res = $this->backends[$bnum]->modify($alias, $userdata);
465 if ($res) {
466 return $bnum;
467 } else {
468 $this->error = $this->backends[$bnum]->error;
469 return false;
470 }
471
472 return FALSE; /* Not reached */
473 } /* end of modify() */
474
475
476 } /* End of class Addressbook */
477
478 /*
479 * Generic backend that all other backends extend
480 */
481 class addressbook_backend {
482
483 /* Variables that all backends must provide. */
484 var $btype = 'dummy';
485 var $bname = 'dummy';
486 var $sname = 'Dummy backend';
487
488 /*
489 * Variables common for all backends, but that
490 * should not be changed by the backends.
491 */
492 var $bnum = -1;
493 var $error = '';
494 var $writeable = false;
495
496 function set_error($string) {
497 $this->error = '[' . $this->sname . '] ' . $string;
498 return false;
499 }
500
501
502 /* ========================== Public ======================== */
503
504 function search($expression) {
505 $this->set_error('search not implemented');
506 return false;
507 }
508
509 function lookup($alias) {
510 $this->set_error('lookup not implemented');
511 return false;
512 }
513
514 function list_addr() {
515 $this->set_error('list_addr not implemented');
516 return false;
517 }
518
519 function add($userdata) {
520 $this->set_error('add not implemented');
521 return false;
522 }
523
524 function remove($alias) {
525 $this->set_error('delete not implemented');
526 return false;
527 }
528
529 function modify($alias, $newuserdata) {
530 $this->set_error('modify not implemented');
531 return false;
532 }
533
534 }
535
536 /* Sort array by the key "name" */
537 function alistcmp($a,$b) {
538 if ($a['backend'] > $b['backend']) {
539 return 1;
540 } else {
541 if ($a['backend'] < $b['backend']) {
542 return -1;
543 }
544 }
545 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
546 }
547
548 ?>