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