Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
VKontakteOAuthService.php
1 <?php
2 /**
3  * VKontakteOAuthService class file.
4  *
5  * Register application: http://vkontakte.ru/editapp?act=create&site=1
6  *
7  * @author Maxim Zemskov <nodge@yandex.ru>
8  * @link http://code.google.com/p/yii-eauth/
9  * @license http://www.opensource.org/licenses/bsd-license.php
10  */
11 
12 require_once dirname(dirname(__FILE__)) . '/EOAuth2Service.php';
13 
14 /**
15  * VKontakte provider class.
16  * @package application.extensions.eauth.services
17  */
19 
20  protected $name = 'vkontakte';
21  protected $title = 'VK.com';
22  protected $type = 'OAuth';
23  protected $jsArguments = array('popup' => array('width' => 585, 'height' => 350));
24 
25  protected $client_id = '';
26  protected $client_secret = '';
27  protected $scope = '';
28  protected $providerOptions = array(
29  'authorize' => 'http://api.vkontakte.ru/oauth/authorize',
30  'access_token' => 'https://api.vkontakte.ru/oauth/access_token',
31  );
32 
33  protected $uid = null;
34 
35  protected function fetchAttributes() {
36  $info = (array)$this->makeSignedRequest('https://api.vkontakte.ru/method/getProfiles', array(
37  'query' => array(
38  'uids' => $this->uid,
39  'fields' => '', // uid, first_name and last_name is always available
40  //'fields' => 'nickname, sex, bdate, city, country, timezone, photo, photo_medium, photo_big, photo_rec',
41  ),
42  ));
43 
44  $info = $info['response'][0];
45 
46  $this->attributes['id'] = $info->uid;
47  $this->attributes['name'] = $info->first_name.' '.$info->last_name;
48  $this->attributes['url'] = 'http://vkontakte.ru/id'.$info->uid;
49 
50  /*if (!empty($info->nickname))
51  $this->attributes['username'] = $info->nickname;
52  else
53  $this->attributes['username'] = 'id'.$info->uid;
54 
55  $this->attributes['gender'] = $info->sex == 1 ? 'F' : 'M';
56 
57  $this->attributes['city'] = $info->city;
58  $this->attributes['country'] = $info->country;
59 
60  $this->attributes['timezone'] = timezone_name_from_abbr('', $info->timezone*3600, date('I'));;
61 
62  $this->attributes['photo'] = $info->photo;
63  $this->attributes['photo_medium'] = $info->photo_medium;
64  $this->attributes['photo_big'] = $info->photo_big;
65  $this->attributes['photo_rec'] = $info->photo_rec;*/
66  }
67 
68  /**
69  * Returns the url to request to get OAuth2 code.
70  * @param string $redirect_uri url to redirect after user confirmation.
71  * @return string url to request.
72  */
73  protected function getCodeUrl($redirect_uri) {
74  $url = parent::getCodeUrl($redirect_uri);
75  if (isset($_GET['js']))
76  $url .= '&display=popup';
77  return $url;
78  }
79 
80  /**
81  * Save access token to the session.
82  * @param stdClass $token access token object.
83  */
84  protected function saveAccessToken($token) {
85  $this->setState('auth_token', $token->access_token);
86  $this->setState('uid', $token->user_id);
87  $this->setState('expires', time() + $token->expires_in - 60);
88  $this->uid = $token->user_id;
89  $this->access_token = $token->access_token;
90  }
91 
92  /**
93  * Restore access token from the session.
94  * @return boolean whether the access token was successfuly restored.
95  */
96  protected function restoreAccessToken() {
97  if ($this->hasState('uid') && parent::restoreAccessToken()) {
98  $this->uid = $this->getState('uid');
99  return true;
100  }
101  else {
102  $this->uid = null;
103  return false;
104  }
105  }
106 
107  /**
108  * Returns the error info from json.
109  * @param stdClass $json the json response.
110  * @return array the error array with 2 keys: code and message. Should be null if no errors.
111  */
112  protected function fetchJsonError($json) {
113  if (isset($json->error)) {
114  return array(
115  'code' => $json->error->error_code,
116  'message' => $json->error->error_msg,
117  );
118  }
119  else
120  return null;
121  }
122 }