Passed
Push — master ( b79026...1b9a7e )
by david
02:00
created

Sdk::gcHmacsha1()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 2
dl 0
loc 23
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Wrapper API Graphcomment v2.0.
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
14
/**
15
 * Class Sdk
16
 * @package Graphcomment
17
 */
18
class Sdk
19
{
20
21
    /**
22
     * @var
23
     */
24
    protected $gcPublic;
25
    protected $gcSecret;
26
    protected $dir = 'https://graphcomment.com/api';
27
28
    /**
29
     * Sdk constructor.
30
     * @param $GC_PUBLIC
31
     * @param $GC_SECRET
32
     */
33
    public function __construct($GC_PUBLIC, $GC_SECRET)
34
    {
35
        $this->setGcPublic($GC_PUBLIC);
36
        $this->setGcSecret($GC_SECRET);
0 ignored issues
show
Bug introduced by
The method setGcSecret() does not exist on Graphcomment\Sdk. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        $this->/** @scrutinizer ignore-call */ 
37
               setGcSecret($GC_SECRET);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
    }
38
39
    /**
40
     * @return mixed
41
     */
42
    public function getGcPublic()
43
    {
44
        return $this->gcPublic;
45
    }
46
47
    /**
48
     * @param mixed $gcPublic
49
     */
50
    public function setGcPublic($gcPublic)
51
    {
52
        $this->gcPublic = $gcPublic;
53
    }
54
55
    /**
56
     * @return mixed
57
     */
58
    public function getGcSecret()
59
    {
60
        return $this->gcSecret;
61
    }
62
63
    /**
64
     * @param mixed $token
65
     */
66
    public function setToken($token)
67
    {
68
        $this->token = $token;
0 ignored issues
show
Bug Best Practice introduced by
The property token does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
69
    }
70
71
    /**
72
     * @return mixed
73
     */
74
    public function getDir()
75
    {
76
        return $this->dir;
77
    }
78
79
    /**
80
     * @param mixed $dir
81
     */
82
    public function setDir($dir)
83
    {
84
        $this->dir = $dir;
85
    }
86
87
    /**
88
     * registerUser() Register a user to Graphcomment.
89
     *
90
     * @param string $username required unique
91
     * @param string $email required unique
92
     * @param string $language (optionnal) default value : en (codes ISO 639-1)
93
     * @param string $picture (full url only example : https://graphcomment.com/image.jpg)
94
     *
95
     * @return object  json response gc_id to store in your database and do_sync which define date of synchronisation
96
     */
97
    public function registerUser($username, $email, $language = "en", $picture = '')
98
    {
99
        $client = new Client();
100
        $data = array(
101
            "username" => $username, // required unique
102
            "email" => $email, // required unique
103
            "language" => $language, //(optionnal) default value : en (codes ISO 639-1)
104
            "picture" => $picture // (optionnal) full url only
105
        );
106
107
        $res = $client->request('POST', $this->getDir() . '/pub/sso/registerUser/pubkey/' . urlencode($this->getGcPublic()) . '/key/' . urlencode($this->generateSsoData($data)), ['http_errors' => false]);
108
109
        if ($res->getStatusCode() == "200") {
110
            return $res->getBody();
111
        } else {
112
            return $res->getBody();
113
        }
114
    }
115
116
    /**
117
     * loginUser() authenticate a user and return a token to login in graphcomment.
118
     *
119
     * @param string $gc_id
120
     *
121
     * @return object  json JWT response
122
     */
123
    public function loginUser($gc_id)
124
    {
125
        $client = new Client();
126
127
        $data = array(
128
            "gc_id" => $gc_id
129
        );
130
131
        $res = $client->request('POST', $this->getDir() . '/pub/sso/loginUser/pubkey/' . urlencode($this->getGcPublic()). '/key/' . urlencode($this->generateSsoData($data)), ['http_errors' => false]);
132
133
        return $res->getBody();
134
    }
135
136
137
    /**
138
     * getUser() return the informations that we have on the user
139
     *
140
     * @param $gc_id
141
     * @return object JSON with do_sync date, if changed, you must synchronise the informations.
142
     */
143
    public function getUser($gc_id)
144
    {
145
        $client = new Client();
146
147
        $data = array(
148
            "gc_id" => $gc_id
149
        );
150
151
        $res = $client->request('GET', $this->getDir() . '/pub/sso/getUser/pubkey/' . urlencode($this->getGcPublic()). '/key/' . urlencode($this->generateSsoData($data)), ['http_errors' => false]);
152
153
        return $res->getBody();
154
    }
155
156
157
    /**
158
     * updateUser() return the informations that we have on the user
159
     *
160
     * @param $gc_id
161
     * @param string $username required unique
162
     * @param string $email required unique
163
     * @param string $language (optionnal) default value : en (codes ISO 639-1)
164
     * @param string $picture (full url only example : https://graphcomment.com/image.jpg)
165
     *
166
     * @return object JSON {
167
     *   gc_id : data.gc_id,
168
     *   do_sync : date of synchronisation
169
     *  res :'updated'
170
     *  } or {
171
     *      gc_id : data.gc_id,
172
     *      res :'nothing updated'
173
     *  }
174
     */
175
    public function updateUser($gc_id, $username, $email, $language, $picture)
176
    {
177
        $client = new Client();
178
179
        $data = array(
180
            "gc_id" => $gc_id,
181
            "username" => $username,
182
            "email" => $email,
183
            "language" => $language,
184
            "picture" => $picture
185
        );
186
187
        $res = $client->request('PUT', $this->getDir() . '/pub/sso/updateUser/pubkey/' . urlencode($this->getGcPublic()). '/key/' . urlencode($this->generateSsoData($data)), ['http_errors' => false]);
188
189
        return $res->getBody();
190
    }
191
192
193
    /**
194
     * deleteUser() delete a user and return ok confirmation.
195
     *
196
     * @param string $gc_id
197
     *
198
     * @return string ok
199
     */
200
    public function deleteUser($gc_id)
201
    {
202
        $client = new Client();
203
204
        $data = array(
205
            "gc_id" => $gc_id
206
        );
207
208
        $res = $client->request('DELETE', $this->getDir() . '/pub/sso/deleteProfileByGcId/pubkey/' . urlencode($this->getGcPublic()). '/key/' . urlencode($this->generateSsoData($data)), ['http_errors' => false]);
209
210
        return $res->getBody();
211
    }
212
213
214
215
    /**
216
     * countComments() return the number thread's comment
217
     *
218
     * @param $url (full url only) required
0 ignored issues
show
Documentation Bug introduced by
The doc comment (full at position 1 could not be parsed: Expected ')' at position 1, but found 'full'.
Loading history...
219
     * @param string $uid (unique id of the thread) optionnal
220
     * @return object json {count: numberOfComments }
221
     */
222
    public function countComments($url, $uid='') {
223
        $client = new Client();
224
225
        $data = array(
226
            "url" => $url,
227
            "uid" => $uid
228
        );
229
230
        $res = $client->request('GET', $this->getDir() . '/pub/sso/numberOfComments/pubkey/' . urlencode($this->getGcPublic()). '/key/' . urlencode($this->generateSsoData($data)), ['http_errors' => false]);
231
232
        return $res->getBody();
233
    }
234
235
    /**
236
     * generateSsoData() generate sso Data
237
     *
238
     * @param $data array
239
     * @return string
240
     */
241
    private function generateSsoData($data) {
242
        $message = base64_encode(json_encode($data));
243
        $timestamp = time();
244
245
        $hexsig = $this->gcHmacsha1($message . ' ' . $timestamp, $this->getGcSecret());
246
247
        return $message . ' ' . $hexsig . ' ' . $timestamp;
248
    }
249
250
    /**
251
     * gcHmacsha1() encode datas
252
     *
253
     * @param $data
254
     * @param $key
255
     * @return string
256
     */
257
    private function gcHmacsha1($data, $key)
258
    {
259
260
        $blocksize = 64;
261
        $hashfunc = 'sha1';
262
263
        if (strlen($key) > $blocksize)
264
            $key = pack('H*', $hashfunc($key));
265
266
        $key = str_pad($key, $blocksize, chr(0x00));
267
        $ipad = str_repeat(chr(0x36), $blocksize);
268
        $opad = str_repeat(chr(0x5c), $blocksize);
269
        $hmac = pack(
270
            'H*', $hashfunc(
271
                ($key ^ $opad) . pack(
272
                    'H*', $hashfunc(
273
                        ($key ^ $ipad) . $data
274
                    )
275
                )
276
            )
277
        );
278
279
        return bin2hex($hmac);
280
    }
281
}