removing trailing ?> from function scripts
[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' ,
828c58f1 236 html_tag( 'td', '<label for="'.$name.'_'.$field.'_'.'">' .
237 $label . '</label>:', 'right', $color[4]) .
c1ac62d4 238 html_tag( 'td', $td_str, 'left', $color[4])
239 )
240 . "\n";
241}
242
243/**
244 * Output form to add and modify address data
245 */
246function address_form($name, $submittext, $values = array()) {
247 global $color, $squirrelmail_language;
248
249 if ($squirrelmail_language == 'ja_JP') {
250 echo html_tag( 'table',
251 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
252 ' <small>' . _("Must be unique") . '</small>') .
253 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
254 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
255 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
256 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
257 list_writable_backends($name) .
258 html_tag( 'tr',
259 html_tag( 'td',
260 addSubmit($submittext, $name.'[SUBMIT]'),
261 'center', $color[4], 'colspan="2"')
262 )
263 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
264 } else {
265 echo html_tag( 'table',
266 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
267 ' <small>' . _("Must be unique") . '</small>') .
268 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
269 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
270 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
271 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
272 list_writable_backends($name) .
273 html_tag( 'tr',
274 html_tag( 'td',
275 addSubmit($submittext, $name.'[SUBMIT]') ,
276 'center', $color[4], 'colspan="2"')
277 )
278 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
279 }
280}
281
6ad2bbe2 282/**
283 * Provides list of writeable backends.
284 * Works only when address is added ($name='addaddr')
285 * @param string $name name of form
286 * @return string html formated backend field (select or hidden)
287 */
c1ac62d4 288function list_writable_backends($name) {
289 global $color, $abook;
290 if ( $name != 'addaddr' ) { return; }
6ad2bbe2 291 $writeable_abook = 1;
c1ac62d4 292 if ( $abook->numbackends > 1 ) {
c1ac62d4 293 $backends = $abook->get_backend_list();
6ad2bbe2 294 $writeable_abooks=array();
c1ac62d4 295 while (list($undef,$v) = each($backends)) {
296 if ($v->writeable) {
6ad2bbe2 297 // add each backend to array
298 $writeable_abooks[$v->bnum]=$v->sname;
299 // save backend number
300 $writeable_abook=$v->bnum;
c1ac62d4 301 }
302 }
6ad2bbe2 303 if (count($writeable_abooks)>1) {
304 // we have more than one writeable backend
305 $ret=addSelect('backend',$writeable_abooks,null,true);
306 return html_tag( 'tr',
307 html_tag( 'td', _("Add to:"),'right', $color[4] ) .
308 html_tag( 'td', $ret, 'left', $color[4] )) . "\n";
309 }
c1ac62d4 310 }
6ad2bbe2 311 // Only one backend exists or is writeable.
312 return html_tag( 'tr',
313 html_tag( 'td',
314 addHidden('backend', $writeable_abook),
315 'center', $color[4], 'colspan="2"')) . "\n";
c1ac62d4 316}
317
318/**
319 * Sort array by the key "name"
320 */
321function alistcmp($a,$b) {
322 $abook_sort_order=get_abook_sort();
323
324 switch ($abook_sort_order) {
325 case 0:
326 case 1:
327 $abook_sort='nickname';
328 break;
329 case 4:
330 case 5:
331 $abook_sort='email';
332 break;
333 case 6:
334 case 7:
335 $abook_sort='label';
336 break;
337 case 2:
338 case 3:
339 case 8:
340 default:
341 $abook_sort='name';
342 }
343
344 if ($a['backend'] > $b['backend']) {
345 return 1;
346 } else {
347 if ($a['backend'] < $b['backend']) {
348 return -1;
349 }
350 }
351
352 if( (($abook_sort_order+2) % 2) == 1) {
353 return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ? 1 : -1;
354 } else {
355 return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1;
356 }
357}
358
359/**
360 * Address book sorting options
361 *
362 * returns address book sorting order
363 * @return integer book sorting options order
364 */
365function get_abook_sort() {
366 global $data_dir, $username;
367
368 /* get sorting order */
369 if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET)) {
370 $abook_sort_order = (int) $temp;
371
372 if ($abook_sort_order < 0 or $abook_sort_order > 8)
373 $abook_sort_order=8;
374
375 setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order);
376 } else {
377 /* get previous sorting options. default to unsorted */
378 $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8);
379 }
380
381 return $abook_sort_order;
382}
383
384/**
385 * This function shows the address book sort button.
386 *
387 * @param integer $abook_sort_order current sort value
388 * @param string $alt_tag alt tag value (string visible to text only browsers)
389 * @param integer $Down sort value when list is sorted ascending
390 * @param integer $Up sort value when list is sorted descending
391 * @return string html code with sorting images and urls
392 */
393function show_abook_sort_button($abook_sort_order, $alt_tag, $Down, $Up ) {
635f7200 394 global $form_url, $icon_theme_path;
c1ac62d4 395
396 /* Figure out which image we want to use. */
397 if ($abook_sort_order != $Up && $abook_sort_order != $Down) {
398 $img = 'sort_none.png';
de783fdf 399 $text_icon = '&#9723;'; // U+25FB WHITE MEDIUM SQUARE
c1ac62d4 400 $which = $Up;
401 } elseif ($abook_sort_order == $Up) {
402 $img = 'up_pointer.png';
de783fdf 403 $text_icon = '&#8679;'; // U+21E7 UPWARDS WHITE ARROW
c1ac62d4 404 $which = $Down;
405 } else {
406 $img = 'down_pointer.png';
de783fdf 407 $text_icon = '&#8681;'; // U+21E9 DOWNWARDS WHITE ARROW
c1ac62d4 408 $which = 8;
409 }
410
635f7200 411 /* Now that we have everything figured out, show the actual button. */
de783fdf 412 return '&nbsp;<a href="' . $form_url .'?abook_sort_order=' . $which .
413 '" style="text-decoration:none" title="'.$alt_tag.'">' .
414 getIcon($icon_theme_path, $img, $text_icon, $alt_tag) .
635f7200 415 '</a>';
c1ac62d4 416}
417
4935919f 418
8f6f9ba5 419/**
81fa4801 420 * This is the main address book class that connect all the
421 * backends and provide services to the functions above.
8f6f9ba5 422 * @package squirrelmail
c1ac62d4 423 * @subpackage addressbook
81fa4801 424 */
81fa4801 425class AddressBook {
4272758c 426 /**
427 * Enabled address book backends
428 * @var array
429 */
81fa4801 430 var $backends = array();
4272758c 431 /**
432 * Number of enabled backends
433 * @var integer
434 */
81fa4801 435 var $numbackends = 0;
4272758c 436 /**
437 * Error messages
438 * @var string
439 */
81fa4801 440 var $error = '';
4272758c 441 /**
442 * id of backend with personal address book
443 * @var integer
444 */
81fa4801 445 var $localbackend = 0;
4272758c 446 /**
447 * Name of backend with personal address book
448 * @var string
449 */
81fa4801 450 var $localbackendname = '';
7b0ea860 451 /**
452 * Controls use of 'extra' field
202bcbcc 453 *
454 * Extra field can be used to add link to form, which allows
455 * to modify all fields supported by backend. This is the only field
7b0ea860 456 * that is not sanitized with htmlspecialchars. Backends MUST make
457 * sure that field data is sanitized and displayed correctly inside
458 * table cell. Use of html formating in other address book fields is
202bcbcc 459 * not allowed. Backends that don't return 'extra' row in address book
7b0ea860 460 * data should not modify this object property.
461 * @var boolean
462 * @since 1.5.1
463 */
464 var $add_extra_field = false;
62f7daa5 465
4272758c 466 /**
467 * Constructor function.
468 */
81fa4801 469 function AddressBook() {
c6b8b46c 470 $this->localbackendname = _("Personal address book");
81fa4801 471 }
4935919f 472
4272758c 473 /**
81fa4801 474 * Return an array of backends of a given type,
475 * or all backends if no type is specified.
4272758c 476 * @param string $type backend type
477 * @return array list of backends
81fa4801 478 */
479 function get_backend_list($type = '') {
480 $ret = array();
481 for ($i = 1 ; $i <= $this->numbackends ; $i++) {
482 if (empty($type) || $type == $this->backends[$i]->btype) {
483 $ret[] = &$this->backends[$i];
484 }
4935919f 485 }
81fa4801 486 return $ret;
487 }
4935919f 488
489
4272758c 490 /* ========================== Public ======================== */
81fa4801 491
4272758c 492 /**
493 * Add a new backend.
494 *
495 * @param string $backend backend name (without the abook_ prefix)
496 * @param mixed optional variable that is passed to the backend constructor.
497 * See each of the backend classes for valid parameters
498 * @return integer number of backends
81fa4801 499 */
500 function add_backend($backend, $param = '') {
202bcbcc 501 static $backend_classes;
502 if (!isset($backend_classes)) {
503 $backend_classes = array();
504 }
505 if (!isset($backend_classes[$backend])) {
506 /**
507 * Support backend provided by plugins. Plugin function must
508 * return an associative array with as key the backend name ($backend)
509 * and as value the file including the path containing the backend class.
510 * i.e.: $aBackend = array('backend_template' => SM_PATH . 'plugins/abook_backend_template/functions.php')
511 *
512 * NB: Because the backend files are included from within this function they DO NOT have access to
513 * vars in the global scope. This function is the global scope for the included backend !!!
514 */
515 $aBackend = do_hook('abook_add_class');
516 if (isset($aBackend) && is_array($aBackend) && isset($aBackend[$backend])) {
517 require_once($aBackend[$backend]);
518 } else {
519 require_once(SM_PATH . 'functions/abook_'.$backend.'.php');
520 }
521 $backend_classes[$backend] = true;
522 }
81fa4801 523 $backend_name = 'abook_' . $backend;
202bcbcc 524 $newback = new $backend_name($param);
525 //eval('$newback = new ' . $backend_name . '($param);');
81fa4801 526 if(!empty($newback->error)) {
527 $this->error = $newback->error;
528 return false;
529 }
530
531 $this->numbackends++;
532
533 $newback->bnum = $this->numbackends;
534 $this->backends[$this->numbackends] = $newback;
62f7daa5 535
81fa4801 536 /* Store ID of first local backend added */
537 if ($this->localbackend == 0 && $newback->btype == 'local') {
538 $this->localbackend = $this->numbackends;
539 $this->localbackendname = $newback->sname;
540 }
541
542 return $this->numbackends;
543 }
4935919f 544
4935919f 545
4272758c 546 /**
547 * create string with name and email address
548 *
62f7daa5 549 * This function takes a $row array as returned by the addressbook
2e542990 550 * search and returns an e-mail address with the full name or
551 * nickname optionally prepended.
4272758c 552 * @param array $row address book entry
553 * @return string email address with real name prepended
2e542990 554 */
2e542990 555 function full_address($row) {
1ba8cd6b 556 global $addrsrch_fullname, $data_dir, $username;
20ad4fdd 557 $prefix = getPref($data_dir, $username, 'addrsrch_fullname');
558 if (($prefix != "" || (isset($addrsrch_fullname) &&
559 $prefix == $addrsrch_fullname)) && $prefix != 'noprefix') {
560 $name = ($prefix == 'nickname' ? $row['nickname'] : $row['name']);
2e542990 561 return $name . ' <' . trim($row['email']) . '>';
562 } else {
563 return trim($row['email']);
564 }
565 }
566
4272758c 567 /**
568 * Search for entries in address books
569 *
570 * Return a list of addresses matching expression in
571 * all backends of a given type.
572 * @param string $expression search expression
573 * @param integer $bnum backend number. default to search in all backends
574 * @return array search results
575 */
81fa4801 576 function search($expression, $bnum = -1) {
577 $ret = array();
578 $this->error = '';
579
580 /* Search all backends */
581 if ($bnum == -1) {
582 $sel = $this->get_backend_list('');
583 $failed = 0;
584 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
585 $backend = &$sel[$i];
586 $backend->error = '';
587 $res = $backend->search($expression);
588 if (is_array($res)) {
589 $ret = array_merge($ret, $res);
590 } else {
35235328 591 $this->error .= "\n" . $backend->error;
81fa4801 592 $failed++;
75e19c7f 593 }
594 }
4935919f 595
81fa4801 596 /* Only fail if all backends failed */
597 if( $failed >= sizeof( $sel ) ) {
598 $ret = FALSE;
4935919f 599 }
4935919f 600
81fa4801 601 } else {
4935919f 602
81fa4801 603 /* Search only one backend */
4935919f 604
81fa4801 605 $ret = $this->backends[$bnum]->search($expression);
606 if (!is_array($ret)) {
35235328 607 $this->error .= "\n" . $this->backends[$bnum]->error;
81fa4801 608 $ret = FALSE;
609 }
610 }
611
612 return( $ret );
4935919f 613 }
614
615
4272758c 616 /**
617 * Sorted search
618 * @param string $expression search expression
619 * @param integer $bnum backend number. default to search in all backends
620 * @return array search results
621 */
81fa4801 622 function s_search($expression, $bnum = -1) {
62f7daa5 623
81fa4801 624 $ret = $this->search($expression, $bnum);
625 if ( is_array( $ret ) ) {
626 usort($ret, 'addressbook_cmp');
62f7daa5 627 }
81fa4801 628 return $ret;
629 }
4935919f 630
631
4272758c 632 /**
633 * Lookup an address by alias.
634 * Only possible in local backends.
635 * @param string $alias
636 * @param integer backend number
637 * @return array lookup results. False, if not found.
81fa4801 638 */
639 function lookup($alias, $bnum = -1) {
62f7daa5 640
81fa4801 641 $ret = array();
62f7daa5 642
81fa4801 643 if ($bnum > -1) {
644 $res = $this->backends[$bnum]->lookup($alias);
645 if (is_array($res)) {
646 return $res;
647 } else {
35235328 648 $this->error = $this->backends[$bnum]->error;
81fa4801 649 return false;
650 }
651 }
62f7daa5 652
81fa4801 653 $sel = $this->get_backend_list('local');
654 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
655 $backend = &$sel[$i];
656 $backend->error = '';
657 $res = $backend->lookup($alias);
658 if (is_array($res)) {
659 if(!empty($res))
660 return $res;
661 } else {
35235328 662 $this->error = $backend->error;
81fa4801 663 return false;
664 }
665 }
62f7daa5 666
81fa4801 667 return $ret;
4935919f 668 }
669
4935919f 670
4272758c 671 /**
672 * Return all addresses
673 * @param integer $bnum backend number
674 * @return array search results
675 */
81fa4801 676 function list_addr($bnum = -1) {
677 $ret = array();
62f7daa5 678
81fa4801 679 if ($bnum == -1) {
4272758c 680 $sel = $this->get_backend_list('');
81fa4801 681 } else {
682 $sel = array(0 => &$this->backends[$bnum]);
683 }
62f7daa5 684
81fa4801 685 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
686 $backend = &$sel[$i];
687 $backend->error = '';
688 $res = $backend->list_addr();
689 if (is_array($res)) {
690 $ret = array_merge($ret, $res);
691 } else {
35235328 692 $this->error = $backend->error;
81fa4801 693 return false;
694 }
695 }
62f7daa5 696
81fa4801 697 return $ret;
698 }
4935919f 699
4272758c 700 /**
91e0dccc 701 * Create a new address
4272758c 702 * @param array $userdata added address record
703 * @param integer $bnum backend number
704 * @return integer the backend number that the/ address was added
81fa4801 705 * to, or false if it failed.
706 */
707 function add($userdata, $bnum) {
62f7daa5 708
81fa4801 709 /* Validate data */
710 if (!is_array($userdata)) {
711 $this->error = _("Invalid input data");
712 return false;
713 }
714 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
715 $this->error = _("Name is missing");
716 return false;
717 }
718 if (empty($userdata['email'])) {
719 $this->error = _("E-mail address is missing");
720 return false;
721 }
722 if (empty($userdata['nickname'])) {
723 $userdata['nickname'] = $userdata['email'];
724 }
62f7daa5 725
35235328 726 /* Blocks use of space, :, |, #, " and ! in nickname */
81fa4801 727 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
728 $this->error = _("Nickname contains illegal characters");
729 return false;
730 }
62f7daa5 731
81fa4801 732 /* Check that specified backend accept new entries */
733 if (!$this->backends[$bnum]->writeable) {
35235328 734 $this->error = _("Address book is read-only");
81fa4801 735 return false;
736 }
62f7daa5 737
81fa4801 738 /* Add address to backend */
739 $res = $this->backends[$bnum]->add($userdata);
740 if ($res) {
741 return $bnum;
742 } else {
35235328 743 $this->error = $this->backends[$bnum]->error;
81fa4801 744 return false;
745 }
62f7daa5 746
81fa4801 747 return false; // Not reached
748 } /* end of add() */
749
750
4272758c 751 /**
752 * Remove the entries from address book
91e0dccc 753 * @param mixed $alias entries that have to be removed. Can be string with nickname or array with list of nicknames
4272758c 754 * @param integer $bnum backend number
755 * @return bool true if removed successfully. false if there s an error. $this->error contains error message
81fa4801 756 */
757 function remove($alias, $bnum) {
62f7daa5 758
81fa4801 759 /* Check input */
760 if (empty($alias)) {
761 return true;
762 }
62f7daa5 763
81fa4801 764 /* Convert string to single element array */
765 if (!is_array($alias)) {
766 $alias = array(0 => $alias);
767 }
62f7daa5 768
769 /* Check that specified backend is writable */
81fa4801 770 if (!$this->backends[$bnum]->writeable) {
35235328 771 $this->error = _("Address book is read-only");
81fa4801 772 return false;
773 }
62f7daa5 774
81fa4801 775 /* Remove user from backend */
776 $res = $this->backends[$bnum]->remove($alias);
777 if ($res) {
778 return $bnum;
779 } else {
35235328 780 $this->error = $this->backends[$bnum]->error;
81fa4801 781 return false;
782 }
62f7daa5 783
81fa4801 784 return FALSE; /* Not reached */
785 } /* end of remove() */
786
787
4272758c 788 /**
789 * Modify entry in address book
790 * @param string $alias nickname
791 * @param array $userdata newdata
792 * @param integer $bnum backend number
81fa4801 793 */
794 function modify($alias, $userdata, $bnum) {
62f7daa5 795
81fa4801 796 /* Check input */
797 if (empty($alias) || !is_string($alias)) {
798 return true;
799 }
62f7daa5 800
81fa4801 801 /* Validate data */
802 if(!is_array($userdata)) {
803 $this->error = _("Invalid input data");
804 return false;
805 }
806 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
807 $this->error = _("Name is missing");
808 return false;
809 }
810 if (empty($userdata['email'])) {
811 $this->error = _("E-mail address is missing");
812 return false;
813 }
62f7daa5 814
81fa4801 815 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
816 $this->error = _("Nickname contains illegal characters");
817 return false;
818 }
62f7daa5 819
81fa4801 820 if (empty($userdata['nickname'])) {
821 $userdata['nickname'] = $userdata['email'];
822 }
62f7daa5 823
824 /* Check that specified backend is writable */
81fa4801 825 if (!$this->backends[$bnum]->writeable) {
35235328 826 $this->error = _("Address book is read-only");;
81fa4801 827 return false;
828 }
62f7daa5 829
81fa4801 830 /* Modify user in backend */
831 $res = $this->backends[$bnum]->modify($alias, $userdata);
832 if ($res) {
833 return $bnum;
834 } else {
35235328 835 $this->error = $this->backends[$bnum]->error;
81fa4801 836 return false;
837 }
62f7daa5 838
81fa4801 839 return FALSE; /* Not reached */
840 } /* end of modify() */
62f7daa5 841
842
81fa4801 843} /* End of class Addressbook */
844
8f6f9ba5 845/**
81fa4801 846 * Generic backend that all other backends extend
8f6f9ba5 847 * @package squirrelmail
c1ac62d4 848 * @subpackage addressbook
81fa4801 849 */
850class addressbook_backend {
851
852 /* Variables that all backends must provide. */
4272758c 853 /**
854 * Backend type
855 *
856 * Can be 'local' or 'remote'
857 * @var string backend type
858 */
81fa4801 859 var $btype = 'dummy';
4272758c 860 /**
861 * Internal backend name
862 * @var string
863 */
81fa4801 864 var $bname = 'dummy';
4272758c 865 /**
866 * Displayed backend name
867 * @var string
868 */
81fa4801 869 var $sname = 'Dummy backend';
62f7daa5 870
81fa4801 871 /*
872 * Variables common for all backends, but that
873 * should not be changed by the backends.
874 */
4272758c 875 /**
876 * Backend number
877 * @var integer
878 */
81fa4801 879 var $bnum = -1;
4272758c 880 /**
881 * Error messages
882 * @var string
883 */
81fa4801 884 var $error = '';
4272758c 885 /**
886 * Writeable flag
887 * @var bool
888 */
81fa4801 889 var $writeable = false;
62f7daa5 890
4272758c 891 /**
892 * Set error message
893 * @param string $string error message
894 * @return bool
895 */
81fa4801 896 function set_error($string) {
897 $this->error = '[' . $this->sname . '] ' . $string;
898 return false;
899 }
62f7daa5 900
901
81fa4801 902 /* ========================== Public ======================== */
62f7daa5 903
4272758c 904 /**
905 * Search for entries in backend
327e2d96 906 *
202bcbcc 907 * Working backend should support use of wildcards. * symbol
327e2d96 908 * should match one or more symbols. ? symbol should match any
202bcbcc 909 * single symbol.
4272758c 910 * @param string $expression
911 * @return bool
912 */
81fa4801 913 function search($expression) {
35235328 914 $this->set_error('search is not implemented');
81fa4801 915 return false;
916 }
62f7daa5 917
4272758c 918 /**
919 * Find entry in backend by alias
920 * @param string $alias name used for id
921 * @return bool
922 */
81fa4801 923 function lookup($alias) {
35235328 924 $this->set_error('lookup is not implemented');
81fa4801 925 return false;
a10110a5 926 }
62f7daa5 927
4272758c 928 /**
929 * List all entries in backend
327e2d96 930 *
931 * Working backend should provide this function or at least
932 * dummy function that returns empty array.
4272758c 933 * @return bool
934 */
81fa4801 935 function list_addr() {
35235328 936 $this->set_error('list_addr is not implemented');
81fa4801 937 return false;
938 }
62f7daa5 939
4272758c 940 /**
941 * Add entry to backend
942 * @param array userdata
943 * @return bool
944 */
81fa4801 945 function add($userdata) {
35235328 946 $this->set_error('add is not implemented');
81fa4801 947 return false;
948 }
62f7daa5 949
4272758c 950 /**
951 * Remove entry from backend
952 * @param string $alias name used for id
953 * @return bool
954 */
81fa4801 955 function remove($alias) {
35235328 956 $this->set_error('delete is not implemented');
81fa4801 957 return false;
958 }
62f7daa5 959
4272758c 960 /**
961 * Modify entry in backend
962 * @param string $alias name used for id
963 * @param array $newuserdata new data
964 * @return bool
965 */
81fa4801 966 function modify($alias, $newuserdata) {
35235328 967 $this->set_error('modify is not implemented');
81fa4801 968 return false;
969 }
35235328 970
971 /**
972 * Creates full name from given name and surname
973 *
974 * Handles name order differences
975 * @param string $firstname given name
976 * @param string $lastname surname
977 * @return string full name
978 * @since 1.5.2
979 */
980 function fullname($firstname,$lastname) {
981 global $squirrelmail_language;
982 if ($squirrelmail_language=='ja_JP') {
983 return trim($lastname . ' ' . $firstname);
984 } else {
985 return trim($firstname . ' ' . $lastname);
986 }
987 }
81fa4801 988}