Issues (70)

src/Model/LDAPUtil.php (1 issue)

1
<?php
2
3
namespace SilverStripe\LDAP\Model;
4
5
/**
6
 * Class LDAPUtil
7
 *
8
 * Provides some commonly used functions for LDAP and SAML.
9
 */
10
class LDAPUtil
11
{
12
    /**
13
     * Checks if the string is a valid guid in the format of A98C5A1E-A742-4808-96FA-6F409E799937
14
     *
15
     * @param  string $guid
16
     * @return bool
17
     */
18
    public static function validGuid($guid)
19
    {
20
        if (preg_match('/^[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}?$/', $guid)) {
21
            return true;
22
        }
23
        return false;
24
    }
25
26
    /**
27
     * @param  string $object_guid
28
     * @return string
29
     */
30
    public static function bin_to_str_guid($object_guid)
31
    {
32
        $hex_guid = bin2hex($object_guid);
33
        $hex_guid_to_guid_str = '';
34
        for ($k = 1; $k <= 4; ++$k) {
35
            $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2);
36
        }
37
        $hex_guid_to_guid_str .= '-';
38
        for ($k = 1; $k <= 2; ++$k) {
39
            $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2);
40
        }
41
        $hex_guid_to_guid_str .= '-';
42
        for ($k = 1; $k <= 2; ++$k) {
43
            $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2);
44
        }
45
        $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4);
46
        $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20);
47
48
        return strtoupper($hex_guid_to_guid_str);
49
    }
50
51
    /**
52
     * @param  string $str_guid
53
     * @param  bool   $escape
54
     * @return string
55
     */
56
    public static function str_to_hex_guid($str_guid, $escape = false)
57
    {
58
        $str_guid = str_replace('-', '', $str_guid);
59
60
        $octet_str = substr($str_guid, 6, 2);
61
        $octet_str .= substr($str_guid, 4, 2);
62
        $octet_str .= substr($str_guid, 2, 2);
63
        $octet_str .= substr($str_guid, 0, 2);
64
        $octet_str .= substr($str_guid, 10, 2);
65
        $octet_str .= substr($str_guid, 8, 2);
66
        $octet_str .= substr($str_guid, 14, 2);
67
        $octet_str .= substr($str_guid, 12, 2);
68
        $octet_str .= substr($str_guid, 16, strlen($str_guid));
69
70
        if ($escape) {
71
            $escaped = '\\';
72
            for ($i = 0; $i < strlen($octet_str); $i += 2) {
73
                $escaped .= substr($octet_str, $i, 2);
74
                if ($i != strlen($octet_str) - 2) {
75
                    $escaped .= '\\';
76
                }
77
            }
78
            return $escaped;
79
        }
80
81
        return $octet_str;
82
    }
83
84
    /**
85
     * @param  string $binsid
86
     * @return string
87
     */
88
    public static function bin_to_str_sid($binsid)
89
    {
90
        $hex_sid = bin2hex($binsid);
91
        $rev = hexdec(substr($hex_sid, 0, 2));
92
        $subcount = hexdec(substr($hex_sid, 2, 2));
93
        $auth = hexdec(substr($hex_sid, 4, 12));
94
        $result = "$rev-$auth";
95
96
        for ($x = 0; $x < $subcount; $x++) {
97
            $subauth[$x] = hexdec(self::little_endian(substr($hex_sid, 16 + ($x * 8), 8)));
98
            $result .= '-' . $subauth[$x];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $subauth does not seem to be defined for all execution paths leading up to this point.
Loading history...
99
        }
100
101
        // Cheat by tacking on the S-
102
        return 'S-' . $result;
103
    }
104
105
    /**
106
     * Converts a little-endian hex-number to one, that 'hexdec' can convert
107
     * @param  string $hex
108
     * @return string
109
     */
110
    public static function little_endian($hex)
111
    {
112
        $result = '';
113
        for ($x = strlen($hex) - 2; $x >= 0; $x = $x - 2) {
114
            $result .= substr($hex, $x, 2);
115
        }
116
        return $result;
117
    }
118
}
119