Passed
Push — master ( 344c14...9aa841 )
by Christopher
01:46
created

src/LdapHelper.php (1 issue)

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
use yii\base\InvalidParamException;
14
15
/**
16
 * Some common helper LDAP functions.
17
 */
18
class LdapHelper
19
{
20
    /**
21
     * Converts a string distinguished name into its separate pieces.
22
     *
23
     * @param string $dn
24
     * @param int $withAttributes Set to 0 to get the attribute names along with the value.
25
     * @return array
26
     */
27
    public static function explodeDn($dn, $withAttributes = 1)
28
    {
29
        $pieces = ldap_explode_dn($dn, $withAttributes);
30
31
        if ($pieces === false || !isset($pieces['count']) || $pieces['count'] == 0) {
32
            throw new InvalidParamException(sprintf('Unable to parse DN "%s".', $dn));
0 ignored issues
show
Deprecated Code introduced by
The class yii\base\InvalidParamException has been deprecated: since 2.0.14. Use [[InvalidArgumentException]] instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

32
            throw /** @scrutinizer ignore-deprecated */ new InvalidParamException(sprintf('Unable to parse DN "%s".', $dn));
Loading history...
33
        }
34
        unset($pieces['count']);
35
        return $pieces;
36
    }
37
38
    /**
39
     * Given a DN as an array in ['cn=Name', 'ou=Employees', 'dc=example', 'dc=com'] form, return it as its string
40
     * representation that is safe to pass back to a query or to save back to LDAP for a DN.
41
     *
42
     * @param array $dn
43
     * @return string
44
     */
45
    public static function implodeDn(array $dn)
46
    {
47
        foreach ($dn as $index => $piece) {
48
            $values = explode('=', $piece, 2);
49
            if (count($values) === 1) {
50
                throw new InvalidArgumentException(sprintf('Unable to parse DN piece "%s".', $values[0]));
51
            }
52
            $dn[$index] = $values[0] . '=' . $values[1];
53
        }
54
55
        return implode(',', $dn);
56
    }
57
58
    /**
59
     * Given a full escaped DN return the RDN in escaped form.
60
     *
61
     * @param string $dn
62
     * @return string Return string like "attribute = value"
63
     */
64
    public static function getRdnFromDn($dn)
65
    {
66
        $rdn = self::explodeDn($dn, 0)[0];
67
        $rdn = explode('=', $rdn, 2);
68
69
        return $rdn[0] . '=' . $rdn[1];
70
    }
71
72
    /**
73
     * Recursively implodes an array with optional key inclusion
74
     *
75
     * Example of $include_keys output: key, value, key, value, key, value
76
     *
77
     * @access  public
78
     * @param   array   $array         multi-dimensional array to recursively implode
79
     * @param   string  $glue          value that glues elements together
80
     * @param   bool    $include_keys  include keys before their values
81
     * @param   bool    $trim_all      trim ALL whitespace from string
82
     * @return  string  imploded array
83
     */
84
    public static function recursive_implode(array $array, $glue = ',', $include_keys = false, $trim_all = true)
85
    {
86
        $glued_string = '';
87
88
        // Recursively iterates array and adds key/value to glued string
89
        array_walk_recursive($array, function($value, $key) use ($glue, $include_keys, &$glued_string) {
90
            $include_keys and $glued_string .= $key . $glue;
91
            $glued_string .= $value . $glue;
92
        });
93
94
        // Removes last $glue from string
95
        strlen($glue) > 0 and $glued_string = substr($glued_string, 0, -strlen($glue));
96
97
        // Trim ALL whitespace
98
        $trim_all and $glued_string = preg_replace("/(\s)/ixsm", '', $glued_string);
99
100
        return (string) $glued_string;
101
    }
102
103
}
104