| 1 |  |  | <?php
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Mezon\SocialNetwork\Auth;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Mezon\SocialNetwork\BaseAuth;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Class Vkontakte
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package BaseAuth
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @subpackage Vkontakte
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author Dodonov A.A.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @version v.1.0 (2019/08/17)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @copyright Copyright (c) 2019, aeon.org
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Class provides integration with VK.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @author Dodonov A.A.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class Vkontakte extends BaseAuth
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * Method returns URL wich generates tokens.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @return string URL
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |      */
 | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     public function getOauthUri(): string
 | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         return 'https://oauth.vk.com/authorize?v=5.0&';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * Method return URL wich provides user's info.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param string $token
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      *            - Token;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @return string URL
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function getUserInfoUri(string $token = ''): string
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         return 'https://api.vk.com/method/users.get?v=5.21&';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * Method returns.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return string URL
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function getTokenUri(): string
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         return 'https://oauth.vk.com/access_token?v=5.0&';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * Method returns a list of desired fields.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return string Comma separated of the desired fields.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function getDesiredFields(): string
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return 'uid,first_name,last_name,email,photo_100';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * Method dispatches user info.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param array $userInfo
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      *            - User info got from social network.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @return array Dispatched user info. Must be as array with keys id, first_name, last_name, email, picture.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function dispatchUserInfo(array $userInfo): array
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $response = $userInfo['response'][0];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $response['email'] = $response['email'] ?? '';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return [
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             'id' => $response['id'],
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'first_name' => $response['first_name'],
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'last_name' => $response['last_name'],
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'picture' => $response['photo_100'],
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             'email' => $response['email']
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         ];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     }
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  | }
 | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  |