ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Facebook.php
Go to the documentation of this file.
1 <?php
2 
10 
11 
15  const STAGE_INIT = 'facebook:init';
16 
17 
21  const AUTHID = 'facebook:AuthId';
22 
23 
27  private $api_key;
28 
29 
33  private $secret;
34 
35 
39  private $req_perms;
40 
41 
54  private $user_fields;
55 
56 
63  public function __construct($info, $config) {
64  assert('is_array($info)');
65  assert('is_array($config)');
66 
67  // Call the parent constructor first, as required by the interface
68  parent::__construct($info, $config);
69 
70  $cfgParse = SimpleSAML_Configuration::loadFromArray($config, 'authsources[' . var_export($this->authId, TRUE) . ']');
71 
72  $this->api_key = $cfgParse->getString('api_key');
73  $this->secret = $cfgParse->getString('secret');
74  $this->req_perms = $cfgParse->getString('req_perms', NULL);
75  $this->user_fields = $cfgParse->getString('user_fields', NULL);
76  }
77 
78 
84  public function authenticate(&$state) {
85  assert('is_array($state)');
86 
87  // We are going to need the authId in order to retrieve this authentication source later
88  $state[self::AUTHID] = $this->authId;
89  SimpleSAML_Auth_State::saveState($state, self::STAGE_INIT);
90 
91  $facebook = new sspmod_authfacebook_Facebook(array('appId' => $this->api_key, 'secret' => $this->secret), $state);
92  $facebook->destroySession();
93 
94  $linkback = SimpleSAML\Module::getModuleURL('authfacebook/linkback.php');
95  $url = $facebook->getLoginUrl(array('redirect_uri' => $linkback, 'scope' => $this->req_perms));
96  SimpleSAML_Auth_State::saveState($state, self::STAGE_INIT);
97 
99  }
100 
101 
102  public function finalStep(&$state) {
103  assert('is_array($state)');
104 
105  $facebook = new sspmod_authfacebook_Facebook(array('appId' => $this->api_key, 'secret' => $this->secret), $state);
106  $uid = $facebook->getUser();
107 
108  if (isset($uid) && $uid) {
109  try {
110  $info = $facebook->api("/" . $uid . ($this->user_fields ? "?fields=" . $this->user_fields : ""));
111  } catch (FacebookApiException $e) {
112  throw new SimpleSAML_Error_AuthSource($this->authId, 'Error getting user profile.', $e);
113  }
114  }
115 
116  if (!isset($info)) {
117  throw new SimpleSAML_Error_AuthSource($this->authId, 'Error getting user profile.');
118  }
119 
120  $attributes = array();
121  foreach($info AS $key => $value) {
122  if (is_string($value) && !empty($value)) {
123  $attributes['facebook.' . $key] = array((string)$value);
124  }
125  }
126 
127  if (array_key_exists('third_party_id', $info)) {
128  $attributes['facebook_user'] = array($info['third_party_id'] . '@facebook.com');
129  } else {
130  $attributes['facebook_user'] = array($uid . '@facebook.com');
131  }
132 
133  $attributes['facebook_targetedID'] = array('http://facebook.com!' . $uid);
134  $attributes['facebook_cn'] = array($info['name']);
135 
136  SimpleSAML\Logger::debug('Facebook Returned Attributes: '. implode(", ", array_keys($attributes)));
137 
138  $state['Attributes'] = $attributes;
139 
140  $facebook->destroySession();
141  }
142 
143 }
$req_perms
Which additional data permissions to request from user.
Definition: Facebook.php:39
const AUTHID
The key of the AuthId field in the state.
Definition: Facebook.php:21
__construct($info, $config)
Constructor for this authentication source.
Definition: Facebook.php:63
Extends the BaseFacebook class with the intent of using PHP sessions to store user ids and access tok...
Definition: Facebook.php:9
static debug($string)
Definition: Logger.php:213
$attributes
$user_fields
A comma-separated list of user profile fields to request.
Definition: Facebook.php:54
static redirectTrustedURL($url, $parameters=array())
This function redirects to the specified URL without performing any security checks.
Definition: HTTP.php:962
authenticate(&$state)
Log-in using Facebook platform.
Definition: Facebook.php:84
static getModuleURL($resource, array $parameters=array())
Get absolute URL to a specified module resource.
Definition: Module.php:303
const STAGE_INIT
The string used to identify our states.
Definition: Facebook.php:15
if(!array_key_exists('stateid', $_REQUEST)) $state
Handle linkback() response from LinkedIn.
Definition: linkback.php:10
$api_key
Facebook App ID or API Key.
Definition: Facebook.php:27
Create styles array
The data for the language used.
$url
$info
Definition: index.php:5
$key
Definition: croninfo.php:18
static loadFromArray($config, $location='[ARRAY]', $instance=null)
Loads a configuration from the given array.
static saveState(&$state, $stage, $rawId=false)
Save the state.
Definition: State.php:194
Copyright 2011 Facebook, Inc.