| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Firesphere\YubiAuth\Helpers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @description Helper Class to convert different keyboard layouts to Qwerty before authenticating | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | class QwertyConvertor | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |      * @var string Dvorak layout | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     protected static $dvorak = "[]',.pyfgcrl/=aoeuidhtns-;qjkxbmwvz{}\"<>PYFGCRL?+AOEUIDHTNS_:QJKXBMWVZ"; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * @var string Qwerty layout | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     protected static $qwerty = "-=qwertyuiop[]asdfghjkl;'zxcvbnm,./_+QWERTYUIOP{}ASDFGHJKL:\"ZXCVBNM<>?"; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * This might be tricky to detect, as the initial position of C seems to be the same | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @var string Azerty layout conversion | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     protected static $azerty = ")-azertyuiop^\$qsdfghjklmùwxcvbn,;:=°_AZERTYUIOP¨*QSDFGHJKLM%WXCVBN?./+"; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * Detect different keyboard layouts and return the converted string. | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * A Yubi-string alsways starts with `cccccc`. If it's different, we have a different layout. | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * Dvorak is easy to detect. Azerty on the other hand, might be tricky. | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * Other conversion additions welcome. | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @param string $yubiString | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public static function convertString($yubiString) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $yubiString = strtolower($yubiString); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         /* The string is Dvorak, convert it to QWERTY */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if (strpos($yubiString, 'jjjjjj') === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             return self::convertToQwerty($yubiString, static::$dvorak); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         /* Azerty has some odd characters which we can use to detect it */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             strpos($yubiString, '°') !== false || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             strpos($yubiString, '¨') !== false || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             strpos($yubiString, '$') !== false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             return self::convertToQwerty($yubiString, static::$azerty); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |         return $yubiString; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @param string $originalString | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * @param string $from Origin we have to convert from | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public static function convertToQwerty($originalString, $from) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $originalArray = str_split($originalString); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $qwerty = str_split(self::$qwerty); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $from = str_split($from); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $return = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         foreach ($originalArray as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $return .= $qwerty[array_search($item, $from, true)]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |         return $return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |  |