diff --git a/htdocs/adherents/ldap.php b/htdocs/adherents/ldap.php index 38e90663c0f..65aea951d76 100644 --- a/htdocs/adherents/ldap.php +++ b/htdocs/adherents/ldap.php @@ -1,6 +1,6 @@ - * Copyright (C) 2006 Regis Houssin +/* Copyright (C) 2006 Laurent Destailleur + * Copyright (C) 2006-2017 Regis Houssin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -218,13 +218,11 @@ if ($result > 0) } else { - dol_print_error('',$ldap->error); + setEventMessages($ldap->error, $ldap->errors, 'errors'); } print ''; - llxFooter(); - $db->close(); diff --git a/htdocs/contact/ldap.php b/htdocs/contact/ldap.php index 16c00bf92cb..759831f7d57 100644 --- a/htdocs/contact/ldap.php +++ b/htdocs/contact/ldap.php @@ -1,6 +1,6 @@ - * Copyright (C) 2006-2012 Regis Houssin + * Copyright (C) 2006-2017 Regis Houssin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -91,7 +91,7 @@ $head = contact_prepare_head($object); dol_fiche_head($head, 'ldap', $title, -1, 'contact'); dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', ''); - + print '
'; print '
'; @@ -196,15 +196,11 @@ if ($result > 0) } else { - dol_print_error('',$ldap->error); + setEventMessages($ldap->error, $ldap->errors, 'errors'); } print ''; - - - llxFooter(); - $db->close(); diff --git a/htdocs/core/class/ldap.class.php b/htdocs/core/class/ldap.class.php index 7c6fc0c5a77..166fe466796 100644 --- a/htdocs/core/class/ldap.class.php +++ b/htdocs/core/class/ldap.class.php @@ -123,7 +123,7 @@ class Ldap $this->filter = $conf->global->LDAP_FILTER_CONNECTION; // Filter on user $this->filtermember = $conf->global->LDAP_MEMBER_FILTER; // Filter on member - + // Users $this->attr_login = $conf->global->LDAP_FIELD_LOGIN; //unix $this->attr_sambalogin = $conf->global->LDAP_FIELD_LOGIN_SAMBA; //samba, activedirectory @@ -176,34 +176,40 @@ class Ldap { if ($connected) break; if (empty($host)) continue; - + if (preg_match('/^ldap/',$host)) { - $this->connection = ldap_connect($host); + if ($this->serverPing($host) === true) { + $this->connection = ldap_connect($host); + } + else continue; } else { - $this->connection = ldap_connect($host,$this->serverPort); + if ($this->serverPing($host, $this->serverPort) === true) { + $this->connection = ldap_connect($host,$this->serverPort); + } + else continue; } - + if (is_resource($this->connection)) { // Begin TLS if requested by the configuration - if (! empty($conf->global->LDAP_SERVER_USE_TLS)) + if (! empty($conf->global->LDAP_SERVER_USE_TLS)) { - if (! ldap_start_tls($this->connection)) + if (! ldap_start_tls($this->connection)) { dol_syslog(get_class($this)."::connect_bind failed to start tls", LOG_WARNING); $connected = 0; $this->close(); } } - + // Execute the ldap_set_option here (after connect and before bind) $this->setVersion(); ldap_set_option($this->connection, LDAP_OPT_SIZELIMIT, 0); // no limit here. should return true. - - + + if ($this->serverType == "activedirectory") { $result=$this->setReferrals(); @@ -256,7 +262,7 @@ class Ldap } } } - + if (! $connected) $this->close(); } } @@ -662,6 +668,24 @@ class Ldap } } + /** + * Ping a server before ldap_connect for avoid waiting + * + * @param string $host Server host or address + * @param int $port Server port (default 389) + * @param int $timeout Timeout in second (default 1s) + * @return boolean true or false + */ + function serverPing($host, $port=389, $timeout=1) + { + $op = @fsockopen($host, $port, $errno, $errstr, $timeout); + if (!$op) return false; //DC is N/A + else { + fclose($opanak); //explicitly close open socket connection + return true; //DC is up & running, we can safely connect with ldap_connect + } + } + // Attribute methods ----------------------------------------------------- diff --git a/htdocs/user/group/ldap.php b/htdocs/user/group/ldap.php index c195136863e..b7b1597ea74 100644 --- a/htdocs/user/group/ldap.php +++ b/htdocs/user/group/ldap.php @@ -206,12 +206,10 @@ if ($result > 0) } else { - dol_print_error('',$ldap->error); + setEventMessages($ldap->error, $ldap->errors, 'errors'); } print ''; llxFooter(); - $db->close(); - diff --git a/htdocs/user/ldap.php b/htdocs/user/ldap.php index 49739fa7c77..001915a8004 100644 --- a/htdocs/user/ldap.php +++ b/htdocs/user/ldap.php @@ -1,6 +1,6 @@ - * Copyright (C) 2006-2015 Regis Houssin + * Copyright (C) 2006-2017 Regis Houssin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -214,7 +214,7 @@ if ($result > 0) } else { - dol_print_error('',$ldap->error); + setEventMessages($ldap->error, $ldap->errors, 'errors'); } print '';