Allow multiple addresses in one abook entry (separate with commas), although we HIGHL...
[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-2007 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 * Since 1.5.2, the plugin arguments are passed inside an array
129 * and by reference, so plugins hooking in here need to accept arguments
130 * in an array and change those values as needed instead of returning
131 * the changed values.
132 */
133 $temp = array(&$abook, &$r, &$onlylocal);
134 do_hook('abook_init', $temp);
135 if (!$r && $showerr) {
136 if ($abook_init_error!='') $abook_init_error.="\n";
137 $abook_init_error.=_("Error initializing other address books.") . "\n" . $abook->error;
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 * Constructs the "new address" form
173 *
174 * NOTE! The form is not closed - the caller
175 * must add the closing form tag itself.
176 *
177 * @since 1.5.1
178 *
179 * @param string $form_url Form action url
180 * @param string $name Form name
181 * @param string $title Form title
182 * @param string $button Form button name
183 * @param int $backend The current backend being displayed
184 * @param array $defdata Values of form fields
185 *
186 * @return string The desired address form display code
187 *
188 */
189 function abook_create_form($form_url, $name, $title, $button,
190 $backend, $defdata=array()) {
191
192 global $oTemplate;
193
194 $output = addForm($form_url, 'post', 'f_add');
195
196 if ($button == _("Update address")) {
197 $edit = true;
198 $backends = NULL;
199 } else {
200 $edit = false;
201 $backends = getWritableBackends();
202 }
203
204 $fields = array (
205 'nickname' => 'NickName',
206 'firstname' => 'FirstName',
207 'lastname' => 'LastName',
208 'email' => 'Email',
209 'label' => 'Info',
210 );
211 $values = array();
212 foreach ($fields as $sqm=>$template) {
213 $values[$template] = isset($defdata[$sqm]) ? $defdata[$sqm] : '';
214 }
215
216 $oTemplate->assign('writable_backends', $backends);
217 $oTemplate->assign('values', $values);
218 $oTemplate->assign('edit', $edit);
219 $oTemplate->assign('current_backend', $backend);
220
221 $output .= $oTemplate->fetch('addrbook_addedit.tpl');
222
223 return $output;
224 }
225
226
227 /**
228 * Had to move this function outside of the Addressbook Class
229 * PHP 4.0.4 Seemed to be having problems with inline functions.
230 * Note: this can return now since we don't support 4.0.4 anymore.
231 */
232 function addressbook_cmp($a,$b) {
233
234 if($a['backend'] > $b['backend']) {
235 return 1;
236 } else if($a['backend'] < $b['backend']) {
237 return -1;
238 }
239
240 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
241
242 }
243
244 /**
245 * Retrieve a list of writable backends
246 * @since 1.5.2
247 */
248 function getWritableBackends () {
249 global $abook;
250
251 $write = array();
252 $backends = $abook->get_backend_list();
253 while (list($undef,$v) = each($backends)) {
254 if ($v->writeable) {
255 $write[$v->bnum]=$v->sname;
256 }
257 }
258
259 return $write;
260 }
261
262 /**
263 * Sort array by the key "name"
264 */
265 function alistcmp($a,$b) {
266 $abook_sort_order=get_abook_sort();
267
268 switch ($abook_sort_order) {
269 case 0:
270 case 1:
271 $abook_sort='nickname';
272 break;
273 case 4:
274 case 5:
275 $abook_sort='email';
276 break;
277 case 6:
278 case 7:
279 $abook_sort='label';
280 break;
281 case 2:
282 case 3:
283 case 8:
284 default:
285 $abook_sort='name';
286 }
287
288 if ($a['backend'] > $b['backend']) {
289 return 1;
290 } else {
291 if ($a['backend'] < $b['backend']) {
292 return -1;
293 }
294 }
295
296 if( (($abook_sort_order+2) % 2) == 1) {
297 return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ? 1 : -1;
298 } else {
299 return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1;
300 }
301 }
302
303 /**
304 * Address book sorting options
305 *
306 * returns address book sorting order
307 * @return integer book sorting options order
308 */
309 function get_abook_sort() {
310 global $data_dir, $username;
311
312 /* get sorting order */
313 if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET)) {
314 $abook_sort_order = (int) $temp;
315
316 if ($abook_sort_order < 0 or $abook_sort_order > 8)
317 $abook_sort_order=8;
318
319 setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order);
320 } else {
321 /* get previous sorting options. default to unsorted */
322 $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8);
323 }
324
325 return $abook_sort_order;
326 }
327
328 /**
329 * This function shows the address book sort button.
330 *
331 * @param integer $abook_sort_order Current sort value
332 * @param string $alt_tag The alt tag value (string
333 * visible to text only browsers)
334 * @param integer $Down Sort value when list is sorted
335 * ascending
336 * @param integer $Up Sort value when list is sorted
337 * descending
338 * @param array $uri_extra Any additional parameters to add
339 * to the button's link, as an
340 * associative array of key/value pairs
341 * (OPTIONAL; default none)
342 *
343 * @return string html code with sorting images and urls
344 *
345 */
346 function show_abook_sort_button($abook_sort_order, $alt_tag,
347 $Down, $Up, $uri_extra=array() ) {
348
349 global $form_url, $icon_theme_path;
350
351 /* Figure out which image we want to use. */
352 if ($abook_sort_order != $Up && $abook_sort_order != $Down) {
353 $img = 'sort_none.png';
354 $text_icon = '&#9723;'; // U+25FB WHITE MEDIUM SQUARE
355 $which = $Up;
356 } elseif ($abook_sort_order == $Up) {
357 $img = 'up_pointer.png';
358 $text_icon = '&#8679;'; // U+21E7 UPWARDS WHITE ARROW
359 $which = $Down;
360 } else {
361 $img = 'down_pointer.png';
362 $text_icon = '&#8681;'; // U+21E9 DOWNWARDS WHITE ARROW
363 $which = 8;
364 }
365
366 $uri_extra['abook_sort_order'] = $which;
367 $uri = set_uri_vars($form_url, $uri_extra, FALSE);
368
369 /* Now that we have everything figured out, show the actual button. */
370 return create_hyperlink($uri,
371 getIcon($icon_theme_path, $img, $text_icon, $alt_tag),
372 '', '', '', '', '',
373 array('style' => 'text-decoration:none',
374 'title' => $alt_tag),
375 FALSE);
376 }
377
378
379 /**
380 * This is the main address book class that connect all the
381 * backends and provide services to the functions above.
382 * @package squirrelmail
383 * @subpackage addressbook
384 */
385 class AddressBook {
386 /**
387 * Enabled address book backends
388 * @var array
389 */
390 var $backends = array();
391 /**
392 * Number of enabled backends
393 * @var integer
394 */
395 var $numbackends = 0;
396 /**
397 * Error messages
398 * @var string
399 */
400 var $error = '';
401 /**
402 * id of backend with personal address book
403 * @var integer
404 */
405 var $localbackend = 0;
406 /**
407 * Name of backend with personal address book
408 * @var string
409 */
410 var $localbackendname = '';
411 /**
412 * Controls use of 'extra' field
413 *
414 * Extra field can be used to add link to form, which allows
415 * to modify all fields supported by backend. This is the only field
416 * that is not sanitized with htmlspecialchars. Backends MUST make
417 * sure that field data is sanitized and displayed correctly inside
418 * table cell. Use of html formating in other address book fields is
419 * not allowed. Backends that don't return 'extra' row in address book
420 * data should not modify this object property.
421 * @var boolean
422 * @since 1.5.1
423 */
424 var $add_extra_field = false;
425
426 /**
427 * Constructor function.
428 */
429 function AddressBook() {
430 $this->localbackendname = _("Personal Address Book");
431 }
432
433 /**
434 * Return an array of backends of a given type,
435 * or all backends if no type is specified.
436 * @param string $type backend type
437 * @return array list of backends
438 */
439 function get_backend_list($type = '') {
440 $ret = array();
441 for ($i = 1 ; $i <= $this->numbackends ; $i++) {
442 if (empty($type) || $type == $this->backends[$i]->btype) {
443 $ret[] = &$this->backends[$i];
444 }
445 }
446 return $ret;
447 }
448
449
450 /* ========================== Public ======================== */
451
452 /**
453 * Add a new backend.
454 *
455 * @param string $backend backend name (without the abook_ prefix)
456 * @param mixed optional variable that is passed to the backend constructor.
457 * See each of the backend classes for valid parameters
458 * @return integer number of backends
459 */
460 function add_backend($backend, $param = '') {
461 static $backend_classes;
462 if (!isset($backend_classes)) {
463 $backend_classes = array();
464 }
465 if (!isset($backend_classes[$backend])) {
466 /**
467 * Support backend provided by plugins. Plugin function must
468 * return an associative array with as key the backend name ($backend)
469 * and as value the file including the path containing the backend class.
470 * i.e.: $aBackend = array('backend_template' => SM_PATH . 'plugins/abook_backend_template/functions.php')
471 *
472 * NB: Because the backend files are included from within this function they DO NOT have access to
473 * vars in the global scope. This function is the global scope for the included backend !!!
474 */
475 global $null;
476 $aBackend = do_hook('abook_add_class', $null);
477 if (isset($aBackend) && is_array($aBackend) && isset($aBackend[$backend])) {
478 require_once($aBackend[$backend]);
479 } else {
480 require_once(SM_PATH . 'functions/abook_'.$backend.'.php');
481 }
482 $backend_classes[$backend] = true;
483 }
484 $backend_name = 'abook_' . $backend;
485 $newback = new $backend_name($param);
486 //eval('$newback = new ' . $backend_name . '($param);');
487 if(!empty($newback->error)) {
488 $this->error = $newback->error;
489 return false;
490 }
491
492 $this->numbackends++;
493
494 $newback->bnum = $this->numbackends;
495 $this->backends[$this->numbackends] = $newback;
496
497 /* Store ID of first local backend added */
498 if ($this->localbackend == 0 && $newback->btype == 'local') {
499 $this->localbackend = $this->numbackends;
500 $this->localbackendname = $newback->sname;
501 }
502
503 return $this->numbackends;
504 }
505
506
507 /**
508 * create string with name and email address
509 *
510 * This function takes a $row array as returned by the addressbook
511 * search and returns an e-mail address with the full name or
512 * nickname optionally prepended.
513 * @param array $row address book entry
514 * @return string email address with real name prepended
515 */
516 function full_address($row) {
517 global $data_dir, $username;
518 $addrsrch_fullname = getPref($data_dir, $username, 'addrsrch_fullname');
519
520 // allow multiple addresses in one row (poor person's grouping - bah)
521 // (separate with commas)
522 //
523 $return = '';
524 $addresses = explode(',', $row['email']);
525 foreach ($addresses as $address) {
526
527 if (!empty($return)) $return .= ', ';
528
529 if ($addrsrch_fullname == 'fullname')
530 $return .= '"' . $row['name'] . '" <' . trim($address) . '>';
531 else if ($addrsrch_fullname == 'nickname')
532 $return .= '"' . $row['nickname'] . '" <' . trim($address) . '>';
533 else // "noprefix"
534 $return .= trim($address);
535
536 }
537
538 return $return;
539 }
540
541 /**
542 * Search for entries in address books
543 *
544 * Return a list of addresses matching expression in
545 * all backends of a given type.
546 * @param string $expression search expression
547 * @param integer $bnum backend number. default to search in all backends
548 * @return array search results
549 */
550 function search($expression, $bnum = -1) {
551 $ret = array();
552 $this->error = '';
553
554 /* Search all backends */
555 if ($bnum == -1) {
556 $sel = $this->get_backend_list('');
557 $failed = 0;
558 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
559 $backend = &$sel[$i];
560 $backend->error = '';
561 $res = $backend->search($expression);
562 if (is_array($res)) {
563 $ret = array_merge($ret, $res);
564 } else {
565 $this->error .= "\n" . $backend->error;
566 $failed++;
567 }
568 }
569
570 /* Only fail if all backends failed */
571 if( $failed >= sizeof( $sel ) ) {
572 $ret = FALSE;
573 }
574
575 } elseif (! isset($this->backends[$bnum])) {
576 /* make sure that backend exists */
577 $this->error = _("Unknown address book backend");
578 $ret = false;
579 } else {
580
581 /* Search only one backend */
582
583 $ret = $this->backends[$bnum]->search($expression);
584 if (!is_array($ret)) {
585 $this->error .= "\n" . $this->backends[$bnum]->error;
586 $ret = FALSE;
587 }
588 }
589
590 return( $ret );
591 }
592
593
594 /**
595 * Sorted search
596 * @param string $expression search expression
597 * @param integer $bnum backend number. default to search in all backends
598 * @return array search results
599 */
600 function s_search($expression, $bnum = -1) {
601
602 $ret = $this->search($expression, $bnum);
603 if ( is_array( $ret ) ) {
604 usort($ret, 'addressbook_cmp');
605 }
606 return $ret;
607 }
608
609
610 /**
611 * Lookup an address by the indicated field.
612 *
613 * Only possible in local backends.
614 *
615 * @param string $value The value to look up
616 * @param integer $bnum The number of the backend to
617 * look within (OPTIONAL; defaults
618 * to look in all local backends)
619 * @param integer $field The field to look in, should be one
620 * of the SM_ABOOK_FIELD_* constants
621 * defined in include/constants.php
622 * (OPTIONAL; defaults to nickname field)
623 * NOTE: uniqueness is only guaranteed
624 * when the nickname field is used here;
625 * otherwise, the first matching address
626 * is returned.
627 *
628 * @return mixed Array with lookup results when the value
629 * was found, an empty array if the value was
630 * not found, or false if an error occured.
631 *
632 */
633 function lookup($value, $bnum = -1, $field = SM_ABOOK_FIELD_NICKNAME) {
634
635 $ret = array();
636
637 if ($bnum > -1) {
638 if (!isset($this->backends[$bnum])) {
639 $this->error = _("Unknown address book backend");
640 return false;
641 }
642 $res = $this->backends[$bnum]->lookup($value, $field);
643 if (is_array($res)) {
644 return $res;
645 } else {
646 $this->error = $this->backends[$bnum]->error;
647 return false;
648 }
649 }
650
651 $sel = $this->get_backend_list('local');
652 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
653 $backend = &$sel[$i];
654 $backend->error = '';
655 $res = $backend->lookup($value, $field);
656
657 // return an address if one is found
658 // (empty array means lookup concluded
659 // but no result found - in this case,
660 // proceed to next backend)
661 //
662 if (is_array($res)) {
663 if (!empty($res)) 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 the indicated field
945 *
946 * @param string $value The value to look up
947 * @param integer $field The field to look in, should be one
948 * of the SM_ABOOK_FIELD_* constants
949 * defined in include/constants.php
950 * NOTE: uniqueness is only guaranteed
951 * when the nickname field is used here;
952 * otherwise, the first matching address
953 * is returned.
954 *
955 * @return mixed Array with lookup results when the value
956 * was found, an empty array if the value was
957 * not found, or false if an error occured.
958 *
959 */
960 function lookup($value, $field) {
961 $this->set_error('lookup is not implemented');
962 return false;
963 }
964
965 /**
966 * List all entries in backend
967 *
968 * Working backend should provide this function or at least
969 * dummy function that returns empty array.
970 * @return bool
971 */
972 function list_addr() {
973 $this->set_error('list_addr is not implemented');
974 return false;
975 }
976
977 /**
978 * Add entry to backend
979 * @param array userdata
980 * @return bool
981 */
982 function add($userdata) {
983 $this->set_error('add is not implemented');
984 return false;
985 }
986
987 /**
988 * Remove entry from backend
989 * @param string $alias name used for id
990 * @return bool
991 */
992 function remove($alias) {
993 $this->set_error('delete is not implemented');
994 return false;
995 }
996
997 /**
998 * Modify entry in backend
999 * @param string $alias name used for id
1000 * @param array $newuserdata new data
1001 * @return bool
1002 */
1003 function modify($alias, $newuserdata) {
1004 $this->set_error('modify is not implemented');
1005 return false;
1006 }
1007
1008 /**
1009 * Creates full name from given name and surname
1010 *
1011 * Handles name order differences. Function always runs in SquirrelMail gettext domain.
1012 * Plugins don't have to switch domains before calling this function.
1013 * @param string $firstname given name
1014 * @param string $lastname surname
1015 * @return string full name
1016 * @since 1.5.2
1017 */
1018 function fullname($firstname,$lastname) {
1019 // i18n: allows to control fullname layout in address book listing
1020 // first %s is for first name, second %s is for last name.
1021 // Translate it to '%2$s %1$s', if surname must be displayed first in your language.
1022 // Please note that variables can be set to empty string and extra formating
1023 // (for example '%2$s, %1$s' as in 'Smith, John') might break. Use it only for
1024 // setting name and surname order. scripts will remove all prepended and appended
1025 // whitespace.
1026 return trim(sprintf(dgettext('squirrelmail',"%s %s"),$firstname,$lastname));
1027 }
1028 }