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