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