summaryrefslogtreecommitdiff
path: root/app/Core/Ldap/Query.php
blob: 1c34fa10e1b0f6503aba7a68ebaa2fd0ad9e11b9 (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
<?php

namespace Kanboard\Core\Ldap;

/**
 * LDAP Query
 *
 * @package ldap
 * @author  Frederic Guillot
 */
class Query
{
    /**
     * Query result
     *
     * @access private
     * @var array
     */
    private $entries = array();

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

    /**
     * Execute query
     *
     * @access public
     * @param  resource  $ldap
     * @param  string    $baseDn
     * @param  string    $filter
     * @param  array     $attributes
     * @return Query
     */
    public function execute($ldap, $baseDn, $filter, array $attributes)
    {
        $sr = ldap_search($ldap, $baseDn, $filter, $attributes);
        if ($sr === false) {
            return $this;
        }

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

        $this->entries = $entries;

        return $this;
    }

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

    /**
     * Return subset of entries
     *
     * @access public
     * @param  string   $key
     * @param  mixed    $default
     * @return array
     */
    public function getAttribute($key, $default = null)
    {
        return isset($this->entries[0][$key]) ? $this->entries[0][$key] : $default;
    }

    /**
     * Return one entry from a list of entries
     *
     * @access public
     * @param  string   $key         Key
     * @param  string   $default     Default value if key not set in entry
     * @return string
     */
    public function getAttributeValue($key, $default = '')
    {
        return isset($this->entries[0][$key][0]) ? $this->entries[0][$key][0] : $default;
    }
}