-
Notifications
You must be signed in to change notification settings - Fork 19
/
FacebookAPI.php
112 lines (90 loc) · 3.63 KB
/
FacebookAPI.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
<?php
/**
* Wrapper class for common Facebook functions, maintaining some compatibility with the old
* Facebook SDK
*/
namespace IdnoPlugins\Facebook {
use \Facebook;
class FacebookAPI {
public $session = false; /* @var \Facebook\Facebook $session */
function __construct()
{
$fb = new \Facebook\Facebook([
'app_id' => \Idno\Core\Idno::site()->config()->facebook['appId'],
'app_secret' => \Idno\Core\Idno::site()->config()->facebook['secret'],
'default_graph_version' => 'v2.6',
]);
$this->session = $fb;
}
function setAccessToken($token)
{
try {
$session = $this->session;
$session->setDefaultAccessToken($token);
$this->session = $session;
return $session;
} catch (\Exception $e) {
var_export($token);
echo $e->getMessage(); exit;
\Idno\Core\site()->session()->addMessage("Your Facebook session seems to have expired. You need to <a href=\"".\Idno\Core\site()->config()->getDisplayURL()."account/facebook/\">re-authenticate</a>.");
}
return false;
}
/**
* Get the URL to authenticate with Facebook
* @param array $params
* @return string
*/
function getLoginUrl($params = []) {
$redirect_url = \Idno\Core\site()->config()->getDisplayURL() . 'facebook/callback';
$session = $this->session; /* @var \Facebook\Facebook $session */
$helper = $session->getRedirectLoginHelper();
return $helper->getLoginUrl($redirect_url, ['publish_pages', 'public_profile','email','manage_pages','publish_actions']);
}
/**
* Get the Facebook access token on redirect
* @return
*/
function getTokenOnLogin() {
$session = $this->session; /* @var \Facebook\Facebook $session */
$helper = $session->getRedirectLoginHelper();
try {
return $helper->getAccessToken();
} catch (\Exception $e) {
return false;
}
}
/**
* Make an API call
* @param $endpoint
* @param $verb
* @param $params
* @return array|bool
*/
function api($endpoint, $verb = 'GET', $params = null) {
if (empty($this->session)) {
return false;
}
try {
$verb_function = strtolower($verb);
$session = $this->session;
$response = $session->$verb_function($endpoint, $params); /* @var \Facebook\FacebookResponse $response */
try {
if ($items = $response->getGraphNode()) {
$result = array('id' => $items->getField('id'), 'response' => $items);
return $result;
}
} catch (\Exception $e) {
// Ok, lets see if its a graph edge before we ditch
if ($items = $response->getGraphEdge()) {
$result = array('id' => $items->getField('id'), 'response' => $items);
return $result;
}
}
} catch (\Exception $e) {
\Idno\Core\site()->logging()->error($e->getMessage());
return false;
}
}
}
}