1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SunnysideUp\ShareThis; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Injector\Injectable; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Dev\Debug; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* https://developers.facebook.com/tools-and-support/ |
11
|
|
|
*/ |
12
|
|
|
class SilverstripeFacebookConnector |
13
|
|
|
{ |
14
|
|
|
use Injectable; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Facebook Connection |
18
|
|
|
*/ |
19
|
|
|
private static $connection = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* settings for connection |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private static $connection_config = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* application ID - get from FB |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private static $app_id = ""; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* application secret - get from FB |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private static $app_secret = ""; |
|
|
|
|
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* debug |
45
|
|
|
* |
46
|
|
|
* @var boolean |
47
|
|
|
*/ |
48
|
|
|
protected static $debug = false; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* keep track of errors |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected static $error = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* set additional connection details - e.g. default_access_token |
58
|
|
|
* |
59
|
|
|
* @param array |
60
|
|
|
*/ |
61
|
|
|
public static function set_connection_config($connectionConfig) |
62
|
|
|
{ |
63
|
|
|
self::$connection_config = $connectionConfig; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* create FB connection... |
68
|
|
|
* @return Facebook\Facebook |
69
|
|
|
*/ |
70
|
|
|
protected static function get_connection() |
71
|
|
|
{ |
72
|
|
|
if (!self::$connection) { |
73
|
|
|
self::$connection_config += [ |
74
|
|
|
'app_id' => Config::inst()->get(SilverstripeFacebookConnector::class, "app_id"), |
75
|
|
|
'app_secret' => Config::inst()->get(SilverstripeFacebookConnector::class, "app_secret"), |
76
|
|
|
'default_graph_version' => 'v2.4', |
77
|
|
|
//'default_access_token' => '{access-token}', // optional |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
self::$connection = new Facebook\Facebook(self::$connection_config); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
return self::$connection; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
* @param string $openGraphCommand |
88
|
|
|
* |
89
|
|
|
* @return FacebookResponse | false |
|
|
|
|
90
|
|
|
*/ |
91
|
|
|
public static function run_command($openGraphCommand = "") |
92
|
|
|
{ |
93
|
|
|
$fb = self::get_connection(); |
94
|
|
|
$accessToken = Config::inst()->get(SilverstripeFacebookConnector::class, "app_id")."|".Config::inst()->get(SilverstripeFacebookConnector::class, "app_secret"); |
95
|
|
|
//$helper = $fb->getPageTabHelper(); |
96
|
|
|
try { |
97
|
|
|
$response = $fb->get($openGraphCommand, $accessToken); |
98
|
|
|
} catch (Facebook\Exceptions\FacebookResponseException $e) { |
|
|
|
|
99
|
|
|
// When Graph returns an error |
100
|
|
|
self::$error[] = 'Graph returned an error: ' . $e->getMessage(); |
101
|
|
|
return false; |
102
|
|
|
} catch (Facebook\Exceptions\FacebookSDKException $e) { |
|
|
|
|
103
|
|
|
// When validation fails or other local issues |
104
|
|
|
self::$error[] = 'Facebook SDK returned an error: ' . $e->getMessage(); |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
if (self::$debug) { |
108
|
|
|
Debug::log(implode(" | ", self::$error)); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
return $response; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return details about logged in person |
|
|
|
|
115
|
|
|
*/ |
116
|
|
|
public static function whoami() |
117
|
|
|
{ |
118
|
|
|
$response = self::run_command("/me"); |
119
|
|
|
if ($response) { |
120
|
|
|
return $response->getGraphUser(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* returns an array of recent posts for a page |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
public static function get_feed($pageID) |
131
|
|
|
{ |
132
|
|
|
$response = self::run_command($pageID . "/posts?fields=message,created_time,id,full_picture,link,from,name,description"); |
133
|
|
|
if ($response) { |
134
|
|
|
$list = $response->getDecodedBody(); |
135
|
|
|
if (isset($list["data"])) { |
136
|
|
|
return $list["data"]; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* returns an array of recent posts for a page |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public static function check_if_posts_exists($UID) |
146
|
|
|
{ |
147
|
|
|
$response = self::run_command('/Post/'.$UID); |
148
|
|
|
print_r($response); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|