summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Protocol/Ldap/LdapQuery.php
blob: f4e198626c7f7adabb485243dfe0a5b276eef3a9 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Protocol\Ldap;

use Icinga\Data\Filter\Filter;
use LogicException;
use Icinga\Data\SimpleQuery;

/**
 * LDAP query class
 */
class LdapQuery extends SimpleQuery
{
    /**
     * The base dn being used for this query
     *
     * @var string
     */
    protected $base;

    /**
     * Whether this query is permitted to utilize paged results
     *
     * @var bool
     */
    protected $usePagedResults;

    /**
     * The name of the attribute used to unfold the result
     *
     * @var string
     */
    protected $unfoldAttribute;

    /**
     * This query's native LDAP filter
     *
     * @var string
     */
    protected $nativeFilter;

    /**
     * Only fetch the entry at the base of the search
     */
    const SCOPE_BASE = 'base';

    /**
     * Fetch entries one below the base DN
     */
    const SCOPE_ONE = 'one';

    /**
     * Fetch all entries below the base DN
     */
    const SCOPE_SUB = 'sub';

    /**
     * All available scopes
     *
     * @var array
     */
    public static $scopes = array(
        LdapQuery::SCOPE_BASE,
        LdapQuery::SCOPE_ONE,
        LdapQuery::SCOPE_SUB
    );

    /**
     * LDAP search scope (default: SCOPE_SUB)
     *
     * @var string
     */
    protected $scope = LdapQuery::SCOPE_SUB;

    /**
     * Initialize this query
     */
    protected function init()
    {
        $this->usePagedResults = false;
    }

    /**
     * Set the base dn to be used for this query
     *
     * @param   string  $base
     *
     * @return  $this
     */
    public function setBase($base)
    {
        $this->base = $base;
        return $this;
    }

    /**
     * Return the base dn being used for this query
     *
     * @return  string
     */
    public function getBase()
    {
        return $this->base;
    }

    /**
     * Set whether this query is permitted to utilize paged results
     *
     * @param   bool    $state
     *
     * @return  $this
     */
    public function setUsePagedResults($state = true)
    {
        $this->usePagedResults = (bool) $state;
        return $this;
    }

    /**
     * Return whether this query is permitted to utilize paged results
     *
     * @return  bool
     */
    public function getUsePagedResults()
    {
        return $this->usePagedResults;
    }

    /**
     * Set the attribute to be used to unfold the result
     *
     * @param   string  $attributeName
     *
     * @return  $this
     */
    public function setUnfoldAttribute($attributeName)
    {
        $this->unfoldAttribute = $attributeName;
        return $this;
    }

    /**
     * Return the attribute to use to unfold the result
     *
     * @return  string
     */
    public function getUnfoldAttribute()
    {
        return $this->unfoldAttribute;
    }

    /**
     * Set this query's native LDAP filter
     *
     * @param   string  $filter
     *
     * @return  $this
     */
    public function setNativeFilter($filter)
    {
        $this->nativeFilter = $filter;
        return $this;
    }

    /**
     * Return this query's native LDAP filter
     *
     * @return  string
     */
    public function getNativeFilter()
    {
        return $this->nativeFilter;
    }

    /**
     * Choose an objectClass and the columns you are interested in
     *
     * {@inheritdoc} This creates an objectClass filter.
     */
    public function from($target, array $fields = null)
    {
        $this->where('objectClass', $target);
        return parent::from($target, $fields);
    }

    public function where($condition, $value = null)
    {
        $this->addFilter(Filter::expression($condition, '=', $value));
        return $this;
    }

    public function addFilter(Filter $filter)
    {
        $this->makeCaseInsensitive($filter);
        return parent::addFilter($filter);
    }

    public function setFilter(Filter $filter)
    {
        $this->makeCaseInsensitive($filter);
        return parent::setFilter($filter);
    }

    protected function makeCaseInsensitive(Filter $filter)
    {
        if ($filter->isExpression()) {
            /** @var \Icinga\Data\Filter\FilterExpression $filter */
            $filter->setCaseSensitive(false);
        } else {
            /** @var \Icinga\Data\Filter\FilterChain $filter */
            foreach ($filter->filters() as $subFilter) {
                $this->makeCaseInsensitive($subFilter);
            }
        }
    }

    public function compare($a, $b, $orderIndex = 0)
    {
        if (array_key_exists($orderIndex, $this->order)) {
            $column = $this->order[$orderIndex][0];
            $direction = $this->order[$orderIndex][1];

            $flippedColumns = $this->flippedColumns ?: array_flip($this->columns);
            if (array_key_exists($column, $flippedColumns) && is_string($flippedColumns[$column])) {
                $column = $flippedColumns[$column];
            }

            if (is_array($a->$column)) {
                // rfc2891 states: If a sort key is a multi-valued attribute, and an entry happens to
                //   have multiple values for that attribute and no other controls are
                //   present that affect the sorting order, then the server SHOULD use the
                //   least value (according to the ORDERING rule for that attribute).
                $a = clone $a;
                $a->$column = array_reduce($a->$column, function ($carry, $item) use ($direction) {
                    $result = $carry === null ? 0 : strcmp($item, $carry);
                    return ($direction === self::SORT_ASC ? $result : $result * -1) < 1 ? $item : $carry;
                });
            }

            if (is_array($b->$column)) {
                $b = clone $b;
                $b->$column = array_reduce($b->$column, function ($carry, $item) use ($direction) {
                    $result = $carry === null ? 0 : strcmp($item, $carry);
                    return ($direction === self::SORT_ASC ? $result : $result * -1) < 1 ? $item : $carry;
                });
            }
        }

        return parent::compare($a, $b, $orderIndex);
    }

    /**
     * Fetch result as tree
     *
     * @return  Root
     *
     * @todo    This is untested waste, not being used anywhere and ignores the query's order and base dn.
     *           Evaluate whether it's reasonable to properly implement and test it.
     */
    public function fetchTree()
    {
        $result = $this->fetchAll();
        $sorted = array();
        $quotedDn = preg_quote($this->ds->getDn(), '/');
        foreach ($result as $key => & $item) {
            $new_key = LdapUtils::implodeDN(
                array_reverse(
                    LdapUtils::explodeDN(
                        preg_replace('/,' . $quotedDn . '$/', '', $key)
                    )
                )
            );
            $sorted[$new_key] = $key;
        }

        ksort($sorted);

        $tree = Root::forConnection($this->ds);
        $root_dn = $tree->getDN();
        foreach ($sorted as $sort_key => & $key) {
            if ($key === $root_dn) {
                continue;
            }
            $tree->createChildByDN($key, $result[$key]);
        }
        return $tree;
    }

    /**
     * Fetch the distinguished name of the first result
     *
     * @return  string|false    The distinguished name or false in case it's not possible to fetch a result
     *
     * @throws  LdapException   In case the query returns multiple results
     *                          (i.e. it's not possible to fetch a unique DN)
     */
    public function fetchDn()
    {
        return $this->ds->fetchDn($this);
    }

    /**
     * Render and return this query's filter
     *
     * @return  string
     */
    public function renderFilter()
    {
        $filter = $this->ds->renderFilter($this->filter);
        if ($this->nativeFilter) {
            $filter = '(&(' . $this->nativeFilter . ')' . $filter . ')';
        }

        return $filter;
    }

    /**
     * Return the LDAP filter to be applied on this query
     *
     * @return  string
     */
    public function __toString()
    {
        return $this->renderFilter();
    }

    /**
     * Get LDAP search scope
     *
     * @return string
     */
    public function getScope()
    {
        return $this->scope;
    }

    /**
     * Set LDAP search scope
     *
     * Valid: sub one base (Default: sub)
     *
     * @param  string          $scope
     *
     * @return LdapQuery
     *
     * @throws LogicException  If scope value is invalid
     */
    public function setScope($scope)
    {
        if (! in_array($scope, static::$scopes)) {
            throw new LogicException(
                'Can\'t set scope %d, it is is invalid. Use one of %s or LdapQuery\'s constants.',
                $scope,
                implode(', ', static::$scopes)
            );
        }
        $this->scope = $scope;
        return $this;
    }
}