/
akismet.php
173 lines (155 loc) · 4.84 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
<?php
/**
* @package Akismet Library
* @author Callum Macrae
* @version 1.0.0-RC1
* @license http://www.gnu.org/licenses/gpl2.html GNU General Public License
*/
class Akismet
{
private $config = array();
private $version = '1.0.0-RC1';
/**
* Gets the configuration, attempts to find the URL if it isn't defined in
* the configuration, and test whether API is valid and a connection can
* be made. If it cannot, call the callback.
*
* @param array $config Configuration
* @param string $fallback The name of the fallback to be called
* @param array $fallback_args The params to be passed to the fallback
* If not an array, it will be converted to one
*/
public function __construct($config = false, $fallback = false, $fallback_args = false)
{
if (!is_array($config))
{
require($config ?: './config.php');
}
$this->config = $config;
if ($this->config['url_auto'])
{
//lets take getting the url far too seriously:
$this->config['url'] = ($_SERVER['HTTPS'] == 'on' ? 'https' : 'http') . '://' . $_SERVER['SERVER_NAME'] . ($_SERVER['SERVER_PORT'] != 80 ? ':' . $_SERVER['SERVER_PORT'] : null) . '/';
}
//check whether api key is valid
if (!$this->test())
{
if ($fallback)
{
//convert arguments to an array if they're not already
if (!is_array($fallback_args))
{
$fallback_args = array($fallback_args);
}
//call the fallback
call_user_func_array($fallback, $fallback_args);
}
return false;
}
return true;
}
/**
* Sends HTTP request to Akismet. Uses HTTP/1.0 because Akismet
* told me to.
*
* @param string $request String to be passed via POST to Akismet
* @param string $url URL for the request to be sent to
* @param string $path Path for the request to be sent to
*/
private function http_post($request, $url, $path)
{
$useragent = $this->config['app'] . '/' . $this->config['app_ver'] . ' callumacrae/Akismet/ . ' . $this->version;
$ch = curl_init('http://' . $url . $path);
curl_setopt_array($ch, array(
CURLOPT_HEADER => false,
CURLOPT_USERAGENT => $useragent,
CURLOPT_TIMEOUT => $this->config['timeout'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $request,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* Tests whether a connection can be made
*/
public function test()
{
$test = $this->http_post('key=' . $this->config['api'] . '&blog=' . $this->config['url'], $this->config['akismet_server'], '/' . $this->config['akismet_version'] . '/verify-key');
return $test == 'valid';
}
/**
* Gets info from the $user array and formats it into something that
* can be sent to Akismet. See the following page for info about what
* to put in the array (all required info is done for you):
* http://akismet.com/development/api/#comment-check
*
* @param array $user Array to be formatted
*/
private function get_info($user)
{
$info = array(
'blog=' . urlencode($this->config['url']),
'user_ip=' . $_SERVER['REMOTE_ADDR'],
'user_agent=' . urlencode($_SERVER['HTTP_USER_AGENT']),
'referrer=' . urlencode($_SERVER['HTTP_REFERER']),
);
if (!empty($user['comment_type']))
{
$info[] = urlencode('comment_type=' . $user['comment_type']);
}
if (!empty($user['comment_author']))
{
$info[] = urlencode('comment_author=' . $user['comment_author']);
}
if (!empty($user['comment_author_email']))
{
$info[] = urlencode('comment_author_email=' . $user['comment_author_email']);
}
if (!empty($user['comment_content']))
{
$info[] = urlencode('comment_content=' . $user['comment_content']);
}
$info = implode('&', $info);
return $info;
}
/**
* Checks whether comment is spam.
*
* @param array $user Array containing details of what should be sent
* to Akismet
*/
public function check_spam($user)
{
$info = $this->get_info($user);
$spam = $this->http_post($info, $this->config['api'] . '.' . $this->config['akismet_server'], '/' . $this->config['akismet_version'] . '/comment-check');
return $spam == 'true';
}
/**
* Submit spam to Akismet
*
* @param array $user Array containing details of what should be sent
* to Akismet
*/
public function submit_spam($user)
{
$info = $this->get_info($user);
$spam = $this->http_post($info, $this->config['api'] . '.' . $this->config['akismet_server'], '/' . $this->config['akismet_version'] . '/submit-spam');
return $spam == 'true';
}
/**
* Submit ham to Akismet
*
* @param array $user Array containing details of what should be sent
* to Akismet
*/
public function submit_ham($user)
{
$info = $this->get_info($user);
$spam = $this->http_post($info, $this->config['api'] . '.' . $this->config['akismet_server'], '/' . $this->config['akismet_version'] . '/submit-ham');
return $spam == 'true';
}
}