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