vtigerCRM-Spain

vtigerCRM-Spain::LDAP and Active Directory

Building LDAP into a webform

Building LDAP into a webform

postby jtrombley80 » Thu Nov 01, 2012 10:56 pm THANK YOU
I wanted to build a webform for the trouble ticket module where a domain user could input their username, click search and then all the fields on the form would be filled from AD automatically. If anyone else has a need to do so here is the basic code I used, Hey Free Code!!!!:

<?php
    // CONFIGURATION START
    $config = array      ('host'          => yourLDAPservernamehere,
                 'dn'             => 'DC=yourdomain,DC=local',
                 'username'         => 'username@yourdomain.local',
                  'password'         => 'passwordhere',
                 'color1'          => '#DDDDDD',
                 'color2'          => '#FFFFFF',
                 );
    // $search_fields ,  select which AD fields you want to search on
    $search_fields=array('Please enter your network username and click Search'       => 'sAMAccountName',
    //             'Last Name'       => 'sn',
                  );
    //$return_fields , select which AD fields you wish to have displayed in the results
    $return_fields=array('Display Name'    => 'displayname',
                 'First Name'      => 'givenname',
                 'Last Name'       => 'sn',
                 'Telephone Number'    => 'telephonenumber',
                 'Email Address'    => 'mail',
    //            'Office'         => 'physicalDeliveryOfficeName',
    //             'Company'          => 'company',
                  );
    //CONFIGURATION END
    ?>
                <thead>
                   <tr>
    <form id="form1" name="form1" method="post" action="">
    <?php
    foreach ($search_fields as $key => $value)
           { ?>
      <label>
    <b><?php echo $key; ?></b> <input name="<?php echo $value; ?>" type="text" id="givenname" />
      </label>
    <?php } ?>
      <label>
      <input name="Search" type="submit" id="Search" value="Find Me" />
      </tr>
      </thead>
      </label>
    </form>
    </div>
    <?php
    $ldap_search='';
    if ($_SERVER['REQUEST_METHOD'] == "POST")
    {
    foreach ($search_fields as $item => $value) //build filter
        {
        if (empty($_POST[$value]))
           {
            $filter[$value]="($value=*)";
           } else {
            $filter[$value]="($value=$_POST[$value])";
           }
        $ldap_search=$ldap_search.$filter[$value];
        }
     $attrs=array();
     foreach ($return_fields as $item => $value) //build attr array
        {
        $attrs[]=$value;
        }
     $ldap_search='(&'.$and.$ldap_search.')';
    $ad = ldap_connect($config['host'])
          or die( "Could not connect!" );
    // Set version number
    ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3)
         or die ("Could not set ldap protocol");
    ldap_set_option($ad, LDAP_OPT_REFERRALS,0)
     or die ("could no se the ldap referrals");
    // Binding to ldap server
    $bd = ldap_bind($ad, $config['username'], $config['password'])
          or die ("Could not bind");
    // Create the DN
    // Specify only those parameters we're interested in displaying
    // Create the filter from the search parameters
    $search = ldap_search($ad, $config['dn'], $ldap_search, $attrs)
              or die ("ldap search failed");
    $entries = ldap_get_entries($ad, $search);
    if ($entries["count"] > 0) {
    $row_count = 0;
    ?>

Now to echo the values grabbed from AD into the Webform with something like this:

<input id="ticket_FirstName" class="ticket-form-input required" type="text" name="label:First_Name" maxlength="75" style="width:175px;" value="<?php echo $entries[$i]['givenname'][0];?>" tabindex="104">

Hopefully this helps someone else out, took me a bit to do.