5100704d |
1 | <?php |
2 | |
35586184 |
3 | /** |
4 | * addressbook.php |
5 | * |
82d304a0 |
6 | * Copyright (c) 1999-2004 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$ |
d6c32258 |
12 | * @package squirrelmail |
35586184 |
13 | */ |
14 | |
d6c32258 |
15 | /** |
81fa4801 |
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 | |
91e51743 |
28 | global $data_dir, $address_book_global_filename; |
81fa4801 |
29 | $address_book_global_filename = "$data_dir/global.abook"; |
30 | |
81fa4801 |
31 | */ |
32 | |
30e9932c |
33 | global $addrbook_dsn, $addrbook_global_dsn; |
81fa4801 |
34 | |
d6c32258 |
35 | /** |
81fa4801 |
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; |
30e9932c |
42 | global $addrbook_global_dsn, $addrbook_global_table, $addrbook_global_writeable, $addrbook_global_listing; |
81fa4801 |
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 | |
30e9932c |
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 | |
81fa4801 |
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( ' ' . _("Error initializing LDAP server %s:") . |
112 | "<BR>\n", $param['host']); |
113 | echo ' ' . $abook->error; |
114 | exit; |
115 | } |
116 | } |
117 | } |
118 | } |
4935919f |
119 | |
81fa4801 |
120 | /* Return the initialized object */ |
121 | return $abook; |
4935919f |
122 | } |
123 | |
124 | |
81fa4801 |
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. |
abd74f7d |
128 | * Note: this can return now since we don't support 4.0.4 anymore. |
81fa4801 |
129 | */ |
130 | function addressbook_cmp($a,$b) { |
4935919f |
131 | |
81fa4801 |
132 | if($a['backend'] > $b['backend']) { |
133 | return 1; |
134 | } else if($a['backend'] < $b['backend']) { |
135 | return -1; |
136 | } |
137 | |
138 | return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1; |
4935919f |
139 | |
81fa4801 |
140 | } |
4935919f |
141 | |
142 | |
8f6f9ba5 |
143 | /** |
81fa4801 |
144 | * This is the main address book class that connect all the |
145 | * backends and provide services to the functions above. |
8f6f9ba5 |
146 | * @package squirrelmail |
81fa4801 |
147 | */ |
5100704d |
148 | |
81fa4801 |
149 | class AddressBook { |
4935919f |
150 | |
81fa4801 |
151 | var $backends = array(); |
152 | var $numbackends = 0; |
153 | var $error = ''; |
154 | var $localbackend = 0; |
155 | var $localbackendname = ''; |
156 | |
157 | // Constructor function. |
158 | function AddressBook() { |
159 | $localbackendname = _("Personal address book"); |
160 | } |
4935919f |
161 | |
81fa4801 |
162 | /* |
163 | * Return an array of backends of a given type, |
164 | * or all backends if no type is specified. |
165 | */ |
166 | function get_backend_list($type = '') { |
167 | $ret = array(); |
168 | for ($i = 1 ; $i <= $this->numbackends ; $i++) { |
169 | if (empty($type) || $type == $this->backends[$i]->btype) { |
170 | $ret[] = &$this->backends[$i]; |
171 | } |
4935919f |
172 | } |
81fa4801 |
173 | return $ret; |
174 | } |
4935919f |
175 | |
176 | |
81fa4801 |
177 | /* |
178 | ========================== Public ======================== |
179 | |
180 | Add a new backend. $backend is the name of a backend |
181 | (without the abook_ prefix), and $param is an optional |
182 | mixed variable that is passed to the backend constructor. |
183 | See each of the backend classes for valid parameters. |
184 | */ |
185 | function add_backend($backend, $param = '') { |
186 | $backend_name = 'abook_' . $backend; |
187 | eval('$newback = new ' . $backend_name . '($param);'); |
188 | if(!empty($newback->error)) { |
189 | $this->error = $newback->error; |
190 | return false; |
191 | } |
192 | |
193 | $this->numbackends++; |
194 | |
195 | $newback->bnum = $this->numbackends; |
196 | $this->backends[$this->numbackends] = $newback; |
197 | |
198 | /* Store ID of first local backend added */ |
199 | if ($this->localbackend == 0 && $newback->btype == 'local') { |
200 | $this->localbackend = $this->numbackends; |
201 | $this->localbackendname = $newback->sname; |
202 | } |
203 | |
204 | return $this->numbackends; |
205 | } |
4935919f |
206 | |
4935919f |
207 | |
2e542990 |
208 | /* |
209 | * This function takes a $row array as returned by the addressbook |
210 | * search and returns an e-mail address with the full name or |
211 | * nickname optionally prepended. |
212 | */ |
213 | |
214 | function full_address($row) { |
1ba8cd6b |
215 | global $addrsrch_fullname, $data_dir, $username; |
2e542990 |
216 | |
1ba8cd6b |
217 | if (($prefix = getPref($data_dir, $username, 'addrsrch_fullname') or |
e7c6d43c |
218 | isset($addrsrch_fullname) and $prefix = $addrsrch_fullname) |
219 | and $prefix !== 'noprefix') { |
b9d210e3 |
220 | $name = ($prefix === 'nickname') ? $row['nickname'] |
2e542990 |
221 | : $row['name']; |
222 | return $name . ' <' . trim($row['email']) . '>'; |
223 | } else { |
224 | return trim($row['email']); |
225 | } |
226 | } |
227 | |
81fa4801 |
228 | /* |
229 | Return a list of addresses matching expression in |
230 | all backends of a given type. |
231 | */ |
232 | function search($expression, $bnum = -1) { |
233 | $ret = array(); |
234 | $this->error = ''; |
235 | |
236 | /* Search all backends */ |
237 | if ($bnum == -1) { |
238 | $sel = $this->get_backend_list(''); |
239 | $failed = 0; |
240 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
241 | $backend = &$sel[$i]; |
242 | $backend->error = ''; |
243 | $res = $backend->search($expression); |
244 | if (is_array($res)) { |
245 | $ret = array_merge($ret, $res); |
246 | } else { |
247 | $this->error .= "<br>\n" . $backend->error; |
248 | $failed++; |
75e19c7f |
249 | } |
250 | } |
4935919f |
251 | |
81fa4801 |
252 | /* Only fail if all backends failed */ |
253 | if( $failed >= sizeof( $sel ) ) { |
254 | $ret = FALSE; |
4935919f |
255 | } |
4935919f |
256 | |
81fa4801 |
257 | } else { |
4935919f |
258 | |
81fa4801 |
259 | /* Search only one backend */ |
4935919f |
260 | |
81fa4801 |
261 | $ret = $this->backends[$bnum]->search($expression); |
262 | if (!is_array($ret)) { |
263 | $this->error .= "<br>\n" . $this->backends[$bnum]->error; |
264 | $ret = FALSE; |
265 | } |
266 | } |
267 | |
268 | return( $ret ); |
4935919f |
269 | } |
270 | |
271 | |
81fa4801 |
272 | /* Return a sorted search */ |
273 | function s_search($expression, $bnum = -1) { |
274 | |
275 | $ret = $this->search($expression, $bnum); |
276 | if ( is_array( $ret ) ) { |
277 | usort($ret, 'addressbook_cmp'); |
278 | } |
279 | return $ret; |
280 | } |
4935919f |
281 | |
282 | |
81fa4801 |
283 | /* |
284 | * Lookup an address by alias. Only possible in |
285 | * local backends. |
286 | */ |
287 | function lookup($alias, $bnum = -1) { |
288 | |
289 | $ret = array(); |
290 | |
291 | if ($bnum > -1) { |
292 | $res = $this->backends[$bnum]->lookup($alias); |
293 | if (is_array($res)) { |
294 | return $res; |
295 | } else { |
296 | $this->error = $backend->error; |
297 | return false; |
298 | } |
299 | } |
300 | |
301 | $sel = $this->get_backend_list('local'); |
302 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
303 | $backend = &$sel[$i]; |
304 | $backend->error = ''; |
305 | $res = $backend->lookup($alias); |
306 | if (is_array($res)) { |
307 | if(!empty($res)) |
308 | return $res; |
309 | } else { |
310 | $this->error = $backend->error; |
311 | return false; |
312 | } |
313 | } |
314 | |
315 | return $ret; |
4935919f |
316 | } |
317 | |
4935919f |
318 | |
81fa4801 |
319 | /* Return all addresses */ |
320 | function list_addr($bnum = -1) { |
321 | $ret = array(); |
322 | |
323 | if ($bnum == -1) { |
324 | $sel = $this->get_backend_list('local'); |
325 | } else { |
326 | $sel = array(0 => &$this->backends[$bnum]); |
327 | } |
328 | |
329 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
330 | $backend = &$sel[$i]; |
331 | $backend->error = ''; |
332 | $res = $backend->list_addr(); |
333 | if (is_array($res)) { |
334 | $ret = array_merge($ret, $res); |
335 | } else { |
336 | $this->error = $backend->error; |
337 | return false; |
338 | } |
339 | } |
340 | |
341 | return $ret; |
342 | } |
4935919f |
343 | |
81fa4801 |
344 | /* |
345 | * Create a new address from $userdata, in backend $bnum. |
346 | * Return the backend number that the/ address was added |
347 | * to, or false if it failed. |
348 | */ |
349 | function add($userdata, $bnum) { |
4935919f |
350 | |
81fa4801 |
351 | /* Validate data */ |
352 | if (!is_array($userdata)) { |
353 | $this->error = _("Invalid input data"); |
354 | return false; |
355 | } |
356 | if (empty($userdata['firstname']) && empty($userdata['lastname'])) { |
357 | $this->error = _("Name is missing"); |
358 | return false; |
359 | } |
360 | if (empty($userdata['email'])) { |
361 | $this->error = _("E-mail address is missing"); |
362 | return false; |
363 | } |
364 | if (empty($userdata['nickname'])) { |
365 | $userdata['nickname'] = $userdata['email']; |
366 | } |
367 | |
368 | if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) { |
369 | $this->error = _("Nickname contains illegal characters"); |
370 | return false; |
371 | } |
372 | |
373 | /* Check that specified backend accept new entries */ |
374 | if (!$this->backends[$bnum]->writeable) { |
375 | $this->error = _("Addressbook is read-only"); |
376 | return false; |
377 | } |
378 | |
379 | /* Add address to backend */ |
380 | $res = $this->backends[$bnum]->add($userdata); |
381 | if ($res) { |
382 | return $bnum; |
383 | } else { |
384 | $this->error = $this->backends[$bnum]->error; |
385 | return false; |
386 | } |
387 | |
388 | return false; // Not reached |
389 | } /* end of add() */ |
390 | |
391 | |
392 | /* |
393 | * Remove the user identified by $alias from backend $bnum |
394 | * If $alias is an array, all users in the array are removed. |
395 | */ |
396 | function remove($alias, $bnum) { |
397 | |
398 | /* Check input */ |
399 | if (empty($alias)) { |
400 | return true; |
401 | } |
402 | |
403 | /* Convert string to single element array */ |
404 | if (!is_array($alias)) { |
405 | $alias = array(0 => $alias); |
406 | } |
407 | |
30e9932c |
408 | /* Check that specified backend is writeable */ |
81fa4801 |
409 | if (!$this->backends[$bnum]->writeable) { |
410 | $this->error = _("Addressbook is read-only"); |
411 | return false; |
412 | } |
413 | |
414 | /* Remove user from backend */ |
415 | $res = $this->backends[$bnum]->remove($alias); |
416 | if ($res) { |
417 | return $bnum; |
418 | } else { |
419 | $this->error = $this->backends[$bnum]->error; |
420 | return false; |
421 | } |
422 | |
423 | return FALSE; /* Not reached */ |
424 | } /* end of remove() */ |
425 | |
426 | |
427 | /* |
428 | * Remove the user identified by $alias from backend $bnum |
429 | * If $alias is an array, all users in the array are removed. |
430 | */ |
431 | function modify($alias, $userdata, $bnum) { |
432 | |
433 | /* Check input */ |
434 | if (empty($alias) || !is_string($alias)) { |
435 | return true; |
436 | } |
437 | |
438 | /* Validate data */ |
439 | if(!is_array($userdata)) { |
440 | $this->error = _("Invalid input data"); |
441 | return false; |
442 | } |
443 | if (empty($userdata['firstname']) && empty($userdata['lastname'])) { |
444 | $this->error = _("Name is missing"); |
445 | return false; |
446 | } |
447 | if (empty($userdata['email'])) { |
448 | $this->error = _("E-mail address is missing"); |
449 | return false; |
450 | } |
451 | |
452 | if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) { |
453 | $this->error = _("Nickname contains illegal characters"); |
454 | return false; |
455 | } |
456 | |
457 | if (empty($userdata['nickname'])) { |
458 | $userdata['nickname'] = $userdata['email']; |
459 | } |
460 | |
30e9932c |
461 | /* Check that specified backend is writeable */ |
81fa4801 |
462 | if (!$this->backends[$bnum]->writeable) { |
463 | $this->error = _("Addressbook is read-only");; |
464 | return false; |
465 | } |
466 | |
467 | /* Modify user in backend */ |
468 | $res = $this->backends[$bnum]->modify($alias, $userdata); |
469 | if ($res) { |
470 | return $bnum; |
471 | } else { |
472 | $this->error = $this->backends[$bnum]->error; |
473 | return false; |
474 | } |
475 | |
476 | return FALSE; /* Not reached */ |
477 | } /* end of modify() */ |
4935919f |
478 | |
4935919f |
479 | |
81fa4801 |
480 | } /* End of class Addressbook */ |
481 | |
8f6f9ba5 |
482 | /** |
81fa4801 |
483 | * Generic backend that all other backends extend |
8f6f9ba5 |
484 | * @package squirrelmail |
81fa4801 |
485 | */ |
486 | class addressbook_backend { |
487 | |
488 | /* Variables that all backends must provide. */ |
489 | var $btype = 'dummy'; |
490 | var $bname = 'dummy'; |
491 | var $sname = 'Dummy backend'; |
4935919f |
492 | |
81fa4801 |
493 | /* |
494 | * Variables common for all backends, but that |
495 | * should not be changed by the backends. |
496 | */ |
497 | var $bnum = -1; |
498 | var $error = ''; |
499 | var $writeable = false; |
4935919f |
500 | |
81fa4801 |
501 | function set_error($string) { |
502 | $this->error = '[' . $this->sname . '] ' . $string; |
503 | return false; |
504 | } |
4935919f |
505 | |
75e19c7f |
506 | |
81fa4801 |
507 | /* ========================== Public ======================== */ |
508 | |
509 | function search($expression) { |
510 | $this->set_error('search not implemented'); |
511 | return false; |
512 | } |
513 | |
514 | function lookup($alias) { |
515 | $this->set_error('lookup not implemented'); |
516 | return false; |
a10110a5 |
517 | } |
81fa4801 |
518 | |
519 | function list_addr() { |
520 | $this->set_error('list_addr not implemented'); |
521 | return false; |
522 | } |
523 | |
524 | function add($userdata) { |
525 | $this->set_error('add not implemented'); |
526 | return false; |
527 | } |
528 | |
529 | function remove($alias) { |
530 | $this->set_error('delete not implemented'); |
531 | return false; |
532 | } |
533 | |
534 | function modify($alias, $newuserdata) { |
535 | $this->set_error('modify not implemented'); |
536 | return false; |
537 | } |
538 | |
539 | } |
540 | |
541 | /* Sort array by the key "name" */ |
542 | function alistcmp($a,$b) { |
543 | if ($a['backend'] > $b['backend']) { |
544 | return 1; |
545 | } else { |
546 | if ($a['backend'] < $b['backend']) { |
547 | return -1; |
548 | } |
549 | } |
550 | return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1; |
551 | } |
552 | |
0419106e |
553 | |
554 | /* |
555 | PHP 5 requires that the class be made first, which seems rather |
556 | logical, and should have been the way it was generated the first time. |
557 | */ |
558 | |
559 | require_once(SM_PATH . 'functions/abook_local_file.php'); |
560 | require_once(SM_PATH . 'functions/abook_ldap_server.php'); |
561 | |
562 | /* Use this if you wanna have a global address book */ |
563 | if (isset($address_book_global_filename)) { |
564 | include_once(SM_PATH . 'functions/abook_global_file.php'); |
565 | } |
566 | |
567 | /* Only load database backend if database is configured */ |
30e9932c |
568 | if((isset($addrbook_dsn) && !empty($addrbook_dsn)) || |
569 | (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) ) { |
0419106e |
570 | include_once(SM_PATH . 'functions/abook_database.php'); |
571 | } |
572 | |
573 | |
2e542990 |
574 | ?> |