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