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