Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
OdnoklassnikiOAuthService.php
1 <?php
2 /**
3  * OdnoklassnikiOAuthService class file.
4  *
5  * Register application: http://www.odnoklassniki.ru/dk?st.cmd=appsInfoMyDevList&st._aid=Apps_Info_MyDev
6  * Note: Enabling this service a little more difficult because of the authorization policy of the service.
7  *
8  * @author Sergey Vardanyan <rakot.ss@gmail.com>
9  * @license http://www.opensource.org/licenses/bsd-license.php
10  */
11 
12 require_once dirname(dirname(__FILE__)) . '/EOAuth2Service.php';
13 
14 /**
15  * Odnoklassniki.Ru provider class.
16  * @package application.extensions.eauth.services
17  */
19 
20  protected $name = 'odnoklassniki';
21  protected $title = 'Odnoklassniki';
22  protected $type = 'OAuth';
23  protected $jsArguments = array('popup' => array('width' => 680, 'height' => 500));
24 
25  protected $client_id = '';
26  protected $client_secret = '';
27  protected $client_public = '';
28  protected $scope = '';
29  protected $providerOptions = array(
30  'authorize' => 'http://www.odnoklassniki.ru/oauth/authorize',
31  'access_token' => 'http://api.odnoklassniki.ru/oauth/token.do',
32  );
33 
34  protected function fetchAttributes() {
35  $sig = strtolower(md5('application_key='.$this->client_public.'client_id='.$this->client_id.'format=JSONmethod=users.getCurrentUser'.md5($this->access_token.$this->client_secret)));
36 
37  $info = $this->makeRequest('http://api.odnoklassniki.ru/fb.do', array(
38  'query' => array(
39  'method' => 'users.getCurrentUser',
40  'sig' => $sig,
41  'format' => 'JSON',
42  'application_key' => $this->client_public,
43  'client_id' => $this->client_id,
44  'access_token' => $this->access_token,
45  ),
46  ));
47 
48  $this->attributes['id'] = $info->uid;
49  $this->attributes['name'] = $info->first_name.' '.$info->last_name;
50  }
51 
52  protected function getTokenUrl($code) {
53  return $this->providerOptions['access_token'];
54  }
55 
56  protected function getAccessToken($code) {
57  $params = array(
58  'client_id' => $this->client_id,
59  'client_secret' => $this->client_secret,
60  'grant_type' => 'authorization_code',
61  'code' => $code,
62  'redirect_uri' => $this->getState('redirect_uri'),
63  );
64  $url = $this->getTokenUrl($code).'?client_id='.$this->client_id.'&client_secret='.$this->client_secret.'&redirect_uri='.urlencode($this->getState('redirect_uri')).'&code='.$code.'&grant_type=authorization_code';
65  $result = $this->makeRequest($url, array('data' => $params));
66  return $result->access_token;
67  }
68 
69  protected function getCodeUrl($redirect_uri) {
70  $this->setState('redirect_uri', $redirect_uri);
71  $url = parent::getCodeUrl($redirect_uri);
72  if (isset($_GET['js']))
73  $url .= '&display=popup';
74  return $url;
75  }
76 
77  /**
78  * Returns the error info from json.
79  * @param stdClass $json the json response.
80  * @return array the error array with 2 keys: code and message. Should be null if no errors.
81  */
82  protected function fetchJsonError($json) {
83  if (isset($json->error)) {
84  return array(
85  'code' => $json->error_code,
86  'message' => $json->error_description,
87  );
88  }
89  else
90  return null;
91  }
92 }