Whoops.  For some reason I've attached the wrong file. Here's the correct one.

Cajus
#!/usr/bin/php4 -q
<?php

##################### T E S T - V A R I A B L E S ##################
$server= "localhost";
$filter= "(uid=*)";
$base  = "dc=example,dc=net";


######################## F U N C T I O N S #########################
function get_additional_error($res)
{
        $error= "";
        ldap_get_option ($res, LDAP_OPT_ERROR_STRING, $error);
        return ($error);
}

function get_error($res)
{
        $error= ldap_error($res);
        if ($error == 'Success'){
                return "success";
        } else {
                $adderror= get_additional_error($res);
                if ($adderror != ""){
                        $error= $error." (".get_additional_error($res).")";
                }
                return $error;
        }
}


function rebind($ldap, $referral)
{
        $server= preg_replace('!^(ldap://[^/]+)/.*$!', '\\1', $referral);
        if (!($ds= ldap_connect($server))){
                echo "reconnect failed - ";
                return ($ldap);
        }
        ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
        ldap_set_option($ds, LDAP_OPT_REFERRALS, 1);
        ldap_set_rebind_proc($lds, "rebind");
        if (!ldap_bind($ds)){
                echo "rebind failed - ";
                return ($ldap);
        }
        echo "rebind to $server - ";
        return ($ds);
}


####################################################################
#                             M A I N                              #
####################################################################
echo "Opening connection to $server - ";
$ds= @ldap_connect($server);

if ($ds) {
        echo "success\n";

        echo "Setting up link parameters - ";
        ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
        ldap_set_option($ds, LDAP_OPT_REFERRALS, 1);
        echo get_error($ds)."\n";

        echo "Setting rebind proc - ";
        ldap_set_rebind_proc($ds, "rebind");
        echo get_error($ds)."\n";

        echo "Binding - ";
        if (!($r= @ldap_bind($ds))){
                echo get_error($ds)."\n\n";
                exit (2);
        }
        echo "success\n";
} else {
        echo get_error($ds)."\n\n";
        exit (1);
}

echo "Performing search with base '$base' and filter '$filter'\n";
echo "* starting search - ";
$sr=   @ldap_search($ds, $base, $filter, array('uid', 'cn'), 0, 0, 0, 
LDAP_DEREF_ALWAYS);
echo get_error($ds)."\n";
if (!$sr){
        exit (3);
}

$info= ldap_get_entries($ds, $sr);
echo "* getting entries - ";
echo get_error($ds)."\n";
if (!$info){
        exit (4);
}
for ($n= 0; $n<=$info['count']; $n++){
        if (!isset($info[$n]['uid'][0])){
                continue;
        }
        echo "  ".$info[$n]['dn']."\n";
}


echo "Closing connection...\n";
ldap_close($ds);
?>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to