1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class LDAPUtil |
4
|
|
|
* |
5
|
|
|
* Provides some commonly used functions for LDAP and SAML. |
6
|
|
|
*/ |
7
|
|
|
class LDAPUtil |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Checks if the string is a valid guid in the format of A98C5A1E-A742-4808-96FA-6F409E799937 |
11
|
|
|
* |
12
|
|
|
* @param $guid |
13
|
|
|
* @return bool |
14
|
|
|
*/ |
15
|
|
|
public static function validGuid($guid) |
16
|
|
|
{ |
17
|
|
|
if (preg_match('/^[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}?$/', $guid)) { |
18
|
|
|
return true; |
19
|
|
|
} |
20
|
|
|
return false; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public static function bin_to_str_guid($object_guid) |
24
|
|
|
{ |
25
|
|
|
$hex_guid = bin2hex($object_guid); |
26
|
|
|
$hex_guid_to_guid_str = ''; |
27
|
|
View Code Duplication |
for ($k = 1; $k <= 4; ++$k) { |
|
|
|
|
28
|
|
|
$hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2); |
29
|
|
|
} |
30
|
|
|
$hex_guid_to_guid_str .= '-'; |
31
|
|
View Code Duplication |
for ($k = 1; $k <= 2; ++$k) { |
|
|
|
|
32
|
|
|
$hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2); |
33
|
|
|
} |
34
|
|
|
$hex_guid_to_guid_str .= '-'; |
35
|
|
View Code Duplication |
for ($k = 1; $k <= 2; ++$k) { |
|
|
|
|
36
|
|
|
$hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2); |
37
|
|
|
} |
38
|
|
|
$hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4); |
39
|
|
|
$hex_guid_to_guid_str .= '-' . substr($hex_guid, 20); |
40
|
|
|
|
41
|
|
|
return strtoupper($hex_guid_to_guid_str); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function str_to_hex_guid($str_guid, $escape = false) |
45
|
|
|
{ |
46
|
|
|
$str_guid = str_replace('-', '', $str_guid); |
47
|
|
|
|
48
|
|
|
$octet_str = substr($str_guid, 6, 2); |
49
|
|
|
$octet_str .= substr($str_guid, 4, 2); |
50
|
|
|
$octet_str .= substr($str_guid, 2, 2); |
51
|
|
|
$octet_str .= substr($str_guid, 0, 2); |
52
|
|
|
$octet_str .= substr($str_guid, 10, 2); |
53
|
|
|
$octet_str .= substr($str_guid, 8, 2); |
54
|
|
|
$octet_str .= substr($str_guid, 14, 2); |
55
|
|
|
$octet_str .= substr($str_guid, 12, 2); |
56
|
|
|
$octet_str .= substr($str_guid, 16, strlen($str_guid)); |
57
|
|
|
|
58
|
|
|
if ($escape) { |
59
|
|
|
$escaped = '\\'; |
60
|
|
|
for ($i = 0; $i < strlen($octet_str); $i+=2) { |
61
|
|
|
$escaped .= substr($octet_str, $i, 2); |
62
|
|
|
if ($i != strlen($octet_str) - 2) { |
63
|
|
|
$escaped .= '\\'; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
return $escaped; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $octet_str; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function bin_to_str_sid($binsid) |
73
|
|
|
{ |
74
|
|
|
$hex_sid = bin2hex($binsid); |
75
|
|
|
$rev = hexdec(substr($hex_sid, 0, 2)); |
76
|
|
|
$subcount = hexdec(substr($hex_sid, 2, 2)); |
77
|
|
|
$auth = hexdec(substr($hex_sid, 4, 12)); |
78
|
|
|
$result = "$rev-$auth"; |
79
|
|
|
|
80
|
|
|
for ($x=0;$x < $subcount; $x++) { |
81
|
|
|
$subauth[$x] = hexdec(self::little_endian(substr($hex_sid, 16 + ($x * 8), 8))); |
|
|
|
|
82
|
|
|
$result .= "-" . $subauth[$x]; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Cheat by tacking on the S- |
86
|
|
|
return 'S-' . $result; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Converts a little-endian hex-number to one, that 'hexdec' can convert |
90
|
|
|
public static function little_endian($hex) |
91
|
|
|
{ |
92
|
|
|
$result = ''; |
93
|
|
|
for ($x = strlen($hex) - 2; $x >= 0; $x = $x - 2) { |
94
|
|
|
$result .= substr($hex, $x, 2); |
95
|
|
|
} |
96
|
|
|
return $result; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.