Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

LDAP Searcher

by BigJoe (Curate)
on Feb 06, 2001 at 02:58 UTC ( #56537=sourcecode: print w/ replies, xml ) Need Help??
Category: networking code
Author/Contact Info Joseph Harnish Big_Joe1008@yahoo.com
Description: This is a quick LDAP search I did. This gives web users to search our internal "Phone book". I figure when I was trying to throw this together I couldn't find any information on it so here it is for everyone. Thanks to Chromatic for giving me a fix for the Win32 problem with the entry's.
#!/usr/bin/perl
use CGI;
use Net::LDAP qw(:all);
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
print $q->header;
if($q->param('looking') eq 'yes'){
     $ldap = Net::LDAP->new('ldapserver') or die "$@";
 

     $ldap->bind;    # an anonymous bind
     my $base = "c=us";
     my @Attrs = ();
    my $search = "(&";
    if($q->param('first') ne ''){
            $search = "$search (givenName=" . $q->param('first') . ")"
+;
    }
    if($q->param('last') ne ''){
            $search = "$search (sn=" . $q->param('last') . ")";
    }
    if($q->param('dept') ne ''){
            $search = "$search (Department=" . $q->param('dept') . ")"
+;
    }
    if($q->param('phone') ne ''){
            $search = "$search (telephoneNumber=" . $q->param('phone')
+ . ")";
    }
    if($q->param('fax') ne ''){
            $search = "$search (facsimileTelephoneNumber=" . $q->param
+('fax') . ")";
    }
    $search = $search . ")";
    
    $result = LDAPsearch($ldap, $search,\@Attrs);
    @entries = $result->entries;  
    
     $ldap->unbind;   # take down session
 
 
 }
 



print <<EODUMP;

     header goes here
     <form method="post" action="directory.pl">
      <input type=hidden name=looking value=yes>
        <table width="85%" border="0" cellspacing="0" cellpadding="0">
          <tr> 
            <td colspan="2"><font size="2">Use this form to find a emp
+loyee. 
              Enter as much information as you know.</font></td>
          </tr>
          <tr> 
            <td width="20%"><font size="2">first Name</font></td>
            <td width="80%"> 
              <input type="text" name="first" size="25">
            </td>
          </tr>
          <tr> 
            <td width="20%"><font size="2">Last Name</font></td>
            <td width="80%"> 
              <input type="text" name="last" size="25" value="$user">
            </td>
          </tr>
          <tr> 
            <td width="20%"><font size="2">Department Name</font></td>
            <td width="80%"> 
              <input type="text" name="dept" size="25">
            </td>
          </tr>
         <tr> 
            <td width="20%"><font size="2">Phone</font></td>
            <td width="80%"> 
              <input type="text" name="phone" size="25">
            </td>
          </tr>
          <tr> 
            <td width="20%"><font size="2">Fax</font></td>
            <td width="80%"> 
              <input type="text" name="fax" size="25">
            </td>
          </tr>
          <tr align="center"> 
            <td colspan="2"><input type=image src="images/go.gif" widt
+h="30" height="20" border=0 alt="submit button"></a></td>
          </tr>
        </table>
      </form>

EODUMP

PRINTRESULTS();

print <<EODUMP;  
   footer goes here
EODUMP

sub PRINTRESULTS
{
print "<table>";
foreach $entr (@entries)
    {
             $phone = $entr->get('telephoneNumber');
              $user = $entr->get('cn');
              $email = $entr->get('mail');
              $department = $entr->get('Department');
              $address = $entr->get('address');
            $address = $address->[0];
            $department = $department->[0];
             $phone = $phone->[0];
              $user = $user->[0];
               $email = $email->[0]; 
            $title = $entr->get('title');
            $title = $title->[0];
            $street = $entr->get('postalAddress');
            $street = $street->[0];
            
             print "<tr><td><a href=\"mailto:$email\">$user</a></td>\n
+";
             print "<td>$phone</td></tr>\n";
            print "<tr><td>$department, $title\n</td></tr>\n";
            print "<tr><td>$street</td>\n";
            print "<tr><td>&nbsp;</td></tr>\n";
        
    }
    print "</table>";
}

sub LDAPsearch
 {
          my ($ldap,$searchString,$attrs) = @_ ;
          my $result = $ldap->search (
                base    => "$base",
                scope   => "sub",
                filter  => "$searchString",
                attrs   =>  $attrs,
        sizelimit => $size_limit
                );
 }
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://56537]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2016-07-26 04:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?


















    Results (231 votes). Check out past polls.