This repository was archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAkismet.php
executable file
·528 lines (459 loc) · 16.6 KB
/
Akismet.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<?php
namespace TijsVerkoyen\Akismet;
/**
* Akismet class
*
* @author Tijs Verkoyen <php-akismet@verkoyen.eu>
* @version 1.1.0
* @copyright Copyright (c) Tijs Verkoyen. All rights reserved.
* @license BSD License
*/
class Akismet
{
// internal constant to enable/disable debugging
const DEBUG = false;
// url for the api
const API_URL = 'http://rest.akismet.com';
// port for the api
const API_PORT = 80;
// version of the api
const API_VERSION = '1.1';
// current version
const VERSION = '1.1.0';
/**
* The key for the API
* @var string
*/
private $apiKey;
/**
* The timeout
* @var int
*/
private $timeOut = 60;
/**
* The user agent
* @var string
*/
private $userAgent;
/**
* The url
* @var string
*/
private $url;
// class methods
/**
* Default constructor
* Creates an instance of the Akismet Class.
*
* @param string $apiKey API key being verified for use with the API.
* @param string $url The front page or home URL of the instance making
* the request. For a blog or wiki this would be the
* front page. Note: Must be a full URI, including
* http://.
*/
public function __construct($apiKey, $url)
{
$this->setApiKey($apiKey);
$this->setUrl($url);
}
/**
* Make the call
* @param string $url URL to call.
* @param array[optional] $aParameters The parameters to pass.
* @param bool[optional] $authenticate Should we authenticate?
* @return string
*/
private function doCall($url, $aParameters = array(), $authenticate = true)
{
// redefine
$url = (string) $url;
$aParameters = (array) $aParameters;
$authenticate = (bool) $authenticate;
// build url
$url = self::API_URL . '/' . self::API_VERSION . '/' . $url;
// add key in front of url
if ($authenticate) {
// get api key
$apiKey = $this->getApiKey();
// validate apiKey
if ($apiKey == '') throw new Exception('Invalid API-key');
// prepend key
$url = str_replace('http://', 'http://' . $apiKey . '.', $url);
}
// add url into the parameters
$aParameters['blog'] = $this->getUrl();
// set options
$options[CURLOPT_URL] = $url;
$options[CURLOPT_PORT] = self::API_PORT;
$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_POST] = true;
$options[CURLOPT_POSTFIELDS] = $aParameters;
// speed up things, use HTTP 1.0
$options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
// 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);
// invalid headers
if (!in_array($headers['http_code'], array(0, 200))) {
// should we provide debug information
if (self::DEBUG) {
// make it output proper
echo '<pre>';
// dump the header-information
var_dump($headers);
// dump the raw response
var_dump($response);
// end proper format
echo '</pre>';
// stop the script
exit();
}
// throw error
throw new Exception(null, (int) $headers['http_code']);
}
// error?
if ((int) $errorNumber > 0) {
throw new Exception($errorMessage, $errorNumber);
}
// return
return $response;
}
/**
* Get the API-key that will be used
*
* @return string
*/
private function getApiKey()
{
return (string) $this->apiKey;
}
/**
* Get the timeout that will be used
*
* @return int
*/
public function getTimeOut()
{
return (int) $this->timeOut;
}
/**
* Get the url of the instance making the request
*
* @return string
*/
public function getUrl()
{
return (string) $this->url;
}
/**
* Get the useragent that will be used.
* Our version will be prepended to yours. It will look like:
* "PHP Akismet/<version> <your-user-agent>"
*
* @return string
*/
public function getUserAgent()
{
return (string) 'PHP Akismet/' . self::VERSION . ' ' . $this->userAgent;
}
/**
* Set API key that has to be used
*
* @param string $apiKey API key to use.
*/
private function setApiKey($apiKey)
{
$this->apiKey = (string) $apiKey;
}
/**
* Set the timeout
* After this time the request will stop. You should handle any errors
* triggered by this.
*
* @param int $seconds The timeout in seconds.
*/
public function setTimeOut($seconds)
{
$this->timeOut = (int) $seconds;
}
/**
* Set the url of the instance making the request
* @param string $url The URL making the request.
*/
private function setUrl($url)
{
$this->url = (string) $url;
}
/**
* Set the user-agent for you application
* It will be appended to ours, the result will look like:
* "PHP Akismet/<version> <your-user-agent>"
*
* @param string $userAgent The user-agent, it should look like:
* <app-name>/<app-version>.
*/
public function setUserAgent($userAgent)
{
$this->userAgent = (string) $userAgent;
}
// api methods
/**
* Verifies the key
* @return bool if the key is valid it will return true, otherwise false
* will be returned.
*/
public function verifyKey()
{
// possible answers
$aPossibleResponses = array('valid', 'invalid');
// build parameters
$aParameters['key'] = $this->getApiKey();
// make the call
$response = $this->doCall('verify-key', $aParameters, false);
// validate response
if (!in_array($response, $aPossibleResponses)) {
throw new Exception($response, 400);
}
// valid key
if ($response == 'valid') return true;
// fallback
return false;
}
/**
* Check if the comment is spam or not
* This is basically the core of everything. This call takes a number of
* arguments and characteristics about the submitted content and then
* returns a thumbs up or thumbs down.
* Almost everything is optional, but performance can drop dramatically if
* you exclude certain elements.
* REMARK: If you are having trouble triggering you can send
* "viagra-test-123" as the author and it will trigger a true response,
* always.
*
* @param string[optional] $content The content that was submitted.
* @param string[optional] $author The name.
* @param string[optional] $email The email address.
* @param string[optional] $url The URL.
* @param string[optional] $permalink The permanent location of the entry
* the comment was submitted to.
* @param string[optional] $type The type, can be blank, comment,
* trackback, pingback, or a made up
* value like "registration".
* @return bool If the comment is spam true will be
* returned, otherwise false.
*/
public function isSpam(
$content,
$author = null,
$email = null,
$url = null,
$permalink = null,
$type = null
)
{
// possible answers
$aPossibleResponses = array('true', 'false');
// redefine
$content = (string) $content;
$author = (string) $author;
$email = (string) $email;
$url = (string) $url;
$permalink = (string) $permalink;
$type = (string) $type;
// get stuff from the $_SERVER-array
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
} else $ip = '';
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$userAgent = (string) $_SERVER['HTTP_USER_AGENT'];
} else $userAgent = '';
if (isset($_SERVER['HTTP_REFERER'])) {
$referrer = (string) $_SERVER['HTTP_REFERER'];
} else $referrer = '';
// build parameters
$aParameters['user_ip'] = $ip;
$aParameters['user_agent'] = $userAgent;
if ($referrer != '') $aParameters['referrer'] = $referrer;
if ($permalink != '') $aParameters['permalink'] = $permalink;
if ($type != '') $aParameters['comment_type'] = $type;
if ($author != '') $aParameters['comment_author'] = $author;
if ($email != '') $aParameters['comment_author_email'] = $email;
if ($url != '') $aParameters['comment_author_url'] = $url;
$aParameters['comment_content'] = $content;
// add all stuff from $_SERVER
foreach ($_SERVER as $key => $value) {
// keys to ignore
$aKeysToIgnore = array(
'HTTP_COOKIE', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED_HOST',
'HTTP_MAX_FORWARDS', 'HTTP_X_FORWARDED_SERVER',
'REDIRECT_STATUS', 'SERVER_PORT', 'PATH', 'DOCUMENT_ROOT',
'SERVER_ADMIN', 'QUERY_STRING', 'PHP_SELF', 'argv', 'argc',
'SCRIPT_FILENAME', 'SCRIPT_NAME'
);
// add to parameters if not in ignore list
if (!in_array($key, $aKeysToIgnore)) $aParameters[$key] = $value;
}
// make the call
$response = $this->doCall('comment-check', $aParameters);
// validate response
if (!in_array($response, $aPossibleResponses)) {
throw new Exception($response, 400);
}
// process response
if ($response == 'true') return true;
// fallback
return false;
}
/**
* Submit ham to Akismet
* This call is intended for the marking of false positives, things that
* were incorrectly marked as spam.
* @param string $userIp The address of the comment submitter.
* @param string $userAgent The agent information.
* @param string[optional] $content The content that was submitted.
* @param string[optional] $author The name of the author.
* @param string[optional] $email The email address.
* @param string[optional] $url The URL.
* @param string[optional] $permalink The permanent location of the entry
* the comment was submitted to.
* @param string[optional] $type The type, can be blank, comment,
* trackback, pingback, or a made up
* value like "registration".
* @param string[optional] $referrer The content of the HTTP_REFERER
* header should be sent here.
* @param array[optional] $others Extra data (the variables from
* $_SERVER).
* @return bool If everything went fine true will be
* returned, otherwise an exception
* will be triggered.
*/
public function submitHam(
$userIp,
$userAgent,
$content,
$author = null,
$email = null,
$url = null,
$permalink = null,
$type = null,
$referrer = null,
$others = null
)
{
// possible answers
$aPossibleResponses = array('Thanks for making the web a better place.');
// redefine
$userIp = (string) $userIp;
$userAgent = (string) $userAgent;
$content = (string) $content;
$author = (string) $author;
$email = (string) $email;
$url = (string) $url;
$permalink = (string) $permalink;
$type = (string) $type;
$referrer = (string) $referrer;
$others = (array) $others;
// build parameters
$aParameters['user_ip'] = $userIp;
$aParameters['user_agent'] = $userAgent;
if ($referrer != '') $aParameters['referrer'] = $referrer;
if ($permalink != '') $aParameters['permalink'] = $permalink;
if ($type != '') $aParameters['comment_type'] = $type;
if ($author != '') $aParameters['comment_author'] = $author;
if ($email != '') $aParameters['comment_author_email'] = $email;
if ($url != '') $aParameters['comment_author_url'] = $url;
$aParameters['comment_content'] = $content;
// add other parameters
foreach ($others as $key => $value) $aParameters[$key] = $value;
// make the call
$response = $this->doCall('submit-ham', $aParameters);
// validate response
if (in_array($response, $aPossibleResponses)) return true;
// fallback
throw new Exception($response);
}
/**
* Submit spam to Akismet
* This call is for submitting comments that weren't marked as spam but
* should have been.
* @param string $userIp The address of the comment submitter.
* @param string $userAgent The agent information.
* @param string[optional] $content The content that was submitted.
* @param string[optional] $author The name of the author.
* @param string[optional] $email The email address.
* @param string[optional] $url The URL.
* @param string[optional] $permalink The permanent location of the entry
* the comment was submitted to.
* @param string[optional] $type The type, can be blank, comment,
* trackback, pingback, or a made up
* value like "registration".
* @param string[optional] $referrer The content of the HTTP_REFERER
* header should be sent here.
* @param array[optional] $others Extra data (the variables from
* $_SERVER).
* @return bool If everything went fine true will be
* returned, otherwise an exception
* will be triggered.
*/
public function submitSpam(
$userIp,
$userAgent,
$content,
$author = null,
$email = null,
$url = null,
$permalink = null,
$type = null,
$referrer = null,
$others = null
)
{
// possible answers
$aPossibleResponses = array('Thanks for making the web a better place.');
// redefine
$userIp = (string) $userIp;
$userAgent = (string) $userAgent;
$content = (string) $content;
$author = (string) $author;
$email = (string) $email;
$url = (string) $url;
$permalink = (string) $permalink;
$type = (string) $type;
$referrer = (string) $referrer;
$others = (array) $others;
// build parameters
$aParameters['user_ip'] = $userIp;
$aParameters['user_agent'] = $userAgent;
if ($referrer != '') $aParameters['referrer'] = $referrer;
if ($permalink != '') $aParameters['permalink'] = $permalink;
if ($type != '') $aParameters['comment_type'] = $type;
if ($author != '') $aParameters['comment_author'] = $author;
if ($email != '') $aParameters['comment_author_email'] = $email;
if ($url != '') $aParameters['comment_author_url'] = $url;
$aParameters['comment_content'] = $content;
// add other parameters
foreach ($others as $key => $value) $aParameters[$key] = $value;
// make the call
$response = $this->doCall('submit-spam', $aParameters);
// validate response
if (in_array($response, $aPossibleResponses)) return true;
// fallback
throw new Exception($response);
}
}