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 | |
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. |
30 | * @param boolean $showerr display any address book init errors. html page header |
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 | } |
72 | |
73 | } |
74 | |
e59a9c41 |
75 | /* Global file based addressbook */ |
76 | if (isset($abook_global_file) && |
77 | isset($abook_global_file_writeable) && |
78 | isset($abook_global_file_listing) && |
79 | trim($abook_global_file)!=''){ |
80 | |
4272758c |
81 | // Detect place of address book |
82 | if (! preg_match("/[\/\\\]/",$abook_global_file)) { |
e4a468a7 |
83 | /* no path chars, address book stored in data directory |
84 | * make sure that there is a slash between data directory |
85 | * and address book file name |
86 | */ |
87 | $abook_global_filename=$data_dir |
88 | . ((substr($data_dir, -1) != '/') ? '/' : '') |
89 | . $abook_global_file; |
4272758c |
90 | } elseif (preg_match("/^\/|\w:/",$abook_global_file)) { |
91 | // full path is set in options (starts with slash or x:) |
92 | $abook_global_filename=$abook_global_file; |
93 | } else { |
94 | $abook_global_filename=SM_PATH . $abook_global_file; |
95 | } |
e59a9c41 |
96 | |
4272758c |
97 | $r = $abook->add_backend('local_file',array('filename'=>$abook_global_filename, |
98 | 'name' => _("Global address book"), |
99 | 'detect_writeable' => false, |
e59a9c41 |
100 | 'writeable'=> $abook_global_file_writeable, |
101 | 'listing' => $abook_global_file_listing)); |
04875ae0 |
102 | |
103 | /* global abook init error is not fatal. add error message and continue */ |
81fa4801 |
104 | if (!$r && $showerr) { |
0d4096aa |
105 | if ($abook_init_error!='') $abook_init_error.="<br />\n"; |
106 | $abook_init_error.=_("Error initializing global addressbook.") . "<br />\n" . $abook->error; |
81fa4801 |
107 | } |
108 | } |
109 | |
30e9932c |
110 | /* Load global addressbook from SQL if configured */ |
111 | if (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) { |
112 | /* Database configured */ |
113 | if (!isset($addrbook_global_table) || empty($addrbook_global_table)) { |
c1ac62d4 |
114 | $addrbook_global_table = 'global_abook'; |
30e9932c |
115 | } |
116 | $r = $abook->add_backend('database', |
c1ac62d4 |
117 | Array('dsn' => $addrbook_global_dsn, |
118 | 'owner' => 'global', |
119 | 'name' => _("Global address book"), |
120 | 'writeable' => $addrbook_global_writeable, |
121 | 'listing' => $addrbook_global_listing, |
122 | 'table' => $addrbook_global_table)); |
0d4096aa |
123 | /* global abook init error is not fatal. add error message and continue */ |
124 | if (!$r && $showerr) { |
125 | if ($abook_init_error!='') $abook_init_error.="<br />\n"; |
126 | $abook_init_error.=_("Error initializing global addressbook.") . "<br />\n" . $abook->error; |
127 | } |
30e9932c |
128 | } |
129 | |
df788686 |
130 | /* |
131 | * hook allows to include different address book backends. |
132 | * plugins should extract $abook and $r from arguments |
133 | * and use same add_backend commands as above functions. |
e4a468a7 |
134 | * @since 1.5.1 |
df788686 |
135 | */ |
136 | $hookReturn = do_hook('abook_init', $abook, $r); |
137 | $abook = $hookReturn[1]; |
138 | $r = $hookReturn[2]; |
62f7daa5 |
139 | |
0d4096aa |
140 | if (! $onlylocal) { |
141 | /* Load configured LDAP servers (if PHP has LDAP support) */ |
142 | if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) { |
143 | reset($ldap_server); |
144 | while (list($undef,$param) = each($ldap_server)) { |
145 | if (is_array($param)) { |
146 | $r = $abook->add_backend('ldap_server', $param); |
147 | if (!$r && $showerr) { |
148 | if ($abook_init_error!='') $abook_init_error.="<br />\n"; |
149 | $abook_init_error.=sprintf(_("Error initializing LDAP server %s:") . |
150 | "<br />\n", $param['host']); |
151 | $abook_init_error.= $abook->error; |
152 | } |
81fa4801 |
153 | } |
154 | } |
0d4096aa |
155 | } // end of ldap server init |
156 | } // end of remote abook backend init |
4935919f |
157 | |
04875ae0 |
158 | /** |
159 | * display address book init errors. |
160 | */ |
161 | if ($abook_init_error!='' && $showerr) { |
162 | error_box($abook_init_error,$color); |
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 | |
81fa4801 |
193 | /* |
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 { |
4272758c |
417 | /** |
418 | * Enabled address book backends |
419 | * @var array |
420 | */ |
81fa4801 |
421 | var $backends = array(); |
4272758c |
422 | /** |
423 | * Number of enabled backends |
424 | * @var integer |
425 | */ |
81fa4801 |
426 | var $numbackends = 0; |
4272758c |
427 | /** |
428 | * Error messages |
429 | * @var string |
430 | */ |
81fa4801 |
431 | var $error = ''; |
4272758c |
432 | /** |
433 | * id of backend with personal address book |
434 | * @var integer |
435 | */ |
81fa4801 |
436 | var $localbackend = 0; |
4272758c |
437 | /** |
438 | * Name of backend with personal address book |
439 | * @var string |
440 | */ |
81fa4801 |
441 | var $localbackendname = ''; |
62f7daa5 |
442 | |
4272758c |
443 | /** |
444 | * Constructor function. |
445 | */ |
81fa4801 |
446 | function AddressBook() { |
c6b8b46c |
447 | $this->localbackendname = _("Personal address book"); |
81fa4801 |
448 | } |
4935919f |
449 | |
4272758c |
450 | /** |
81fa4801 |
451 | * Return an array of backends of a given type, |
452 | * or all backends if no type is specified. |
4272758c |
453 | * @param string $type backend type |
454 | * @return array list of backends |
81fa4801 |
455 | */ |
456 | function get_backend_list($type = '') { |
457 | $ret = array(); |
458 | for ($i = 1 ; $i <= $this->numbackends ; $i++) { |
459 | if (empty($type) || $type == $this->backends[$i]->btype) { |
460 | $ret[] = &$this->backends[$i]; |
461 | } |
4935919f |
462 | } |
81fa4801 |
463 | return $ret; |
464 | } |
4935919f |
465 | |
466 | |
4272758c |
467 | /* ========================== Public ======================== */ |
81fa4801 |
468 | |
4272758c |
469 | /** |
470 | * Add a new backend. |
471 | * |
472 | * @param string $backend backend name (without the abook_ prefix) |
473 | * @param mixed optional variable that is passed to the backend constructor. |
474 | * See each of the backend classes for valid parameters |
475 | * @return integer number of backends |
81fa4801 |
476 | */ |
477 | function add_backend($backend, $param = '') { |
478 | $backend_name = 'abook_' . $backend; |
479 | eval('$newback = new ' . $backend_name . '($param);'); |
480 | if(!empty($newback->error)) { |
481 | $this->error = $newback->error; |
482 | return false; |
483 | } |
484 | |
485 | $this->numbackends++; |
486 | |
487 | $newback->bnum = $this->numbackends; |
488 | $this->backends[$this->numbackends] = $newback; |
62f7daa5 |
489 | |
81fa4801 |
490 | /* Store ID of first local backend added */ |
491 | if ($this->localbackend == 0 && $newback->btype == 'local') { |
492 | $this->localbackend = $this->numbackends; |
493 | $this->localbackendname = $newback->sname; |
494 | } |
495 | |
496 | return $this->numbackends; |
497 | } |
4935919f |
498 | |
4935919f |
499 | |
4272758c |
500 | /** |
501 | * create string with name and email address |
502 | * |
62f7daa5 |
503 | * This function takes a $row array as returned by the addressbook |
2e542990 |
504 | * search and returns an e-mail address with the full name or |
505 | * nickname optionally prepended. |
4272758c |
506 | * @param array $row address book entry |
507 | * @return string email address with real name prepended |
2e542990 |
508 | */ |
2e542990 |
509 | function full_address($row) { |
1ba8cd6b |
510 | global $addrsrch_fullname, $data_dir, $username; |
20ad4fdd |
511 | $prefix = getPref($data_dir, $username, 'addrsrch_fullname'); |
512 | if (($prefix != "" || (isset($addrsrch_fullname) && |
513 | $prefix == $addrsrch_fullname)) && $prefix != 'noprefix') { |
514 | $name = ($prefix == 'nickname' ? $row['nickname'] : $row['name']); |
2e542990 |
515 | return $name . ' <' . trim($row['email']) . '>'; |
516 | } else { |
517 | return trim($row['email']); |
518 | } |
519 | } |
520 | |
4272758c |
521 | /** |
522 | * Search for entries in address books |
523 | * |
524 | * Return a list of addresses matching expression in |
525 | * all backends of a given type. |
526 | * @param string $expression search expression |
527 | * @param integer $bnum backend number. default to search in all backends |
528 | * @return array search results |
529 | */ |
81fa4801 |
530 | function search($expression, $bnum = -1) { |
531 | $ret = array(); |
532 | $this->error = ''; |
533 | |
534 | /* Search all backends */ |
535 | if ($bnum == -1) { |
536 | $sel = $this->get_backend_list(''); |
537 | $failed = 0; |
538 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
539 | $backend = &$sel[$i]; |
540 | $backend->error = ''; |
541 | $res = $backend->search($expression); |
542 | if (is_array($res)) { |
543 | $ret = array_merge($ret, $res); |
544 | } else { |
6fd95361 |
545 | $this->error .= "<br />\n" . $backend->error; |
81fa4801 |
546 | $failed++; |
75e19c7f |
547 | } |
548 | } |
4935919f |
549 | |
81fa4801 |
550 | /* Only fail if all backends failed */ |
551 | if( $failed >= sizeof( $sel ) ) { |
552 | $ret = FALSE; |
4935919f |
553 | } |
4935919f |
554 | |
81fa4801 |
555 | } else { |
4935919f |
556 | |
81fa4801 |
557 | /* Search only one backend */ |
4935919f |
558 | |
81fa4801 |
559 | $ret = $this->backends[$bnum]->search($expression); |
560 | if (!is_array($ret)) { |
6fd95361 |
561 | $this->error .= "<br />\n" . $this->backends[$bnum]->error; |
81fa4801 |
562 | $ret = FALSE; |
563 | } |
564 | } |
565 | |
566 | return( $ret ); |
4935919f |
567 | } |
568 | |
569 | |
4272758c |
570 | /** |
571 | * Sorted search |
572 | * @param string $expression search expression |
573 | * @param integer $bnum backend number. default to search in all backends |
574 | * @return array search results |
575 | */ |
81fa4801 |
576 | function s_search($expression, $bnum = -1) { |
62f7daa5 |
577 | |
81fa4801 |
578 | $ret = $this->search($expression, $bnum); |
579 | if ( is_array( $ret ) ) { |
580 | usort($ret, 'addressbook_cmp'); |
62f7daa5 |
581 | } |
81fa4801 |
582 | return $ret; |
583 | } |
4935919f |
584 | |
585 | |
4272758c |
586 | /** |
587 | * Lookup an address by alias. |
588 | * Only possible in local backends. |
589 | * @param string $alias |
590 | * @param integer backend number |
591 | * @return array lookup results. False, if not found. |
81fa4801 |
592 | */ |
593 | function lookup($alias, $bnum = -1) { |
62f7daa5 |
594 | |
81fa4801 |
595 | $ret = array(); |
62f7daa5 |
596 | |
81fa4801 |
597 | if ($bnum > -1) { |
598 | $res = $this->backends[$bnum]->lookup($alias); |
599 | if (is_array($res)) { |
600 | return $res; |
601 | } else { |
602 | $this->error = $backend->error; |
603 | return false; |
604 | } |
605 | } |
62f7daa5 |
606 | |
81fa4801 |
607 | $sel = $this->get_backend_list('local'); |
608 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
609 | $backend = &$sel[$i]; |
610 | $backend->error = ''; |
611 | $res = $backend->lookup($alias); |
612 | if (is_array($res)) { |
613 | if(!empty($res)) |
614 | return $res; |
615 | } else { |
616 | $this->error = $backend->error; |
617 | return false; |
618 | } |
619 | } |
62f7daa5 |
620 | |
81fa4801 |
621 | return $ret; |
4935919f |
622 | } |
623 | |
4935919f |
624 | |
4272758c |
625 | /** |
626 | * Return all addresses |
627 | * @param integer $bnum backend number |
628 | * @return array search results |
629 | */ |
81fa4801 |
630 | function list_addr($bnum = -1) { |
631 | $ret = array(); |
62f7daa5 |
632 | |
81fa4801 |
633 | if ($bnum == -1) { |
4272758c |
634 | $sel = $this->get_backend_list(''); |
81fa4801 |
635 | } else { |
636 | $sel = array(0 => &$this->backends[$bnum]); |
637 | } |
62f7daa5 |
638 | |
81fa4801 |
639 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
640 | $backend = &$sel[$i]; |
641 | $backend->error = ''; |
642 | $res = $backend->list_addr(); |
643 | if (is_array($res)) { |
644 | $ret = array_merge($ret, $res); |
645 | } else { |
646 | $this->error = $backend->error; |
647 | return false; |
648 | } |
649 | } |
62f7daa5 |
650 | |
81fa4801 |
651 | return $ret; |
652 | } |
4935919f |
653 | |
4272758c |
654 | /** |
91e0dccc |
655 | * Create a new address |
4272758c |
656 | * @param array $userdata added address record |
657 | * @param integer $bnum backend number |
658 | * @return integer the backend number that the/ address was added |
81fa4801 |
659 | * to, or false if it failed. |
660 | */ |
661 | function add($userdata, $bnum) { |
62f7daa5 |
662 | |
81fa4801 |
663 | /* Validate data */ |
664 | if (!is_array($userdata)) { |
665 | $this->error = _("Invalid input data"); |
666 | return false; |
667 | } |
668 | if (empty($userdata['firstname']) && empty($userdata['lastname'])) { |
669 | $this->error = _("Name is missing"); |
670 | return false; |
671 | } |
672 | if (empty($userdata['email'])) { |
673 | $this->error = _("E-mail address is missing"); |
674 | return false; |
675 | } |
676 | if (empty($userdata['nickname'])) { |
677 | $userdata['nickname'] = $userdata['email']; |
678 | } |
62f7daa5 |
679 | |
81fa4801 |
680 | if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) { |
681 | $this->error = _("Nickname contains illegal characters"); |
682 | return false; |
683 | } |
62f7daa5 |
684 | |
81fa4801 |
685 | /* Check that specified backend accept new entries */ |
686 | if (!$this->backends[$bnum]->writeable) { |
687 | $this->error = _("Addressbook is read-only"); |
688 | return false; |
689 | } |
62f7daa5 |
690 | |
81fa4801 |
691 | /* Add address to backend */ |
692 | $res = $this->backends[$bnum]->add($userdata); |
693 | if ($res) { |
694 | return $bnum; |
695 | } else { |
696 | $this->error = $this->backends[$bnum]->error; |
697 | return false; |
698 | } |
62f7daa5 |
699 | |
81fa4801 |
700 | return false; // Not reached |
701 | } /* end of add() */ |
702 | |
703 | |
4272758c |
704 | /** |
705 | * Remove the entries from address book |
91e0dccc |
706 | * @param mixed $alias entries that have to be removed. Can be string with nickname or array with list of nicknames |
4272758c |
707 | * @param integer $bnum backend number |
708 | * @return bool true if removed successfully. false if there s an error. $this->error contains error message |
81fa4801 |
709 | */ |
710 | function remove($alias, $bnum) { |
62f7daa5 |
711 | |
81fa4801 |
712 | /* Check input */ |
713 | if (empty($alias)) { |
714 | return true; |
715 | } |
62f7daa5 |
716 | |
81fa4801 |
717 | /* Convert string to single element array */ |
718 | if (!is_array($alias)) { |
719 | $alias = array(0 => $alias); |
720 | } |
62f7daa5 |
721 | |
722 | /* Check that specified backend is writable */ |
81fa4801 |
723 | if (!$this->backends[$bnum]->writeable) { |
724 | $this->error = _("Addressbook is read-only"); |
725 | return false; |
726 | } |
62f7daa5 |
727 | |
81fa4801 |
728 | /* Remove user from backend */ |
729 | $res = $this->backends[$bnum]->remove($alias); |
730 | if ($res) { |
731 | return $bnum; |
732 | } else { |
733 | $this->error = $this->backends[$bnum]->error; |
734 | return false; |
735 | } |
62f7daa5 |
736 | |
81fa4801 |
737 | return FALSE; /* Not reached */ |
738 | } /* end of remove() */ |
739 | |
740 | |
4272758c |
741 | /** |
742 | * Modify entry in address book |
743 | * @param string $alias nickname |
744 | * @param array $userdata newdata |
745 | * @param integer $bnum backend number |
81fa4801 |
746 | */ |
747 | function modify($alias, $userdata, $bnum) { |
62f7daa5 |
748 | |
81fa4801 |
749 | /* Check input */ |
750 | if (empty($alias) || !is_string($alias)) { |
751 | return true; |
752 | } |
62f7daa5 |
753 | |
81fa4801 |
754 | /* Validate data */ |
755 | if(!is_array($userdata)) { |
756 | $this->error = _("Invalid input data"); |
757 | return false; |
758 | } |
759 | if (empty($userdata['firstname']) && empty($userdata['lastname'])) { |
760 | $this->error = _("Name is missing"); |
761 | return false; |
762 | } |
763 | if (empty($userdata['email'])) { |
764 | $this->error = _("E-mail address is missing"); |
765 | return false; |
766 | } |
62f7daa5 |
767 | |
81fa4801 |
768 | if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) { |
769 | $this->error = _("Nickname contains illegal characters"); |
770 | return false; |
771 | } |
62f7daa5 |
772 | |
81fa4801 |
773 | if (empty($userdata['nickname'])) { |
774 | $userdata['nickname'] = $userdata['email']; |
775 | } |
62f7daa5 |
776 | |
777 | /* Check that specified backend is writable */ |
81fa4801 |
778 | if (!$this->backends[$bnum]->writeable) { |
779 | $this->error = _("Addressbook is read-only");; |
780 | return false; |
781 | } |
62f7daa5 |
782 | |
81fa4801 |
783 | /* Modify user in backend */ |
784 | $res = $this->backends[$bnum]->modify($alias, $userdata); |
785 | if ($res) { |
786 | return $bnum; |
787 | } else { |
788 | $this->error = $this->backends[$bnum]->error; |
789 | return false; |
790 | } |
62f7daa5 |
791 | |
81fa4801 |
792 | return FALSE; /* Not reached */ |
793 | } /* end of modify() */ |
62f7daa5 |
794 | |
795 | |
81fa4801 |
796 | } /* End of class Addressbook */ |
797 | |
8f6f9ba5 |
798 | /** |
81fa4801 |
799 | * Generic backend that all other backends extend |
8f6f9ba5 |
800 | * @package squirrelmail |
c1ac62d4 |
801 | * @subpackage addressbook |
81fa4801 |
802 | */ |
803 | class addressbook_backend { |
804 | |
805 | /* Variables that all backends must provide. */ |
4272758c |
806 | /** |
807 | * Backend type |
808 | * |
809 | * Can be 'local' or 'remote' |
810 | * @var string backend type |
811 | */ |
81fa4801 |
812 | var $btype = 'dummy'; |
4272758c |
813 | /** |
814 | * Internal backend name |
815 | * @var string |
816 | */ |
81fa4801 |
817 | var $bname = 'dummy'; |
4272758c |
818 | /** |
819 | * Displayed backend name |
820 | * @var string |
821 | */ |
81fa4801 |
822 | var $sname = 'Dummy backend'; |
62f7daa5 |
823 | |
81fa4801 |
824 | /* |
825 | * Variables common for all backends, but that |
826 | * should not be changed by the backends. |
827 | */ |
4272758c |
828 | /** |
829 | * Backend number |
830 | * @var integer |
831 | */ |
81fa4801 |
832 | var $bnum = -1; |
4272758c |
833 | /** |
834 | * Error messages |
835 | * @var string |
836 | */ |
81fa4801 |
837 | var $error = ''; |
4272758c |
838 | /** |
839 | * Writeable flag |
840 | * @var bool |
841 | */ |
81fa4801 |
842 | var $writeable = false; |
62f7daa5 |
843 | |
4272758c |
844 | /** |
845 | * Set error message |
846 | * @param string $string error message |
847 | * @return bool |
848 | */ |
81fa4801 |
849 | function set_error($string) { |
850 | $this->error = '[' . $this->sname . '] ' . $string; |
851 | return false; |
852 | } |
62f7daa5 |
853 | |
854 | |
81fa4801 |
855 | /* ========================== Public ======================== */ |
62f7daa5 |
856 | |
4272758c |
857 | /** |
858 | * Search for entries in backend |
859 | * @param string $expression |
860 | * @return bool |
861 | */ |
81fa4801 |
862 | function search($expression) { |
863 | $this->set_error('search not implemented'); |
864 | return false; |
865 | } |
62f7daa5 |
866 | |
4272758c |
867 | /** |
868 | * Find entry in backend by alias |
869 | * @param string $alias name used for id |
870 | * @return bool |
871 | */ |
81fa4801 |
872 | function lookup($alias) { |
873 | $this->set_error('lookup not implemented'); |
874 | return false; |
a10110a5 |
875 | } |
62f7daa5 |
876 | |
4272758c |
877 | /** |
878 | * List all entries in backend |
879 | * @return bool |
880 | */ |
81fa4801 |
881 | function list_addr() { |
882 | $this->set_error('list_addr not implemented'); |
883 | return false; |
884 | } |
62f7daa5 |
885 | |
4272758c |
886 | /** |
887 | * Add entry to backend |
888 | * @param array userdata |
889 | * @return bool |
890 | */ |
81fa4801 |
891 | function add($userdata) { |
892 | $this->set_error('add not implemented'); |
893 | return false; |
894 | } |
62f7daa5 |
895 | |
4272758c |
896 | /** |
897 | * Remove entry from backend |
898 | * @param string $alias name used for id |
899 | * @return bool |
900 | */ |
81fa4801 |
901 | function remove($alias) { |
902 | $this->set_error('delete not implemented'); |
903 | return false; |
904 | } |
62f7daa5 |
905 | |
4272758c |
906 | /** |
907 | * Modify entry in backend |
908 | * @param string $alias name used for id |
909 | * @param array $newuserdata new data |
910 | * @return bool |
911 | */ |
81fa4801 |
912 | function modify($alias, $newuserdata) { |
913 | $this->set_error('modify not implemented'); |
914 | return false; |
915 | } |
81fa4801 |
916 | } |
917 | |
0419106e |
918 | /* |
919 | PHP 5 requires that the class be made first, which seems rather |
920 | logical, and should have been the way it was generated the first time. |
921 | */ |
922 | |
923 | require_once(SM_PATH . 'functions/abook_local_file.php'); |
924 | require_once(SM_PATH . 'functions/abook_ldap_server.php'); |
925 | |
0419106e |
926 | /* Only load database backend if database is configured */ |
62f7daa5 |
927 | if((isset($addrbook_dsn) && !empty($addrbook_dsn)) || |
30e9932c |
928 | (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) ) { |
0419106e |
929 | include_once(SM_PATH . 'functions/abook_database.php'); |
930 | } |
931 | |
df788686 |
932 | /* |
933 | * hook allows adding different address book classes. |
934 | * class must follow address book class coding standards. |
935 | * |
936 | * see addressbook_backend class and functions/abook_*.php files. |
e4a468a7 |
937 | * @since 1.5.1 |
df788686 |
938 | */ |
939 | do_hook('abook_add_class'); |
0419106e |
940 | |
796f91d9 |
941 | ?> |