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