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