Jump to content

Active Directory Search


garyjames82

Recommended Posts

I am very new to using php, I have copied this code from a tutorial somewhere on the web, then tweaked it to my own needs, it is working fine and is returning the active directory contents i request.  One thing i would like it to do is prevent it listing accounts that are hidden from the global address list in our exchange active directory.

 

Please see the php code below, any help would be appreciated.

 

<html>

<head>
<script>

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

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Your Company Phonebook</title>
<link href="default.css" rel="stylesheet" type="text/css">
</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"> <h1><br>
        MMUK Contact Search 
        <!-- this is the form that the user will input their search criteria-->
      </h1>
      <form method="GET" action="<?php echo $PHP_SELF; ?>" name="searchform">
        <table width="95%" border="0">
          <tr> 
            <td width="28%" colspan="2"> <div align="center"> </div>
              <div align="center"></div>
              <div align="center"> 
                <input type="text" name="criteria" size="31">
                <input type="submit" value="Search" name="B1">
                <a href="#" onClick="MyWindow=window.open('searchtips.htm','MyWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=300,height=140'); return false;"><font size="1">search 
                tips</font></a></div></td>
          </tr>
        </table>
        <h3><br>
          Search by: 
          <input type="radio" value="l_name" name="params">
          Last Name   
          <input type="radio" value="f_name" name="params">
          First Name   
          <input type="radio" value="district" name="params">
          Location    
          <input type="radio" value="exten" name="params">
          Phone   
          <input type="radio" value="all" name="params" checked>
          All<br>
          <br>
          Your search does not need to be a complete word or number, the first 
          few letters are acceptable.</h3>
        </form></td>
  </tr>
  <?php

require ('ldap.config.php');

//if the search parameters are sent, then start the results output
if ($params)
{

//details what to do given the different type of searches, firstname, last, etc.
if ($params == "all")
{
	$filter = "(|(sn=$criteria*)(givenname=$criteria*)(physicaldeliveryofficename=$criteria*)(telephonenumber=$criteria*))";
	if (!$sort_by){$sort_by = "sn";}
}

elseif ($params == "f_name")
{
	$filter = "(givenname=$criteria*)";
	if (!$sort_by){$sort_by = "cn";}
}

elseif ($params == "l_name")
{
	$filter = "(sn=$criteria*)";
	if (!$sort_by){$sort_by = "cn";}
}

elseif ($params == "exten")
{
	$filter = "(telephonenumber=$criteria*)";
	if (!$sort_by){$sort_by = "telephonenumber";}
}

elseif ($params == "district")
{
	$filter = "(physicaldeliveryofficename=*$criteria*)";
	if (!$sort_by){$sort_by = "physicaldeliveryofficename";}
}

//the fields that the search will pull from in ad
$get_this=array("cn", "telephonenumber","mobile", "physicaldeliveryofficename", "description", "mail", "givenname", "sn");

//make the ldap connection
$connect = ldap_connect( $ldap_host, $ldap_port)
         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
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 $info["count"]." entry returned for \"$criteria\".<br><br>";
}else{
	echo $info["count"]." entries returned for \"$criteria\".<br><br>";
}
?><tr>
  <td align="center">
  <table border="0" width="100%" id="table4" bordercolorlight="#FFFFFF">
    <tr> 
      <td width="16%" bgcolor="#000080"><a style="color: #FFFFFF" href="<?php echo "$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%" bgcolor="#000080"><a style="color: #FFFFFF" href="<?php echo "$PHP_SELF?params=$params&criteria=$criteria&sort_by=givenname"; ?>"><b>First 
        Name 
        <? if ($sort_by == "givenname"){echo "»"; } ?>
        </b></a></td>
      <td width="16%" bgcolor="#000080"><a style="color: #FFFFFF" href="<?php echo "$PHP_SELF?params=$params&criteria=$criteria&sort_by=physicaldeliveryofficename"; ?>"><b>Office 
        <? if ($sort_by == "physicaldeliveryofficename"){echo "»"; } ?>
        </b></a></td>
      <td width="16%" bgcolor="#000080"><a style="color: #FFFFFF" href="<?php echo "$PHP_SELF?params=$params&criteria=$criteria&sort_by=description"; ?>"><b>Position 
        <? if ($sort_by == "description"){echo "»"; } ?>
        </b></a></td>
      <td width="16%" bgcolor="#000080"><a style="color: #FFFFFF" href="<?php echo "$PHP_SELF?params=$params&criteria=$criteria&sort_by=telephonenumber"; ?>"><b>Phone 
        <? if ($sort_by == "telephonenumber"){echo "»"; } ?>
        </b></a></td>
      <td width="16%" bgcolor="#000080"><a style="color: #FFFFFF" href="<?php echo "$PHP_SELF?params=$params&criteria=$criteria&sort_by=mobile"; ?>"><b>Mobile 
        <? if ($sort_by == "mobile"){echo "»"; } ?>
        </b></a></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 = "#FFFFCC";
	}else{
		$color = "#FFFFFF";
	}
	$email = $info[$a]["mail"][0];
	echo "<tr>";
	echo "<td bgcolor=\"$color\" width=\"16%\"><a href=\"mailto:$email\" title=\"Email ".$info[$a]["givenname"][0]." ".$info[$a]["sn"][0]."\"><b>".$info[$a]["sn"][0]."</b></a> </td>";
	echo "<td bgcolor=\"$color\" width=\"16%\"><a href=\"mailto:$email\" title=\"Email ".$info[$a]["givenname"][0]." ".$info[$a]["sn"][0]."\"><b>".$info[$a]["givenname"][0]."</b></a> </td>";
	echo "<td bgcolor=\"$color\" width=\"16%\"><h3>".$info[$a]["physicaldeliveryofficename"][0]." </td>";
	echo "<td bgcolor=\"$color\" width=\"16%\"><h3>".$info[$a]["description"][0]." </td>";
	echo "<td bgcolor=\"$color\" width=\"16%\"><h3>".$info[$a]["telephonenumber"][0]." </td>";
	echo "<td bgcolor=\"$color\" width=\"16%\"><h3>".$info[$a]["mobile"][0]." </td>";
	echo "</tr>";
}
//diconnect from the ldap dbase
ldap_close($connect);
}

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

 

Many Thanks in advance

 

 

Link to comment
https://forums.phpfreaks.com/topic/60575-active-directory-search/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.