summaryrefslogtreecommitdiff
path: root/app/Core/Ldap/Query.php
blob: 0fe7f2d38eefc408efd089245dcfeb90c4a0836e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php

namespace Kanboard\Core\Ldap;

/**
 * LDAP Query
 *
 * @package ldap
 * @author  Frederic Guillot
 */
class Query
{
    /**
     * LDAP client
     *
     * @access protected
     * @var Client
     */
    protected $client = null;

    /**
     * Query result
     *
     * @access protected
     * @var array
     */
    protected $entries = array();

    /**
     * Constructor
     *
     * @access public
     * @param  Client $client
     */
    public function __construct(Client $client)
    {
        $this->client = $client;
    }

    /**
     * Execute query
     *
     * @access public
     * @param  string    $baseDn
     * @param  string    $filter
     * @param  array     $attributes
     * @param  integer   $limit
     * @return $this
     */
    public function execute($baseDn, $filter, array $attributes, $limit = 0)
    {
        if (DEBUG && $this->client->hasLogger()) {
            $this->client->getLogger()->debug('BaseDN='.$baseDn);
            $this->client->getLogger()->debug('Filter='.$filter);
            $this->client->getLogger()->debug('Attributes='.implode(', ', $attributes));
        }

        $sr = @ldap_search($this->client->getConnection(), $baseDn, $filter, $attributes, null, $limit);
        if ($sr === false) {
            return $this;
        }

        $entries = ldap_get_entries($this->client->getConnection(), $sr);
        if ($entries === false || count($entries) === 0 || $entries['count'] == 0) {
            return $this;
        }

        $this->entries = $entries;

        if (DEBUG && $this->client->hasLogger()) {
            $this->client->getLogger()->debug('NbEntries='.$entries['count']);
        }

        return $this;
    }

    /**
     * Return true if the query returned a result
     *
     * @access public
     * @return boolean
     */
    public function hasResult()
    {
        return ! empty($this->entries);
    }

    /**
     * Get LDAP Entries
     *
     * @access public
     * @return Entries
     */
    public function getEntries()
    {
        return new Entries($this->entries);
    }
}