1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
<?php
/* !
* HybridAuth
* http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
* (c) 2009-2012, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
*/
/**
* Yahoo OAuth Class.
*
* @package HybridAuth providers package
* @author Lukasz Koprowski <azram19@gmail.com>
* @author Oleg Kuzava <olegkuzava@gmail.com>
* @version 1.0
* @license BSD License
*/
/**
* Hybrid_Providers_Yahoo - Yahoo provider adapter based on OAuth2 protocol.
*/
class Hybrid_Providers_Yahoo extends Hybrid_Provider_Model_OAuth2 {
/**
* Define Yahoo scopes.
*
* @var array $scope
* If empty will be used YDN App scopes.
* @see https://developer.yahoo.com/oauth2/guide/yahoo_scopes.
*/
public $scope = array();
/**
* {@inheritdoc}
*/
function initialize() {
parent::initialize();
// Provider api end-points.
$this->api->api_base_url = "https://social.yahooapis.com/v1/";
$this->api->authorize_url = "https://api.login.yahoo.com/oauth2/request_auth";
$this->api->token_url = "https://api.login.yahoo.com/oauth2/get_token";
// Set token headers.
$this->setAuthorizationHeaders("basic");
}
/**
* {@inheritdoc}
*/
function loginBegin() {
if (is_array($this->scope)) {
$this->scope = implode(",", $this->scope);
}
parent::loginBegin();
}
/**
* {@inheritdoc}
*/
function getUserProfile() {
$userId = $this->getCurrentUserId();
$response = $this->api->get("user/{$userId}/profile", array(
"format" => "json",
));
if (!isset($response->profile)) {
throw new Exception("User profile request failed! {$this->providerId} returned an invalid response: " . Hybrid_Logger::dumpData($response), 6);
}
$data = $response->profile;
$this->user->profile->identifier = isset($data->guid) ? $data->guid : "";
$this->user->profile->firstName = isset($data->givenName) ? $data->givenName : "";
$this->user->profile->lastName = isset($data->familyName) ? $data->familyName : "";
$this->user->profile->displayName = isset($data->nickname) ? trim($data->nickname) : "";
$this->user->profile->profileURL = isset($data->profileUrl) ? $data->profileUrl : "";
$this->user->profile->gender = isset($data->gender) ? $data->gender : "";
if ($this->user->profile->gender === "F") {
$this->user->profile->gender = "female";
}
elseif ($this->user->profile->gender === "M") {
$this->user->profile->gender = "male";
}
if (isset($data->emails)) {
$email = "";
foreach ($data->emails as $v) {
if (isset($v->primary) && $v->primary) {
$email = isset($v->handle) ? $v->handle : "";
break;
}
}
$this->user->profile->email = $email;
$this->user->profile->emailVerified = $email;
}
$this->user->profile->age = isset($data->displayAge) ? $data->displayAge : "";
$this->user->profile->photoURL = isset($data->image) ? $data->image->imageUrl : "";
$this->user->profile->address = isset($data->location) ? $data->location : "";
$this->user->profile->language = isset($data->lang) ? $data->lang : "";
return $this->user->profile;
}
/**
* {@inheritdoc}
*/
function getUserContacts() {
$userId = $this->getCurrentUserId();
$response = $this->api->get("user/{$userId}/contacts", array(
"format" => "json",
"count" => "max",
));
if ($this->api->http_code != 200) {
throw new Exception("User contacts request failed! {$this->providerId} returned an error: " . $this->errorMessageByStatus());
}
if (!isset($response->contacts) || !isset($response->contacts->contact) || (isset($response->errcode) && $response->errcode != 0)) {
return array();
}
$contacts = array();
foreach ($response->contacts->contact as $item) {
$uc = new Hybrid_User_Contact();
$uc->identifier = isset($item->id) ? $item->id : "";
$uc->email = $this->selectEmail($item->fields);
$uc->displayName = $this->selectName($item->fields);
$uc->photoURL = $this->selectPhoto($item->fields);
$contacts[] = $uc;
}
return $contacts;
}
/**
* Returns current user id.
*
* @return string
* Current user ID.
* @throws Exception
*/
function getCurrentUserId() {
// Set headers to get refresh token.
$this->setAuthorizationHeaders("basic");
// Refresh tokens if needed.
$this->refreshToken();
// Set headers to make api call.
$this->setAuthorizationHeaders("bearer");
$response = $this->api->get("me/guid", array(
"format" => "json",
));
if (!isset($response->guid->value)) {
throw new Exception("User id request failed! {$this->providerId} returned an invalid response: " . Hybrid_Logger::dumpData($response));
}
return $response->guid->value;
}
/**
* Utility function for returning values from XML-like objects.
*
* @param stdClass $vs
* Object.
* @param string $t
* Property name.
* @return mixed
*/
private function select($vs, $t) {
foreach ($vs as $v) {
if ($v->type == $t) {
return $v;
}
}
return null;
}
/**
* Parses user name.
*
* @param stdClass $v
* Object.
* @return string
* User name.
*/
private function selectName($v) {
$s = $this->select($v, "name");
if (!$s) {
$s = $this->select($v, "nickname");
return isset($s->value) ? $s->value : "";
}
return isset($s->value) ? "{$s->value->givenName} {$s->value->familyName}" : "";
}
/**
* Parses photo URL.
*
* @param stdClass $v
* Object.
* @return string
* Photo URL.
*/
private function selectPhoto($v) {
$s = $this->select($v, "image");
return isset($s->value) ? $s->value->imageUrl : "";
}
/**
* Parses email.
*
* @param stdClass $v
* Object
* @return string
* An email address.
*/
private function selectEmail($v) {
$s = $this->select($v, "email");
if (empty($s)) {
$s = $this->select($v, "yahooid");
if (isset($s->value) && strpos($s->value, "@") === FALSE) {
$s->value .= "@yahoo.com";
}
}
return isset($s->value) ? $s->value : "";
}
/**
* Set correct Authorization headers.
*
* @param string $token_type
* Specify token type.
*
* @return void
*/
private function setAuthorizationHeaders($token_type) {
switch ($token_type) {
case "basic":
// The /get_token requires authorization header.
$token = base64_encode("{$this->config["keys"]["id"]}:{$this->config["keys"]["secret"]}");
$this->api->curl_header = array(
"Authorization: Basic {$token}",
"Content-Type: application/x-www-form-urlencoded",
);
break;
case "bearer":
// Yahoo API requires the token to be passed as a Bearer within the authorization header.
$this->api->curl_header = array(
"Authorization: Bearer {$this->api->access_token}",
);
break;
}
}
}
|