@@ -2944,6 +2944,35 @@ function address_filter_sql($addresses, $type)
29442944 return $ sqladdr ;
29452945}
29462946
2947+ /**
2948+ * Constructs an LDAP URI using the given host and port.
2949+ *
2950+ * If the host doesn't already include a protocol, it will be prefixed with "ldaps://" when the port is "636",
2951+ * otherwise with "ldap://". If the URI doesn't already contain a port, the port is appended.
2952+ *
2953+ * @param string $host The LDAP host.
2954+ * @param int|string $port The LDAP port.
2955+ * @return string The constructed LDAP URI.
2956+ */
2957+ function ldap_build_uri ($ host , $ port ) {
2958+ // Convert the port to a string immediately
2959+ $ portStr = (string )$ port ;
2960+
2961+ // If the host doesn't already start with "ldap://" or "ldaps://", prepend the appropriate protocol
2962+ if (stripos ($ host , 'ldap:// ' ) !== 0 && stripos ($ host , 'ldaps:// ' ) !== 0 ) {
2963+ $ protocol = ($ portStr === '636 ' ) ? 'ldaps:// ' : 'ldap:// ' ;
2964+ $ host = $ protocol . $ host ;
2965+ }
2966+
2967+ // Use parse_url to check if the URI already includes a port
2968+ $ parsedUrl = parse_url ($ host );
2969+ if ($ portStr && !isset ($ parsedUrl ['port ' ])) {
2970+ $ host .= ': ' . $ portStr ;
2971+ }
2972+
2973+ return $ host ;
2974+ }
2975+
29472976/**
29482977 * @param string $username
29492978 * @param string $password
@@ -2954,7 +2983,8 @@ function ldap_authenticate($username, $password)
29542983{
29552984 $ username = ldap_escape (strtolower ($ username ), '' , LDAP_ESCAPE_DN );
29562985 if ('' !== $ username && '' !== $ password ) {
2957- $ ds = ldap_connect (LDAP_HOST , LDAP_PORT ) or exit (__ ('ldpaauth103 ' ) . ' ' . LDAP_HOST );
2986+ $ ldap_uri = ldap_build_uri (LDAP_HOST , LDAP_PORT );
2987+ $ ds = ldap_connect ($ ldap_uri ) or exit (__ ('ldpaauth103 ' ) . ' ' . $ ldap_uri );
29582988
29592989 $ ldap_protocol_version = 3 ;
29602990 if (defined ('LDAP_PROTOCOL_VERSION ' )) {
@@ -3172,8 +3202,9 @@ function ldap_get_conf_var($entry)
31723202 // Translate MailScanner.conf vars to internal
31733203 $ entry = translate_etoi ($ entry );
31743204
3175- $ lh = ldap_connect (LDAP_HOST , LDAP_PORT )
3176- or exit (__ ('ldapgetconfvar103 ' ) . ' ' . LDAP_HOST . "\n" );
3205+ $ ldap_uri = ldap_build_uri (LDAP_HOST , LDAP_PORT );
3206+ $ lh = ldap_connect ($ ldap_uri )
3207+ or exit (__ ('ldapgetconfvar103 ' ) . ' ' . $ ldap_uri . "\n" );
31773208
31783209 @ldap_bind ($ lh )
31793210 or exit (__ ('ldapgetconfvar203 ' ) . "\n" );
@@ -3212,8 +3243,9 @@ function ldap_get_conf_truefalse($entry)
32123243 // Translate MailScanner.conf vars to internal
32133244 $ entry = translate_etoi ($ entry );
32143245
3215- $ lh = ldap_connect (LDAP_HOST , LDAP_PORT )
3216- or exit (__ ('ldapgetconfvar103 ' ) . ' ' . LDAP_HOST . "\n" );
3246+ $ ldap_uri = ldap_build_uri (LDAP_HOST , LDAP_PORT );
3247+ $ lh = ldap_connect ($ ldap_uri )
3248+ or exit (__ ('ldapgetconfvar103 ' ) . ' ' . $ ldap_uri . "\n" );
32173249
32183250 @ldap_bind ($ lh )
32193251 or exit (__ ('ldapgetconfvar203 ' ) . "\n" );
0 commit comments