-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApi.php
427 lines (377 loc) · 10.4 KB
/
Api.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
<?php
namespace ForkCms\Api;
/**
* @author Tijs Verkoyen <[email protected]>
* @version 1.0.1
* @copyright Copyright (c) 2008, Tijs Verkoyen. All rights reserved.
* @license BSD License
*/
class Api
{
// current version
const VERSION = '1.0.1';
/**
* The API key to use for authentication
*
* @var string
*/
private $apiKey;
/**
* The e-mail address to use for authentication
*
* @var string
*/
private $email;
/**
* The timeout
*
* @var int
*/
private $timeOut = 10;
/**
* The user agent
*
* @var string
*/
private $userAgent;
/**
* The url to communicate on
*
* @var string
*/
private $url;
/**
* Default constructor
*/
public function __construct($url, $email = null, $apiKey = null)
{
$this->setUrl($url);
if ($email !== null) {
$this->setEmail($email);
}
if ($apiKey !== null) {
$this->setApiKey($apiKey);
}
}
/**
* Make the call
*
* @param string $method The method to call.
* @param array $parameters The parameters to pass.
* @param string $httpMethod The HTTP method to use.
* @param bool $authenticate Should we use authentication?
* @return mixed
*/
public function doCall(
$method,
array $parameters = null,
$httpMethod = 'GET',
$authenticate = true
) {
// build the url
$url = $this->getUrl();
$parameters['method'] = (string) $method;
$parameters['format'] = 'json';
// when we need authentication we need to add some extra parameters
if ($authenticate) {
$parameters['email'] = $this->getEmail();
$parameters['nonce'] = md5(microtime(true) + rand(0, time()));
$parameters['secret'] = $this->getSecret($parameters['nonce']);
}
// HTTP method
if ($httpMethod == 'POST') {
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = http_build_query($parameters);
} else {
$options[CURLOPT_POST] = false;
if (!empty($parameters)) {
$url .= '?' . http_build_query($parameters);
}
}
// set options
$options[CURLOPT_URL] = $url;
$options[CURLOPT_USERAGENT] = $this->getUserAgent();
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
$options[CURLOPT_FOLLOWLOCATION] = true;
}
$options[CURLOPT_RETURNTRANSFER] = true;
$options[CURLOPT_TIMEOUT] = (int) $this->getTimeOut();
$options[CURLOPT_SSL_VERIFYPEER] = false;
$options[CURLOPT_SSL_VERIFYHOST] = false;
// init
$curl = curl_init();
// set options
curl_setopt_array($curl, $options);
// execute
$response = curl_exec($curl);
$headers = curl_getinfo($curl);
// fetch errors
$errorNumber = curl_errno($curl);
$errorMessage = curl_error($curl);
// close
curl_close($curl);
$json = json_decode($response, true);
if (
!isset($json['meta']['status_code']) ||
!isset($json['data'])
) {
throw new Exception('Invalid response');
}
if ($json['meta']['status_code'] != 200) {
throw new Exception(
$json['meta']['status'],
$json['meta']['status_code']
);
}
// we expect JSON, so decode it
return $json['data'];
}
/**
* get the API key
*
* @return string
*/
public function getApiKey()
{
return $this->apiKey;
}
/**
* Get the e-mail address
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Get the timeout that will be used
*
* @return int
*/
public function getTimeOut()
{
return $this->timeOut;
}
/**
* Calculate the secret
*
* @param string $nonce
* @return string
*/
private function getSecret($nonce)
{
$base = $this->getEmail();
$base .= $this->getApiKey();
return sha1(md5($nonce) . md5($base));
}
/**
* Get the useragent that will be used. Our version will be prepended to
* yours.
* It will look like: "PHP ForkAPI/<version> <your-user-agent>"
*
* @return string
*/
public function getUserAgent()
{
return 'PHP ForkAPI/'. self::VERSION .' '. $this->userAgent;
}
/**
* Get the URL of the website we are working on
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set the API key
*
* @param string $apiKey
*/
public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;
}
/**
* Set the e-mail address
*
* @param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* Set the timeout
* After this time the request will stop. You should handle any errors triggered by this.
*
* @return void
* @param int $seconds The timeout in seconds.
*/
public function setTimeOut($seconds)
{
$this->timeOut = (int) $seconds;
}
/**
* Set the user-agent for you application
* It will be appended to ours, the result will look like:
* "PHP ForkAPI/<version> <your-user-agent>"
*
* @return void
* @param string $userAgent Your user-agent, it should look like <app-name>/<app-version>.
*/
public function setUserAgent($userAgent)
{
$this->userAgent = (string) $userAgent;
}
/**
* Set the URL to work on
*
* @param string $url
*/
protected function setUrl($url)
{
// make sure there is a / on the end
$url = trim((string) $url, '/') . '/';
$this->url = (string) $url;
}
/**
* Get the API key for a user
*
* @param string $email
* @param string $password
* @return array
*/
public function coreGetAPIKey($email, $password)
{
// build parameters
$parameters['email'] = (string) $email;
$parameters['password'] = (string) $password;
// make the call
return $this->doCall('core.getAPIKey', $parameters, 'GET', false);
}
/**
* Get info about the site.
*
* @return array
*/
public function coreGetInfo()
{
return $this->doCall('core.getInfo', null, 'GET');
}
/**
* Add a Apple device token to a user
*
* @param string $token
*/
public function coreAppleAddDevice($token)
{
// build parameters
$parameters['token'] = (string) $token;
// make the call
$this->doCall('core.apple.addDevice', $parameters, 'POST');
}
/**
* Remove a Apple device token from a user
*
* @param string $token
*/
public function coreAppleRemoveDevice($token)
{
// build parameters
$parameters['token'] = (string) $token;
// make the call
$this->doCall('core.apple.removeDevice', $parameters, 'POST');
}
/**
* Get the comments
*
* @param string[optional] $status The type of comments to get. Possible values are: published, moderation, spam.
* @param int[optional] $limit The maximum number of items to retrieve.
* @param int[optional] $offset The offset.
* @return mixed
*/
public function blogCommentsGet($status = null, $limit = null, $offset = null)
{
// build parameters
$parameters = null;
if ($status !== null) {
$parameters['status'] = (string) $status;
}
if ($limit !== null) {
$parameters['limit'] = (int) $limit;
}
if ($offset !== null) {
$parameters['offset'] = (int) $offset;
}
// make the call
return $this->doCall('blog.comments.get', $parameters);
}
/**
* Get a single comment.
*
* @param string $id
* @return mixed
*/
public function blogCommentsGetById($id)
{
// build parameters
$parameters['id'] = (string) $id;
// make the call
return $this->doCall('blog.comments.getById', $parameters);
}
/**
* Update a comment
*
* @param string $id The id of the comment.
* @param string[optional] $status The new status for the comment. Possible values are: published, moderation, spam.
* @param string[optional] $text The new text for the comment.
* @param string[optional] $authorName The new author for the comment.
* @param string[optional] $authorEmail The new email for the comment.
* @param string[optional] $authorWebsite The new website for the comment.
*/
public function blogCommentsUpdate(
$id,
$status = null,
$text = null,
$authorName = null,
$authorEmail = null,
$authorWebsite = null
) {
// build parameters
$parameters['id'] = (string) $id;
if ($status !== null) {
$parameters['status'] = (string) $status;
}
if ($text !== null) {
$parameters['text'] = (string) $text;
}
if ($authorName !== null) {
$parameters['authorName'] = (string) $authorName;
}
if ($authorEmail !== null) {
$parameters['authorEmail'] = (string) $authorEmail;
}
if ($authorWebsite !== null) {
$parameters['authorWebsite'] = (string) $authorWebsite;
}
// make the call
$this->doCall('blog.comments.update', $parameters, 'POST');
}
/**
* Update the status for multiple comments at once.
*
* @param array $ids The ids of the commen(s to update.
* @param string $status The new status for the comment. Possible values are: published, moderation, spam.
*/
public function blogCommentsUpdateStatus(array $ids, $status)
{
// build parameters
$parameters['id'] = $ids;
$parameters['status'] = (string) $status;
// make the call
$this->doCall('blog.comments.updateStatus', $parameters, 'POST');
}
}