Foreign characters and LDAP. What encoding / encoding does LDAP expect?

I parse XML with simplexml_load_string() and use the data in it to update Active Directory (AD) objects via LDAP.

XML example (simplified):

 <?xml version="1.0" encoding="UTF-8"?> <users> <user>Bìlbö Bággįnš</user> <user>Gãńdåłf Thê Gręât</user> <user>Śām Wīšë</user> </users> 

First I ran ldap_search() to find one user, and then proceed to change their attributes. Pumping the above values ​​directly into AD using LDAP will result in some pretty garbled characters.

For example: Bìlbö Bággįnš

I tried the following functions to no avail:

 utf8_encode($str); utf8_decode($str); iconv("UTF-8", "ISO-8859-1//TRANSLIT", $str); iconv("UTF-8", "ASCII//TRANSLIT", $str); iconv("UTF-8", "T.61", $str); 

Ideally, I do not want to do any of these string conversions. UTF-8 must be right, right ?!

I also noticed the following: I printed out the values ​​to see how they exit. a hovering script in the CLI will show the correct characters, but web browsers show the same as AD.

What's happening? Should I look at something else, for example. URL coding? I hope this is up to a simple mistake at my end.

EDIT: I entered these characters using the AD admin GUI to see how they come out. I can read them through LDAP. The correct characters are displayed in the browser. hovering through the CLI will show question marks instead of foreign characters. Passing one of these return values ​​to mb_detect_encoding() will return UTF-8.

I decided to immediately change the same object, not writing to a new line, but simply changing the existing value and saving the object. This works fine - I see the correct value (reverse) in AD.

  • Development on Mac OS X 10.7 Lion - PHP 5.4.3
  • Current job: Red Hat 6 - PHP 5.4.3
  • AD Server: Windows 2003

UPDATE: After a few months, I could not find an answer / solution to this problem. In the end, I went with replacing the characters with their equivalent without an accent (NOT perfect, I know).

+4
source share
4 answers

Are you using LDAP v3?

 ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); 

LDAPv3 supports UTF-8 by default, and it expects requests and responses to be enabled by default. See here: http://technet.microsoft.com/en-us/library/cc961766.aspx

+6
source

I managed to add foreign characters to LDAP with two steps:

  • add user with ASCII characters only (iconv "ASCII // TRANSLIT")

  • use ldapmodify to update fields (s) with UTF-8 characters

LDAPv3 is UTF-8, but the tool I used (from smbldap-tools ) did not deal with it properly.

+1
source

One more note for those who stumble over this:

If your text is already in UTF-8, do not try to transcode it. Please note the following notes on the utf8_encode document page . Re-encoding an already encoded string will distort the text. In addition, the function allows only one specific encoding for another.

You can easily check if UTF-8 needs to encode a string by doing something like:

 if (!preg_match('//u', $value)) { // do your encoding process... } 

As for characters that are not displayed correctly on the web page, but they are in the CLI, make sure you set the correct encoding in your headers:

header('Content-type: text/html; charset=utf-8');

0
source

Here is a solution that worked for me. Do the following things:

1.) First, make sure you are using LDAP version 3, which uses "UTF-8" by default:

 ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); 

2.) If you want to change the user password, make sure Use TLS is set to true and use SSL for false .

 ldap_start_tls($ldapConnection); 

3.) I used port number 389 .

4.) Use the PHP function ldap_mod_replace to replace the user password.

5.) To encode $password :

use the following function:
 public function encodePassword($password) { $password="\"".$password."\""; $encoded=""; for ($i=0; $i <strlen($password); $i++){ $encoded.="{$password{$i}}\000"; } return $encoded; } 

6.) To change the user password, use the following logic:

 $password="test"; if(mb_detect_encoding($password) == 'UTF-8') { $password = utf8_decode($password); } $add=array(); $add["unicodePwd"][0] = encodePassword($password); $result = @ldap_mod_replace($ldapConnection, $userDn, $add); if ($result === false){ //your action } else{ //Your action } 

7.) Note that the encodePassword function will encode your $password to UTF-8 encoded. If your password is UTF-8 encoded, then you will have to decode it before sending it to encodePassword . This is why I wrote the line:

 if(mb_detect_encoding($password) == 'UTF-8') { $password = utf8_decode($password); } 

This code worked for me when I provide German Umlauts with a password: äüößÄÜ , etc.

0
source

All Articles