|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/chrmorandi/yii2-ldap for the source repository |
|
4
|
|
|
* @package yii2-ldap |
|
5
|
|
|
* @author Christopher Mota <[email protected]> |
|
6
|
|
|
* @license MIT License - view the LICENSE file that was distributed with this source code. |
|
7
|
|
|
* @since 1.0.0 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace chrmorandi\ldap; |
|
11
|
|
|
|
|
12
|
|
|
use yii\base\InvalidArgumentException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Some common helper LDAP functions. |
|
16
|
|
|
*/ |
|
17
|
|
|
class LdapHelper |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Converts a string distinguished name into its separate pieces. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $dn |
|
23
|
|
|
* @param int $withAttributes Set to 0 to get the attribute names along with the value. |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function explodeDn($dn, $withAttributes = 1) |
|
27
|
|
|
{ |
|
28
|
|
|
$pieces = ldap_explode_dn($dn, $withAttributes); |
|
29
|
|
|
|
|
30
|
|
|
if ($pieces === false || !isset($pieces['count']) || $pieces['count'] == 0) { |
|
31
|
|
|
throw new InvalidArgumentException(sprintf('Unable to parse DN "%s".', $dn)); |
|
32
|
|
|
} |
|
33
|
|
|
unset($pieces['count']); |
|
34
|
|
|
return $pieces; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Given a DN as an array in ['cn=Name', 'ou=Employees', 'dc=example', 'dc=com'] form, return it as its string |
|
39
|
|
|
* representation that is safe to pass back to a query or to save back to LDAP for a DN. |
|
40
|
|
|
* |
|
41
|
|
|
* @param array $dn |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function implodeDn(array $dn) |
|
45
|
|
|
{ |
|
46
|
|
|
foreach ($dn as $index => $piece) { |
|
47
|
|
|
$values = explode('=', $piece, 2); |
|
48
|
|
|
if (count($values) === 1) { |
|
49
|
|
|
throw new InvalidArgumentException(sprintf('Unable to parse DN piece "%s".', $values[0])); |
|
50
|
|
|
} |
|
51
|
|
|
$dn[$index] = $values[0] . '=' . $values[1]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return implode(',', $dn); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Given a full escaped DN return the RDN in escaped form. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $dn |
|
61
|
|
|
* @return string Return string like "attribute = value" |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function getRdnFromDn($dn) |
|
64
|
|
|
{ |
|
65
|
|
|
$rdn = self::explodeDn($dn, 0)[0]; |
|
66
|
|
|
$rdn = explode('=', $rdn, 2); |
|
67
|
|
|
|
|
68
|
|
|
return $rdn[0] . '=' . $rdn[1]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Recursively implodes an array with optional key inclusion |
|
73
|
|
|
* |
|
74
|
|
|
* Example of $include_keys output: key, value, key, value, key, value |
|
75
|
|
|
* |
|
76
|
|
|
* @access public |
|
77
|
|
|
* @param array $array multi-dimensional array to recursively implode |
|
78
|
|
|
* @param string $glue value that glues elements together |
|
79
|
|
|
* @param bool $include_keys include keys before their values |
|
80
|
|
|
* @param bool $trim_all trim ALL whitespace from string |
|
81
|
|
|
* @return string imploded array |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function recursiveImplode(array $array, $glue = ',', $include_keys = false, $trim_all = true) |
|
84
|
|
|
{ |
|
85
|
|
|
$glued_string = ''; |
|
86
|
|
|
|
|
87
|
|
|
// Recursively iterates array and adds key/value to glued string |
|
88
|
|
|
array_walk_recursive($array, function($value, $key) use ($glue, $include_keys, &$glued_string) { |
|
89
|
|
|
$include_keys and $glued_string .= $key . $glue; |
|
90
|
|
|
$glued_string .= $value . $glue; |
|
91
|
|
|
}); |
|
92
|
|
|
|
|
93
|
|
|
// Removes last $glue from string |
|
94
|
|
|
strlen($glue) > 0 and $glued_string = substr($glued_string, 0, -strlen($glue)); |
|
95
|
|
|
|
|
96
|
|
|
// Trim ALL whitespace |
|
97
|
|
|
$trim_all and $glued_string = preg_replace("/(\s)/ixsm", '', $glued_string); |
|
98
|
|
|
|
|
99
|
|
|
return (string) $glued_string; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|