LDAPUtil::validGuid()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Class LDAPUtil
4
 *
5
 * Provides some commonly used functions for LDAP and SAML.
6
 */
7
class LDAPUtil
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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) {
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...
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) {
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...
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) {
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...
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)));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$subauth was never initialized. Although not strictly required by PHP, it is generally a good practice to add $subauth = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
82
            $result .= "-" . $subauth[$x];
0 ignored issues
show
Bug introduced by
The variable $subauth does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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