Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
FacebookOAuthService.php
1 <?php
2 /**
3  * FacebookOAuthService class file.
4  *
5  * Register application: https://developers.facebook.com/apps/
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  * Facebook provider class.
16  * @package application.extensions.eauth.services
17  */
19 
20  protected $name = 'facebook';
21  protected $title = 'Facebook';
22  protected $type = 'OAuth';
23  protected $jsArguments = array('popup' => array('width' => 585, 'height' => 290));
24 
25  protected $client_id = '';
26  protected $client_secret = '';
27  protected $scope = '';
28  protected $providerOptions = array(
29  'authorize' => 'https://www.facebook.com/dialog/oauth',
30  'access_token' => 'https://graph.facebook.com/oauth/access_token',
31  );
32 
33  protected function fetchAttributes() {
34  $info = (object) $this->makeSignedRequest('https://graph.facebook.com/me');
35 
36  $this->attributes['id'] = $info->id;
37  $this->attributes['name'] = $info->name;
38  $this->attributes['url'] = $info->link;
39  }
40 
41  protected function getCodeUrl($redirect_uri) {
42  if (strpos($redirect_uri, '?') !== false) {
43  $url = explode('?', $redirect_uri);
44  $url[1] = preg_replace('#[/]#', '%2F', $url[1]);
45  $redirect_uri = implode('?', $url);
46  }
47 
48  $this->setState('redirect_uri', $redirect_uri);
49 
50  $url = parent::getCodeUrl($redirect_uri);
51  if (isset($_GET['js']))
52  $url .= '&display=popup';
53 
54  return $url;
55  }
56 
57  protected function getTokenUrl($code) {
58  return parent::getTokenUrl($code).'&redirect_uri='.urlencode($this->getState('redirect_uri'));
59  }
60 
61  protected function getAccessToken($code) {
62  $response = $this->makeRequest($this->getTokenUrl($code), array(), false);
63  parse_str($response, $result);
64  return $result;
65  }
66 
67  /**
68  * Save access token to the session.
69  * @param array $token access token array.
70  */
71  protected function saveAccessToken($token) {
72  $this->setState('auth_token', $token['access_token']);
73  $this->setState('expires', isset($token['expires']) ? time() + (int)$token['expires'] - 60 : 0);
74  $this->access_token = $token['access_token'];
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->message,
87  );
88  }
89  else
90  return null;
91  }
92 }