Class LdapClient

Description

LDAP client class

Implementation of an LDAP client, which is able to search for entries, add new entries and modify or delete existing entries.

Example:

  1.  $ldap new LdapClient(array(
  2.    'host' => 'ldap.mydomain.com',
  3.    'bindDN' => 'cn=Administrator,dc=yourcompany,dc=com',
  4.    'bindPassword' => 'password',
  5.    'baseDN' => 'dc=yourcompany,dc=com'
  6.  ));
  7.  if ($ldap->connect()) {
  8.    $result $ldap->search('(cn=*)');
  9.    while ($entry $result->fetchEntry()) {
  10.      println($entry->getAttribute('cn');
  11.    }
  12.    $ldap->close();
  13.  }

Located in /core/net/ldap/LdapClient.class.php (line 83)

PHP2Go
   |
   --LdapClient
Variable Summary
string $baseDN
string $bindDN
string $bindPassword
resource $handle
string $host
int $port
bool $useTLS
Method Summary
LdapClient LdapClient ([array $config = array()])
void __destruct ()
bool add (LdapEntry $entry)
void close ()
bool connect ()
bool delete (LdapEntry|string $entry, [bool $recursive = TRUE])
LdapEntry|FALSE find (string $dn)
resource &getHandle ()
mixed getOption (int $option)
bool isConnected ()
LdapSearchResult|NULL search (string $filter, [array $settings = array()], [ $sort = array()])
void setBaseDN (string $baseDN)
void setBindParameters (string $bindDN, string $bindPassword)
bool setOption (int $option, mixed $value)
void setProtocolVersion (int $version)
void setServer (string $host, [int $port = NULL])
bool update (LdapEntry $entry)
array _explodeDN (string $dn)
string _getErrorMessage ()
Variables
string $baseDN (line 108)

Base DN used on searches

string $bindDN (line 114)

DN used to bind

string $bindPassword (line 120)

Password used to bind

resource $handle (line 133)

Connection handle

  • access: private
string $host = 'localhost' (line 96)

LDAP host

int $port = LDAP_DEFAULT_PORT (line 102)

LDAP port

int $protocolVersion = 3 (line 90)

LDAP protocol version

bool $useTLS = TRUE (line 126)

Enable or disable TLS (Transport Layer Security)

Methods
Constructor LdapClient (line 149)

Class constructor

Allowed config arguments:

  • host
  • bindDN
  • bindPassword
  • baseDN
  • useTLS
  • protocolVersion

LdapClient LdapClient ([array $config = array()])
  • array $config: Configuration parameters
Destructor __destruct (line 171)

Class destructor

Closes the LDAP connection if opened.

void __destruct ()
add (line 433)

Adds a new entry on the LDAP server

Example:

  1.  $ldap new LdapClient(array(
  2.    'host' => 'ldap.yourdomain.com',
  3.    'bindDN' => 'cn=Administrator,dc=yourcompany,dc=com',
  4.    'bindPassword' => '123456',
  5.    'baseDN' => 'dc=yourcompany,dc=com'
  6.  ));
  7.  $entry new LdapEntry('uid=foo,dc=yourcompany,dc=com'array(
  8.    'cn' => 'Foo Bar',
  9.    'gn' => 'Foo',
  10.    'sn' => 'Bar',
  11.    'uid' => 'foo',
  12.    'userPassword' => UnixCrypt::encrypt('123456'),
  13.    'objectClass' => 'inetOrgPerson'
  14.  ));
  15.  $ldap->add($entry);

bool add (LdapEntry $entry)
close (line 560)

Closes the LDAP connection

void close ()
connect (line 240)

Opens the connection with the LDAP server

bool connect ()
delete (line 528)

Deletes an entry from the LDAP server

bool delete (LdapEntry|string $entry, [bool $recursive = TRUE])
  • LdapEntry|string $entry: Entry object or entry DN
  • bool $recursive: Delete entry's children recursively
find (line 392)

Searches for a given DN in the LDAP directory

Returns FALSE in case of error or entry not found. When DN is incomplete, base DN will be added.

Examples:

  1.  $client new LdapClient(array(
  2.    'bindDN' => 'cn=Administrator,dc=yourcompany,dc=com',
  3.    'bindPassword' => 'secret',
  4.    'baseDN' => 'dc=yourcompany,dc=com'
  5.  ));
  6.  $entry->find('uid=foo');
  7.  $entry->find('uid=foo,ou=people,dc=yourcompany,dc=com');

LdapEntry|FALSE find (string $dn)
  • string $dn: Entry DN
getHandle (line 284)

Get connection handle

resource &getHandle ()
getOption (line 295)

Reads an option from the LDAP server

mixed getOption (int $option)
  • int $option: Option number
isConnected (line 275)

Checks if connection is active

bool isConnected ()
search (line 338)

Performs a search on the LDAP server

Allowed keys for $settings:

LdapSearchResult|NULL search (string $filter, [array $settings = array()], [ $sort = array()])
  • string $filter: Search filter
  • array $settings: Search settings
  • $sort
setBaseDN (line 203)

Set base DN used to perform searches

void setBaseDN (string $baseDN)
  • string $baseDN: Base DN
setBindParameters (line 228)

Set bind parameters

When using LDAP, $bindDN is a distinguished name. When using AD, $bindDN is an email address.

Examples:

  1.  /* LDAP */
  2.  $client new LdapClient();
  3.  $client->setServer('ldap.yourcompany.com'389);
  4.  $client->setBindParameters('cn=Administrator,dc=yourcompany,dc=com''secret');
  5.  /* Microsoft AD */
  6.  $client new LdapClient();
  7.  $client->setServer('ad.yourcompany.com'389);
  8.  $client->setBindParameters('admin@yourcompany.com''secret');

void setBindParameters (string $bindDN, string $bindPassword)
  • string $bindDN: Bind DN
  • string $bindPassword: Bind password
setOption (line 313)

Changes an option on the LDAP server

bool setOption (int $option, mixed $value)
  • int $option: Option number
  • mixed $value: Option value
setProtocolVersion (line 180)

Set LDAP protocol version

void setProtocolVersion (int $version)
  • int $version: Protocol version
setServer (line 190)

Set LDAP host and port

void setServer (string $host, [int $port = NULL])
  • string $host: Host name or IP address
  • int $port: Port number
update (line 470)

Updates a given LDAP entry

Collects all changes stored in the class, performing attribute inserts, updates and removes sequentially.

Example:

  1.  $ldap new LdapClient(array(
  2.    'host' => 'ldap.yourdomain.com',
  3.    'bindDN' => 'cn=Administrator,dc=yourcompany,dc=com',
  4.    'bindPassword' => '123456',
  5.    'baseDN' => 'dc=yourcompany,dc=com'
  6.  ));
  7.  $entry $ldap->find('uid=foo');
  8.  $entry->setAttribute('gn''Foo');
  9.  $entry->setAttribute('sn''Bar');
  10.  $entry->addValues('mail'array('foo@foo.org''foobar@bar.org'));
  11.  $ldap->update($entry);

bool update (LdapEntry $entry)
_explodeDN (line 589)

Wrapper over the function ldap_explode_dn()

Ensures that a DN is properly escaped and encoded.

  • return: Exploded DN
  • access: private
array _explodeDN (string $dn)
  • string $dn: DN
_getErrorMessage (line 573)

Retrieves an error message from the server

  • access: private
string _getErrorMessage ()

Inherited Methods

Inherited From PHP2Go

PHP2Go::PHP2Go()
PHP2Go::equals()
PHP2Go::generateUniqueId()
PHP2Go::getClassName()
PHP2Go::getConfigVal()
PHP2Go::getLangVal()
PHP2Go::getObjectName()
PHP2Go::getParentName()
PHP2Go::hasDestructor()
PHP2Go::hashCode()
PHP2Go::isA()
PHP2Go::isSubclassOf()
PHP2Go::logError()
PHP2Go::raiseError()
PHP2Go::registerDestructor()
PHP2Go::registerShutdownFunc()
PHP2Go::retrieve()
PHP2Go::store()
PHP2Go::__toString()

Documentation generated on Sun, 07 Oct 2007 19:27:06 -0300 by phpDocumentor 1.3.2