2015-09-25 10:29:08 +01:00
|
|
|
<?php
|
|
|
|
|
2015-09-28 22:09:00 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2015-09-25 10:29:08 +01:00
|
|
|
namespace Symfony\Component\Ldap;
|
|
|
|
|
|
|
|
use Symfony\Component\Ldap\Exception\ConnectionException;
|
|
|
|
use Symfony\Component\Ldap\Exception\LdapException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Grégoire Pineau <lyrixx@lyrixx.info>
|
|
|
|
* @author Francis Besset <francis.besset@gmail.com>
|
|
|
|
* @author Charles Sarrazin <charles@sarraz.in>
|
2015-11-28 22:09:12 +00:00
|
|
|
*
|
|
|
|
* @internal
|
2015-09-25 10:29:08 +01:00
|
|
|
*/
|
|
|
|
class LdapClient implements LdapClientInterface
|
|
|
|
{
|
|
|
|
private $host;
|
|
|
|
private $port;
|
|
|
|
private $version;
|
|
|
|
private $useSsl;
|
|
|
|
private $useStartTls;
|
|
|
|
private $optReferrals;
|
|
|
|
private $connection;
|
|
|
|
private $charmaps;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param string $host
|
|
|
|
* @param int $port
|
|
|
|
* @param int $version
|
|
|
|
* @param bool $useSsl
|
|
|
|
* @param bool $useStartTls
|
|
|
|
* @param bool $optReferrals
|
|
|
|
*/
|
|
|
|
public function __construct($host = null, $port = 389, $version = 3, $useSsl = false, $useStartTls = false, $optReferrals = false)
|
|
|
|
{
|
|
|
|
if (!extension_loaded('ldap')) {
|
|
|
|
throw new LdapException('The ldap module is needed.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->host = $host;
|
|
|
|
$this->port = $port;
|
|
|
|
$this->version = $version;
|
|
|
|
$this->useSsl = (bool) $useSsl;
|
|
|
|
$this->useStartTls = (bool) $useStartTls;
|
|
|
|
$this->optReferrals = (bool) $optReferrals;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
$this->disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function bind($dn = null, $password = null)
|
|
|
|
{
|
|
|
|
if (!$this->connection) {
|
|
|
|
$this->connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false === @ldap_bind($this->connection, $dn, $password)) {
|
|
|
|
throw new ConnectionException(ldap_error($this->connection));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function find($dn, $query, $filter = '*')
|
|
|
|
{
|
|
|
|
if (!is_array($filter)) {
|
|
|
|
$filter = array($filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
$search = ldap_search($this->connection, $dn, $query, $filter);
|
|
|
|
$infos = ldap_get_entries($this->connection, $search);
|
|
|
|
|
|
|
|
if (0 === $infos['count']) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $infos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function escape($subject, $ignore = '', $flags = 0)
|
|
|
|
{
|
2015-10-14 15:40:43 +01:00
|
|
|
return ldap_escape($subject, $ignore, $flags);
|
2015-09-25 10:29:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function connect()
|
|
|
|
{
|
|
|
|
if (!$this->connection) {
|
|
|
|
$host = $this->host;
|
|
|
|
|
|
|
|
if ($this->useSsl) {
|
|
|
|
$host = 'ldaps://'.$host;
|
|
|
|
}
|
|
|
|
|
2015-11-03 14:09:23 +00:00
|
|
|
$this->connection = ldap_connect($host, $this->port);
|
|
|
|
|
2015-09-25 10:29:08 +01:00
|
|
|
ldap_set_option($this->connection, LDAP_OPT_PROTOCOL_VERSION, $this->version);
|
|
|
|
ldap_set_option($this->connection, LDAP_OPT_REFERRALS, $this->optReferrals);
|
|
|
|
|
|
|
|
if ($this->useStartTls) {
|
|
|
|
ldap_start_tls($this->connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function disconnect()
|
|
|
|
{
|
|
|
|
if ($this->connection && is_resource($this->connection)) {
|
|
|
|
ldap_unbind($this->connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->connection = null;
|
|
|
|
}
|
|
|
|
}
|