forked from factorenergia/yii2-ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLdapUtils.php
106 lines (91 loc) · 3.26 KB
/
LdapUtils.php
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
<?php
/**
* This file is part of the LdapTools package.
*
* (c) Chad Sikorra <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace websvc\ldap;
/**
* Some common helper LDAP functions.
*
* @author Chad Sikorra <[email protected]>
*/
class LdapUtils
{
/**
* Converts a string distinguished name into its separate pieces.
*
* @param string $dn
* @param int $withAttributes Set to 0 to get the attribute names along with the value.
* @return array
*/
public static function explodeDn($dn, $withAttributes = 1)
{
$pieces = ldap_explode_dn($dn, $withAttributes);
if ($pieces === false || !isset($pieces['count']) || $pieces['count'] == 0) {
throw new \yii\base\InvalidParamException(sprintf('Unable to parse DN "%s".', $dn));
}
unset($pieces['count']);
return $pieces;
}
/**
* Given a DN as an array in ['cn=Name', 'ou=Employees', 'dc=example', 'dc=com'] form, return it as its string
* representation that is safe to pass back to a query or to save back to LDAP for a DN.
*
* @param array $dn
* @return string
*/
public static function implodeDn(array $dn)
{
foreach ($dn as $index => $piece) {
$values = explode('=', $piece, 2);
if (count($values) === 1) {
throw new InvalidArgumentException(sprintf('Unable to parse DN piece "%s".', $values[0]));
}
$dn[$index] = $values[0].'='.$values[1];
}
return implode(',', $dn);
}
/**
* Given a full escaped DN return the RDN in escaped form.
*
* @param string $dn
* @return string
*/
public static function getRdnFromDn($dn)
{
$rdn = self::explodeDn($dn, 0)[0];
$rdn = explode('=', $rdn, 2);
return $rdn[0].'='.$rdn[1];
}
/**
* Recursively implodes an array with optional key inclusion
*
* Example of $include_keys output: key, value, key, value, key, value
*
* @access public
* @param array $array multi-dimensional array to recursively implode
* @param string $glue value that glues elements together
* @param bool $include_keys include keys before their values
* @param bool $trim_all trim ALL whitespace from string
* @return string imploded array
*/
public static function recursive_implode(array $array, $glue = ',', $include_keys = false, $trim_all = true)
{
$glued_string = '';
// Recursively iterates array and adds key/value to glued string
array_walk_recursive($array, function($value, $key) use ($glue, $include_keys, &$glued_string)
{
$include_keys and $glued_string .= $key.$glue;
$glued_string .= $value.$glue;
});
// Removes last $glue from string
strlen($glue) > 0 and $glued_string = substr($glued_string, 0, -strlen($glue));
// Trim ALL whitespace
$trim_all and $glued_string = preg_replace("/(\s)/ixsm", '', $glued_string);
return (string) $glued_string;
}
}