ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
MySpace.php
Go to the documentation of this file.
1 <?php
2 
3 require_once(dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/oauth/libextinc/OAuth.php');
4 
12 
16  const STAGE_INIT = 'authmyspace:init';
17 
21  const AUTHID = 'authmyspace:AuthId';
22 
23  private $key;
24  private $secret;
25 
26 
33  public function __construct($info, $config) {
34  assert('is_array($info)');
35  assert('is_array($config)');
36 
37  // Call the parent constructor first, as required by the interface
38  parent::__construct($info, $config);
39 
40  if (!array_key_exists('key', $config))
41  throw new Exception('MySpace authentication source is not properly configured: missing [key]');
42 
43  $this->key = $config['key'];
44 
45  if (!array_key_exists('secret', $config))
46  throw new Exception('MySpace authentication source is not properly configured: missing [secret]');
47 
48  $this->secret = $config['secret'];
49  }
50 
51 
57  public function authenticate(&$state) {
58  assert('is_array($state)');
59 
60  // We are going to need the authId in order to retrieve this authentication source later
61  $state[self::AUTHID] = $this->authId;
62 
63  $consumer = new sspmod_oauth_Consumer($this->key, $this->secret);
64 
65  // Get the request token
66  $requestToken = $consumer->getRequestToken('http://api.myspace.com/request_token');
67  SimpleSAML\Logger::debug("Got a request token from the OAuth service provider [" .
68  $requestToken->key . "] with the secret [" . $requestToken->secret . "]");
69 
70  $state['authmyspace:requestToken'] = $requestToken;
71 
72  $stateID = SimpleSAML_Auth_State::saveState($state, self::STAGE_INIT);
73  SimpleSAML\Logger::debug('authmyspace auth state id = ' . $stateID);
74 
75  // Authorize the request token
76  $consumer->getAuthorizeRequest('http://api.myspace.com/authorize', $requestToken, TRUE, SimpleSAML\Module::getModuleUrl('authmyspace') . '/linkback.php?stateid=' . $stateID);
77 
78  }
79 
80 
81 
82  public function finalStep(&$state) {
83 
84  $requestToken = $state['authmyspace:requestToken'];
85 
86  $consumer = new sspmod_oauth_Consumer($this->key, $this->secret);
87 
88  SimpleSAML\Logger::debug("oauth: Using this request token [" .
89  $requestToken->key . "] with the secret [" . $requestToken->secret . "]");
90 
91  // Replace the request token with an access token
92  $accessToken = $consumer->getAccessToken('http://api.myspace.com/access_token', $requestToken);
93  SimpleSAML\Logger::debug("Got an access token from the OAuth service provider [" .
94  $accessToken->key . "] with the secret [" . $accessToken->secret . "]");
95 
96  // People API - http://developerwiki.myspace.com/index.php?title=People_API
97  $userdata = $consumer->getUserInfo('http://api.myspace.com/1.0/people/@me/@self?fields=@all', $accessToken);
98 
99  $attributes = array();
100 
101  if (is_array($userdata['person'])) {
102  foreach($userdata['person'] AS $key => $value) {
103  if (is_string($value) || is_int($value))
104  $attributes['myspace.' . $key] = array((string)$value);
105 
106  if (is_array($value)) {
107  foreach($value AS $key2 => $value2) {
108  if (is_string($value2) || is_int($value2))
109  $attributes['myspace.' . $key . '.' . $key2] = array((string)$value2);
110  }
111  }
112  }
113 
114  if (array_key_exists('id', $userdata['person']) ) {
115 
116  // person-id in the format of myspace.com.person.1234567890
117  if (preg_match('/(\d+)$/',$userdata['person']['id'],$matches)) {
118  $attributes['myspace_targetedID'] = array('http://myspace.com!' . $matches[1]);
119  $attributes['myspace_uid'] = array($matches[1]);
120  $attributes['myspace_user'] = array($matches[1] . '@myspace.com');
121  }
122  }
123 
124  // profileUrl in the format http://www.myspace.com/username
125  if (array_key_exists('profileUrl', $userdata['person']) ) {
126  if (preg_match('@/([^/]+)$@',$userdata['person']['profileUrl'],$matches)) {
127  $attributes['myspace_username'] = array($matches[1]);
128  $attributes['myspace_user'] = array($matches[1] . '@myspace.com');
129  }
130  }
131  }
132 
133  SimpleSAML\Logger::debug('MySpace Returned Attributes: '. implode(", ",array_keys($attributes)));
134 
135  $state['Attributes'] = $attributes;
136  }
137 }
static debug($string)
Definition: Logger.php:213
$userdata
Definition: demo.php:48
authenticate(&$state)
Log-in using MySpace platform.
Definition: MySpace.php:57
$attributes
const STAGE_INIT
The string used to identify our states.
Definition: MySpace.php:16
$requestToken
Definition: demo.php:33
if(!array_key_exists('stateid', $_REQUEST)) $state
Handle linkback() response from LinkedIn.
Definition: linkback.php:10
Attribute-related utility methods.
const AUTHID
The key of the AuthId field in the state.
Definition: MySpace.php:21
$consumer
Definition: demo.php:30
Create styles array
The data for the language used.
$accessToken
Definition: demo.php:45
$info
Definition: index.php:5
static saveState(&$state, $stage, $rawId=false)
Save the state.
Definition: State.php:194
__construct($info, $config)
Constructor for this authentication source.
Definition: MySpace.php:33