diff --git a/phpgwapi/inc/class.ldap.inc.php b/phpgwapi/inc/class.ldap.inc.php index 712efc09de..a2926977e7 100644 --- a/phpgwapi/inc/class.ldap.inc.php +++ b/phpgwapi/inc/class.ldap.inc.php @@ -74,7 +74,11 @@ class ldap } /** - * connect to the ldap server and return a handle + * Connect to ldap server and return a handle + * + * If multiple (space-separated) ldap servers are given, try them in order and + * move first successful one to first place in session, to try not working ones + * only once per session. * * @param $host ldap host * @param $dn ldap dn @@ -115,7 +119,47 @@ class ldap $port = parse_url($host,PHP_URL_PORT); $host = parse_url($host,PHP_URL_HOST); } - // connects to ldap server + // if multiple hosts given, try them all, but only once per session! + if (isset($_SESSION) && isset($_SESSION['ldapConnect']) && isset($_SESSION['ldapConnect'][$host])) + { + $host = $_SESSION['ldapConnect'][$host]; + } + foreach($hosts=preg_split('/[ ,;]+/', $host) as $h) + { + if ($this->_connect($h, $dn, $passwd)) + { + if ($h !== $host) + { + $this->ldapServerInfo[$host] =& $this->ldapServerInfo[$h]; + + if (isset($_SESSION)) // store working host as first choice in session + { + $_SESSION['ldapConnect'][$host] = implode(' ',array_unique(array_merge(array($h),$hosts))); + } + } + return $this->ds; + } + error_log(__METHOD__."('$h', '$dn', \$passwd) Can't connect/bind to ldap server!". + ($this->ds ? ' '.ldap_error($this->ds).' ('.ldap_errno($this->ds).')' : ''). + ' '.function_backtrace()); + } + // give visible error, only if we cant connect to any ldap server + echo "
Error: Can't connect/bind to LDAP server '$host' and dn='$dn'!
".function_backtrace()."