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