PHP + Ldap, Ldap Searches Not Current in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
clark.milholland
Forum Newbie
Posts: 2
Joined: Fri Jan 09, 2009 12:50 pm

PHP + Ldap, Ldap Searches Not Current in PHP

Post by clark.milholland »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hello,

My work just implemented a new Cisco VoIP system and it has some really cool features that I am taking some advantage of, soap based web dialer, ldap search directory, Web Based Phone Management, and things of the sort.

The reason I am posting this is because I am having some issues with a Web Directory that I have implemented. The way it works is I a php script that is querying the Call Managers Users via Ldap. I go and add a user to the Call Manager and it doesn't update the Web Directory with the new user. I originally though it was something to do with the server and updating ldap because I am a perfect coder and would never make such a mistake to cause this ;)

After playing around I found that it is updating ldap just fine and handing out the results. I tested this with using Softerra LDAP Administrator.

So now I am thinking that my server is caching the results... I looked at every program that has the ability to cache and didnt find that they were caching any thing. Apache2, PHP, OpenLDAP.

I am hoping that you guys can give me some insight on this issue.

I will attach my Search Code and will give you guys any other information you need. I have full access to the server and built the server.

Code: Select all

<?php
//*********************************Functions************************************
/**
 * @param array $entries
 * @param array $attribs
 * @desc Sort LDAP result entries by multiple attributes.
*/
function ldap_multi_sort(&$entries, $attribs){
    for ($i=1; $i<$entries['count']; $i++){
        $index = $entries[$i];
        $j=$i;
        do {
            //create comparison variables from attributes:
            $a = $b = null;
            foreach($attribs as $attrib){
                $a .= $entries[$j-1][$attrib][0];
                $b .= $index[$attrib][0];
            }
            // do the comparison
            if ($a > $b){
            if ($entries[$j]['givenname'][0]=="Main"){
            $is_greater=true;
            $entries[$j] = $entries[$j-1];
            $j=$j-1;
            }else{
                $is_greater = true;
                $entries[$j] = $entries[$j-1];
                $j = $j-1;}
            }else{
                $is_greater = false;
            }
        } while ($j>0 && $is_greater);
       
        $entries[$j] = $index;
    }
    return $entries;
}
 
//*************************END FUNCTIONS****************************************
 
 
// basic sequence with LDAP is connect, bind, search, interpret search
// result, close connection
 
 
if($post_firstName != null || $post_lastName != null || $post_departmentNumber != null || $post_telephoneNumber != null){
      $searchString = "(givenName=" . $post_firstName . "*))" . $searchString;
      $searchString = "(sn=" . $post_lastName . "*)" . $searchString;
      $searchString = "(departmentNumber=" . $post_departmentNumber . "*)" . $searchString;
      $searchString = "(&(telephonenumber=" . $post_telephoneNumber . "*)" . $searchString;
      }else{
      $searchString = "(&(sn=*)(givenName=*)(departmentNumber=*))"; 
}
 
    if($post_formid == null){
    
    }else{
        
        $ds=ldap_connect("172.16.*.*", "8404"); 
        if ($ds) { 
            //echo "Binding ..."; 
            $r=ldap_bind($ds, "CN=Directory, OU=Users, O=cisco.com", "*****");     // this is an "anonymous" bind, typically
            $sr=ldap_search($ds, "ou=Users, o=cisco.com", $searchString);  
 
    $atrbs = array('departmentnumber', 'sn');
 
    $ent = ldap_get_entries($ds, $sr);
    $info = ldap_multi_sort($ent, $atrbs);
    echo "<table id=\"userTable\" summery=\"Phone User and Ext Table\">\n";
    echo "<thead>\n";
    echo "<tr>\n";
    echo "<th scope=\"col\">First Name</th>\n";
    echo "<th scope=\"col\">Last Name</th>\n";
    echo "<th scope=\"col\">Department</th>\n";
    echo "<th scope=\"col\">Extension</th>\n";
    echo "</tr>\n";
    echo "</thead>\n";
    echo "<tbody>\n";
          // ldap_get_entries returns all lowercase... RTFM
            for ($i=0; $i<$info["count"]; $i++) {
    echo "<tr>\n";
    echo "<td>" . $info[$i]["givenname"][0] . "</td>\n";
    echo "<td>" . $info[$i]["sn"][0] . "</td>\n";
    echo "<td>" . $info[$i]["departmentnumber"][0] . "</td>\n";
    echo "<td><A href=\"javascript&#058;launchWebDialerServlet('" . $info[$i]["cn"][0] . "')\">" . $info[$i]["cn"][0] . "</a></td>\n";
    echo "</tr>\n";
 
                } 
    echo "</tr>\n";
    echo "</tbody>\n";
    echo "</table>\n";
            ldap_close($ds);
 
        } else {
            echo "<h4>Unable to connect to LDAP server</h4>\n";
        }
    }
?>
Thanks in Advanced.

-CM


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP + Ldap, Ldap Searches Not Current in PHP

Post by pickle »

I've never heard of, nor experienced an LDAP search result being cached. I use LDAP quite a bit.

Is it possible your browser is caching?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
clark.milholland
Forum Newbie
Posts: 2
Joined: Fri Jan 09, 2009 12:50 pm

Re: PHP + Ldap, Ldap Searches Not Current in PHP

Post by clark.milholland »

It is doing it on every machine that I have checked, that includes different subnets of our network...

Any other ideas?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP + Ldap, Ldap Searches Not Current in PHP

Post by pickle »

They may still be caching. To make sure, I usually go to: http://this.is.my/url/?randomcraphere

Output $ent after ldap_get_entries() is called & see what that data looks like. That'll eliminate (or implicate) your sort function.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply