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

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

new plugin to stop spammers

File size: 3.2 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  $ip = $_SERVER['REMOTE_ADDR'];
78
79  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
80
81  $query = '
82SELECT *
83  FROM '.STOP_SPAMMERS_TABLE.'
84  WHERE ip = \''.$ip.'\'
85;';
86  $blocked = pwg_db_fetch_assoc(pwg_query($query));
87  if (!empty($blocked))
88  {
89    single_update(
90      STOP_SPAMMERS_TABLE,
91      array('last_update' => $dbnow, 'occurrences' => $blocked['occurrences']+1),
92      array('id' => $blocked['id'])
93      );
94
95    return false;
96  }
97
98  // file_put_contents('/tmp/sfs.log', "==== ".date('c')." ".__FUNCTION__.' : '.$ip."\n", FILE_APPEND);
99
100  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
101 
102  $sfs_url = 'http://www.stopforumspam.com/api?ip='.$ip.'&f=serial&confidence';
103  fetchRemote($sfs_url, $result);
104  $result = unserialize($result);
105
106  if (isset($result['ip']['confidence']))
107  {
108    if ($result['ip']['confidence'] > 50)
109    {
110      single_insert(
111        STOP_SPAMMERS_TABLE,
112        array(
113          'ip' => $ip,
114          'blocker' => 'stopforumspam',
115          'since' => $dbnow,
116          'last_update' => $dbnow,
117          'occurrences' => 1,
118          )
119        );
120
121      return false;
122    }
123  }
124
125  return true;
126}
127?>
Note: See TracBrowser for help on using the repository browser.