HelpSpot Help Desk Software | HelpSpot Blog | HelpSpot Support

Live Lookup Error When passing variables


#1

Okay so have a perplexing issue here. I am receiving an error when running Live Lookup in Helpspot. Upon clicking the view XML button I receive the following error "ldap search failed: Operations error"
I am using the Live_Lookup_AD_and_LDAP.php file from https://support.helpspot.com/index.php?pg=kb.page&id=6
If I hit the script via http ( http://URLforMySITE/helpspot/custom_code/live_lookup_AD_and_LDAP.php ) it will spit back a long list of users. Not every user in my domain however, seemingly random ones from all sorts of OU’s. I have $dn set to the top level of my domain (this is the ldap settings for all our MFP’s global address lists so this bind and account work in many other places in our org) However if I attempt to pass any variables to the script (example: …blahblahblah.php?last_name=smith) it will fail in the browser with: This page contains the following errors: error on line 1 at column 1: Document is empty.
Tracing back the errors I come to this section:

// Binding to ad/ldap server
$bd = ldap_bind($ad, $user, $pswd)
or die ("Could not bind: ".ldap_error($ad));

$search = ldap_search($ad, $dn, $filter, $attrs)
or die ("ldap search failed: ".ldap_error($ad));

$entries = ldap_get_entries($ad, $search);

echo ‘<?xml version="1.0" encoding="utf-8"?>’;
?>

<?php for ($i=0; $i < $entries["count"]; $i++) : ?>

<customer_id><?php if(isset($entries[$i]['uid'][0])){ echo htmlspecialchars($entries[$i]['uid'][0]); } ?></customer_id>
<first_name><?php if(isset($entries[$i]['givenname'][0])){ echo htmlspecialchars($entries[$i]['givenname'][0]); } ?></first_name>
<last_name><?php if(isset($entries[$i]['sn'][0])){ echo htmlspecialchars($entries[$i]['sn'][0]); } ?></last_name>
<?php if(isset($entries[$i]['mail'][0])){ echo htmlspecialchars($entries[$i]['mail'][0]); } ?>
<?php if(isset($entries[$i]['telephonenumber'][0])){ echo htmlspecialchars($entries[$i]['telephonenumber'][0]); } ?>


<?php endfor; ?>

<?php ldap_unbind($ad); ?>

I notice the error I receive when running Live Lookup via Helpspot is the same one listed under: $search = ldap_search($ad, $dn, $filter, $attrs)
or die ("ldap search failed: ".ldap_error($ad));

So tracing that down we see it is setting the variable for the $entries line, which is then used for the first time in:

<?php for ($i=0; $i < $entries["count"]; $i++) : ?>

So we have figured that we are breaking down right here when it is building the xml.
I have no idea why passing along a variable would break it at this step when running the php by itself does yield a laundry list of users.

Has anyone seen this behavior before or have any clues as to where to look at next? If I can provide any additional info please let me know.


#2

Hi Mike,
I’m not sure if you have already done this, but when debugging the live lookup scripts it can be helpful to comment out this line header('Content-type: text/xml'); Then add ini_set("display_errors", 1) and error_reporting(E_ALL). When you run there query in the browser you should get some better errors.

Thanks,
Matt


#3

I am by no means an expert in php and I am stumbling my way through this but I believe I made the changes you mentioned, and did not get syntax errors when checking it so i am pretty sure I did it right. I get basically the same thing when running without passing any variables, however if I search for a last name etc now I get:
Warning: ldap_search(): Search: Operations error in C:\Program Files (x86)\helpspot\helpspot\custom_code\live_lookup_ad_and_ldap.php on line 67
ldap search failed: Operations error

When I trace down line 67 it points right to the same line we had been suspecting:
$search = ldap_search($ad, $dn, $filter, $attrs)

Not sure what is wrong with this line, I assume one of those variables set earlier in the script is giving us a hard time. If this is not what I should be seeing after making those changes please let me know.


#4

That makes sense and sounds like the issue. I suspect one of the filter attributes isn’t right which is why it works when you pass nothing in but fails when you do. Perhaps the attribute names in the filters near the top of the script don’t match how your AD is setup?


#5

Sounds like that might be my issue, let me check on our side and remove anything that doesn’t have a match in our environment and I will update you all, thank you!


#6

I was able to find a someone having a very similar issue, although not the same causes. I was able to replicate some sections of their script to get the sample one provided by helpspot working in my environment. I don’t know exactly why this variation works better for me but I suspect it is the change in location of certain steps/variable declarations and a change in formatting for the bind step. The live lookup script working for me now is:

<?php
header('Content-type: text/xml');

$hostname="ldap://SERVER IP ADDRESS";
$ds=ldap_connect($hostname, 389);
$user = "domain\username";
$pswd = "PASSWORD";
ldap_set_option ($ds, LDAP_OPT_REFERRALS, 0) or die('Unable to set LDAP opt referrals');
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');

if ($ds)
{
$dn = "DC=DOMAIN,DC=local";

if (!($ldapc=ldap_bind($ds, $user, $pswd))) { 
    echo "<p>Error:" . ldap_error($ds) . "</p>"; 
    echo "<p>Error number:" . ldap_errno($ds) . "</p>"; 
    echo "<p>Error:" . ldap_err2str(ldap_errno($ds)) . "</p>"; 
    die;
} 

$attributes = array('employeeID','givenname','sn','mail','telephoneNumber');

if(!empty($_GET['customer_id'])){   //If an ID is passed in use that to make a direct lookup

    $filter = 'employeeID='.$_GET['customer_id'].'*';   

}elseif(!empty($_GET['email'])){            //If no ID then try email

    $filter = 'mail='.$_GET['email'].'*';

}elseif(!empty($_GET['last_name'])){    //If no ID or email then search on last name

    $filter = 'sn='.$_GET['last_name'].'*';



}elseif(!empty($_GET['first_name'])){   //Try first name if no ID,email,last name

    $filter = 'givenname='.$_GET['first_name'].'*'; 

}else{
    $filter = 'sn='.'*';    //Return everyone
}


$search = ldap_search($ds, $dn, $filter, $attributes)
          or die ("ldap search failed: ".ldap_error($ds));

$entries = ldap_get_entries($ds, $search);

echo '<?xml version="1.0" encoding="utf-8"?>';
}
?>
<livelookup version="1.0" columns="first_name,last_name, email">
        <?php for ($i=0; $i < $entries["count"]; $i++) : ?>
        <customer>
                <customer_id><?php if(isset($entries[$i]['employeeID'][0])){ echo htmlspecialchars($entries[$i]['employeeID'][0]); } ?></customer_id>
                <first_name><?php if(isset($entries[$i]['givenname'][0])){ echo htmlspecialchars($entries[$i]['givenname'][0]); } ?></first_name>
                <last_name><?php if(isset($entries[$i]['sn'][0])){ echo htmlspecialchars($entries[$i]['sn'][0]); } ?></last_name>
                <email><?php if(isset($entries[$i]['mail'][0])){ echo htmlspecialchars($entries[$i]['mail'][0]); } ?></email>
                <phone><?php if(isset($entries[$i]['telephonenumber'][0])){ echo htmlspecialchars($entries[$i]['telephonenumber'][0]); } ?></phone>
                <!-- Add custom elements here. Simply add them to $attrs above and then output the tag like the others here -->
        </customer>
        <?php endfor; ?>
</livelookup>

<?php ldap_unbind($ds); ?>

#7

Thanks for the update! It’s always good to have these variations posted as configurations can be wildly different.