5100704d |
1 | <?php |
35586184 |
2 | /** |
6ad2bbe2 |
3 | * functions/addressbook.php - Functions and classes for the addressbook system |
35586184 |
4 | * |
6c84ba1e |
5 | * Copyright (c) 1999-2005 The SquirrelMail Project Team |
35586184 |
6 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
7 | * |
6ad2bbe2 |
8 | * Functions require SM_PATH and support of forms.php functions |
35586184 |
9 | * |
a9d318b0 |
10 | * @version $Id$ |
d6c32258 |
11 | * @package squirrelmail |
a9d318b0 |
12 | * @subpackage addressbook |
35586184 |
13 | */ |
14 | |
30e9932c |
15 | global $addrbook_dsn, $addrbook_global_dsn; |
81fa4801 |
16 | |
d6c32258 |
17 | /** |
81fa4801 |
18 | Create and initialize an addressbook object. |
19 | Returns the created object |
20 | */ |
21 | function addressbook_init($showerr = true, $onlylocal = false) { |
22 | global $data_dir, $username, $ldap_server, $address_book_global_filename; |
23 | global $addrbook_dsn, $addrbook_table; |
4272758c |
24 | global $abook_global_file, $abook_global_file_writeable; |
30e9932c |
25 | global $addrbook_global_dsn, $addrbook_global_table, $addrbook_global_writeable, $addrbook_global_listing; |
81fa4801 |
26 | |
27 | /* Create a new addressbook object */ |
28 | $abook = new AddressBook; |
29 | |
30 | /* |
31 | Always add a local backend. We use *either* file-based *or* a |
32 | database addressbook. If $addrbook_dsn is set, the database |
33 | backend is used. If not, addressbooks are stores in files. |
34 | */ |
35 | if (isset($addrbook_dsn) && !empty($addrbook_dsn)) { |
36 | /* Database */ |
37 | if (!isset($addrbook_table) || empty($addrbook_table)) { |
38 | $addrbook_table = 'address'; |
39 | } |
40 | $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn, |
41 | 'owner' => $username, |
42 | 'table' => $addrbook_table)); |
43 | if (!$r && $showerr) { |
44 | echo _("Error initializing addressbook database."); |
45 | exit; |
46 | } |
47 | } else { |
48 | /* File */ |
49 | $filename = getHashedFile($username, $data_dir, "$username.abook"); |
50 | $r = $abook->add_backend('local_file', Array('filename' => $filename, |
51 | 'create' => true)); |
52 | if(!$r && $showerr) { |
53 | printf( _("Error opening file %s"), $filename ); |
54 | exit; |
55 | } |
56 | |
57 | } |
58 | |
59 | /* This would be for the global addressbook */ |
4272758c |
60 | if (isset($abook_global_file) && isset($abook_global_file_writeable) |
91e0dccc |
61 | && trim($abook_global_file)!=''){ |
4272758c |
62 | // Detect place of address book |
63 | if (! preg_match("/[\/\\\]/",$abook_global_file)) { |
64 | // no path chars |
65 | $abook_global_filename=$data_dir . $abook_global_file; |
66 | } elseif (preg_match("/^\/|\w:/",$abook_global_file)) { |
67 | // full path is set in options (starts with slash or x:) |
68 | $abook_global_filename=$abook_global_file; |
69 | } else { |
70 | $abook_global_filename=SM_PATH . $abook_global_file; |
71 | } |
72 | $r = $abook->add_backend('local_file',array('filename'=>$abook_global_filename, |
73 | 'name' => _("Global address book"), |
74 | 'detect_writeable' => false, |
75 | 'writeable'=> $abook_global_file_writeable)); |
81fa4801 |
76 | if (!$r && $showerr) { |
77 | echo _("Error initializing global addressbook."); |
78 | exit; |
79 | } |
80 | } |
81 | |
30e9932c |
82 | /* Load global addressbook from SQL if configured */ |
83 | if (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) { |
84 | /* Database configured */ |
85 | if (!isset($addrbook_global_table) || empty($addrbook_global_table)) { |
c1ac62d4 |
86 | $addrbook_global_table = 'global_abook'; |
30e9932c |
87 | } |
88 | $r = $abook->add_backend('database', |
c1ac62d4 |
89 | Array('dsn' => $addrbook_global_dsn, |
90 | 'owner' => 'global', |
91 | 'name' => _("Global address book"), |
92 | 'writeable' => $addrbook_global_writeable, |
93 | 'listing' => $addrbook_global_listing, |
94 | 'table' => $addrbook_global_table)); |
30e9932c |
95 | } |
96 | |
df788686 |
97 | /* |
98 | * hook allows to include different address book backends. |
99 | * plugins should extract $abook and $r from arguments |
100 | * and use same add_backend commands as above functions. |
101 | */ |
102 | $hookReturn = do_hook('abook_init', $abook, $r); |
103 | $abook = $hookReturn[1]; |
104 | $r = $hookReturn[2]; |
62f7daa5 |
105 | |
81fa4801 |
106 | if ($onlylocal) { |
107 | return $abook; |
108 | } |
109 | |
110 | /* Load configured LDAP servers (if PHP has LDAP support) */ |
111 | if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) { |
112 | reset($ldap_server); |
113 | while (list($undef,$param) = each($ldap_server)) { |
114 | if (is_array($param)) { |
115 | $r = $abook->add_backend('ldap_server', $param); |
116 | if (!$r && $showerr) { |
117 | printf( ' ' . _("Error initializing LDAP server %s:") . |
6fd95361 |
118 | "<br />\n", $param['host']); |
81fa4801 |
119 | echo ' ' . $abook->error; |
120 | exit; |
121 | } |
122 | } |
123 | } |
124 | } |
4935919f |
125 | |
81fa4801 |
126 | /* Return the initialized object */ |
127 | return $abook; |
4935919f |
128 | } |
129 | |
c1ac62d4 |
130 | /** |
131 | * Display the "new address" form |
132 | * |
133 | * Form is not closed and you must add closing form tag. |
134 | * @since 1.5.1 |
135 | * @param string $form_url form action url |
136 | * @param string $name form name |
137 | * @param string $title form title |
138 | * @param string $button form button name |
139 | * @param array $defdata values of form fields |
140 | */ |
141 | function abook_create_form($form_url,$name,$title,$button,$defdata=array()) { |
142 | global $color; |
143 | echo addForm($form_url, 'post', 'f_add'). |
144 | html_tag( 'table', |
145 | html_tag( 'tr', |
146 | html_tag( 'td', "\n". '<strong>' . $title . '</strong>' . "\n", |
147 | 'center', $color[0] |
148 | ) |
149 | ) |
150 | , 'center', '', 'width="100%"' ) ."\n"; |
151 | address_form($name, $button, $defdata); |
152 | } |
153 | |
4935919f |
154 | |
81fa4801 |
155 | /* |
156 | * Had to move this function outside of the Addressbook Class |
157 | * PHP 4.0.4 Seemed to be having problems with inline functions. |
abd74f7d |
158 | * Note: this can return now since we don't support 4.0.4 anymore. |
62f7daa5 |
159 | */ |
81fa4801 |
160 | function addressbook_cmp($a,$b) { |
4935919f |
161 | |
81fa4801 |
162 | if($a['backend'] > $b['backend']) { |
163 | return 1; |
164 | } else if($a['backend'] < $b['backend']) { |
165 | return -1; |
166 | } |
62f7daa5 |
167 | |
81fa4801 |
168 | return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1; |
4935919f |
169 | |
81fa4801 |
170 | } |
4935919f |
171 | |
c1ac62d4 |
172 | /** |
173 | * Make an input field |
174 | * @param string $label |
175 | * @param string $field |
176 | * @param string $name |
177 | * @param string $size |
178 | * @param array $values |
179 | * @param string $add |
180 | */ |
181 | function addressbook_inp_field($label, $field, $name, $size, $values, $add='') { |
182 | global $color; |
183 | $value = ( isset($values[$field]) ? $values[$field] : ''); |
184 | |
185 | if (is_array($value)) { |
186 | $td_str = addSelect($name.'['.$field.']', $value); |
187 | } else { |
188 | $td_str = addInput($name.'['.$field.']', $value, $size); |
189 | } |
190 | $td_str .= $add ; |
191 | |
192 | return html_tag( 'tr' , |
193 | html_tag( 'td', $label . ':', 'right', $color[4]) . |
194 | html_tag( 'td', $td_str, 'left', $color[4]) |
195 | ) |
196 | . "\n"; |
197 | } |
198 | |
199 | /** |
200 | * Output form to add and modify address data |
201 | */ |
202 | function address_form($name, $submittext, $values = array()) { |
203 | global $color, $squirrelmail_language; |
204 | |
205 | if ($squirrelmail_language == 'ja_JP') { |
206 | echo html_tag( 'table', |
207 | addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values, |
208 | ' <small>' . _("Must be unique") . '</small>') . |
209 | addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') . |
210 | addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') . |
211 | addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') . |
212 | addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') . |
213 | list_writable_backends($name) . |
214 | html_tag( 'tr', |
215 | html_tag( 'td', |
216 | addSubmit($submittext, $name.'[SUBMIT]'), |
217 | 'center', $color[4], 'colspan="2"') |
218 | ) |
219 | , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n"; |
220 | } else { |
221 | echo html_tag( 'table', |
222 | addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values, |
223 | ' <small>' . _("Must be unique") . '</small>') . |
224 | addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') . |
225 | addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') . |
226 | addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') . |
227 | addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') . |
228 | list_writable_backends($name) . |
229 | html_tag( 'tr', |
230 | html_tag( 'td', |
231 | addSubmit($submittext, $name.'[SUBMIT]') , |
232 | 'center', $color[4], 'colspan="2"') |
233 | ) |
234 | , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n"; |
235 | } |
236 | } |
237 | |
6ad2bbe2 |
238 | /** |
239 | * Provides list of writeable backends. |
240 | * Works only when address is added ($name='addaddr') |
241 | * @param string $name name of form |
242 | * @return string html formated backend field (select or hidden) |
243 | */ |
c1ac62d4 |
244 | function list_writable_backends($name) { |
245 | global $color, $abook; |
246 | if ( $name != 'addaddr' ) { return; } |
6ad2bbe2 |
247 | $writeable_abook = 1; |
c1ac62d4 |
248 | if ( $abook->numbackends > 1 ) { |
c1ac62d4 |
249 | $backends = $abook->get_backend_list(); |
6ad2bbe2 |
250 | $writeable_abooks=array(); |
c1ac62d4 |
251 | while (list($undef,$v) = each($backends)) { |
252 | if ($v->writeable) { |
6ad2bbe2 |
253 | // add each backend to array |
254 | $writeable_abooks[$v->bnum]=$v->sname; |
255 | // save backend number |
256 | $writeable_abook=$v->bnum; |
c1ac62d4 |
257 | } |
258 | } |
6ad2bbe2 |
259 | if (count($writeable_abooks)>1) { |
260 | // we have more than one writeable backend |
261 | $ret=addSelect('backend',$writeable_abooks,null,true); |
262 | return html_tag( 'tr', |
263 | html_tag( 'td', _("Add to:"),'right', $color[4] ) . |
264 | html_tag( 'td', $ret, 'left', $color[4] )) . "\n"; |
265 | } |
c1ac62d4 |
266 | } |
6ad2bbe2 |
267 | // Only one backend exists or is writeable. |
268 | return html_tag( 'tr', |
269 | html_tag( 'td', |
270 | addHidden('backend', $writeable_abook), |
271 | 'center', $color[4], 'colspan="2"')) . "\n"; |
c1ac62d4 |
272 | } |
273 | |
274 | /** |
275 | * Sort array by the key "name" |
276 | */ |
277 | function alistcmp($a,$b) { |
278 | $abook_sort_order=get_abook_sort(); |
279 | |
280 | switch ($abook_sort_order) { |
281 | case 0: |
282 | case 1: |
283 | $abook_sort='nickname'; |
284 | break; |
285 | case 4: |
286 | case 5: |
287 | $abook_sort='email'; |
288 | break; |
289 | case 6: |
290 | case 7: |
291 | $abook_sort='label'; |
292 | break; |
293 | case 2: |
294 | case 3: |
295 | case 8: |
296 | default: |
297 | $abook_sort='name'; |
298 | } |
299 | |
300 | if ($a['backend'] > $b['backend']) { |
301 | return 1; |
302 | } else { |
303 | if ($a['backend'] < $b['backend']) { |
304 | return -1; |
305 | } |
306 | } |
307 | |
308 | if( (($abook_sort_order+2) % 2) == 1) { |
309 | return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ? 1 : -1; |
310 | } else { |
311 | return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1; |
312 | } |
313 | } |
314 | |
315 | /** |
316 | * Address book sorting options |
317 | * |
318 | * returns address book sorting order |
319 | * @return integer book sorting options order |
320 | */ |
321 | function get_abook_sort() { |
322 | global $data_dir, $username; |
323 | |
324 | /* get sorting order */ |
325 | if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET)) { |
326 | $abook_sort_order = (int) $temp; |
327 | |
328 | if ($abook_sort_order < 0 or $abook_sort_order > 8) |
329 | $abook_sort_order=8; |
330 | |
331 | setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order); |
332 | } else { |
333 | /* get previous sorting options. default to unsorted */ |
334 | $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8); |
335 | } |
336 | |
337 | return $abook_sort_order; |
338 | } |
339 | |
340 | /** |
341 | * This function shows the address book sort button. |
342 | * |
343 | * @param integer $abook_sort_order current sort value |
344 | * @param string $alt_tag alt tag value (string visible to text only browsers) |
345 | * @param integer $Down sort value when list is sorted ascending |
346 | * @param integer $Up sort value when list is sorted descending |
347 | * @return string html code with sorting images and urls |
348 | */ |
349 | function show_abook_sort_button($abook_sort_order, $alt_tag, $Down, $Up ) { |
350 | global $form_url; |
351 | |
352 | /* Figure out which image we want to use. */ |
353 | if ($abook_sort_order != $Up && $abook_sort_order != $Down) { |
354 | $img = 'sort_none.png'; |
355 | $which = $Up; |
356 | } elseif ($abook_sort_order == $Up) { |
357 | $img = 'up_pointer.png'; |
358 | $which = $Down; |
359 | } else { |
360 | $img = 'down_pointer.png'; |
361 | $which = 8; |
362 | } |
363 | |
364 | /* Now that we have everything figured out, show the actual button. */ |
365 | return ' <a href="' . $form_url .'?abook_sort_order=' . $which |
366 | . '"><img src="../images/' . $img |
367 | . '" border="0" width="12" height="10" alt="' . $alt_tag . '" title="' |
368 | . _("Click here to change the sorting of the address list") .'" /></a>'; |
369 | } |
370 | |
4935919f |
371 | |
8f6f9ba5 |
372 | /** |
81fa4801 |
373 | * This is the main address book class that connect all the |
374 | * backends and provide services to the functions above. |
8f6f9ba5 |
375 | * @package squirrelmail |
c1ac62d4 |
376 | * @subpackage addressbook |
81fa4801 |
377 | */ |
81fa4801 |
378 | class AddressBook { |
4272758c |
379 | /** |
380 | * Enabled address book backends |
381 | * @var array |
382 | */ |
81fa4801 |
383 | var $backends = array(); |
4272758c |
384 | /** |
385 | * Number of enabled backends |
386 | * @var integer |
387 | */ |
81fa4801 |
388 | var $numbackends = 0; |
4272758c |
389 | /** |
390 | * Error messages |
391 | * @var string |
392 | */ |
81fa4801 |
393 | var $error = ''; |
4272758c |
394 | /** |
395 | * id of backend with personal address book |
396 | * @var integer |
397 | */ |
81fa4801 |
398 | var $localbackend = 0; |
4272758c |
399 | /** |
400 | * Name of backend with personal address book |
401 | * @var string |
402 | */ |
81fa4801 |
403 | var $localbackendname = ''; |
62f7daa5 |
404 | |
4272758c |
405 | /** |
406 | * Constructor function. |
407 | */ |
81fa4801 |
408 | function AddressBook() { |
c6b8b46c |
409 | $this->localbackendname = _("Personal address book"); |
81fa4801 |
410 | } |
4935919f |
411 | |
4272758c |
412 | /** |
81fa4801 |
413 | * Return an array of backends of a given type, |
414 | * or all backends if no type is specified. |
4272758c |
415 | * @param string $type backend type |
416 | * @return array list of backends |
81fa4801 |
417 | */ |
418 | function get_backend_list($type = '') { |
419 | $ret = array(); |
420 | for ($i = 1 ; $i <= $this->numbackends ; $i++) { |
421 | if (empty($type) || $type == $this->backends[$i]->btype) { |
422 | $ret[] = &$this->backends[$i]; |
423 | } |
4935919f |
424 | } |
81fa4801 |
425 | return $ret; |
426 | } |
4935919f |
427 | |
428 | |
4272758c |
429 | /* ========================== Public ======================== */ |
81fa4801 |
430 | |
4272758c |
431 | /** |
432 | * Add a new backend. |
433 | * |
434 | * @param string $backend backend name (without the abook_ prefix) |
435 | * @param mixed optional variable that is passed to the backend constructor. |
436 | * See each of the backend classes for valid parameters |
437 | * @return integer number of backends |
81fa4801 |
438 | */ |
439 | function add_backend($backend, $param = '') { |
440 | $backend_name = 'abook_' . $backend; |
441 | eval('$newback = new ' . $backend_name . '($param);'); |
442 | if(!empty($newback->error)) { |
443 | $this->error = $newback->error; |
444 | return false; |
445 | } |
446 | |
447 | $this->numbackends++; |
448 | |
449 | $newback->bnum = $this->numbackends; |
450 | $this->backends[$this->numbackends] = $newback; |
62f7daa5 |
451 | |
81fa4801 |
452 | /* Store ID of first local backend added */ |
453 | if ($this->localbackend == 0 && $newback->btype == 'local') { |
454 | $this->localbackend = $this->numbackends; |
455 | $this->localbackendname = $newback->sname; |
456 | } |
457 | |
458 | return $this->numbackends; |
459 | } |
4935919f |
460 | |
4935919f |
461 | |
4272758c |
462 | /** |
463 | * create string with name and email address |
464 | * |
62f7daa5 |
465 | * This function takes a $row array as returned by the addressbook |
2e542990 |
466 | * search and returns an e-mail address with the full name or |
467 | * nickname optionally prepended. |
4272758c |
468 | * @param array $row address book entry |
469 | * @return string email address with real name prepended |
2e542990 |
470 | */ |
2e542990 |
471 | function full_address($row) { |
1ba8cd6b |
472 | global $addrsrch_fullname, $data_dir, $username; |
20ad4fdd |
473 | $prefix = getPref($data_dir, $username, 'addrsrch_fullname'); |
474 | if (($prefix != "" || (isset($addrsrch_fullname) && |
475 | $prefix == $addrsrch_fullname)) && $prefix != 'noprefix') { |
476 | $name = ($prefix == 'nickname' ? $row['nickname'] : $row['name']); |
2e542990 |
477 | return $name . ' <' . trim($row['email']) . '>'; |
478 | } else { |
479 | return trim($row['email']); |
480 | } |
481 | } |
482 | |
4272758c |
483 | /** |
484 | * Search for entries in address books |
485 | * |
486 | * Return a list of addresses matching expression in |
487 | * all backends of a given type. |
488 | * @param string $expression search expression |
489 | * @param integer $bnum backend number. default to search in all backends |
490 | * @return array search results |
491 | */ |
81fa4801 |
492 | function search($expression, $bnum = -1) { |
493 | $ret = array(); |
494 | $this->error = ''; |
495 | |
496 | /* Search all backends */ |
497 | if ($bnum == -1) { |
498 | $sel = $this->get_backend_list(''); |
499 | $failed = 0; |
500 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
501 | $backend = &$sel[$i]; |
502 | $backend->error = ''; |
503 | $res = $backend->search($expression); |
504 | if (is_array($res)) { |
505 | $ret = array_merge($ret, $res); |
506 | } else { |
6fd95361 |
507 | $this->error .= "<br />\n" . $backend->error; |
81fa4801 |
508 | $failed++; |
75e19c7f |
509 | } |
510 | } |
4935919f |
511 | |
81fa4801 |
512 | /* Only fail if all backends failed */ |
513 | if( $failed >= sizeof( $sel ) ) { |
514 | $ret = FALSE; |
4935919f |
515 | } |
4935919f |
516 | |
81fa4801 |
517 | } else { |
4935919f |
518 | |
81fa4801 |
519 | /* Search only one backend */ |
4935919f |
520 | |
81fa4801 |
521 | $ret = $this->backends[$bnum]->search($expression); |
522 | if (!is_array($ret)) { |
6fd95361 |
523 | $this->error .= "<br />\n" . $this->backends[$bnum]->error; |
81fa4801 |
524 | $ret = FALSE; |
525 | } |
526 | } |
527 | |
528 | return( $ret ); |
4935919f |
529 | } |
530 | |
531 | |
4272758c |
532 | /** |
533 | * Sorted search |
534 | * @param string $expression search expression |
535 | * @param integer $bnum backend number. default to search in all backends |
536 | * @return array search results |
537 | */ |
81fa4801 |
538 | function s_search($expression, $bnum = -1) { |
62f7daa5 |
539 | |
81fa4801 |
540 | $ret = $this->search($expression, $bnum); |
541 | if ( is_array( $ret ) ) { |
542 | usort($ret, 'addressbook_cmp'); |
62f7daa5 |
543 | } |
81fa4801 |
544 | return $ret; |
545 | } |
4935919f |
546 | |
547 | |
4272758c |
548 | /** |
549 | * Lookup an address by alias. |
550 | * Only possible in local backends. |
551 | * @param string $alias |
552 | * @param integer backend number |
553 | * @return array lookup results. False, if not found. |
81fa4801 |
554 | */ |
555 | function lookup($alias, $bnum = -1) { |
62f7daa5 |
556 | |
81fa4801 |
557 | $ret = array(); |
62f7daa5 |
558 | |
81fa4801 |
559 | if ($bnum > -1) { |
560 | $res = $this->backends[$bnum]->lookup($alias); |
561 | if (is_array($res)) { |
562 | return $res; |
563 | } else { |
564 | $this->error = $backend->error; |
565 | return false; |
566 | } |
567 | } |
62f7daa5 |
568 | |
81fa4801 |
569 | $sel = $this->get_backend_list('local'); |
570 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
571 | $backend = &$sel[$i]; |
572 | $backend->error = ''; |
573 | $res = $backend->lookup($alias); |
574 | if (is_array($res)) { |
575 | if(!empty($res)) |
576 | return $res; |
577 | } else { |
578 | $this->error = $backend->error; |
579 | return false; |
580 | } |
581 | } |
62f7daa5 |
582 | |
81fa4801 |
583 | return $ret; |
4935919f |
584 | } |
585 | |
4935919f |
586 | |
4272758c |
587 | /** |
588 | * Return all addresses |
589 | * @param integer $bnum backend number |
590 | * @return array search results |
591 | */ |
81fa4801 |
592 | function list_addr($bnum = -1) { |
593 | $ret = array(); |
62f7daa5 |
594 | |
81fa4801 |
595 | if ($bnum == -1) { |
4272758c |
596 | $sel = $this->get_backend_list(''); |
81fa4801 |
597 | } else { |
598 | $sel = array(0 => &$this->backends[$bnum]); |
599 | } |
62f7daa5 |
600 | |
81fa4801 |
601 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
602 | $backend = &$sel[$i]; |
603 | $backend->error = ''; |
604 | $res = $backend->list_addr(); |
605 | if (is_array($res)) { |
606 | $ret = array_merge($ret, $res); |
607 | } else { |
608 | $this->error = $backend->error; |
609 | return false; |
610 | } |
611 | } |
62f7daa5 |
612 | |
81fa4801 |
613 | return $ret; |
614 | } |
4935919f |
615 | |
4272758c |
616 | /** |
91e0dccc |
617 | * Create a new address |
4272758c |
618 | * @param array $userdata added address record |
619 | * @param integer $bnum backend number |
620 | * @return integer the backend number that the/ address was added |
81fa4801 |
621 | * to, or false if it failed. |
622 | */ |
623 | function add($userdata, $bnum) { |
62f7daa5 |
624 | |
81fa4801 |
625 | /* Validate data */ |
626 | if (!is_array($userdata)) { |
627 | $this->error = _("Invalid input data"); |
628 | return false; |
629 | } |
630 | if (empty($userdata['firstname']) && empty($userdata['lastname'])) { |
631 | $this->error = _("Name is missing"); |
632 | return false; |
633 | } |
634 | if (empty($userdata['email'])) { |
635 | $this->error = _("E-mail address is missing"); |
636 | return false; |
637 | } |
638 | if (empty($userdata['nickname'])) { |
639 | $userdata['nickname'] = $userdata['email']; |
640 | } |
62f7daa5 |
641 | |
81fa4801 |
642 | if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) { |
643 | $this->error = _("Nickname contains illegal characters"); |
644 | return false; |
645 | } |
62f7daa5 |
646 | |
81fa4801 |
647 | /* Check that specified backend accept new entries */ |
648 | if (!$this->backends[$bnum]->writeable) { |
649 | $this->error = _("Addressbook is read-only"); |
650 | return false; |
651 | } |
62f7daa5 |
652 | |
81fa4801 |
653 | /* Add address to backend */ |
654 | $res = $this->backends[$bnum]->add($userdata); |
655 | if ($res) { |
656 | return $bnum; |
657 | } else { |
658 | $this->error = $this->backends[$bnum]->error; |
659 | return false; |
660 | } |
62f7daa5 |
661 | |
81fa4801 |
662 | return false; // Not reached |
663 | } /* end of add() */ |
664 | |
665 | |
4272758c |
666 | /** |
667 | * Remove the entries from address book |
91e0dccc |
668 | * @param mixed $alias entries that have to be removed. Can be string with nickname or array with list of nicknames |
4272758c |
669 | * @param integer $bnum backend number |
670 | * @return bool true if removed successfully. false if there s an error. $this->error contains error message |
81fa4801 |
671 | */ |
672 | function remove($alias, $bnum) { |
62f7daa5 |
673 | |
81fa4801 |
674 | /* Check input */ |
675 | if (empty($alias)) { |
676 | return true; |
677 | } |
62f7daa5 |
678 | |
81fa4801 |
679 | /* Convert string to single element array */ |
680 | if (!is_array($alias)) { |
681 | $alias = array(0 => $alias); |
682 | } |
62f7daa5 |
683 | |
684 | /* Check that specified backend is writable */ |
81fa4801 |
685 | if (!$this->backends[$bnum]->writeable) { |
686 | $this->error = _("Addressbook is read-only"); |
687 | return false; |
688 | } |
62f7daa5 |
689 | |
81fa4801 |
690 | /* Remove user from backend */ |
691 | $res = $this->backends[$bnum]->remove($alias); |
692 | if ($res) { |
693 | return $bnum; |
694 | } else { |
695 | $this->error = $this->backends[$bnum]->error; |
696 | return false; |
697 | } |
62f7daa5 |
698 | |
81fa4801 |
699 | return FALSE; /* Not reached */ |
700 | } /* end of remove() */ |
701 | |
702 | |
4272758c |
703 | /** |
704 | * Modify entry in address book |
705 | * @param string $alias nickname |
706 | * @param array $userdata newdata |
707 | * @param integer $bnum backend number |
81fa4801 |
708 | */ |
709 | function modify($alias, $userdata, $bnum) { |
62f7daa5 |
710 | |
81fa4801 |
711 | /* Check input */ |
712 | if (empty($alias) || !is_string($alias)) { |
713 | return true; |
714 | } |
62f7daa5 |
715 | |
81fa4801 |
716 | /* Validate data */ |
717 | if(!is_array($userdata)) { |
718 | $this->error = _("Invalid input data"); |
719 | return false; |
720 | } |
721 | if (empty($userdata['firstname']) && empty($userdata['lastname'])) { |
722 | $this->error = _("Name is missing"); |
723 | return false; |
724 | } |
725 | if (empty($userdata['email'])) { |
726 | $this->error = _("E-mail address is missing"); |
727 | return false; |
728 | } |
62f7daa5 |
729 | |
81fa4801 |
730 | if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) { |
731 | $this->error = _("Nickname contains illegal characters"); |
732 | return false; |
733 | } |
62f7daa5 |
734 | |
81fa4801 |
735 | if (empty($userdata['nickname'])) { |
736 | $userdata['nickname'] = $userdata['email']; |
737 | } |
62f7daa5 |
738 | |
739 | /* Check that specified backend is writable */ |
81fa4801 |
740 | if (!$this->backends[$bnum]->writeable) { |
741 | $this->error = _("Addressbook is read-only");; |
742 | return false; |
743 | } |
62f7daa5 |
744 | |
81fa4801 |
745 | /* Modify user in backend */ |
746 | $res = $this->backends[$bnum]->modify($alias, $userdata); |
747 | if ($res) { |
748 | return $bnum; |
749 | } else { |
750 | $this->error = $this->backends[$bnum]->error; |
751 | return false; |
752 | } |
62f7daa5 |
753 | |
81fa4801 |
754 | return FALSE; /* Not reached */ |
755 | } /* end of modify() */ |
62f7daa5 |
756 | |
757 | |
81fa4801 |
758 | } /* End of class Addressbook */ |
759 | |
8f6f9ba5 |
760 | /** |
81fa4801 |
761 | * Generic backend that all other backends extend |
8f6f9ba5 |
762 | * @package squirrelmail |
c1ac62d4 |
763 | * @subpackage addressbook |
81fa4801 |
764 | */ |
765 | class addressbook_backend { |
766 | |
767 | /* Variables that all backends must provide. */ |
4272758c |
768 | /** |
769 | * Backend type |
770 | * |
771 | * Can be 'local' or 'remote' |
772 | * @var string backend type |
773 | */ |
81fa4801 |
774 | var $btype = 'dummy'; |
4272758c |
775 | /** |
776 | * Internal backend name |
777 | * @var string |
778 | */ |
81fa4801 |
779 | var $bname = 'dummy'; |
4272758c |
780 | /** |
781 | * Displayed backend name |
782 | * @var string |
783 | */ |
81fa4801 |
784 | var $sname = 'Dummy backend'; |
62f7daa5 |
785 | |
81fa4801 |
786 | /* |
787 | * Variables common for all backends, but that |
788 | * should not be changed by the backends. |
789 | */ |
4272758c |
790 | /** |
791 | * Backend number |
792 | * @var integer |
793 | */ |
81fa4801 |
794 | var $bnum = -1; |
4272758c |
795 | /** |
796 | * Error messages |
797 | * @var string |
798 | */ |
81fa4801 |
799 | var $error = ''; |
4272758c |
800 | /** |
801 | * Writeable flag |
802 | * @var bool |
803 | */ |
81fa4801 |
804 | var $writeable = false; |
62f7daa5 |
805 | |
4272758c |
806 | /** |
807 | * Set error message |
808 | * @param string $string error message |
809 | * @return bool |
810 | */ |
81fa4801 |
811 | function set_error($string) { |
812 | $this->error = '[' . $this->sname . '] ' . $string; |
813 | return false; |
814 | } |
62f7daa5 |
815 | |
816 | |
81fa4801 |
817 | /* ========================== Public ======================== */ |
62f7daa5 |
818 | |
4272758c |
819 | /** |
820 | * Search for entries in backend |
821 | * @param string $expression |
822 | * @return bool |
823 | */ |
81fa4801 |
824 | function search($expression) { |
825 | $this->set_error('search not implemented'); |
826 | return false; |
827 | } |
62f7daa5 |
828 | |
4272758c |
829 | /** |
830 | * Find entry in backend by alias |
831 | * @param string $alias name used for id |
832 | * @return bool |
833 | */ |
81fa4801 |
834 | function lookup($alias) { |
835 | $this->set_error('lookup not implemented'); |
836 | return false; |
a10110a5 |
837 | } |
62f7daa5 |
838 | |
4272758c |
839 | /** |
840 | * List all entries in backend |
841 | * @return bool |
842 | */ |
81fa4801 |
843 | function list_addr() { |
844 | $this->set_error('list_addr not implemented'); |
845 | return false; |
846 | } |
62f7daa5 |
847 | |
4272758c |
848 | /** |
849 | * Add entry to backend |
850 | * @param array userdata |
851 | * @return bool |
852 | */ |
81fa4801 |
853 | function add($userdata) { |
854 | $this->set_error('add not implemented'); |
855 | return false; |
856 | } |
62f7daa5 |
857 | |
4272758c |
858 | /** |
859 | * Remove entry from backend |
860 | * @param string $alias name used for id |
861 | * @return bool |
862 | */ |
81fa4801 |
863 | function remove($alias) { |
864 | $this->set_error('delete not implemented'); |
865 | return false; |
866 | } |
62f7daa5 |
867 | |
4272758c |
868 | /** |
869 | * Modify entry in backend |
870 | * @param string $alias name used for id |
871 | * @param array $newuserdata new data |
872 | * @return bool |
873 | */ |
81fa4801 |
874 | function modify($alias, $newuserdata) { |
875 | $this->set_error('modify not implemented'); |
876 | return false; |
877 | } |
81fa4801 |
878 | } |
879 | |
0419106e |
880 | /* |
881 | PHP 5 requires that the class be made first, which seems rather |
882 | logical, and should have been the way it was generated the first time. |
883 | */ |
884 | |
885 | require_once(SM_PATH . 'functions/abook_local_file.php'); |
886 | require_once(SM_PATH . 'functions/abook_ldap_server.php'); |
887 | |
0419106e |
888 | /* Only load database backend if database is configured */ |
62f7daa5 |
889 | if((isset($addrbook_dsn) && !empty($addrbook_dsn)) || |
30e9932c |
890 | (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) ) { |
0419106e |
891 | include_once(SM_PATH . 'functions/abook_database.php'); |
892 | } |
893 | |
df788686 |
894 | /* |
895 | * hook allows adding different address book classes. |
896 | * class must follow address book class coding standards. |
897 | * |
898 | * see addressbook_backend class and functions/abook_*.php files. |
899 | */ |
900 | do_hook('abook_add_class'); |
0419106e |
901 | |
796f91d9 |
902 | ?> |