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 | * Hybrid_Providers_LinkedIn provider adapter based on OAuth1 protocol |
---|
10 | * |
---|
11 | * Hybrid_Providers_LinkedIn use linkedinPHP library created by fiftyMission Inc. |
---|
12 | * |
---|
13 | * http://hybridauth.sourceforge.net/userguide/IDProvider_info_LinkedIn.html |
---|
14 | */ |
---|
15 | class Hybrid_Providers_LinkedIn extends Hybrid_Provider_Model |
---|
16 | { |
---|
17 | /** |
---|
18 | * IDp wrappers initializer |
---|
19 | */ |
---|
20 | function initialize() |
---|
21 | { |
---|
22 | if ( ! $this->config["keys"]["key"] || ! $this->config["keys"]["secret"] ){ |
---|
23 | throw new Exception( "Your application key and secret are required in order to connect to {$this->providerId}.", 4 ); |
---|
24 | } |
---|
25 | |
---|
26 | require_once Hybrid_Auth::$config["path_libraries"] . "OAuth/OAuth.php"; |
---|
27 | require_once Hybrid_Auth::$config["path_libraries"] . "LinkedIn/LinkedIn.php"; |
---|
28 | |
---|
29 | $this->api = new LinkedIn( array( 'appKey' => $this->config["keys"]["key"], 'appSecret' => $this->config["keys"]["secret"], 'callbackUrl' => $this->endpoint ) ); |
---|
30 | |
---|
31 | if( $this->token( "access_token_linkedin" ) ){ |
---|
32 | $this->api->setTokenAccess( $this->token( "access_token_linkedin" ) ); |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | /** |
---|
37 | * begin login step |
---|
38 | */ |
---|
39 | function loginBegin() |
---|
40 | { |
---|
41 | // send a request for a LinkedIn access token |
---|
42 | $response = $this->api->retrieveTokenRequest(); |
---|
43 | |
---|
44 | if( isset( $response['success'] ) && $response['success'] === TRUE ){ |
---|
45 | $this->token( "oauth_token", $response['linkedin']['oauth_token'] ); |
---|
46 | $this->token( "oauth_token_secret", $response['linkedin']['oauth_token_secret'] ); |
---|
47 | |
---|
48 | # redirect user to LinkedIn authorisation web page |
---|
49 | Hybrid_Auth::redirect( LINKEDIN::_URL_AUTH . $response['linkedin']['oauth_token'] ); |
---|
50 | } |
---|
51 | else{ |
---|
52 | throw new Exception( "Authentication failed! {$this->providerId} returned an invalid Token.", 5 ); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * finish login step |
---|
58 | */ |
---|
59 | function loginFinish() |
---|
60 | { |
---|
61 | $oauth_token = $_REQUEST['oauth_token']; |
---|
62 | $oauth_verifier = $_REQUEST['oauth_verifier']; |
---|
63 | |
---|
64 | if ( ! $oauth_verifier ){ |
---|
65 | throw new Exception( "Authentication failed! {$this->providerId} returned an invalid Token.", 5 ); |
---|
66 | } |
---|
67 | |
---|
68 | $response = $this->api->retrieveTokenAccess( $oauth_token, $this->token( "oauth_token_secret" ), $oauth_verifier ); |
---|
69 | |
---|
70 | if( isset( $response['success'] ) && $response['success'] === TRUE ){ |
---|
71 | $this->deleteToken( "oauth_token" ); |
---|
72 | $this->deleteToken( "oauth_token_secret" ); |
---|
73 | |
---|
74 | $this->token( "access_token_linkedin", $response['linkedin'] ); |
---|
75 | $this->token( "access_token" , $response['linkedin']['oauth_token'] ); |
---|
76 | $this->token( "access_token_secret" , $response['linkedin']['oauth_token_secret'] ); |
---|
77 | |
---|
78 | // set user as logged in |
---|
79 | $this->setUserConnected(); |
---|
80 | } |
---|
81 | else{ |
---|
82 | throw new Exception( "Authentication failed! {$this->providerId} returned an invalid Token.", 5 ); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * load the user profile from the IDp api client |
---|
88 | */ |
---|
89 | function getUserProfile() |
---|
90 | { |
---|
91 | try{ |
---|
92 | // http://developer.linkedin.com/docs/DOC-1061 |
---|
93 | $response = $this->api->profile('~:(id,first-name,last-name,public-profile-url,picture-url,email-address,date-of-birth,phone-numbers,summary)'); |
---|
94 | } |
---|
95 | catch( LinkedInException $e ){ |
---|
96 | throw new Exception( "User profile request failed! {$this->providerId} returned an error: $e", 6 ); |
---|
97 | } |
---|
98 | |
---|
99 | if( isset( $response['success'] ) && $response['success'] === TRUE ){ |
---|
100 | $data = @ new SimpleXMLElement( $response['linkedin'] ); |
---|
101 | |
---|
102 | if ( ! is_object( $data ) ){ |
---|
103 | throw new Exception( "User profile request failed! {$this->providerId} returned an invalid xml data.", 6 ); |
---|
104 | } |
---|
105 | |
---|
106 | $this->user->profile->identifier = (string) $data->{'id'}; |
---|
107 | $this->user->profile->firstName = (string) $data->{'first-name'}; |
---|
108 | $this->user->profile->lastName = (string) $data->{'last-name'}; |
---|
109 | $this->user->profile->displayName = trim( $this->user->profile->firstName . " " . $this->user->profile->lastName ); |
---|
110 | |
---|
111 | $this->user->profile->email = (string) $data->{'email-address'}; |
---|
112 | $this->user->profile->emailVerified = (string) $data->{'email-address'}; |
---|
113 | |
---|
114 | $this->user->profile->photoURL = (string) $data->{'picture-url'}; |
---|
115 | $this->user->profile->profileURL = (string) $data->{'public-profile-url'}; |
---|
116 | $this->user->profile->description = (string) $data->{'summary'}; |
---|
117 | |
---|
118 | if( $data->{'phone-numbers'} && $data->{'phone-numbers'}->{'phone-number'} ){ |
---|
119 | $this->user->profile->phone = (string) $data->{'phone-numbers'}->{'phone-number'}->{'phone-number'}; |
---|
120 | } |
---|
121 | else{ |
---|
122 | $this->user->profile->phone = null; |
---|
123 | } |
---|
124 | |
---|
125 | if( $data->{'date-of-birth'} ){ |
---|
126 | $this->user->profile->birthDay = (string) $data->{'date-of-birth'}->day; |
---|
127 | $this->user->profile->birthMonth = (string) $data->{'date-of-birth'}->month; |
---|
128 | $this->user->profile->birthYear = (string) $data->{'date-of-birth'}->year; |
---|
129 | } |
---|
130 | |
---|
131 | return $this->user->profile; |
---|
132 | } |
---|
133 | else{ |
---|
134 | throw new Exception( "User profile request failed! {$this->providerId} returned an invalid response.", 6 ); |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * load the user contacts |
---|
140 | */ |
---|
141 | function getUserContacts() |
---|
142 | { |
---|
143 | try{ |
---|
144 | $response = $this->api->profile('~/connections:(id,first-name,last-name,picture-url,public-profile-url,summary)'); |
---|
145 | } |
---|
146 | catch( LinkedInException $e ){ |
---|
147 | throw new Exception( "User contacts request failed! {$this->providerId} returned an error: $e" ); |
---|
148 | } |
---|
149 | |
---|
150 | if( ! $response || ! $response['success'] ){ |
---|
151 | return ARRAY(); |
---|
152 | } |
---|
153 | |
---|
154 | $connections = new SimpleXMLElement( $response['linkedin'] ); |
---|
155 | |
---|
156 | $contacts = ARRAY(); |
---|
157 | |
---|
158 | foreach( $connections->person as $connection ) { |
---|
159 | $uc = new Hybrid_User_Contact(); |
---|
160 | |
---|
161 | $uc->identifier = (string) $connection->id; |
---|
162 | $uc->displayName = (string) $connection->{'last-name'} . " " . $connection->{'first-name'}; |
---|
163 | $uc->profileURL = (string) $connection->{'public-profile-url'}; |
---|
164 | $uc->photoURL = (string) $connection->{'picture-url'}; |
---|
165 | $uc->description = (string) $connection->{'summary'}; |
---|
166 | |
---|
167 | $contacts[] = $uc; |
---|
168 | } |
---|
169 | |
---|
170 | return $contacts; |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * update user status |
---|
175 | */ |
---|
176 | function setUserStatus( $status ) |
---|
177 | { |
---|
178 | $parameters = array(); |
---|
179 | $private = true; // share with your connections only |
---|
180 | |
---|
181 | if( is_array( $status ) ){ |
---|
182 | if( isset( $status[0] ) && ! empty( $status[0] ) ) $parameters["title"] = $status[0]; // post title |
---|
183 | if( isset( $status[1] ) && ! empty( $status[1] ) ) $parameters["comment"] = $status[1]; // post comment |
---|
184 | if( isset( $status[2] ) && ! empty( $status[2] ) ) $parameters["submitted-url"] = $status[2]; // post url |
---|
185 | if( isset( $status[3] ) && ! empty( $status[3] ) ) $parameters["submitted-image-url"] = $status[3]; // post picture url |
---|
186 | if( isset( $status[4] ) && ! empty( $status[4] ) ) $private = $status[4]; // true or false |
---|
187 | } |
---|
188 | else{ |
---|
189 | $parameters["comment"] = $status; |
---|
190 | } |
---|
191 | |
---|
192 | try{ |
---|
193 | $response = $this->api->share( 'new', $parameters, $private ); |
---|
194 | } |
---|
195 | catch( LinkedInException $e ){ |
---|
196 | throw new Exception( "Update user status update failed! {$this->providerId} returned an error: $e" ); |
---|
197 | } |
---|
198 | |
---|
199 | if ( ! $response || ! $response['success'] ) |
---|
200 | { |
---|
201 | throw new Exception( "Update user status update failed! {$this->providerId} returned an error." ); |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * load the user latest activity |
---|
207 | * - timeline : all the stream |
---|
208 | * - me : the user activity only |
---|
209 | */ |
---|
210 | function getUserActivity( $stream ) |
---|
211 | { |
---|
212 | try{ |
---|
213 | if( $stream == "me" ){ |
---|
214 | $response = $this->api->updates( '?type=SHAR&scope=self&count=25' ); |
---|
215 | } |
---|
216 | else{ |
---|
217 | $response = $this->api->updates( '?type=SHAR&count=25' ); |
---|
218 | } |
---|
219 | } |
---|
220 | catch( LinkedInException $e ){ |
---|
221 | throw new Exception( "User activity stream request failed! {$this->providerId} returned an error: $e" ); |
---|
222 | } |
---|
223 | |
---|
224 | if( ! $response || ! $response['success'] ){ |
---|
225 | return ARRAY(); |
---|
226 | } |
---|
227 | |
---|
228 | $updates = new SimpleXMLElement( $response['linkedin'] ); |
---|
229 | |
---|
230 | $activities = ARRAY(); |
---|
231 | |
---|
232 | foreach( $updates->update as $update ) { |
---|
233 | $person = $update->{'update-content'}->person; |
---|
234 | $share = $update->{'update-content'}->person->{'current-share'}; |
---|
235 | |
---|
236 | $ua = new Hybrid_User_Activity(); |
---|
237 | |
---|
238 | $ua->id = (string) $update->id; |
---|
239 | $ua->date = (string) $update->timestamp; |
---|
240 | $ua->text = (string) $share->{'comment'}; |
---|
241 | |
---|
242 | $ua->user->identifier = (string) $person->id; |
---|
243 | $ua->user->displayName = (string) $person->{'first-name'} . ' ' . $person->{'last-name'}; |
---|
244 | $ua->user->profileURL = (string) $person->{'site-standard-profile-request'}->url; |
---|
245 | $ua->user->photoURL = NULL; |
---|
246 | |
---|
247 | $activities[] = $ua; |
---|
248 | } |
---|
249 | |
---|
250 | return $activities; |
---|
251 | } |
---|
252 | } |
---|