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