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