为了详细说明 rcrow 的帖子,如果你想将 objectSID 值转换为可用的字符串(来自 Active Directory),以下函数可以做到(这是从手册的另一个部分借来的,只是想在这里添加一下)
// 返回文本 SID
function bin_to_str_sid($binsid) {
$hex_sid = bin2hex($binsid);
$rev = hexdec(substr($hex_sid, 0, 2));
$subcount = hexdec(substr($hex_sid, 2, 2));
$auth = hexdec(substr($hex_sid, 4, 12));
$result = "$rev-$auth";
for ($x=0;$x < $subcount; $x++) {
$subauth[$x] =
hexdec($this->little_endian(substr($hex_sid, 16 + ($x * 8), 8)));
$result .= "-" . $subauth[$x];
}
// 通过添加 S- 来作弊
return 'S-' . $result;
}
// 将小端序十六进制数字转换为可以被 'hexdec' 转换的数字
function little_endian($hex) {
for ($x = strlen($hex) - 2; $x >= 0; $x = $x - 2) {
$result .= substr($hex, $x, 2);
}
return $result;
}
此函数与 ldap_get_values_len 函数无关,但如果你想将 objectGUID 二进制值转换为字符串格式(从 Richard Mueller 提供的一些 vbscript 转换而来),它仍然很有用
// 此函数将把二进制值 guid 转换为有效的字符串。
function bin_to_str_guid($object_guid) {
$hex_guid = bin2hex($object_guid);
$hex_guid_to_guid_str = '';
for($k = 1; $k <= 4; ++$k) {
$hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2);
}
$hex_guid_to_guid_str .= '-';
for($k = 1; $k <= 2; ++$k) {
$hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2);
}
$hex_guid_to_guid_str .= '-';
for($k = 1; $k <= 2; ++$k) {
$hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2);
}
$hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4);
$hex_guid_to_guid_str .= '-' . substr($hex_guid, 20);
return strtoupper($hex_guid_to_guid_str);
}
以下是如何使用这两个函数的示例
$filter="samaccountname=".$username;
$fields=array("objectguid","objectsid");
// 首先建立连接并指定 base_dn。手册中有很多关于此的示例
$sr=ldap_search($this->_conn,$this->_base_dn,$filter,$fields);
$entries = ldap_get_entries($this->_conn, $sr);
if (in_array("objectguid", $fields)) {
$entries[0]["objectguid"][0]=
$this->bin_to_str_guid($entries[0]["objectguid"][0]);
}
if (in_array("objectsid", $fields)) {
$entry = ldap_first_entry($this->_conn, $sr);
$objectsid_binary = ldap_get_values_len($this->_conn, $entry, "objectsid");
$entries[0]["objectsid"][0] = $this->bin_to_str_sid($objectsid_binary[0]);
}
希望这对某些人有所帮助!