source: extensions/stop_spammers/main.inc.php @ 29761

Last change on this file since 29761 was 27641, checked in by plg, 11 years ago

ability to configure stop_spammers_sfs_threshold (local configuration)

File size: 3.4 KB
Line 
1<?php
2/*
3Plugin Name: Stop Spammers
4Version: auto
5Description: Fight against spammers
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=
7Author: plg
8Author URI: http://le-gall.net/pierrick
9*/
10
11if (!defined('PHPWG_ROOT_PATH'))
12{
13  die('Hacking attempt!');
14}
15
16global $prefixeTable;
17
18// +-----------------------------------------------------------------------+
19// | Define plugin constants                                               |
20// +-----------------------------------------------------------------------+
21
22defined('STOP_SPAMMERS_ID') or define('STOP_SPAMMERS_ID', basename(dirname(__FILE__)));
23define('STOP_SPAMMERS_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');
24define('STOP_SPAMMERS_TABLE', $prefixeTable.'stop_spammers');
25define('STOP_SPAMMERS_VERSION', 'auto');
26
27// init the plugin
28add_event_handler('init', 'stop_spammers_init');
29/**
30 * plugin initialization
31 *   - check for upgrades
32 *   - unserialize configuration
33 *   - load language
34 */
35function stop_spammers_init()
36{
37  global $conf, $user, $pwg_loaded_plugins;
38
39  // apply upgrade if needed
40  if (
41    STOP_SPAMMERS_VERSION == 'auto' or
42    $pwg_loaded_plugins[STOP_SPAMMERS_ID]['version'] == 'auto' or
43    version_compare($pwg_loaded_plugins[STOP_SPAMMERS_ID]['version'], STOP_SPAMMERS_VERSION, '<')
44  )
45  {
46    // call install function
47    include_once(STOP_SPAMMERS_PATH.'include/install.inc.php');
48    stop_spammers_install();
49
50    // update plugin version in database
51    if ( $pwg_loaded_plugins[STOP_SPAMMERS_ID]['version'] != 'auto' and STOP_SPAMMERS_VERSION != 'auto' )
52    {
53      $query = '
54UPDATE '. PLUGINS_TABLE .'
55SET version = "'. STOP_SPAMMERS_VERSION .'"
56WHERE id = "'. STOP_SPAMMERS_ID .'"';
57      pwg_query($query);
58
59      $pwg_loaded_plugins[STOP_SPAMMERS_ID]['version'] = STOP_SPAMMERS_VERSION;
60    }
61  }
62}
63
64add_event_handler('user_comment_check', 'stop_spammers_checks', EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
65function stop_spammers_checks($action, $comment)
66{
67  if (!stop_spammers_check_stopforumspam())
68  {
69    return 'reject';
70  }
71
72  return $action;
73}
74
75function stop_spammers_check_stopforumspam()
76{
77  global $conf;
78
79  if (!isset($conf['stop_spammers_sfs_threshold']))
80  {
81    $conf['stop_spammers_sfs_threshold'] = 50;
82  }
83 
84  $ip = $_SERVER['REMOTE_ADDR'];
85
86  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
87
88  $query = '
89SELECT *
90  FROM '.STOP_SPAMMERS_TABLE.'
91  WHERE ip = \''.$ip.'\'
92;';
93  $blocked = pwg_db_fetch_assoc(pwg_query($query));
94  if (!empty($blocked))
95  {
96    single_update(
97      STOP_SPAMMERS_TABLE,
98      array('last_update' => $dbnow, 'occurrences' => $blocked['occurrences']+1),
99      array('id' => $blocked['id'])
100      );
101
102    return false;
103  }
104
105  // file_put_contents('/tmp/sfs.log', "==== ".date('c')." ".__FUNCTION__.' : '.$ip."\n", FILE_APPEND);
106
107  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
108 
109  $sfs_url = 'http://www.stopforumspam.com/api?ip='.$ip.'&f=serial&confidence';
110  fetchRemote($sfs_url, $result);
111  $result = unserialize($result);
112
113  if (isset($result['ip']['confidence']))
114  {
115    if ($result['ip']['confidence'] > $conf['stop_spammers_sfs_threshold'])
116    {
117      single_insert(
118        STOP_SPAMMERS_TABLE,
119        array(
120          'ip' => $ip,
121          'blocker' => 'stopforumspam',
122          'since' => $dbnow,
123          'last_update' => $dbnow,
124          'occurrences' => 1,
125          )
126        );
127
128      return false;
129    }
130  }
131
132  return true;
133}
134?>
Note: See TracBrowser for help on using the repository browser.