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
@ -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

@ -179,11 +179,17 @@ class Ldap
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))
@ -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>';