Format Output
When an LDAP search is performed, the data is returned in whatever sequence it is found.
In other words, there is not an easy way to sort the data like with a common SQL ORDER
BY statement. As well, many public LDAP directories do not have standard capitalization.
Since the sort is based on the ASCII value of the strings, we must format the strings with
all lowercase letters to appropriately alphabetize our output.
In other words, there is not an easy way to sort the data like with a common SQL ORDER
BY statement. As well, many public LDAP directories do not have standard capitalization.
Since the sort is based on the ASCII value of the strings, we must format the strings with
all lowercase letters to appropriately alphabetize our output.
It is important to note, that an LDAP result set is returned as a multi-dimensional array. Thus,
at this point in our script $result_array contains something like this:
at this point in our script $result_array contains something like this:
$result_array[0]["cn"] [0] = "Dannie Stanley" ["dn"] [0] = "uid=dannie,dc=spinweb.net" ["givenname"][0] = "Dannie" ["sn"] [0] = "Stanley" ["mail"] [0] = "[email protected]" $result_array[1]["cn"] [0] = "Michael Reynolds" ["dn"] [0] = "uid=michael,dc=spinweb.net" ["givennam""][0] = "Michael" ["sn"] [0] = "Reynolds" ["mail"] [0] = "[email protected]"
The data is stored in this format because each attribute may have more than one value
(IE a tree structure). For example, if my name is ‘Dannie,’ yet everyone knows me as ‘Dan,’
I could add an attribute to LDAP to store both representations of my given name like this:
(IE a tree structure). For example, if my name is ‘Dannie,’ yet everyone knows me as ‘Dan,’
I could add an attribute to LDAP to store both representations of my given name like this:
$result_array[0]["cn"] [0] = "Dannie Stanley" ["dn"] [0] = "uid=dannie,dc=spinweb.net" ["givenname"][0] = "Dannie" ["givenname"][0] = "Dan" ["sn"] [0] = "Stanley" ["mail"] [0] = "[email protected]"
For this search, we are only worried about the first value of every attribute so we will
be using 0 as the index for each attribute, except for dn (Distinguished Name),
which contains only one value. Here is a brief list of attributes and their meaning:
be using 0 as the index for each attribute, except for dn (Distinguished Name),
which contains only one value. Here is a brief list of attributes and their meaning:
- cn
- Common Name
- dn
- Distinguished Name
- givenname
- First Name
- sn
- Last Name
- Email Address
<?php
//Sort results if search was successful
if($result_array)
{
for($i=0; $i<count($result_array); $i++)
{
$format_array[$i][0] = strtolower($result_array[$i]["cn"][0]);
$format_array[$i][1] = $result_array[$i]["dn"];
$format_array[$i][2] = strtolower($result_array[$i]["givenname"][0]);
$format_array[$i][3] = strtolower($result_array[$i]["sn"][0]);
$format_array[$i][4] = strtolower($result_array[$i]["mail"][0]);
}
//Sort array
sort($format_array, "SORT_STRING");
for(
$i=0; $i<count($format_array); $i++)
{
$cn = $format_array[$i][0];
$dn = $format_array[$i][1];
$fname = ucwords($format_array[$i][2]);
$lname = ucwords($format_array[$i][3]);
$email = $format_array[$i][4];
if(
$dn && $fname && $lname && $email)
{
$result_list .= "<A HREF="ldap://$LDAP_SERVER[$SERVER_ID]/$dn">$fname $lname</A>";
$result_list .= " <<A HREF="mailto:$email">$email</A>><BR>n";
}
elseif($dn && $cn && $email)
{
$result_list .= "<A HREF="ldap://$LDAP_SERVER[$SERVER_ID]/$dn">$cn</A>";
$result_list .= " <<A HREF="mailto:$email">$email</A>><BR>n";
}
}
}
else
{
echo "Result set empty for query: $ldap_query";
}
?>
In our example, $format_array is our new array which contains the query results
in a format optimized for output. First, we loop through every element of the
$result_array and assign it to a two-dimensional array for sorting purposes. At
the same time we are using the strtolower() function to make all values lower-case.
in a format optimized for output. First, we loop through every element of the
$result_array and assign it to a two-dimensional array for sorting purposes. At
the same time we are using the strtolower() function to make all values lower-case.
Second, we sort the array using a handy little search algorithm provided by PHP called
sort(). The first argument is the array. The second is what type of sorting to perform,
as defined by the PHP documentation. Since we are sorting by string, we use “SORT_STRING”.
sort(). The first argument is the array. The second is what type of sorting to perform,
as defined by the PHP documentation. Since we are sorting by string, we use “SORT_STRING”.
Third, we loop through the newly formatted array and assign it to an output string named
$result_list that contains the HTML representation of the data. It is important to
note that I have used the ldap URL format for the hyper-links. An example of this looks something
like this: HREF=”ldap://ldap.domain.net/uid=dannie,dc=domain.net”.
$result_list that contains the HTML representation of the data. It is important to
note that I have used the ldap URL format for the hyper-links. An example of this looks something
like this: HREF=”ldap://ldap.domain.net/uid=dannie,dc=domain.net”.
Close Connection
Now that we have all of our data contained in $result_list, we can safely disconnect
from the LDAP connection.
from the LDAP connection.
<?php
//Close Connection
ldap_close($connect_id);
?>