1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the LdapTools package. |
4
|
|
|
* |
5
|
|
|
* (c) Chad Sikorra <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace chrmorandi\ldap; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Some common helper LDAP functions. |
16
|
|
|
* |
17
|
|
|
* @author Chad Sikorra <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class LdapUtils |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Converts a string distinguished name into its separate pieces. |
24
|
|
|
* |
25
|
|
|
* @param string $dn |
26
|
|
|
* @param int $withAttributes Set to 0 to get the attribute names along with the value. |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public static function explodeDn($dn, $withAttributes = 1) |
30
|
|
|
{ |
31
|
|
|
$pieces = ldap_explode_dn($dn, $withAttributes); |
32
|
|
|
|
33
|
|
|
if ($pieces === false || !isset($pieces['count']) || $pieces['count'] == 0) { |
34
|
|
|
throw new \yii\base\InvalidParamException(sprintf('Unable to parse DN "%s".', $dn)); |
35
|
|
|
} |
36
|
|
|
unset($pieces['count']); |
37
|
|
|
return $pieces; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Given a DN as an array in ['cn=Name', 'ou=Employees', 'dc=example', 'dc=com'] form, return it as its string |
42
|
|
|
* representation that is safe to pass back to a query or to save back to LDAP for a DN. |
43
|
|
|
* |
44
|
|
|
* @param array $dn |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public static function implodeDn(array $dn) |
48
|
|
|
{ |
49
|
|
|
foreach ($dn as $index => $piece) { |
50
|
|
|
$values = explode('=', $piece, 2); |
51
|
|
|
if (count($values) === 1) { |
52
|
|
|
throw new InvalidArgumentException(sprintf('Unable to parse DN piece "%s".', $values[0])); |
53
|
|
|
} |
54
|
|
|
$dn[$index] = $values[0].'='.$values[1]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return implode(',', $dn); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Given a full escaped DN return the RDN in escaped form. |
62
|
|
|
* |
63
|
|
|
* @param string $dn |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public static function getRdnFromDn($dn) |
67
|
|
|
{ |
68
|
|
|
$rdn = self::explodeDn($dn, 0)[0]; |
69
|
|
|
$rdn = explode('=', $rdn, 2); |
70
|
|
|
|
71
|
|
|
return $rdn[0].'='.$rdn[1]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Recursively implodes an array with optional key inclusion |
76
|
|
|
* |
77
|
|
|
* Example of $include_keys output: key, value, key, value, key, value |
78
|
|
|
* |
79
|
|
|
* @access public |
80
|
|
|
* @param array $array multi-dimensional array to recursively implode |
81
|
|
|
* @param string $glue value that glues elements together |
82
|
|
|
* @param bool $include_keys include keys before their values |
83
|
|
|
* @param bool $trim_all trim ALL whitespace from string |
84
|
|
|
* @return string imploded array |
85
|
|
|
*/ |
86
|
|
|
public static function recursive_implode(array $array, $glue = ',', $include_keys = false, $trim_all = true) |
87
|
|
|
{ |
88
|
|
|
$glued_string = ''; |
89
|
|
|
|
90
|
|
|
// Recursively iterates array and adds key/value to glued string |
91
|
|
|
array_walk_recursive($array, function($value, $key) use ($glue, $include_keys, &$glued_string) |
92
|
|
|
{ |
93
|
|
|
$include_keys and $glued_string .= $key.$glue; |
|
|
|
|
94
|
|
|
$glued_string .= $value.$glue; |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
// Removes last $glue from string |
98
|
|
|
strlen($glue) > 0 and $glued_string = substr($glued_string, 0, -strlen($glue)); |
|
|
|
|
99
|
|
|
|
100
|
|
|
// Trim ALL whitespace |
101
|
|
|
$trim_all and $glued_string = preg_replace("/(\s)/ixsm", '', $glued_string); |
|
|
|
|
102
|
|
|
|
103
|
|
|
return (string) $glued_string; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.