michihide's blog

技術メモおよび雑感

Net_LDAP2の使い方

# pear install Net_LDAP2

とりあえず動いたのでサンプル:

#!/usr/bin/php
<?php
require_once('Net/LDAP2.php');
$ldap = Net_LDAP2::connect(array(
    'host'        => 'localhost'
  , 'port'        => 389
  , 'version'     => 3
  , 'starttls'    => false
  , 'binddn'      => 'cn=Manager,dc=example,dc=com'
  , 'bindpw'      => 'secret'
  , 'basedn'      => 'dc=example,dc=com'
    )
);
if (PEAR::isError($ldap)) {
    die($ldap->getMessage() . "\n");
}
$filter = Net_LDAP2_Filter::create('uid', 'equals', 'hotta');
$searchbase = 'ou=Nagasaki,ou=Users,dc=example,dc=com';
$options = array(
    'scope' => 'sub',
    'attributes' => array('sambaHomePath')
);
$search = $ldap->search($searchbase, $filter, $options);
if (PEAR::isError($search)) {
    die($search->getMessage() . "\n");
}
printf("Found %d entries.\n", $search->count());
foreach ($search as $dn => $entry) {
    $sambaHomePath = $entry->getValue('sambaHomePath', 'single');
    printf("Fetched DN: %s\n",$dn);
    printf("sambaHomePath: %s\n", $sambaHomePath);
}

$ php ldaptest.php
Found 1 entries.
Fetched DN: uid=hotta,ou=Nagasaki,ou=Users,dc=example,dc=com
sambaHomePath: \\file4\homes