Techmonkeys.co.uk

Full Version: Active Directory Phonebook
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Ok so I found a great tool and cant get it to work.

I installed WAMP and configured it to connect to LDAP (I Think) and added Active Directory Phonebook. I try to search and it doesnt error out or appear to be connecting to my AD server... Any ideas..


BTW it referenced this site and group as the creater.

Thanks,

Glen
hey buddy,

Welcome to techmonkeys.

I am fairly sure KiD, did make that script. Unfortunately i am also fairly sure that he wont be looking at the site until tomorrow morning (GMT).

hopefully he will be able to help you then.

Cadet
Thanks for the update. I sure hope so. I want to have this working or somewhat ready for my boss.. he gets back next week from vacation.
hi Gpetit, :welcome to Techmonkeys mate.

"i wrote the script" isnt entirely true. I took an old script I found on the interweb and hacked it to bits until I got it working Smile

So what problem are you having with it mate? I have probably modded and changed it a lot since I uploaded it to this site, so let me copy and paste the new code for you.

index.php
Code:
<!--
LDAP directory Codes
Surname = sn
Chosen name (first name) = cn
E-mail address = mail
Location = physicaldeliveryofficename
Department = department
Job Title = description
Phone Number = telephonenumber
Fax Number = facsimiletelephonenumber
Mobile Number = mobile
HO Ext No = ipphone
-->
<html>

<head>
<script>

function setFocus() {document.searchform.criteria.focus();}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<link rel="stylesheet" type="text/css" href="default.css">
<title>Phonebook</title></head>

<body onLoad="setFocus()" topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">

<table border="0" width="100%" id="table1" cellspacing="0" cellpadding="0">
    <tr>
        <td align="center" valign="top">
        <table border="0" width="760" id="table2" cellspacing="0" cellpadding="0">
            <tr>
                <td align="center">
                <font color="#FF0000"><b>*These details are for <u>internal use only</u> and must not be given out to 3rd parties*</b></font>
            <tr>
                <td align="center">

<!-- this is the form that the user will input their search criteria-->
<br /><div class="imgblock"><a href="./" Title=" Phone Book" Alt="Phone Book"><img src="./images/pbooklogo.png" Title="Phone Book" Alt="Phone Book"></a></div><br /><br />
<a href="http://intranet/" Title="Back to home page" Alt="Back to home page"><b>&lt;&lt;Return To Intranet&gt;&gt;</b></a><br /><br />
<form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="searchform">
    <input type="text" name="criteria" size="30"><input type="submit" value="Search" name="submit"><br>
    <p><input type="radio" value="l_name" name="params">Last Name*&nbsp;&nbsp;
    <input type="radio" value="f_name" name="params"> First Name*&nbsp;&nbsp;
    <input type="radio" value="district" name="params"> Location*&nbsp;&nbsp;
    <input type="radio" value="dept" name="params"> Department*&nbsp;&nbsp;
    <input type="radio" value="all" name="params" checked> All of the Above*<br>
    <br>
&nbsp;
    * Your search does not need to be a complete word or number, the first few
    letters are acceptable.</p>
</form>
                
                </td>
            </tr>
            

<?php

require ('./ldap.config.php');

/* if (!$criteria) & (!$name) {
    echo "<font color=\"#ff0000\"><strong>Search box cannot be empty!</strong></font>";
    $yesstar = "1";
}
else { */


//Check if name has been clicked on
if (isset($_GET['name'])) {
    if (isset($_GET['params'])) {
        $params = $_GET['params'];
        $criteria = $_GET['criteria'];
    }
    $name = $_GET['name'];

    $filter = "(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(samaccountname=$name))";
    
    $get_this=array("department", "co", "streetaddress", "st", "postalcode", "l", "cn", "samaccountname", "physicaldeliveryofficename", "facsimiletelephonenumber", "mobile", "description", "mail", "givenname", "sn", "telephonenumber", "ipphone", "department", "mobile" );
    
    $connect = ldap_connect( $ldap_host)
        or exit(">>Could not connect to LDAP server<<");
        
    //for win2003
    ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
    
    //for win2003
    ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
    
    //this is where the username and password are used to make the ldap connection
    $bind = ldap_bind($connect, $ldap_user, $ldap_pass)
         or exit(">>Could not bind to $ldap_host<<");
    
    //search ad ldap
    $read = ldap_search($connect, $base_dn, $filter, $get_this)
         or exit(">>Unable to search ldap server<<");
    
    //sort the results by the user specifications
    $sort_by = "cn";
    ldap_sort($connect, $read, $sort_by);
    
    //get the entries and put into a multi dimensional array
    $info = ldap_get_entries($connect, $read);
    
    // Get address details from OU.
    $ou = $info[0]["physicaldeliveryofficename"][0];
    
    $filter2="(ou=$ou)";
        
    $get_this2=array("street", "l", "st", "postalcode", "co",);
    
    //search ad ldap for ou details
    $read2 = ldap_search($connect, $base_dn, $filter2, $get_this2)
         or exit(">>Unable to search ldap server<<");
        
    //get the  OU entries and put into a multi dimensional array
    $info2 = ldap_get_entries($connect, $read2);

    
?>

    
<tr><td align="center" colspan="4">        
<?php
    if (!file_exists("images/photos/$name.png")) {
        $imagename = "idcard-nopic.png";
        $title = "If you would like your image here, send a digital photo to your admin";
    } else {
        $imagename = "$name.png";
        $title = "$name";
    }

    if (isset($info[0]["ipphone"][0])) {
        $ext = " - Ext (".$info[0]["ipphone"][0].")";
    }
    
    if (isset($info[0]["mail"][0])) {
        $email = $info[0]["mail"][0];
    }
    
    if (isset($info[0]["department"][0])) {
        $dept = $info[0]["department"][0];
    }
    echo "<div id=\"idcard\">";
        echo "<div id=\"photo\">";
            echo "<a href=\"mailto:emailaddresshere?subject=Please add my picture to my profile&body=Please attach a photo of yourself to this email before sending\"><img src=\"images/photos/$imagename\" title=\"$title\"></a>";
        echo "</div>";
        echo "<div id=\"idtext\">";
            echo "<h1>".$info[0]["cn"][0]."</h1>";
            echo "<span class=\"title\">".$info[0]["description"][0]."</span>";
            echo "<div class=\"idtextlabels\">";
            if (isset($dept)) { echo "Department:<br />"; }
                echo "E-mail: <br />";
                echo "Location: <br />";
                echo "Telephone: <br />";
                echo "Mobile: <br />";
                echo "Fax: <br />";
            echo "</div>";
            echo "<div class=\"idtextvalues\">";
                if (isset($dept)) { echo "$dept <br />"; }
                echo "<a href=\"mailto:$email\" title=\"Send e-mail to: ".$info[0]["cn"][0]."\">$email</a><br />";
                echo $info[0]["physicaldeliveryofficename"][0]."<br />";
                echo (isset($ext) ? $info[0]["telephonenumber"][0]." ($ext)    <br />" : (isset($info[0]["telephonenumber"][0]) ? $info[0]["telephonenumber"][0] : "None Specified") ." <br />");
                echo (isset($info[0]["mobile"][0]) ? $info[0]["mobile"][0]."<br />" : "None<br />");
                echo (isset($info[0]["facsimiletelephonenumber"][0]) ? $info[0]["facsimiletelephonenumber"][0] : "None Specified") ."<br />";
            
            echo "</div>";
            echo "</div>";
        echo "<div id=\"idtext2\">";
            $roaming = $info2[0]["street"][0];
            if ($roaming != "Roaming User - no fixed location") {
                echo "<a href=\"http://maps.google.co.uk/maps?f=q&hl=en&geocode=&q=".$info2[0]["postalcode"][0]."\" Title=\"Click to view map of location\" Target=\"_new\">".$info2[0]["street"][0].", ".$info2[0]["l"][0].", ". (isset($info2[0]["st"][0]) ? $info2[0]["st"][0] .",": "&nbsp") ." ".$info2[0]["postalcode"][0].",".$info2[0]["co"][0]."</a>";
            }
            else {
                echo "Roaming Staff - No Fixed Location";
            }
            echo "<hr />";
        echo "</div>";
    echo "</div>";

    echo "<h3>Are these details correct? If not <a href=\"mailto:emailaddresshere?subject=Contact Information Update: ".$info[0]["cn"][0]."\">Click Here!</a> to e-mail us with correct details</h3>";
    
    //diconnect from the ldap dbase
    ldap_close($connect);
}?>
</td></tr>
<?php

//if the search parameters are sent, then start the results output
if (isset($_GET['params'])) {
$params = $_GET['params'];
$criteria = $_GET['criteria'];
if (isset($_GET['name'])) {
$name = $_GET['name'];
}

    //details what to do given the different type of searches, firstname, last, etc.
    if ($params == "all")
    {
        $filter = "(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(|(sn=$criteria*)(givenname=$criteria*)(cn=$criteria)(department=$criteria*)(physicaldeliveryofficename=$criteria*)))";
        // (&(objectClass=User)(!(userAccountControl:1.2.840.113556.1.4.803:=2))
        if (!isset($sort_by)){$sort_by = "sn";}
    
    }
    elseif ($params == "f_name")
    {
        $filter = "(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(givenname=$criteria*))";
        if (!isset($sort_by)){$sort_by = "cn";}
    }
    
    elseif ($params == "l_name")
    {
        $filter = "(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(sn=$criteria*))";
        if (!isset($sort_by)){$sort_by = "cn";}
    }
    elseif ($params == "dept")
    {
        $filter = "(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(department=*$criteria*))";
        if (!isset($sort_by)){$sort_by = "cn";}
    }
    elseif ($params == "district")
    {
        $filter = "(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(physicaldeliveryofficename=*$criteria*))";
        if (!isset($sort_by)){$sort_by = "physicaldeliveryofficename";}
    }
    //the fields that the search will pull from in ad
    $get_this=array("cn", "samaccountname", "physicaldeliveryofficename", "facsimiletelephonenumber", "mobile", "description", "mail", "givenname", "sn", "telephonenumber", "ipphone", "department", "mobile" );
    
    //make the ldap connection
    $connect = ldap_connect( $ldap_host)
             or exit(">>Could not connect to LDAP server<<");
    
    //for win2003
    ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
    
    //for win2003
    ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
    
    //this is where the username and password are used to make the ldap connection
    $bind = ldap_bind($connect, $ldap_user, $ldap_pass)
         or exit(">>Could not bind to $ldap_host<<");
    
    //search ad ldap
    $read = ldap_search($connect, $base_dn, $filter, $get_this)
         or exit(">>Unable to search or no details entered!<<");
    
    //sort the results by the user specifications
    $sort_by = "cn";
    ldap_sort($connect, $read, $sort_by);
    
    //get the entries and put into a multi dimensional array
    $info = ldap_get_entries($connect, $read);
        
    echo "<tr>";
    echo "<td align=\"center\">";
    echo "<p align=\"left\">";

    //print the number, if any, of results
    
    if ($info["count"] == 1)
    {
        echo "<h3><b>".$info["count"]." result for \"$criteria\".</b></h3>";
    }elseif ($info["count"] != 0) {
        echo "<h3><b>".$info["count"]." results for \"$criteria\".</b></h3>";
    }

    if ($info["count"] != 0) {
?>


</td>
            </tr>
            <tr>
                <td align="center">


<table border="1" width="85%" id="table4" bordercolorlight="#FFFFFF">
    <tr>
        <td width="16.6%" bgcolor="#caaacf"><a style="color: #453797" href="<?php echo $_SERVER['PHP_SELF']."?params=$params&criteria=$criteria&sort_by=givenname"; ?>"><b>First Name <? if ($sort_by == "givenname"){echo "»"; } ?></b></a></td>
        <td width="16.6%" bgcolor="#caaacf"><a style="color: #453797" href="<?php echo $_SERVER['PHP_SELF']."?params=$params&criteria=$criteria&sort_by=sn"; ?>"><b>Last Name <? if ($sort_by == "sn" || $sort_by == "cn"){echo "»"; } ?></b></a></td>
        <td width="16.6%" bgcolor="#caaacf"><a style="color: #453797" href="<?php echo $_SERVER['PHP_SELF']."?params=$params&criteria=$criteria&sort_by=description"; ?>"><b>Job Title <? if ($sort_by == "description"){echo "»"; } ?></b></a></td>
        <td width="16.6%" bgcolor="#caaacf"><a style="color: #453797" href="<?php echo $_SERVER['PHP_SELF']."?params=$params&criteria=$criteria&sort_by=physicaldeliveryofficename"; ?>"><b>Location <? if ($sort_by == "physicaldeliveryofficename"){echo "»"; } ?></b></a></td>
        <td width="16.6%" bgcolor="#caaacf"><a style="color: #453797" href="<?php echo $_SERVER['PHP_SELF']."?params=$params&criteria=$criteria&sort_by=telephonenumber"; ?>"><b>Phone <? if ($sort_by == "telephonenumber"){echo "»"; } ?></b></a></td>
        <td width="10%" bgcolor="#caaacf" ><font color="#453797"><b>More</b></font></td>
    </tr>
    
<?php

//start the loop for printing the results
for ($a=0; $a<$info["count"]; $a++)
{
        //make every other row colored, feel free to change the colors
        if ($a % 2 == 0)
        {
            $color = "#d8d2e3";
        }else{
            $color = "#f1eff5";
        }
        if (isset($info[$a]["description"][0]) && $info[$a]["description"][0] != "Admin Account") {
            $email = (isset($info[$a]["mail"][0]) ? $info[$a]["mail"][0] : 'no e-mail on record');
            $accountname = $info[$a]["samaccountname"][0];
            echo "<tr>";
            echo "<td bgcolor=\"$color\" width=\"12.5%\"><a href=\"mailto:$email\" title=\"Email ".$info[$a]["givenname"][0]." ".$info[$a]["sn"][0]."\"><b>".$info[$a]["givenname"][0]."</b></a>&nbsp;</td>";
            echo "<td bgcolor=\"$color\" width=\"12.5%\"><a href=\"mailto:$email\" title=\"Email ".$info[$a]["givenname"][0]." ".$info[$a]["sn"][0]."\"><b>".$info[$a]["sn"][0]."</b></a>&nbsp;</td>";
            echo "<td bgcolor=\"$color\" width=\"18.5%\">" . (isset($info[$a]['description'][0]) ? $info[$a]['description'][0] : '&nbsp') . "</td>";
            echo "<td bgcolor=\"$color\" width=\"16%\">" . (isset($info[$a]["physicaldeliveryofficename"][0]) ? "<a href=\"?criteria=".$info[$a]["physicaldeliveryofficename"][0]."&params=district\" Title=\"Click here to find more people at ".$info[$a]["physicaldeliveryofficename"][0]."\">".$info[$a]["physicaldeliveryofficename"][0]."</a>&nbsp;" : "None specified") . "</td>";
            echo "<td bgcolor=\"$color\" width=\"12.5%\">" . (isset($info[$a]['telephonenumber'][0]) ? $info[$a]['telephonenumber'][0] : 'None Specified') . "</td>";
            echo "<td bgcolor=\"$color\" width=\"12.5%\"><a href=\"?name=$accountname\" title=\"More Details For: ".$info[$a]["cn"][0]."\"><b>More</b></a>&nbsp;</td>";
            echo "</tr>";
        }
    }
} else {
        echo "<h3><font color=\"#FF0000\"><b>".$info["count"]." results for \"$criteria\". Try again!</b></font></h3>";
    }
//diconnect from the ldap dbase
ldap_close($connect);
}

echo "</table>";
//}
?>                
                </td>
            </tr>
        </table>
        </td>
    </tr>
</table>

</body>

</html>

ldap.config.php
Code:
<?php

//the fqdn for the ad domain, company.local, etc.
$ldap_host = "XYZ.com";
    
//the ou the you will be pulling results from, it reads backwards - you may have to us CN= or OU=, I have seen both work.  If your domain XYZ.com then use: $base_dn = "OU=Users, DC=XYZ, DC=com

$base_dn = "OU=Users, DC=XYZ, DC=com";

//the username for the domain, most users have read rights to ad eg: administrator@yourdomain.com
$ldap_user  = "<username>@<domain>.com";

//the password for that username    
$ldap_pass = "<password>";

?>
Thanks for the update. Is there a way to do a search for mutiple users?

IE search mike, glen, rich????


Thanks,
Glen

BTW I was able to get it work with so work. just need to work on the finer details.

I may hit you up again. Great job in it .
It should do that automatically. If you leave it on the default search option "all of the above" then it should search by whatever you enter followed by a wildcard.

eg: Searching for dar would bring up:
Darren
Darrell
Darkness

etc
I guess what I am asking is there a way to search for muiltible people at one time?

like if you enter in the search ashley, mike and glen...

it show 0 results.
oh I get you, not to search multiple glen's as in glen*, but instead multiple people of different names.

Suppose it could be done, if you put the names in separated by a comma you could change the code to do multiple searches.

Not something we need here though.

By the way this is something I should mention, this code is something I wrote simply for our inetrnal intranet here, so it hasnt been vulnerability tested and could have security implications, definitely not a good idea to give public access to your phone book,
True, this would be for our use only not for the public.
Pages: 1 2
Reference URL's