Fix #38860 build valid ldap:// URI for scheme-less host on PHP 8.3+ (#39167)

Since PHP 8.3 ldap_connect() takes a single URI argument. connectBind()
concatenated host and port as "host:port", which is a malformed URI for a
scheme-less host (localhost, 192.168.0.2) since the host is then parsed as the
URI scheme, so the following bind returns false. It also appended ":port" to an
already qualified ldaps:// URI. Prefix scheme-less hosts with ldap:// and leave
ldap(s):// URIs untouched.

Signed-off-by: Dolicraft <clement@pixodeo.com>
This commit is contained in:
w3houde 2026-07-15 05:41:18 +07:00 committed by GitHub
parent 5c6e4f486d
commit fe1386fa2d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -358,7 +358,10 @@ class Ldap
dol_syslog(get_class($this)."::connectBind serverPing true, we try ldap_connect to ".$host, LOG_DEBUG);
}
if (version_compare(PHP_VERSION, '8.3.0', '>=')) {
$uri = $host.':'.$this->serverPort;
// Since PHP 8.3, ldap_connect() expects a single URI argument. A scheme-less
// host (ex: localhost, 192.168.0.2) must be turned into a valid ldap:// URI,
// otherwise the host is parsed as the URI scheme and the later bind fails.
$uri = preg_match('/^ldaps?:\/\//i', $host) ? $host : 'ldap://'.$host.':'.$this->serverPort;
$this->connection = ldap_connect($uri);
} else {
$this->connection = ldap_connect($host, $this->serverPort);
@ -371,7 +374,7 @@ class Ldap
dol_syslog(get_class($this)."::connectBind serverPing false, we try ldap_connect to ".$host, LOG_DEBUG);
}
if (version_compare(PHP_VERSION, '8.3.0', '>=')) {
$uri = $host.':'.$this->serverPort;
$uri = preg_match('/^ldaps?:\/\//i', $host) ? $host : 'ldap://'.$host.':'.$this->serverPort;
$this->connection = ldap_connect($uri);
} else {
$this->connection = ldap_connect($host, $this->serverPort);