3 * functions/addressbook.php - Functions and classes for the addressbook system
5 * Copyright (c) 1999-2005 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * Functions require SM_PATH and support of forms.php functions
11 * @package squirrelmail
12 * @subpackage addressbook
15 global $addrbook_dsn, $addrbook_global_dsn;
18 Create and initialize an addressbook object.
19 Returns the created object
21 function addressbook_init($showerr = true, $onlylocal = false) {
22 global $data_dir, $username, $ldap_server, $address_book_global_filename;
23 global $addrbook_dsn, $addrbook_table;
24 global $abook_global_file, $abook_global_file_writeable;
25 global $addrbook_global_dsn, $addrbook_global_table, $addrbook_global_writeable, $addrbook_global_listing;
27 /* Create a new addressbook object */
28 $abook = new AddressBook
;
31 Always add a local backend. We use *either* file-based *or* a
32 database addressbook. If $addrbook_dsn is set, the database
33 backend is used. If not, addressbooks are stores in files.
35 if (isset($addrbook_dsn) && !empty($addrbook_dsn)) {
37 if (!isset($addrbook_table) ||
empty($addrbook_table)) {
38 $addrbook_table = 'address';
40 $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
42 'table' => $addrbook_table));
43 if (!$r && $showerr) {
44 echo _("Error initializing addressbook database.");
49 $filename = getHashedFile($username, $data_dir, "$username.abook");
50 $r = $abook->add_backend('local_file', Array('filename' => $filename,
53 printf( _("Error opening file %s"), $filename );
59 /* This would be for the global addressbook */
60 if (isset($abook_global_file) && isset($abook_global_file_writeable)
61 && trim($abook_global_file)!=''){
62 // Detect place of address book
63 if (! preg_match("/[\/\\\]/",$abook_global_file)) {
65 $abook_global_filename=$data_dir . $abook_global_file;
66 } elseif (preg_match("/^\/|\w:/",$abook_global_file)) {
67 // full path is set in options (starts with slash or x:)
68 $abook_global_filename=$abook_global_file;
70 $abook_global_filename=SM_PATH
. $abook_global_file;
72 $r = $abook->add_backend('local_file',array('filename'=>$abook_global_filename,
73 'name' => _("Global address book"),
74 'detect_writeable' => false,
75 'writeable'=> $abook_global_file_writeable));
76 if (!$r && $showerr) {
77 echo _("Error initializing global addressbook.");
82 /* Load global addressbook from SQL if configured */
83 if (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) {
84 /* Database configured */
85 if (!isset($addrbook_global_table) ||
empty($addrbook_global_table)) {
86 $addrbook_global_table = 'global_abook';
88 $r = $abook->add_backend('database',
89 Array('dsn' => $addrbook_global_dsn,
91 'name' => _("Global address book"),
92 'writeable' => $addrbook_global_writeable,
93 'listing' => $addrbook_global_listing,
94 'table' => $addrbook_global_table));
98 * hook allows to include different address book backends.
99 * plugins should extract $abook and $r from arguments
100 * and use same add_backend commands as above functions.
102 $hookReturn = do_hook('abook_init', $abook, $r);
103 $abook = $hookReturn[1];
110 /* Load configured LDAP servers (if PHP has LDAP support) */
111 if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) {
113 while (list($undef,$param) = each($ldap_server)) {
114 if (is_array($param)) {
115 $r = $abook->add_backend('ldap_server', $param);
116 if (!$r && $showerr) {
117 printf( ' ' . _("Error initializing LDAP server %s:") .
118 "<br />\n", $param['host']);
119 echo ' ' . $abook->error
;
126 /* Return the initialized object */
131 * Display the "new address" form
133 * Form is not closed and you must add closing form tag.
135 * @param string $form_url form action url
136 * @param string $name form name
137 * @param string $title form title
138 * @param string $button form button name
139 * @param array $defdata values of form fields
141 function abook_create_form($form_url,$name,$title,$button,$defdata=array()) {
143 echo addForm($form_url, 'post', 'f_add').
146 html_tag( 'td', "\n". '<strong>' . $title . '</strong>' . "\n",
150 , 'center', '', 'width="100%"' ) ."\n";
151 address_form($name, $button, $defdata);
156 * Had to move this function outside of the Addressbook Class
157 * PHP 4.0.4 Seemed to be having problems with inline functions.
158 * Note: this can return now since we don't support 4.0.4 anymore.
160 function addressbook_cmp($a,$b) {
162 if($a['backend'] > $b['backend']) {
164 } else if($a['backend'] < $b['backend']) {
168 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
173 * Make an input field
174 * @param string $label
175 * @param string $field
176 * @param string $name
177 * @param string $size
178 * @param array $values
181 function addressbook_inp_field($label, $field, $name, $size, $values, $add='') {
183 $value = ( isset($values[$field]) ?
$values[$field] : '');
185 if (is_array($value)) {
186 $td_str = addSelect($name.'['.$field.']', $value);
188 $td_str = addInput($name.'['.$field.']', $value, $size);
192 return html_tag( 'tr' ,
193 html_tag( 'td', $label . ':', 'right', $color[4]) .
194 html_tag( 'td', $td_str, 'left', $color[4])
200 * Output form to add and modify address data
202 function address_form($name, $submittext, $values = array()) {
203 global $color, $squirrelmail_language;
205 if ($squirrelmail_language == 'ja_JP') {
206 echo html_tag( 'table',
207 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
208 ' <small>' . _("Must be unique") . '</small>') .
209 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
210 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
211 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
212 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
213 list_writable_backends($name) .
216 addSubmit($submittext, $name.'[SUBMIT]'),
217 'center', $color[4], 'colspan="2"')
219 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
221 echo html_tag( 'table',
222 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
223 ' <small>' . _("Must be unique") . '</small>') .
224 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
225 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
226 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
227 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
228 list_writable_backends($name) .
231 addSubmit($submittext, $name.'[SUBMIT]') ,
232 'center', $color[4], 'colspan="2"')
234 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
239 * Provides list of writeable backends.
240 * Works only when address is added ($name='addaddr')
241 * @param string $name name of form
242 * @return string html formated backend field (select or hidden)
244 function list_writable_backends($name) {
245 global $color, $abook;
246 if ( $name != 'addaddr' ) { return; }
247 $writeable_abook = 1;
248 if ( $abook->numbackends
> 1 ) {
249 $backends = $abook->get_backend_list();
250 $writeable_abooks=array();
251 while (list($undef,$v) = each($backends)) {
253 // add each backend to array
254 $writeable_abooks[$v->bnum
]=$v->sname
;
255 // save backend number
256 $writeable_abook=$v->bnum
;
259 if (count($writeable_abooks)>1) {
260 // we have more than one writeable backend
261 $ret=addSelect('backend',$writeable_abooks,null,true);
262 return html_tag( 'tr',
263 html_tag( 'td', _("Add to:"),'right', $color[4] ) .
264 html_tag( 'td', $ret, 'left', $color[4] )) . "\n";
267 // Only one backend exists or is writeable.
268 return html_tag( 'tr',
270 addHidden('backend', $writeable_abook),
271 'center', $color[4], 'colspan="2"')) . "\n";
275 * Sort array by the key "name"
277 function alistcmp($a,$b) {
278 $abook_sort_order=get_abook_sort();
280 switch ($abook_sort_order) {
283 $abook_sort='nickname';
300 if ($a['backend'] > $b['backend']) {
303 if ($a['backend'] < $b['backend']) {
308 if( (($abook_sort_order+
2) %
2) == 1) {
309 return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ?
1 : -1;
311 return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ?
1 : -1;
316 * Address book sorting options
318 * returns address book sorting order
319 * @return integer book sorting options order
321 function get_abook_sort() {
322 global $data_dir, $username;
324 /* get sorting order */
325 if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET
)) {
326 $abook_sort_order = (int) $temp;
328 if ($abook_sort_order < 0 or $abook_sort_order > 8)
331 setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order);
333 /* get previous sorting options. default to unsorted */
334 $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8);
337 return $abook_sort_order;
341 * This function shows the address book sort button.
343 * @param integer $abook_sort_order current sort value
344 * @param string $alt_tag alt tag value (string visible to text only browsers)
345 * @param integer $Down sort value when list is sorted ascending
346 * @param integer $Up sort value when list is sorted descending
347 * @return string html code with sorting images and urls
349 function show_abook_sort_button($abook_sort_order, $alt_tag, $Down, $Up ) {
352 /* Figure out which image we want to use. */
353 if ($abook_sort_order != $Up && $abook_sort_order != $Down) {
354 $img = 'sort_none.png';
356 } elseif ($abook_sort_order == $Up) {
357 $img = 'up_pointer.png';
360 $img = 'down_pointer.png';
364 /* Now that we have everything figured out, show the actual button. */
365 return ' <a href="' . $form_url .'?abook_sort_order=' . $which
366 . '"><img src="../images/' . $img
367 . '" border="0" width="12" height="10" alt="' . $alt_tag . '" title="'
368 . _("Click here to change the sorting of the address list") .'" /></a>';
373 * This is the main address book class that connect all the
374 * backends and provide services to the functions above.
375 * @package squirrelmail
376 * @subpackage addressbook
380 * Enabled address book backends
383 var $backends = array();
385 * Number of enabled backends
388 var $numbackends = 0;
395 * id of backend with personal address book
398 var $localbackend = 0;
400 * Name of backend with personal address book
403 var $localbackendname = '';
406 * Constructor function.
408 function AddressBook() {
409 $this->localbackendname
= _("Personal address book");
413 * Return an array of backends of a given type,
414 * or all backends if no type is specified.
415 * @param string $type backend type
416 * @return array list of backends
418 function get_backend_list($type = '') {
420 for ($i = 1 ; $i <= $this->numbackends
; $i++
) {
421 if (empty($type) ||
$type == $this->backends
[$i]->btype
) {
422 $ret[] = &$this->backends
[$i];
429 /* ========================== Public ======================== */
434 * @param string $backend backend name (without the abook_ prefix)
435 * @param mixed optional variable that is passed to the backend constructor.
436 * See each of the backend classes for valid parameters
437 * @return integer number of backends
439 function add_backend($backend, $param = '') {
440 $backend_name = 'abook_' . $backend;
441 eval('$newback = new ' . $backend_name . '($param);');
442 if(!empty($newback->error
)) {
443 $this->error
= $newback->error
;
447 $this->numbackends++
;
449 $newback->bnum
= $this->numbackends
;
450 $this->backends
[$this->numbackends
] = $newback;
452 /* Store ID of first local backend added */
453 if ($this->localbackend
== 0 && $newback->btype
== 'local') {
454 $this->localbackend
= $this->numbackends
;
455 $this->localbackendname
= $newback->sname
;
458 return $this->numbackends
;
463 * create string with name and email address
465 * This function takes a $row array as returned by the addressbook
466 * search and returns an e-mail address with the full name or
467 * nickname optionally prepended.
468 * @param array $row address book entry
469 * @return string email address with real name prepended
471 function full_address($row) {
472 global $addrsrch_fullname, $data_dir, $username;
473 $prefix = getPref($data_dir, $username, 'addrsrch_fullname');
474 if (($prefix != "" ||
(isset($addrsrch_fullname) &&
475 $prefix == $addrsrch_fullname)) && $prefix != 'noprefix') {
476 $name = ($prefix == 'nickname' ?
$row['nickname'] : $row['name']);
477 return $name . ' <' . trim($row['email']) . '>';
479 return trim($row['email']);
484 * Search for entries in address books
486 * Return a list of addresses matching expression in
487 * all backends of a given type.
488 * @param string $expression search expression
489 * @param integer $bnum backend number. default to search in all backends
490 * @return array search results
492 function search($expression, $bnum = -1) {
496 /* Search all backends */
498 $sel = $this->get_backend_list('');
500 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
501 $backend = &$sel[$i];
502 $backend->error
= '';
503 $res = $backend->search($expression);
504 if (is_array($res)) {
505 $ret = array_merge($ret, $res);
507 $this->error
.= "<br />\n" . $backend->error
;
512 /* Only fail if all backends failed */
513 if( $failed >= sizeof( $sel ) ) {
519 /* Search only one backend */
521 $ret = $this->backends
[$bnum]->search($expression);
522 if (!is_array($ret)) {
523 $this->error
.= "<br />\n" . $this->backends
[$bnum]->error
;
534 * @param string $expression search expression
535 * @param integer $bnum backend number. default to search in all backends
536 * @return array search results
538 function s_search($expression, $bnum = -1) {
540 $ret = $this->search($expression, $bnum);
541 if ( is_array( $ret ) ) {
542 usort($ret, 'addressbook_cmp');
549 * Lookup an address by alias.
550 * Only possible in local backends.
551 * @param string $alias
552 * @param integer backend number
553 * @return array lookup results. False, if not found.
555 function lookup($alias, $bnum = -1) {
560 $res = $this->backends
[$bnum]->lookup($alias);
561 if (is_array($res)) {
564 $this->error
= $backend->error
;
569 $sel = $this->get_backend_list('local');
570 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
571 $backend = &$sel[$i];
572 $backend->error
= '';
573 $res = $backend->lookup($alias);
574 if (is_array($res)) {
578 $this->error
= $backend->error
;
588 * Return all addresses
589 * @param integer $bnum backend number
590 * @return array search results
592 function list_addr($bnum = -1) {
596 $sel = $this->get_backend_list('');
598 $sel = array(0 => &$this->backends
[$bnum]);
601 for ($i = 0 ; $i < sizeof($sel) ; $i++
) {
602 $backend = &$sel[$i];
603 $backend->error
= '';
604 $res = $backend->list_addr();
605 if (is_array($res)) {
606 $ret = array_merge($ret, $res);
608 $this->error
= $backend->error
;
617 * Create a new address
618 * @param array $userdata added address record
619 * @param integer $bnum backend number
620 * @return integer the backend number that the/ address was added
621 * to, or false if it failed.
623 function add($userdata, $bnum) {
626 if (!is_array($userdata)) {
627 $this->error
= _("Invalid input data");
630 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
631 $this->error
= _("Name is missing");
634 if (empty($userdata['email'])) {
635 $this->error
= _("E-mail address is missing");
638 if (empty($userdata['nickname'])) {
639 $userdata['nickname'] = $userdata['email'];
642 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
643 $this->error
= _("Nickname contains illegal characters");
647 /* Check that specified backend accept new entries */
648 if (!$this->backends
[$bnum]->writeable
) {
649 $this->error
= _("Addressbook is read-only");
653 /* Add address to backend */
654 $res = $this->backends
[$bnum]->add($userdata);
658 $this->error
= $this->backends
[$bnum]->error
;
662 return false; // Not reached
667 * Remove the entries from address book
668 * @param mixed $alias entries that have to be removed. Can be string with nickname or array with list of nicknames
669 * @param integer $bnum backend number
670 * @return bool true if removed successfully. false if there s an error. $this->error contains error message
672 function remove($alias, $bnum) {
679 /* Convert string to single element array */
680 if (!is_array($alias)) {
681 $alias = array(0 => $alias);
684 /* Check that specified backend is writable */
685 if (!$this->backends
[$bnum]->writeable
) {
686 $this->error
= _("Addressbook is read-only");
690 /* Remove user from backend */
691 $res = $this->backends
[$bnum]->remove($alias);
695 $this->error
= $this->backends
[$bnum]->error
;
699 return FALSE; /* Not reached */
700 } /* end of remove() */
704 * Modify entry in address book
705 * @param string $alias nickname
706 * @param array $userdata newdata
707 * @param integer $bnum backend number
709 function modify($alias, $userdata, $bnum) {
712 if (empty($alias) ||
!is_string($alias)) {
717 if(!is_array($userdata)) {
718 $this->error
= _("Invalid input data");
721 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
722 $this->error
= _("Name is missing");
725 if (empty($userdata['email'])) {
726 $this->error
= _("E-mail address is missing");
730 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
731 $this->error
= _("Nickname contains illegal characters");
735 if (empty($userdata['nickname'])) {
736 $userdata['nickname'] = $userdata['email'];
739 /* Check that specified backend is writable */
740 if (!$this->backends
[$bnum]->writeable
) {
741 $this->error
= _("Addressbook is read-only");;
745 /* Modify user in backend */
746 $res = $this->backends
[$bnum]->modify($alias, $userdata);
750 $this->error
= $this->backends
[$bnum]->error
;
754 return FALSE; /* Not reached */
755 } /* end of modify() */
758 } /* End of class Addressbook */
761 * Generic backend that all other backends extend
762 * @package squirrelmail
763 * @subpackage addressbook
765 class addressbook_backend
{
767 /* Variables that all backends must provide. */
771 * Can be 'local' or 'remote'
772 * @var string backend type
774 var $btype = 'dummy';
776 * Internal backend name
779 var $bname = 'dummy';
781 * Displayed backend name
784 var $sname = 'Dummy backend';
787 * Variables common for all backends, but that
788 * should not be changed by the backends.
804 var $writeable = false;
808 * @param string $string error message
811 function set_error($string) {
812 $this->error
= '[' . $this->sname
. '] ' . $string;
817 /* ========================== Public ======================== */
820 * Search for entries in backend
821 * @param string $expression
824 function search($expression) {
825 $this->set_error('search not implemented');
830 * Find entry in backend by alias
831 * @param string $alias name used for id
834 function lookup($alias) {
835 $this->set_error('lookup not implemented');
840 * List all entries in backend
843 function list_addr() {
844 $this->set_error('list_addr not implemented');
849 * Add entry to backend
850 * @param array userdata
853 function add($userdata) {
854 $this->set_error('add not implemented');
859 * Remove entry from backend
860 * @param string $alias name used for id
863 function remove($alias) {
864 $this->set_error('delete not implemented');
869 * Modify entry in backend
870 * @param string $alias name used for id
871 * @param array $newuserdata new data
874 function modify($alias, $newuserdata) {
875 $this->set_error('modify not implemented');
881 PHP 5 requires that the class be made first, which seems rather
882 logical, and should have been the way it was generated the first time.
885 require_once(SM_PATH
. 'functions/abook_local_file.php');
886 require_once(SM_PATH
. 'functions/abook_ldap_server.php');
888 /* Only load database backend if database is configured */
889 if((isset($addrbook_dsn) && !empty($addrbook_dsn)) ||
890 (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) ) {
891 include_once(SM_PATH
. 'functions/abook_database.php');
895 * hook allows adding different address book classes.
896 * class must follow address book class coding standards.
898 * see addressbook_backend class and functions/abook_*.php files.
900 do_hook('abook_add_class');