Location: PHPKode > projects > Network Asset Management Archive > nama/ldap2.php
<body text="#000000" bgcolor="#FFFFCC" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<?
$now = date("m/j/y h:i:s",time());
include "connect.php";


echo "<h3>LDAP query test</h3>";
echo "Connecting ...";
$ds=ldap_connect($ldaphost);// must be a valid LDAP server!
echo "connect result is ".$ds."<p>";

if ($ds) { 
    echo "Binding ..."; 
    $r=ldap_bind($ds);     // this is an "anonymous" bind, typically
                           // read-only access
    echo "Bind result is ".$r."<p>";
    // Search surname entry
$filter='objectclass=organizationalPerson';
$justthese = array( "ou", "sn", "uid", "givenname", "mail", "telephonenumber", "company", "rdn", "initials" );
    $sr=ldap_search($ds,"ou=" . $ldapou, $filter, $justthese);  
    echo "Search result is ".$sr."<p>";

    echo "Number of entires returned is ".ldap_count_entries($ds,$sr)."<p>";

    echo "Getting entries ...<p>";
    $info = ldap_get_entries($ds, $sr);
    echo "Data for ".$info["count"]." items returned:<p>";
echo '<table border="3">';
?>
<tr ALIGN=CENTER BGCOLOR="#999999">
<td><b><font face="Times New Roman,Times">UID</font></b></td>

<td><b><font face="Times New Roman,Times">Display Name</font></b></td>

<td><b><font face="Times New Roman,Times">Email</font></b></td>

<td><b><font face="Times New Roman,Times">First Name</font></b></td>

<td><b><font face="Times New Roman,Times">Initial</font></b></td>

<td><b><font face="Times New Roman,Times">Last Name</font></b></td>

<td><b><font face="Times New Roman,Times">Phone</font></b></td>

<td><b><font face="Times New Roman,Times">Lab</font></b></td>

</tr>

<?
    for ($i=0; $i<$info["count"]; $i++) {
//        echo "<tr><td>". $info[$i]["dn"] ."</td>";
     if (($info[$i]["company"][0] != '') && (stristr($info[$i]["uid"][0],"u4"))){
        $labpos = strpos($info[$i]["company"][0],"-")+1;
        $lab = substr($info[$i]["company"][0],$labpos, strlen($info[$i]["company"][0])-$labpos); 
        if (($lab != 'ITL') && ($lab != 'CHL') && ($lab != 'GSL') && ($lab != 'EL')) {
           $lab = 'SSE';
        }
        $uid = strtoupper($info[$i]["uid"][0]);
        echo "<tr><td>". $uid ."&nbsp</td>";
//        $cn = str_replace("'", "`", $info[$i]["cn"][0]);
//        $cn = str_replace("ERDC-", "", $cn);
//        $cn = str_replace("-MS", "", $cn);
//        $cn = str_replace("Contractor", "", $cn);
       $cn = str_replace("'", "`",$info[$i]["rdn"][0]);
        $display_name = $sn . ', ' . $givenname . ' ' . $initials . ' ' . $lab;
        echo '<td><A HREF="mailto:'. $display_name . '&nbsp">' .  $info[$i]["rdn"][0] . '</td>';
        $mail = str_replace("'", "`", $info[$i]["mail"][0]);
        echo '<td><A HREF="mailto:'. $mail . '&nbsp">' .  $info[$i]["mail"][0] . '</td>';
        $givenname = str_replace("'", "`", $info[$i]["givenname"][0]);
        echo "<td>". $givenname ."&nbsp</td>";
        $initials = str_replace("'", "`", $info[$i]["initials"][0]);
        echo "<td>". $initials ."&nbsp</td>";
        $sn = str_replace("'", "`", $info[$i]["sn"][0]);
        echo "<td>". $sn ."&nbsp</td>";
        $telephonenumber = substr($info[$i]["telephonenumber"][0],0,12);
        echo "<td>". $telephonenumber ."&nbsp</td>";
        echo "<td>". $lab ."&nbsp</td>";
        echo "</tr>";
        $display_name = $sn . ', ' . $givenname . ' ' . $initials . ' ' . $lab;
        $findserial = "select serial from owner where (DISPLAY_NAME = '$display_name');";
        $findserialq = ibase_query($dbh, $findserial);
        if ($foundserial = ibase_fetch_object($findserialq)) {
             $stmt = "update owner set UID = '$uid', DISPLAY_NAME = '$display_name',  FIRST_NAME = '$givenname', LAST_NAME = '$sn', MIDDLE_NAME = '$initials', EMAIL_ADDRESS = '$mail', PHONE = '$telephonenumber', LAB = '$lab', LAST_TIME = '$now' where SERIAL = '$foundserial->SERIAL';"; 
//       echo 'found ' . $stmt;
             $sth = ibase_query($dbh, $stmt);
} else {
             $stmt = "insert into owner (UID, DISPLAY_NAME, FIRST_NAME, LAST_NAME, MIDDLE_NAME, EMAIL_ADDRESS, PHONE, LAB, LAST_TIME) VALUES ('$uid', '$display_name',  '$givenname', '$sn', '$initials', '$mail', '$telephonenumber', '$lab', '$now');";
      echo 'not found ' . $stmt; 
             $sth = ibase_query ($dbh,$stmt);
}
     }
    }
echo "</table>";

    echo "Closing connection";
    ldap_close($ds);

} else {
    echo "<h4>Unable to connect to LDAP server</h4>";
}
?>
Return current item: Network Asset Management Archive