Passed
Push — master ( 3c190c...2857c2 )
by Robbie
03:26
created

LDAPUtil   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 8.41 %

Importance

Changes 0
Metric Value
dl 9
loc 107
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A little_endian() 0 7 2
A bin_to_str_guid() 9 19 4
A bin_to_str_sid() 0 15 2
B str_to_hex_guid() 0 26 4
A validGuid() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
        for ($k = 1; $k <= 4; ++$k) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
            $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2);
36
        }
37
        $hex_guid_to_guid_str .= '-';
38 View Code Duplication
        for ($k = 1; $k <= 2; ++$k) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
            $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2);
40
        }
41
        $hex_guid_to_guid_str .= '-';
42 View Code Duplication
        for ($k = 1; $k <= 2; ++$k) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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