forked from habari-extras/habariakismet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhabariakismet.plugin.php
153 lines (121 loc) · 5.32 KB
/
habariakismet.plugin.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
<?php
namespace Habari;
require_once 'vendor/Akismet.class.php';
class HabariAkismet extends Plugin
{
const SERVER_AKISMET = 'rest.akismet.com';
const SERVER_TYPEPAD = 'api.antispam.typepad.com';
public function action_plugin_activation($file)
{
if (realpath($file) == __FILE__) {
Session::notice(_t('Please set your Akismet or TypePad AntiSpam API Key in the configuration.'));
}
}
public function filter_plugin_config($actions, $plugin_id)
{
if ($plugin_id == $this->plugin_id()) {
$actions[] = _t('Configure');
}
return $actions;
}
public function action_plugin_ui($plugin_id, $action)
{
if ($plugin_id == $this->plugin_id()) {
switch ($action) {
case _t('Configure'):
$form = new FormUI(strtolower(get_class($this)));
$p = FormControlSelect::create('provider', 'habariakismet__provider')->label('Service');
$form->append($p);
$form->provider->options = array(
'Akismet' => 'Akismet',
'TypePad AntiSpam' => 'TypePad AntiSpam'
);
$k = FormControlText::create('api_key', 'habariakismet__api_key')->label('API Key');
$api_key = $form->append($k);
$api_key->add_validator(array($this, 'validate_required'));
$api_key->add_validator(array($this, 'validate_api_key'));
$form->append('submit', 'save', 'Save');
$form->out();
break;
}
}
}
public function validate_api_key($key, $control, $form)
{
$endpoint = ($form->provider->value == 'Akismet') ? self::SERVER_AKISMET : self::SERVER_TYPEPAD;
$a = new \Akismet(Site::get_url('habari'), $form->api_key->value);
$a->setAkismetServer($endpoint);
if (!$a->isKeyValid()) {
return array(sprintf(_t('Sorry, the %s API key %s is <b>invalid</b>. Please check to make sure the key is entered correctly.'), $form->provider->value, $form->api_key->value));
}
return array();
}
public function set_priorities()
{
return array(
'action_comment_insert_before' => 1
);
}
public function action_comment_insert_before(Comment $comment)
{
$api_key = Options::get('habariakismet__api_key');
$provider = Options::get('habariakismet__provider');
if ($api_key == null || $provider == null) {
return;
}
$endpoint = ($provider == 'Akismet') ? self::SERVER_AKISMET : self::SERVER_TYPEPAD;
$a = new \Akismet(Site::get_url('habari'), $api_key);
$a->setAkismetServer($endpoint);
$a->setCommentAuthor($comment->name);
$a->setCommentAuthorEmail($comment->email);
$a->setCommentAuthorURL($comment->url);
$a->setCommentContent($comment->content);
$a->setPermalink($comment->post->permalink);
try {
$comment->status = ($a->isCommentSpam()) ? 'spam' : 'ham';
return;
} catch (Exception $e) {
EventLog::log($e->getMessage(), 'notice', 'comment', 'HabariAkismet');
}
}
public function action_admin_moderate_comments($action, $comments, $handler)
{
$false_negatives = 0;
$false_positives = 0;
$provider = Options::get('habariakismet__provider');
$endpoint = ($provider == 'Akismet') ? self::SERVER_AKISMET : self::SERVER_TYPEPAD;
$a = new \Akismet(Site::get_url('habari'), Options::get('habariakismet__api_key'));
$a->setAkismetServer($endpoint);
foreach ($comments as $comment) {
switch ($action) {
case 'spam':
if ($comment->status == Comment::STATUS_APPROVED || $comment->status == Comment::STATUS_UNAPPROVED) {
$a->setCommentAuthor($comment->name);
$a->setCommentAuthorEmail($comment->email);
$a->setCommentAuthorURL($comment->url);
$a->setCommentContent($comment->content);
$a->submitSpam();
$false_negatives++;
}
break;
case 'approved':
if ($comment->status == Comment::STATUS_SPAM) {
$a->setCommentAuthor($comment->name);
$a->setCommentAuthorEmail($comment->email);
$a->setCommentAuthorURL($comment->url);
$a->setCommentContent($comment->content);
$a->submitHam();
$false_positives++;
}
break;
}
}
if ($false_negatives) {
Session::notice(_t('Reported %d false negatives to %s.', array($false_negatives, $provider)));
}
if ($false_positives) {
Session::notice(_t('Reported %d false positives to %s.', array($false_positives, $provider)));
}
}
}
?>