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_Facebook provider adapter based on OAuth2 protocol |
---|
10 | * |
---|
11 | * Hybrid_Providers_Facebook use the Facebook PHP SDK created by Facebook |
---|
12 | * |
---|
13 | * http://hybridauth.sourceforge.net/userguide/IDProvider_info_Facebook.html |
---|
14 | */ |
---|
15 | class Hybrid_Providers_Facebook extends Hybrid_Provider_Model |
---|
16 | { |
---|
17 | // default permissions, and alot of them. You can change them from the configuration by setting the scope to what you want/need |
---|
18 | public $scope = "email, user_about_me, user_birthday, user_hometown, user_website, read_stream, offline_access, publish_stream, read_friendlists"; |
---|
19 | |
---|
20 | /** |
---|
21 | * IDp wrappers initializer |
---|
22 | */ |
---|
23 | function initialize() |
---|
24 | { |
---|
25 | if ( ! $this->config["keys"]["id"] || ! $this->config["keys"]["secret"] ){ |
---|
26 | throw new Exception( "Your application id and secret are required in order to connect to {$this->providerId}.", 4 ); |
---|
27 | } |
---|
28 | |
---|
29 | if ( ! class_exists('FacebookApiException', false) ) { |
---|
30 | require_once Hybrid_Auth::$config["path_libraries"] . "Facebook/base_facebook.php"; |
---|
31 | require_once Hybrid_Auth::$config["path_libraries"] . "Facebook/facebook.php"; |
---|
32 | } |
---|
33 | |
---|
34 | if ( isset ( Hybrid_Auth::$config["proxy"] ) ) { |
---|
35 | BaseFacebook::$CURL_OPTS[CURLOPT_PROXY] = Hybrid_Auth::$config["proxy"]; |
---|
36 | } |
---|
37 | |
---|
38 | $this->api = new Facebook( ARRAY( 'appId' => $this->config["keys"]["id"], 'secret' => $this->config["keys"]["secret"] ) ); |
---|
39 | |
---|
40 | if ( $this->token("access_token") ) { |
---|
41 | $this->api->setAccessToken( $this->token("access_token") ); |
---|
42 | $this->api->setExtendedAccessToken(); |
---|
43 | $access_token = $this->api->getAccessToken(); |
---|
44 | |
---|
45 | if( $access_token ){ |
---|
46 | $this->token("access_token", $access_token ); |
---|
47 | $this->api->setAccessToken( $access_token ); |
---|
48 | } |
---|
49 | |
---|
50 | $this->api->setAccessToken( $this->token("access_token") ); |
---|
51 | } |
---|
52 | |
---|
53 | $this->api->getUser(); |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * begin login step |
---|
58 | * |
---|
59 | * simply call Facebook::require_login(). |
---|
60 | */ |
---|
61 | function loginBegin() |
---|
62 | { |
---|
63 | $parameters = array("scope" => $this->scope, "redirect_uri" => $this->endpoint, "display" => "page"); |
---|
64 | $optionals = array("scope", "redirect_uri", "display"); |
---|
65 | |
---|
66 | foreach ($optionals as $parameter){ |
---|
67 | if( isset( $this->config[$parameter] ) && ! empty( $this->config[$parameter] ) ){ |
---|
68 | $parameters[$parameter] = $this->config[$parameter]; |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | // get the login url |
---|
73 | $url = $this->api->getLoginUrl( $parameters ); |
---|
74 | |
---|
75 | // redirect to facebook |
---|
76 | Hybrid_Auth::redirect( $url ); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * finish login step |
---|
81 | */ |
---|
82 | function loginFinish() |
---|
83 | { |
---|
84 | // in case we get error_reason=user_denied&error=access_denied |
---|
85 | if ( isset( $_REQUEST['error'] ) && $_REQUEST['error'] == "access_denied" ){ |
---|
86 | throw new Exception( "Authentication failed! The user denied your request.", 5 ); |
---|
87 | } |
---|
88 | |
---|
89 | // try to get the UID of the connected user from fb, should be > 0 |
---|
90 | if ( ! $this->api->getUser() ){ |
---|
91 | throw new Exception( "Authentication failed! {$this->providerId} returned an invalid user id.", 5 ); |
---|
92 | } |
---|
93 | |
---|
94 | // set user as logged in |
---|
95 | $this->setUserConnected(); |
---|
96 | |
---|
97 | // store facebook access token |
---|
98 | $this->token( "access_token", $this->api->getAccessToken() ); |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * logout |
---|
103 | */ |
---|
104 | function logout() |
---|
105 | { |
---|
106 | $this->api->destroySession(); |
---|
107 | |
---|
108 | parent::logout(); |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * load the user profile from the IDp api client |
---|
113 | */ |
---|
114 | function getUserProfile() |
---|
115 | { |
---|
116 | // request user profile from fb api |
---|
117 | try{ |
---|
118 | $data = $this->api->api('/me'); |
---|
119 | } |
---|
120 | catch( FacebookApiException $e ){ |
---|
121 | throw new Exception( "User profile request failed! {$this->providerId} returned an error: $e", 6 ); |
---|
122 | } |
---|
123 | |
---|
124 | // if the provider identifier is not recived, we assume the auth has failed |
---|
125 | if ( ! isset( $data["id"] ) ){ |
---|
126 | throw new Exception( "User profile request failed! {$this->providerId} api returned an invalid response.", 6 ); |
---|
127 | } |
---|
128 | |
---|
129 | # store the user profile. |
---|
130 | $this->user->profile->identifier = (array_key_exists('id',$data))?$data['id']:""; |
---|
131 | $this->user->profile->displayName = (array_key_exists('name',$data))?$data['name']:""; |
---|
132 | $this->user->profile->firstName = (array_key_exists('first_name',$data))?$data['first_name']:""; |
---|
133 | $this->user->profile->lastName = (array_key_exists('last_name',$data))?$data['last_name']:""; |
---|
134 | $this->user->profile->photoURL = "https://graph.facebook.com/" . $this->user->profile->identifier . "/picture?width=150&height=150"; |
---|
135 | $this->user->profile->profileURL = (array_key_exists('link',$data))?$data['link']:""; |
---|
136 | $this->user->profile->webSiteURL = (array_key_exists('website',$data))?$data['website']:""; |
---|
137 | $this->user->profile->gender = (array_key_exists('gender',$data))?$data['gender']:""; |
---|
138 | $this->user->profile->description = (array_key_exists('bio',$data))?$data['bio']:""; |
---|
139 | $this->user->profile->email = (array_key_exists('email',$data))?$data['email']:""; |
---|
140 | $this->user->profile->emailVerified = (array_key_exists('email',$data))?$data['email']:""; |
---|
141 | $this->user->profile->region = (array_key_exists("hometown",$data)&&array_key_exists("name",$data['hometown']))?$data['hometown']["name"]:""; |
---|
142 | |
---|
143 | if( array_key_exists('birthday',$data) ) { |
---|
144 | list($birthday_month, $birthday_day, $birthday_year) = explode( "/", $data['birthday'] ); |
---|
145 | |
---|
146 | $this->user->profile->birthDay = (int) $birthday_day; |
---|
147 | $this->user->profile->birthMonth = (int) $birthday_month; |
---|
148 | $this->user->profile->birthYear = (int) $birthday_year; |
---|
149 | } |
---|
150 | |
---|
151 | return $this->user->profile; |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * load the user contacts |
---|
156 | */ |
---|
157 | function getUserContacts() |
---|
158 | { |
---|
159 | try{ |
---|
160 | $response = $this->api->api('/me/friends'); |
---|
161 | } |
---|
162 | catch( FacebookApiException $e ){ |
---|
163 | throw new Exception( "User contacts request failed! {$this->providerId} returned an error: $e" ); |
---|
164 | } |
---|
165 | |
---|
166 | if( ! $response || ! count( $response["data"] ) ){ |
---|
167 | return ARRAY(); |
---|
168 | } |
---|
169 | |
---|
170 | $contacts = ARRAY(); |
---|
171 | |
---|
172 | foreach( $response["data"] as $item ){ |
---|
173 | $uc = new Hybrid_User_Contact(); |
---|
174 | |
---|
175 | $uc->identifier = (array_key_exists("id",$item))?$item["id"]:""; |
---|
176 | $uc->displayName = (array_key_exists("name",$item))?$item["name"]:""; |
---|
177 | $uc->profileURL = "https://www.facebook.com/profile.php?id=" . $uc->identifier; |
---|
178 | $uc->photoURL = "https://graph.facebook.com/" . $uc->identifier . "/picture?width=150&height=150"; |
---|
179 | |
---|
180 | $contacts[] = $uc; |
---|
181 | } |
---|
182 | |
---|
183 | return $contacts; |
---|
184 | } |
---|
185 | |
---|
186 | /** |
---|
187 | * update user status |
---|
188 | */ |
---|
189 | function setUserStatus( $status ) |
---|
190 | { |
---|
191 | $parameters = array(); |
---|
192 | |
---|
193 | if( is_array( $status ) ){ |
---|
194 | $parameters = $status; |
---|
195 | } |
---|
196 | else{ |
---|
197 | $parameters["message"] = $status; |
---|
198 | } |
---|
199 | |
---|
200 | try{ |
---|
201 | $response = $this->api->api( "/me/feed", "post", $parameters ); |
---|
202 | } |
---|
203 | catch( FacebookApiException $e ){ |
---|
204 | throw new Exception( "Update user status failed! {$this->providerId} returned an error: $e" ); |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * load the user latest activity |
---|
210 | * - timeline : all the stream |
---|
211 | * - me : the user activity only |
---|
212 | */ |
---|
213 | function getUserActivity( $stream ) |
---|
214 | { |
---|
215 | try{ |
---|
216 | if( $stream == "me" ){ |
---|
217 | $response = $this->api->api( '/me/feed' ); |
---|
218 | } |
---|
219 | else{ |
---|
220 | $response = $this->api->api('/me/home'); |
---|
221 | } |
---|
222 | } |
---|
223 | catch( FacebookApiException $e ){ |
---|
224 | throw new Exception( "User activity stream request failed! {$this->providerId} returned an error: $e" ); |
---|
225 | } |
---|
226 | |
---|
227 | if( ! $response || ! count( $response['data'] ) ){ |
---|
228 | return ARRAY(); |
---|
229 | } |
---|
230 | |
---|
231 | $activities = ARRAY(); |
---|
232 | |
---|
233 | foreach( $response['data'] as $item ){ |
---|
234 | if( $stream == "me" && $item["from"]["id"] != $this->api->getUser() ){ |
---|
235 | continue; |
---|
236 | } |
---|
237 | |
---|
238 | $ua = new Hybrid_User_Activity(); |
---|
239 | |
---|
240 | $ua->id = (array_key_exists("id",$item))?$item["id"]:""; |
---|
241 | $ua->date = (array_key_exists("created_time",$item))?strtotime($item["created_time"]):""; |
---|
242 | |
---|
243 | if( $item["type"] == "video" ){ |
---|
244 | $ua->text = (array_key_exists("link",$item))?$item["link"]:""; |
---|
245 | } |
---|
246 | |
---|
247 | if( $item["type"] == "link" ){ |
---|
248 | $ua->text = (array_key_exists("link",$item))?$item["link"]:""; |
---|
249 | } |
---|
250 | |
---|
251 | if( empty( $ua->text ) && isset( $item["story"] ) ){ |
---|
252 | $ua->text = (array_key_exists("link",$item))?$item["link"]:""; |
---|
253 | } |
---|
254 | |
---|
255 | if( empty( $ua->text ) && isset( $item["message"] ) ){ |
---|
256 | $ua->text = (array_key_exists("message",$item))?$item["message"]:""; |
---|
257 | } |
---|
258 | |
---|
259 | if( ! empty( $ua->text ) ){ |
---|
260 | $ua->user->identifier = (array_key_exists("id",$item["from"]))?$item["from"]["id"]:""; |
---|
261 | $ua->user->displayName = (array_key_exists("name",$item["from"]))?$item["from"]["name"]:""; |
---|
262 | $ua->user->profileURL = "https://www.facebook.com/profile.php?id=" . $ua->user->identifier; |
---|
263 | $ua->user->photoURL = "https://graph.facebook.com/" . $ua->user->identifier . "/picture?type=square"; |
---|
264 | |
---|
265 | $activities[] = $ua; |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | return $activities; |
---|
270 | } |
---|
271 | } |
---|