<?php $s=$_GET["search"]; print "<input type=\"text\" name=\"search\" value=\"$s\">";
?>
<?php
if (isset($s)) {
$orig=$s;
// Clean the string
$s=preg_replace("/\s+$/", "", $s);
$s=preg_replace("/^\s+/", "", $s);
$s=preg_replace("/\s+/", "|", $s);
$s=preg_replace('/[^a-zA-Z0-9_-|]+/', "", $s);
// Do the LDAP magic
$ds=ldap_connect("ldaps://ldap.cs.ait.ac.th/");
if (!$ds) {
print "Could not establish connection to the LDAP server, contact the administrator.";
}
else {
$r=ldap_bind($ds);
// Build a nice LDAP OR filter
$filter="(&(|(employeeType=*staff*)(employeeType=*faculty*)(employeeType=*student*))(|";
foreach(preg_split("/\|/", $s) as $n) {
$filter=$filter."(sn=*$n*)(givenName=*$n*)(displayName=*$n*)";
$filter=$filter."))";
$sr=ldap_search($ds, "ou=People,ou=csim,dc=cs,dc=ait,dc=ac,dc=th",
$filter,
array('sn', 'givenName', 'displayName', 'mail'));
if (ldap_count_entries($ds, $sr)>0) {
$info = ldap_get_entries($ds, $sr);
$count=ldap_count_entries($ds, $sr);
if ($count==1) {
print "<p>There is only one entry found.</p>";
print "<p>There are $count entries found.</p>";
print "<table width=100%>";
// Create a big table with all the named and mail
for ($i=0; $i<$count; $i++) {
$name=$info[$i]["sn"][0]." ".$info[$i]["givenname"][0];
if (isset($info[$i]["displayname"][0])) {
$name=$name." (".$info[$i]["displayname"][0].")";
$entry[$name]=$info[$i]["mail"][0];
} // end for
// Sort the big list, and it's time to close LDAP connection, now that we have
// the names in an array.
ldap_close($ds);
ksort($entry);
$i=0;
foreach ($entry as $k=>$v) {
if ($i==0) {
$i=1;
print "<tr><td>$k</td><td><a href=\"mailto:$v\">$v</a></td>";
print "<td>$k</td><td><a href=\"mailto:$v\">$v</a></td></tr>";
if ($i==1 && $count >1) {
// Need to create and empty right column
print "<td> </td><td> </td></tr>";
print "</table>";
print "<p>There is no user with \"$orig\" in their name.</p>";