Blame view

common/components/nodge/eauth/src/services/VKontakteOAuth2Service.php 3.45 KB
14a09168   Alex Savenko   init commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  <?php

  /**

   * VKontakteOAuth2Service class file.

   *

   * Register application: http://vk.com/editapp?act=create&site=1

   *

   * @author Maxim Zemskov <nodge@yandex.ru>

   * @link http://github.com/Nodge/yii2-eauth/

   * @license http://www.opensource.org/licenses/bsd-license.php

   */

  

  namespace common\components\nodge\eauth\src\services;

  

  use nodge\eauth\oauth2\Service;

  use OAuth\OAuth2\Service\ServiceInterface;

  

  /**

   * VKontakte provider class.

   *

   * @package application.extensions.eauth.services

   */

  class VKontakteOAuth2Service extends Service

  {

  

  	const SCOPE_FRIENDS = 'friends';

  

  	protected $name = 'vkontakte';

  	protected $title = 'VK.com';

  	protected $type = 'OAuth2';

  	protected $jsArguments = ['popup' => ['width' => 585, 'height' => 350]];

  

  	protected $scopes = [self::SCOPE_FRIENDS];

  	protected $providerOptions = [

  		'authorize' => 'http://api.vk.com/oauth/authorize',

  		'access_token' => 'https://api.vk.com/oauth/access_token',

  	];

  	protected $baseApiUrl = 'https://api.vk.com/method/';

  

  	protected function fetchAttributes()

  	{

  		$tokenData = $this->getAccessTokenData();

  		$info = $this->makeSignedRequest('users.get.json', [

  			'query' => [

  				'uids' => $tokenData['params']['user_id'],

  				'fields' => '', // uid, first_name and last_name is always available

  				'fields' => 'nickname, sex, bdate, city, country, timezone, photo, photo_medium, photo_big, photo_rec',

  			],

  		]);

  

  		$info = $info['response'][0];

  

  		$this->attributes['id'] = $info['uid'];

  		$this->attributes['name'] = $info['first_name'] . ' ' . $info['last_name'];

  		$this->attributes['url'] = 'http://vk.com/id' . $info['uid'];

  

  		if (!empty($info['nickname']))

  			$this->attributes['username'] = $info['nickname'];

  		else

  			$this->attributes['username'] = 'id'.$info['uid'];

  

  		$this->attributes['gender'] = $info['sex'] == 1 ? 'F' : 'M';

  

  		$this->attributes['city'] = $info['city'];

  		$this->attributes['country'] = $info['country'];

  

  		$this->attributes['timezone'] = timezone_name_from_abbr('', $info['timezone']*3600, date('I'));;

  

  		$this->attributes['photo'] = $info['photo'];

  		$this->attributes['photo_medium'] = $info['photo_medium'];

  		$this->attributes['photo_big'] = $info['photo_big'];

  		$this->attributes['photo_rec'] = $info['photo_rec'];

  

  		return true;

  	}

  

  	/**

  	 * Returns the error array.

  	 *

  	 * @param array $response

  	 * @return array the error array with 2 keys: code and message. Should be null if no errors.

  	 */

  	protected function fetchResponseError($response)

  	{

  		if (isset($response['error'])) {

  			return [

  				'code' => is_string($response['error']) ? 0 : $response['error']['error_code'],

  //				'message' => is_string($response['error']) ? $response['error'] : $response['error']['error_msg'],

  //				'message' => is_string($response['error']) ? $response['error'] : $response['error']['error_msg'],

  			];

  		} else {

  			return null;

  		}

  	}

  

  	/**

  	 * @param array $data

  	 * @return string|null

  	 */

  	public function getAccessTokenResponseError($data)

  	{

  		if (!isset($data['error'])) {

  			return null;

  		}

  		$error = $data['error'];

  		if (isset($data['error_description'])) {

  			$error .= ': ' . $data['error_description'];

  		}

  		return $error;

  	}

  

  	/**

  	 * Returns a class constant from ServiceInterface defining the authorization method used for the API.

  	 *

  	 * @return int

  	 */

  	public function getAuthorizationMethod()

  	{

  		return ServiceInterface::AUTHORIZATION_METHOD_QUERY_STRING;

  	}

  

  }