Dotclear

source: plugins/antispam/filters/class.dc.filter.words.php @ 1512:ef727a109ce6

Revision 1512:ef727a109ce6, 8.1 KB checked in by Franck Paul <carnet.franck.paul@…>, 11 years ago (diff)

Merged in pascalchevrel/dotclear/ticket1515 (pull request #46) - Fixes 1515: move hardcoded css from antispam filters to css file

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Antispam, a plugin for Dotclear 2.
5#
6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14class dcFilterWords extends dcSpamFilter
15{
16     public $has_gui = true;
17     public $name = 'Bad Words';
18
19     private $con;
20     private $table;
21
22     public function __construct($core)
23     {
24          parent::__construct($core);
25          $this->con =& $core->con;
26          $this->table = $core->prefix.'spamrule';
27     }
28
29     protected function setInfo()
30     {
31          $this->description = __('Words Blacklist');
32     }
33
34     public function getStatusMessage($status,$comment_id)
35     {
36          return sprintf(__('Filtered by %1$s with word %2$s.'),$this->guiLink(),'<em>'.$status.'</em>');
37     }
38
39     public function isSpam($type,$author,$email,$site,$ip,$content,$post_id,&$status)
40     {
41          $str = $author.' '.$email.' '.$site.' '.$content;
42
43          $rs = $this->getRules();
44
45          while ($rs->fetch())
46          {
47               $word = $rs->rule_content;
48
49               if (substr($word,0,1) == '/' && substr($word,-1,1) == '/') {
50                    $reg = substr(substr($word,1),0,-1);
51               } else {
52                    $reg = preg_quote($word, '/');
53                    $reg = '(^|\s+|>|<)'.$reg.'(>|<|\s+|\.|$)';
54               }
55
56               if (preg_match('/'.$reg.'/msiu',$str)) {
57                    $status = $word;
58                    return true;
59               }
60          }
61     }
62
63     public function gui($url)
64     {
65          $core =& $this->core;
66
67          # Create list
68          if (!empty($_POST['createlist']))
69          {
70               try {
71                    $this->defaultWordsList();
72                    http::redirect($url.'&list=1');
73               } catch (Exception $e) {
74                    $core->error->add($e->getMessage());
75               }
76          }
77
78          # Adding a word
79          if (!empty($_POST['swa']))
80          {
81               $globalsw = !empty($_POST['globalsw']) && $core->auth->isSuperAdmin();
82
83               try {
84                    $this->addRule($_POST['swa'],$globalsw);
85                    http::redirect($url.'&added=1');
86               } catch (Exception $e) {
87                    $core->error->add($e->getMessage());
88               }
89          }
90
91          # Removing spamwords
92          if (!empty($_POST['swd']) && is_array($_POST['swd']))
93          {
94               try {
95                    $this->removeRule($_POST['swd']);
96                    http::redirect($url.'&removed=1');
97               } catch (Exception $e) {
98                    $core->error->add($e->getMessage());
99               }
100          }
101
102          /* DISPLAY
103          ---------------------------------------------- */
104          $res = '';
105
106          if (!empty($_GET['list'])) {
107               $res .= dcPage::message(__('Words have been successfully added.'),true,false,false);
108          }
109          if (!empty($_GET['added'])) {
110               $res .= dcPage::message(__('Word has been successfully added.'),true,false,false);
111          }
112          if (!empty($_GET['removed'])) {
113               $res .= dcPage::message(__('Words have been successfully removed.'),true,false,false);
114          }
115
116          $res .=
117          '<form action="'.html::escapeURL($url).'" method="post" class="fieldset">'.
118          '<p><label class="classic" for="swa">'.__('Add a word ').'</label> '.form::field('swa',20,128);
119
120          if ($core->auth->isSuperAdmin()) {
121               $res .= '<label class="classic" for="globalsw">'.form::checkbox('globalsw',1).'</label> '.
122               __('Global word (used for all blogs)');
123          }
124
125          $res .=
126          $core->formNonce().
127          '</p>'.
128          '<p><input type="submit" value="'.__('Add').'"/></p>'.
129          '</form>';
130
131          $rs = $this->getRules();
132          if ($rs->isEmpty())
133          {
134               $res .= '<p><strong>'.__('No word in list.').'</strong></p>';
135          }
136          else
137          {
138               $res .=
139               '<form action="'.html::escapeURL($url).'" method="post" class="fieldset">'.
140               '<h3>' . __('List of bad words') . '</h3>'.
141               '<div class="antispam">';
142
143               $res_global = '';
144               $res_local = '';
145               while ($rs->fetch())
146               {
147                    $disabled_word = false;
148
149                    $p_style = '';
150
151                    if (!$rs->blog_id) {
152                         $disabled_word = !$core->auth->isSuperAdmin();
153                         $p_style .= ' global';
154                    }
155
156                    $item = '<p class="'.$p_style.'"><label class="classic" for="word-'.$rs->rule_id.'">'.
157                         form::checkbox(array('swd[]', 'word-'.$rs->rule_id),$rs->rule_id,false,'','',$disabled_word).' '.
158                         html::escapeHTML($rs->rule_content).
159                         '</label></p>';
160
161                    if ($rs->blog_id) {
162                         // local list
163                         if ($res_local == '') {
164                              $res_local = '<h4>'.__('Local words (used only for this blog)').'</h4>';
165                         }
166                         $res_local .= $item;
167                    } else {
168                         // global list
169                         if ($res_global == '') {
170                              $res_global = '<h4>'.__('Global words (used for all blogs)').'</h4>';
171                         }
172                         $res_global .= $item;
173                    }
174               }
175               $res .= $res_local.$res_global;
176
177               $res .=
178               '</div>'.
179               '<p>'.form::hidden(array('spamwords'),1).
180               $core->formNonce().
181               '<input class="submit delete" type="submit" value="' . __('Delete selected words') . '"/></p>'.
182               '</form>';
183          }
184
185          if ($core->auth->isSuperAdmin())
186          {
187               $res .=
188               '<form action="'.html::escapeURL($url).'" method="post">'.
189               '<p><input type="submit" value="'.__('Create default wordlist').'" />'.
190               form::hidden(array('spamwords'),1).
191               form::hidden(array('createlist'),1).
192               $core->formNonce().'</p>'.
193               '</form>';
194          }
195
196          return $res;
197     }
198
199     private function getRules()
200     {
201          $strReq = 'SELECT rule_id, blog_id, rule_content '.
202                    'FROM '.$this->table.' '.
203                    "WHERE rule_type = 'word' ".
204                    "AND ( blog_id = '".$this->con->escape($this->core->blog->id)."' ".
205                    "OR blog_id IS NULL ) ".
206                    'ORDER BY blog_id ASC, rule_content ASC ';
207
208          return $this->con->select($strReq);
209     }
210
211     private function addRule($content,$general=false)
212     {
213          $strReq = 'SELECT rule_id FROM '.$this->table.' '.
214                    "WHERE rule_type = 'word' ".
215                    "AND rule_content = '".$this->con->escape($content)."' ";
216          $rs = $this->con->select($strReq);
217
218          if (!$rs->isEmpty()) {
219               throw new Exception(__('This word exists'));
220          }
221
222          $rs = $this->con->select('SELECT MAX(rule_id) FROM '.$this->table);
223          $id = (integer) $rs->f(0) + 1;
224
225          $cur = $this->con->openCursor($this->table);
226          $cur->rule_id = $id;
227          $cur->rule_type = 'word';
228          $cur->rule_content = (string) $content;
229
230          if ($general && $this->core->auth->isSuperAdmin()) {
231               $cur->blog_id = null;
232          } else {
233               $cur->blog_id = $this->core->blog->id;
234          }
235
236          $cur->insert();
237     }
238
239     private function removeRule($ids)
240     {
241          $strReq = 'DELETE FROM '.$this->table.' ';
242
243          if (is_array($ids)) {
244               foreach ($ids as &$v) {
245                    $v = (integer) $v;
246               }
247               $strReq .= 'WHERE rule_id IN ('.implode(',',$ids).') ';
248          } else {
249               $ids = (integer) $ids;
250               $strReq .= 'WHERE rule_id = '.$ids.' ';
251          }
252
253          if (!$this->core->auth->isSuperAdmin()) {
254               $strReq .= "AND blog_id = '".$this->con->escape($this->core->blog->id)."' ";
255          }
256
257          $this->con->execute($strReq);
258     }
259
260     public function defaultWordsList()
261     {
262          $words = array(
263               '/-credit(\s+|$)/',
264               '/-digest(\s+|$)/',
265               '/-loan(\s+|$)/',
266               '/-online(\s+|$)/',
267               '4u',
268               'adipex',
269               'advicer',
270               'ambien',
271               'baccarat',
272               'baccarrat',
273               'blackjack',
274               'bllogspot',
275               'bolobomb',
276               'booker',
277               'byob',
278               'car-rental-e-site',
279               'car-rentals-e-site',
280               'carisoprodol',
281               'cash',
282               'casino',
283               'casinos',
284               'chatroom',
285               'cialis',
286               'craps',
287               'credit-card',
288               'credit-report-4u',
289               'cwas',
290               'cyclen',
291               'cyclobenzaprine',
292               'dating-e-site',
293               'day-trading',
294               'debt',
295               'digest-',
296               'discount',
297               'discreetordering',
298               'duty-free',
299               'dutyfree',
300               'estate',
301               'favourits',
302               'fioricet',
303               'flowers-leading-site',
304               'freenet',
305               'freenet-shopping',
306               'gambling',
307               'gamias',
308               'health-insurancedeals-4u',
309               'holdem',
310               'holdempoker',
311               'holdemsoftware',
312               'holdemtexasturbowilson',
313               'hotel-dealse-site',
314               'hotele-site',
315               'hotelse-site',
316               'incest',
317               'insurance-quotesdeals-4u',
318               'insurancedeals-4u',
319               'jrcreations',
320               'levitra',
321               'macinstruct',
322               'mortgage',
323               'online-gambling',
324               'onlinegambling-4u',
325               'ottawavalleyag',
326               'ownsthis',
327               'palm-texas-holdem-game',
328               'paxil',
329               'pharmacy',
330               'phentermine',
331               'pills',
332               'poker',
333               'poker-chip',
334               'poze',
335               'prescription',
336               'rarehomes',
337               'refund',
338               'rental-car-e-site',
339               'roulette',
340               'shemale',
341               'slot',
342               'slot-machine',
343               'soma',
344               'taboo',
345               'tamiflu',
346               'texas-holdem',
347               'thorcarlson',
348               'top-e-site',
349               'top-site',
350               'tramadol',
351               'trim-spa',
352               'ultram',
353               'v1h',
354               'vacuum',
355               'valeofglamorganconservatives',
356               'viagra',
357               'vicodin',
358               'vioxx',
359               'xanax',
360               'zolus'
361          );
362
363          foreach ($words as $w) {
364               try {
365                    $this->addRule($w,true);
366               } catch (Exception $e) {}
367          }
368     }
369}
370?>
Note: See TracBrowser for help on using the repository browser.

Sites map