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