Dotclear

source: plugins/antispam/filters/class.dc.filter.words.php @ 1454:e64273de06ad

Revision 1454:e64273de06ad, 8.3 KB checked in by Anne Kozlika <kozlika@…>, 11 years ago (diff)

Let labels be labels, the end (I hope so).

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

Sites map