Skip to content

Commit d78d973

Browse files
committed
Fixed CS and hardened some code
1 parent 41daf2c commit d78d973

File tree

5 files changed

+93
-46
lines changed

5 files changed

+93
-46
lines changed

Adapter/AdapterInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public function getConnection();
2626
/**
2727
* Creates a new Query.
2828
*
29-
* @param $dn
30-
* @param $query
31-
* @param array $options
29+
* @param string $dn
30+
* @param string $query
31+
* @param array $options
3232
*
3333
* @return QueryInterface
3434
*/

Adapter/ExtLdap/Collection.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Ldap\Adapter\CollectionInterface;
1515
use Symfony\Component\Ldap\Entry;
16+
use Symfony\Component\Ldap\Exception\LdapException;
1617

1718
/**
1819
* @author Charles Sarrazin <charles@sarraz.in>
@@ -84,7 +85,13 @@ private function initialize()
8485
return;
8586
}
8687

87-
$entries = ldap_get_entries($this->connection->getResource(), $this->search->getResource());
88+
$con = $this->connection->getResource();
89+
90+
$entries = ldap_get_entries($con, $this->search->getResource());
91+
92+
if (false === $entries) {
93+
throw new LdapException(sprintf('Could not load entries: %s', ldap_error($con)));
94+
}
8895

8996
if (0 === $entries['count']) {
9097
return array();
@@ -94,15 +101,22 @@ private function initialize()
94101

95102
$this->entries = array_map(function (array $entry) {
96103
$dn = $entry['dn'];
97-
$attributes = array_diff_key($entry, array_flip(range(0, $entry['count'] - 1)) + array(
104+
$attributes = $this->cleanupAttributes($entry);
105+
106+
return new Entry($dn, $attributes);
107+
}, $entries);
108+
}
109+
110+
private function cleanupAttributes(array $entry = array())
111+
{
112+
$attributes = array_diff_key($entry, array_flip(range(0, $entry['count'] - 1)) + array(
98113
'count' => null,
99114
'dn' => null,
100115
));
101-
array_walk($attributes, function (&$value) {
102-
unset($value['count']);
103-
});
116+
array_walk($attributes, function (&$value) {
117+
unset($value['count']);
118+
});
104119

105-
return new Entry($dn, $attributes);
106-
}, $entries);
120+
return $attributes;
107121
}
108122
}

Adapter/ExtLdap/Connection.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Ldap\Adapter\AbstractConnection;
1515
use Symfony\Component\Ldap\Exception\ConnectionException;
16+
use Symfony\Component\Ldap\Exception\LdapException;
1617

1718
/**
1819
* @author Charles Sarrazin <charles@sarraz.in>
@@ -30,6 +31,9 @@ public function __destruct()
3031
$this->disconnect();
3132
}
3233

34+
/**
35+
* {@inheritdoc}
36+
*/
3337
public function isBound()
3438
{
3539
return $this->bound;
@@ -55,6 +59,8 @@ public function bind($dn = null, $password = null)
5559
* Returns a link resource.
5660
*
5761
* @return resource
62+
*
63+
* @internal
5864
*/
5965
public function getResource()
6066
{
@@ -66,15 +72,20 @@ private function connect()
6672
if ($this->connection) {
6773
return;
6874
}
75+
6976
$host = $this->config['host'];
7077

7178
ldap_set_option($this->connection, LDAP_OPT_PROTOCOL_VERSION, $this->config['version']);
7279
ldap_set_option($this->connection, LDAP_OPT_REFERRALS, $this->config['optReferrals']);
7380

7481
$this->connection = ldap_connect($host, $this->config['port']);
7582

76-
if ($this->config['useStartTls']) {
77-
ldap_start_tls($this->connection);
83+
if (false === $this->connection) {
84+
throw new LdapException(sprintf('Could not connect to Ldap server: %s', ldap_error($this->connection)));
85+
}
86+
87+
if ($this->config['useStartTls'] && false === ldap_start_tls($this->connection)) {
88+
throw new LdapException(sprintf('Could not initiate TLS connection: %s', ldap_error($this->connection)));
7889
}
7990
}
8091

@@ -85,5 +96,6 @@ private function disconnect()
8596
}
8697

8798
$this->connection = null;
99+
$this->bound = false;
88100
}
89101
}

Adapter/ExtLdap/Query.php

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,51 @@ public function __construct(Connection $connection, $dn, $query, array $options
3030
parent::__construct($connection, $dn, $query, $options);
3131
}
3232

33+
public function __destruct()
34+
{
35+
$con = $this->connection->getResource();
36+
$this->connection = null;
37+
38+
if (null === $this->search) {
39+
return;
40+
}
41+
42+
$success = ldap_free_result($this->search);
43+
$this->search = null;
44+
45+
if (!$success) {
46+
throw new LdapException(sprintf('Could not free results: %s', ldap_error($con)));
47+
}
48+
}
49+
3350
/**
3451
* {@inheritdoc}
3552
*/
3653
public function execute()
3754
{
38-
// If the connection is not bound, then we try an anonymous bind.
39-
if (!$this->connection->isBound()) {
40-
$this->connection->bind();
41-
}
55+
if (null === $this->search) {
56+
// If the connection is not bound, then we try an anonymous bind.
57+
if (!$this->connection->isBound()) {
58+
$this->connection->bind();
59+
}
4260

43-
$con = $this->connection->getResource();
61+
$con = $this->connection->getResource();
4462

45-
$this->search = ldap_search(
46-
$con,
47-
$this->dn,
48-
$this->query,
49-
$this->options['filter'],
50-
$this->options['attrsOnly'],
51-
$this->options['maxItems'],
52-
$this->options['timeout'],
53-
$this->options['deref']
54-
);
55-
56-
if (!$this->search) {
63+
$this->search = ldap_search(
64+
$con,
65+
$this->dn,
66+
$this->query,
67+
$this->options['filter'],
68+
$this->options['attrsOnly'],
69+
$this->options['maxItems'],
70+
$this->options['timeout'],
71+
$this->options['deref']
72+
);
73+
}
74+
75+
if (false === $this->search) {
5776
throw new LdapException(sprintf('Could not complete search with dn "%s", query "%s" and filters "%s"', $this->dn, $this->query, implode(',', $this->options['filter'])));
58-
};
77+
}
5978

6079
return new Collection($this->connection, $this);
6180
}
@@ -64,6 +83,8 @@ public function execute()
6483
* Returns a LDAP search resource.
6584
*
6685
* @return resource
86+
*
87+
* @internal
6788
*/
6889
public function getResource()
6990
{

Adapter/ExtLdap/ResultIterator.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
namespace Symfony\Component\Ldap\Adapter\ExtLdap;
1313

1414
use Symfony\Component\Ldap\Entry;
15+
use Symfony\Component\Ldap\Exception\LdapException;
1516

1617
/**
1718
* @author Charles Sarrazin <charles@sarraz.in>
19+
*
20+
* @internal
1821
*/
1922
class ResultIterator implements \Iterator
2023
{
@@ -37,45 +40,42 @@ public function __construct(Connection $connection, Query $search)
3740
public function current()
3841
{
3942
$attributes = ldap_get_attributes($this->connection, $this->current);
43+
44+
if (false === $attributes) {
45+
throw new LdapException(sprintf('Could not fetch attributes: %s', ldap_error($this->connection)));
46+
}
47+
4048
$dn = ldap_get_dn($this->connection, $this->current);
4149

50+
if (false === $dn) {
51+
throw new LdapException(sprintf('Could not fetch DN: %s', ldap_error($this->connection)));
52+
}
53+
4254
return new Entry($dn, $attributes);
4355
}
4456

45-
/**
46-
* Sets the cursor to the next entry.
47-
*/
4857
public function next()
4958
{
5059
$this->current = ldap_next_entry($this->connection, $this->current);
5160
++$this->key;
5261
}
5362

54-
/**
55-
* Returns the current key.
56-
*
57-
* @return int
58-
*/
5963
public function key()
6064
{
6165
return $this->key;
6266
}
6367

64-
/**
65-
* Checks whether the current entry is valid or not.
66-
*
67-
* @return bool
68-
*/
6968
public function valid()
7069
{
7170
return false !== $this->current;
7271
}
7372

74-
/**
75-
* Rewinds the iterator to the first entry.
76-
*/
7773
public function rewind()
7874
{
7975
$this->current = ldap_first_entry($this->connection, $this->search);
76+
77+
if (false === $this->current) {
78+
throw new LdapException(sprintf('Could not rewind entries array: %s', ldap_error($this->connection)));
79+
}
8080
}
8181
}

0 commit comments

Comments
 (0)