New: Ping ldap server before ldap_connect for avoid waiting

This commit is contained in:
Regis Houssin 2017-09-15 18:02:52 +02:00
parent 3debd0f49d
commit 7f57f737b3
5 changed files with 44 additions and 28 deletions

View File

@ -1,6 +1,6 @@
<?php <?php
/* Copyright (C) 2006 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2006 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2006 Regis Houssin <regis.houssin@capnetworks.com> * Copyright (C) 2006-2017 Regis Houssin <regis.houssin@capnetworks.com>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -218,13 +218,11 @@ if ($result > 0)
} }
else else
{ {
dol_print_error('',$ldap->error); setEventMessages($ldap->error, $ldap->errors, 'errors');
} }
print '</table>'; print '</table>';
llxFooter(); llxFooter();
$db->close(); $db->close();

View File

@ -1,6 +1,6 @@
<?php <?php
/* Copyright (C) 2006-2010 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2006-2010 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2006-2012 Regis Houssin <regis.houssin@capnetworks.com> * Copyright (C) 2006-2017 Regis Houssin <regis.houssin@capnetworks.com>
* *
* This program is free software; you can redistribute it and/or modify * 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 * 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_fiche_head($head, 'ldap', $title, -1, 'contact');
dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', ''); dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', '');
print '<div class="fichecenter">'; print '<div class="fichecenter">';
print '<div class="underbanner clearboth"></div>'; print '<div class="underbanner clearboth"></div>';
@ -196,15 +196,11 @@ if ($result > 0)
} }
else else
{ {
dol_print_error('',$ldap->error); setEventMessages($ldap->error, $ldap->errors, 'errors');
} }
print '</table>'; print '</table>';
llxFooter(); llxFooter();
$db->close(); $db->close();

View File

@ -123,7 +123,7 @@ class Ldap
$this->filter = $conf->global->LDAP_FILTER_CONNECTION; // Filter on user $this->filter = $conf->global->LDAP_FILTER_CONNECTION; // Filter on user
$this->filtermember = $conf->global->LDAP_MEMBER_FILTER; // Filter on member $this->filtermember = $conf->global->LDAP_MEMBER_FILTER; // Filter on member
// Users // Users
$this->attr_login = $conf->global->LDAP_FIELD_LOGIN; //unix $this->attr_login = $conf->global->LDAP_FIELD_LOGIN; //unix
$this->attr_sambalogin = $conf->global->LDAP_FIELD_LOGIN_SAMBA; //samba, activedirectory $this->attr_sambalogin = $conf->global->LDAP_FIELD_LOGIN_SAMBA; //samba, activedirectory
@ -176,34 +176,40 @@ class Ldap
{ {
if ($connected) break; if ($connected) break;
if (empty($host)) continue; if (empty($host)) continue;
if (preg_match('/^ldap/',$host)) if (preg_match('/^ldap/',$host))
{ {
$this->connection = ldap_connect($host); if ($this->serverPing($host) === true) {
$this->connection = ldap_connect($host);
}
else continue;
} }
else 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)) if (is_resource($this->connection))
{ {
// Begin TLS if requested by the configuration // 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); dol_syslog(get_class($this)."::connect_bind failed to start tls", LOG_WARNING);
$connected = 0; $connected = 0;
$this->close(); $this->close();
} }
} }
// Execute the ldap_set_option here (after connect and before bind) // Execute the ldap_set_option here (after connect and before bind)
$this->setVersion(); $this->setVersion();
ldap_set_option($this->connection, LDAP_OPT_SIZELIMIT, 0); // no limit here. should return true. ldap_set_option($this->connection, LDAP_OPT_SIZELIMIT, 0); // no limit here. should return true.
if ($this->serverType == "activedirectory") if ($this->serverType == "activedirectory")
{ {
$result=$this->setReferrals(); $result=$this->setReferrals();
@ -256,7 +262,7 @@ class Ldap
} }
} }
} }
if (! $connected) $this->close(); 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 ----------------------------------------------------- // Attribute methods -----------------------------------------------------

View File

@ -206,12 +206,10 @@ if ($result > 0)
} }
else else
{ {
dol_print_error('',$ldap->error); setEventMessages($ldap->error, $ldap->errors, 'errors');
} }
print '</table>'; print '</table>';
llxFooter(); llxFooter();
$db->close(); $db->close();

View File

@ -1,6 +1,6 @@
<?php <?php
/* Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2006-2015 Regis Houssin <regis.houssin@capnetworks.com> * Copyright (C) 2006-2017 Regis Houssin <regis.houssin@capnetworks.com>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -214,7 +214,7 @@ if ($result > 0)
} }
else else
{ {
dol_print_error('',$ldap->error); setEventMessages($ldap->error, $ldap->errors, 'errors');
} }
print '</table>'; print '</table>';