1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) Enalean, 2014. All Rights Reserved. |
4
|
|
|
* |
5
|
|
|
* This file is a part of Tuleap. |
6
|
|
|
* |
7
|
|
|
* Tuleap is free software; you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* Tuleap is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with Tuleap. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Provide utility functions to query an LDAP directory |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
class LdapQueryEscaper { |
26
|
|
|
const LDAP_ESCAPE_FILTER = 1; |
27
|
|
|
const LDAP_ESCAPE_DN = 2; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Escape strings for safe use in an LDAP filter or DN |
31
|
|
|
* |
32
|
|
|
* @see RFC2254 define how string search filters must be represented |
33
|
|
|
* @see For PHP >= 5.6.0, ldap_escape() is a core function |
34
|
|
|
* |
35
|
|
|
* @author Chris Wright |
36
|
|
|
* @see https://github.com/DaveRandom/LDAPi/blob/master/src/global_functions.php |
37
|
|
|
* |
38
|
|
|
* @return String |
39
|
|
|
*/ |
40
|
|
|
private function escape($value, $ignore = '', $flags = 0) { |
41
|
|
|
if(function_exists('ldap_escape')) { |
42
|
|
|
return ldap_escape($value, $ignore, $flags); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$value = (string) $value; |
46
|
|
|
$ignore = (string) $ignore; |
47
|
|
|
$flags = (int) $flags; |
48
|
|
|
|
49
|
|
|
if ($value === '') { |
50
|
|
|
return ''; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$char_list = array(); |
54
|
|
|
if ($flags & self::LDAP_ESCAPE_FILTER) { |
55
|
|
|
$char_list = array("\\", "*", "(", ")", "\x00"); |
56
|
|
|
} |
57
|
|
|
if ($flags & self::LDAP_ESCAPE_DN) { |
58
|
|
|
$char_list = array_merge($char_list, array("\\", ",", "=", "+", "<", ">", ";", "\"", "#")); |
59
|
|
|
} |
60
|
|
|
if (!$char_list) { |
|
|
|
|
61
|
|
|
for ($i = 0; $i < 256; $i++) { |
62
|
|
|
$char_list[] = chr($i); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
$char_list = array_flip($char_list); |
66
|
|
|
|
67
|
|
|
for ($i = 0; isset($ignore[$i]); $i++) { |
68
|
|
|
unset($char_list[$ignore[$i]]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
foreach ($char_list as $k => &$v) { |
72
|
|
|
$v = sprintf('\%02x', ord($k)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return strtr($value, $char_list); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function escapeFilter($value) { |
79
|
|
|
return $this->escape($value, '', self::LDAP_ESCAPE_FILTER); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.