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