Passed
Pull Request — master (#32)
by Tim
02:52
created

LdapHelpers::asc2hex32()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\ldap\Connector;
6
7
use SimpleSAML\Utils;
8
9
use function asc2hex32;
0 ignored issues
show
introduced by
The function asc2hex32 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
10
use function dec2hex;
0 ignored issues
show
introduced by
The function dec2hex was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
11
use function ord;
12
use function str_pad;
13
use function str_replace;
14
use function strlen;
15
use function substr;
16
17
trait LdapHelpers
18
{
19
    /**
20
     * Escapes the given VALUES according to RFC 2254 so that they can be safely used in LDAP filters.
21
     *
22
     * Any control characters with an ASCII code < 32 as well as the characters with special meaning in
23
     * LDAP filters "*", "(", ")", and "\" (the backslash) are converted into the representation of a
24
     * backslash followed by two hex digits representing the hexadecimal value of the character.
25
     *
26
     * @param string|string[] $values Array of values to escape
27
     * @param bool $singleValue
28
     * @return string|string[] Array $values, but escaped
29
     */
30
    public function escapeFilterValue($values = [], bool $singleValue = true)
31
    {
32
        // Parameter validation
33
        $arrayUtils = new Utils\Arrays();
34
        $values = $arrayUtils->arrayize($values);
35
36
        foreach ($values as $key => $val) {
37
            if ($val === null) {
38
                $val = '\0'; // apply escaped "null" if string is empty
39
            } else {
40
                // Escaping of filter meta characters
41
                $val = str_replace('\\', '\5c', $val);
42
                $val = str_replace('*', '\2a', $val);
43
                $val = str_replace('(', '\28', $val);
44
                $val = str_replace(')', '\29', $val);
45
46
                // ASCII < 32 escaping
47
                $val = $this->asc2hex32($val);
48
            }
49
50
            $values[$key] = $val;
51
        }
52
53
        if ($singleValue) {
54
            return $values[0];
55
        }
56
57
        return $values;
58
    }
59
60
61
    /**
62
     * Converts all ASCII chars < 32 to "\HEX"
63
     *
64
     * @param string $string String to convert
65
     * @return string
66
     */
67
    public function asc2hex32(string $string): string
68
    {
69
        for ($i = 0; $i < strlen($string); $i++) {
70
            $char = substr($string, $i, 1);
71
72
            if (ord($char) < 32) {
73
                $hex = str_pad(dechex(ord($char)), 2, '0', STR_PAD_LEFT);
74
                $string = str_replace($char, '\\' . $hex, $string);
75
            }
76
        }
77
78
        return $string;
79
    }
80
}
81