Category: Win32
Author/Contact Info Joseph Harnish
Description: This file goes along with the LDAP searcher that I posted previously. I will add that file to this code. This file you need to name with a .vcf extension. It requires that a perl handler be set up for the .vcf extension. It is the same as adding the .pl extention to the web server. It takes the information from the LDAP and creates a .vcf file that is read by M$Outlook as a Contact.
This file is called directory.pl

#!/usr/bin/perl
use CGI;
use Net::LDAP qw(:all);
use CGI::Carp qw(fatalsToBrowser);
use strict;
my $q = new CGI;
print $q->header;
if($q->param('looking') eq 'yes'){
     my $ldap = Net::LDAP->new('ldap') 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);
    my @entries = $result->entries;  
    
     $ldap->unbind;   # take down session
 
 
 }
 



print <<EODUMP;


<html>
<head>

<title>Directory</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1
+">
</head>

<body bgcolor="#FFFFFF">

<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0" height=
+"238">
  <tr> 
    <td colspan="2" align="center" height="29"><font size="5">Director
+y</font></td>
  </tr>
  <tr> 
    <td width="50%" valign=top align="center"><img src="images/clipart
+/directory.gif" width="188" height="175"></td>
    <td width="50%"> 
      <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 an em
+ployee. 
              Enter as much information as you know.  Use * for wildca
+rds.</font></td>
          </tr>
          <tr> 
            <td><font size="2">First Name</font></td>
            <td> 
              <input type="text" name="first" size="25">
            </td>
          </tr>
          <tr> 
            <td><font size="2">Last Name</font></td>
            <td> 
              <input type="text" name="last" size="25" value="$user">
            </td>
          </tr>
          <tr> 
            <td><font size="2">Department Name</font></td>
            <td> 
              <input type="text" name="dept" size="25">
            </td>
          </tr>
         <tr> 
            <td><font size="2">Phone</font></td>
            <td> 
              <input type="text" name="phone" size="25">
            </td>
          </tr>
          <tr> 
            <td><font size="2">Fax</font></td>
            <td> 
              <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];
            $fax = $entr->get('facsimileTelephoneNumber');
            $fax = $fax->[0];
            (my $lname, my $fname) = split(/\, /, $user);
                        
             print "<tr><td><a href=\"mailto:$email\">$user</a></td>\n
+";
             print "<td>$phone</td></tr>\n";
            print "<tr><td>$title\n</td>\n";
            print "<td><form method=post action=vcfcreater.vcf>\n";
            print "<input type=hidden name=title value=\"$title\">\n";
            print "<input type=hidden name=phone value=\"$phone\">\n";
            print "<input type=hidden name=email value=\"$email\">\n";
            print "<input type=hidden name=fax value=\"$fax\">\n";
            print "<input type=hidden name=address value=\"$street\">\
+n";
            print "<input type=hidden name=fname value=\"$fname\">\n";
            print "<input type=hidden name=lname value=\"$lname\">\n";
            print "<input type=image src=\"images/vcf.gif\" width=\"30
+\" height=\"20\" border=0 alt=\"submit button\">";
            print "</form></td>\n";
            print "</tr>";
            print "<tr><td>$department</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
                );
 }
This file is called vcfcreate.vcf
#!/usr/bin/perl
# vcfcreater.vcf
# If you use IIS all you need to do is add a handler for 
# the .vcf file to be run through perl.exe
# On *nix you just need to make it executable and have a 
# handle for the extention.
use CGI;
use strict;
my $q = new CGI;
my $city = "CITY";
my $state = "NO";
my $zip = "44444";
#Example if you are in eastern time zone in the US it is 5
my $Greenwich_mean_time_difference = 5;

my $office = "Dot Com Store";
my $fname = $q->param('fname');
my $lname = $q->param('lname');
my $title = $q->param('title');
my $phone = $q->param('phone');
my $fax = $q->param('fax');
my $address = $q->param('address');
my $email = $q->param('email');
my($sec, $min, $hour, $day, $month, $year)=(localtime)[0,1,2,3,4,5];
$hour = $hour + $Greenwich_mean_time_difference;

$year += 1900;
$month++;
if($month < 10){
   $month = "0$month";
}
if($day < 10) {
    $day = "0$day";
}

print "Content-Type: download/x-vcard\n\n";

print "BEGIN:VCARD\nVERSION:2.1\n";
print "N:$lname;$fname\n";
print "FN:$fname $lname\n";
print "ORG:$office\n";
print "TITLE:$title\n";
print "TEL;WORK;VOICE:(616) $phone\n";
print "TEL;WORK;FAX:(616) $fax\n";
print "ADR;WORK:;;$address, MW;$city;$state;$zip;United States of Amer
+ica\n";
print "LABEL;WORK;ENCODING=QUOTED-PRINTABLE:$address, MW=0D=0A$city, $
+state $zip=0D=0AUnited States of America\n";
print "EMAIL;PREF;INTERNET:$email\@somewhere.com\n";
print "REV:$year$month$dayT$hour$min$secZ\n";
print "END:VCARD\n";
exit;
Update Added strict to directory.pl