1 | <?php |
---|
2 | /*! |
---|
3 | * HybridAuth |
---|
4 | * http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth |
---|
5 | * (c) 2009-2012, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Yahoo OAuth Class |
---|
10 | * |
---|
11 | * @package HybridAuth providers package |
---|
12 | * @author Lukasz Koprowski <azram19@gmail.com> |
---|
13 | * @version 0.2 |
---|
14 | * @license BSD License |
---|
15 | */ |
---|
16 | |
---|
17 | /** |
---|
18 | * Hybrid_Providers_Yahoo - Yahoo provider adapter based on OAuth1 protocol |
---|
19 | */ |
---|
20 | class Hybrid_Providers_Yahoo extends Hybrid_Provider_Model_OAuth1 |
---|
21 | { |
---|
22 | function initialize() |
---|
23 | { |
---|
24 | parent::initialize(); |
---|
25 | |
---|
26 | // Provider api end-points |
---|
27 | $this->api->api_base_url = 'https://social.yahooapis.com/v1/'; |
---|
28 | $this->api->authorize_url = 'https://api.login.yahoo.com/oauth/v2/request_auth'; |
---|
29 | $this->api->request_token_url = 'https://api.login.yahoo.com/oauth/v2/get_request_token'; |
---|
30 | $this->api->access_token_url = 'https://api.login.yahoo.com/oauth/v2/get_token'; |
---|
31 | } |
---|
32 | |
---|
33 | function getUserProfile() |
---|
34 | { |
---|
35 | $userId = $this->getCurrentUserId(); |
---|
36 | |
---|
37 | $parameters = array(); |
---|
38 | $parameters['format'] = 'json'; |
---|
39 | |
---|
40 | $response = $this->api->get( 'user/' . $userId . '/profile', $parameters ); |
---|
41 | |
---|
42 | if ( ! isset( $response->profile ) ){ |
---|
43 | throw new Exception( "User profile request failed! {$this->providerId} returned an invalid response.", 6 ); |
---|
44 | } |
---|
45 | |
---|
46 | $data = $response->profile; |
---|
47 | |
---|
48 | $this->user->profile->identifier = (property_exists($data,'guid'))?$data->guid:""; |
---|
49 | $this->user->profile->firstName = (property_exists($data,'givenName'))?$data->givenName:""; |
---|
50 | $this->user->profile->lastName = (property_exists($data,'familyName'))?$data->familyName:""; |
---|
51 | $this->user->profile->displayName = (property_exists($data,'nickname'))?trim( $data->nickname ):""; |
---|
52 | $this->user->profile->profileURL = (property_exists($data,'profileUrl'))?$data->profileUrl:""; |
---|
53 | $this->user->profile->gender = (property_exists($data,'gender'))?$data->gender:""; |
---|
54 | |
---|
55 | if( $this->user->profile->gender == "F" ){ |
---|
56 | $this->user->profile->gender = "female"; |
---|
57 | } |
---|
58 | |
---|
59 | if( $this->user->profile->gender == "M" ){ |
---|
60 | $this->user->profile->gender = "male"; |
---|
61 | } |
---|
62 | |
---|
63 | if( isset($data->emails) ){ |
---|
64 | $email = ""; |
---|
65 | foreach( $data->emails as $v ){ |
---|
66 | if( isset($v->primary) && $v->primary ) { |
---|
67 | $email = (property_exists($v,'handle'))?$v->handle:""; |
---|
68 | |
---|
69 | break; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | $this->user->profile->email = $email; |
---|
74 | $this->user->profile->emailVerified = $email; |
---|
75 | } |
---|
76 | |
---|
77 | $this->user->profile->age = (property_exists($data,'displayAge'))?$data->displayAge:""; |
---|
78 | $this->user->profile->photoURL = (property_exists($data,'image'))?$data->image->imageUrl:""; |
---|
79 | |
---|
80 | $this->user->profile->address = (property_exists($data,'location'))?$data->location:""; |
---|
81 | $this->user->profile->language = (property_exists($data,'lang'))?$data->lang:""; |
---|
82 | |
---|
83 | return $this->user->profile; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * load the user contacts |
---|
88 | */ |
---|
89 | function getUserContacts() |
---|
90 | { |
---|
91 | $userId = $this->getCurrentUserId(); |
---|
92 | |
---|
93 | $parameters = array(); |
---|
94 | $parameters['format'] = 'json'; |
---|
95 | $parameters['count'] = 'max'; |
---|
96 | |
---|
97 | $response = $this->api->get('user/' . $userId . '/contacts', $parameters); |
---|
98 | |
---|
99 | if ( $this->api->http_code != 200 ) |
---|
100 | { |
---|
101 | throw new Exception( 'User contacts request failed! ' . $this->providerId . ' returned an error: ' . $this->errorMessageByStatus( $this->api->http_code ) ); |
---|
102 | } |
---|
103 | |
---|
104 | if ( !$response->contacts->contact && ( $response->errcode != 0 ) ) |
---|
105 | { |
---|
106 | return array(); |
---|
107 | } |
---|
108 | |
---|
109 | $contacts = array(); |
---|
110 | |
---|
111 | foreach( $response->contacts->contact as $item ) { |
---|
112 | $uc = new Hybrid_User_Contact(); |
---|
113 | |
---|
114 | $uc->identifier = $this->selectGUID( $item ); |
---|
115 | $uc->email = $this->selectEmail( $item->fields ); |
---|
116 | $uc->displayName = $this->selectName( $item->fields ); |
---|
117 | $uc->photoURL = $this->selectPhoto( $item->fields ); |
---|
118 | |
---|
119 | $contacts[] = $uc; |
---|
120 | } |
---|
121 | |
---|
122 | return $contacts; |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * return the user activity stream |
---|
127 | */ |
---|
128 | function getUserActivity( $stream ) |
---|
129 | { |
---|
130 | $userId = $this->getCurrentUserId(); |
---|
131 | |
---|
132 | $parameters = array(); |
---|
133 | $parameters['format'] = 'json'; |
---|
134 | $parameters['count'] = 'max'; |
---|
135 | |
---|
136 | $response = $this->api->get('user/' . $userId . '/updates', $parameters); |
---|
137 | |
---|
138 | if( ! $response->updates || $this->api->http_code != 200 ) |
---|
139 | { |
---|
140 | throw new Exception( 'User activity request failed! ' . $this->providerId . ' returned an error: ' . $this->errorMessageByStatus( $this->api->http_code ) ); |
---|
141 | } |
---|
142 | |
---|
143 | $activities = array(); |
---|
144 | |
---|
145 | foreach( $response->updates as $item ){ |
---|
146 | $ua = new Hybrid_User_Activity(); |
---|
147 | |
---|
148 | $ua->id = (property_exists($item,'collectionID'))?$item->collectionID:""; |
---|
149 | $ua->date = (property_exists($item,'lastUpdated'))?$item->lastUpdated:""; |
---|
150 | $ua->text = (property_exists($item,'loc_longForm'))?$item->loc_longForm:""; |
---|
151 | |
---|
152 | $ua->user->identifier = (property_exists($item,'profile_guid'))?$item->profile_guid:""; |
---|
153 | $ua->user->displayName = (property_exists($item,'profile_nickname'))?$item->profile_nickname:""; |
---|
154 | $ua->user->profileURL = (property_exists($item,'profile_profileUrl'))?$item->profile_profileUrl:""; |
---|
155 | $ua->user->photoURL = (property_exists($item,'profile_displayImage'))?$item->profile_displayImage:""; |
---|
156 | |
---|
157 | $activities[] = $ua; |
---|
158 | } |
---|
159 | |
---|
160 | if( $stream == "me" ){ |
---|
161 | $userId = $this->getCurrentUserId(); |
---|
162 | $my_activities = array(); |
---|
163 | |
---|
164 | foreach( $activities as $a ){ |
---|
165 | if( $a->user->identifier == $userId ){ |
---|
166 | $my_activities[] = $a; |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | return $my_activities; |
---|
171 | } |
---|
172 | |
---|
173 | return $activities; |
---|
174 | } |
---|
175 | |
---|
176 | //-- |
---|
177 | |
---|
178 | function select($vs, $t) |
---|
179 | { |
---|
180 | foreach( $vs as $v ){ |
---|
181 | if( $v->type == $t ) { |
---|
182 | return $v; |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | return NULL; |
---|
187 | } |
---|
188 | |
---|
189 | function selectGUID( $v ) |
---|
190 | { |
---|
191 | return (property_exists($v,'id'))?$v->id:""; |
---|
192 | } |
---|
193 | |
---|
194 | function selectName( $v ) |
---|
195 | { |
---|
196 | $s = $this->select($v, 'name'); |
---|
197 | |
---|
198 | if( ! $s ){ |
---|
199 | $s = $this->select($v, 'nickname'); |
---|
200 | return ($s)?$s->value:""; |
---|
201 | } else { |
---|
202 | return ($s)?$s->value->givenName . " " . $s->value->familyName:""; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | function selectNickame( $v ) |
---|
207 | { |
---|
208 | $s = $this->select($v, 'nickname'); |
---|
209 | return ($s)?$s:""; |
---|
210 | } |
---|
211 | |
---|
212 | function selectPhoto( $v ) |
---|
213 | { |
---|
214 | $s = $this->select($v, 'guid'); |
---|
215 | return ($s)?(property_exists($s,'image')):""; |
---|
216 | } |
---|
217 | |
---|
218 | function selectEmail( $v ) |
---|
219 | { |
---|
220 | $s = $this->select($v, 'email'); |
---|
221 | return ($s)?$s->value:""; |
---|
222 | } |
---|
223 | |
---|
224 | public function getCurrentUserId() |
---|
225 | { |
---|
226 | $parameters = array(); |
---|
227 | $parameters['format'] = 'json'; |
---|
228 | |
---|
229 | $response = $this->api->get( 'me/guid', $parameters ); |
---|
230 | |
---|
231 | if ( ! isset( $response->guid->value ) ){ |
---|
232 | throw new Exception( "User id request failed! {$this->providerId} returned an invalid response." ); |
---|
233 | } |
---|
234 | |
---|
235 | return $response->guid->value; |
---|
236 | } |
---|
237 | } |
---|