1 | <?php |
||
2 | /** |
||
3 | * Wrapper API Graphcomment v2.3.2 |
||
4 | * @author ddtraceweb <[email protected]> |
||
5 | * @copyright 2018 Graphcomment |
||
6 | * Date: 12/11/2018 |
||
7 | * Time: 18:37 |
||
8 | */ |
||
9 | |||
10 | namespace Graphcomment; |
||
11 | |||
12 | use GuzzleHttp\Client; |
||
13 | use GuzzleHttp\Exception\GuzzleException; |
||
14 | |||
15 | /** |
||
16 | * Class Sdk |
||
17 | * @package Graphcomment |
||
18 | */ |
||
19 | class Sdk |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * @var |
||
24 | */ |
||
25 | protected $gcPublic; |
||
26 | protected $gcSecret; |
||
27 | protected $dir = 'https://graphcomment.com/api'; |
||
28 | |||
29 | /** |
||
30 | * Sdk constructor. |
||
31 | * @param $GC_PUBLIC |
||
32 | * @param $GC_SECRET |
||
33 | */ |
||
34 | public function __construct($GC_PUBLIC, $GC_SECRET) |
||
35 | { |
||
36 | $this->setGcPublic($GC_PUBLIC); |
||
37 | $this->setGcSecret($GC_SECRET); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @return mixed |
||
42 | */ |
||
43 | public function getGcPublic() |
||
44 | { |
||
45 | return $this->gcPublic; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param mixed $gcPublic |
||
50 | */ |
||
51 | public function setGcPublic($gcPublic) |
||
52 | { |
||
53 | $this->gcPublic = $gcPublic; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @return mixed |
||
58 | */ |
||
59 | public function getGcSecret() |
||
60 | { |
||
61 | return $this->gcSecret; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param mixed $gcSecret |
||
66 | */ |
||
67 | public function setGcSecret($gcSecret) |
||
68 | { |
||
69 | $this->gcSecret = $gcSecret; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return mixed |
||
74 | */ |
||
75 | public function getDir() |
||
76 | { |
||
77 | return $this->dir; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param mixed $dir |
||
82 | */ |
||
83 | public function setDir($dir) |
||
84 | { |
||
85 | $this->dir = $dir; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * registerUser() Register a user to Graphcomment. |
||
90 | * |
||
91 | * @param string $username required unique |
||
92 | * @param string $email required unique |
||
93 | * @param string $language (optionnal) default value : en (codes ISO 639-1) |
||
94 | * @param string $picture (full url only example : https://graphcomment.com/image.jpg) |
||
95 | * |
||
96 | * @return object json response gc_id to store in your database and do_sync which define date of synchronisation |
||
97 | * @throws GuzzleException |
||
98 | */ |
||
99 | public function registerUser($username, $email, $language = "en", $picture = '') |
||
100 | { |
||
101 | $client = new Client(); |
||
102 | $data = array( |
||
103 | "username" => $username, // required unique |
||
104 | "email" => $email, // required unique |
||
105 | "language" => $language, //(optionnal) default value : en (codes ISO 639-1) |
||
106 | "picture" => $picture // (optionnal) full url only |
||
107 | ); |
||
108 | |||
109 | $res = $client->request('POST', $this->getDir() . '/pub/sso/registerUser/pubkey/' . $this->getGcPublic() . '?key=' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]); |
||
110 | |||
111 | if ($res->getStatusCode() == "200") { |
||
112 | return $res->getBody(); |
||
113 | } else { |
||
114 | return $res->getBody(); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * loginUser() authenticate a user and return a token to login in graphcomment. |
||
120 | * |
||
121 | * @param string $gc_id |
||
122 | * |
||
123 | * @return object json JWT response |
||
124 | * @throws GuzzleException |
||
125 | */ |
||
126 | public function loginUser($gc_id) |
||
127 | { |
||
128 | $client = new Client(); |
||
129 | |||
130 | $data = array( |
||
131 | "gc_id" => $gc_id |
||
132 | ); |
||
133 | |||
134 | $res = $client->request('POST', $this->getDir() . '/pub/sso/loginUser/pubkey/' . $this->getGcPublic(). '?key=' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]); |
||
135 | |||
136 | return $res->getBody(); |
||
137 | } |
||
138 | |||
139 | |||
140 | /** |
||
141 | * getUser() return the informations that we have on the user |
||
142 | * |
||
143 | * @param $gc_id |
||
144 | * @return object JSON with do_sync date, if changed, you must synchronise the informations. |
||
145 | * @throws GuzzleException |
||
146 | */ |
||
147 | public function getUser($gc_id) |
||
148 | { |
||
149 | $client = new Client(); |
||
150 | |||
151 | $data = array( |
||
152 | "gc_id" => $gc_id |
||
153 | ); |
||
154 | |||
155 | $res = $client->request('GET', $this->getDir() . '/pub/sso/getUser/pubkey/' . $this->getGcPublic(). '?key=' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]); |
||
156 | |||
157 | return $res->getBody(); |
||
158 | } |
||
159 | |||
160 | |||
161 | /** |
||
162 | * updateUser() return the informations that we have on the user |
||
163 | * |
||
164 | * @param $gc_id |
||
165 | * @param string $username required unique |
||
166 | * @param string $email required unique |
||
167 | * @param string $language (optionnal) default value : en (codes ISO 639-1) |
||
168 | * @param string $picture (full url only example : https://graphcomment.com/image.jpg) |
||
169 | * |
||
170 | * @return object JSON { |
||
171 | * gc_id : data.gc_id, |
||
172 | * do_sync : date of synchronisation |
||
173 | * res :'updated' |
||
174 | * } or { |
||
175 | * gc_id : data.gc_id, |
||
176 | * res :'nothing updated' |
||
177 | * } |
||
178 | * @throws GuzzleException |
||
179 | */ |
||
180 | public function updateUser($gc_id, $username, $email, $language, $picture) |
||
181 | { |
||
182 | $client = new Client(); |
||
183 | |||
184 | $data = array( |
||
185 | "gc_id" => $gc_id, |
||
186 | "username" => $username, |
||
187 | "email" => $email, |
||
188 | "language" => $language, |
||
189 | "picture" => $picture |
||
190 | ); |
||
191 | |||
192 | $res = $client->request('PUT', $this->getDir() . '/pub/sso/updateUser/pubkey/' . $this->getGcPublic(). '?key=' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]); |
||
193 | |||
194 | return $res->getBody(); |
||
195 | } |
||
196 | |||
197 | |||
198 | /** |
||
199 | * deleteUser() delete a user and return ok confirmation. |
||
200 | * |
||
201 | * @param string $gc_id |
||
202 | * |
||
203 | * @return string ok |
||
204 | * @throws GuzzleException |
||
205 | */ |
||
206 | public function deleteUser($gc_id) |
||
207 | { |
||
208 | $client = new Client(); |
||
209 | |||
210 | $data = array( |
||
211 | "gc_id" => $gc_id); |
||
212 | |||
213 | $res = $client->request('DELETE', $this->getDir() . '/pub/sso/deleteProfileByGcId/pubkey/' . $this->getGcPublic(). '?key=' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]); |
||
214 | |||
215 | return $res->getBody(); |
||
216 | } |
||
217 | |||
218 | |||
219 | /** |
||
220 | * countComments() return the number thread's comment |
||
221 | * |
||
222 | * @param $url (full url only) required |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
223 | * @param string $uid (unique id of the thread) optionnal |
||
224 | * @return object json {count: numberOfComments } |
||
225 | * @throws GuzzleException |
||
226 | */ |
||
227 | public function countComments($url, $uid='') { |
||
228 | $client = new Client(); |
||
229 | |||
230 | $data = array( |
||
231 | "url" => $url, |
||
232 | "uid" => $uid |
||
233 | ); |
||
234 | |||
235 | $res = $client->request('GET', $this->getDir() . '/pub/sso/numberOfComments/pubkey/' . $this->getGcPublic(). '?key=' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]); |
||
236 | |||
237 | return $res->getBody(); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * getThreadJsonLdFormat() return the number thread's comment |
||
242 | * |
||
243 | * @param $url (full url only) required |
||
244 | * @param string $uid (unique id of the thread) optionnal |
||
245 | * @param string $pageId (page id of the thread) optionnal for graphdebate |
||
246 | * @return object json-ld |
||
247 | * @throws GuzzleException |
||
248 | */ |
||
249 | public function getThreadJsonLdFormat($url, $uid='', $pageId='') { |
||
250 | $client = new Client(); |
||
251 | |||
252 | $data = array( |
||
253 | "url" => $url, |
||
254 | "uid" => $uid, |
||
255 | "page_id" => $pageId |
||
256 | ); |
||
257 | |||
258 | $res = $client->request('GET', $this->getDir() . '/pub/sso/thread-jsonld/pubkey/' . $this->getGcPublic(). '?key=' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]); |
||
259 | |||
260 | return $res->getBody(); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * exportComments() return the comments to import in your system, group by 20 comments |
||
265 | * |
||
266 | * @return array of object json [{commentObject}] |
||
267 | * @throws GuzzleException |
||
268 | */ |
||
269 | public function exportComments() { |
||
270 | $client = new Client(); |
||
271 | |||
272 | $res = $client->request('GET', $this->getDir() . '/pub/export-comments/pubkey/' . $this->getGcPublic(). '?key=' . $this->generateSsoData('key'), ['http_errors' => false, 'timeout' => 5]); |
||
273 | |||
274 | return $res->getBody(); |
||
0 ignored issues
–
show
|
|||
275 | } |
||
276 | |||
277 | /** |
||
278 | * exportConfirmComments() send id |
||
279 | * |
||
280 | * @param array $commentIds |
||
281 | * |
||
282 | * @return array of object json [{commentObject}] |
||
283 | * @throws GuzzleException |
||
284 | */ |
||
285 | public function exportConfirmComments(Array $commentIds) { |
||
286 | $client = new Client(); |
||
287 | |||
288 | $data = $commentIds; |
||
289 | |||
290 | $res = $client->request('POST', $this->getDir() . '/pub/export-comments/pubkey/' . $this->getGcPublic(). '?key=' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]); |
||
291 | |||
292 | return $res->getBody(); |
||
0 ignored issues
–
show
|
|||
293 | } |
||
294 | |||
295 | /** |
||
296 | * generateSsoData() generate sso Data |
||
297 | * |
||
298 | * @param $data array|string |
||
299 | * @return string |
||
300 | */ |
||
301 | private function generateSsoData($data) { |
||
302 | $message = base64_encode(json_encode($data)); |
||
303 | $timestamp = time(); |
||
304 | |||
305 | $hexsig = $this->gcHmacsha1($message . ' ' . $timestamp, $this->getGcSecret()); |
||
306 | |||
307 | return $message . ' ' . $hexsig . ' ' . $timestamp; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * gcHmacsha1() encode datas |
||
312 | * |
||
313 | * @param $data |
||
314 | * @param $key |
||
315 | * @return string |
||
316 | */ |
||
317 | private function gcHmacsha1($data, $key) |
||
318 | { |
||
319 | |||
320 | $blocksize = 64; |
||
321 | $hashfunc = 'sha1'; |
||
322 | |||
323 | if (strlen($key) > $blocksize) |
||
324 | $key = pack('H*', $hashfunc($key)); |
||
325 | |||
326 | $key = str_pad($key, $blocksize, chr(0x00)); |
||
327 | $ipad = str_repeat(chr(0x36), $blocksize); |
||
328 | $opad = str_repeat(chr(0x5c), $blocksize); |
||
329 | $hmac = pack( |
||
330 | 'H*', $hashfunc( |
||
331 | ($key ^ $opad) . pack( |
||
332 | 'H*', $hashfunc( |
||
333 | ($key ^ $ipad) . $data |
||
334 | ) |
||
335 | ) |
||
336 | ) |
||
337 | ); |
||
338 | |||
339 | return bin2hex($hmac); |
||
340 | } |
||
341 | } |