forked from ProThoughts/q2apro-stop-spam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq2apro-stopspam-filter-posts.php
336 lines (285 loc) · 10.2 KB
/
q2apro-stopspam-filter-posts.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
<?php
/*
Plugin Name: Stop Spam
Plugin URI: http://www.q2apro.com/plugins/stop-spam
Plugin Description: An easy to use customizable captcha with a honeypot that keeps 99 % of the spam away
Plugin Version: 1.0
Plugin Date: 2014-02-20
Plugin Author: q2apro.com
Plugin Author URI: http://www.q2apro.com
Plugin Minimum Question2Answer Version: 1.5
Plugin Update Check URI: http://www.q2apro.com/pluginupdate?id=7
Licence: Copyright © q2apro.com - All rights reserved
*/
if (!defined('QA_VERSION'))
{ // don't allow this page to be requested directly from browser
header('Location: ../');
exit;
}
class q2apro_stopspam_filter_posts
{
var $directory;
function load_module($directory, $urltoroot)
{
$this->directory=$directory;
}
// newly added, prevent certain emails to be registered
public function filter_email(&$email, $olduser)
{
$email = strtolower($email);
// also remove all whitespaces
$emailbans = preg_replace('/\s+/', '', qa_opt('q2apro_stopspam_filter_emails') );
if(!empty($emailbans))
{
// array from string
$notAllowed = explode(',', $emailbans);
// check if email contains one of the spam strings
foreach($notAllowed as $string)
{
if(strpos($email, $string) !== false)
{
// error message frontend
return qa_lang('q2apro_stopspam_lang/emaildomain_unwanted');
}
}
}
}
function filter_question(&$question, &$errors, $oldquestion)
{
$question['content'] = $this->removeLineBreaks($question['content'], $question['format']);
// remove invisible characters from title, mostly happens on copy & paste from PDFs
$question['title'] = preg_replace('/[^\P{C}\n]+/u', '', $question['title']);
// check if question title already exists, prevent duplicate
$quTitleExists = qa_db_read_one_value( qa_db_query_sub('SELECT title
FROM `^posts`
WHERE title = #
AND type = "Q"
LIMIT 1', $question['title']), true
);
if( $quTitleExists && is_null($oldquestion) )
{
$errors['title'] = qa_lang('q2apro_stopspam_lang/is_duplicate');
}
// is admin mentioned, e.g. @admin, then send email
$this->notifyIfMentioned($question['content']);
// intercept certain spam words or domains
$blockmsg = $this->checkForSpamWords($question['content']);
if($blockmsg)
{
$errors['content'] = $blockmsg;
return;
}
// link array
$links = array();
// get number of http links from content
// $links = substr_count($question['content'], 'http');
$links = q2apro_list_all_getUrls_spamplugin($question['content']);
if(q2apro_stopspam_urlexceeded($links))
{
$errors['content'] = qa_lang('q2apro_stopspam_lang/too_many_links');
return;
}
/* Queue Example */
// if ( strpos($question['content'], 'idiot') !== false ) {
// $question['queued'] = true;
// }
}
function filter_answer(&$answer, &$errors, $question, $oldanswer)
{
$answer['content'] = $this->removeLineBreaks($answer['content'], $answer['format']);
// is admin mentioned, e.g. @admin, then send email
$this->notifyIfMentioned($answer['content']);
// intercept certain spam words or domains
$blockmsg = $this->checkForSpamWords($answer['content']);
if($blockmsg)
{
$errors['content'] = $blockmsg;
return;
}
// check if number of links is exceeded
$allowedlinks = qa_opt('q2apro_stopspam_allow_links_count');
// get number of http links from content
$linkcount = substr_count($answer['content'], 'http');
if($linkcount>$allowedlinks)
{
$errors['content'] = qa_lang('q2apro_stopspam_lang/too_many_links');
return;
}
// normal user that posts links, moderate post by sending it to the queue
if($linkcount>0 && qa_get_logged_in_level() < QA_USER_LEVEL_EXPERT && qa_opt('q2apro_stopspam_moderate_post_with_links'))
{
$answer['queued'] = true;
}
}
function filter_comment(&$comment, &$errors, $question, $parent, $oldcomment)
{
$comment['content'] = $this->removeLineBreaks($comment['content'], $comment['format']);
// is admin mentioned, e.g. @admin, then send email
$this->notifyIfMentioned($comment['content']);
// intercept certain spam words or domains
$blockmsg = $this->checkForSpamWords($comment['content']);
if($blockmsg)
{
$errors['content'] = $blockmsg;
}
// check if number of links is exceeded
$allowedlinks = qa_opt('q2apro_stopspam_allow_links_count');
// get number of http links from content
$linkcount = substr_count($comment['content'], 'http');
if($linkcount>$allowedlinks)
{
$errors['content'] = qa_lang('q2apro_stopspam_lang/too_many_links');
return;
}
// normal user that posts links, moderate post by sending it to the queue
if($linkcount>0 && qa_get_logged_in_level() < QA_USER_LEVEL_EXPERT && qa_opt('q2apro_stopspam_moderate_post_with_links'))
{
$comment['queued'] = true;
}
}
/* intercept certain spam words or domains, returns not-allowed-String if spam found, else false */
function checkForSpamWords($posttext)
{
$checktext = strtolower($posttext);
// receive spam words from table qa_options, row filter_posts_words, which can be set in plugin options
// also remove all whitespaces
$spamwords = preg_replace('/\s+/', '', qa_opt('q2apro_stopspam_filter_words') );
if(!empty($spamwords))
{
// array from string
$notAllowedTerms = explode(',', $spamwords);
// check if checktext contains one of the spam strings
foreach($notAllowedTerms as $string)
{
if(strpos($checktext, $string) !== false)
{
// $answer['queued'] = true;
// error message
return qa_lang('q2apro_stopspam_lang/contains_unwanted').' '.$string;
}
}
}
// check for chinese characters and block them
if(qa_opt('q2apro_stopspam_filter_languages'))
{
$languages = qa_opt('q2apro_stopspam_filter_languages');
// remove whitespaces and convert string to array
$blocklanguages = explode(',', str_replace(' ', '', $languages));
// $blocklanguages = array('Han','Hangul','Hebrew','Arabic','Cyrillic','Greek','Khmer');
foreach($blocklanguages as $lang)
{
if(preg_match("/\p{".$lang."}+/u", $posttext))
{
if($lang=='Han')
{
// just for frontend that users understand
$lang = 'Chinese';
}
return qa_lang('q2apro_stopspam_lang/contains_unwanted_lang').' '.$lang;
}
}
}
// no spam word found
return false;
}
/* remove empty p and br tags causing unnecessary line breaks */
function removeLineBreaks(&$content, &$format)
{
if(qa_opt('q2apro_stopspam_remove_linebreaks'))
{
// only TEXT - the text is converted to nl2br later
if(empty($format))
{
// merge \n line-breaks into one
$content = preg_replace('/( (\\n\s*))+/im', "\n", $content);
// \s : match also new lines
// * : /cat*/g matches all the cats in "cat catamaran cater"
// i : ignore case
// m : go over multiple lines (match the beginning or end of each line, delimited by \n or \r, not only the very beginning or end of the whole input string)
// remove line breaks from end of string, i.e. text ending with \n
$content = preg_replace('/(\n)+$/', '', $content);
}
// HTML with tags
else
{
// merge empty p tags into one
$content = preg_replace('/((<p\s*\/?>\s*) (<\/p\s*\/?>\s*))+/im', "<p> </p>\n", $content);
// remove spaces from end of string ( )
$content = preg_replace('/( )+$/', '', $content);
// remove sceditor's: <p>\n<br>\n</p> from end of string
// \s* matches any number of whitespace characters (" ", \t, \n, etc)
// (?:...)+ matches one or more (without capturing the group)
// $ forces match to only be made at the end of the string
$content = preg_replace("/(?:<p>\s*(<br>\s*)+\s*<\/p>\s*)+$/", "", $content);
// remove line breaks from end of string - $ is end of line, +$ is end of line including \n
// html with <p> </p>
$content = preg_replace('/(<p> <\/p>)+$/', '', $content);
$content = preg_replace('/(<br>)+$/', '', $content);
// remove line breaks from beginning of string
$content = preg_replace('/^(<p> <\/p>)+/', '', $content);
}
// filter and mask email addresses (also done in jquery)
// $pattern = '/[^@\s]*@[^@\s]*\.[^@\s]*/';
if(qa_opt('q2apro_stopspam_mask_emails'))
{
$pattern = '/[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/';
$replacement = qa_lang('q2apro_stopspam_lang/emails_notallowed');
$content = preg_replace($pattern, $replacement, $content);
}
/* remove invisible characters, mostly happens on copy & paste from PDFs */
$content = preg_replace('/[^\P{C}\n]+/u', '', $content);
}
// done
return $content;
}
function notifyIfMentioned($fullstring)
{
$searchstring = qa_opt('q2apro_stopspam_notify_mentioning');
if (!empty($searchstring) && strpos($fullstring,$searchstring) !== false)
{
require_once QA_INCLUDE_DIR.'qa-app-emails.php';
// qa_send_notification($userid, $email, $handle, $subject, $body, $subs)
qa_send_notification(null, qa_opt('feedback_email'), null, 'You were mentioned in a post', $fullstring, null);
}
}
}
function q2apro_list_all_getUrls_spamplugin($string)
{
// $regex = '/https?\:\/\/[^\" ]+/i';
$regex = '#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#';
preg_match_all($regex, $string, $matches);
return ($matches[0]);
}
function q2apro_stopspam_urlexceeded($links)
{
$spamurlcount = 0;
if(count($links)>0)
{
// remove duplicate links
$links = array_unique($links);
// ignore internal links
$ignorelinks = array(qa_opt('site_url'));
for($i=0;$i<count($links);$i++)
{
// check against list of ignore words
foreach($ignorelinks as $ignorelink)
{
if(isset($links[$i]))
{
if(strpos($links[$i],$ignorelink) !== false)
{
}
else
{
$spamurlcount++;
}
}
}
}
}
// check if number of links is exceeded
return ($spamurlcount>qa_opt('q2apro_stopspam_allow_links_count'));
}
/*
Omit PHP closing tag to help avoid accidental output
*/