PluginProbe
wpForo Forum / 1.4.0
wpForo Forum v1.4.0
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / wpf-includes / class-moderation.php

class-moderation.php in wpForo Forum 1.4.0, at wpf-includes/class-moderation.php

391 lines 14.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if (!defined('ABSPATH')) exit;
4
5 class wpForoModeration
6 {
7 private $wpforo;
8 private $db;
9 public $post_statuses;
10
11 public function __construct($wpforo)
12 {
13 $this->wpforo = $wpforo;
14 $this->db = $wpforo->db;
15 $this->post_statuses = apply_filters('wpforo_post_statuses', array('approved', 'unapproved'));
16 }
17
18 public function init(){
19 if( !$this->wpforo->perm->usergroup_can( 'aup' ) ){
20 add_filter('wpforo_add_topic_data_filter', array(&$this, 'auto_moderate'));
21 add_filter('wpforo_add_post_data_filter', array(&$this, 'auto_moderate'));
22 }
23 else{
24 if( !$this->wpforo->perm->can_link() ){
25 add_filter('wpforo_add_topic_data_filter', array(&$this, 'remove_links'), 7);
26 add_filter('wpforo_edit_topic_data_filter', array(&$this, 'remove_links'), 7);
27 add_filter('wpforo_add_post_data_filter', array(&$this, 'remove_links'), 7);
28 add_filter('wpforo_edit_post_data_filter', array(&$this, 'remove_links'), 7);
29 }
30 if( $this->wpforo->member->current_user_is_new() ){
31 if (class_exists('Akismet')) {
32 add_filter('wpforo_add_topic_data_filter', array(&$this, 'akismet_topic'), 8);
33 add_filter('wpforo_edit_topic_data_filter', array(&$this, 'akismet_topic'), 8);
34 add_filter('wpforo_add_post_data_filter', array(&$this, 'akismet_post'), 8);
35 add_filter('wpforo_edit_post_data_filter', array(&$this, 'akismet_post'), 8);
36 }
37 if ( $this->wpforo->tools_antispam['spam_filter'] ) {
38 add_filter('wpforo_add_topic_data_filter', array(&$this, 'spam_topic'), 9);
39 add_filter('wpforo_edit_topic_data_filter', array(&$this, 'spam_topic'), 9);
40 add_filter('wpforo_add_topic_data_filter', array(&$this, 'spam_post'), 9);
41 add_filter('wpforo_edit_topic_data_filter', array(&$this, 'spam_post'), 9);
42 add_filter('wpforo_add_post_data_filter', array(&$this, 'spam_post'), 9);
43 add_filter('wpforo_edit_post_data_filter', array(&$this, 'spam_post'), 9);
44 }
45 }
46 if ( $this->wpforo->tools_antispam['spam_filter'] ) {
47 add_filter('wpforo_add_topic_data_filter', array(&$this, 'auto_moderate'), 10);
48 add_filter('wpforo_add_post_data_filter', array(&$this, 'auto_moderate'), 10);
49 }
50 }
51 }
52
53 public function get_post_status_dname($status)
54 {
55 $status = intval($status);
56 return (isset($this->post_statuses[$status]) ? $this->post_statuses[$status] : $status);
57 }
58
59 public function get_moderations($args, &$items_count = 0)
60 {
61 if (isset($_GET['filter_by_userid']) && wpforo_bigintval($_GET['filter_by_userid'])) $args['userid'] = wpforo_bigintval($_GET['filter_by_userid']);
62 $filter_by_status = intval((isset($_GET['filter_by_status']) ? $_GET['filter_by_status'] : 1));
63 $args['status'] = $filter_by_status;
64 if( !isset($_GET['order']) ) $args['order'] = 'DESC';
65 $posts = $this->wpforo->post->get_posts($args, $items_count);
66 return $posts;
67 }
68
69 public function search($needle, $fields = array())
70 {
71 $posts = $this->wpforo->post->search($needle);
72 $pids = array();
73 foreach ($posts as $post) $pids[] = $post['postid'];
74 return $pids;
75 }
76
77 public function post_approve($postid)
78 {
79 return $this->wpforo->post->status($postid, 0);
80 }
81
82 public function post_unapprove($postid)
83 {
84 return $this->wpforo->post->status($postid, 1);
85 }
86
87 public function get_view_url($arg)
88 {
89 return $this->wpforo->post->get_post_url($arg);
90 }
91
92 public function akismet_topic($item)
93 {
94 $post = array();
95 $post['user_ip'] = (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null);
96 $post['user_agent'] = (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null);
97 $post['referrer'] = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null);
98 $post['blog'] = get_option('home');
99 $post['blog_lang'] = get_locale();
100 $post['blog_charset'] = get_option('blog_charset');
101 $post['comment_type'] = 'forum-post';
102
103 if (empty($item['forumid'])) {
104 $topic = $this->wpforo->topic->get_topic($item['topicid']);
105 $item['forumid'] = $topic['forumid'];
106 }
107
108 $post['comment_author'] = $this->wpforo->current_user['user_nicename'];
109 $post['comment_author_email'] = $this->wpforo->current_user['user_email'];
110 $post['comment_author_url'] = $this->wpforo->member->get_profile_url($this->wpforo->current_userid);
111 $post['comment_post_modified_gmt'] = current_time('mysql', 1);
112 $post['comment_content'] = $item['title'] . " \r\n " . $item['body'];
113 $post['permalink'] = $this->wpforo->forum->get_forum_url($item['forumid']);
114
115 $response = Akismet::http_post(Akismet::build_query($post), 'comment-check');
116 if ($response[1] == 'true') {
117 $this->ban_for_spam( $this->wpforo->current_userid );
118 $item['status'] = 1;
119 }
120
121 return $item;
122 }
123
124 public function akismet_post($item)
125 {
126 $post = array();
127 $post['user_ip'] = (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null);
128 $post['user_agent'] = (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null);
129 $post['referrer'] = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null);
130 $post['blog'] = get_option('home');
131 $post['blog_lang'] = get_locale();
132 $post['blog_charset'] = get_option('blog_charset');
133 $post['comment_type'] = 'forum-post';
134
135 $topic = $this->wpforo->topic->get_topic($item['topicid']);
136
137 $post['comment_author'] = $this->wpforo->current_user['user_nicename'];
138 $post['comment_author_email'] = $this->wpforo->current_user['user_email'];
139 $post['comment_author_url'] = $this->wpforo->member->get_profile_url($this->wpforo->current_userid);
140 $post['comment_post_modified_gmt'] = $topic['modified'];
141 $post['comment_content'] = $item['body'];
142 $post['permalink'] = $this->wpforo->topic->get_topic_url($item['topicid']);
143
144 $response = Akismet::http_post(Akismet::build_query($post), 'comment-check');
145 if ($response[1] == 'true') {
146 $this->ban_for_spam( $this->wpforo->current_userid );
147 $item['status'] = 1;
148 }
149
150 return $item;
151 }
152
153 public function spam_attachment(){
154 $upload_dir = wp_upload_dir();
155 $default_attachments_dir = $upload_dir['basedir'] . '/wpforo/default_attachments/';
156 if(is_dir($default_attachments_dir)){
157 if ($handle = opendir($default_attachments_dir)){
158 while (false !== ($filename = readdir($handle))){
159 $file = $default_attachments_dir . '/' . $filename;
160 if( $filename == '.' || $filename == '..') continue;
161 $level = $this->spam_file($filename);
162 if( $level > 2 ){
163 $link = '<a href="' . admin_url('admin.php?page=wpforo-tools&tab=antispam#spam-files') . '"><strong>&gt;&gt;</strong></a>';
164 $phrase = '<strong>SPAM! - </strong>' . sprintf( __('Probably spam file attachments have been detected by wpForo Spam Control. Please moderate suspected files here %s', 'wpforo'), $link);
165 $this->wpforo->notice->add( $phrase, 'error' );
166 return true;
167 }
168 }
169 }
170 }
171 return false;
172 }
173
174 public function spam_file( $item, $type = 'file' ){
175 if( !isset($item) || !$item ) return false;
176 $level = 0;
177 $item = strtolower($item);
178 $spam_file_phrases = array(
179 0 => array( 'watch', 'movie'),
180 1 => array( 'download', 'free')
181 );
182 if($type == 'file'){
183 $ext = strtolower(pathinfo($item, PATHINFO_EXTENSION));
184 $ext_risk = array('pdf', 'doc', 'docx', 'txt', 'htm', 'html', 'rtf', 'xml', 'xls', 'xlsx', 'php', 'cgi');
185 $ext_high_risk = array('php', 'cgi', 'exe');
186 if( in_array($ext, $ext_risk) ){
187 $has_post = $this->wpforo->db->get_var( "SELECT `postid` FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `body` LIKE '%" . esc_sql( $item ) . "%' LIMIT 1" );
188 foreach($spam_file_phrases as $phrases){
189 foreach($phrases as $phrase){
190 if( strpos($item, $phrase) !== FALSE ){
191 if( !$has_post ){
192 $level = 4; break 2;
193 }
194 else{
195 $level = 2; break 2;
196 }
197 }
198 }
199 }
200 if( !$level ){
201 if( !$has_post ){
202 $level = 3;
203 }
204 else{
205 if( in_array($ext, $ext_high_risk) ){
206 $level = 5;
207 }
208 else{
209 $level = 1;
210 }
211 }
212 }
213 }
214 return $level;
215 }
216 elseif($type == 'file-open'){
217 $ext = strtolower(pathinfo($item, PATHINFO_EXTENSION));
218 $allow_to_open = array('pdf', 'doc', 'docx', 'txt', 'rtf', 'xls', 'xlsx');
219 if( in_array($ext, $allow_to_open) ){
220 return true;
221 }
222 else{
223 return false;
224 }
225 }
226 return 0;
227 }
228
229 public function spam_topic($topic)
230 {
231 if( empty($topic) ) return $topic;
232 if( isset($topic['title']) ){
233 $item = $topic['title'];
234 }
235 else{
236 return $topic;
237 }
238 $len = wpforo_strlen($item);
239 if( $len < 10 ) return $topic;
240 $item = strip_tags($item);
241 $is_similar = false;
242 $topic_args = array( 'userid' => $topic['userid'] );
243 $topics = $this->wpforo->topic->get_topics($topic_args);
244 $sc_level = ( isset($this->wpforo->tools_antispam['spam_filter_level_topic'])) ? intval($this->wpforo->tools_antispam['spam_filter_level_topic']) : 100;
245 if( $sc_level > 100 ) $sc_level = 60; $sc_level = (101 - $sc_level);
246 if( !empty($topics) ){
247 $count = count($topics);
248 $keys[0] = array_rand($topics); if( $count > 1) $keys[1] = array_rand($topics);
249 $check_1 = (isset($keys[0])) ? strip_tags($topics[$keys[0]]['title']) : '';
250 $check_2 = (isset($keys[1])) ? strip_tags($topics[$keys[1]]['title']) : '';
251 if($check_1){ similar_text($item, $check_1, $percent); if( $percent > $sc_level ) $is_similar = true; }
252 if($check_2 && !$is_similar){ similar_text($item, $check_2, $percent); if( $percent > $sc_level ) $is_similar = true; }
253 if( $is_similar ){
254 $this->ban_for_spam( $this->wpforo->current_userid );
255 $topic['status'] = 1;
256 }
257 }
258 return $topic;
259 }
260
261 public function spam_post($post)
262 {
263 if( empty($post) ) return $post;
264 if( isset($post['body']) ){
265 $item = $post['body'];
266 }
267 else{
268 return $post;
269 }
270
271 // $len = wpforo_strlen($item);
272 $item = strip_tags($item);
273 $is_similar = false;
274 $post_args = array( 'userid' => $post['userid'] );
275 $posts = $this->wpforo->post->get_posts($post_args);
276 $sc_level = ( isset($this->wpforo->tools_antispam['spam_filter_level_post'])) ? intval($this->wpforo->tools_antispam['spam_filter_level_post']) : 100;
277 if( $sc_level > 100 ) $sc_level = 70; $sc_level = (101 - $sc_level);
278 if( !empty($posts) ){
279 $count = count($posts);
280 $keys[0] = array_rand($posts); if( $count > 1) $keys[1] = array_rand($posts);
281 $check_1 = (isset($keys[0])) ? strip_tags($posts[$keys[0]]['body']) : '';
282 $check_2 = (isset($keys[1])) ? strip_tags($posts[$keys[1]]['body']) : '';
283 if($check_1){ similar_text($item, $check_1, $percent); if( isset($percent) && $percent > $sc_level ) $is_similar = true; }
284 if($check_2 && !$is_similar){ similar_text($item, $check_2, $percent); if( isset($percent) && $percent > $sc_level ) $is_similar = true; }
285 if( $is_similar ){
286 $this->ban_for_spam( $this->wpforo->current_userid );
287 $post['status'] = 1;
288 }
289 }
290 return $post;
291 }
292
293 public function auto_moderate($item){
294
295 if( empty($item) ) return $item;
296 if( $this->wpforo->perm->usergroup_can( 'em' ) ){
297 $item['status'] = 0;
298 return $item;
299 }
300 if( !$this->wpforo->perm->usergroup_can( 'aup' ) ){
301 $item['status'] = 1;
302 return $item;
303 }
304
305 if( $this->wpforo->member->current_user_is_new() ){
306 if( ( isset($item['status']) && $item['status'] == 1 ) || $this->has_unapproved( $this->wpforo->current_userid ) ){
307 $this->set_all_unapproved( $this->wpforo->current_userid );
308 $item['status'] = 1;
309 }
310 if( isset($item['body']) && isset($item['title']) && ( $this->has_link($item['body']) || $this->has_link($item['title']) ) ){
311 $item['status'] = 1;
312 }
313 }
314 else{
315 if( !$this->has_approved( $this->wpforo->current_userid ) ){
316 $item['status'] = 1;
317 }
318 }
319
320 return $item;
321 }
322
323 public function has_approved($user){
324 if( empty($user) ) return false;
325 if( isset($user['ID']) ){
326 $userid = intval($user['ID']);
327 }
328 else{
329 $userid = intval($user);
330 }
331 $has_approved_post = $this->wpforo->db->get_var( "SELECT `postid` FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `userid` = '" . intval($userid) . "' AND `status` = 0 LIMIT 1" );
332 if( $has_approved_post ){
333 return true;
334 }
335 else{
336 return false;
337 }
338 }
339
340 public function has_unapproved($user){
341 if( empty($user) ) return false;
342 if( isset($user['ID']) ){
343 $userid = intval($user['ID']);
344 }
345 else{
346 $userid = intval($user);
347 }
348 $has_unapproved_post = $this->wpforo->db->get_var( "SELECT `postid` FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `userid` = '" . intval($userid) . "' AND `status` = 1 LIMIT 1" );
349 if( $has_unapproved_post ){
350 return true;
351 }
352 else{
353 return false;
354 }
355 }
356
357 public function ban_for_spam( $userid ){
358 if ( isset($userid) && $this->wpforo->tools_antispam['spam_user_ban'] ) {
359 if( !$this->has_approved( $this->wpforo->current_userid ) ){
360 $this->wpforo->member->autoban( $userid );
361 }
362 }
363 }
364
365 public function set_all_unapproved( $userid ){
366 if ( isset($userid) ) {
367 $this->wpforo->db->update( $this->wpforo->db->prefix."wpforo_topics", array('status' => 1), array('userid' => intval($userid)), array('%d'), array('%d'));
368 $this->wpforo->db->update( $this->wpforo->db->prefix."wpforo_posts", array('status' => 1), array('userid' => intval($userid)), array('%d'), array('%d'));
369 }
370 }
371
372 public function remove_links( $item ){
373 if( isset($item['body']) && $item['body'] ){
374 $item['body'] = preg_replace('/((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z0-9\&\.\/\?\:@\-_=#])*/is', ' <span style="color:#aaa;">' . wpforo_phrase('removed link', false, false) . '</span> ', $item['body']);
375 }
376 if( isset($item['title']) && $item['title'] ){
377 if(preg_match('/((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z0-9\&\.\/\?\:@\-_=#])*/is', $item['title'] )){
378 $item['title'] = preg_replace('/((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z0-9\&\.\/\?\:@\-_=#])*/is', ' -' . wpforo_phrase('removed link', false, false) . '- ', $item['title']);
379 }
380 }
381 return $item;
382 }
383
384 public function has_link( $content ){
385 if( preg_match('/((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z0-9\&\.\/\?\:@\-_=#])*/is', $content ) ){
386 return true;
387 }
388 return false;
389 }
390
391 }