-
Notifications
You must be signed in to change notification settings - Fork 5
/
FacebookPlatform.php
456 lines (385 loc) · 11.8 KB
/
FacebookPlatform.php
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
<?php
require_once 'Platform.php';
require_once 'facebook/facebook.php';
class FacebookPlatform extends Platform {
protected $facebook;
protected $user;
protected $config;
public function __construct($config) {
$this->config = $config;
$this->facebook = new Facebook(array(
'appId' => $config['app_id'],
'secret' => $config['app_secret'],
'cookie' => true
));
}
public function loadLibraries() {
?>
<div id="fb-root"></div>
<div id="iframe_container" style="display:none"></div>
<script type="text/javascript">
var origPostTarget;
window.fbAsyncInit = function() {
FB.init({
appId : '<?= $this->config['app_id'] ?>',
session : <?= json_encode($this->facebook->getSession()); ?>,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Canvas.setAutoResize();
FB.Event.subscribe('auth.login', function() {
window.location.reload();
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
function publishStream(title, message, body, actionText, link, images, target) {
link = '<?= $this->config['app_id'] ?>/' + link + "&ref_id=" + <?= $this->user ?>;
var data = {
method: 'stream.publish',
display: 'dialog',
message: message,
attachment: {
name: title,
caption: body,
href: link
},
action_links: [
{ text: actionText, href: link }
],
user_message_prompt: 'Share your thoughts'
};
data.attachment.media = [];
var i = 0;
for (var image in images) {
data.attachment.media[i] = {'type':'image','src':'images/' + images[image],'href':link};
i++;
}
FB.ui(data, function(response) {
});
var attachment = {'media':[], 'name':title, 'caption':body};
}
function showInvitePopup() {
FB.ui({method: 'apprequests',
message: 'You should learn more about this awesome game.',
display: 'iframe',
data: 'trackingdata'});
}
</script>
<?php
}
public function getSessionParams() {
global $_REQUEST;
if (isset($_REQUEST['signed_request']) && $_REQUEST['signed_request'] != "") {
return "signed_request=" . $_REQUEST['signed_request'];
}
return "session=" . urlencode(json_encode($this->facebook->getSession()));
}
public function displayLogin() {
$loginUrl = $this->facebook->getLoginUrl();
echo "<a href='$loginUrl'>";
echo "<img src='http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif'></a>";
}
public function displayLogout() {
$logoutUrl = $this->facebook->getLogoutUrl();
echo "<a href='$logoutUrl'>";
echo "<img src='http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif'></a>";
}
public function getFlashHeight() {
return 660;
}
public function getFlashParams() {
$tpi = $this->getThirdPartyID($this->user);
$params = "&user_id=$this->user";
$params .= "&is_canvas=true";
$params .= "&tpi=$tpi";
return $params;
}
public function displayFlashFile() {
?>
<div id="swfdiv" style='z-index:-1;'>
<EMBED src="/Main.swf" wmode="transparent" FlashVars="<?=$this->getFlashParams()?>" quality=high width=760
height=<?=$this->getFlashHeight()?> TYPE=
"application/x-shockwave-flash"></EMBED>
</div>
<?php
}
public function showPurchaseButton() {
?>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<p> <a onclick="placeOrder(); return false;">Buy Stuff</a></p>
<script>
FB.init({appId: "162536733803732", status: true, cookie: true});
function placeOrder() {
// Assign an internal ID that points to a database record
var order_info = 'abc123';
// calling the API ...
var obj = {
method: 'pay',
order_info: order_info,
purchase_type: 'item'
};
FB.ui(obj, callback);
}
var callback = function(data) {
if (data['order_id']) {
return true;
} else {
//handle errors here
return false;
}
};
function writeback(str) {
document.getElementById('output').innerHTML=str;
}
</script>
<?php
}
public function getRequestListLink() {
return "http://www.facebook.com/reqs.php#confirm_{$this->config['app_id']}" . "_0";
}
public function getAppInfoPage() {
return "http://www.facebook.com/apps/application.php?id={$this->config['app_id']}";
}
public function getAppNewsfeedPage() {
return "http://www.facebook.com/home.php?filter=app_{$this->config['app_id']}";
}
public function getAppForums() {
return "http://www.facebook.com/board.php?uid={$this->config['app_id']}";
}
// User Calls
public function getUser() {
if (!isset($this->user)) {
$this->user = $this->login();
}
return $this->user;
}
public function login() {
$session = $this->facebook->getSession();
if (!$session) {
$url = $this->facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
));
echo "<script type='text/javascript'>top.location.href = '$url';</script>";
exit();
} else {
try {
$this->user = $this->facebook->getUser();
} catch (FacebookApiException $e) {
error_log("Facebook API Error: GetUser - $e");
}
}
return $this->user;
}
public function isLoggedIn() {
return isset($this->user);
}
public function getUserName() {
return $this->getName($this->user);
}
public function displayName($user_id) {
echo"<fb:name uid='<?=$user_id?>' useyou='false' linked='false'></fb:name>";
}
public function getName($user_id) {
try {
$fbuser = $this->facebook->api("/$user_id");
if (isset($fbuser['name'])) {
return $fbuser['name'];
}
} catch (FacebookApiException $e) {
error_log("Facebook API Exception: getName $e");
}
return "Facebook User";
}
public function getUserInfo($user_id, $fields) {
if (is_array($fields))
$fieldString = implode(",", $fields);
else
$fieldString = $fields;
try {
$param = array('method' => 'users.getInfo',
'uids' => $user_id,
'fields' => $fieldString);
$response = $this->facebook->api($param);
} catch (FacebookApiException $e) {
error_log("Facebook API Error: getUserInfo $e");
return false;
}
return $response;
}
public function getFriends() {
try {
return $this->facebook->api("/me/friends");
} catch (FacebookApiException $e) {
error_log("Facebook Api Exception: getFriends $e");
return array();
}
}
public function getFriendsAppUsers() {
try {
$param = array('method' => 'friends.getAppUsers');
return $this->facebook->api($param);
} catch (FacebookApiException $e) {
error_log("Facebook Api Exception: getFriendsAppUsers $e");
return array();
}
}
public function getFriendsNotAppUsers() {
$query = "SELECT uid, has_added_app FROM user WHERE has_added_app!=1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = '" . $this->user . "')";
try {
$param = array('method' => 'fql.query', 'query' => $query, 'callback' => '');
$result = $this->facebook->api($param);
} catch (FacebookApiException $e) {
error_log("Facebook API Error: getFriendsNotAppUsers $e");
return array();
}
$friends = array();
if (!empty($result)) {
foreach ($result as $r) {
$friend = $r['uid'];
if ($friend != null)
$friends[] = $friend;
}
}
return $friends;
}
public function areFriends($user_id, $friend_id) {
try {
$param = array('method' => 'friends.areFriends',
'uids1' => $user_id,
'uids2' => $friend_id,
'fields' => 'are_friends');
$response = $this->facebook->api($param);
} catch (FacebookApiException $e) {
error_log("Facebook API Error: areFriends $e");
return false;
}
if (!isset($response) || !isset($response[0]))
return false;
$isFriend = $response[0]['are_friends'];
return $isFriend;
}
public function isFan() {
$user = $this->user;
$query = "SELECT uid FROM page_fan WHERE uid = '$user' AND page_id = {$this->config['app_id']}";
try {
$param = array('method' => 'fql.query', 'query' => $query, 'callback' => '');
$result = $this->facebook->api($param);
} catch (FacebookApiException $e) {
error_log("Facebook API Error: isFan $e");
return false;
}
return isset($result[0]);
}
public function isBookmarked() {
$user = $this->user;
$query = "SELECT bookmarked FROM permissions WHERE uid = $user";
try {
$param = array('method' => 'fql.query', 'query' => $query, 'callback' => '');
$result = $this->facebook->api($param);
if (!isset($result) || !isset($result[0]))
return false;
$bookmarked = $result[0]['bookmarked'];
} catch (FacebookApiException $e) {
error_log("Facebook API Error: isBookmarked $e");
return false;
}
return $bookmarked;
}
public function isSubscribed() {
$user = $this->user;
$query = "SELECT email FROM permissions WHERE uid='$user'"; //(see above examples)
try {
$param = array('method' => 'fql.query', 'query' => $query, 'callback' => '');
$result = $this->facebook->api($param);
if (!isset($result) || !isset($result[0])) {
return false;
}
return $result[0]['email'];
} catch (FacebookApiException $e) {
error_log("Facebook API Error: isBookmarked $e");
return false;
}
}
public function isStreaming() {
$user = $this->user;
$query = "SELECT publish_stream FROM permissions WHERE uid='$user'"; //(see above examples)
try {
$param = array('method' => 'fql.query', 'query' => $query, 'callback' => '');
$result = $this->facebook->api($param);
if (!isset($result) || !isset($result[0])) {
return false;
}
return $result[0]['publish_stream'];
} catch (FacebookApiException $e) {
error_log("Facebook API Error: isBookmarked $e");
return false;
}
}
public function getFeaturedNews() {
$news = array();
$news[] = array('banner' => '5starreviewnewsbanner.png', 'link' => 'http://www.facebook.com/apps/application.php?id=130193190327885&v=app_6261817190');
$news[] = array('banner' => 'forumsbanner.jpg', 'link' => 'http://www.facebook.com/board.php?uid=130193190327885');
return $news;
}
public function displayProfilePicture($user_id) {
echo "<img src='http://graph.facebook.com/<?=$user_id?>/picture' height='50' width='50'/>";
}
public function getEmail($uid) {
$query = "SELECT uid,email FROM user WHERE uid=$uid"; //(see above examples)
try {
//$results = $this->facebook->api_client->fql_query($query);
$param = array('method' => 'fql.query', 'query' => $query, 'callback' => '');
$results = $this->facebook->api($param);
if (!isset($results) || !isset($results[0])) {
return false;
}
return $results[0];
} catch (FacebookApiException $e) {
error_log("Facebook API Error: getPermissiosn $e");
return false;
}
}
public function getThirdPartyID($uid) {
try {
$result = $this->facebook->api("/$uid/?fields=third_party_id");
if (!isset($result) || !isset($result['third_party_id'])) {
return false;
}
$tpi = $result['third_party_id'];
} catch (FacebookApiException $e) {
error_log("Facebook API Error: third_party_id lookup $e");
return false;
}
return $tpi;
}
public function getUidFromThirdPartyID($tpi) {
try {
$query = "select uid from user where third_party_id='$tpi'";
$param = array('method' => 'fql.query', 'query' => $query, 'callback' => '');
$result = $this->facebook->api($param);
if (!isset($result) || !isset($result[0])) {
return false;
}
$uid = $result[0]['uid'];
} catch (FacebookApiException $e) {
error_log("Facebook API Error: uidTpi lookup $e");
return false;
}
return $uid;
}
public function isFeatureEnabled($feature) {
switch($feature) {
case"stats":
return false;
}
return true;
}
}