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 | * |
a9d318b0 |
11 | * @version $Id$ |
d6c32258 |
12 | * @package squirrelmail |
a9d318b0 |
13 | * @subpackage addressbook |
35586184 |
14 | */ |
15 | |
d6c32258 |
16 | /** |
81fa4801 |
17 | This is the path to the global site-wide addressbook. |
18 | It looks and feels just like a user's .abook file |
19 | If this is in the data directory, use "$data_dir/global.abook" |
20 | If not, specify the path as though it was accessed from the |
21 | src/ directory ("../global.abook" -> in main directory) |
22 | |
23 | If you don't want a global site-wide addressbook, comment these |
24 | two lines out. (They are disabled by default.) |
25 | |
26 | The global addressbook is unmodifiable by anyone. You must actually |
27 | use a shell script or whatnot to modify the contents. |
28 | |
91e51743 |
29 | global $data_dir, $address_book_global_filename; |
81fa4801 |
30 | $address_book_global_filename = "$data_dir/global.abook"; |
31 | |
81fa4801 |
32 | */ |
33 | |
30e9932c |
34 | global $addrbook_dsn, $addrbook_global_dsn; |
81fa4801 |
35 | |
d6c32258 |
36 | /** |
81fa4801 |
37 | Create and initialize an addressbook object. |
38 | Returns the created object |
39 | */ |
40 | function addressbook_init($showerr = true, $onlylocal = false) { |
41 | global $data_dir, $username, $ldap_server, $address_book_global_filename; |
42 | global $addrbook_dsn, $addrbook_table; |
30e9932c |
43 | global $addrbook_global_dsn, $addrbook_global_table, $addrbook_global_writeable, $addrbook_global_listing; |
81fa4801 |
44 | |
45 | /* Create a new addressbook object */ |
46 | $abook = new AddressBook; |
47 | |
48 | /* |
49 | Always add a local backend. We use *either* file-based *or* a |
50 | database addressbook. If $addrbook_dsn is set, the database |
51 | backend is used. If not, addressbooks are stores in files. |
52 | */ |
53 | if (isset($addrbook_dsn) && !empty($addrbook_dsn)) { |
54 | /* Database */ |
55 | if (!isset($addrbook_table) || empty($addrbook_table)) { |
56 | $addrbook_table = 'address'; |
57 | } |
58 | $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn, |
59 | 'owner' => $username, |
60 | 'table' => $addrbook_table)); |
61 | if (!$r && $showerr) { |
62 | echo _("Error initializing addressbook database."); |
63 | exit; |
64 | } |
65 | } else { |
66 | /* File */ |
67 | $filename = getHashedFile($username, $data_dir, "$username.abook"); |
68 | $r = $abook->add_backend('local_file', Array('filename' => $filename, |
69 | 'create' => true)); |
70 | if(!$r && $showerr) { |
71 | printf( _("Error opening file %s"), $filename ); |
72 | exit; |
73 | } |
74 | |
75 | } |
76 | |
77 | /* This would be for the global addressbook */ |
78 | if (isset($address_book_global_filename)) { |
79 | $r = $abook->add_backend('global_file'); |
80 | if (!$r && $showerr) { |
81 | echo _("Error initializing global addressbook."); |
82 | exit; |
83 | } |
84 | } |
85 | |
30e9932c |
86 | /* Load global addressbook from SQL if configured */ |
87 | if (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) { |
88 | /* Database configured */ |
89 | if (!isset($addrbook_global_table) || empty($addrbook_global_table)) { |
90 | $addrbook_global_table = 'global_abook'; |
91 | } |
92 | $r = $abook->add_backend('database', |
93 | Array('dsn' => $addrbook_global_dsn, |
94 | 'owner' => 'global', |
95 | 'name' => _("Global address book"), |
96 | 'writeable' => $addrbook_global_writeable, |
97 | 'listing' => $addrbook_global_listing, |
98 | 'table' => $addrbook_global_table)); |
99 | } |
100 | |
df788686 |
101 | /* |
102 | * hook allows to include different address book backends. |
103 | * plugins should extract $abook and $r from arguments |
104 | * and use same add_backend commands as above functions. |
105 | */ |
106 | $hookReturn = do_hook('abook_init', $abook, $r); |
107 | $abook = $hookReturn[1]; |
108 | $r = $hookReturn[2]; |
109 | |
81fa4801 |
110 | if ($onlylocal) { |
111 | return $abook; |
112 | } |
113 | |
114 | /* Load configured LDAP servers (if PHP has LDAP support) */ |
115 | if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) { |
116 | reset($ldap_server); |
117 | while (list($undef,$param) = each($ldap_server)) { |
118 | if (is_array($param)) { |
119 | $r = $abook->add_backend('ldap_server', $param); |
120 | if (!$r && $showerr) { |
121 | printf( ' ' . _("Error initializing LDAP server %s:") . |
122 | "<BR>\n", $param['host']); |
123 | echo ' ' . $abook->error; |
124 | exit; |
125 | } |
126 | } |
127 | } |
128 | } |
4935919f |
129 | |
81fa4801 |
130 | /* Return the initialized object */ |
131 | return $abook; |
4935919f |
132 | } |
133 | |
134 | |
81fa4801 |
135 | /* |
136 | * Had to move this function outside of the Addressbook Class |
137 | * PHP 4.0.4 Seemed to be having problems with inline functions. |
abd74f7d |
138 | * Note: this can return now since we don't support 4.0.4 anymore. |
81fa4801 |
139 | */ |
140 | function addressbook_cmp($a,$b) { |
4935919f |
141 | |
81fa4801 |
142 | if($a['backend'] > $b['backend']) { |
143 | return 1; |
144 | } else if($a['backend'] < $b['backend']) { |
145 | return -1; |
146 | } |
147 | |
148 | return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1; |
4935919f |
149 | |
81fa4801 |
150 | } |
4935919f |
151 | |
152 | |
8f6f9ba5 |
153 | /** |
81fa4801 |
154 | * This is the main address book class that connect all the |
155 | * backends and provide services to the functions above. |
8f6f9ba5 |
156 | * @package squirrelmail |
81fa4801 |
157 | */ |
5100704d |
158 | |
81fa4801 |
159 | class AddressBook { |
4935919f |
160 | |
81fa4801 |
161 | var $backends = array(); |
162 | var $numbackends = 0; |
163 | var $error = ''; |
164 | var $localbackend = 0; |
165 | var $localbackendname = ''; |
166 | |
167 | // Constructor function. |
168 | function AddressBook() { |
169 | $localbackendname = _("Personal address book"); |
170 | } |
4935919f |
171 | |
81fa4801 |
172 | /* |
173 | * Return an array of backends of a given type, |
174 | * or all backends if no type is specified. |
175 | */ |
176 | function get_backend_list($type = '') { |
177 | $ret = array(); |
178 | for ($i = 1 ; $i <= $this->numbackends ; $i++) { |
179 | if (empty($type) || $type == $this->backends[$i]->btype) { |
180 | $ret[] = &$this->backends[$i]; |
181 | } |
4935919f |
182 | } |
81fa4801 |
183 | return $ret; |
184 | } |
4935919f |
185 | |
186 | |
81fa4801 |
187 | /* |
188 | ========================== Public ======================== |
189 | |
190 | Add a new backend. $backend is the name of a backend |
191 | (without the abook_ prefix), and $param is an optional |
192 | mixed variable that is passed to the backend constructor. |
193 | See each of the backend classes for valid parameters. |
194 | */ |
195 | function add_backend($backend, $param = '') { |
196 | $backend_name = 'abook_' . $backend; |
197 | eval('$newback = new ' . $backend_name . '($param);'); |
198 | if(!empty($newback->error)) { |
199 | $this->error = $newback->error; |
200 | return false; |
201 | } |
202 | |
203 | $this->numbackends++; |
204 | |
205 | $newback->bnum = $this->numbackends; |
206 | $this->backends[$this->numbackends] = $newback; |
207 | |
208 | /* Store ID of first local backend added */ |
209 | if ($this->localbackend == 0 && $newback->btype == 'local') { |
210 | $this->localbackend = $this->numbackends; |
211 | $this->localbackendname = $newback->sname; |
212 | } |
213 | |
214 | return $this->numbackends; |
215 | } |
4935919f |
216 | |
4935919f |
217 | |
2e542990 |
218 | /* |
219 | * This function takes a $row array as returned by the addressbook |
220 | * search and returns an e-mail address with the full name or |
221 | * nickname optionally prepended. |
222 | */ |
223 | |
224 | function full_address($row) { |
1ba8cd6b |
225 | global $addrsrch_fullname, $data_dir, $username; |
20ad4fdd |
226 | $prefix = getPref($data_dir, $username, 'addrsrch_fullname'); |
227 | if (($prefix != "" || (isset($addrsrch_fullname) && |
228 | $prefix == $addrsrch_fullname)) && $prefix != 'noprefix') { |
229 | $name = ($prefix == 'nickname' ? $row['nickname'] : $row['name']); |
2e542990 |
230 | return $name . ' <' . trim($row['email']) . '>'; |
231 | } else { |
232 | return trim($row['email']); |
233 | } |
234 | } |
235 | |
81fa4801 |
236 | /* |
237 | Return a list of addresses matching expression in |
238 | all backends of a given type. |
239 | */ |
240 | function search($expression, $bnum = -1) { |
241 | $ret = array(); |
242 | $this->error = ''; |
243 | |
244 | /* Search all backends */ |
245 | if ($bnum == -1) { |
246 | $sel = $this->get_backend_list(''); |
247 | $failed = 0; |
248 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
249 | $backend = &$sel[$i]; |
250 | $backend->error = ''; |
251 | $res = $backend->search($expression); |
252 | if (is_array($res)) { |
253 | $ret = array_merge($ret, $res); |
254 | } else { |
255 | $this->error .= "<br>\n" . $backend->error; |
256 | $failed++; |
75e19c7f |
257 | } |
258 | } |
4935919f |
259 | |
81fa4801 |
260 | /* Only fail if all backends failed */ |
261 | if( $failed >= sizeof( $sel ) ) { |
262 | $ret = FALSE; |
4935919f |
263 | } |
4935919f |
264 | |
81fa4801 |
265 | } else { |
4935919f |
266 | |
81fa4801 |
267 | /* Search only one backend */ |
4935919f |
268 | |
81fa4801 |
269 | $ret = $this->backends[$bnum]->search($expression); |
270 | if (!is_array($ret)) { |
271 | $this->error .= "<br>\n" . $this->backends[$bnum]->error; |
272 | $ret = FALSE; |
273 | } |
274 | } |
275 | |
276 | return( $ret ); |
4935919f |
277 | } |
278 | |
279 | |
81fa4801 |
280 | /* Return a sorted search */ |
281 | function s_search($expression, $bnum = -1) { |
282 | |
283 | $ret = $this->search($expression, $bnum); |
284 | if ( is_array( $ret ) ) { |
285 | usort($ret, 'addressbook_cmp'); |
286 | } |
287 | return $ret; |
288 | } |
4935919f |
289 | |
290 | |
81fa4801 |
291 | /* |
292 | * Lookup an address by alias. Only possible in |
293 | * local backends. |
294 | */ |
295 | function lookup($alias, $bnum = -1) { |
296 | |
297 | $ret = array(); |
298 | |
299 | if ($bnum > -1) { |
300 | $res = $this->backends[$bnum]->lookup($alias); |
301 | if (is_array($res)) { |
302 | return $res; |
303 | } else { |
304 | $this->error = $backend->error; |
305 | return false; |
306 | } |
307 | } |
308 | |
309 | $sel = $this->get_backend_list('local'); |
310 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
311 | $backend = &$sel[$i]; |
312 | $backend->error = ''; |
313 | $res = $backend->lookup($alias); |
314 | if (is_array($res)) { |
315 | if(!empty($res)) |
316 | return $res; |
317 | } else { |
318 | $this->error = $backend->error; |
319 | return false; |
320 | } |
321 | } |
322 | |
323 | return $ret; |
4935919f |
324 | } |
325 | |
4935919f |
326 | |
81fa4801 |
327 | /* Return all addresses */ |
328 | function list_addr($bnum = -1) { |
329 | $ret = array(); |
330 | |
331 | if ($bnum == -1) { |
332 | $sel = $this->get_backend_list('local'); |
333 | } else { |
334 | $sel = array(0 => &$this->backends[$bnum]); |
335 | } |
336 | |
337 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
338 | $backend = &$sel[$i]; |
339 | $backend->error = ''; |
340 | $res = $backend->list_addr(); |
341 | if (is_array($res)) { |
342 | $ret = array_merge($ret, $res); |
343 | } else { |
344 | $this->error = $backend->error; |
345 | return false; |
346 | } |
347 | } |
348 | |
349 | return $ret; |
350 | } |
4935919f |
351 | |
81fa4801 |
352 | /* |
353 | * Create a new address from $userdata, in backend $bnum. |
354 | * Return the backend number that the/ address was added |
355 | * to, or false if it failed. |
356 | */ |
357 | function add($userdata, $bnum) { |
4935919f |
358 | |
81fa4801 |
359 | /* Validate data */ |
360 | if (!is_array($userdata)) { |
361 | $this->error = _("Invalid input data"); |
362 | return false; |
363 | } |
364 | if (empty($userdata['firstname']) && empty($userdata['lastname'])) { |
365 | $this->error = _("Name is missing"); |
366 | return false; |
367 | } |
368 | if (empty($userdata['email'])) { |
369 | $this->error = _("E-mail address is missing"); |
370 | return false; |
371 | } |
372 | if (empty($userdata['nickname'])) { |
373 | $userdata['nickname'] = $userdata['email']; |
374 | } |
375 | |
376 | if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) { |
377 | $this->error = _("Nickname contains illegal characters"); |
378 | return false; |
379 | } |
380 | |
381 | /* Check that specified backend accept new entries */ |
382 | if (!$this->backends[$bnum]->writeable) { |
383 | $this->error = _("Addressbook is read-only"); |
384 | return false; |
385 | } |
386 | |
387 | /* Add address to backend */ |
388 | $res = $this->backends[$bnum]->add($userdata); |
389 | if ($res) { |
390 | return $bnum; |
391 | } else { |
392 | $this->error = $this->backends[$bnum]->error; |
393 | return false; |
394 | } |
395 | |
396 | return false; // Not reached |
397 | } /* end of add() */ |
398 | |
399 | |
400 | /* |
401 | * Remove the user identified by $alias from backend $bnum |
402 | * If $alias is an array, all users in the array are removed. |
403 | */ |
404 | function remove($alias, $bnum) { |
405 | |
406 | /* Check input */ |
407 | if (empty($alias)) { |
408 | return true; |
409 | } |
410 | |
411 | /* Convert string to single element array */ |
412 | if (!is_array($alias)) { |
413 | $alias = array(0 => $alias); |
414 | } |
415 | |
30e9932c |
416 | /* Check that specified backend is writeable */ |
81fa4801 |
417 | if (!$this->backends[$bnum]->writeable) { |
418 | $this->error = _("Addressbook is read-only"); |
419 | return false; |
420 | } |
421 | |
422 | /* Remove user from backend */ |
423 | $res = $this->backends[$bnum]->remove($alias); |
424 | if ($res) { |
425 | return $bnum; |
426 | } else { |
427 | $this->error = $this->backends[$bnum]->error; |
428 | return false; |
429 | } |
430 | |
431 | return FALSE; /* Not reached */ |
432 | } /* end of remove() */ |
433 | |
434 | |
435 | /* |
436 | * Remove the user identified by $alias from backend $bnum |
437 | * If $alias is an array, all users in the array are removed. |
438 | */ |
439 | function modify($alias, $userdata, $bnum) { |
440 | |
441 | /* Check input */ |
442 | if (empty($alias) || !is_string($alias)) { |
443 | return true; |
444 | } |
445 | |
446 | /* Validate data */ |
447 | if(!is_array($userdata)) { |
448 | $this->error = _("Invalid input data"); |
449 | return false; |
450 | } |
451 | if (empty($userdata['firstname']) && empty($userdata['lastname'])) { |
452 | $this->error = _("Name is missing"); |
453 | return false; |
454 | } |
455 | if (empty($userdata['email'])) { |
456 | $this->error = _("E-mail address is missing"); |
457 | return false; |
458 | } |
459 | |
460 | if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) { |
461 | $this->error = _("Nickname contains illegal characters"); |
462 | return false; |
463 | } |
464 | |
465 | if (empty($userdata['nickname'])) { |
466 | $userdata['nickname'] = $userdata['email']; |
467 | } |
468 | |
30e9932c |
469 | /* Check that specified backend is writeable */ |
81fa4801 |
470 | if (!$this->backends[$bnum]->writeable) { |
471 | $this->error = _("Addressbook is read-only");; |
472 | return false; |
473 | } |
474 | |
475 | /* Modify user in backend */ |
476 | $res = $this->backends[$bnum]->modify($alias, $userdata); |
477 | if ($res) { |
478 | return $bnum; |
479 | } else { |
480 | $this->error = $this->backends[$bnum]->error; |
481 | return false; |
482 | } |
483 | |
484 | return FALSE; /* Not reached */ |
485 | } /* end of modify() */ |
4935919f |
486 | |
4935919f |
487 | |
81fa4801 |
488 | } /* End of class Addressbook */ |
489 | |
8f6f9ba5 |
490 | /** |
81fa4801 |
491 | * Generic backend that all other backends extend |
8f6f9ba5 |
492 | * @package squirrelmail |
81fa4801 |
493 | */ |
494 | class addressbook_backend { |
495 | |
496 | /* Variables that all backends must provide. */ |
497 | var $btype = 'dummy'; |
498 | var $bname = 'dummy'; |
499 | var $sname = 'Dummy backend'; |
4935919f |
500 | |
81fa4801 |
501 | /* |
502 | * Variables common for all backends, but that |
503 | * should not be changed by the backends. |
504 | */ |
505 | var $bnum = -1; |
506 | var $error = ''; |
507 | var $writeable = false; |
4935919f |
508 | |
81fa4801 |
509 | function set_error($string) { |
510 | $this->error = '[' . $this->sname . '] ' . $string; |
511 | return false; |
512 | } |
4935919f |
513 | |
75e19c7f |
514 | |
81fa4801 |
515 | /* ========================== Public ======================== */ |
516 | |
517 | function search($expression) { |
518 | $this->set_error('search not implemented'); |
519 | return false; |
520 | } |
521 | |
522 | function lookup($alias) { |
523 | $this->set_error('lookup not implemented'); |
524 | return false; |
a10110a5 |
525 | } |
81fa4801 |
526 | |
527 | function list_addr() { |
528 | $this->set_error('list_addr not implemented'); |
529 | return false; |
530 | } |
531 | |
532 | function add($userdata) { |
533 | $this->set_error('add not implemented'); |
534 | return false; |
535 | } |
536 | |
537 | function remove($alias) { |
538 | $this->set_error('delete not implemented'); |
539 | return false; |
540 | } |
541 | |
542 | function modify($alias, $newuserdata) { |
543 | $this->set_error('modify not implemented'); |
544 | return false; |
545 | } |
546 | |
547 | } |
548 | |
92948f86 |
549 | /** |
550 | * Sort array by the key "name" |
551 | */ |
81fa4801 |
552 | function alistcmp($a,$b) { |
92948f86 |
553 | $abook_sort=get_abook_sort(); |
554 | |
81fa4801 |
555 | if ($a['backend'] > $b['backend']) { |
556 | return 1; |
557 | } else { |
558 | if ($a['backend'] < $b['backend']) { |
559 | return -1; |
560 | } |
561 | } |
92948f86 |
562 | return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1; |
81fa4801 |
563 | } |
564 | |
92948f86 |
565 | /** |
566 | * Address book sorting order |
567 | * |
568 | * returns name of field that should be used for sorting. |
569 | * @return string name of address book field |
570 | */ |
571 | function get_abook_sort() { |
572 | global $data_dir, $username; |
573 | |
574 | /* get sorting order */ |
575 | if(sqgetGlobalVar('abook_sort', $abook_sort,SQ_GET)) { |
576 | sqgetGlobalVar('abook_sort', $abook_sort,SQ_GET); |
577 | setPref($data_dir, $username, 'abook_sort', $abook_sort); |
578 | } else { |
579 | $abook_sort = getPref($data_dir, $username, 'abook_sort', 'name'); |
580 | } |
581 | |
582 | if ($abook_sort != "nickname" && $abook_sort != "email" && $abook_sort != "label") |
583 | $abook_sort = "name"; |
584 | |
585 | return $abook_sort; |
586 | } |
0419106e |
587 | |
588 | /* |
589 | PHP 5 requires that the class be made first, which seems rather |
590 | logical, and should have been the way it was generated the first time. |
591 | */ |
592 | |
593 | require_once(SM_PATH . 'functions/abook_local_file.php'); |
594 | require_once(SM_PATH . 'functions/abook_ldap_server.php'); |
595 | |
596 | /* Use this if you wanna have a global address book */ |
597 | if (isset($address_book_global_filename)) { |
598 | include_once(SM_PATH . 'functions/abook_global_file.php'); |
599 | } |
600 | |
601 | /* Only load database backend if database is configured */ |
30e9932c |
602 | if((isset($addrbook_dsn) && !empty($addrbook_dsn)) || |
603 | (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) ) { |
0419106e |
604 | include_once(SM_PATH . 'functions/abook_database.php'); |
605 | } |
606 | |
df788686 |
607 | /* |
608 | * hook allows adding different address book classes. |
609 | * class must follow address book class coding standards. |
610 | * |
611 | * see addressbook_backend class and functions/abook_*.php files. |
612 | */ |
613 | do_hook('abook_add_class'); |
0419106e |
614 | |
2e542990 |
615 | ?> |