2016-02-08 18:00:45 +00:00
< ? php
/*
* 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 .
*/
namespace Symfony\Component\Ldap ;
2017-12-31 05:33:21 +00:00
@ trigger_error ( 'The ' . __NAMESPACE__ . '\LdapClient class is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Ldap class directly instead.' , E_USER_DEPRECATED );
2016-06-01 20:47:08 +01:00
2016-02-08 18:00:45 +00:00
/**
* @ author Grégoire Pineau < lyrixx @ lyrixx . info >
* @ author Francis Besset < francis . besset @ gmail . com >
* @ author Charles Sarrazin < charles @ sarraz . in >
*
2017-05-20 14:43:53 +01:00
* @ deprecated since version 3.1 , to be removed in 4.0 . Use the Ldap class instead .
2016-02-08 18:00:45 +00:00
*/
final class LdapClient implements LdapClientInterface
{
private $ldap ;
public function __construct ( $host = null , $port = 389 , $version = 3 , $useSsl = false , $useStartTls = false , $optReferrals = false , LdapInterface $ldap = null )
{
2016-06-01 20:47:08 +01:00
$config = $this -> normalizeConfig ( $host , $port , $version , $useSsl , $useStartTls , $optReferrals );
2016-02-08 18:00:45 +00:00
$this -> ldap = null !== $ldap ? $ldap : Ldap :: create ( 'ext_ldap' , $config );
}
/**
* { @ inheritdoc }
*/
public function bind ( $dn = null , $password = null )
{
$this -> ldap -> bind ( $dn , $password );
}
2016-03-09 12:30:20 +00:00
/**
* { @ inheritdoc }
*/
public function query ( $dn , $query , array $options = array ())
{
return $this -> ldap -> query ( $dn , $query , $options );
}
/**
* { @ inheritdoc }
*/
public function getEntryManager ()
{
return $this -> ldap -> getEntryManager ();
}
2016-02-08 18:00:45 +00:00
/**
* { @ inheritdoc }
*/
public function find ( $dn , $query , $filter = '*' )
{
2017-12-31 05:33:21 +00:00
@ trigger_error ( 'The "find" method is deprecated since Symfony 3.1 and will be removed in 4.0. Use the "query" method instead.' , E_USER_DEPRECATED );
2016-02-08 18:00:45 +00:00
$query = $this -> ldap -> query ( $dn , $query , array ( 'filter' => $filter ));
$entries = $query -> execute ();
2016-09-19 17:37:53 +01:00
$result = array (
'count' => 0 ,
);
2016-02-08 18:00:45 +00:00
foreach ( $entries as $entry ) {
$resultEntry = array ();
foreach ( $entry -> getAttributes () as $attribute => $values ) {
2016-09-19 17:37:53 +01:00
$resultAttribute = array (
'count' => count ( $values ),
);
foreach ( $values as $val ) {
$resultAttribute [] = $val ;
}
$attributeName = strtolower ( $attribute );
2016-02-08 18:00:45 +00:00
$resultAttribute [ 'count' ] = count ( $values );
2016-09-19 17:37:53 +01:00
$resultEntry [ $attributeName ] = $resultAttribute ;
$resultEntry [] = $attributeName ;
2016-02-08 18:00:45 +00:00
}
$resultEntry [ 'count' ] = count ( $resultEntry ) / 2 ;
2016-09-19 17:37:53 +01:00
$resultEntry [ 'dn' ] = $entry -> getDn ();
2016-02-08 18:00:45 +00:00
$result [] = $resultEntry ;
}
2016-09-19 17:37:53 +01:00
$result [ 'count' ] = count ( $result ) - 1 ;
2016-02-08 18:00:45 +00:00
return $result ;
}
/**
* { @ inheritdoc }
*/
public function escape ( $subject , $ignore = '' , $flags = 0 )
{
return $this -> ldap -> escape ( $subject , $ignore , $flags );
}
2016-06-01 20:47:08 +01:00
private function normalizeConfig ( $host , $port , $version , $useSsl , $useStartTls , $optReferrals )
{
if (( bool ) $useSsl ) {
$encryption = 'ssl' ;
} elseif (( bool ) $useStartTls ) {
$encryption = 'tls' ;
} else {
$encryption = 'none' ;
}
return array (
'host' => $host ,
'port' => $port ,
'encryption' => $encryption ,
'options' => array (
'protocol_version' => $version ,
'referrals' => ( bool ) $optReferrals ,
),
);
}
2016-02-08 18:00:45 +00:00
}