note 73198 added to function.ldap-get-values-len
| From: | derek dot ethier at gmail dot com | Date: | Tue, 13 Feb 2007 03:49:49 +0000 |
| Subject: | note 73198 added to function.ldap-get-values-len | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123300@lists.php.net to get a copy of this message | ||
To elaborate on rcrow's post, if you want to convert the objectSID value to a usable string
(from Active Directory) the following function will do the trick (this was borrowed from another
section of the manual, just thought I'd add it here):
// Returns the textual 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];
}
// Cheat by tacking on the S-
return 'S-' . $result;
}
// Converts a little-endian hex-number to one, that 'hexdec' can convert
function little_endian($hex) {
for ($x = strlen($hex) - 2; $x >= 0; $x = $x - 2) {
$result .= substr($hex, $x, 2);
}
return $result;
}
This function is not related to the ldap_get_values_len function but is still helpful if you want to
convert the objectGUID binary value to a string format (converted from some vbscript provided by
Richard Mueller):
// This function will convert a binary value guid into a valid string.
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);
}
Here's an example on how to use both:
$filter="samaccountname=".$username;
$fields=array("objectguid","objectsid");
//establish the connection and specify the base_dn first. there are a lot of examples in the manual
for this
$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]);
}
Hope this helps someone!
----
Server IP: 69.147.83.197
Probable Submitter: 142.214.60.130
----
Manual Page -- http://www.php.net/manual/en/function.ldap-get-values-len.php
Edit -- https://master.php.net/note/edit/73198
Del: integrated -- https://master.php.net/note/delete/73198/integrated
Del: useless -- https://master.php.net/note/delete/73198/useless
Del: bad code -- https://master.php.net/note/delete/73198/bad+code
Del: spam -- https://master.php.net/note/delete/73198/spam
Del: non-english -- https://master.php.net/note/delete/73198/non-english
Del: in docs -- https://master.php.net/note/delete/73198/in+docs
Del: other reasons-- https://master.php.net/note/delete/73198
Reject -- https://master.php.net/note/reject/73198
Search -- https://master.php.net/manage/user-notes.php