avoid E_STRICT errors
[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) {
56196737 488 global $data_dir, $username;
489 $addrsrch_fullname = getPref($data_dir, $username, 'addrsrch_fullname');
490 if ($addrsrch_fullname == 'fullname')
491 return $row['name'] . ' <' . trim($row['email']) . '>';
492 else if ($addrsrch_fullname == 'nickname')
493 return $row['nickname'] . ' <' . trim($row['email']) . '>';
494 else // "noprefix"
2e542990 495 return trim($row['email']);
2e542990 496 }
497
4272758c 498 /**
499 * Search for entries in address books
500 *
501 * Return a list of addresses matching expression in
502 * all backends of a given type.
503 * @param string $expression search expression
504 * @param integer $bnum backend number. default to search in all backends
505 * @return array search results
506 */
81fa4801 507 function search($expression, $bnum = -1) {
508 $ret = array();
509 $this->error = '';
510
511 /* Search all backends */
512 if ($bnum == -1) {
513 $sel = $this->get_backend_list('');
514 $failed = 0;
515 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
516 $backend = &$sel[$i];
517 $backend->error = '';
518 $res = $backend->search($expression);
519 if (is_array($res)) {
520 $ret = array_merge($ret, $res);
521 } else {
35235328 522 $this->error .= "\n" . $backend->error;
81fa4801 523 $failed++;
75e19c7f 524 }
525 }
4935919f 526
81fa4801 527 /* Only fail if all backends failed */
528 if( $failed >= sizeof( $sel ) ) {
529 $ret = FALSE;
4935919f 530 }
4935919f 531
01066e58 532 } elseif (! isset($this->backends[$bnum])) {
533 /* make sure that backend exists */
534 $this->error = _("Unknown address book backend");
535 $ret = false;
536 } else {
4935919f 537
81fa4801 538 /* Search only one backend */
4935919f 539
81fa4801 540 $ret = $this->backends[$bnum]->search($expression);
541 if (!is_array($ret)) {
35235328 542 $this->error .= "\n" . $this->backends[$bnum]->error;
81fa4801 543 $ret = FALSE;
544 }
545 }
546
547 return( $ret );
4935919f 548 }
549
550
4272758c 551 /**
552 * Sorted search
553 * @param string $expression search expression
554 * @param integer $bnum backend number. default to search in all backends
555 * @return array search results
556 */
81fa4801 557 function s_search($expression, $bnum = -1) {
62f7daa5 558
81fa4801 559 $ret = $this->search($expression, $bnum);
560 if ( is_array( $ret ) ) {
561 usort($ret, 'addressbook_cmp');
62f7daa5 562 }
81fa4801 563 return $ret;
564 }
4935919f 565
566
4272758c 567 /**
568 * Lookup an address by alias.
569 * Only possible in local backends.
570 * @param string $alias
571 * @param integer backend number
572 * @return array lookup results. False, if not found.
81fa4801 573 */
574 function lookup($alias, $bnum = -1) {
62f7daa5 575
81fa4801 576 $ret = array();
62f7daa5 577
81fa4801 578 if ($bnum > -1) {
01066e58 579 if (!isset($this->backends[$bnum])) {
580 $this->error = _("Unknown address book backend");
581 return false;
582 }
81fa4801 583 $res = $this->backends[$bnum]->lookup($alias);
584 if (is_array($res)) {
585 return $res;
586 } else {
35235328 587 $this->error = $this->backends[$bnum]->error;
81fa4801 588 return false;
589 }
590 }
62f7daa5 591
81fa4801 592 $sel = $this->get_backend_list('local');
593 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
594 $backend = &$sel[$i];
595 $backend->error = '';
596 $res = $backend->lookup($alias);
597 if (is_array($res)) {
598 if(!empty($res))
599 return $res;
600 } else {
35235328 601 $this->error = $backend->error;
81fa4801 602 return false;
603 }
604 }
62f7daa5 605
81fa4801 606 return $ret;
4935919f 607 }
608
4935919f 609
4272758c 610 /**
611 * Return all addresses
612 * @param integer $bnum backend number
5b1c13d3 613 * @return mixed array with search results or boolean false on error.
4272758c 614 */
81fa4801 615 function list_addr($bnum = -1) {
616 $ret = array();
62f7daa5 617
81fa4801 618 if ($bnum == -1) {
4272758c 619 $sel = $this->get_backend_list('');
01066e58 620 } elseif (! isset($this->backends[$bnum])) {
621 /* make sure that backend exists */
622 $this->error = _("Unknown address book backend");
623 $ret = false;
81fa4801 624 } else {
625 $sel = array(0 => &$this->backends[$bnum]);
626 }
62f7daa5 627
81fa4801 628 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
629 $backend = &$sel[$i];
630 $backend->error = '';
631 $res = $backend->list_addr();
632 if (is_array($res)) {
633 $ret = array_merge($ret, $res);
634 } else {
35235328 635 $this->error = $backend->error;
81fa4801 636 return false;
637 }
638 }
62f7daa5 639
81fa4801 640 return $ret;
641 }
4935919f 642
4272758c 643 /**
91e0dccc 644 * Create a new address
4272758c 645 * @param array $userdata added address record
646 * @param integer $bnum backend number
647 * @return integer the backend number that the/ address was added
81fa4801 648 * to, or false if it failed.
649 */
650 function add($userdata, $bnum) {
62f7daa5 651
81fa4801 652 /* Validate data */
653 if (!is_array($userdata)) {
654 $this->error = _("Invalid input data");
655 return false;
656 }
657 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
658 $this->error = _("Name is missing");
659 return false;
660 }
661 if (empty($userdata['email'])) {
662 $this->error = _("E-mail address is missing");
663 return false;
664 }
665 if (empty($userdata['nickname'])) {
666 $userdata['nickname'] = $userdata['email'];
667 }
62f7daa5 668
35235328 669 /* Blocks use of space, :, |, #, " and ! in nickname */
81fa4801 670 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
671 $this->error = _("Nickname contains illegal characters");
672 return false;
673 }
62f7daa5 674
01066e58 675 /* make sure that backend exists */
676 if (! isset($this->backends[$bnum])) {
677 $this->error = _("Unknown address book backend");
678 return false;
679 }
680
81fa4801 681 /* Check that specified backend accept new entries */
682 if (!$this->backends[$bnum]->writeable) {
35235328 683 $this->error = _("Address book is read-only");
81fa4801 684 return false;
685 }
62f7daa5 686
81fa4801 687 /* Add address to backend */
688 $res = $this->backends[$bnum]->add($userdata);
689 if ($res) {
690 return $bnum;
691 } else {
35235328 692 $this->error = $this->backends[$bnum]->error;
81fa4801 693 return false;
694 }
62f7daa5 695
81fa4801 696 return false; // Not reached
697 } /* end of add() */
698
699
4272758c 700 /**
701 * Remove the entries from address book
91e0dccc 702 * @param mixed $alias entries that have to be removed. Can be string with nickname or array with list of nicknames
4272758c 703 * @param integer $bnum backend number
704 * @return bool true if removed successfully. false if there s an error. $this->error contains error message
81fa4801 705 */
706 function remove($alias, $bnum) {
62f7daa5 707
81fa4801 708 /* Check input */
709 if (empty($alias)) {
710 return true;
711 }
62f7daa5 712
81fa4801 713 /* Convert string to single element array */
714 if (!is_array($alias)) {
715 $alias = array(0 => $alias);
716 }
62f7daa5 717
01066e58 718 /* make sure that backend exists */
719 if (! isset($this->backends[$bnum])) {
720 $this->error = _("Unknown address book backend");
721 return false;
722 }
723
62f7daa5 724 /* Check that specified backend is writable */
81fa4801 725 if (!$this->backends[$bnum]->writeable) {
35235328 726 $this->error = _("Address book is read-only");
81fa4801 727 return false;
728 }
62f7daa5 729
81fa4801 730 /* Remove user from backend */
731 $res = $this->backends[$bnum]->remove($alias);
732 if ($res) {
733 return $bnum;
734 } else {
35235328 735 $this->error = $this->backends[$bnum]->error;
81fa4801 736 return false;
737 }
62f7daa5 738
81fa4801 739 return FALSE; /* Not reached */
740 } /* end of remove() */
741
742
4272758c 743 /**
744 * Modify entry in address book
745 * @param string $alias nickname
746 * @param array $userdata newdata
747 * @param integer $bnum backend number
81fa4801 748 */
749 function modify($alias, $userdata, $bnum) {
62f7daa5 750
81fa4801 751 /* Check input */
752 if (empty($alias) || !is_string($alias)) {
753 return true;
754 }
62f7daa5 755
81fa4801 756 /* Validate data */
757 if(!is_array($userdata)) {
758 $this->error = _("Invalid input data");
759 return false;
760 }
761 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
762 $this->error = _("Name is missing");
763 return false;
764 }
765 if (empty($userdata['email'])) {
766 $this->error = _("E-mail address is missing");
767 return false;
768 }
62f7daa5 769
81fa4801 770 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
771 $this->error = _("Nickname contains illegal characters");
772 return false;
773 }
62f7daa5 774
81fa4801 775 if (empty($userdata['nickname'])) {
776 $userdata['nickname'] = $userdata['email'];
777 }
62f7daa5 778
01066e58 779 /* make sure that backend exists */
780 if (! isset($this->backends[$bnum])) {
781 $this->error = _("Unknown address book backend");
782 return false;
783 }
784
62f7daa5 785 /* Check that specified backend is writable */
81fa4801 786 if (!$this->backends[$bnum]->writeable) {
35235328 787 $this->error = _("Address book is read-only");;
81fa4801 788 return false;
789 }
62f7daa5 790
81fa4801 791 /* Modify user in backend */
792 $res = $this->backends[$bnum]->modify($alias, $userdata);
793 if ($res) {
794 return $bnum;
795 } else {
35235328 796 $this->error = $this->backends[$bnum]->error;
81fa4801 797 return false;
798 }
62f7daa5 799
81fa4801 800 return FALSE; /* Not reached */
801 } /* end of modify() */
62f7daa5 802
803
81fa4801 804} /* End of class Addressbook */
805
8f6f9ba5 806/**
81fa4801 807 * Generic backend that all other backends extend
8f6f9ba5 808 * @package squirrelmail
c1ac62d4 809 * @subpackage addressbook
81fa4801 810 */
811class addressbook_backend {
812
813 /* Variables that all backends must provide. */
4272758c 814 /**
815 * Backend type
816 *
817 * Can be 'local' or 'remote'
818 * @var string backend type
819 */
81fa4801 820 var $btype = 'dummy';
4272758c 821 /**
822 * Internal backend name
823 * @var string
824 */
81fa4801 825 var $bname = 'dummy';
4272758c 826 /**
827 * Displayed backend name
828 * @var string
829 */
81fa4801 830 var $sname = 'Dummy backend';
62f7daa5 831
81fa4801 832 /*
833 * Variables common for all backends, but that
834 * should not be changed by the backends.
835 */
4272758c 836 /**
837 * Backend number
838 * @var integer
839 */
81fa4801 840 var $bnum = -1;
4272758c 841 /**
842 * Error messages
843 * @var string
844 */
81fa4801 845 var $error = '';
4272758c 846 /**
847 * Writeable flag
848 * @var bool
849 */
81fa4801 850 var $writeable = false;
62f7daa5 851
4272758c 852 /**
853 * Set error message
854 * @param string $string error message
855 * @return bool
856 */
81fa4801 857 function set_error($string) {
858 $this->error = '[' . $this->sname . '] ' . $string;
859 return false;
860 }
62f7daa5 861
862
81fa4801 863 /* ========================== Public ======================== */
62f7daa5 864
4272758c 865 /**
866 * Search for entries in backend
327e2d96 867 *
202bcbcc 868 * Working backend should support use of wildcards. * symbol
327e2d96 869 * should match one or more symbols. ? symbol should match any
202bcbcc 870 * single symbol.
4272758c 871 * @param string $expression
872 * @return bool
873 */
81fa4801 874 function search($expression) {
35235328 875 $this->set_error('search is not implemented');
81fa4801 876 return false;
877 }
62f7daa5 878
4272758c 879 /**
880 * Find entry in backend by alias
881 * @param string $alias name used for id
882 * @return bool
883 */
81fa4801 884 function lookup($alias) {
35235328 885 $this->set_error('lookup is not implemented');
81fa4801 886 return false;
a10110a5 887 }
62f7daa5 888
4272758c 889 /**
890 * List all entries in backend
327e2d96 891 *
892 * Working backend should provide this function or at least
893 * dummy function that returns empty array.
4272758c 894 * @return bool
895 */
81fa4801 896 function list_addr() {
35235328 897 $this->set_error('list_addr is not implemented');
81fa4801 898 return false;
899 }
62f7daa5 900
4272758c 901 /**
902 * Add entry to backend
903 * @param array userdata
904 * @return bool
905 */
81fa4801 906 function add($userdata) {
35235328 907 $this->set_error('add is not implemented');
81fa4801 908 return false;
909 }
62f7daa5 910
4272758c 911 /**
912 * Remove entry from backend
913 * @param string $alias name used for id
914 * @return bool
915 */
81fa4801 916 function remove($alias) {
35235328 917 $this->set_error('delete is not implemented');
81fa4801 918 return false;
919 }
62f7daa5 920
4272758c 921 /**
922 * Modify entry in backend
923 * @param string $alias name used for id
924 * @param array $newuserdata new data
925 * @return bool
926 */
81fa4801 927 function modify($alias, $newuserdata) {
35235328 928 $this->set_error('modify is not implemented');
81fa4801 929 return false;
930 }
35235328 931
932 /**
933 * Creates full name from given name and surname
934 *
5b1c13d3 935 * Handles name order differences. Function always runs in SquirrelMail gettext domain.
936 * Plugins don't have to switch domains before calling this function.
35235328 937 * @param string $firstname given name
938 * @param string $lastname surname
939 * @return string full name
940 * @since 1.5.2
941 */
942 function fullname($firstname,$lastname) {
5b1c13d3 943 /**
944 * i18n: allows to control fullname layout in address book listing
945 * first %s is for first name, second %s is for last name.
946 * Translate it to '%2$s %1$s', if surname must be displayed first in your language.
947 * Please note that variables can be set to empty string and extra formating
948 * (for example '%2$s, %1$s' as in 'Smith, John') might break. Use it only for
949 * setting name and surname order. scripts will remove all prepended and appended
950 * whitespace.
951 */
952 return trim(sprintf(dgettext('squirrelmail',"%s %s"),$firstname,$lastname));
35235328 953 }
81fa4801 954}