Completed
Push — master ( 3f374a...a9e3ec )
by Christopher
01:41
created

LdapHelper   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 1
dl 0
loc 86
rs 10
c 0
b 0
f 0

4 Methods

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