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

Last change on this file since 31033 was 30719, checked in by plg, 10 years ago

check IP address on ContactForm

File size: 3.5 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);
65add_event_handler('contact_form_check', 'stop_spammers_checks', EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
66function stop_spammers_checks($action, $comment)
67{
68  global $page;
69 
70  if (!stop_spammers_check_stopforumspam())
71  {
72    $page['errors'][] = l10n('IP address rejected');
73    return 'reject';
74  }
75
76  return $action;
77}
78
79function stop_spammers_check_stopforumspam()
80{
81  global $conf;
82
83  if (!isset($conf['stop_spammers_sfs_threshold']))
84  {
85    $conf['stop_spammers_sfs_threshold'] = 50;
86  }
87 
88  $ip = $_SERVER['REMOTE_ADDR'];
89
90  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
91
92  $query = '
93SELECT *
94  FROM '.STOP_SPAMMERS_TABLE.'
95  WHERE ip = \''.$ip.'\'
96;';
97  $blocked = pwg_db_fetch_assoc(pwg_query($query));
98  if (!empty($blocked))
99  {
100    single_update(
101      STOP_SPAMMERS_TABLE,
102      array('last_update' => $dbnow, 'occurrences' => $blocked['occurrences']+1),
103      array('id' => $blocked['id'])
104      );
105
106    return false;
107  }
108
109  // file_put_contents('/tmp/sfs.log', "==== ".date('c')." ".__FUNCTION__.' : '.$ip."\n", FILE_APPEND);
110
111  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
112 
113  $sfs_url = 'http://www.stopforumspam.com/api?ip='.$ip.'&f=serial&confidence';
114  fetchRemote($sfs_url, $result);
115  $result = unserialize($result);
116
117  if (isset($result['ip']['confidence']))
118  {
119    if ($result['ip']['confidence'] > $conf['stop_spammers_sfs_threshold'])
120    {
121      single_insert(
122        STOP_SPAMMERS_TABLE,
123        array(
124          'ip' => $ip,
125          'blocker' => 'stopforumspam',
126          'since' => $dbnow,
127          'last_update' => $dbnow,
128          'occurrences' => 1,
129          )
130        );
131
132      return false;
133    }
134  }
135
136  return true;
137}
138?>
Note: See TracBrowser for help on using the repository browser.